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