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 #if !defined(__WXMSW__) && defined(__WXMAC__)
40 static char wxBuffer
[512];
43 char *SafeString(char *s
);
44 char *SafeWord(const wxString
& s
);
46 // Save an association between the child resource and the panel item, to allow
47 // us not to require unique window names.
48 wxControl
*wxResourceTableWithSaving::CreateItem(wxPanel
*panel
, const wxItemResource
*childResource
, const wxItemResource
* parentResource
)
50 wxControl
*item
= wxResourceTable::CreateItem(panel
, childResource
, parentResource
);
52 wxResourceManager::GetCurrentResourceManager()->GetResourceAssociations().Put((long)childResource
, item
);
56 void wxResourceTableWithSaving::OutputFont(wxTextOutputStream
& stream
, const wxFont
& font
)
58 stream
<< "[" << font
.GetPointSize() << ", '";
59 stream
<< font
.GetFamilyString() << "', '";
60 stream
<< font
.GetStyleString() << "', '";
61 stream
<< font
.GetWeightString() << "', ";
62 stream
<< (int)font
.GetUnderlined();
63 if (font
.GetFaceName() != "")
64 stream
<< ", '" << font
.GetFaceName() << "'";
69 * Resource table with saving (basic one only has loading)
72 bool wxResourceTableWithSaving::Save(const wxString
& filename
)
74 wxFileOutputStream
file_output( filename
);
75 if (file_output
.LastError())
78 wxTextOutputStream
stream( file_output
);
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(wxTextOutputStream
& 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 = " << SafeWord(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 /* Styles are now in the window style, not in a separate arg
311 switch (item->GetValue1())
315 stream << "'wxLB_MULTIPLE'";
320 stream << "'wxLB_EXTENDED'";
326 stream << "'wxLB_SINGLE'";
332 if (item
->GetFont().Ok())
335 OutputFont(stream
, item
->GetFont());
338 else if (itemType
== "wxChoice" || itemType
== "wxComboBox")
340 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
342 stream
<< item
->GetId() << ", " << itemType
<< ", " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
343 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
344 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
346 if (itemType
== "wxComboBox")
347 stream
<< ", " << SafeWord(item
->GetValue4());
349 // Default list of values
352 if (item
->GetStringValues().Number() > 0)
354 wxNode
*node
= item
->GetStringValues().First();
357 char *s
= (char *)node
->Data();
358 stream
<< SafeWord(s
);
365 if (item
->GetFont().Ok())
368 OutputFont(stream
, item
->GetFont());
371 else if (itemType
== "wxRadioBox")
373 // Must write out the orientation and number of rows/cols!!
374 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
375 stream
<< item
->GetId() << ", " << "wxRadioBox, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
376 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
377 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
379 // Default list of values
382 if (item
->GetStringValues().Number() > 0)
384 wxNode
*node
= item
->GetStringValues().First();
387 char *s
= (char *)node
->Data();
388 stream
<< SafeWord(s
);
394 stream
<< "], " << item
->GetValue1();
395 if (item
->GetFont().Ok())
398 OutputFont(stream
, item
->GetFont());
401 else if (itemType
== "wxBitmap")
403 stream
<< "static char *" << item
->GetName() << " = \"bitmap(name = '" << item
->GetName() << "',\\\n";
405 wxNode
*node
= item
->GetChildren().First();
408 wxItemResource
*child
= (wxItemResource
*)node
->Data();
409 stream
<< " bitmap = [";
412 strcpy(buf
, child
->GetName());
414 wxDos2UnixFilename(buf
);
417 stream
<< "'" << buf
<< "', ";
419 int bitmapType
= (int)child
->GetValue1();
422 case wxBITMAP_TYPE_XBM_DATA
:
424 stream
<< "wxBITMAP_TYPE_XBM_DATA";
427 case wxBITMAP_TYPE_XPM_DATA
:
429 stream
<< "wxBITMAP_TYPE_XPM_DATA";
432 case wxBITMAP_TYPE_XBM
:
434 stream
<< "wxBITMAP_TYPE_XBM";
437 case wxBITMAP_TYPE_XPM
:
439 stream
<< "wxBITMAP_TYPE_XPM";
442 case wxBITMAP_TYPE_BMP
:
444 stream
<< "wxBITMAP_TYPE_BMP";
447 case wxBITMAP_TYPE_BMP_RESOURCE
:
449 stream
<< "wxBITMAP_TYPE_BMP_RESOURCE";
452 case wxBITMAP_TYPE_GIF
:
454 stream
<< "wxBITMAP_TYPE_GIF";
457 case wxBITMAP_TYPE_TIF
:
459 stream
<< "wxBITMAP_TYPE_TIF";
462 case wxBITMAP_TYPE_ICO
:
464 stream
<< "wxBITMAP_TYPE_ICO";
467 case wxBITMAP_TYPE_ICO_RESOURCE
:
469 stream
<< "wxBITMAP_TYPE_ICO_RESOURCE";
472 case wxBITMAP_TYPE_CUR
:
474 stream
<< "wxBITMAP_TYPE_CUR";
477 case wxBITMAP_TYPE_CUR_RESOURCE
:
479 stream
<< "wxBITMAP_TYPE_CUR_RESOURCE";
483 case wxBITMAP_TYPE_ANY
:
485 stream
<< "wxBITMAP_TYPE_ANY";
490 int platform
= child
->GetValue2();
493 case RESOURCE_PLATFORM_WINDOWS
:
495 stream
<< "'WINDOWS'";
498 case RESOURCE_PLATFORM_X
:
503 case RESOURCE_PLATFORM_MAC
:
508 case RESOURCE_PLATFORM_ANY
:
514 int noColours
= (int)child
->GetValue3();
516 stream
<< ", " << noColours
;
525 stream
<< ").\";\n\n";
529 wxString
str("Unimplemented resource type: ");
536 void wxResourceTableWithSaving::GenerateDialogStyleString(long windowStyle
, char *buf
)
539 m_styleTable
.GenerateStyleStrings("wxWindow", windowStyle
, buf
);
540 m_styleTable
.GenerateStyleStrings("wxPanel", windowStyle
, buf
);
541 m_styleTable
.GenerateStyleStrings("wxDialog", windowStyle
, buf
);
543 if (strlen(buf
) == 0)
547 void wxResourceTableWithSaving::GeneratePanelStyleString(long windowStyle
, char *buf
)
550 m_styleTable
.GenerateStyleStrings("wxWindow", windowStyle
, buf
);
551 m_styleTable
.GenerateStyleStrings("wxPanel", windowStyle
, buf
);
553 if (strlen(buf
) == 0)
558 void wxResourceTableWithSaving::GenerateControlStyleString(const wxString
& windowClass
, long windowStyle
, char *buf
)
561 m_styleTable
.GenerateStyleStrings("wxWindow", windowStyle
, buf
);
562 m_styleTable
.GenerateStyleStrings("wxControl", windowStyle
, buf
);
563 m_styleTable
.GenerateStyleStrings(windowClass
, windowStyle
, buf
);
565 if (strlen(buf
) == 0)
569 // Returns quoted string or "NULL"
570 char *SafeString(const wxString
& s
)
576 strcpy(wxBuffer
, "\"");
578 strcat(wxBuffer
, "\"");
583 // Returns quoted string or '' : convert " to \"
584 char *SafeWord(const wxString
& s
)
600 } else if(*cp
== '\'') {