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 #include "wx/scrolbar.h"
33 #include "wx/string.h"
34 #include "wx/wfstream.h"
35 #include "wx/txtstrm.h"
39 char *SafeString(char *s
);
40 char *SafeWord(const wxString
& s
);
42 // Save an association between the child resource and the panel item, to allow
43 // us not to require unique window names.
44 wxControl
*wxResourceTableWithSaving::CreateItem(wxPanel
*panel
, const wxItemResource
*childResource
, const wxItemResource
* parentResource
)
46 wxControl
*item
= wxResourceTable::CreateItem(panel
, childResource
, parentResource
);
48 wxResourceManager::GetCurrentResourceManager()->GetResourceAssociations().Put((long)childResource
, item
);
52 void wxResourceTableWithSaving::OutputFont(wxTextOutputStream
& stream
, const wxFont
& font
)
54 stream
<< "[" << font
.GetPointSize() << ", '";
55 stream
<< font
.GetFamilyString() << "', '";
56 stream
<< font
.GetStyleString() << "', '";
57 stream
<< font
.GetWeightString() << "', ";
58 stream
<< (int)font
.GetUnderlined();
59 if (font
.GetFaceName() != "")
60 stream
<< ", '" << font
.GetFaceName() << "'";
65 * Resource table with saving (basic one only has loading)
68 bool wxResourceTableWithSaving::Save(const wxString
& filename
)
70 wxFileOutputStream
file_output( filename
);
71 if (file_output
.LastError())
74 wxTextOutputStream
stream( file_output
);
78 while ((node
= Next()))
80 wxItemResource
*item
= (wxItemResource
*)node
->Data();
81 wxString
resType(item
->GetType());
83 if (resType
== "wxDialogBox" || resType
== "wxDialog" || resType
== "wxPanel" || resType
== "wxBitmap")
85 if (!SaveResource(stream
, item
, (wxItemResource
*) NULL
))
92 bool wxResourceTableWithSaving::SaveResource(wxTextOutputStream
& stream
, wxItemResource
* item
, wxItemResource
* parentItem
)
95 wxString
itemType(item
->GetType());
97 if (itemType
== "wxDialogBox" || itemType
== "wxDialog" || itemType
== "wxPanel")
99 if (itemType
== "wxDialogBox" || itemType
== "wxDialog")
101 stream
<< "static char *" << item
->GetName() << " = \"dialog(name = '" << item
->GetName() << "',\\\n";
102 GenerateDialogStyleString(item
->GetStyle(), styleBuf
);
106 stream
<< "static char *" << item
->GetName() << " = \"panel(name = '" << item
->GetName() << "',\\\n";
107 GenerateDialogStyleString(item
->GetStyle(), styleBuf
);
110 stream
<< " style = '" << styleBuf
<< "',\\\n";
111 stream
<< " title = " << SafeWord(item
->GetTitle()) << ",\\\n";
112 stream
<< " id = " << item
->GetId() << ",\\\n";
113 stream
<< " x = " << item
->GetX() << ", y = " << item
->GetY();
114 stream
<< ", width = " << item
->GetWidth() << ", height = " << item
->GetHeight();
116 if (1) // item->GetStyle() & wxNO_3D)
118 if (item
->GetBackgroundColour().Ok())
121 wxDecToHex(item
->GetBackgroundColour().Red(), buf
);
122 wxDecToHex(item
->GetBackgroundColour().Green(), buf
+2);
123 wxDecToHex(item
->GetBackgroundColour().Blue(), buf
+4);
126 stream
<< ",\\\n " << "background_colour = '" << buf
<< "'";
132 if ((item
->GetResourceStyle() & wxRESOURCE_DIALOG_UNITS
) != 0)
134 if ((item
->GetResourceStyle() & wxRESOURCE_USE_DEFAULTS
) != 0)
137 stream
<< ",\\\n " << "use_dialog_units = " << dialogUnits
;
138 stream
<< ",\\\n " << "use_system_defaults = " << useDefaults
;
140 if (item
->GetFont().Ok())
142 stream
<< ",\\\n font = ";
143 OutputFont(stream
, item
->GetFont());
146 if (item
->GetChildren().Number() > 0)
150 wxNode
*node
= item
->GetChildren().First();
153 wxItemResource
*child
= (wxItemResource
*)node
->Data();
155 stream
<< " control = [";
157 SaveResource(stream
, child
, item
);
165 stream
<< ").\";\n\n";
167 else if (itemType
== "wxButton" || itemType
== "wxBitmapButton")
169 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
170 stream
<< item
->GetId() << ", " << itemType
<< ", " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
171 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
172 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
173 if (item
->GetValue4())
174 stream
<< ", '" << item
->GetValue4() << "'";
175 if (item
->GetFont().Ok())
178 OutputFont(stream
, item
->GetFont());
181 else if (itemType
== "wxStaticText" || itemType
== "wxStaticBitmap")
183 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
184 stream
<< item
->GetId() << ", " << itemType
<< ", " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
185 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
186 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
187 if (item
->GetValue4())
188 stream
<< ", '" << item
->GetValue4() << "'";
189 if (item
->GetFont().Ok())
192 OutputFont(stream
, item
->GetFont());
195 else if (itemType
== "wxCheckBox")
197 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
198 stream
<< item
->GetId() << ", " << "wxCheckBox, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
199 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
200 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
201 stream
<< ", " << item
->GetValue1();
202 if (item
->GetFont().Ok())
205 OutputFont(stream
, item
->GetFont());
208 else if (itemType
== "wxRadioButton")
210 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
211 stream
<< item
->GetId() << ", " << "wxRadioButton, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
212 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
213 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
214 stream
<< ", " << item
->GetValue1();
215 if (item
->GetFont().Ok())
218 OutputFont(stream
, item
->GetFont());
221 else if (itemType
== "wxStaticBox")
223 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
224 stream
<< item
->GetId() << ", " << "wxStaticBox, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
225 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
226 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
227 if (item
->GetFont().Ok())
230 OutputFont(stream
, item
->GetFont());
233 else if (itemType
== "wxText" || itemType
== "wxMultiText" || itemType
== "wxTextCtrl")
235 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
236 stream
<< item
->GetId() << ", " << "wxTextCtrl, ";
237 stream
<< SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
238 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
239 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
240 stream
<< ", " << SafeWord(item
->GetValue4());
241 if (item
->GetFont().Ok())
244 OutputFont(stream
, item
->GetFont());
247 else if (itemType
== "wxGauge")
249 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
250 stream
<< item
->GetId() << ", " << "wxGauge, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
251 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
252 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
253 stream
<< ", " << item
->GetValue1() << ", " << item
->GetValue2();
254 if (item
->GetFont().Ok())
257 OutputFont(stream
, item
->GetFont());
260 else if (itemType
== "wxSlider")
262 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
263 stream
<< item
->GetId() << ", " << "wxSlider, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
264 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
265 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
266 stream
<< ", " << item
->GetValue1() << ", " << item
->GetValue2() << ", " << item
->GetValue3();
267 if (item
->GetFont().Ok())
270 OutputFont(stream
, item
->GetFont());
273 else if (itemType
== "wxScrollBar")
275 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
276 stream
<< item
->GetId() << ", " << "wxScrollBar, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
277 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
278 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
279 stream
<< ", " << item
->GetValue1() << ", " << item
->GetValue2() << ", " << item
->GetValue3() << ", ";
280 stream
<< item
->GetValue5();
282 else if (itemType
== "wxListBox")
284 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
285 stream
<< item
->GetId() << ", " << "wxListBox, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
286 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
287 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
289 // Default list of values
292 if (item
->GetStringValues().Number() > 0)
294 wxNode
*node
= item
->GetStringValues().First();
297 char *s
= (char *)node
->Data();
298 stream
<< SafeWord(s
);
305 /* Styles are now in the window style, not in a separate arg
307 switch (item->GetValue1())
311 stream << "'wxLB_MULTIPLE'";
316 stream << "'wxLB_EXTENDED'";
322 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 '' : convert " to \"
580 char *SafeWord(const wxString
& s
)
596 } else if(*cp
== '\'') {