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