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