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(char *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
, wxItemResource
*childResource
)
52 wxControl
*item
= wxResourceTable::CreateItem(panel
, childResource
);
54 wxResourceManager::GetCurrentResourceManager()->GetResourceAssociations().Put((long)childResource
, item
);
58 void wxResourceTableWithSaving::OutputFont(ostream
& stream
, 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());
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
))
96 bool wxResourceTableWithSaving::SaveResource(ostream
& stream
, wxItemResource
*item
)
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
);
113 stream
<< " style = '" << styleBuf
<< "',\\\n";
114 stream
<< " title = '" << item
->GetTitle() << "',\\\n";
115 stream
<< " id = " << item
->GetId() << ",\\\n";
116 stream
<< " x = " << item
->GetX() << ", y = " << item
->GetY();
117 stream
<< ", width = " << item
->GetWidth() << ", height = " << item
->GetHeight();
119 if (1) // item->GetStyle() & wxNO_3D)
121 if (item
->GetBackgroundColour())
124 wxDecToHex(item
->GetBackgroundColour()->Red(), buf
);
125 wxDecToHex(item
->GetBackgroundColour()->Green(), buf
+2);
126 wxDecToHex(item
->GetBackgroundColour()->Blue(), buf
+4);
129 stream
<< ",\\\n " << "background_colour = '" << buf
<< "'";
133 if (item
->GetFont() && item
->GetFont()->Ok())
135 stream
<< ",\\\n font = ";
136 OutputFont(stream
, item
->GetFont());
139 if (item
->GetChildren().Number() > 0)
143 wxNode
*node
= item
->GetChildren().First();
146 wxItemResource
*child
= (wxItemResource
*)node
->Data();
148 stream
<< " control = [";
150 SaveResource(stream
, child
);
158 stream
<< ").\";\n\n";
160 else if (itemType
== "wxButton" || itemType
== "wxBitmapButton")
162 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
163 stream
<< item
->GetId() << ", " << itemType
<< ", " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
164 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
165 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
166 if (item
->GetValue4())
167 stream
<< ", '" << item
->GetValue4() << "'";
171 OutputFont(stream
, item
->GetFont());
174 else if (itemType
== "wxStaticText" || itemType
== "wxStaticBitmap")
176 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
177 stream
<< item
->GetId() << ", " << itemType
<< ", " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
178 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
179 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
180 if (item
->GetValue4())
181 stream
<< ", '" << item
->GetValue4() << "'";
185 OutputFont(stream
, item
->GetFont());
188 else if (itemType
== "wxCheckBox")
190 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
191 stream
<< item
->GetId() << ", " << "wxCheckBox, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
192 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
193 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
194 stream
<< ", " << item
->GetValue1();
198 OutputFont(stream
, item
->GetFont());
201 else if (itemType
== "wxRadioButton")
203 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
204 stream
<< item
->GetId() << ", " << "wxRadioButton, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
205 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
206 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
207 stream
<< ", " << item
->GetValue1();
211 OutputFont(stream
, item
->GetFont());
214 else if (itemType
== "wxStaticBox")
216 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
217 stream
<< item
->GetId() << ", " << "wxStaticBox, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
218 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
219 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
223 OutputFont(stream
, item
->GetFont());
226 else if (itemType
== "wxText" || itemType
== "wxMultiText" || itemType
== "wxTextCtrl")
228 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
229 stream
<< item
->GetId() << ", " << "wxTextCtrl, ";
230 stream
<< SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
231 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
232 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
233 stream
<< ", " << SafeWord(item
->GetValue4());
237 OutputFont(stream
, item
->GetFont());
240 else if (itemType
== "wxGauge")
242 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
243 stream
<< item
->GetId() << ", " << "wxGauge, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
244 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
245 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
246 stream
<< ", " << item
->GetValue1() << ", " << item
->GetValue2();
250 OutputFont(stream
, item
->GetFont());
253 else if (itemType
== "wxSlider")
255 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
256 stream
<< item
->GetId() << ", " << "wxSlider, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
257 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
258 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
259 stream
<< ", " << item
->GetValue1() << ", " << item
->GetValue2() << ", " << item
->GetValue3();
263 OutputFont(stream
, item
->GetFont());
266 else if (itemType
== "wxScrollBar")
268 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
269 stream
<< item
->GetId() << ", " << "wxScrollBar, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
270 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
271 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
272 stream
<< ", " << item
->GetValue1() << ", " << item
->GetValue2() << ", " << item
->GetValue3() << ", ";
273 stream
<< item
->GetValue5();
275 else if (itemType
== "wxListBox")
277 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
278 stream
<< item
->GetId() << ", " << "wxListBox, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
279 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
280 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
282 // Default list of values
285 if (item
->GetStringValues())
287 wxNode
*node
= item
->GetStringValues()->First();
290 char *s
= (char *)node
->Data();
291 stream
<< SafeWord(s
);
298 switch (item
->GetValue1())
302 stream
<< "'wxLB_MULTIPLE'";
307 stream
<< "'wxLB_EXTENDED'";
313 stream
<< "'wxLB_SINGLE'";
320 OutputFont(stream
, item
->GetFont());
323 else if (itemType
== "wxChoice" || itemType
== "wxComboBox")
325 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
327 stream
<< item
->GetId() << ", " << itemType
<< ", " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
328 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
329 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
331 if (itemType
== "wxComboBox")
332 stream
<< ", " << SafeWord(item
->GetValue4());
334 // Default list of values
337 if (item
->GetStringValues())
339 wxNode
*node
= item
->GetStringValues()->First();
342 char *s
= (char *)node
->Data();
343 stream
<< SafeWord(s
);
353 OutputFont(stream
, item
->GetFont());
356 else if (itemType
== "wxRadioBox")
358 // Must write out the orientation and number of rows/cols!!
359 GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
);
360 stream
<< item
->GetId() << ", " << "wxRadioBox, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf
<< "', ";
361 stream
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", ";
362 stream
<< item
->GetWidth() << ", " << item
->GetHeight();
364 // Default list of values
367 if (item
->GetStringValues())
369 wxNode
*node
= item
->GetStringValues()->First();
372 char *s
= (char *)node
->Data();
373 stream
<< SafeWord(s
);
379 stream
<< "], " << item
->GetValue1();
383 OutputFont(stream
, item
->GetFont());
386 else if (itemType
== "wxBitmap")
388 stream
<< "static char *" << item
->GetName() << " = \"bitmap(name = '" << item
->GetName() << "',\\\n";
390 wxNode
*node
= item
->GetChildren().First();
393 wxItemResource
*child
= (wxItemResource
*)node
->Data();
394 stream
<< " bitmap = [";
397 strcpy(buf
, child
->GetName());
399 wxDos2UnixFilename(buf
);
402 stream
<< "'" << buf
<< "', ";
404 int bitmapType
= (int)child
->GetValue1();
407 case wxBITMAP_TYPE_XBM_DATA
:
409 stream
<< "wxBITMAP_TYPE_XBM_DATA";
412 case wxBITMAP_TYPE_XPM_DATA
:
414 stream
<< "wxBITMAP_TYPE_XPM_DATA";
417 case wxBITMAP_TYPE_XBM
:
419 stream
<< "wxBITMAP_TYPE_XBM";
422 case wxBITMAP_TYPE_XPM
:
424 stream
<< "wxBITMAP_TYPE_XPM";
427 case wxBITMAP_TYPE_BMP
:
429 stream
<< "wxBITMAP_TYPE_BMP";
432 case wxBITMAP_TYPE_BMP_RESOURCE
:
434 stream
<< "wxBITMAP_TYPE_BMP_RESOURCE";
437 case wxBITMAP_TYPE_GIF
:
439 stream
<< "wxBITMAP_TYPE_GIF";
442 case wxBITMAP_TYPE_TIF
:
444 stream
<< "wxBITMAP_TYPE_TIF";
447 case wxBITMAP_TYPE_ICO
:
449 stream
<< "wxBITMAP_TYPE_ICO";
452 case wxBITMAP_TYPE_ICO_RESOURCE
:
454 stream
<< "wxBITMAP_TYPE_ICO_RESOURCE";
457 case wxBITMAP_TYPE_CUR
:
459 stream
<< "wxBITMAP_TYPE_CUR";
462 case wxBITMAP_TYPE_CUR_RESOURCE
:
464 stream
<< "wxBITMAP_TYPE_CUR_RESOURCE";
468 case wxBITMAP_TYPE_ANY
:
470 stream
<< "wxBITMAP_TYPE_ANY";
475 int platform
= child
->GetValue2();
478 case RESOURCE_PLATFORM_WINDOWS
:
480 stream
<< "'WINDOWS'";
483 case RESOURCE_PLATFORM_X
:
488 case RESOURCE_PLATFORM_MAC
:
493 case RESOURCE_PLATFORM_ANY
:
499 int noColours
= (int)child
->GetValue3();
501 stream
<< ", " << noColours
;
510 stream
<< ").\";\n\n";
514 wxString
str("Unimplemented resource type: ");
521 void wxResourceTableWithSaving::GenerateDialogStyleString(long windowStyle
, char *buf
)
524 m_styleTable
.GenerateStyleStrings("wxWindow", windowStyle
, buf
);
525 m_styleTable
.GenerateStyleStrings("wxPanel", windowStyle
, buf
);
526 m_styleTable
.GenerateStyleStrings("wxDialog", windowStyle
, buf
);
528 if (strlen(buf
) == 0)
532 void wxResourceTableWithSaving::GeneratePanelStyleString(long windowStyle
, char *buf
)
535 m_styleTable
.GenerateStyleStrings("wxWindow", windowStyle
, buf
);
536 m_styleTable
.GenerateStyleStrings("wxPanel", windowStyle
, buf
);
538 if (strlen(buf
) == 0)
543 void wxResourceTableWithSaving::GenerateControlStyleString(const wxString
& windowClass
, long windowStyle
, char *buf
)
546 m_styleTable
.GenerateStyleStrings("wxWindow", windowStyle
, buf
);
547 m_styleTable
.GenerateStyleStrings("wxControl", windowStyle
, buf
);
548 m_styleTable
.GenerateStyleStrings(windowClass
, windowStyle
, buf
);
550 if (strlen(buf
) == 0)
554 // Returns quoted string or "NULL"
555 char *SafeString(char *s
)
561 strcpy(wxBuffer
, "\"");
563 strcat(wxBuffer
, "\"");
568 // Returns quoted string or ''
569 char *SafeWord(char *s
)
575 strcpy(wxBuffer
, "'");
577 strcat(wxBuffer
, "'");