]> git.saurik.com Git - wxWidgets.git/blame - utils/dialoged/src/reswrite.cpp
wxTimer was in the wrong place in the demo
[wxWidgets.git] / utils / dialoged / src / reswrite.cpp
CommitLineData
457814b5
JS
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
457814b5
JS
32#include "wx/scrolbar.h"
33#include "wx/string.h"
e70f5e13
RR
34#include "wx/wfstream.h"
35#include "wx/txtstrm.h"
457814b5
JS
36
37#include "reseditr.h"
38
9f4de6b2 39#if ( !defined(__WXMSW__) && defined(__WXMAC__) ) || defined( __VMS )
c246bd7c 40static char wxBuffer[512];
777dbdc7 41#endif
c246bd7c 42
457814b5 43char *SafeString(char *s);
fd71308f 44char *SafeWord(const wxString& s);
457814b5
JS
45
46// Save an association between the child resource and the panel item, to allow
47// us not to require unique window names.
fd71308f 48wxControl *wxResourceTableWithSaving::CreateItem(wxPanel *panel, const wxItemResource *childResource, const wxItemResource* parentResource)
457814b5 49{
f6bcfd97
BP
50 wxControl *item = wxResourceTable::CreateItem(panel, childResource, parentResource);
51 if (item)
52 wxResourceManager::GetCurrentResourceManager()->GetResourceAssociations().Put((long)childResource, item);
53 return item;
457814b5
JS
54}
55
e70f5e13 56void wxResourceTableWithSaving::OutputFont(wxTextOutputStream& stream, const wxFont& font)
457814b5 57{
f6bcfd97
BP
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 << "]";
457814b5
JS
66}
67
68/*
f6bcfd97
BP
69* Resource table with saving (basic one only has loading)
70*/
71
457814b5
JS
72bool wxResourceTableWithSaving::Save(const wxString& filename)
73{
f6bcfd97
BP
74 wxFileOutputStream file_output( filename );
75 if (file_output.LastError())
76 return FALSE;
e70f5e13 77
f6bcfd97 78 wxTextOutputStream stream( file_output );
457814b5 79
f6bcfd97
BP
80 BeginFind();
81 wxNode *node = NULL;
82 while ((node = Next()))
457814b5 83 {
f6bcfd97
BP
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 }
457814b5 92 }
f6bcfd97 93 return TRUE;
457814b5
JS
94}
95
e70f5e13 96bool wxResourceTableWithSaving::SaveResource(wxTextOutputStream& stream, wxItemResource* item, wxItemResource* parentItem)
457814b5 97{
f6bcfd97
BP
98 char styleBuf[400];
99 wxString itemType(item->GetType());
100
101 if (itemType == "wxDialogBox" || itemType == "wxDialog" || itemType == "wxPanel")
457814b5 102 {
f6bcfd97 103 if (itemType == "wxDialogBox" || itemType == "wxDialog")
457814b5 104 {
f6bcfd97
BP
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);
457814b5 112 }
457814b5 113
f6bcfd97
BP
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();
457814b5 119
f6bcfd97
BP
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";
457814b5 170 }
f6bcfd97 171 else if (itemType == "wxButton" || itemType == "wxBitmapButton")
457814b5 172 {
f6bcfd97
BP
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 }
457814b5 184 }
f6bcfd97 185 else if (itemType == "wxStaticText" || itemType == "wxStaticBitmap")
457814b5 186 {
f6bcfd97
BP
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 }
457814b5 198 }
f6bcfd97 199 else if (itemType == "wxCheckBox")
457814b5 200 {
f6bcfd97
BP
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 }
457814b5 211 }
f6bcfd97 212 else if (itemType == "wxRadioButton")
03f68f12 213 {
f6bcfd97
BP
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 }
03f68f12 224 }
f6bcfd97 225 else if (itemType == "wxStaticBox")
457814b5 226 {
f6bcfd97
BP
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 }
457814b5 236 }
f6bcfd97 237 else if (itemType == "wxText" || itemType == "wxMultiText" || itemType == "wxTextCtrl")
457814b5 238 {
f6bcfd97
BP
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 }
457814b5 250 }
f6bcfd97 251 else if (itemType == "wxGauge")
457814b5 252 {
f6bcfd97
BP
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 }
457814b5 263 }
f6bcfd97 264 else if (itemType == "wxSlider")
457814b5 265 {
f6bcfd97
BP
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 }
457814b5 276 }
f6bcfd97 277 else if (itemType == "wxScrollBar")
457814b5 278 {
f6bcfd97
BP
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();
457814b5 285 }
f6bcfd97 286 else if (itemType == "wxListBox")
457814b5 287 {
f6bcfd97
BP
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)
457814b5 297 {
f6bcfd97
BP
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 }
457814b5 307 }
f6bcfd97
BP
308 stream << "]";
309 /* Styles are now in the window style, not in a separate arg
310 stream << ", ";
311 switch (item->GetValue1())
312 {
457814b5
JS
313 case wxLB_MULTIPLE:
314 {
f6bcfd97
BP
315 stream << "'wxLB_MULTIPLE'";
316 break;
457814b5
JS
317 }
318 case wxLB_EXTENDED:
319 {
f6bcfd97
BP
320 stream << "'wxLB_EXTENDED'";
321 break;
457814b5
JS
322 }
323 case wxLB_SINGLE:
324 default:
325 {
f6bcfd97
BP
326 stream << "'wxLB_SINGLE'";
327 break;
457814b5 328 }
f6bcfd97
BP
329 }
330 */
331
332 if (item->GetFont().Ok())
457814b5 333 {
f6bcfd97
BP
334 stream << ",\\\n ";
335 OutputFont(stream, item->GetFont());
457814b5 336 }
457814b5 337 }
f6bcfd97 338 else if (itemType == "wxChoice" || itemType == "wxComboBox")
457814b5 339 {
f6bcfd97
BP
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)
457814b5 353 {
f6bcfd97
BP
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());
457814b5 369 }
457814b5 370 }
f6bcfd97 371 else if (itemType == "wxRadioBox")
457814b5 372 {
f6bcfd97
BP
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();
457814b5 378
f6bcfd97 379 // Default list of values
457814b5 380
f6bcfd97
BP
381 stream << ", [";
382 if (item->GetStringValues().Number() > 0)
457814b5 383 {
f6bcfd97
BP
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 }
457814b5 393 }
f6bcfd97
BP
394 stream << "], " << item->GetValue1();
395 if (item->GetFont().Ok())
457814b5 396 {
f6bcfd97
BP
397 stream << ",\\\n ";
398 OutputFont(stream, item->GetFont());
457814b5 399 }
f6bcfd97
BP
400 }
401 else if (itemType == "wxBitmap")
402 {
403 stream << "static char *" << item->GetName() << " = \"bitmap(name = '" << item->GetName() << "',\\\n";
457814b5 404
f6bcfd97
BP
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();
457814b5
JS
524 }
525 stream << ").\";\n\n";
526 }
9c331ded
JS
527 else
528 {
529 wxString str("Unimplemented resource type: ");
530 str += itemType;
531 wxMessageBox(str);
532 }
f6bcfd97 533 return TRUE;
457814b5
JS
534}
535
457814b5
JS
536void wxResourceTableWithSaving::GenerateDialogStyleString(long windowStyle, char *buf)
537{
f6bcfd97
BP
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");
457814b5
JS
545}
546
547void wxResourceTableWithSaving::GeneratePanelStyleString(long windowStyle, char *buf)
548{
f6bcfd97
BP
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");
457814b5
JS
555}
556
457814b5 557
bbcdf8bc 558void wxResourceTableWithSaving::GenerateControlStyleString(const wxString& windowClass, long windowStyle, char *buf)
457814b5 559{
f6bcfd97
BP
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");
457814b5
JS
567}
568
457814b5 569// Returns quoted string or "NULL"
fd71308f 570char *SafeString(const wxString& s)
457814b5 571{
f6bcfd97
BP
572 if (s == "")
573 return "NULL";
574 else
575 {
576 strcpy(wxBuffer, "\"");
577 strcat(wxBuffer, s);
578 strcat(wxBuffer, "\"");
579 return wxBuffer;
580 }
457814b5
JS
581}
582
8f19c916 583// Returns quoted string or '' : convert " to \"
fd71308f 584char *SafeWord(const wxString& s)
457814b5 585{
f6bcfd97
BP
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 }
457814b5
JS
613}
614