1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Resource writing functionality
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
32 #if defined(__WINDOWS__) && !defined(__GNUWIN32__)
35 #include <strstream.h>
40 #include "wx/scrolbar.h"
41 #include "wx/string.h"
45 char *SafeString(char *s
);
46 char *SafeWord(char *s
);
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
)
52 wxControl
*item
= wxResourceTable::CreateItem(panel
, childResource
);
54 wxResourceManager::currentResourceManager
->GetResourceAssociations().Put((long)childResource
, item
);
58 void wxResourceTableWithSaving::OutputFont(ostream
& stream
, wxFont
*font
)
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() << "'";
71 * Resource table with saving (basic one only has loading)
74 bool wxResourceTableWithSaving::Save(const wxString
& filename
)
76 ofstream
stream(((wxString
&) filename
).GetData());
84 wxItemResource
*item
= (wxItemResource
*)node
->Data();
85 wxString
resType(item
->GetType());
87 if (resType
== "wxDialogBox" || resType
== "wxDialog" || resType
== "wxPanel" || resType
== "wxBitmap")
89 if (!SaveResource(stream
, item
))
96 bool wxResourceTableWithSaving::SaveResource(ostream
& stream
, wxItemResource
*item
)
99 wxString
itemType(item
->GetType());
101 if (itemType
== "wxDialogBox" || itemType
== "wxDialog" || itemType
== "wxPanel")
103 if (itemType
== "wxDialogBox" || itemType
== "wxDialog")
105 stream
<< "static char *" << item
->GetName() << " = \"dialog(name = '" << item
->GetName() << "',\\\n";
106 GenerateDialogStyleString(item
->GetStyle(), styleBuf
);
110 stream
<< "static char *" << item
->GetName() << " = \"panel(name = '" << item
->GetName() << "',\\\n";
111 GeneratePanelStyleString(item
->GetStyle(), styleBuf
);
113 stream
<< " style = '" << styleBuf
<< "',\\\n";
114 stream
<< " title = '" << item
->GetTitle() << "',\\\n";
115 stream
<< " x = " << item
->GetX() << ", y = " << item
->GetY();
116 stream
<< ", width = " << item
->GetWidth() << ", height = " << item
->GetHeight() << ",\\\n";
117 stream
<< " modal = " << item
->GetValue1();
119 if (item
->GetStyle() & wxUSER_COLOURS
)
121 if (item
->GetBackgroundColour())
124 wxDecToHex(item
->GetBackgroundColour()->Red(), buf
);
125 wxDecToHex(item
->GetBackgroundColour()->Green(), buf
+2);
126 wxDecToHex(item
->GetBackgroundColour()->Blue(), buf
+4);
129 stream
<< ",\\\n " << "background_colour = '" << buf
<< "'";
131 if (item
->GetLabelColour())
134 wxDecToHex(item
->GetLabelColour()->Red(), buf
);
135 wxDecToHex(item
->GetLabelColour()->Green(), buf
+2);
136 wxDecToHex(item
->GetLabelColour()->Blue(), buf
+4);
139 stream
<< ",\\\n " << "label_colour = '" << buf
<< "'";
141 if (item
->GetButtonColour())
144 wxDecToHex(item
->GetButtonColour()->Red(), buf
);
145 wxDecToHex(item
->GetButtonColour()->Green(), buf
+2);
146 wxDecToHex(item
->GetButtonColour()->Blue(), buf
+4);
149 stream
<< ",\\\n " << "button_colour = '" << buf
<< "'";
155 stream
<< ",\\\n font = ";
156 OutputFont(stream
, item
->GetFont());
159 if (item->GetButtonFont())
161 stream << ",\\\n button_font = ";
162 OutputFont(stream, item->GetButtonFont());
166 if (item
->GetChildren().Number() > 0)
170 wxNode
*node
= item
->GetChildren().First();
173 wxItemResource
*child
= (wxItemResource
*)node
->Data();
175 stream
<< " control = [";
177 SaveResource(stream
, child
);
185 stream
<< ").\";\n\n";
187 else if (itemType
== "wxButton")
189 GenerateButtonStyleString(item
->GetStyle(), styleBuf
);
190 stream
<< "wxButton, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
191 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
192 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
193 if (item
->GetValue4())
194 stream
<< ", '" << item
->GetValue4() << "'";
198 OutputFont(stream
, item
->GetFont());
201 else if (itemType
== "wxMessage")
203 GenerateMessageStyleString(item
->GetStyle(), styleBuf
);
204 stream
<< "wxMessage, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
205 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
206 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
207 if (item
->GetValue4())
208 stream
<< ", '" << item
->GetValue4() << "'";
212 OutputFont(stream
, item
->GetFont());
215 else if (itemType
== "wxCheckBox")
217 GenerateCheckBoxStyleString(item
->GetStyle(), styleBuf
);
218 stream
<< "wxCheckBox, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
219 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
220 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
221 stream
<< ", " << item
->GetValue1();
225 OutputFont(stream
, item
->GetFont());
228 else if (itemType
== "wxGroupBox")
230 GenerateGroupBoxStyleString(item
->GetStyle(), styleBuf
);
231 stream
<< "wxGroupBox, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
232 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
233 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
237 OutputFont(stream
, item
->GetFont());
240 else if (itemType
== "wxText" || itemType
== "wxMultiText")
242 GenerateTextStyleString(item
->GetStyle(), styleBuf
);
243 stream
<< ((itemType
== "wxText") ? "wxText, " : "wxMultiText, ");
244 stream
<< SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
245 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
246 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
247 stream
<< ", " << SafeWord(item
->GetValue4());
251 OutputFont(stream
, item
->GetFont());
254 else if (itemType
== "wxGauge")
256 GenerateGaugeStyleString(item
->GetStyle(), styleBuf
);
257 stream
<< "wxGauge, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
258 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
259 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
260 stream
<< ", " << item
->GetValue1() << ", " << item
->GetValue2();
264 OutputFont(stream
, item
->GetFont());
267 else if (itemType
== "wxSlider")
269 GenerateSliderStyleString(item
->GetStyle(), styleBuf
);
270 stream
<< "wxSlider, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
271 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
272 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
273 stream
<< ", " << item
->GetValue1() << ", " << item
->GetValue2() << ", " << item
->GetValue3();
277 OutputFont(stream
, item
->GetFont());
280 else if (itemType
== "wxScrollBar")
282 GenerateScrollBarStyleString(item
->GetStyle(), styleBuf
);
283 stream
<< "wxScrollBar, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
284 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
285 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
286 stream
<< ", " << item
->GetValue1() << ", " << item
->GetValue2() << ", " << item
->GetValue3() << ", ";
287 stream
<< item
->GetValue5();
289 else if (itemType
== "wxListBox")
291 GenerateListBoxStyleString(item
->GetStyle(), styleBuf
);
292 stream
<< "wxListBox, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
293 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
294 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
296 // Default list of values
299 if (item
->GetStringValues())
301 wxNode
*node
= item
->GetStringValues()->First();
304 char *s
= (char *)node
->Data();
305 stream
<< SafeWord(s
);
312 switch (item
->GetValue1())
316 stream
<< "'wxLB_MULTIPLE'";
321 stream
<< "'wxLB_EXTENDED'";
327 stream
<< "'wxLB_SINGLE'";
334 OutputFont(stream
, item
->GetFont());
337 else if (itemType
== "wxChoice")
339 GenerateChoiceStyleString(item
->GetStyle(), styleBuf
);
340 stream
<< "wxChoice, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
341 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
342 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
344 // Default list of values
347 if (item
->GetStringValues())
349 wxNode
*node
= item
->GetStringValues()->First();
352 char *s
= (char *)node
->Data();
353 stream
<< SafeWord(s
);
363 OutputFont(stream
, item
->GetFont());
366 else if (itemType
== "wxRadioBox")
368 // Must write out the orientation and number of rows/cols!!
369 GenerateRadioBoxStyleString(item
->GetStyle(), styleBuf
);
370 stream
<< "wxRadioBox, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
371 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
372 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
374 // Default list of values
377 if (item
->GetStringValues())
379 wxNode
*node
= item
->GetStringValues()->First();
382 char *s
= (char *)node
->Data();
383 stream
<< SafeWord(s
);
389 stream
<< "], " << item
->GetValue1();
393 OutputFont(stream
, item
->GetFont());
396 else if (itemType
== "wxBitmap")
398 stream
<< "static char *" << item
->GetName() << " = \"bitmap(name = '" << item
->GetName() << "',\\\n";
400 wxNode
*node
= item
->GetChildren().First();
403 wxItemResource
*child
= (wxItemResource
*)node
->Data();
404 stream
<< " bitmap = [";
407 strcpy(buf
, child
->GetName());
409 wxDos2UnixFilename(buf
);
412 stream
<< "'" << buf
<< "', ";
414 int bitmapType
= (int)child
->GetValue1();
417 case wxBITMAP_TYPE_XBM_DATA
:
419 stream
<< "wxBITMAP_TYPE_XBM_DATA";
422 case wxBITMAP_TYPE_XPM_DATA
:
424 stream
<< "wxBITMAP_TYPE_XPM_DATA";
427 case wxBITMAP_TYPE_XBM
:
429 stream
<< "wxBITMAP_TYPE_XBM";
432 case wxBITMAP_TYPE_XPM
:
434 stream
<< "wxBITMAP_TYPE_XPM";
437 case wxBITMAP_TYPE_BMP
:
439 stream
<< "wxBITMAP_TYPE_BMP";
442 case wxBITMAP_TYPE_BMP_RESOURCE
:
444 stream
<< "wxBITMAP_TYPE_BMP_RESOURCE";
447 case wxBITMAP_TYPE_GIF
:
449 stream
<< "wxBITMAP_TYPE_GIF";
452 case wxBITMAP_TYPE_TIF
:
454 stream
<< "wxBITMAP_TYPE_TIF";
457 case wxBITMAP_TYPE_ICO
:
459 stream
<< "wxBITMAP_TYPE_ICO";
462 case wxBITMAP_TYPE_ICO_RESOURCE
:
464 stream
<< "wxBITMAP_TYPE_ICO_RESOURCE";
467 case wxBITMAP_TYPE_CUR
:
469 stream
<< "wxBITMAP_TYPE_CUR";
472 case wxBITMAP_TYPE_CUR_RESOURCE
:
474 stream
<< "wxBITMAP_TYPE_CUR_RESOURCE";
478 case wxBITMAP_TYPE_ANY
:
480 stream
<< "wxBITMAP_TYPE_ANY";
485 int platform
= child
->GetValue2();
488 case RESOURCE_PLATFORM_WINDOWS
:
490 stream
<< "'WINDOWS'";
493 case RESOURCE_PLATFORM_X
:
498 case RESOURCE_PLATFORM_MAC
:
503 case RESOURCE_PLATFORM_ANY
:
509 int noColours
= (int)child
->GetValue3();
511 stream
<< ", " << noColours
;
520 stream
<< ").\";\n\n";
525 void wxResourceTableWithSaving::GenerateWindowStyleString(long windowStyle
, char *buf
)
527 GenerateStyle(buf
, windowStyle
, wxUSER_COLOURS
, "wxUSER_COLOURS");
528 GenerateStyle(buf
, windowStyle
, wxVSCROLL
, "wxVSCROLL");
529 GenerateStyle(buf
, windowStyle
, wxHSCROLL
, "wxHSCROLL");
530 GenerateStyle(buf
, windowStyle
, wxBORDER
, "wxBORDER");
533 void wxResourceTableWithSaving::GenerateDialogStyleString(long windowStyle
, char *buf
)
536 GenerateWindowStyleString(windowStyle
, buf
);
539 GenerateStyle(buf, windowStyle, wxRETAINED, "wxRETAINED");
541 if (!GenerateStyle(buf
, windowStyle
, wxDEFAULT_DIALOG_STYLE
, "wxDEFAULT_DIALOG_STYLE"))
543 GenerateStyle(buf
, windowStyle
, wxCAPTION
, "wxCAPTION");
544 GenerateStyle(buf
, windowStyle
, wxTHICK_FRAME
, "wxTHICK_FRAME");
545 GenerateStyle(buf
, windowStyle
, wxRESIZE_BORDER
, "wxRESIZE_BORDER");
546 GenerateStyle(buf
, windowStyle
, wxSYSTEM_MENU
, "wxSYSTEM_MENU");
547 GenerateStyle(buf
, windowStyle
, wxMINIMIZE_BOX
, "wxMINIMIZE_BOX");
548 GenerateStyle(buf
, windowStyle
, wxMAXIMIZE_BOX
, "wxMAXIMIZE_BOX");
550 if (strlen(buf
) == 0)
554 void wxResourceTableWithSaving::GeneratePanelStyleString(long windowStyle
, char *buf
)
557 GenerateWindowStyleString(windowStyle
, buf
);
560 GenerateStyle(buf, windowStyle, wxRETAINED, "wxRETAINED");
562 if (strlen(buf
) == 0)
567 void wxResourceTableWithSaving::GenerateItemStyleString(long windowStyle
, char *buf
)
569 GenerateWindowStyleString(windowStyle
, buf
);
571 GenerateStyle(buf
, windowStyle
, wxHORIZONTAL
, "wxHORIZONTAL");
572 GenerateStyle(buf
, windowStyle
, wxVERTICAL
, "wxVERTICAL");
575 void wxResourceTableWithSaving::GenerateRadioBoxStyleString(long windowStyle
, char *buf
)
578 GenerateItemStyleString(windowStyle
, buf
);
580 if (strlen(buf
) == 0)
584 void wxResourceTableWithSaving::GenerateMessageStyleString(long windowStyle
, char *buf
)
587 GenerateItemStyleString(windowStyle
, buf
);
589 if (strlen(buf
) == 0)
593 void wxResourceTableWithSaving::GenerateTextStyleString(long windowStyle
, char *buf
)
596 GenerateItemStyleString(windowStyle
, buf
);
598 GenerateStyle(buf
, windowStyle
, wxTE_PROCESS_ENTER
, "wxTE_PROCESS_ENTER");
599 GenerateStyle(buf
, windowStyle
, wxTE_READONLY
, "wxTE_READONLY");
600 GenerateStyle(buf
, windowStyle
, wxTE_PASSWORD
, "wxTE_PASSWORD");
602 if (strlen(buf
) == 0)
606 void wxResourceTableWithSaving::GenerateButtonStyleString(long windowStyle
, char *buf
)
609 GenerateItemStyleString(windowStyle
, buf
);
611 if (strlen(buf
) == 0)
615 void wxResourceTableWithSaving::GenerateCheckBoxStyleString(long windowStyle
, char *buf
)
618 GenerateItemStyleString(windowStyle
, buf
);
620 if (strlen(buf
) == 0)
624 void wxResourceTableWithSaving::GenerateListBoxStyleString(long windowStyle
, char *buf
)
627 GenerateItemStyleString(windowStyle
, buf
);
629 GenerateStyle(buf
, windowStyle
, wxLB_ALWAYS_SB
, "wxLB_ALWAYS_SB");
630 GenerateStyle(buf
, windowStyle
, wxLB_SORT
, "wxLB_SORT");
631 // GenerateStyle(buf, windowStyle, wxLB_SINGLE, "wxLB_SINGLE"); // Done already
632 GenerateStyle(buf
, windowStyle
, wxLB_MULTIPLE
, "wxLB_MULTIPLE");
633 GenerateStyle(buf
, windowStyle
, wxLB_EXTENDED
, "wxLB_EXTENDED");
635 if (strlen(buf
) == 0)
639 void wxResourceTableWithSaving::GenerateSliderStyleString(long windowStyle
, char *buf
)
642 GenerateItemStyleString(windowStyle
, buf
);
644 if (strlen(buf
) == 0)
648 void wxResourceTableWithSaving::GenerateGroupBoxStyleString(long windowStyle
, char *buf
)
651 GenerateItemStyleString(windowStyle
, buf
);
653 if (strlen(buf
) == 0)
657 void wxResourceTableWithSaving::GenerateGaugeStyleString(long windowStyle
, char *buf
)
660 GenerateItemStyleString(windowStyle
, buf
);
662 GenerateStyle(buf
, windowStyle
, wxGA_PROGRESSBAR
, "wxGA_PROGRESSBAR");
664 if (strlen(buf
) == 0)
668 void wxResourceTableWithSaving::GenerateChoiceStyleString(long windowStyle
, char *buf
)
671 GenerateItemStyleString(windowStyle
, buf
);
673 if (strlen(buf
) == 0)
677 void wxResourceTableWithSaving::GenerateScrollBarStyleString(long windowStyle
, char *buf
)
680 GenerateItemStyleString(windowStyle
, buf
);
682 if (strlen(buf
) == 0)
686 bool wxResourceTableWithSaving::GenerateStyle(char *buf
, long windowStyle
, long flag
, char *strStyle
)
688 if ((windowStyle
& flag
) == flag
)
692 strcat(buf
, strStyle
);
699 // Returns quoted string or "NULL"
700 char *SafeString(char *s
)
706 strcpy(wxBuffer
, "\"");
708 strcat(wxBuffer
, "\"");
713 // Returns quoted string or ''
714 char *SafeWord(char *s
)
720 strcpy(wxBuffer
, "'");
722 strcat(wxBuffer
, "'");