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