]> git.saurik.com Git - wxWidgets.git/blob - utils/dialoged/src/reswrite.cpp
Motif wxNotebook about done; added print/preview to OGLEdit sample
[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 #else
36 #include <strstream.h>
37 #include <fstream.h>
38 #endif
39 #else
40 #include <strstream>
41 #include <fstream>
42 #endif
43
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 switch (item->GetValue1())
315 {
316 case wxLB_MULTIPLE:
317 {
318 stream << "'wxLB_MULTIPLE'";
319 break;
320 }
321 case wxLB_EXTENDED:
322 {
323 stream << "'wxLB_EXTENDED'";
324 break;
325 }
326 case wxLB_SINGLE:
327 default:
328 {
329 stream << "'wxLB_SINGLE'";
330 break;
331 }
332 }
333 if (item->GetFont().Ok())
334 {
335 stream << ",\\\n ";
336 OutputFont(stream, item->GetFont());
337 }
338 }
339 else if (itemType == "wxChoice" || itemType == "wxComboBox")
340 {
341 GenerateControlStyleString(itemType, item->GetStyle(), styleBuf);
342
343 stream << item->GetId() << ", " << itemType << ", " << SafeWord(item->GetTitle()) << ", '" << styleBuf << "', ";
344 stream << SafeWord(item->GetName()) << ", " << item->GetX() << ", " << item->GetY() << ", ";
345 stream << item->GetWidth() << ", " << item->GetHeight();
346
347 if (itemType == "wxComboBox")
348 stream << ", " << SafeWord(item->GetValue4());
349
350 // Default list of values
351
352 stream << ", [";
353 if (item->GetStringValues().Number() > 0)
354 {
355 wxNode *node = item->GetStringValues().First();
356 while (node)
357 {
358 char *s = (char *)node->Data();
359 stream << SafeWord(s);
360 if (node->Next())
361 stream << ", ";
362 node = node->Next();
363 }
364 }
365 stream << "]";
366 if (item->GetFont().Ok())
367 {
368 stream << ",\\\n ";
369 OutputFont(stream, item->GetFont());
370 }
371 }
372 else if (itemType == "wxRadioBox")
373 {
374 // Must write out the orientation and number of rows/cols!!
375 GenerateControlStyleString(itemType, item->GetStyle(), styleBuf);
376 stream << item->GetId() << ", " << "wxRadioBox, " << SafeWord(item->GetTitle()) << ", '" << styleBuf << "', ";
377 stream << SafeWord(item->GetName()) << ", " << item->GetX() << ", " << item->GetY() << ", ";
378 stream << item->GetWidth() << ", " << item->GetHeight();
379
380 // Default list of values
381
382 stream << ", [";
383 if (item->GetStringValues().Number() > 0)
384 {
385 wxNode *node = item->GetStringValues().First();
386 while (node)
387 {
388 char *s = (char *)node->Data();
389 stream << SafeWord(s);
390 if (node->Next())
391 stream << ", ";
392 node = node->Next();
393 }
394 }
395 stream << "], " << item->GetValue1();
396 if (item->GetFont().Ok())
397 {
398 stream << ",\\\n ";
399 OutputFont(stream, item->GetFont());
400 }
401 }
402 else if (itemType == "wxBitmap")
403 {
404 stream << "static char *" << item->GetName() << " = \"bitmap(name = '" << item->GetName() << "',\\\n";
405
406 wxNode *node = item->GetChildren().First();
407 while (node)
408 {
409 wxItemResource *child = (wxItemResource *)node->Data();
410 stream << " bitmap = [";
411
412 char buf[400];
413 strcpy(buf, child->GetName());
414 #ifdef __WXMSW__
415 wxDos2UnixFilename(buf);
416 #endif
417
418 stream << "'" << buf << "', ";
419
420 int bitmapType = (int)child->GetValue1();
421 switch (bitmapType)
422 {
423 case wxBITMAP_TYPE_XBM_DATA:
424 {
425 stream << "wxBITMAP_TYPE_XBM_DATA";
426 break;
427 }
428 case wxBITMAP_TYPE_XPM_DATA:
429 {
430 stream << "wxBITMAP_TYPE_XPM_DATA";
431 break;
432 }
433 case wxBITMAP_TYPE_XBM:
434 {
435 stream << "wxBITMAP_TYPE_XBM";
436 break;
437 }
438 case wxBITMAP_TYPE_XPM:
439 {
440 stream << "wxBITMAP_TYPE_XPM";
441 break;
442 }
443 case wxBITMAP_TYPE_BMP:
444 {
445 stream << "wxBITMAP_TYPE_BMP";
446 break;
447 }
448 case wxBITMAP_TYPE_BMP_RESOURCE:
449 {
450 stream << "wxBITMAP_TYPE_BMP_RESOURCE";
451 break;
452 }
453 case wxBITMAP_TYPE_GIF:
454 {
455 stream << "wxBITMAP_TYPE_GIF";
456 break;
457 }
458 case wxBITMAP_TYPE_TIF:
459 {
460 stream << "wxBITMAP_TYPE_TIF";
461 break;
462 }
463 case wxBITMAP_TYPE_ICO:
464 {
465 stream << "wxBITMAP_TYPE_ICO";
466 break;
467 }
468 case wxBITMAP_TYPE_ICO_RESOURCE:
469 {
470 stream << "wxBITMAP_TYPE_ICO_RESOURCE";
471 break;
472 }
473 case wxBITMAP_TYPE_CUR:
474 {
475 stream << "wxBITMAP_TYPE_CUR";
476 break;
477 }
478 case wxBITMAP_TYPE_CUR_RESOURCE:
479 {
480 stream << "wxBITMAP_TYPE_CUR_RESOURCE";
481 break;
482 }
483 default:
484 case wxBITMAP_TYPE_ANY:
485 {
486 stream << "wxBITMAP_TYPE_ANY";
487 break;
488 }
489 }
490 stream << ", ";
491 int platform = child->GetValue2();
492 switch (platform)
493 {
494 case RESOURCE_PLATFORM_WINDOWS:
495 {
496 stream << "'WINDOWS'";
497 break;
498 }
499 case RESOURCE_PLATFORM_X:
500 {
501 stream << "'X'";
502 break;
503 }
504 case RESOURCE_PLATFORM_MAC:
505 {
506 stream << "'MAC'";
507 break;
508 }
509 case RESOURCE_PLATFORM_ANY:
510 {
511 stream << "'ANY'";
512 break;
513 }
514 }
515 int noColours = (int)child->GetValue3();
516 if (noColours > 0)
517 stream << ", " << noColours;
518
519 stream << "]";
520
521 if (node->Next())
522 stream << ",\\\n";
523
524 node = node->Next();
525 }
526 stream << ").\";\n\n";
527 }
528 else
529 {
530 wxString str("Unimplemented resource type: ");
531 str += itemType;
532 wxMessageBox(str);
533 }
534 return TRUE;
535 }
536
537 void wxResourceTableWithSaving::GenerateDialogStyleString(long windowStyle, char *buf)
538 {
539 buf[0] = 0;
540 m_styleTable.GenerateStyleStrings("wxWindow", windowStyle, buf);
541 m_styleTable.GenerateStyleStrings("wxPanel", windowStyle, buf);
542 m_styleTable.GenerateStyleStrings("wxDialog", windowStyle, buf);
543
544 if (strlen(buf) == 0)
545 strcat(buf, "0");
546 }
547
548 void wxResourceTableWithSaving::GeneratePanelStyleString(long windowStyle, char *buf)
549 {
550 buf[0] = 0;
551 m_styleTable.GenerateStyleStrings("wxWindow", windowStyle, buf);
552 m_styleTable.GenerateStyleStrings("wxPanel", windowStyle, buf);
553
554 if (strlen(buf) == 0)
555 strcat(buf, "0");
556 }
557
558
559 void wxResourceTableWithSaving::GenerateControlStyleString(const wxString& windowClass, long windowStyle, char *buf)
560 {
561 buf[0] = 0;
562 m_styleTable.GenerateStyleStrings("wxWindow", windowStyle, buf);
563 m_styleTable.GenerateStyleStrings("wxControl", windowStyle, buf);
564 m_styleTable.GenerateStyleStrings(windowClass, windowStyle, buf);
565
566 if (strlen(buf) == 0)
567 strcat(buf, "0");
568 }
569
570 // Returns quoted string or "NULL"
571 char *SafeString(const wxString& s)
572 {
573 if (s == "")
574 return "NULL";
575 else
576 {
577 strcpy(wxBuffer, "\"");
578 strcat(wxBuffer, s);
579 strcat(wxBuffer, "\"");
580 return wxBuffer;
581 }
582 }
583
584 // Returns quoted string or ''
585 char *SafeWord(const wxString& s)
586 {
587 if (s == "")
588 return "''";
589 else
590 {
591 strcpy(wxBuffer, "'");
592 strcat(wxBuffer, (const char*) s);
593 strcat(wxBuffer, "'");
594 return wxBuffer;
595 }
596 }
597