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__) 
  37 #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 /* Styles are now in the window style, not in a separate arg 
 316       switch (item->GetValue1()) 
 320           stream << "'wxLB_MULTIPLE'"; 
 325           stream << "'wxLB_EXTENDED'"; 
 331           stream << "'wxLB_SINGLE'"; 
 337       if (item
->GetFont().Ok()) 
 340         OutputFont(stream
, item
->GetFont()); 
 343   else if (itemType 
== "wxChoice" || itemType 
== "wxComboBox") 
 345       GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
); 
 347       stream 
<< item
->GetId() << ", " << itemType 
<< ", " << SafeWord(item
->GetTitle()) << ", '" << styleBuf 
<< "', "; 
 348       stream 
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", "; 
 349       stream 
<< item
->GetWidth() << ", " << item
->GetHeight(); 
 351       if (itemType 
== "wxComboBox") 
 352         stream 
<< ", " << SafeWord(item
->GetValue4()); 
 354       // Default list of values 
 357       if (item
->GetStringValues().Number() > 0) 
 359         wxNode 
*node 
= item
->GetStringValues().First(); 
 362           char *s 
= (char *)node
->Data(); 
 363           stream 
<< SafeWord(s
); 
 370       if (item
->GetFont().Ok()) 
 373         OutputFont(stream
, item
->GetFont()); 
 376   else if (itemType 
== "wxRadioBox") 
 378       // Must write out the orientation and number of rows/cols!! 
 379       GenerateControlStyleString(itemType
, item
->GetStyle(), styleBuf
); 
 380       stream 
<< item
->GetId() << ", " << "wxRadioBox, " << SafeWord(item
->GetTitle()) << ", '" << styleBuf 
<< "', "; 
 381       stream 
<< SafeWord(item
->GetName()) << ", " << item
->GetX() << ", " << item
->GetY() << ", "; 
 382       stream 
<< item
->GetWidth() << ", " << item
->GetHeight(); 
 384       // Default list of values 
 387       if (item
->GetStringValues().Number() > 0) 
 389         wxNode 
*node 
= item
->GetStringValues().First(); 
 392           char *s 
= (char *)node
->Data(); 
 393           stream 
<< SafeWord(s
); 
 399       stream 
<< "], " << item
->GetValue1(); 
 400       if (item
->GetFont().Ok()) 
 403         OutputFont(stream
, item
->GetFont()); 
 406   else if (itemType 
== "wxBitmap") 
 408       stream 
<< "static char *" << item
->GetName() << " = \"bitmap(name = '" << item
->GetName() << "',\\\n"; 
 410       wxNode 
*node 
= item
->GetChildren().First(); 
 413         wxItemResource 
*child 
= (wxItemResource 
*)node
->Data(); 
 414         stream 
<< "  bitmap = ["; 
 417         strcpy(buf
, child
->GetName()); 
 419         wxDos2UnixFilename(buf
); 
 422         stream 
<< "'" << buf 
<< "', "; 
 424         int bitmapType 
= (int)child
->GetValue1(); 
 427           case wxBITMAP_TYPE_XBM_DATA
: 
 429             stream 
<< "wxBITMAP_TYPE_XBM_DATA"; 
 432           case wxBITMAP_TYPE_XPM_DATA
: 
 434             stream 
<< "wxBITMAP_TYPE_XPM_DATA"; 
 437           case wxBITMAP_TYPE_XBM
: 
 439             stream 
<< "wxBITMAP_TYPE_XBM"; 
 442           case wxBITMAP_TYPE_XPM
: 
 444             stream 
<< "wxBITMAP_TYPE_XPM"; 
 447           case wxBITMAP_TYPE_BMP
: 
 449             stream 
<< "wxBITMAP_TYPE_BMP"; 
 452           case wxBITMAP_TYPE_BMP_RESOURCE
: 
 454             stream 
<< "wxBITMAP_TYPE_BMP_RESOURCE"; 
 457           case wxBITMAP_TYPE_GIF
: 
 459             stream 
<< "wxBITMAP_TYPE_GIF"; 
 462           case wxBITMAP_TYPE_TIF
: 
 464             stream 
<< "wxBITMAP_TYPE_TIF"; 
 467           case wxBITMAP_TYPE_ICO
: 
 469             stream 
<< "wxBITMAP_TYPE_ICO"; 
 472           case wxBITMAP_TYPE_ICO_RESOURCE
: 
 474             stream 
<< "wxBITMAP_TYPE_ICO_RESOURCE"; 
 477           case wxBITMAP_TYPE_CUR
: 
 479             stream 
<< "wxBITMAP_TYPE_CUR"; 
 482           case wxBITMAP_TYPE_CUR_RESOURCE
: 
 484             stream 
<< "wxBITMAP_TYPE_CUR_RESOURCE"; 
 488           case wxBITMAP_TYPE_ANY
: 
 490             stream 
<< "wxBITMAP_TYPE_ANY"; 
 495         int platform 
= child
->GetValue2(); 
 498           case RESOURCE_PLATFORM_WINDOWS
: 
 500             stream 
<< "'WINDOWS'"; 
 503           case RESOURCE_PLATFORM_X
: 
 508           case RESOURCE_PLATFORM_MAC
: 
 513           case RESOURCE_PLATFORM_ANY
: 
 519         int noColours 
= (int)child
->GetValue3(); 
 521           stream 
<< ", " << noColours
; 
 530       stream 
<< ").\";\n\n"; 
 534         wxString 
str("Unimplemented resource type: "); 
 541 void wxResourceTableWithSaving::GenerateDialogStyleString(long windowStyle
, char *buf
) 
 544   m_styleTable
.GenerateStyleStrings("wxWindow", windowStyle
, buf
); 
 545   m_styleTable
.GenerateStyleStrings("wxPanel", windowStyle
, buf
); 
 546   m_styleTable
.GenerateStyleStrings("wxDialog", windowStyle
, buf
); 
 548   if (strlen(buf
) == 0) 
 552 void wxResourceTableWithSaving::GeneratePanelStyleString(long windowStyle
, char *buf
) 
 555   m_styleTable
.GenerateStyleStrings("wxWindow", windowStyle
, buf
); 
 556   m_styleTable
.GenerateStyleStrings("wxPanel", windowStyle
, buf
); 
 558   if (strlen(buf
) == 0) 
 563 void wxResourceTableWithSaving::GenerateControlStyleString(const wxString
& windowClass
, long windowStyle
, char *buf
) 
 566   m_styleTable
.GenerateStyleStrings("wxWindow", windowStyle
, buf
); 
 567   m_styleTable
.GenerateStyleStrings("wxControl", windowStyle
, buf
); 
 568   m_styleTable
.GenerateStyleStrings(windowClass
, windowStyle
, buf
); 
 570   if (strlen(buf
) == 0) 
 574 // Returns quoted string or "NULL" 
 575 char *SafeString(const wxString
& s
) 
 581     strcpy(wxBuffer
, "\""); 
 583     strcat(wxBuffer
, "\""); 
 588 // Returns quoted string or '' 
 589 char *SafeWord(const wxString
& s
) 
 595     strcpy(wxBuffer
, "'"); 
 596     strcat(wxBuffer
, (const char*) s
); 
 597     strcat(wxBuffer
, "'");