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 static char wxBuffer
[512];
41 char *SafeString(char *s
);
42 char *SafeWord(const wxString
& s
);
44 // Save an association between the child resource and the panel item, to allow
45 // us not to require unique window names.
46 wxControl
*wxResourceTableWithSaving::CreateItem(wxPanel
*panel
, const wxItemResource
*childResource
, const wxItemResource
* parentResource
)
48 wxControl
*item
= wxResourceTable::CreateItem(panel
, childResource
, parentResource
);
50 wxResourceManager::GetCurrentResourceManager()->GetResourceAssociations().Put((long)childResource
, item
);
54 void wxResourceTableWithSaving::OutputFont(wxTextOutputStream
& stream
, const wxFont
& font
)
56 stream
<< "[" << font
.GetPointSize() << ", '";
57 stream
<< font
.GetFamilyString() << "', '";
58 stream
<< font
.GetStyleString() << "', '";
59 stream
<< font
.GetWeightString() << "', ";
60 stream
<< (int)font
.GetUnderlined();
61 if (font
.GetFaceName() != "")
62 stream
<< ", '" << font
.GetFaceName() << "'";
67 * Resource table with saving (basic one only has loading)
70 bool wxResourceTableWithSaving::Save(const wxString
& filename
)
72 wxFileOutputStream
file_output( filename
);
73 if (file_output
.LastError())
76 wxTextOutputStream
stream( file_output
);
80 while ((node
= Next()))
82 wxItemResource
*item
= (wxItemResource
*)node
->Data();
83 wxString
resType(item
->GetType());
85 if (resType
== "wxDialogBox" || resType
== "wxDialog" || resType
== "wxPanel" || resType
== "wxBitmap")
87 if (!SaveResource(stream
, item
, (wxItemResource
*) NULL
))
94 bool wxResourceTableWithSaving::SaveResource(wxTextOutputStream
& stream
, wxItemResource
* item
, wxItemResource
* parentItem
)
97 wxString
itemType(item
->GetType());
99 if (itemType
== "wxDialogBox" || itemType
== "wxDialog" || itemType
== "wxPanel")
101 if (itemType
== "wxDialogBox" || itemType
== "wxDialog")
103 stream
<< "static char *" << item
->GetName() << " = \"dialog(name = '" << item
->GetName() << "',\\\n";
104 GenerateDialogStyleString(item
->GetStyle(), styleBuf
);
108 stream
<< "static char *" << item
->GetName() << " = \"panel(name = '" << item
->GetName() << "',\\\n";
109 GenerateDialogStyleString(item
->GetStyle(), styleBuf
);
112 stream
<< " style = '" << styleBuf
<< "',\\\n";
113 stream
<< " title = " << SafeWord(item
->GetTitle()) << ",\\\n";
114 stream
<< " id = " << item
->GetId() << ",\\\n";
115 stream
<< " x = " << item
->GetX() << ", y = " << item
->GetY();
116 stream
<< ", width = " << item
->GetWidth() << ", height = " << item
->GetHeight();
118 if (1) // item->GetStyle() & wxNO_3D)
120 if (item
->GetBackgroundColour().Ok())
123 wxDecToHex(item
->GetBackgroundColour().Red(), buf
);
124 wxDecToHex(item
->GetBackgroundColour().Green(), buf
+2);
125 wxDecToHex(item
->GetBackgroundColour().Blue(), buf
+4);
128 stream
<< ",\\\n " << "background_colour = '" << buf
<< "'";
134 if ((item
->GetResourceStyle() & wxRESOURCE_DIALOG_UNITS
) != 0)
136 if ((item
->GetResourceStyle() & wxRESOURCE_USE_DEFAULTS
) != 0)
139 stream
<< ",\\\n " << "use_dialog_units = " << dialogUnits
;
140 stream
<< ",\\\n " << "use_system_defaults = " << useDefaults
;
142 if (item
->GetFont().Ok())
144 stream
<< ",\\\n font = ";
145 OutputFont(stream
, item
->GetFont());
148 if (item
->GetChildren().Number() > 0)
152 wxNode
*node
= item
->GetChildren().First();
155 wxItemResource
*child
= (wxItemResource
*)node
->Data();
157 stream
<< " control = [";
159 SaveResource(stream
, child
, item
);
167 stream
<< ").\";\n\n";
169 else if (itemType
== "wxButton" || itemType
== "wxBitmapButton")
171 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
172 stream
<< item
->GetId() << ", " << itemType
<< ", " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
173 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
174 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
175 if (item
->GetValue4())
176 stream
<< ", '" << item
->GetValue4() << "'";
177 if (item
->GetFont().Ok())
180 OutputFont(stream
, item
->GetFont());
183 else if (itemType
== "wxStaticText" || itemType
== "wxStaticBitmap")
185 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
186 stream
<< item
->GetId() << ", " << 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() << "'";
191 if (item
->GetFont().Ok())
194 OutputFont(stream
, item
->GetFont());
197 else if (itemType
== "wxCheckBox")
199 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
200 stream
<< item
->GetId() << ", " << "wxCheckBox, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
201 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
202 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
203 stream
<< ", " << item
->GetValue1();
204 if (item
->GetFont().Ok())
207 OutputFont(stream
, item
->GetFont());
210 else if (itemType
== "wxRadioButton")
212 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
213 stream
<< item
->GetId() << ", " << "wxRadioButton, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
214 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
215 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
216 stream
<< ", " << item
->GetValue1();
217 if (item
->GetFont().Ok())
220 OutputFont(stream
, item
->GetFont());
223 else if (itemType
== "wxStaticBox")
225 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
226 stream
<< item
->GetId() << ", " << "wxStaticBox, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
227 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
228 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
229 if (item
->GetFont().Ok())
232 OutputFont(stream
, item
->GetFont());
235 else if (itemType
== "wxText" || itemType
== "wxMultiText" || itemType
== "wxTextCtrl")
237 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
238 stream
<< item
->GetId() << ", " << "wxTextCtrl, ";
239 stream
<< SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
240 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
241 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
242 stream
<< ", " << SafeWord(item
->GetValue4());
243 if (item
->GetFont().Ok())
246 OutputFont(stream
, item
->GetFont());
249 else if (itemType
== "wxGauge")
251 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
252 stream
<< item
->GetId() << ", " << "wxGauge, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
253 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
254 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
255 stream
<< ", " << item
->GetValue1() << ", " << item
->GetValue2();
256 if (item
->GetFont().Ok())
259 OutputFont(stream
, item
->GetFont());
262 else if (itemType
== "wxSlider")
264 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
265 stream
<< item
->GetId() << ", " << "wxSlider, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
266 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
267 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
268 stream
<< ", " << item
->GetValue1() << ", " << item
->GetValue2() << ", " << item
->GetValue3();
269 if (item
->GetFont().Ok())
272 OutputFont(stream
, item
->GetFont());
275 else if (itemType
== "wxScrollBar")
277 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
278 stream
<< item
->GetId() << ", " << "wxScrollBar, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
279 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
280 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
281 stream
<< ", " << item
->GetValue1() << ", " << item
->GetValue2() << ", " << item
->GetValue3() << ", ";
282 stream
<< item
->GetValue5();
284 else if (itemType
== "wxListBox")
286 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
287 stream
<< item
->GetId() << ", " << "wxListBox, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
288 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
289 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
291 // Default list of values
294 if (item
->GetStringValues().Number() > 0)
296 wxNode
*node
= item
->GetStringValues().First();
299 char *s
= (char *)node
->Data();
300 stream
<< SafeWord(s
);
307 /* Styles are now in the window style, not in a separate arg
309 switch (item->GetValue1())
313 stream << "'wxLB_MULTIPLE'";
318 stream << "'wxLB_EXTENDED'";
324 stream << "'wxLB_SINGLE'";
330 if (item
->GetFont().Ok())
333 OutputFont(stream
, item
->GetFont());
336 else if (itemType
== "wxChoice" || itemType
== "wxComboBox")
338 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
340 stream
<< item
->GetId() << ", " << itemType
<< ", " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
341 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
342 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
344 if (itemType
== "wxComboBox")
345 stream
<< ", " << SafeWord(item
->GetValue4());
347 // Default list of values
350 if (item
->GetStringValues().Number() > 0)
352 wxNode
*node
= item
->GetStringValues().First();
355 char *s
= (char *)node
->Data();
356 stream
<< SafeWord(s
);
363 if (item
->GetFont().Ok())
366 OutputFont(stream
, item
->GetFont());
369 else if (itemType
== "wxRadioBox")
371 // Must write out the orientation and number of rows/cols!!
372 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
373 stream
<< item
->GetId() << ", " << "wxRadioBox, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
374 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
375 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
377 // Default list of values
380 if (item
->GetStringValues().Number() > 0)
382 wxNode
*node
= item
->GetStringValues().First();
385 char *s
= (char *)node
->Data();
386 stream
<< SafeWord(s
);
392 stream
<< "], " << item
->GetValue1();
393 if (item
->GetFont().Ok())
396 OutputFont(stream
, item
->GetFont());
399 else if (itemType
== "wxBitmap")
401 stream
<< "static char *" << item
->GetName() << " = \"bitmap(name = '" << item
->GetName() << "',\\\n";
403 wxNode
*node
= item
->GetChildren().First();
406 wxItemResource
*child
= (wxItemResource
*)node
->Data();
407 stream
<< " bitmap = [";
410 strcpy(buf
, child
->GetName());
412 wxDos2UnixFilename(buf
);
415 stream
<< "'" << buf
<< "', ";
417 int bitmapType
= (int)child
->GetValue1();
420 case wxBITMAP_TYPE_XBM_DATA
:
422 stream
<< "wxBITMAP_TYPE_XBM_DATA";
425 case wxBITMAP_TYPE_XPM_DATA
:
427 stream
<< "wxBITMAP_TYPE_XPM_DATA";
430 case wxBITMAP_TYPE_XBM
:
432 stream
<< "wxBITMAP_TYPE_XBM";
435 case wxBITMAP_TYPE_XPM
:
437 stream
<< "wxBITMAP_TYPE_XPM";
440 case wxBITMAP_TYPE_BMP
:
442 stream
<< "wxBITMAP_TYPE_BMP";
445 case wxBITMAP_TYPE_BMP_RESOURCE
:
447 stream
<< "wxBITMAP_TYPE_BMP_RESOURCE";
450 case wxBITMAP_TYPE_GIF
:
452 stream
<< "wxBITMAP_TYPE_GIF";
455 case wxBITMAP_TYPE_TIF
:
457 stream
<< "wxBITMAP_TYPE_TIF";
460 case wxBITMAP_TYPE_ICO
:
462 stream
<< "wxBITMAP_TYPE_ICO";
465 case wxBITMAP_TYPE_ICO_RESOURCE
:
467 stream
<< "wxBITMAP_TYPE_ICO_RESOURCE";
470 case wxBITMAP_TYPE_CUR
:
472 stream
<< "wxBITMAP_TYPE_CUR";
475 case wxBITMAP_TYPE_CUR_RESOURCE
:
477 stream
<< "wxBITMAP_TYPE_CUR_RESOURCE";
481 case wxBITMAP_TYPE_ANY
:
483 stream
<< "wxBITMAP_TYPE_ANY";
488 int platform
= child
->GetValue2();
491 case RESOURCE_PLATFORM_WINDOWS
:
493 stream
<< "'WINDOWS'";
496 case RESOURCE_PLATFORM_X
:
501 case RESOURCE_PLATFORM_MAC
:
506 case RESOURCE_PLATFORM_ANY
:
512 int noColours
= (int)child
->GetValue3();
514 stream
<< ", " << noColours
;
523 stream
<< ").\";\n\n";
527 wxString
str("Unimplemented resource type: ");
534 void wxResourceTableWithSaving::GenerateDialogStyleString(long windowStyle
, char *buf
)
537 m_styleTable
.GenerateStyleStrings("wxWindow", windowStyle
, buf
);
538 m_styleTable
.GenerateStyleStrings("wxPanel", windowStyle
, buf
);
539 m_styleTable
.GenerateStyleStrings("wxDialog", windowStyle
, buf
);
541 if (strlen(buf
) == 0)
545 void wxResourceTableWithSaving::GeneratePanelStyleString(long windowStyle
, char *buf
)
548 m_styleTable
.GenerateStyleStrings("wxWindow", windowStyle
, buf
);
549 m_styleTable
.GenerateStyleStrings("wxPanel", windowStyle
, buf
);
551 if (strlen(buf
) == 0)
556 void wxResourceTableWithSaving::GenerateControlStyleString(const wxString
& windowClass
, long windowStyle
, char *buf
)
559 m_styleTable
.GenerateStyleStrings("wxWindow", windowStyle
, buf
);
560 m_styleTable
.GenerateStyleStrings("wxControl", windowStyle
, buf
);
561 m_styleTable
.GenerateStyleStrings(windowClass
, windowStyle
, buf
);
563 if (strlen(buf
) == 0)
567 // Returns quoted string or "NULL"
568 char *SafeString(const wxString
& s
)
574 strcpy(wxBuffer
, "\"");
576 strcat(wxBuffer
, "\"");
581 // Returns quoted string or '' : convert " to \"
582 char *SafeWord(const wxString
& s
)
598 } else if(*cp
== '\'') {