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(const wxString
& 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
, const wxItemResource
*childResource
, const wxItemResource
* parentResource
)
52 wxControl
*item
= wxResourceTable::CreateItem(panel
, childResource
, parentResource
);
54 wxResourceManager::GetCurrentResourceManager()->GetResourceAssociations().Put((long)childResource
, item
);
58 void wxResourceTableWithSaving::OutputFont(ostream
& stream
, const 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());
82 while ((node
= Next()))
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
, (wxItemResource
*) NULL
))
96 bool wxResourceTableWithSaving::SaveResource(ostream
& stream
, wxItemResource
* item
, wxItemResource
* parentItem
)
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 GenerateDialogStyleString(item
->GetStyle(), styleBuf
);
114 stream
<< " style = '" << styleBuf
<< "',\\\n";
115 stream
<< " title = '" << item
->GetTitle() << "',\\\n";
116 stream
<< " id = " << item
->GetId() << ",\\\n";
117 stream
<< " x = " << item
->GetX() << ", y = " << item
->GetY();
118 stream
<< ", width = " << item
->GetWidth() << ", height = " << item
->GetHeight();
120 if (1) // item->GetStyle() & wxNO_3D)
122 if (item
->GetBackgroundColour().Ok())
125 wxDecToHex(item
->GetBackgroundColour().Red(), buf
);
126 wxDecToHex(item
->GetBackgroundColour().Green(), buf
+2);
127 wxDecToHex(item
->GetBackgroundColour().Blue(), buf
+4);
130 stream
<< ",\\\n " << "background_colour = '" << buf
<< "'";
136 if ((item
->GetResourceStyle() & wxRESOURCE_DIALOG_UNITS
) != 0)
138 if ((item
->GetResourceStyle() & wxRESOURCE_USE_DEFAULTS
) != 0)
141 stream
<< ",\\\n " << "use_dialog_units = " << dialogUnits
;
142 stream
<< ",\\\n " << "use_system_defaults = " << useDefaults
;
144 if (item
->GetFont().Ok())
146 stream
<< ",\\\n font = ";
147 OutputFont(stream
, item
->GetFont());
150 if (item
->GetChildren().Number() > 0)
154 wxNode
*node
= item
->GetChildren().First();
157 wxItemResource
*child
= (wxItemResource
*)node
->Data();
159 stream
<< " control = [";
161 SaveResource(stream
, child
, item
);
169 stream
<< ").\";\n\n";
171 else if (itemType
== "wxButton" || itemType
== "wxBitmapButton")
173 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
174 stream
<< item
->GetId() << ", " << itemType
<< ", " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
175 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
176 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
177 if (item
->GetValue4())
178 stream
<< ", '" << item
->GetValue4() << "'";
179 if (item
->GetFont().Ok())
182 OutputFont(stream
, item
->GetFont());
185 else if (itemType
== "wxStaticText" || itemType
== "wxStaticBitmap")
187 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
188 stream
<< item
->GetId() << ", " << itemType
<< ", " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
189 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
190 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
191 if (item
->GetValue4())
192 stream
<< ", '" << item
->GetValue4() << "'";
193 if (item
->GetFont().Ok())
196 OutputFont(stream
, item
->GetFont());
199 else if (itemType
== "wxCheckBox")
201 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
202 stream
<< item
->GetId() << ", " << "wxCheckBox, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
203 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
204 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
205 stream
<< ", " << item
->GetValue1();
206 if (item
->GetFont().Ok())
209 OutputFont(stream
, item
->GetFont());
212 else if (itemType
== "wxRadioButton")
214 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
215 stream
<< item
->GetId() << ", " << "wxRadioButton, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
216 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
217 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
218 stream
<< ", " << item
->GetValue1();
219 if (item
->GetFont().Ok())
222 OutputFont(stream
, item
->GetFont());
225 else if (itemType
== "wxStaticBox")
227 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
228 stream
<< item
->GetId() << ", " << "wxStaticBox, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
229 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
230 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
231 if (item
->GetFont().Ok())
234 OutputFont(stream
, item
->GetFont());
237 else if (itemType
== "wxText" || itemType
== "wxMultiText" || itemType
== "wxTextCtrl")
239 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
240 stream
<< item
->GetId() << ", " << "wxTextCtrl, ";
241 stream
<< SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
242 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
243 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
244 stream
<< ", " << SafeWord(item
->GetValue4());
245 if (item
->GetFont().Ok())
248 OutputFont(stream
, item
->GetFont());
251 else if (itemType
== "wxGauge")
253 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
254 stream
<< item
->GetId() << ", " << "wxGauge, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
255 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
256 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
257 stream
<< ", " << item
->GetValue1() << ", " << item
->GetValue2();
258 if (item
->GetFont().Ok())
261 OutputFont(stream
, item
->GetFont());
264 else if (itemType
== "wxSlider")
266 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
267 stream
<< item
->GetId() << ", " << "wxSlider, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
268 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
269 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
270 stream
<< ", " << item
->GetValue1() << ", " << item
->GetValue2() << ", " << item
->GetValue3();
271 if (item
->GetFont().Ok())
274 OutputFont(stream
, item
->GetFont());
277 else if (itemType
== "wxScrollBar")
279 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
280 stream
<< item
->GetId() << ", " << "wxScrollBar, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
281 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
282 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
283 stream
<< ", " << item
->GetValue1() << ", " << item
->GetValue2() << ", " << item
->GetValue3() << ", ";
284 stream
<< item
->GetValue5();
286 else if (itemType
== "wxListBox")
288 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
289 stream
<< item
->GetId() << ", " << "wxListBox, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
290 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
291 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
293 // Default list of values
296 if (item
->GetStringValues().Number() > 0)
298 wxNode
*node
= item
->GetStringValues().First();
301 char *s
= (char *)node
->Data();
302 stream
<< SafeWord(s
);
309 switch (item
->GetValue1())
313 stream
<< "'wxLB_MULTIPLE'";
318 stream
<< "'wxLB_EXTENDED'";
324 stream
<< "'wxLB_SINGLE'";
328 if (item
->GetFont().Ok())
331 OutputFont(stream
, item
->GetFont());
334 else if (itemType
== "wxChoice" || itemType
== "wxComboBox")
336 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
338 stream
<< item
->GetId() << ", " << itemType
<< ", " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
339 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
340 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
342 if (itemType
== "wxComboBox")
343 stream
<< ", " << SafeWord(item
->GetValue4());
345 // Default list of values
348 if (item
->GetStringValues().Number() > 0)
350 wxNode
*node
= item
->GetStringValues().First();
353 char *s
= (char *)node
->Data();
354 stream
<< SafeWord(s
);
361 if (item
->GetFont().Ok())
364 OutputFont(stream
, item
->GetFont());
367 else if (itemType
== "wxRadioBox")
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();
375 // Default list of values
378 if (item
->GetStringValues().Number() > 0)
380 wxNode
*node
= item
->GetStringValues().First();
383 char *s
= (char *)node
->Data();
384 stream
<< SafeWord(s
);
390 stream
<< "], " << item
->GetValue1();
391 if (item
->GetFont().Ok())
394 OutputFont(stream
, item
->GetFont());
397 else if (itemType
== "wxBitmap")
399 stream
<< "static char *" << item
->GetName() << " = \"bitmap(name = '" << item
->GetName() << "',\\\n";
401 wxNode
*node
= item
->GetChildren().First();
404 wxItemResource
*child
= (wxItemResource
*)node
->Data();
405 stream
<< " bitmap = [";
408 strcpy(buf
, child
->GetName());
410 wxDos2UnixFilename(buf
);
413 stream
<< "'" << buf
<< "', ";
415 int bitmapType
= (int)child
->GetValue1();
418 case wxBITMAP_TYPE_XBM_DATA
:
420 stream
<< "wxBITMAP_TYPE_XBM_DATA";
423 case wxBITMAP_TYPE_XPM_DATA
:
425 stream
<< "wxBITMAP_TYPE_XPM_DATA";
428 case wxBITMAP_TYPE_XBM
:
430 stream
<< "wxBITMAP_TYPE_XBM";
433 case wxBITMAP_TYPE_XPM
:
435 stream
<< "wxBITMAP_TYPE_XPM";
438 case wxBITMAP_TYPE_BMP
:
440 stream
<< "wxBITMAP_TYPE_BMP";
443 case wxBITMAP_TYPE_BMP_RESOURCE
:
445 stream
<< "wxBITMAP_TYPE_BMP_RESOURCE";
448 case wxBITMAP_TYPE_GIF
:
450 stream
<< "wxBITMAP_TYPE_GIF";
453 case wxBITMAP_TYPE_TIF
:
455 stream
<< "wxBITMAP_TYPE_TIF";
458 case wxBITMAP_TYPE_ICO
:
460 stream
<< "wxBITMAP_TYPE_ICO";
463 case wxBITMAP_TYPE_ICO_RESOURCE
:
465 stream
<< "wxBITMAP_TYPE_ICO_RESOURCE";
468 case wxBITMAP_TYPE_CUR
:
470 stream
<< "wxBITMAP_TYPE_CUR";
473 case wxBITMAP_TYPE_CUR_RESOURCE
:
475 stream
<< "wxBITMAP_TYPE_CUR_RESOURCE";
479 case wxBITMAP_TYPE_ANY
:
481 stream
<< "wxBITMAP_TYPE_ANY";
486 int platform
= child
->GetValue2();
489 case RESOURCE_PLATFORM_WINDOWS
:
491 stream
<< "'WINDOWS'";
494 case RESOURCE_PLATFORM_X
:
499 case RESOURCE_PLATFORM_MAC
:
504 case RESOURCE_PLATFORM_ANY
:
510 int noColours
= (int)child
->GetValue3();
512 stream
<< ", " << noColours
;
521 stream
<< ").\";\n\n";
525 wxString
str("Unimplemented resource type: ");
532 void wxResourceTableWithSaving::GenerateDialogStyleString(long windowStyle
, char *buf
)
535 m_styleTable
.GenerateStyleStrings("wxWindow", windowStyle
, buf
);
536 m_styleTable
.GenerateStyleStrings("wxPanel", windowStyle
, buf
);
537 m_styleTable
.GenerateStyleStrings("wxDialog", windowStyle
, buf
);
539 if (strlen(buf
) == 0)
543 void wxResourceTableWithSaving::GeneratePanelStyleString(long windowStyle
, char *buf
)
546 m_styleTable
.GenerateStyleStrings("wxWindow", windowStyle
, buf
);
547 m_styleTable
.GenerateStyleStrings("wxPanel", windowStyle
, buf
);
549 if (strlen(buf
) == 0)
554 void wxResourceTableWithSaving::GenerateControlStyleString(const wxString
& windowClass
, long windowStyle
, char *buf
)
557 m_styleTable
.GenerateStyleStrings("wxWindow", windowStyle
, buf
);
558 m_styleTable
.GenerateStyleStrings("wxControl", windowStyle
, buf
);
559 m_styleTable
.GenerateStyleStrings(windowClass
, windowStyle
, buf
);
561 if (strlen(buf
) == 0)
565 // Returns quoted string or "NULL"
566 char *SafeString(const wxString
& s
)
572 strcpy(wxBuffer
, "\"");
574 strcat(wxBuffer
, "\"");
579 // Returns quoted string or ''
580 char *SafeWord(const wxString
& s
)
586 strcpy(wxBuffer
, "'");
587 strcat(wxBuffer
, (const char*) s
);
588 strcat(wxBuffer
, "'");