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(__WXMSW__) && !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::GetCurrentResourceManager()->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();
117 // stream << " modal = " << item->GetValue1();
119 if (1) // item->GetStyle() & wxNO_3D)
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
<< "'";
132 if (item
->GetLabelColour())
135 wxDecToHex(item
->GetLabelColour()->Red(), buf
);
136 wxDecToHex(item
->GetLabelColour()->Green(), buf
+2);
137 wxDecToHex(item
->GetLabelColour()->Blue(), buf
+4);
140 stream
<< ",\\\n " << "label_colour = '" << buf
<< "'";
142 if (item
->GetButtonColour())
145 wxDecToHex(item
->GetButtonColour()->Red(), buf
);
146 wxDecToHex(item
->GetButtonColour()->Green(), buf
+2);
147 wxDecToHex(item
->GetButtonColour()->Blue(), buf
+4);
150 stream
<< ",\\\n " << "button_colour = '" << buf
<< "'";
156 if (item
->GetFont() && item
->GetFont()->Ok())
158 stream
<< ",\\\n font = ";
159 OutputFont(stream
, item
->GetFont());
162 if (item
->GetChildren().Number() > 0)
166 wxNode
*node
= item
->GetChildren().First();
169 wxItemResource
*child
= (wxItemResource
*)node
->Data();
171 stream
<< " control = [";
173 SaveResource(stream
, child
);
181 stream
<< ").\";\n\n";
183 else if (itemType
== "wxButton" || itemType
== "wxBitmapButton")
185 GenerateButtonStyleString(item
->GetStyle(), styleBuf
);
186 stream
<< 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() << "'";
194 OutputFont(stream
, item
->GetFont());
197 else if (itemType
== "wxStaticText" || itemType
== "wxStaticBitmap")
199 GenerateMessageStyleString(item
->GetStyle(), styleBuf
);
200 stream
<< itemType
<< ", " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
201 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
202 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
203 if (item
->GetValue4())
204 stream
<< ", '" << item
->GetValue4() << "'";
208 OutputFont(stream
, item
->GetFont());
211 else if (itemType
== "wxCheckBox")
213 GenerateCheckBoxStyleString(item
->GetStyle(), styleBuf
);
214 stream
<< "wxCheckBox, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
215 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
216 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
217 stream
<< ", " << item
->GetValue1();
221 OutputFont(stream
, item
->GetFont());
224 else if (itemType
== "wxStaticBox")
226 GenerateGroupBoxStyleString(item
->GetStyle(), styleBuf
);
227 stream
<< "wxGroupBox, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
228 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
229 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
233 OutputFont(stream
, item
->GetFont());
236 else if (itemType
== "wxText" || itemType
== "wxMultiText" || itemType
== "wxTextCtrl")
238 GenerateTextStyleString(item
->GetStyle(), styleBuf
);
239 stream
<< "wxTextCtrl, ";
240 stream
<< SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
241 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
242 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
243 stream
<< ", " << SafeWord(item
->GetValue4());
247 OutputFont(stream
, item
->GetFont());
250 else if (itemType
== "wxGauge")
252 GenerateGaugeStyleString(item
->GetStyle(), styleBuf
);
253 stream
<< "wxGauge, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
254 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
255 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
256 stream
<< ", " << item
->GetValue1() << ", " << item
->GetValue2();
260 OutputFont(stream
, item
->GetFont());
263 else if (itemType
== "wxSlider")
265 GenerateSliderStyleString(item
->GetStyle(), styleBuf
);
266 stream
<< "wxSlider, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
267 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
268 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
269 stream
<< ", " << item
->GetValue1() << ", " << item
->GetValue2() << ", " << item
->GetValue3();
273 OutputFont(stream
, item
->GetFont());
276 else if (itemType
== "wxScrollBar")
278 GenerateScrollBarStyleString(item
->GetStyle(), styleBuf
);
279 stream
<< "wxScrollBar, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
280 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
281 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
282 stream
<< ", " << item
->GetValue1() << ", " << item
->GetValue2() << ", " << item
->GetValue3() << ", ";
283 stream
<< item
->GetValue5();
285 else if (itemType
== "wxListBox")
287 GenerateListBoxStyleString(item
->GetStyle(), styleBuf
);
288 stream
<< "wxListBox, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
289 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
290 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
292 // Default list of values
295 if (item
->GetStringValues())
297 wxNode
*node
= item
->GetStringValues()->First();
300 char *s
= (char *)node
->Data();
301 stream
<< SafeWord(s
);
308 switch (item
->GetValue1())
312 stream
<< "'wxLB_MULTIPLE'";
317 stream
<< "'wxLB_EXTENDED'";
323 stream
<< "'wxLB_SINGLE'";
330 OutputFont(stream
, item
->GetFont());
333 else if (itemType
== "wxChoice")
335 GenerateChoiceStyleString(item
->GetStyle(), styleBuf
);
336 stream
<< "wxChoice, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
337 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
338 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
340 // Default list of values
343 if (item
->GetStringValues())
345 wxNode
*node
= item
->GetStringValues()->First();
348 char *s
= (char *)node
->Data();
349 stream
<< SafeWord(s
);
359 OutputFont(stream
, item
->GetFont());
362 else if (itemType
== "wxRadioBox")
364 // Must write out the orientation and number of rows/cols!!
365 GenerateRadioBoxStyleString(item
->GetStyle(), styleBuf
);
366 stream
<< "wxRadioBox, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
367 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
368 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
370 // Default list of values
373 if (item
->GetStringValues())
375 wxNode
*node
= item
->GetStringValues()->First();
378 char *s
= (char *)node
->Data();
379 stream
<< SafeWord(s
);
385 stream
<< "], " << item
->GetValue1();
389 OutputFont(stream
, item
->GetFont());
392 else if (itemType
== "wxBitmap")
394 stream
<< "static char *" << item
->GetName() << " = \"bitmap(name = '" << item
->GetName() << "',\\\n";
396 wxNode
*node
= item
->GetChildren().First();
399 wxItemResource
*child
= (wxItemResource
*)node
->Data();
400 stream
<< " bitmap = [";
403 strcpy(buf
, child
->GetName());
405 wxDos2UnixFilename(buf
);
408 stream
<< "'" << buf
<< "', ";
410 int bitmapType
= (int)child
->GetValue1();
413 case wxBITMAP_TYPE_XBM_DATA
:
415 stream
<< "wxBITMAP_TYPE_XBM_DATA";
418 case wxBITMAP_TYPE_XPM_DATA
:
420 stream
<< "wxBITMAP_TYPE_XPM_DATA";
423 case wxBITMAP_TYPE_XBM
:
425 stream
<< "wxBITMAP_TYPE_XBM";
428 case wxBITMAP_TYPE_XPM
:
430 stream
<< "wxBITMAP_TYPE_XPM";
433 case wxBITMAP_TYPE_BMP
:
435 stream
<< "wxBITMAP_TYPE_BMP";
438 case wxBITMAP_TYPE_BMP_RESOURCE
:
440 stream
<< "wxBITMAP_TYPE_BMP_RESOURCE";
443 case wxBITMAP_TYPE_GIF
:
445 stream
<< "wxBITMAP_TYPE_GIF";
448 case wxBITMAP_TYPE_TIF
:
450 stream
<< "wxBITMAP_TYPE_TIF";
453 case wxBITMAP_TYPE_ICO
:
455 stream
<< "wxBITMAP_TYPE_ICO";
458 case wxBITMAP_TYPE_ICO_RESOURCE
:
460 stream
<< "wxBITMAP_TYPE_ICO_RESOURCE";
463 case wxBITMAP_TYPE_CUR
:
465 stream
<< "wxBITMAP_TYPE_CUR";
468 case wxBITMAP_TYPE_CUR_RESOURCE
:
470 stream
<< "wxBITMAP_TYPE_CUR_RESOURCE";
474 case wxBITMAP_TYPE_ANY
:
476 stream
<< "wxBITMAP_TYPE_ANY";
481 int platform
= child
->GetValue2();
484 case RESOURCE_PLATFORM_WINDOWS
:
486 stream
<< "'WINDOWS'";
489 case RESOURCE_PLATFORM_X
:
494 case RESOURCE_PLATFORM_MAC
:
499 case RESOURCE_PLATFORM_ANY
:
505 int noColours
= (int)child
->GetValue3();
507 stream
<< ", " << noColours
;
516 stream
<< ").\";\n\n";
521 void wxResourceTableWithSaving::GenerateWindowStyleString(long windowStyle
, char *buf
)
523 GenerateStyle(buf
, windowStyle
, wxNO_3D
, "wxNO_3D");
524 GenerateStyle(buf
, windowStyle
, wxVSCROLL
, "wxVSCROLL");
525 GenerateStyle(buf
, windowStyle
, wxHSCROLL
, "wxHSCROLL");
526 GenerateStyle(buf
, windowStyle
, wxBORDER
, "wxBORDER");
529 void wxResourceTableWithSaving::GenerateDialogStyleString(long windowStyle
, char *buf
)
532 GenerateWindowStyleString(windowStyle
, buf
);
535 GenerateStyle(buf, windowStyle, wxRETAINED, "wxRETAINED");
537 if (!GenerateStyle(buf
, windowStyle
, wxDEFAULT_DIALOG_STYLE
, "wxDEFAULT_DIALOG_STYLE"))
539 GenerateStyle(buf
, windowStyle
, wxCAPTION
, "wxCAPTION");
540 GenerateStyle(buf
, windowStyle
, wxTHICK_FRAME
, "wxTHICK_FRAME");
541 GenerateStyle(buf
, windowStyle
, wxRESIZE_BORDER
, "wxRESIZE_BORDER");
542 GenerateStyle(buf
, windowStyle
, wxSYSTEM_MENU
, "wxSYSTEM_MENU");
543 GenerateStyle(buf
, windowStyle
, wxMINIMIZE_BOX
, "wxMINIMIZE_BOX");
544 GenerateStyle(buf
, windowStyle
, wxMAXIMIZE_BOX
, "wxMAXIMIZE_BOX");
546 if (strlen(buf
) == 0)
550 void wxResourceTableWithSaving::GeneratePanelStyleString(long windowStyle
, char *buf
)
553 GenerateWindowStyleString(windowStyle
, buf
);
556 GenerateStyle(buf, windowStyle, wxRETAINED, "wxRETAINED");
558 if (strlen(buf
) == 0)
563 void wxResourceTableWithSaving::GenerateItemStyleString(long windowStyle
, char *buf
)
565 GenerateWindowStyleString(windowStyle
, buf
);
567 GenerateStyle(buf
, windowStyle
, wxHORIZONTAL
, "wxHORIZONTAL");
568 GenerateStyle(buf
, windowStyle
, wxVERTICAL
, "wxVERTICAL");
571 void wxResourceTableWithSaving::GenerateRadioBoxStyleString(long windowStyle
, char *buf
)
574 GenerateItemStyleString(windowStyle
, buf
);
576 if (strlen(buf
) == 0)
580 void wxResourceTableWithSaving::GenerateMessageStyleString(long windowStyle
, char *buf
)
583 GenerateItemStyleString(windowStyle
, buf
);
585 if (strlen(buf
) == 0)
589 void wxResourceTableWithSaving::GenerateTextStyleString(long windowStyle
, char *buf
)
592 GenerateItemStyleString(windowStyle
, buf
);
594 GenerateStyle(buf
, windowStyle
, wxTE_PROCESS_ENTER
, "wxTE_PROCESS_ENTER");
595 GenerateStyle(buf
, windowStyle
, wxTE_READONLY
, "wxTE_READONLY");
596 GenerateStyle(buf
, windowStyle
, wxTE_PASSWORD
, "wxTE_PASSWORD");
597 GenerateStyle(buf
, windowStyle
, wxTE_MULTILINE
, "wxTE_MULTILINE");
599 if (strlen(buf
) == 0)
603 void wxResourceTableWithSaving::GenerateButtonStyleString(long windowStyle
, char *buf
)
606 GenerateItemStyleString(windowStyle
, buf
);
608 if (strlen(buf
) == 0)
612 void wxResourceTableWithSaving::GenerateCheckBoxStyleString(long windowStyle
, char *buf
)
615 GenerateItemStyleString(windowStyle
, buf
);
617 if (strlen(buf
) == 0)
621 void wxResourceTableWithSaving::GenerateListBoxStyleString(long windowStyle
, char *buf
)
624 GenerateItemStyleString(windowStyle
, buf
);
626 GenerateStyle(buf
, windowStyle
, wxLB_ALWAYS_SB
, "wxLB_ALWAYS_SB");
627 GenerateStyle(buf
, windowStyle
, wxLB_SORT
, "wxLB_SORT");
628 // GenerateStyle(buf, windowStyle, wxLB_SINGLE, "wxLB_SINGLE"); // Done already
629 GenerateStyle(buf
, windowStyle
, wxLB_MULTIPLE
, "wxLB_MULTIPLE");
630 GenerateStyle(buf
, windowStyle
, wxLB_EXTENDED
, "wxLB_EXTENDED");
632 if (strlen(buf
) == 0)
636 void wxResourceTableWithSaving::GenerateSliderStyleString(long windowStyle
, char *buf
)
639 GenerateItemStyleString(windowStyle
, buf
);
641 if (strlen(buf
) == 0)
645 void wxResourceTableWithSaving::GenerateGroupBoxStyleString(long windowStyle
, char *buf
)
648 GenerateItemStyleString(windowStyle
, buf
);
650 if (strlen(buf
) == 0)
654 void wxResourceTableWithSaving::GenerateGaugeStyleString(long windowStyle
, char *buf
)
657 GenerateItemStyleString(windowStyle
, buf
);
659 GenerateStyle(buf
, windowStyle
, wxGA_PROGRESSBAR
, "wxGA_PROGRESSBAR");
661 if (strlen(buf
) == 0)
665 void wxResourceTableWithSaving::GenerateChoiceStyleString(long windowStyle
, char *buf
)
668 GenerateItemStyleString(windowStyle
, buf
);
670 if (strlen(buf
) == 0)
674 void wxResourceTableWithSaving::GenerateScrollBarStyleString(long windowStyle
, char *buf
)
677 GenerateItemStyleString(windowStyle
, buf
);
679 if (strlen(buf
) == 0)
683 bool wxResourceTableWithSaving::GenerateStyle(char *buf
, long windowStyle
, long flag
, char *strStyle
)
685 if ((windowStyle
& flag
) == flag
)
689 strcat(buf
, strStyle
);
696 // Returns quoted string or "NULL"
697 char *SafeString(char *s
)
703 strcpy(wxBuffer
, "\"");
705 strcat(wxBuffer
, "\"");
710 // Returns quoted string or ''
711 char *SafeWord(char *s
)
717 strcpy(wxBuffer
, "'");
719 strcat(wxBuffer
, "'");