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