]> git.saurik.com Git - wxWidgets.git/blob - utils/dialoged/src/reswrite.cpp
fixed somebody's poorly done StreamSize-->GetSize transition
[wxWidgets.git] / utils / dialoged / src / reswrite.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: reswrite.cpp
3 // Purpose: Resource writing functionality
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation
14 #endif
15
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/wx.h"
25 #endif
26
27 #include <ctype.h>
28 #include <stdlib.h>
29 #include <math.h>
30 #include <string.h>
31
32 #if wxUSE_IOSTREAMH
33 #if defined(__WXMSW__) && !defined(__GNUWIN32__)
34 #include <strstrea.h>
35 #include <fstream.h>
36 #else
37 #include <strstream.h>
38 #include <fstream.h>
39 #endif
40 #else
41 #include <strstream>
42 #include <fstream>
43 #endif
44
45 #include "wx/scrolbar.h"
46 #include "wx/string.h"
47
48 #include "reseditr.h"
49
50 char *SafeString(char *s);
51 char *SafeWord(const wxString& s);
52
53 // Save an association between the child resource and the panel item, to allow
54 // us not to require unique window names.
55 wxControl *wxResourceTableWithSaving::CreateItem(wxPanel *panel, const wxItemResource *childResource, const wxItemResource* parentResource)
56 {
57 wxControl *item = wxResourceTable::CreateItem(panel, childResource, parentResource);
58 if (item)
59 wxResourceManager::GetCurrentResourceManager()->GetResourceAssociations().Put((long)childResource, item);
60 return item;
61 }
62
63 void wxResourceTableWithSaving::OutputFont(ostream& stream, const wxFont& font)
64 {
65 stream << "[" << font.GetPointSize() << ", '";
66 stream << font.GetFamilyString() << "', '";
67 stream << font.GetStyleString() << "', '";
68 stream << font.GetWeightString() << "', ";
69 stream << (int)font.GetUnderlined();
70 if (font.GetFaceName() != "")
71 stream << ", '" << font.GetFaceName() << "'";
72 stream << "]";
73 }
74
75 /*
76 * Resource table with saving (basic one only has loading)
77 */
78
79 bool wxResourceTableWithSaving::Save(const wxString& filename)
80 {
81 ofstream stream(((wxString &) filename).GetData());
82 if (stream.bad())
83 return FALSE;
84
85 BeginFind();
86 wxNode *node = NULL;
87 while ((node = Next()))
88 {
89 wxItemResource *item = (wxItemResource *)node->Data();
90 wxString resType(item->GetType());
91
92 if (resType == "wxDialogBox" || resType == "wxDialog" || resType == "wxPanel" || resType == "wxBitmap")
93 {
94 if (!SaveResource(stream, item, (wxItemResource*) NULL))
95 return FALSE;
96 }
97 }
98 return TRUE;
99 }
100
101 bool wxResourceTableWithSaving::SaveResource(ostream& stream, wxItemResource* item, wxItemResource* parentItem)
102 {
103 char styleBuf[400];
104 wxString itemType(item->GetType());
105
106 if (itemType == "wxDialogBox" || itemType == "wxDialog" || itemType == "wxPanel")
107 {
108 if (itemType == "wxDialogBox" || itemType == "wxDialog")
109 {
110 stream << "static char *" << item->GetName() << " = \"dialog(name = '" << item->GetName() << "',\\\n";
111 GenerateDialogStyleString(item->GetStyle(), styleBuf);
112 }
113 else
114 {
115 stream << "static char *" << item->GetName() << " = \"panel(name = '" << item->GetName() << "',\\\n";
116 GenerateDialogStyleString(item->GetStyle(), styleBuf);
117 }
118
119 stream << " style = '" << styleBuf << "',\\\n";
120 stream << " title = '" << item->GetTitle() << "',\\\n";
121 stream << " id = " << item->GetId() << ",\\\n";
122 stream << " x = " << item->GetX() << ", y = " << item->GetY();
123 stream << ", width = " << item->GetWidth() << ", height = " << item->GetHeight();
124
125 if (1) // item->GetStyle() & wxNO_3D)
126 {
127 if (item->GetBackgroundColour().Ok())
128 {
129 char buf[7];
130 wxDecToHex(item->GetBackgroundColour().Red(), buf);
131 wxDecToHex(item->GetBackgroundColour().Green(), buf+2);
132 wxDecToHex(item->GetBackgroundColour().Blue(), buf+4);
133 buf[6] = 0;
134
135 stream << ",\\\n " << "background_colour = '" << buf << "'";
136 }
137 }
138
139 int dialogUnits = 0;
140 int useDefaults = 0;
141 if ((item->GetResourceStyle() & wxRESOURCE_DIALOG_UNITS) != 0)
142 dialogUnits = 1;
143 if ((item->GetResourceStyle() & wxRESOURCE_USE_DEFAULTS) != 0)
144 useDefaults = 1;
145
146 stream << ",\\\n " << "use_dialog_units = " << dialogUnits;
147 stream << ",\\\n " << "use_system_defaults = " << useDefaults;
148
149 if (item->GetFont().Ok())
150 {
151 stream << ",\\\n font = ";
152 OutputFont(stream, item->GetFont());
153 }
154
155 if (item->GetChildren().Number() > 0)
156 stream << ",\\\n";
157 else
158 stream << "\\\n";
159 wxNode *node = item->GetChildren().First();
160 while (node)
161 {
162 wxItemResource *child = (wxItemResource *)node->Data();
163
164 stream << " control = [";
165
166 SaveResource(stream, child, item);
167
168 stream << "]";
169
170 if (node->Next())
171 stream << ",\\\n";
172 node = node->Next();
173 }
174 stream << ").\";\n\n";
175 }
176 else if (itemType == "wxButton" || itemType == "wxBitmapButton")
177 {
178 GenerateControlStyleString(itemType, item->GetStyle(), styleBuf);
179 stream << item->GetId() << ", " << itemType << ", " << SafeWord(item->GetTitle()) << ", '" << styleBuf << "', ";
180 stream << SafeWord(item->GetName()) << ", " << item->GetX() << ", " << item->GetY() << ", ";
181 stream << item->GetWidth() << ", " << item->GetHeight();
182 if (item->GetValue4())
183 stream << ", '" << item->GetValue4() << "'";
184 if (item->GetFont().Ok())
185 {
186 stream << ",\\\n ";
187 OutputFont(stream, item->GetFont());
188 }
189 }
190 else if (itemType == "wxStaticText" || itemType == "wxStaticBitmap")
191 {
192 GenerateControlStyleString(itemType, item->GetStyle(), styleBuf);
193 stream << item->GetId() << ", " << itemType << ", " << SafeWord(item->GetTitle()) << ", '" << styleBuf << "', ";
194 stream << SafeWord(item->GetName()) << ", " << item->GetX() << ", " << item->GetY() << ", ";
195 stream << item->GetWidth() << ", " << item->GetHeight();
196 if (item->GetValue4())
197 stream << ", '" << item->GetValue4() << "'";
198 if (item->GetFont().Ok())
199 {
200 stream << ",\\\n ";
201 OutputFont(stream, item->GetFont());
202 }
203 }
204 else if (itemType == "wxCheckBox")
205 {
206 GenerateControlStyleString(itemType, item->GetStyle(), styleBuf);
207 stream << item->GetId() << ", " << "wxCheckBox, " << SafeWord(item->GetTitle()) << ", '" << styleBuf << "', ";
208 stream << SafeWord(item->GetName()) << ", " << item->GetX() << ", " << item->GetY() << ", ";
209 stream << item->GetWidth() << ", " << item->GetHeight();
210 stream << ", " << item->GetValue1();
211 if (item->GetFont().Ok())
212 {
213 stream << ",\\\n ";
214 OutputFont(stream, item->GetFont());
215 }
216 }
217 else if (itemType == "wxRadioButton")
218 {
219 GenerateControlStyleString(itemType, item->GetStyle(), styleBuf);
220 stream << item->GetId() << ", " << "wxRadioButton, " << SafeWord(item->GetTitle()) << ", '" << styleBuf << "', ";
221 stream << SafeWord(item->GetName()) << ", " << item->GetX() << ", " << item->GetY() << ", ";
222 stream << item->GetWidth() << ", " << item->GetHeight();
223 stream << ", " << item->GetValue1();
224 if (item->GetFont().Ok())
225 {
226 stream << ",\\\n ";
227 OutputFont(stream, item->GetFont());
228 }
229 }
230 else if (itemType == "wxStaticBox")
231 {
232 GenerateControlStyleString(itemType, item->GetStyle(), styleBuf);
233 stream << item->GetId() << ", " << "wxStaticBox, " << SafeWord(item->GetTitle()) << ", '" << styleBuf << "', ";
234 stream << SafeWord(item->GetName()) << ", " << item->GetX() << ", " << item->GetY() << ", ";
235 stream << item->GetWidth() << ", " << item->GetHeight();
236 if (item->GetFont().Ok())
237 {
238 stream << ",\\\n ";
239 OutputFont(stream, item->GetFont());
240 }
241 }
242 else if (itemType == "wxText" || itemType == "wxMultiText" || itemType == "wxTextCtrl")
243 {
244 GenerateControlStyleString(itemType, item->GetStyle(), styleBuf);
245 stream << item->GetId() << ", " << "wxTextCtrl, ";
246 stream << SafeWord(item->GetTitle()) << ", '" << styleBuf << "', ";
247 stream << SafeWord(item->GetName()) << ", " << item->GetX() << ", " << item->GetY() << ", ";
248 stream << item->GetWidth() << ", " << item->GetHeight();
249 stream << ", " << SafeWord(item->GetValue4());
250 if (item->GetFont().Ok())
251 {
252 stream << ",\\\n ";
253 OutputFont(stream, item->GetFont());
254 }
255 }
256 else if (itemType == "wxGauge")
257 {
258 GenerateControlStyleString(itemType, item->GetStyle(), styleBuf);
259 stream << item->GetId() << ", " << "wxGauge, " << SafeWord(item->GetTitle()) << ", '" << styleBuf << "', ";
260 stream << SafeWord(item->GetName()) << ", " << item->GetX() << ", " << item->GetY() << ", ";
261 stream << item->GetWidth() << ", " << item->GetHeight();
262 stream << ", " << item->GetValue1() << ", " << item->GetValue2();
263 if (item->GetFont().Ok())
264 {
265 stream << ",\\\n ";
266 OutputFont(stream, item->GetFont());
267 }
268 }
269 else if (itemType == "wxSlider")
270 {
271 GenerateControlStyleString(itemType, item->GetStyle(), styleBuf);
272 stream << item->GetId() << ", " << "wxSlider, " << SafeWord(item->GetTitle()) << ", '" << styleBuf << "', ";
273 stream << SafeWord(item->GetName()) << ", " << item->GetX() << ", " << item->GetY() << ", ";
274 stream << item->GetWidth() << ", " << item->GetHeight();
275 stream << ", " << item->GetValue1() << ", " << item->GetValue2() << ", " << item->GetValue3();
276 if (item->GetFont().Ok())
277 {
278 stream << ",\\\n ";
279 OutputFont(stream, item->GetFont());
280 }
281 }
282 else if (itemType == "wxScrollBar")
283 {
284 GenerateControlStyleString(itemType, item->GetStyle(), styleBuf);
285 stream << item->GetId() << ", " << "wxScrollBar, " << SafeWord(item->GetTitle()) << ", '" << styleBuf << "', ";
286 stream << SafeWord(item->GetName()) << ", " << item->GetX() << ", " << item->GetY() << ", ";
287 stream << item->GetWidth() << ", " << item->GetHeight();
288 stream << ", " << item->GetValue1() << ", " << item->GetValue2() << ", " << item->GetValue3() << ", ";
289 stream << item->GetValue5();
290 }
291 else if (itemType == "wxListBox")
292 {
293 GenerateControlStyleString(itemType, item->GetStyle(), styleBuf);
294 stream << item->GetId() << ", " << "wxListBox, " << SafeWord(item->GetTitle()) << ", '" << styleBuf << "', ";
295 stream << SafeWord(item->GetName()) << ", " << item->GetX() << ", " << item->GetY() << ", ";
296 stream << item->GetWidth() << ", " << item->GetHeight();
297
298 // Default list of values
299
300 stream << ", [";
301 if (item->GetStringValues().Number() > 0)
302 {
303 wxNode *node = item->GetStringValues().First();
304 while (node)
305 {
306 char *s = (char *)node->Data();
307 stream << SafeWord(s);
308 if (node->Next())
309 stream << ", ";
310 node = node->Next();
311 }
312 }
313 stream << "]";
314 /* Styles are now in the window style, not in a separate arg
315 stream << ", ";
316 switch (item->GetValue1())
317 {
318 case wxLB_MULTIPLE:
319 {
320 stream << "'wxLB_MULTIPLE'";
321 break;
322 }
323 case wxLB_EXTENDED:
324 {
325 stream << "'wxLB_EXTENDED'";
326 break;
327 }
328 case wxLB_SINGLE:
329 default:
330 {
331 stream << "'wxLB_SINGLE'";
332 break;
333 }
334 }
335 */
336
337 if (item->GetFont().Ok())
338 {
339 stream << ",\\\n ";
340 OutputFont(stream, item->GetFont());
341 }
342 }
343 else if (itemType == "wxChoice" || itemType == "wxComboBox")
344 {
345 GenerateControlStyleString(itemType, item->GetStyle(), styleBuf);
346
347 stream << item->GetId() << ", " << itemType << ", " << SafeWord(item->GetTitle()) << ", '" << styleBuf << "', ";
348 stream << SafeWord(item->GetName()) << ", " << item->GetX() << ", " << item->GetY() << ", ";
349 stream << item->GetWidth() << ", " << item->GetHeight();
350
351 if (itemType == "wxComboBox")
352 stream << ", " << SafeWord(item->GetValue4());
353
354 // Default list of values
355
356 stream << ", [";
357 if (item->GetStringValues().Number() > 0)
358 {
359 wxNode *node = item->GetStringValues().First();
360 while (node)
361 {
362 char *s = (char *)node->Data();
363 stream << SafeWord(s);
364 if (node->Next())
365 stream << ", ";
366 node = node->Next();
367 }
368 }
369 stream << "]";
370 if (item->GetFont().Ok())
371 {
372 stream << ",\\\n ";
373 OutputFont(stream, item->GetFont());
374 }
375 }
376 else if (itemType == "wxRadioBox")
377 {
378 // Must write out the orientation and number of rows/cols!!
379 GenerateControlStyleString(itemType, item->GetStyle(), styleBuf);
380 stream << item->GetId() << ", " << "wxRadioBox, " << SafeWord(item->GetTitle()) << ", '" << styleBuf << "', ";
381 stream << SafeWord(item->GetName()) << ", " << item->GetX() << ", " << item->GetY() << ", ";
382 stream << item->GetWidth() << ", " << item->GetHeight();
383
384 // Default list of values
385
386 stream << ", [";
387 if (item->GetStringValues().Number() > 0)
388 {
389 wxNode *node = item->GetStringValues().First();
390 while (node)
391 {
392 char *s = (char *)node->Data();
393 stream << SafeWord(s);
394 if (node->Next())
395 stream << ", ";
396 node = node->Next();
397 }
398 }
399 stream << "], " << item->GetValue1();
400 if (item->GetFont().Ok())
401 {
402 stream << ",\\\n ";
403 OutputFont(stream, item->GetFont());
404 }
405 }
406 else if (itemType == "wxBitmap")
407 {
408 stream << "static char *" << item->GetName() << " = \"bitmap(name = '" << item->GetName() << "',\\\n";
409
410 wxNode *node = item->GetChildren().First();
411 while (node)
412 {
413 wxItemResource *child = (wxItemResource *)node->Data();
414 stream << " bitmap = [";
415
416 char buf[400];
417 strcpy(buf, child->GetName());
418 #ifdef __WXMSW__
419 wxDos2UnixFilename(buf);
420 #endif
421
422 stream << "'" << buf << "', ";
423
424 int bitmapType = (int)child->GetValue1();
425 switch (bitmapType)
426 {
427 case wxBITMAP_TYPE_XBM_DATA:
428 {
429 stream << "wxBITMAP_TYPE_XBM_DATA";
430 break;
431 }
432 case wxBITMAP_TYPE_XPM_DATA:
433 {
434 stream << "wxBITMAP_TYPE_XPM_DATA";
435 break;
436 }
437 case wxBITMAP_TYPE_XBM:
438 {
439 stream << "wxBITMAP_TYPE_XBM";
440 break;
441 }
442 case wxBITMAP_TYPE_XPM:
443 {
444 stream << "wxBITMAP_TYPE_XPM";
445 break;
446 }
447 case wxBITMAP_TYPE_BMP:
448 {
449 stream << "wxBITMAP_TYPE_BMP";
450 break;
451 }
452 case wxBITMAP_TYPE_BMP_RESOURCE:
453 {
454 stream << "wxBITMAP_TYPE_BMP_RESOURCE";
455 break;
456 }
457 case wxBITMAP_TYPE_GIF:
458 {
459 stream << "wxBITMAP_TYPE_GIF";
460 break;
461 }
462 case wxBITMAP_TYPE_TIF:
463 {
464 stream << "wxBITMAP_TYPE_TIF";
465 break;
466 }
467 case wxBITMAP_TYPE_ICO:
468 {
469 stream << "wxBITMAP_TYPE_ICO";
470 break;
471 }
472 case wxBITMAP_TYPE_ICO_RESOURCE:
473 {
474 stream << "wxBITMAP_TYPE_ICO_RESOURCE";
475 break;
476 }
477 case wxBITMAP_TYPE_CUR:
478 {
479 stream << "wxBITMAP_TYPE_CUR";
480 break;
481 }
482 case wxBITMAP_TYPE_CUR_RESOURCE:
483 {
484 stream << "wxBITMAP_TYPE_CUR_RESOURCE";
485 break;
486 }
487 default:
488 case wxBITMAP_TYPE_ANY:
489 {
490 stream << "wxBITMAP_TYPE_ANY";
491 break;
492 }
493 }
494 stream << ", ";
495 int platform = child->GetValue2();
496 switch (platform)
497 {
498 case RESOURCE_PLATFORM_WINDOWS:
499 {
500 stream << "'WINDOWS'";
501 break;
502 }
503 case RESOURCE_PLATFORM_X:
504 {
505 stream << "'X'";
506 break;
507 }
508 case RESOURCE_PLATFORM_MAC:
509 {
510 stream << "'MAC'";
511 break;
512 }
513 case RESOURCE_PLATFORM_ANY:
514 {
515 stream << "'ANY'";
516 break;
517 }
518 }
519 int noColours = (int)child->GetValue3();
520 if (noColours > 0)
521 stream << ", " << noColours;
522
523 stream << "]";
524
525 if (node->Next())
526 stream << ",\\\n";
527
528 node = node->Next();
529 }
530 stream << ").\";\n\n";
531 }
532 else
533 {
534 wxString str("Unimplemented resource type: ");
535 str += itemType;
536 wxMessageBox(str);
537 }
538 return TRUE;
539 }
540
541 void wxResourceTableWithSaving::GenerateDialogStyleString(long windowStyle, char *buf)
542 {
543 buf[0] = 0;
544 m_styleTable.GenerateStyleStrings("wxWindow", windowStyle, buf);
545 m_styleTable.GenerateStyleStrings("wxPanel", windowStyle, buf);
546 m_styleTable.GenerateStyleStrings("wxDialog", windowStyle, buf);
547
548 if (strlen(buf) == 0)
549 strcat(buf, "0");
550 }
551
552 void wxResourceTableWithSaving::GeneratePanelStyleString(long windowStyle, char *buf)
553 {
554 buf[0] = 0;
555 m_styleTable.GenerateStyleStrings("wxWindow", windowStyle, buf);
556 m_styleTable.GenerateStyleStrings("wxPanel", windowStyle, buf);
557
558 if (strlen(buf) == 0)
559 strcat(buf, "0");
560 }
561
562
563 void wxResourceTableWithSaving::GenerateControlStyleString(const wxString& windowClass, long windowStyle, char *buf)
564 {
565 buf[0] = 0;
566 m_styleTable.GenerateStyleStrings("wxWindow", windowStyle, buf);
567 m_styleTable.GenerateStyleStrings("wxControl", windowStyle, buf);
568 m_styleTable.GenerateStyleStrings(windowClass, windowStyle, buf);
569
570 if (strlen(buf) == 0)
571 strcat(buf, "0");
572 }
573
574 // Returns quoted string or "NULL"
575 char *SafeString(const wxString& s)
576 {
577 if (s == "")
578 return "NULL";
579 else
580 {
581 strcpy(wxBuffer, "\"");
582 strcat(wxBuffer, s);
583 strcat(wxBuffer, "\"");
584 return wxBuffer;
585 }
586 }
587
588 // Returns quoted string or ''
589 char *SafeWord(const wxString& s)
590 {
591 if (s == "")
592 return "''";
593 else
594 {
595 strcpy(wxBuffer, "'");
596 strcat(wxBuffer, (const char*) s);
597 strcat(wxBuffer, "'");
598 return wxBuffer;
599 }
600 }
601