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"
33 #if defined(__WXMSW__) && !defined(__GNUWIN32__)
36 #include <strstream.h>
45 #include "wx/scrolbar.h"
46 #include "wx/string.h"
50 char *SafeString(char *s
);
51 char *SafeWord(const wxString
& s
);
53 // Save an association between the child resource and the panel item, to allow
54 // us not to require unique window names.
55 wxControl
*wxResourceTableWithSaving::CreateItem(wxPanel
*panel
, const wxItemResource
*childResource
, const wxItemResource
* parentResource
)
57 wxControl
*item
= wxResourceTable::CreateItem(panel
, childResource
, parentResource
);
59 wxResourceManager::GetCurrentResourceManager()->GetResourceAssociations().Put((long)childResource
, item
);
63 void wxResourceTableWithSaving::OutputFont(ostream
& stream
, const wxFont
& font
)
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() << "'";
76 * Resource table with saving (basic one only has loading)
79 bool wxResourceTableWithSaving::Save(const wxString
& filename
)
81 ofstream
stream(((wxString
&) filename
).GetData());
87 while ((node
= Next()))
89 wxItemResource
*item
= (wxItemResource
*)node
->Data();
90 wxString
resType(item
->GetType());
92 if (resType
== "wxDialogBox" || resType
== "wxDialog" || resType
== "wxPanel" || resType
== "wxBitmap")
94 if (!SaveResource(stream
, item
, (wxItemResource
*) NULL
))
101 bool wxResourceTableWithSaving::SaveResource(ostream
& stream
, wxItemResource
* item
, wxItemResource
* parentItem
)
104 wxString
itemType(item
->GetType());
106 if (itemType
== "wxDialogBox" || itemType
== "wxDialog" || itemType
== "wxPanel")
108 if (itemType
== "wxDialogBox" || itemType
== "wxDialog")
110 stream
<< "static char *" << item
->GetName() << " = \"dialog(name = '" << item
->GetName() << "',\\\n";
111 GenerateDialogStyleString(item
->GetStyle(), styleBuf
);
115 stream
<< "static char *" << item
->GetName() << " = \"panel(name = '" << item
->GetName() << "',\\\n";
116 GenerateDialogStyleString(item
->GetStyle(), styleBuf
);
119 stream
<< " style = '" << styleBuf
<< "',\\\n";
120 stream
<< " title = '" << item
->GetTitle() << "',\\\n";
121 stream
<< " id = " << item
->GetId() << ",\\\n";
122 stream
<< " x = " << item
->GetX() << ", y = " << item
->GetY();
123 stream
<< ", width = " << item
->GetWidth() << ", height = " << item
->GetHeight();
125 if (1) // item->GetStyle() & wxNO_3D)
127 if (item
->GetBackgroundColour().Ok())
130 wxDecToHex(item
->GetBackgroundColour().Red(), buf
);
131 wxDecToHex(item
->GetBackgroundColour().Green(), buf
+2);
132 wxDecToHex(item
->GetBackgroundColour().Blue(), buf
+4);
135 stream
<< ",\\\n " << "background_colour = '" << buf
<< "'";
141 if ((item
->GetResourceStyle() & wxRESOURCE_DIALOG_UNITS
) != 0)
143 if ((item
->GetResourceStyle() & wxRESOURCE_USE_DEFAULTS
) != 0)
146 stream
<< ",\\\n " << "use_dialog_units = " << dialogUnits
;
147 stream
<< ",\\\n " << "use_system_defaults = " << useDefaults
;
149 if (item
->GetFont().Ok())
151 stream
<< ",\\\n font = ";
152 OutputFont(stream
, item
->GetFont());
155 if (item
->GetChildren().Number() > 0)
159 wxNode
*node
= item
->GetChildren().First();
162 wxItemResource
*child
= (wxItemResource
*)node
->Data();
164 stream
<< " control = [";
166 SaveResource(stream
, child
, item
);
174 stream
<< ").\";\n\n";
176 else if (itemType
== "wxButton" || itemType
== "wxBitmapButton")
178 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
179 stream
<< item
->GetId() << ", " << itemType
<< ", " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
180 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
181 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
182 if (item
->GetValue4())
183 stream
<< ", '" << item
->GetValue4() << "'";
184 if (item
->GetFont().Ok())
187 OutputFont(stream
, item
->GetFont());
190 else if (itemType
== "wxStaticText" || itemType
== "wxStaticBitmap")
192 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
193 stream
<< item
->GetId() << ", " << itemType
<< ", " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
194 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
195 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
196 if (item
->GetValue4())
197 stream
<< ", '" << item
->GetValue4() << "'";
198 if (item
->GetFont().Ok())
201 OutputFont(stream
, item
->GetFont());
204 else if (itemType
== "wxCheckBox")
206 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
207 stream
<< item
->GetId() << ", " << "wxCheckBox, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
208 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
209 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
210 stream
<< ", " << item
->GetValue1();
211 if (item
->GetFont().Ok())
214 OutputFont(stream
, item
->GetFont());
217 else if (itemType
== "wxRadioButton")
219 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
220 stream
<< item
->GetId() << ", " << "wxRadioButton, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
221 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
222 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
223 stream
<< ", " << item
->GetValue1();
224 if (item
->GetFont().Ok())
227 OutputFont(stream
, item
->GetFont());
230 else if (itemType
== "wxStaticBox")
232 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
233 stream
<< item
->GetId() << ", " << "wxStaticBox, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
234 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
235 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
236 if (item
->GetFont().Ok())
239 OutputFont(stream
, item
->GetFont());
242 else if (itemType
== "wxText" || itemType
== "wxMultiText" || itemType
== "wxTextCtrl")
244 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
245 stream
<< item
->GetId() << ", " << "wxTextCtrl, ";
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());
250 if (item
->GetFont().Ok())
253 OutputFont(stream
, item
->GetFont());
256 else if (itemType
== "wxGauge")
258 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
259 stream
<< item
->GetId() << ", " << "wxGauge, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
260 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
261 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
262 stream
<< ", " << item
->GetValue1() << ", " << item
->GetValue2();
263 if (item
->GetFont().Ok())
266 OutputFont(stream
, item
->GetFont());
269 else if (itemType
== "wxSlider")
271 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
272 stream
<< item
->GetId() << ", " << "wxSlider, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
273 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
274 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
275 stream
<< ", " << item
->GetValue1() << ", " << item
->GetValue2() << ", " << item
->GetValue3();
276 if (item
->GetFont().Ok())
279 OutputFont(stream
, item
->GetFont());
282 else if (itemType
== "wxScrollBar")
284 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
285 stream
<< item
->GetId() << ", " << "wxScrollBar, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
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();
291 else if (itemType
== "wxListBox")
293 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
294 stream
<< item
->GetId() << ", " << "wxListBox, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
295 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
296 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
298 // Default list of values
301 if (item
->GetStringValues().Number() > 0)
303 wxNode
*node
= item
->GetStringValues().First();
306 char *s
= (char *)node
->Data();
307 stream
<< SafeWord(s
);
314 switch (item
->GetValue1())
318 stream
<< "'wxLB_MULTIPLE'";
323 stream
<< "'wxLB_EXTENDED'";
329 stream
<< "'wxLB_SINGLE'";
333 if (item
->GetFont().Ok())
336 OutputFont(stream
, item
->GetFont());
339 else if (itemType
== "wxChoice" || itemType
== "wxComboBox")
341 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
343 stream
<< item
->GetId() << ", " << itemType
<< ", " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
344 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
345 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
347 if (itemType
== "wxComboBox")
348 stream
<< ", " << SafeWord(item
->GetValue4());
350 // Default list of values
353 if (item
->GetStringValues().Number() > 0)
355 wxNode
*node
= item
->GetStringValues().First();
358 char *s
= (char *)node
->Data();
359 stream
<< SafeWord(s
);
366 if (item
->GetFont().Ok())
369 OutputFont(stream
, item
->GetFont());
372 else if (itemType
== "wxRadioBox")
374 // Must write out the orientation and number of rows/cols!!
375 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
376 stream
<< item
->GetId() << ", " << "wxRadioBox, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
377 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
378 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
380 // Default list of values
383 if (item
->GetStringValues().Number() > 0)
385 wxNode
*node
= item
->GetStringValues().First();
388 char *s
= (char *)node
->Data();
389 stream
<< SafeWord(s
);
395 stream
<< "], " << item
->GetValue1();
396 if (item
->GetFont().Ok())
399 OutputFont(stream
, item
->GetFont());
402 else if (itemType
== "wxBitmap")
404 stream
<< "static char *" << item
->GetName() << " = \"bitmap(name = '" << item
->GetName() << "',\\\n";
406 wxNode
*node
= item
->GetChildren().First();
409 wxItemResource
*child
= (wxItemResource
*)node
->Data();
410 stream
<< " bitmap = [";
413 strcpy(buf
, child
->GetName());
415 wxDos2UnixFilename(buf
);
418 stream
<< "'" << buf
<< "', ";
420 int bitmapType
= (int)child
->GetValue1();
423 case wxBITMAP_TYPE_XBM_DATA
:
425 stream
<< "wxBITMAP_TYPE_XBM_DATA";
428 case wxBITMAP_TYPE_XPM_DATA
:
430 stream
<< "wxBITMAP_TYPE_XPM_DATA";
433 case wxBITMAP_TYPE_XBM
:
435 stream
<< "wxBITMAP_TYPE_XBM";
438 case wxBITMAP_TYPE_XPM
:
440 stream
<< "wxBITMAP_TYPE_XPM";
443 case wxBITMAP_TYPE_BMP
:
445 stream
<< "wxBITMAP_TYPE_BMP";
448 case wxBITMAP_TYPE_BMP_RESOURCE
:
450 stream
<< "wxBITMAP_TYPE_BMP_RESOURCE";
453 case wxBITMAP_TYPE_GIF
:
455 stream
<< "wxBITMAP_TYPE_GIF";
458 case wxBITMAP_TYPE_TIF
:
460 stream
<< "wxBITMAP_TYPE_TIF";
463 case wxBITMAP_TYPE_ICO
:
465 stream
<< "wxBITMAP_TYPE_ICO";
468 case wxBITMAP_TYPE_ICO_RESOURCE
:
470 stream
<< "wxBITMAP_TYPE_ICO_RESOURCE";
473 case wxBITMAP_TYPE_CUR
:
475 stream
<< "wxBITMAP_TYPE_CUR";
478 case wxBITMAP_TYPE_CUR_RESOURCE
:
480 stream
<< "wxBITMAP_TYPE_CUR_RESOURCE";
484 case wxBITMAP_TYPE_ANY
:
486 stream
<< "wxBITMAP_TYPE_ANY";
491 int platform
= child
->GetValue2();
494 case RESOURCE_PLATFORM_WINDOWS
:
496 stream
<< "'WINDOWS'";
499 case RESOURCE_PLATFORM_X
:
504 case RESOURCE_PLATFORM_MAC
:
509 case RESOURCE_PLATFORM_ANY
:
515 int noColours
= (int)child
->GetValue3();
517 stream
<< ", " << noColours
;
526 stream
<< ").\";\n\n";
530 wxString
str("Unimplemented resource type: ");
537 void wxResourceTableWithSaving::GenerateDialogStyleString(long windowStyle
, char *buf
)
540 m_styleTable
.GenerateStyleStrings("wxWindow", windowStyle
, buf
);
541 m_styleTable
.GenerateStyleStrings("wxPanel", windowStyle
, buf
);
542 m_styleTable
.GenerateStyleStrings("wxDialog", windowStyle
, buf
);
544 if (strlen(buf
) == 0)
548 void wxResourceTableWithSaving::GeneratePanelStyleString(long windowStyle
, char *buf
)
551 m_styleTable
.GenerateStyleStrings("wxWindow", windowStyle
, buf
);
552 m_styleTable
.GenerateStyleStrings("wxPanel", windowStyle
, buf
);
554 if (strlen(buf
) == 0)
559 void wxResourceTableWithSaving::GenerateControlStyleString(const wxString
& windowClass
, long windowStyle
, char *buf
)
562 m_styleTable
.GenerateStyleStrings("wxWindow", windowStyle
, buf
);
563 m_styleTable
.GenerateStyleStrings("wxControl", windowStyle
, buf
);
564 m_styleTable
.GenerateStyleStrings(windowClass
, windowStyle
, buf
);
566 if (strlen(buf
) == 0)
570 // Returns quoted string or "NULL"
571 char *SafeString(const wxString
& s
)
577 strcpy(wxBuffer
, "\"");
579 strcat(wxBuffer
, "\"");
584 // Returns quoted string or ''
585 char *SafeWord(const wxString
& s
)
591 strcpy(wxBuffer
, "'");
592 strcat(wxBuffer
, (const char*) s
);
593 strcat(wxBuffer
, "'");