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