]> git.saurik.com Git - wxWidgets.git/blob - src/common/resource.cpp
1. wxTimer::Start() returns bool in wxGTK too (other minor corrections
[wxWidgets.git] / src / common / resource.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: resource.cpp
3 // Purpose: Resource system
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "resource.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/defs.h"
25 #include "wx/setup.h"
26 #include "wx/list.h"
27 #include "wx/hash.h"
28 #include "wx/gdicmn.h"
29 #include "wx/utils.h"
30 #include "wx/types.h"
31 #include "wx/menu.h"
32 #include "wx/stattext.h"
33 #include "wx/button.h"
34 #include "wx/bmpbuttn.h"
35 #include "wx/radiobox.h"
36 #include "wx/listbox.h"
37 #include "wx/choice.h"
38 #include "wx/checkbox.h"
39 #include "wx/slider.h"
40 #include "wx/statbox.h"
41 #if wxUSE_GAUGE
42 #include "wx/gauge.h"
43 #endif
44 #include "wx/textctrl.h"
45 #include "wx/msgdlg.h"
46 #include "wx/intl.h"
47 #endif
48
49 #if wxUSE_SCROLLBAR
50 #include "wx/scrolbar.h"
51 #endif
52
53 #if wxUSE_COMBOBOX
54 #include "wx/combobox.h"
55 #endif
56
57 #include "wx/validate.h"
58
59 #include "wx/log.h"
60
61 #if wxUSE_WX_RESOURCES
62
63 #include <ctype.h>
64 #include <math.h>
65 #include <stdlib.h>
66 #include <string.h>
67
68 #include "wx/resource.h"
69 #include "wx/string.h"
70 #include "wx/wxexpr.h"
71
72 #include "wx/settings.h"
73
74 // Forward (private) declarations
75 bool wxResourceInterpretResources(wxResourceTable& table, wxExprDatabase& db);
76 wxItemResource *wxResourceInterpretDialog(wxResourceTable& table, wxExpr *expr, bool isPanel = FALSE);
77 wxItemResource *wxResourceInterpretControl(wxResourceTable& table, wxExpr *expr);
78 wxItemResource *wxResourceInterpretMenu(wxResourceTable& table, wxExpr *expr);
79 wxItemResource *wxResourceInterpretMenuBar(wxResourceTable& table, wxExpr *expr);
80 wxItemResource *wxResourceInterpretString(wxResourceTable& table, wxExpr *expr);
81 wxItemResource *wxResourceInterpretBitmap(wxResourceTable& table, wxExpr *expr);
82 wxItemResource *wxResourceInterpretIcon(wxResourceTable& table, wxExpr *expr);
83 // Interpret list expression
84 wxFont wxResourceInterpretFontSpec(wxExpr *expr);
85
86 bool wxResourceReadOneResource(FILE *fd, wxExprDatabase& db, bool *eof, wxResourceTable *table = (wxResourceTable *) NULL);
87 bool wxResourceParseIncludeFile(const wxString& f, wxResourceTable *table = (wxResourceTable *) NULL);
88
89 wxResourceTable *wxDefaultResourceTable = (wxResourceTable *) NULL;
90
91 static char *wxResourceBuffer = (char *) NULL;
92 static long wxResourceBufferSize = 0;
93 static long wxResourceBufferCount = 0;
94 static int wxResourceStringPtr = 0;
95
96 void wxInitializeResourceSystem()
97 {
98 wxDefaultResourceTable = new wxResourceTable;
99 }
100
101 void wxCleanUpResourceSystem()
102 {
103 delete wxDefaultResourceTable;
104 if (wxResourceBuffer)
105 delete[] wxResourceBuffer;
106 }
107
108 void wxLogWarning(char *msg)
109 {
110 wxMessageBox(msg, _("Warning"), wxOK);
111 }
112
113 #if !USE_SHARED_LIBRARY
114 IMPLEMENT_DYNAMIC_CLASS(wxItemResource, wxObject)
115 IMPLEMENT_DYNAMIC_CLASS(wxResourceTable, wxHashTable)
116 #endif
117
118 wxItemResource::wxItemResource()
119 {
120 m_itemType = "";
121 m_title = "";
122 m_name = "";
123 m_windowStyle = 0;
124 m_x = m_y = m_width = m_height = 0;
125 m_value1 = m_value2 = m_value3 = m_value5 = 0;
126 m_value4 = "";
127 m_windowId = 0;
128 m_exStyle = 0;
129 }
130
131 wxItemResource::~wxItemResource()
132 {
133 wxNode *node = m_children.First();
134 while (node)
135 {
136 wxItemResource *item = (wxItemResource *)node->Data();
137 delete item;
138 delete node;
139 node = m_children.First();
140 }
141 }
142
143 /*
144 * Resource table
145 */
146
147 wxResourceTable::wxResourceTable():wxHashTable(wxKEY_STRING), identifiers(wxKEY_STRING)
148 {
149 }
150
151 wxResourceTable::~wxResourceTable()
152 {
153 ClearTable();
154 }
155
156 wxItemResource *wxResourceTable::FindResource(const wxString& name) const
157 {
158 wxItemResource *item = (wxItemResource *)Get((char *)(const char *)name);
159 return item;
160 }
161
162 void wxResourceTable::AddResource(wxItemResource *item)
163 {
164 wxString name = item->GetName();
165 if (name == "")
166 name = item->GetTitle();
167 if (name == "")
168 name = "no name";
169
170 // Delete existing resource, if any.
171 Delete(name);
172
173 Put(name, item);
174 }
175
176 bool wxResourceTable::DeleteResource(const wxString& name)
177 {
178 wxItemResource *item = (wxItemResource *)Delete((char *)(const char *)name);
179 if (item)
180 {
181 // See if any resource has this as its child; if so, delete from
182 // parent's child list.
183 BeginFind();
184 wxNode *node = (wxNode *) NULL;
185 while ((node = Next()))
186 {
187 wxItemResource *parent = (wxItemResource *)node->Data();
188 if (parent->GetChildren().Member(item))
189 {
190 parent->GetChildren().DeleteObject(item);
191 break;
192 }
193 }
194
195 delete item;
196 return TRUE;
197 }
198 else
199 return FALSE;
200 }
201
202 bool wxResourceTable::ParseResourceFile(const wxString& filename)
203 {
204 wxExprDatabase db;
205
206 FILE *fd = fopen((const char*) filename, "r");
207 if (!fd)
208 return FALSE;
209 bool eof = FALSE;
210 while (wxResourceReadOneResource(fd, db, &eof, this) && !eof)
211 {
212 // Loop
213 }
214 fclose(fd);
215 return wxResourceInterpretResources(*this, db);
216 }
217
218 bool wxResourceTable::ParseResourceData(const wxString& data)
219 {
220 wxExprDatabase db;
221 if (!db.ReadFromString(data))
222 {
223 wxLogWarning(_("Ill-formed resource file syntax."));
224 return FALSE;
225 }
226
227 return wxResourceInterpretResources(*this, db);
228 }
229
230 bool wxResourceTable::RegisterResourceBitmapData(const wxString& name, char bits[], int width, int height)
231 {
232 // Register pre-loaded bitmap data
233 wxItemResource *item = new wxItemResource;
234 // item->SetType(wxRESOURCE_TYPE_XBM_DATA);
235 item->SetType("wxXBMData");
236 item->SetName(name);
237 item->SetValue1((long)bits);
238 item->SetValue2((long)width);
239 item->SetValue3((long)height);
240 AddResource(item);
241 return TRUE;
242 }
243
244 bool wxResourceTable::RegisterResourceBitmapData(const wxString& name, char **data)
245 {
246 // Register pre-loaded bitmap data
247 wxItemResource *item = new wxItemResource;
248 // item->SetType(wxRESOURCE_TYPE_XPM_DATA);
249 item->SetType("wxXPMData");
250 item->SetName(name);
251 item->SetValue1((long)data);
252 AddResource(item);
253 return TRUE;
254 }
255
256 bool wxResourceTable::SaveResource(const wxString& WXUNUSED(filename))
257 {
258 return FALSE;
259 }
260
261 void wxResourceTable::ClearTable()
262 {
263 BeginFind();
264 wxNode *node = Next();
265 while (node)
266 {
267 wxNode *next = Next();
268 wxItemResource *item = (wxItemResource *)node->Data();
269 delete item;
270 delete node;
271 node = next;
272 }
273 }
274
275 wxControl *wxResourceTable::CreateItem(wxWindow *parent, const wxItemResource* childResource, const wxItemResource* parentResource) const
276 {
277 int id = childResource->GetId();
278 if ( id == 0 )
279 id = -1;
280
281 bool dlgUnits = ((parentResource->GetResourceStyle() & wxRESOURCE_DIALOG_UNITS) != 0);
282
283 wxControl *control = (wxControl *) NULL;
284 wxString itemType(childResource->GetType());
285
286 wxPoint pos;
287 wxSize size;
288 if (dlgUnits)
289 {
290 pos = parent->ConvertDialogToPixels(wxPoint(childResource->GetX(), childResource->GetY()));
291 size = parent->ConvertDialogToPixels(wxSize(childResource->GetWidth(), childResource->GetHeight()));
292 }
293 else
294 {
295 pos = wxPoint(childResource->GetX(), childResource->GetY());
296 size = wxSize(childResource->GetWidth(), childResource->GetHeight());
297 }
298
299 if (itemType == wxString("wxButton") || itemType == wxString("wxBitmapButton"))
300 {
301 if (childResource->GetValue4() != "")
302 {
303 // Bitmap button
304 wxBitmap bitmap = childResource->GetBitmap();
305 if (!bitmap.Ok())
306 {
307 bitmap = wxResourceCreateBitmap(childResource->GetValue4(), (wxResourceTable *)this);
308 ((wxItemResource*) childResource)->SetBitmap(bitmap);
309 }
310 if (bitmap.Ok())
311 control = new wxBitmapButton(parent, id, bitmap, pos, size,
312 childResource->GetStyle(), wxDefaultValidator, childResource->GetName());
313 }
314 else
315 // Normal, text button
316 control = new wxButton(parent, id, childResource->GetTitle(), pos, size,
317 childResource->GetStyle(), wxDefaultValidator, childResource->GetName());
318 }
319 else if (itemType == wxString("wxMessage") || itemType == wxString("wxStaticText") ||
320 itemType == wxString("wxStaticBitmap"))
321 {
322 if (childResource->GetValue4() != "")
323 {
324 // Bitmap message
325 wxBitmap bitmap = childResource->GetBitmap();
326 if (!bitmap.Ok())
327 {
328 bitmap = wxResourceCreateBitmap(childResource->GetValue4(), (wxResourceTable *)this);
329 ((wxItemResource*) childResource)->SetBitmap(bitmap);
330 }
331 #if wxUSE_BITMAP_MESSAGE
332 if (bitmap.Ok())
333 control = new wxStaticBitmap(parent, id, bitmap, pos, size,
334 childResource->GetStyle(), childResource->GetName());
335 #endif
336 }
337 else
338 {
339 control = new wxStaticText(parent, id, childResource->GetTitle(), pos, size,
340 childResource->GetStyle(), childResource->GetName());
341 }
342 }
343 else if (itemType == wxString("wxText") || itemType == wxString("wxTextCtrl") || itemType == wxString("wxMultiText"))
344 {
345 control = new wxTextCtrl(parent, id, childResource->GetValue4(), pos, size,
346 childResource->GetStyle(), wxDefaultValidator, childResource->GetName());
347 }
348 else if (itemType == wxString("wxCheckBox"))
349 {
350 control = new wxCheckBox(parent, id, childResource->GetTitle(), pos, size,
351 childResource->GetStyle(), wxDefaultValidator, childResource->GetName());
352
353 ((wxCheckBox *)control)->SetValue((childResource->GetValue1() != 0));
354 }
355 #if wxUSE_GAUGE
356 else if (itemType == wxString("wxGauge"))
357 {
358 control = new wxGauge(parent, id, (int)childResource->GetValue2(), pos, size,
359 childResource->GetStyle(), wxDefaultValidator, childResource->GetName());
360
361 ((wxGauge *)control)->SetValue((int)childResource->GetValue1());
362 }
363 #endif
364 #if wxUSE_RADIOBUTTON
365 else if (itemType == wxString("wxRadioButton"))
366 {
367 control = new wxRadioButton(parent, id, childResource->GetTitle(), // (int)childResource->GetValue1(),
368 pos, size,
369 childResource->GetStyle(), wxDefaultValidator, childResource->GetName());
370 }
371 #endif
372 #if wxUSE_SCROLLBAR
373 else if (itemType == wxString("wxScrollBar"))
374 {
375 control = new wxScrollBar(parent, id, pos, size,
376 childResource->GetStyle(), wxDefaultValidator, childResource->GetName());
377 ((wxScrollBar *)control)->SetValue((int)childResource->GetValue1());
378 ((wxScrollBar *)control)->SetPageSize((int)childResource->GetValue2());
379 ((wxScrollBar *)control)->SetObjectLength((int)childResource->GetValue3());
380 ((wxScrollBar *)control)->SetViewLength((int)(long)childResource->GetValue5());
381 }
382 #endif
383 else if (itemType == wxString("wxSlider"))
384 {
385 control = new wxSlider(parent, id, (int)childResource->GetValue1(),
386 (int)childResource->GetValue2(), (int)childResource->GetValue3(), pos, size,
387 childResource->GetStyle(), wxDefaultValidator, childResource->GetName());
388 }
389 else if (itemType == wxString("wxGroupBox") || itemType == wxString("wxStaticBox"))
390 {
391 control = new wxStaticBox(parent, id, childResource->GetTitle(), pos, size,
392 childResource->GetStyle(), childResource->GetName());
393 }
394 else if (itemType == wxString("wxListBox"))
395 {
396 wxStringList& stringList = childResource->GetStringValues();
397 wxString *strings = (wxString *) NULL;
398 int noStrings = 0;
399 if (stringList.Number() > 0)
400 {
401 noStrings = stringList.Number();
402 strings = new wxString[noStrings];
403 wxNode *node = stringList.First();
404 int i = 0;
405 while (node)
406 {
407 strings[i] = (char *)node->Data();
408 i ++;
409 node = node->Next();
410 }
411 }
412 control = new wxListBox(parent, id, pos, size,
413 noStrings, strings, childResource->GetStyle(), wxDefaultValidator, childResource->GetName());
414
415 if (strings)
416 delete[] strings;
417 }
418 else if (itemType == wxString("wxChoice"))
419 {
420 wxStringList& stringList = childResource->GetStringValues();
421 wxString *strings = (wxString *) NULL;
422 int noStrings = 0;
423 if (stringList.Number() > 0)
424 {
425 noStrings = stringList.Number();
426 strings = new wxString[noStrings];
427 wxNode *node = stringList.First();
428 int i = 0;
429 while (node)
430 {
431 strings[i] = (char *)node->Data();
432 i ++;
433 node = node->Next();
434 }
435 }
436 control = new wxChoice(parent, id, pos, size,
437 noStrings, strings, childResource->GetStyle(), wxDefaultValidator, childResource->GetName());
438
439 if (strings)
440 delete[] strings;
441 }
442 #if wxUSE_COMBOBOX
443 else if (itemType == wxString("wxComboBox"))
444 {
445 wxStringList& stringList = childResource->GetStringValues();
446 wxString *strings = (wxString *) NULL;
447 int noStrings = 0;
448 if (stringList.Number() > 0)
449 {
450 noStrings = stringList.Number();
451 strings = new wxString[noStrings];
452 wxNode *node = stringList.First();
453 int i = 0;
454 while (node)
455 {
456 strings[i] = (char *)node->Data();
457 i ++;
458 node = node->Next();
459 }
460 }
461 control = new wxComboBox(parent, id, childResource->GetValue4(), pos, size,
462 noStrings, strings, childResource->GetStyle(), wxDefaultValidator, childResource->GetName());
463
464 if (strings)
465 delete[] strings;
466 }
467 #endif
468 else if (itemType == wxString("wxRadioBox"))
469 {
470 wxStringList& stringList = childResource->GetStringValues();
471 wxString *strings = (wxString *) NULL;
472 int noStrings = 0;
473 if (stringList.Number() > 0)
474 {
475 noStrings = stringList.Number();
476 strings = new wxString[noStrings];
477 wxNode *node = stringList.First();
478 int i = 0;
479 while (node)
480 {
481 strings[i] = (char *)node->Data();
482 i ++;
483 node = node->Next();
484 }
485 }
486 control = new wxRadioBox(parent, (wxWindowID) id, wxString(childResource->GetTitle()), pos, size,
487 noStrings, strings, (int)childResource->GetValue1(), childResource->GetStyle(), wxDefaultValidator,
488 childResource->GetName());
489
490 if (strings)
491 delete[] strings;
492 }
493
494 if ((parentResource->GetResourceStyle() & wxRESOURCE_USE_DEFAULTS) != 0)
495 {
496 // Don't set font; will be inherited from parent.
497 }
498 else
499 {
500 if (control && childResource->GetFont().Ok())
501 control->SetFont(childResource->GetFont());
502 }
503 return control;
504 }
505
506 /*
507 * Interpret database as a series of resources
508 */
509
510 bool wxResourceInterpretResources(wxResourceTable& table, wxExprDatabase& db)
511 {
512 wxNode *node = db.First();
513 while (node)
514 {
515 wxExpr *clause = (wxExpr *)node->Data();
516 wxString functor(clause->Functor());
517
518 wxItemResource *item = (wxItemResource *) NULL;
519 if (functor == "dialog")
520 item = wxResourceInterpretDialog(table, clause);
521 else if (functor == "panel")
522 item = wxResourceInterpretDialog(table, clause, TRUE);
523 else if (functor == "menubar")
524 item = wxResourceInterpretMenuBar(table, clause);
525 else if (functor == "menu")
526 item = wxResourceInterpretMenu(table, clause);
527 else if (functor == "string")
528 item = wxResourceInterpretString(table, clause);
529 else if (functor == "bitmap")
530 item = wxResourceInterpretBitmap(table, clause);
531 else if (functor == "icon")
532 item = wxResourceInterpretIcon(table, clause);
533
534 if (item)
535 {
536 // Remove any existing resource of same name
537 if (item->GetName() != "")
538 table.DeleteResource(item->GetName());
539 table.AddResource(item);
540 }
541 node = node->Next();
542 }
543 return TRUE;
544 }
545
546 static char *g_ValidControlClasses[] = { "wxButton", "wxBitmapButton", "wxMessage",
547 "wxStaticText", "wxStaticBitmap", "wxText", "wxTextCtrl", "wxMultiText",
548 "wxListBox", "wxRadioBox", "wxRadioButton", "wxCheckBox", "wxBitmapCheckBox",
549 "wxGroupBox", "wxStaticBox", "wxSlider", "wxGauge", "wxScrollBar",
550 "wxChoice", "wxComboBox" } ;
551 static int g_ValidControlClassesCount = sizeof(g_ValidControlClasses) / sizeof(char *) ;
552
553 static bool wxIsValidControlClass(const wxString& c)
554 {
555 int i;
556 for ( i = 0; i < g_ValidControlClassesCount; i++)
557 {
558 if ( c == g_ValidControlClasses[i] )
559 return TRUE;
560 }
561 return FALSE;
562 }
563
564 wxItemResource *wxResourceInterpretDialog(wxResourceTable& table, wxExpr *expr, bool isPanel)
565 {
566 wxItemResource *dialogItem = new wxItemResource;
567 if (isPanel)
568 dialogItem->SetType("wxPanel");
569 else
570 dialogItem->SetType("wxDialog");
571 wxString style = "";
572 wxString title = "";
573 wxString name = "";
574 wxString backColourHex = "";
575 wxString labelColourHex = "";
576 wxString buttonColourHex = "";
577
578 long windowStyle = wxDEFAULT_DIALOG_STYLE;
579 if (isPanel)
580 windowStyle = 0;
581
582 int x = 0; int y = 0; int width = -1; int height = -1;
583 int isModal = 0;
584 wxExpr *labelFontExpr = (wxExpr *) NULL;
585 wxExpr *buttonFontExpr = (wxExpr *) NULL;
586 wxExpr *fontExpr = (wxExpr *) NULL;
587 expr->GetAttributeValue("style", style);
588 expr->GetAttributeValue("name", name);
589 expr->GetAttributeValue("title", title);
590 expr->GetAttributeValue("x", x);
591 expr->GetAttributeValue("y", y);
592 expr->GetAttributeValue("width", width);
593 expr->GetAttributeValue("height", height);
594 expr->GetAttributeValue("modal", isModal);
595 expr->GetAttributeValue("label_font", &labelFontExpr);
596 expr->GetAttributeValue("button_font", &buttonFontExpr);
597 expr->GetAttributeValue("font", &fontExpr);
598 expr->GetAttributeValue("background_colour", backColourHex);
599 expr->GetAttributeValue("label_colour", labelColourHex);
600 expr->GetAttributeValue("button_colour", buttonColourHex);
601
602 int useDialogUnits = 0;
603 expr->GetAttributeValue("use_dialog_units", useDialogUnits);
604 if (useDialogUnits != 0)
605 dialogItem->SetResourceStyle(dialogItem->GetResourceStyle() | wxRESOURCE_DIALOG_UNITS);
606
607 int useDefaults = 0;
608 expr->GetAttributeValue("use_system_defaults", useDefaults);
609 if (useDefaults != 0)
610 dialogItem->SetResourceStyle(dialogItem->GetResourceStyle() | wxRESOURCE_USE_DEFAULTS);
611
612 long id = 0;
613 expr->GetAttributeValue("id", id);
614 dialogItem->SetId(id);
615
616 if (style != "")
617 {
618 windowStyle = wxParseWindowStyle(style);
619 }
620 dialogItem->SetStyle(windowStyle);
621 dialogItem->SetValue1(isModal);
622 dialogItem->SetName(name);
623 dialogItem->SetTitle(title);
624 dialogItem->SetSize(x, y, width, height);
625
626 if (backColourHex != "")
627 {
628 int r = 0;
629 int g = 0;
630 int b = 0;
631 r = wxHexToDec(backColourHex.Mid(0, 2));
632 g = wxHexToDec(backColourHex.Mid(2, 2));
633 b = wxHexToDec(backColourHex.Mid(4, 2));
634 dialogItem->SetBackgroundColour(wxColour((unsigned char)r,(unsigned char)g,(unsigned char)b));
635 }
636 if (labelColourHex != "")
637 {
638 int r = 0;
639 int g = 0;
640 int b = 0;
641 r = wxHexToDec(labelColourHex.Mid(0, 2));
642 g = wxHexToDec(labelColourHex.Mid(2, 2));
643 b = wxHexToDec(labelColourHex.Mid(4, 2));
644 dialogItem->SetLabelColour(wxColour((unsigned char)r,(unsigned char)g,(unsigned char)b));
645 }
646 if (buttonColourHex != "")
647 {
648 int r = 0;
649 int g = 0;
650 int b = 0;
651 r = wxHexToDec(buttonColourHex.Mid(0, 2));
652 g = wxHexToDec(buttonColourHex.Mid(2, 2));
653 b = wxHexToDec(buttonColourHex.Mid(4, 2));
654 dialogItem->SetButtonColour(wxColour((unsigned char)r,(unsigned char)g,(unsigned char)b));
655 }
656
657 if (fontExpr)
658 dialogItem->SetFont(wxResourceInterpretFontSpec(fontExpr));
659 else if (buttonFontExpr)
660 dialogItem->SetFont(wxResourceInterpretFontSpec(buttonFontExpr));
661 else if (labelFontExpr)
662 dialogItem->SetFont(wxResourceInterpretFontSpec(labelFontExpr));
663
664 // Now parse all controls
665 wxExpr *controlExpr = expr->GetFirst();
666 while (controlExpr)
667 {
668 if (controlExpr->Number() == 3)
669 {
670 wxString controlKeyword(controlExpr->Nth(1)->StringValue());
671 if (controlKeyword != "" && controlKeyword == "control")
672 {
673 // The value part: always a list.
674 wxExpr *listExpr = controlExpr->Nth(2);
675 if (listExpr->Type() == PrologList)
676 {
677 wxItemResource *controlItem = wxResourceInterpretControl(table, listExpr);
678 if (controlItem)
679 {
680 dialogItem->GetChildren().Append(controlItem);
681 }
682 }
683 }
684 }
685 controlExpr = controlExpr->GetNext();
686 }
687 return dialogItem;
688 }
689
690 wxItemResource *wxResourceInterpretControl(wxResourceTable& table, wxExpr *expr)
691 {
692 wxItemResource *controlItem = new wxItemResource;
693
694 // First, find the standard features of a control definition:
695 // [optional integer/string id], control name, title, style, name, x, y, width, height
696
697 wxString controlType;
698 wxString style;
699 wxString title;
700 wxString name;
701 int id = 0;
702 long windowStyle = 0;
703 int x = 0; int y = 0; int width = -1; int height = -1;
704 int count = 0;
705
706 wxExpr *expr1 = expr->Nth(0);
707
708 if ( expr1->Type() == PrologString || expr1->Type() == PrologWord )
709 {
710 if ( wxIsValidControlClass(expr1->StringValue()) )
711 {
712 count = 1;
713 controlType = expr1->StringValue();
714 }
715 else
716 {
717 wxString str(expr1->StringValue());
718 id = wxResourceGetIdentifier(str, &table);
719 if (id == 0)
720 {
721 wxLogWarning(_("Could not resolve control class or id '%s'. "
722 "Use (non-zero) integer instead\n or provide #define "
723 "(see manual for caveats)"),
724 (const char*) expr1->StringValue());
725 delete controlItem;
726 return (wxItemResource *) NULL;
727 }
728 else
729 {
730 // Success - we have an id, so the 2nd element must be the control class.
731 controlType = expr->Nth(1)->StringValue();
732 count = 2;
733 }
734 }
735 }
736 else if (expr1->Type() == PrologInteger)
737 {
738 id = (int)expr1->IntegerValue();
739 // Success - we have an id, so the 2nd element must be the control class.
740 controlType = expr->Nth(1)->StringValue();
741 count = 2;
742 }
743
744 expr1 = expr->Nth(count);
745 count ++;
746 if ( expr1 )
747 title = expr1->StringValue();
748
749 expr1 = expr->Nth(count);
750 count ++;
751 if (expr1)
752 {
753 style = expr1->StringValue();
754 windowStyle = wxParseWindowStyle(style);
755 }
756
757 expr1 = expr->Nth(count);
758 count ++;
759 if (expr1)
760 name = expr1->StringValue();
761
762 expr1 = expr->Nth(count);
763 count ++;
764 if (expr1)
765 x = (int)expr1->IntegerValue();
766
767 expr1 = expr->Nth(count);
768 count ++;
769 if (expr1)
770 y = (int)expr1->IntegerValue();
771
772 expr1 = expr->Nth(count);
773 count ++;
774 if (expr1)
775 width = (int)expr1->IntegerValue();
776
777 expr1 = expr->Nth(count);
778 count ++;
779 if (expr1)
780 height = (int)expr1->IntegerValue();
781
782 controlItem->SetStyle(windowStyle);
783 controlItem->SetName(name);
784 controlItem->SetTitle(title);
785 controlItem->SetSize(x, y, width, height);
786 controlItem->SetType(controlType);
787 controlItem->SetId(id);
788
789 if (controlType == "wxButton")
790 {
791 // Check for bitmap resource name
792 if (expr->Nth(count) && ((expr->Nth(count)->Type() == PrologString) || (expr->Nth(count)->Type() == PrologWord)))
793 {
794 wxString str(expr->Nth(count)->StringValue());
795 controlItem->SetValue4(str);
796 count ++;
797 if (expr->Nth(count) && expr->Nth(count)->Type() == PrologList)
798 controlItem->SetFont(wxResourceInterpretFontSpec(expr->Nth(count)));
799 }
800 }
801 else if (controlType == "wxCheckBox")
802 {
803 // Check for default value
804 if (expr->Nth(count) && (expr->Nth(count)->Type() == PrologInteger))
805 {
806 controlItem->SetValue1(expr->Nth(count)->IntegerValue());
807 count ++;
808 if (expr->Nth(count) && expr->Nth(count)->Type() == PrologList)
809 controlItem->SetFont(wxResourceInterpretFontSpec(expr->Nth(count)));
810 }
811 }
812 #if wxUSE_RADIOBUTTON
813 else if (controlType == "wxRadioButton")
814 {
815 // Check for default value
816 if (expr->Nth(count) && (expr->Nth(count)->Type() == PrologInteger))
817 {
818 controlItem->SetValue1(expr->Nth(count)->IntegerValue());
819 count ++;
820 if (expr->Nth(count) && expr->Nth(count)->Type() == PrologList)
821 controlItem->SetFont(wxResourceInterpretFontSpec(expr->Nth(count)));
822 }
823 }
824 #endif
825 else if (controlType == "wxText" || controlType == "wxTextCtrl" || controlType == "wxMultiText")
826 {
827 // Check for default value
828 if (expr->Nth(count) && ((expr->Nth(count)->Type() == PrologString) || (expr->Nth(count)->Type() == PrologWord)))
829 {
830 wxString str(expr->Nth(count)->StringValue());
831 controlItem->SetValue4(str);
832 count ++;
833
834 if (expr->Nth(count) && expr->Nth(count)->Type() == PrologList)
835 {
836 // controlItem->SetLabelFont(wxResourceInterpretFontSpec(expr->Nth(count)));
837 // Do nothing - no label font any more
838 count ++;
839 if (expr->Nth(count) && expr->Nth(count)->Type() == PrologList)
840 controlItem->SetFont(wxResourceInterpretFontSpec(expr->Nth(count)));
841 }
842 }
843 }
844 else if (controlType == "wxMessage" || controlType == "wxStaticText")
845 {
846 // Check for bitmap resource name
847 if (expr->Nth(count) && ((expr->Nth(count)->Type() == PrologString) || (expr->Nth(count)->Type() == PrologWord)))
848 {
849 wxString str(expr->Nth(count)->StringValue());
850 controlItem->SetValue4(str);
851 count ++;
852 if (expr->Nth(count) && expr->Nth(count)->Type() == PrologList)
853 controlItem->SetFont(wxResourceInterpretFontSpec(expr->Nth(count)));
854 }
855 }
856 else if (controlType == "wxGroupBox" || controlType == "wxStaticBox")
857 {
858 if (expr->Nth(count) && expr->Nth(count)->Type() == PrologList)
859 controlItem->SetFont(wxResourceInterpretFontSpec(expr->Nth(count)));
860 }
861 else if (controlType == "wxGauge")
862 {
863 // Check for default value
864 if (expr->Nth(count) && (expr->Nth(count)->Type() == PrologInteger))
865 {
866 controlItem->SetValue1(expr->Nth(count)->IntegerValue());
867 count ++;
868
869 // Check for range
870 if (expr->Nth(count) && (expr->Nth(count)->Type() == PrologInteger))
871 {
872 controlItem->SetValue2(expr->Nth(count)->IntegerValue());
873 count ++;
874
875 if (expr->Nth(count) && expr->Nth(count)->Type() == PrologList)
876 {
877 // controlItem->SetLabelFont(wxResourceInterpretFontSpec(expr->Nth(count)));
878 // Do nothing
879 count ++;
880
881 if (expr->Nth(count) && expr->Nth(count)->Type() == PrologList)
882 controlItem->SetFont(wxResourceInterpretFontSpec(expr->Nth(count)));
883 }
884 }
885 }
886 }
887 else if (controlType == "wxSlider")
888 {
889 // Check for default value
890 if (expr->Nth(count) && (expr->Nth(count)->Type() == PrologInteger))
891 {
892 controlItem->SetValue1(expr->Nth(count)->IntegerValue());
893 count ++;
894
895 // Check for min
896 if (expr->Nth(count) && (expr->Nth(count)->Type() == PrologInteger))
897 {
898 controlItem->SetValue2(expr->Nth(count)->IntegerValue());
899 count ++;
900
901 // Check for max
902 if (expr->Nth(count) && (expr->Nth(count)->Type() == PrologInteger))
903 {
904 controlItem->SetValue3(expr->Nth(count)->IntegerValue());
905 count ++;
906
907 if (expr->Nth(count) && expr->Nth(count)->Type() == PrologList)
908 {
909 // controlItem->SetLabelFont(wxResourceInterpretFontSpec(expr->Nth(count)));
910 // do nothing
911 count ++;
912
913 if (expr->Nth(count) && expr->Nth(count)->Type() == PrologList)
914 controlItem->SetFont(wxResourceInterpretFontSpec(expr->Nth(count)));
915 }
916 }
917 }
918 }
919 }
920 else if (controlType == "wxScrollBar")
921 {
922 // DEFAULT VALUE
923 if (expr->Nth(count) && (expr->Nth(count)->Type() == PrologInteger))
924 {
925 controlItem->SetValue1(expr->Nth(count)->IntegerValue());
926 count ++;
927
928 // PAGE LENGTH
929 if (expr->Nth(count) && (expr->Nth(count)->Type() == PrologInteger))
930 {
931 controlItem->SetValue2(expr->Nth(count)->IntegerValue());
932 count ++;
933
934 // OBJECT LENGTH
935 if (expr->Nth(count) && (expr->Nth(count)->Type() == PrologInteger))
936 {
937 controlItem->SetValue3(expr->Nth(count)->IntegerValue());
938 count ++;
939
940 // VIEW LENGTH
941 if (expr->Nth(count) && (expr->Nth(count)->Type() == PrologInteger))
942 controlItem->SetValue5(expr->Nth(count)->IntegerValue());
943 }
944 }
945 }
946 }
947 else if (controlType == "wxListBox")
948 {
949 wxExpr *valueList = (wxExpr *) NULL;
950
951 if ((valueList = expr->Nth(count)) && (valueList->Type() == PrologList))
952 {
953 wxStringList stringList;
954 wxExpr *stringExpr = valueList->GetFirst();
955 while (stringExpr)
956 {
957 stringList.Add(stringExpr->StringValue());
958 stringExpr = stringExpr->GetNext();
959 }
960 controlItem->SetStringValues(stringList);
961 count ++;
962
963 // Check for wxSINGLE/wxMULTIPLE
964 wxExpr *mult = (wxExpr *) NULL;
965 controlItem->SetValue1(wxLB_SINGLE);
966 if ((mult = expr->Nth(count)) && ((mult->Type() == PrologString)||(mult->Type() == PrologWord)))
967 {
968 wxString m(mult->StringValue());
969 if (m == "wxMULTIPLE")
970 controlItem->SetValue1(wxLB_MULTIPLE);
971 else if (m == "wxEXTENDED")
972 controlItem->SetValue1(wxLB_EXTENDED);
973 count ++;
974 if (expr->Nth(count) && expr->Nth(count)->Type() == PrologList)
975 {
976 // controlItem->SetLabelFont(wxResourceInterpretFontSpec(expr->Nth(count)));
977 count ++;
978 if (expr->Nth(count) && expr->Nth(count)->Type() == PrologList)
979 controlItem->SetFont(wxResourceInterpretFontSpec(expr->Nth(count)));
980 }
981 }
982 }
983 }
984 else if (controlType == "wxChoice")
985 {
986 wxExpr *valueList = (wxExpr *) NULL;
987 // Check for default value list
988 if ((valueList = expr->Nth(count)) && (valueList->Type() == PrologList))
989 {
990 wxStringList stringList;
991 wxExpr *stringExpr = valueList->GetFirst();
992 while (stringExpr)
993 {
994 stringList.Add(stringExpr->StringValue());
995 stringExpr = stringExpr->GetNext();
996 }
997 controlItem->SetStringValues(stringList);
998
999 count ++;
1000
1001 if (expr->Nth(count) && expr->Nth(count)->Type() == PrologList)
1002 {
1003 // controlItem->SetLabelFont(wxResourceInterpretFontSpec(expr->Nth(count)));
1004 count ++;
1005
1006 if (expr->Nth(count) && expr->Nth(count)->Type() == PrologList)
1007 controlItem->SetFont(wxResourceInterpretFontSpec(expr->Nth(count)));
1008 }
1009 }
1010 }
1011 #if wxUSE_COMBOBOX
1012 else if (controlType == "wxComboBox")
1013 {
1014 wxExpr *textValue = expr->Nth(count);
1015 if (textValue && (textValue->Type() == PrologString || textValue->Type() == PrologWord))
1016 {
1017 wxString str(textValue->StringValue());
1018 controlItem->SetValue4(str);
1019
1020 count ++;
1021
1022 wxExpr *valueList = (wxExpr *) NULL;
1023 // Check for default value list
1024 if ((valueList = expr->Nth(count)) && (valueList->Type() == PrologList))
1025 {
1026 wxStringList stringList;
1027 wxExpr *stringExpr = valueList->GetFirst();
1028 while (stringExpr)
1029 {
1030 stringList.Add(stringExpr->StringValue());
1031 stringExpr = stringExpr->GetNext();
1032 }
1033 controlItem->SetStringValues(stringList);
1034
1035 count ++;
1036
1037 if (expr->Nth(count) && expr->Nth(count)->Type() == PrologList)
1038 {
1039 // controlItem->SetLabelFont(wxResourceInterpretFontSpec(expr->Nth(count)));
1040 count ++;
1041
1042 if (expr->Nth(count) && expr->Nth(count)->Type() == PrologList)
1043 controlItem->SetFont(wxResourceInterpretFontSpec(expr->Nth(count)));
1044 }
1045 }
1046 }
1047 }
1048 #endif
1049 #if 1
1050 else if (controlType == "wxRadioBox")
1051 {
1052 wxExpr *valueList = (wxExpr *) NULL;
1053 // Check for default value list
1054 if ((valueList = expr->Nth(count)) && (valueList->Type() == PrologList))
1055 {
1056 wxStringList stringList;
1057 wxExpr *stringExpr = valueList->GetFirst();
1058 while (stringExpr)
1059 {
1060 stringList.Add(stringExpr->StringValue());
1061 stringExpr = stringExpr->GetNext();
1062 }
1063 controlItem->SetStringValues(stringList);
1064 count ++;
1065
1066 // majorDim (number of rows or cols)
1067 if (expr->Nth(count) && (expr->Nth(count)->Type() == PrologInteger))
1068 {
1069 controlItem->SetValue1(expr->Nth(count)->IntegerValue());
1070 count ++;
1071 }
1072 else
1073 controlItem->SetValue1(0);
1074
1075 if (expr->Nth(count) && expr->Nth(count)->Type() == PrologList)
1076 {
1077 // controlItem->SetLabelFont(wxResourceInterpretFontSpec(expr->Nth(count)));
1078 count ++;
1079
1080 if (expr->Nth(count) && expr->Nth(count)->Type() == PrologList)
1081 controlItem->SetFont(wxResourceInterpretFontSpec(expr->Nth(count)));
1082 }
1083 }
1084 }
1085 #endif
1086 else
1087 {
1088 delete controlItem;
1089 return (wxItemResource *) NULL;
1090 }
1091 return controlItem;
1092 }
1093
1094 // Forward declaration
1095 wxItemResource *wxResourceInterpretMenu1(wxResourceTable& table, wxExpr *expr);
1096
1097 /*
1098 * Interpet a menu item
1099 */
1100
1101 wxItemResource *wxResourceInterpretMenuItem(wxResourceTable& table, wxExpr *expr)
1102 {
1103 wxItemResource *item = new wxItemResource;
1104
1105 wxExpr *labelExpr = expr->Nth(0);
1106 wxExpr *idExpr = expr->Nth(1);
1107 wxExpr *helpExpr = expr->Nth(2);
1108 wxExpr *checkableExpr = expr->Nth(3);
1109
1110 // Further keywords/attributes to follow sometime...
1111 if (expr->Number() == 0)
1112 {
1113 // item->SetType(wxRESOURCE_TYPE_SEPARATOR);
1114 item->SetType("wxMenuSeparator");
1115 return item;
1116 }
1117 else
1118 {
1119 // item->SetType(wxTYPE_MENU); // Well, menu item, but doesn't matter.
1120 item->SetType("wxMenu"); // Well, menu item, but doesn't matter.
1121 if (labelExpr)
1122 {
1123 wxString str(labelExpr->StringValue());
1124 item->SetTitle(str);
1125 }
1126 if (idExpr)
1127 {
1128 int id = 0;
1129 // If a string or word, must look up in identifier table.
1130 if ((idExpr->Type() == PrologString) || (idExpr->Type() == PrologWord))
1131 {
1132 wxString str(idExpr->StringValue());
1133 id = wxResourceGetIdentifier(str, &table);
1134 if (id == 0)
1135 {
1136 wxLogWarning(_("Could not resolve menu id '%s'. "
1137 "Use (non-zero) integer instead\n"
1138 "or provide #define (see manual for caveats)"),
1139 (const char*) idExpr->StringValue());
1140 }
1141 }
1142 else if (idExpr->Type() == PrologInteger)
1143 id = (int)idExpr->IntegerValue();
1144 item->SetValue1(id);
1145 }
1146 if (helpExpr)
1147 {
1148 wxString str(helpExpr->StringValue());
1149 item->SetValue4(str);
1150 }
1151 if (checkableExpr)
1152 item->SetValue2(checkableExpr->IntegerValue());
1153
1154 // Find the first expression that's a list, for submenu
1155 wxExpr *subMenuExpr = expr->GetFirst();
1156 while (subMenuExpr && (subMenuExpr->Type() != PrologList))
1157 subMenuExpr = subMenuExpr->GetNext();
1158
1159 while (subMenuExpr)
1160 {
1161 wxItemResource *child = wxResourceInterpretMenuItem(table, subMenuExpr);
1162 item->GetChildren().Append(child);
1163 subMenuExpr = subMenuExpr->GetNext();
1164 }
1165 }
1166 return item;
1167 }
1168
1169 /*
1170 * Interpret a nested list as a menu
1171 */
1172 /*
1173 wxItemResource *wxResourceInterpretMenu1(wxResourceTable& table, wxExpr *expr)
1174 {
1175 wxItemResource *menu = new wxItemResource;
1176 // menu->SetType(wxTYPE_MENU);
1177 menu->SetType("wxMenu");
1178 wxExpr *element = expr->GetFirst();
1179 while (element)
1180 {
1181 wxItemResource *item = wxResourceInterpretMenuItem(table, element);
1182 if (item)
1183 menu->GetChildren().Append(item);
1184 element = element->GetNext();
1185 }
1186 return menu;
1187 }
1188 */
1189
1190 wxItemResource *wxResourceInterpretMenu(wxResourceTable& table, wxExpr *expr)
1191 {
1192 wxExpr *listExpr = (wxExpr *) NULL;
1193 expr->GetAttributeValue("menu", &listExpr);
1194 if (!listExpr)
1195 return (wxItemResource *) NULL;
1196
1197 wxItemResource *menuResource = wxResourceInterpretMenuItem(table, listExpr);
1198
1199 if (!menuResource)
1200 return (wxItemResource *) NULL;
1201
1202 wxString name;
1203 if (expr->GetAttributeValue("name", name))
1204 {
1205 menuResource->SetName(name);
1206 }
1207
1208 return menuResource;
1209 }
1210
1211 wxItemResource *wxResourceInterpretMenuBar(wxResourceTable& table, wxExpr *expr)
1212 {
1213 wxExpr *listExpr = (wxExpr *) NULL;
1214 expr->GetAttributeValue("menu", &listExpr);
1215 if (!listExpr)
1216 return (wxItemResource *) NULL;
1217
1218 wxItemResource *resource = new wxItemResource;
1219 resource->SetType("wxMenu");
1220 // resource->SetType(wxTYPE_MENU);
1221
1222 wxExpr *element = listExpr->GetFirst();
1223 while (element)
1224 {
1225 wxItemResource *menuResource = wxResourceInterpretMenuItem(table, listExpr);
1226 resource->GetChildren().Append(menuResource);
1227 element = element->GetNext();
1228 }
1229
1230 wxString name;
1231 if (expr->GetAttributeValue("name", name))
1232 {
1233 resource->SetName(name);
1234 }
1235
1236 return resource;
1237 }
1238
1239 wxItemResource *wxResourceInterpretString(wxResourceTable& WXUNUSED(table), wxExpr *WXUNUSED(expr))
1240 {
1241 return (wxItemResource *) NULL;
1242 }
1243
1244 wxItemResource *wxResourceInterpretBitmap(wxResourceTable& WXUNUSED(table), wxExpr *expr)
1245 {
1246 wxItemResource *bitmapItem = new wxItemResource;
1247 // bitmapItem->SetType(wxTYPE_BITMAP);
1248 bitmapItem->SetType("wxBitmap");
1249 wxString name;
1250 if (expr->GetAttributeValue("name", name))
1251 {
1252 bitmapItem->SetName(name);
1253 }
1254 // Now parse all bitmap specifications
1255 wxExpr *bitmapExpr = expr->GetFirst();
1256 while (bitmapExpr)
1257 {
1258 if (bitmapExpr->Number() == 3)
1259 {
1260 wxString bitmapKeyword(bitmapExpr->Nth(1)->StringValue());
1261 if (bitmapKeyword == "bitmap" || bitmapKeyword == "icon")
1262 {
1263 // The value part: always a list.
1264 wxExpr *listExpr = bitmapExpr->Nth(2);
1265 if (listExpr->Type() == PrologList)
1266 {
1267 wxItemResource *bitmapSpec = new wxItemResource;
1268 // bitmapSpec->SetType(wxTYPE_BITMAP);
1269 bitmapSpec->SetType("wxBitmap");
1270
1271 // List is of form: [filename, bitmaptype, platform, colours, xresolution, yresolution]
1272 // where everything after 'filename' is optional.
1273 wxExpr *nameExpr = listExpr->Nth(0);
1274 wxExpr *typeExpr = listExpr->Nth(1);
1275 wxExpr *platformExpr = listExpr->Nth(2);
1276 wxExpr *coloursExpr = listExpr->Nth(3);
1277 wxExpr *xresExpr = listExpr->Nth(4);
1278 wxExpr *yresExpr = listExpr->Nth(5);
1279 if (nameExpr && nameExpr->StringValue())
1280 {
1281 bitmapSpec->SetName(nameExpr->StringValue());
1282 }
1283 if (typeExpr && typeExpr->StringValue())
1284 {
1285 bitmapSpec->SetValue1(wxParseWindowStyle(typeExpr->StringValue()));
1286 }
1287 else
1288 bitmapSpec->SetValue1(0);
1289
1290 if (platformExpr && platformExpr->StringValue())
1291 {
1292 wxString plat(platformExpr->StringValue());
1293 if (plat == "windows" || plat == "WINDOWS")
1294 bitmapSpec->SetValue2(RESOURCE_PLATFORM_WINDOWS);
1295 else if (plat == "x" || plat == "X")
1296 bitmapSpec->SetValue2(RESOURCE_PLATFORM_X);
1297 else if (plat == "mac" || plat == "MAC")
1298 bitmapSpec->SetValue2(RESOURCE_PLATFORM_MAC);
1299 else
1300 bitmapSpec->SetValue2(RESOURCE_PLATFORM_ANY);
1301 }
1302 else
1303 bitmapSpec->SetValue2(RESOURCE_PLATFORM_ANY);
1304
1305 if (coloursExpr)
1306 bitmapSpec->SetValue3(coloursExpr->IntegerValue());
1307 int xres = 0;
1308 int yres = 0;
1309 if (xresExpr)
1310 xres = (int)xresExpr->IntegerValue();
1311 if (yresExpr)
1312 yres = (int)yresExpr->IntegerValue();
1313 bitmapSpec->SetSize(0, 0, xres, yres);
1314
1315 bitmapItem->GetChildren().Append(bitmapSpec);
1316 }
1317 }
1318 }
1319 bitmapExpr = bitmapExpr->GetNext();
1320 }
1321
1322 return bitmapItem;
1323 }
1324
1325 wxItemResource *wxResourceInterpretIcon(wxResourceTable& table, wxExpr *expr)
1326 {
1327 wxItemResource *item = wxResourceInterpretBitmap(table, expr);
1328 if (item)
1329 {
1330 // item->SetType(wxTYPE_ICON);
1331 item->SetType("wxIcon");
1332 return item;
1333 }
1334 else
1335 return (wxItemResource *) NULL;
1336 }
1337
1338 // Interpret list expression as a font
1339 wxFont wxResourceInterpretFontSpec(wxExpr *expr)
1340 {
1341 if (expr->Type() != PrologList)
1342 return wxNullFont;
1343
1344 int point = 10;
1345 int family = wxSWISS;
1346 int style = wxNORMAL;
1347 int weight = wxNORMAL;
1348 int underline = 0;
1349 wxString faceName("");
1350
1351 wxExpr *pointExpr = expr->Nth(0);
1352 wxExpr *familyExpr = expr->Nth(1);
1353 wxExpr *styleExpr = expr->Nth(2);
1354 wxExpr *weightExpr = expr->Nth(3);
1355 wxExpr *underlineExpr = expr->Nth(4);
1356 wxExpr *faceNameExpr = expr->Nth(5);
1357 if (pointExpr)
1358 point = (int)pointExpr->IntegerValue();
1359
1360 wxString str;
1361 if (familyExpr)
1362 {
1363 str = familyExpr->StringValue();
1364 family = (int)wxParseWindowStyle(str);
1365 }
1366 if (styleExpr)
1367 {
1368 str = styleExpr->StringValue();
1369 style = (int)wxParseWindowStyle(str);
1370 }
1371 if (weightExpr)
1372 {
1373 str = weightExpr->StringValue();
1374 weight = (int)wxParseWindowStyle(str);
1375 }
1376 if (underlineExpr)
1377 underline = (int)underlineExpr->IntegerValue();
1378 if (faceNameExpr)
1379 faceName = faceNameExpr->StringValue();
1380
1381 wxFont font(point, family, style, weight, (underline != 0), faceName);
1382 return font;
1383 }
1384
1385 /*
1386 * (Re)allocate buffer for reading in from resource file
1387 */
1388
1389 bool wxReallocateResourceBuffer()
1390 {
1391 if (!wxResourceBuffer)
1392 {
1393 wxResourceBufferSize = 1000;
1394 wxResourceBuffer = new char[wxResourceBufferSize];
1395 return TRUE;
1396 }
1397 if (wxResourceBuffer)
1398 {
1399 long newSize = wxResourceBufferSize + 1000;
1400 char *tmp = new char[(int)newSize];
1401 strncpy(tmp, wxResourceBuffer, (int)wxResourceBufferCount);
1402 delete[] wxResourceBuffer;
1403 wxResourceBuffer = tmp;
1404 wxResourceBufferSize = newSize;
1405 }
1406 return TRUE;
1407 }
1408
1409 static bool wxEatWhiteSpace(FILE *fd)
1410 {
1411 int ch = getc(fd);
1412 if ((ch != ' ') && (ch != '/') && (ch != ' ') && (ch != 10) && (ch != 13) && (ch != 9))
1413 {
1414 ungetc(ch, fd);
1415 return TRUE;
1416 }
1417
1418 // Eat whitespace
1419 while (ch == ' ' || ch == 10 || ch == 13 || ch == 9)
1420 ch = getc(fd);
1421 // Check for comment
1422 if (ch == '/')
1423 {
1424 ch = getc(fd);
1425 if (ch == '*')
1426 {
1427 bool finished = FALSE;
1428 while (!finished)
1429 {
1430 ch = getc(fd);
1431 if (ch == EOF)
1432 return FALSE;
1433 if (ch == '*')
1434 {
1435 int newCh = getc(fd);
1436 if (newCh == '/')
1437 finished = TRUE;
1438 else
1439 {
1440 ungetc(newCh, fd);
1441 }
1442 }
1443 }
1444 }
1445 else // False alarm
1446 return FALSE;
1447 }
1448 else
1449 ungetc(ch, fd);
1450 return wxEatWhiteSpace(fd);
1451 }
1452
1453 bool wxGetResourceToken(FILE *fd)
1454 {
1455 if (!wxResourceBuffer)
1456 wxReallocateResourceBuffer();
1457 wxResourceBuffer[0] = 0;
1458 wxEatWhiteSpace(fd);
1459
1460 int ch = getc(fd);
1461 if (ch == '"')
1462 {
1463 // Get string
1464 wxResourceBufferCount = 0;
1465 ch = getc(fd);
1466 while (ch != '"')
1467 {
1468 int actualCh = ch;
1469 if (ch == EOF)
1470 {
1471 wxResourceBuffer[wxResourceBufferCount] = 0;
1472 return FALSE;
1473 }
1474 // Escaped characters
1475 else if (ch == '\\')
1476 {
1477 int newCh = getc(fd);
1478 if (newCh == '"')
1479 actualCh = '"';
1480 else if (newCh == 10)
1481 actualCh = 10;
1482 else
1483 {
1484 ungetc(newCh, fd);
1485 }
1486 }
1487
1488 if (wxResourceBufferCount >= wxResourceBufferSize-1)
1489 wxReallocateResourceBuffer();
1490 wxResourceBuffer[wxResourceBufferCount] = (char)actualCh;
1491 wxResourceBufferCount ++;
1492 ch = getc(fd);
1493 }
1494 wxResourceBuffer[wxResourceBufferCount] = 0;
1495 }
1496 else
1497 {
1498 wxResourceBufferCount = 0;
1499 // Any other token
1500 while (ch != ' ' && ch != EOF && ch != ' ' && ch != 13 && ch != 9 && ch != 10)
1501 {
1502 if (wxResourceBufferCount >= wxResourceBufferSize-1)
1503 wxReallocateResourceBuffer();
1504 wxResourceBuffer[wxResourceBufferCount] = (char)ch;
1505 wxResourceBufferCount ++;
1506
1507 ch = getc(fd);
1508 }
1509 wxResourceBuffer[wxResourceBufferCount] = 0;
1510 if (ch == EOF)
1511 return FALSE;
1512 }
1513 return TRUE;
1514 }
1515
1516 /*
1517 * Files are in form:
1518 static char *name = "....";
1519 with possible comments.
1520 */
1521
1522 bool wxResourceReadOneResource(FILE *fd, wxExprDatabase& db, bool *eof, wxResourceTable *table)
1523 {
1524 if (!table)
1525 table = wxDefaultResourceTable;
1526
1527 // static or #define
1528 if (!wxGetResourceToken(fd))
1529 {
1530 *eof = TRUE;
1531 return FALSE;
1532 }
1533
1534 if (strcmp(wxResourceBuffer, "#define") == 0)
1535 {
1536 wxGetResourceToken(fd);
1537 char *name = copystring(wxResourceBuffer);
1538 wxGetResourceToken(fd);
1539 char *value = copystring(wxResourceBuffer);
1540 if (isalpha(value[0]))
1541 {
1542 int val = (int)atol(value);
1543 wxResourceAddIdentifier(name, val, table);
1544 }
1545 else
1546 {
1547 wxLogWarning(_("#define %s must be an integer."), name);
1548 delete[] name;
1549 delete[] value;
1550 return FALSE;
1551 }
1552 delete[] name;
1553 delete[] value;
1554
1555 return TRUE;
1556 }
1557 else if (strcmp(wxResourceBuffer, "#include") == 0)
1558 {
1559 wxGetResourceToken(fd);
1560 char *name = copystring(wxResourceBuffer);
1561 char *actualName = name;
1562 if (name[0] == '"')
1563 actualName = name + 1;
1564 int len = strlen(name);
1565 if ((len > 0) && (name[len-1] == '"'))
1566 name[len-1] = 0;
1567 if (!wxResourceParseIncludeFile(actualName, table))
1568 {
1569 wxLogWarning(_("Could not find resource include file %s."), actualName);
1570 }
1571 delete[] name;
1572 return TRUE;
1573 }
1574 else if (strcmp(wxResourceBuffer, "static") != 0)
1575 {
1576 char buf[300];
1577 strcpy(buf, _("Found "));
1578 strncat(buf, wxResourceBuffer, 30);
1579 strcat(buf, _(", expected static, #include or #define\nwhilst parsing resource."));
1580 wxLogWarning(buf);
1581 return FALSE;
1582 }
1583
1584 // char
1585 if (!wxGetResourceToken(fd))
1586 {
1587 wxLogWarning(_("Unexpected end of file whilst parsing resource."));
1588 *eof = TRUE;
1589 return FALSE;
1590 }
1591
1592 if (strcmp(wxResourceBuffer, "char") != 0)
1593 {
1594 wxLogWarning(_("Expected 'char' whilst parsing resource."));
1595 return FALSE;
1596 }
1597
1598 // *name
1599 if (!wxGetResourceToken(fd))
1600 {
1601 wxLogWarning(_("Unexpected end of file whilst parsing resource."));
1602 *eof = TRUE;
1603 return FALSE;
1604 }
1605
1606 if (wxResourceBuffer[0] != '*')
1607 {
1608 wxLogWarning(_("Expected '*' whilst parsing resource."));
1609 return FALSE;
1610 }
1611 char nameBuf[100];
1612 strncpy(nameBuf, wxResourceBuffer+1, 99);
1613
1614 // =
1615 if (!wxGetResourceToken(fd))
1616 {
1617 wxLogWarning(_("Unexpected end of file whilst parsing resource."));
1618 *eof = TRUE;
1619 return FALSE;
1620 }
1621
1622 if (strcmp(wxResourceBuffer, "=") != 0)
1623 {
1624 wxLogWarning(_("Expected '=' whilst parsing resource."));
1625 return FALSE;
1626 }
1627
1628 // String
1629 if (!wxGetResourceToken(fd))
1630 {
1631 wxLogWarning(_("Unexpected end of file whilst parsing resource."));
1632 *eof = TRUE;
1633 return FALSE;
1634 }
1635 else
1636 {
1637 if (!db.ReadPrologFromString(wxResourceBuffer))
1638 {
1639 wxLogWarning(_("%s: ill-formed resource file syntax."), nameBuf);
1640 return FALSE;
1641 }
1642 }
1643 // Semicolon
1644 if (!wxGetResourceToken(fd))
1645 {
1646 *eof = TRUE;
1647 }
1648 return TRUE;
1649 }
1650
1651 /*
1652 * Parses string window style into integer window style
1653 */
1654
1655 /*
1656 * Style flag parsing, e.g.
1657 * "wxSYSTEM_MENU | wxBORDER" -> integer
1658 */
1659
1660 char* wxResourceParseWord(char*s, int *i)
1661 {
1662 if (!s)
1663 return (char*) NULL;
1664
1665 static char buf[150];
1666 int len = strlen(s);
1667 int j = 0;
1668 int ii = *i;
1669 while ((ii < len) && (isalpha(s[ii]) || (s[ii] == '_')))
1670 {
1671 buf[j] = s[ii];
1672 j ++;
1673 ii ++;
1674 }
1675 buf[j] = 0;
1676
1677 // Eat whitespace and conjunction characters
1678 while ((ii < len) &&
1679 ((s[ii] == ' ') || (s[ii] == '|') || (s[ii] == ',')))
1680 {
1681 ii ++;
1682 }
1683 *i = ii;
1684 if (j == 0)
1685 return (char*) NULL;
1686 else
1687 return buf;
1688 }
1689
1690 struct wxResourceBitListStruct
1691 {
1692 char *word;
1693 long bits;
1694 };
1695
1696 static wxResourceBitListStruct wxResourceBitListTable[] =
1697 {
1698 /* wxListBox */
1699 { "wxSINGLE", wxLB_SINGLE },
1700 { "wxMULTIPLE", wxLB_MULTIPLE },
1701 { "wxEXTENDED", wxLB_EXTENDED },
1702 { "wxLB_SINGLE", wxLB_SINGLE },
1703 { "wxLB_MULTIPLE", wxLB_MULTIPLE },
1704 { "wxLB_EXTENDED", wxLB_EXTENDED },
1705 { "wxLB_NEEDED_SB", wxLB_NEEDED_SB },
1706 { "wxLB_ALWAYS_SB", wxLB_ALWAYS_SB },
1707 { "wxLB_SORT", wxLB_SORT },
1708 { "wxLB_OWNERDRAW", wxLB_OWNERDRAW },
1709 { "wxLB_HSCROLL", wxLB_HSCROLL },
1710
1711 /* wxComboxBox */
1712 { "wxCB_SIMPLE", wxCB_SIMPLE },
1713 { "wxCB_DROPDOWN", wxCB_DROPDOWN },
1714 { "wxCB_READONLY", wxCB_READONLY },
1715 { "wxCB_SORT", wxCB_SORT },
1716
1717 /* wxGauge */
1718 { "wxGA_PROGRESSBAR", wxGA_PROGRESSBAR },
1719 { "wxGA_HORIZONTAL", wxGA_HORIZONTAL },
1720 { "wxGA_VERTICAL", wxGA_VERTICAL },
1721
1722 /* wxTextCtrl */
1723 { "wxPASSWORD", wxPASSWORD},
1724 { "wxPROCESS_ENTER", wxPROCESS_ENTER},
1725 { "wxTE_PASSWORD", wxTE_PASSWORD},
1726 { "wxTE_READONLY", wxTE_READONLY},
1727 { "wxTE_PROCESS_ENTER", wxTE_PROCESS_ENTER},
1728 { "wxTE_MULTILINE", wxTE_MULTILINE},
1729
1730 /* wxRadioBox/wxRadioButton */
1731 { "wxRB_GROUP", wxRB_GROUP },
1732 { "wxRA_HORIZONTAL", wxRA_HORIZONTAL },
1733 { "wxRA_VERTICAL", wxRA_VERTICAL },
1734
1735 /* wxSlider */
1736 { "wxSL_HORIZONTAL", wxSL_HORIZONTAL },
1737 { "wxSL_VERTICAL", wxSL_VERTICAL },
1738 { "wxSL_AUTOTICKS", wxSL_AUTOTICKS },
1739 { "wxSL_LABELS", wxSL_LABELS },
1740 { "wxSL_LEFT", wxSL_LEFT },
1741 { "wxSL_TOP", wxSL_TOP },
1742 { "wxSL_RIGHT", wxSL_RIGHT },
1743 { "wxSL_BOTTOM", wxSL_BOTTOM },
1744 { "wxSL_BOTH", wxSL_BOTH },
1745 { "wxSL_SELRANGE", wxSL_SELRANGE },
1746
1747 /* wxScrollBar */
1748 { "wxSB_HORIZONTAL", wxSB_HORIZONTAL },
1749 { "wxSB_VERTICAL", wxSB_VERTICAL },
1750
1751 /* wxButton */
1752 { "wxBU_AUTODRAW", wxBU_AUTODRAW },
1753 { "wxBU_NOAUTODRAW", wxBU_NOAUTODRAW },
1754
1755 /* wxTreeCtrl */
1756 { "wxTR_HAS_BUTTONS", wxTR_HAS_BUTTONS },
1757 { "wxTR_EDIT_LABELS", wxTR_EDIT_LABELS },
1758 { "wxTR_LINES_AT_ROOT", wxTR_LINES_AT_ROOT },
1759
1760 /* wxListCtrl */
1761 { "wxLC_ICON", wxLC_ICON },
1762 { "wxLC_SMALL_ICON", wxLC_SMALL_ICON },
1763 { "wxLC_LIST", wxLC_LIST },
1764 { "wxLC_REPORT", wxLC_REPORT },
1765 { "wxLC_ALIGN_TOP", wxLC_ALIGN_TOP },
1766 { "wxLC_ALIGN_LEFT", wxLC_ALIGN_LEFT },
1767 { "wxLC_AUTOARRANGE", wxLC_AUTOARRANGE },
1768 { "wxLC_USER_TEXT", wxLC_USER_TEXT },
1769 { "wxLC_EDIT_LABELS", wxLC_EDIT_LABELS },
1770 { "wxLC_NO_HEADER", wxLC_NO_HEADER },
1771 { "wxLC_NO_SORT_HEADER", wxLC_NO_SORT_HEADER },
1772 { "wxLC_SINGLE_SEL", wxLC_SINGLE_SEL },
1773 { "wxLC_SORT_ASCENDING", wxLC_SORT_ASCENDING },
1774 { "wxLC_SORT_DESCENDING", wxLC_SORT_DESCENDING },
1775
1776 /* wxSpinButton */
1777 { "wxSP_VERTICAL", wxSP_VERTICAL},
1778 { "wxSP_HORIZONTAL", wxSP_HORIZONTAL},
1779 { "wxSP_ARROW_KEYS", wxSP_ARROW_KEYS},
1780 { "wxSP_WRAP", wxSP_WRAP},
1781
1782 /* wxSplitterWnd */
1783 { "wxSP_NOBORDER", wxSP_NOBORDER},
1784 { "wxSP_3D", wxSP_3D},
1785 { "wxSP_BORDER", wxSP_BORDER},
1786
1787 /* wxTabCtrl */
1788 { "wxTC_MULTILINE", wxTC_MULTILINE},
1789 { "wxTC_RIGHTJUSTIFY", wxTC_RIGHTJUSTIFY},
1790 { "wxTC_FIXEDWIDTH", wxTC_FIXEDWIDTH},
1791 { "wxTC_OWNERDRAW", wxTC_OWNERDRAW},
1792
1793 /* wxStatusBar95 */
1794 { "wxST_SIZEGRIP", wxST_SIZEGRIP},
1795
1796 /* wxControl */
1797 { "wxFIXED_LENGTH", wxFIXED_LENGTH},
1798 { "wxALIGN_LEFT", wxALIGN_LEFT},
1799 { "wxALIGN_CENTER", wxALIGN_CENTER},
1800 { "wxALIGN_CENTRE", wxALIGN_CENTRE},
1801 { "wxALIGN_RIGHT", wxALIGN_RIGHT},
1802 { "wxCOLOURED", wxCOLOURED},
1803
1804 /* wxToolBar */
1805 { "wxTB_3DBUTTONS", wxTB_3DBUTTONS},
1806 { "wxTB_HORIZONTAL", wxTB_HORIZONTAL},
1807 { "wxTB_VERTICAL", wxTB_VERTICAL},
1808 { "wxTB_FLAT", wxTB_FLAT},
1809
1810 /* Generic */
1811 { "wxVSCROLL", wxVSCROLL },
1812 { "wxHSCROLL", wxHSCROLL },
1813 { "wxCAPTION", wxCAPTION },
1814 { "wxSTAY_ON_TOP", wxSTAY_ON_TOP},
1815 { "wxICONIZE", wxICONIZE},
1816 { "wxMINIMIZE", wxICONIZE},
1817 { "wxMAXIMIZE", wxMAXIMIZE},
1818 { "wxSDI", 0},
1819 { "wxMDI_PARENT", 0},
1820 { "wxMDI_CHILD", 0},
1821 { "wxTHICK_FRAME", wxTHICK_FRAME},
1822 { "wxRESIZE_BORDER", wxRESIZE_BORDER},
1823 { "wxSYSTEM_MENU", wxSYSTEM_MENU},
1824 { "wxMINIMIZE_BOX", wxMINIMIZE_BOX},
1825 { "wxMAXIMIZE_BOX", wxMAXIMIZE_BOX},
1826 { "wxRESIZE_BOX", wxRESIZE_BOX},
1827 { "wxDEFAULT_FRAME", wxDEFAULT_FRAME},
1828 { "wxDEFAULT_DIALOG_STYLE", wxDEFAULT_DIALOG_STYLE},
1829 { "wxBORDER", wxBORDER},
1830 { "wxRETAINED", wxRETAINED},
1831 { "wxNATIVE_IMPL", 0},
1832 { "wxEXTENDED_IMPL", 0},
1833 { "wxBACKINGSTORE", wxBACKINGSTORE},
1834 // { "wxFLAT", wxFLAT},
1835 // { "wxMOTIF_RESIZE", wxMOTIF_RESIZE},
1836 { "wxFIXED_LENGTH", 0},
1837 { "wxDOUBLE_BORDER", wxDOUBLE_BORDER},
1838 { "wxSUNKEN_BORDER", wxSUNKEN_BORDER},
1839 { "wxRAISED_BORDER", wxRAISED_BORDER},
1840 { "wxSIMPLE_BORDER", wxSIMPLE_BORDER},
1841 { "wxSTATIC_BORDER", wxSTATIC_BORDER},
1842 { "wxTRANSPARENT_WINDOW", wxTRANSPARENT_WINDOW},
1843 { "wxNO_BORDER", wxNO_BORDER},
1844 { "wxCLIP_CHILDREN", wxCLIP_CHILDREN},
1845
1846 { "wxTINY_CAPTION_HORIZ", wxTINY_CAPTION_HORIZ},
1847 { "wxTINY_CAPTION_VERT", wxTINY_CAPTION_VERT},
1848
1849 // Text font families
1850 { "wxDEFAULT", wxDEFAULT},
1851 { "wxDECORATIVE", wxDECORATIVE},
1852 { "wxROMAN", wxROMAN},
1853 { "wxSCRIPT", wxSCRIPT},
1854 { "wxSWISS", wxSWISS},
1855 { "wxMODERN", wxMODERN},
1856 { "wxTELETYPE", wxTELETYPE},
1857 { "wxVARIABLE", wxVARIABLE},
1858 { "wxFIXED", wxFIXED},
1859 { "wxNORMAL", wxNORMAL},
1860 { "wxLIGHT", wxLIGHT},
1861 { "wxBOLD", wxBOLD},
1862 { "wxITALIC", wxITALIC},
1863 { "wxSLANT", wxSLANT},
1864 { "wxSOLID", wxSOLID},
1865 { "wxDOT", wxDOT},
1866 { "wxLONG_DASH", wxLONG_DASH},
1867 { "wxSHORT_DASH", wxSHORT_DASH},
1868 { "wxDOT_DASH", wxDOT_DASH},
1869 { "wxUSER_DASH", wxUSER_DASH},
1870 { "wxTRANSPARENT", wxTRANSPARENT},
1871 { "wxSTIPPLE", wxSTIPPLE},
1872 { "wxBDIAGONAL_HATCH", wxBDIAGONAL_HATCH},
1873 { "wxCROSSDIAG_HATCH", wxCROSSDIAG_HATCH},
1874 { "wxFDIAGONAL_HATCH", wxFDIAGONAL_HATCH},
1875 { "wxCROSS_HATCH", wxCROSS_HATCH},
1876 { "wxHORIZONTAL_HATCH", wxHORIZONTAL_HATCH},
1877 { "wxVERTICAL_HATCH", wxVERTICAL_HATCH},
1878 { "wxJOIN_BEVEL", wxJOIN_BEVEL},
1879 { "wxJOIN_MITER", wxJOIN_MITER},
1880 { "wxJOIN_ROUND", wxJOIN_ROUND},
1881 { "wxCAP_ROUND", wxCAP_ROUND},
1882 { "wxCAP_PROJECTING", wxCAP_PROJECTING},
1883 { "wxCAP_BUTT", wxCAP_BUTT},
1884
1885 // Logical ops
1886 { "wxCLEAR", wxCLEAR},
1887 { "wxXOR", wxXOR},
1888 { "wxINVERT", wxINVERT},
1889 { "wxOR_REVERSE", wxOR_REVERSE},
1890 { "wxAND_REVERSE", wxAND_REVERSE},
1891 { "wxCOPY", wxCOPY},
1892 { "wxAND", wxAND},
1893 { "wxAND_INVERT", wxAND_INVERT},
1894 { "wxNO_OP", wxNO_OP},
1895 { "wxNOR", wxNOR},
1896 { "wxEQUIV", wxEQUIV},
1897 { "wxSRC_INVERT", wxSRC_INVERT},
1898 { "wxOR_INVERT", wxOR_INVERT},
1899 { "wxNAND", wxNAND},
1900 { "wxOR", wxOR},
1901 { "wxSET", wxSET},
1902
1903 { "wxFLOOD_SURFACE", wxFLOOD_SURFACE},
1904 { "wxFLOOD_BORDER", wxFLOOD_BORDER},
1905 { "wxODDEVEN_RULE", wxODDEVEN_RULE},
1906 { "wxWINDING_RULE", wxWINDING_RULE},
1907 { "wxHORIZONTAL", wxHORIZONTAL},
1908 { "wxVERTICAL", wxVERTICAL},
1909 { "wxBOTH", wxBOTH},
1910 { "wxCENTER_FRAME", wxCENTER_FRAME},
1911 { "wxOK", wxOK},
1912 { "wxYES_NO", wxYES_NO},
1913 { "wxCANCEL", wxCANCEL},
1914 { "wxYES", wxYES},
1915 { "wxNO", wxNO},
1916 { "wxICON_EXCLAMATION", wxICON_EXCLAMATION},
1917 { "wxICON_HAND", wxICON_HAND},
1918 { "wxICON_QUESTION", wxICON_QUESTION},
1919 { "wxICON_INFORMATION", wxICON_INFORMATION},
1920 { "wxICON_STOP", wxICON_STOP},
1921 { "wxICON_ASTERISK", wxICON_ASTERISK},
1922 { "wxICON_MASK", wxICON_MASK},
1923 { "wxCENTRE", wxCENTRE},
1924 { "wxCENTER", wxCENTRE},
1925 { "wxUSER_COLOURS", wxUSER_COLOURS},
1926 { "wxVERTICAL_LABEL", 0},
1927 { "wxHORIZONTAL_LABEL", 0},
1928
1929 // Bitmap types (not strictly styles)
1930 { "wxBITMAP_TYPE_XPM", wxBITMAP_TYPE_XPM},
1931 { "wxBITMAP_TYPE_XBM", wxBITMAP_TYPE_XBM},
1932 { "wxBITMAP_TYPE_BMP", wxBITMAP_TYPE_BMP},
1933 { "wxBITMAP_TYPE_RESOURCE", wxBITMAP_TYPE_BMP_RESOURCE},
1934 { "wxBITMAP_TYPE_BMP_RESOURCE", wxBITMAP_TYPE_BMP_RESOURCE},
1935 { "wxBITMAP_TYPE_GIF", wxBITMAP_TYPE_GIF},
1936 { "wxBITMAP_TYPE_TIF", wxBITMAP_TYPE_TIF},
1937 { "wxBITMAP_TYPE_ICO", wxBITMAP_TYPE_ICO},
1938 { "wxBITMAP_TYPE_ICO_RESOURCE", wxBITMAP_TYPE_ICO_RESOURCE},
1939 { "wxBITMAP_TYPE_CUR", wxBITMAP_TYPE_CUR},
1940 { "wxBITMAP_TYPE_CUR_RESOURCE", wxBITMAP_TYPE_CUR_RESOURCE},
1941 { "wxBITMAP_TYPE_XBM_DATA", wxBITMAP_TYPE_XBM_DATA},
1942 { "wxBITMAP_TYPE_XPM_DATA", wxBITMAP_TYPE_XPM_DATA},
1943 { "wxBITMAP_TYPE_ANY", wxBITMAP_TYPE_ANY}
1944 };
1945
1946 static int wxResourceBitListCount = (sizeof(wxResourceBitListTable)/sizeof(wxResourceBitListStruct));
1947
1948 long wxParseWindowStyle(const wxString& bitListString)
1949 {
1950 int i = 0;
1951 char *word;
1952 long bitList = 0;
1953 while ((word = wxResourceParseWord((char*) (const char*) bitListString, &i)))
1954 {
1955 bool found = FALSE;
1956 int j;
1957 for (j = 0; j < wxResourceBitListCount; j++)
1958 if (strcmp(wxResourceBitListTable[j].word, word) == 0)
1959 {
1960 bitList |= wxResourceBitListTable[j].bits;
1961 found = TRUE;
1962 break;
1963 }
1964 if (!found)
1965 {
1966 wxLogWarning(_("Unrecognized style %s whilst parsing resource."), word);
1967 return 0;
1968 }
1969 }
1970 return bitList;
1971 }
1972
1973 /*
1974 * Load a bitmap from a wxWindows resource, choosing an optimum
1975 * depth and appropriate type.
1976 */
1977
1978 wxBitmap wxResourceCreateBitmap(const wxString& resource, wxResourceTable *table)
1979 {
1980 if (!table)
1981 table = wxDefaultResourceTable;
1982
1983 wxItemResource *item = table->FindResource(resource);
1984 if (item)
1985 {
1986 if (!item->GetType() || strcmp(item->GetType(), "wxBitmap") != 0)
1987 {
1988 wxLogWarning(_("%s not a bitmap resource specification."), (const char*) resource);
1989 return wxNullBitmap;
1990 }
1991 int thisDepth = wxDisplayDepth();
1992 long thisNoColours = (long)pow(2.0, (double)thisDepth);
1993
1994 wxItemResource *optResource = (wxItemResource *) NULL;
1995
1996 // Try to find optimum bitmap for this platform/colour depth
1997 wxNode *node = item->GetChildren().First();
1998 while (node)
1999 {
2000 wxItemResource *child = (wxItemResource *)node->Data();
2001 int platform = (int)child->GetValue2();
2002 int noColours = (int)child->GetValue3();
2003 /*
2004 char *name = child->GetName();
2005 int bitmapType = (int)child->GetValue1();
2006 int xRes = child->GetWidth();
2007 int yRes = child->GetHeight();
2008 */
2009
2010 switch (platform)
2011 {
2012 case RESOURCE_PLATFORM_ANY:
2013 {
2014 if (!optResource && ((noColours == 0) || (noColours <= thisNoColours)))
2015 optResource = child;
2016 else
2017 {
2018 // Maximise the number of colours.
2019 // If noColours is zero (unspecified), then assume this
2020 // is the right one.
2021 if ((noColours == 0) || ((noColours <= thisNoColours) && (noColours > optResource->GetValue3())))
2022 optResource = child;
2023 }
2024 break;
2025 }
2026 #ifdef __WXMSW__
2027 case RESOURCE_PLATFORM_WINDOWS:
2028 {
2029 if (!optResource && ((noColours == 0) || (noColours <= thisNoColours)))
2030 optResource = child;
2031 else
2032 {
2033 // Maximise the number of colours
2034 if ((noColours > 0) || ((noColours <= thisNoColours) && (noColours > optResource->GetValue3())))
2035 optResource = child;
2036 }
2037 break;
2038 }
2039 #endif
2040 #ifdef __WXGTK__
2041 case RESOURCE_PLATFORM_X:
2042 {
2043 if (!optResource && ((noColours == 0) || (noColours <= thisNoColours)))
2044 optResource = child;
2045 else
2046 {
2047 // Maximise the number of colours
2048 if ((noColours == 0) || ((noColours <= thisNoColours) && (noColours > optResource->GetValue3())))
2049 optResource = child;
2050 }
2051 break;
2052 }
2053 #endif
2054 #ifdef wx_max
2055 case RESOURCE_PLATFORM_MAC:
2056 {
2057 if (!optResource && ((noColours == 0) || (noColours <= thisNoColours)))
2058 optResource = child;
2059 else
2060 {
2061 // Maximise the number of colours
2062 if ((noColours == 0) || ((noColours <= thisNoColours) && (noColours > optResource->GetValue3())))
2063 optResource = child;
2064 }
2065 break;
2066 }
2067 #endif
2068 default:
2069 break;
2070 }
2071 node = node->Next();
2072 }
2073 // If no matching resource, fail.
2074 if (!optResource)
2075 return wxNullBitmap;
2076
2077 wxString name = optResource->GetName();
2078 int bitmapType = (int)optResource->GetValue1();
2079 switch (bitmapType)
2080 {
2081 case wxBITMAP_TYPE_XBM_DATA:
2082 {
2083 #ifdef __WXGTK__
2084 wxItemResource *item = table->FindResource(name);
2085 if (!item)
2086 {
2087 wxLogWarning(_("Failed to find XBM resource %s.\n"
2088 "Forgot to use wxResourceLoadBitmapData?"), (const char*) name);
2089 return (wxBitmap *) NULL;
2090 }
2091 return wxBitmap(item->GetValue1(), (int)item->GetValue2(), (int)item->GetValue3()) ;
2092 #else
2093 wxLogWarning(_("No XBM facility available!"));
2094 #endif
2095 break;
2096 }
2097 case wxBITMAP_TYPE_XPM_DATA:
2098 {
2099 #if (defined(__WXGTK__)) || (defined(__WXMSW__) && wxUSE_XPM_IN_MSW)
2100 wxItemResource *item = table->FindResource(name);
2101 if (!item)
2102 {
2103 wxLogWarning(_("Failed to find XPM resource %s.\n"
2104 "Forgot to use wxResourceLoadBitmapData?"), (const char*) name);
2105 return (wxBitmap *) NULL;
2106 }
2107 return wxBitmap(item->GetValue1());
2108 #else
2109 wxLogWarning(_("No XPM facility available!"));
2110 #endif
2111 break;
2112 }
2113 default:
2114 {
2115 return wxBitmap(name, bitmapType);
2116 break;
2117 }
2118 }
2119 return wxNullBitmap;
2120 }
2121 else
2122 {
2123 wxLogWarning(_("Bitmap resource specification %s not found."), (const char*) resource);
2124 return wxNullBitmap;
2125 }
2126 }
2127
2128 /*
2129 * Load an icon from a wxWindows resource, choosing an optimum
2130 * depth and appropriate type.
2131 */
2132
2133 wxIcon wxResourceCreateIcon(const wxString& resource, wxResourceTable *table)
2134 {
2135 if (!table)
2136 table = wxDefaultResourceTable;
2137
2138 wxItemResource *item = table->FindResource(resource);
2139 if (item)
2140 {
2141 if ((item->GetType() == "") || strcmp(item->GetType(), "wxIcon") != 0)
2142 {
2143 wxLogWarning(_("%s not an icon resource specification."), (const char*) resource);
2144 return wxNullIcon;
2145 }
2146 int thisDepth = wxDisplayDepth();
2147 long thisNoColours = (long)pow(2.0, (double)thisDepth);
2148
2149 wxItemResource *optResource = (wxItemResource *) NULL;
2150
2151 // Try to find optimum icon for this platform/colour depth
2152 wxNode *node = item->GetChildren().First();
2153 while (node)
2154 {
2155 wxItemResource *child = (wxItemResource *)node->Data();
2156 int platform = (int)child->GetValue2();
2157 int noColours = (int)child->GetValue3();
2158 /*
2159 char *name = child->GetName();
2160 int bitmapType = (int)child->GetValue1();
2161 int xRes = child->GetWidth();
2162 int yRes = child->GetHeight();
2163 */
2164
2165 switch (platform)
2166 {
2167 case RESOURCE_PLATFORM_ANY:
2168 {
2169 if (!optResource && ((noColours == 0) || (noColours <= thisNoColours)))
2170 optResource = child;
2171 else
2172 {
2173 // Maximise the number of colours.
2174 // If noColours is zero (unspecified), then assume this
2175 // is the right one.
2176 if ((noColours == 0) || ((noColours <= thisNoColours) && (noColours > optResource->GetValue3())))
2177 optResource = child;
2178 }
2179 break;
2180 }
2181 #ifdef __WXMSW__
2182 case RESOURCE_PLATFORM_WINDOWS:
2183 {
2184 if (!optResource && ((noColours == 0) || (noColours <= thisNoColours)))
2185 optResource = child;
2186 else
2187 {
2188 // Maximise the number of colours
2189 if ((noColours > 0) || ((noColours <= thisNoColours) && (noColours > optResource->GetValue3())))
2190 optResource = child;
2191 }
2192 break;
2193 }
2194 #endif
2195 #ifdef __WXGTK__
2196 case RESOURCE_PLATFORM_X:
2197 {
2198 if (!optResource && ((noColours == 0) || (noColours <= thisNoColours)))
2199 optResource = child;
2200 else
2201 {
2202 // Maximise the number of colours
2203 if ((noColours == 0) || ((noColours <= thisNoColours) && (noColours > optResource->GetValue3())))
2204 optResource = child;
2205 }
2206 break;
2207 }
2208 #endif
2209 #ifdef wx_max
2210 case RESOURCE_PLATFORM_MAC:
2211 {
2212 if (!optResource && ((noColours == 0) || (noColours <= thisNoColours)))
2213 optResource = child;
2214 else
2215 {
2216 // Maximise the number of colours
2217 if ((noColours == 0) || ((noColours <= thisNoColours) && (noColours > optResource->GetValue3())))
2218 optResource = child;
2219 }
2220 break;
2221 }
2222 #endif
2223 default:
2224 break;
2225 }
2226 node = node->Next();
2227 }
2228 // If no matching resource, fail.
2229 if (!optResource)
2230 return wxNullIcon;
2231
2232 wxString name = optResource->GetName();
2233 int bitmapType = (int)optResource->GetValue1();
2234 switch (bitmapType)
2235 {
2236 case wxBITMAP_TYPE_XBM_DATA:
2237 {
2238 #ifdef __WXGTK__
2239 wxItemResource *item = table->FindResource(name);
2240 if (!item)
2241 {
2242 wxLogWarning(_("Failed to find XBM resource %s.\n"
2243 "Forgot to use wxResourceLoadIconData?"), (const char*) name);
2244 return (wxIcon *) NULL;
2245 }
2246 return wxIcon((char **)item->GetValue1(), (int)item->GetValue2(), (int)item->GetValue3());
2247 #else
2248 wxLogWarning(_("No XBM facility available!"));
2249 #endif
2250 break;
2251 }
2252 case wxBITMAP_TYPE_XPM_DATA:
2253 {
2254 // *** XPM ICON NOT YET IMPLEMENTED IN WXWINDOWS ***
2255 /*
2256 #if (defined(__WXGTK__)) || (defined(__WXMSW__) && wxUSE_XPM_IN_MSW)
2257 wxItemResource *item = table->FindResource(name);
2258 if (!item)
2259 {
2260 char buf[400];
2261 sprintf(buf, _("Failed to find XPM resource %s.\nForgot to use wxResourceLoadIconData?"), name);
2262 wxLogWarning(buf);
2263 return NULL;
2264 }
2265 return wxIcon((char **)item->GetValue1());
2266 #else
2267 wxLogWarning(_("No XPM facility available!"));
2268 #endif
2269 */
2270 wxLogWarning(_("No XPM icon facility available!"));
2271 break;
2272 }
2273 default:
2274 {
2275 #ifdef __WXGTK__
2276 wxLogWarning(_("Icon resource specification %s not found."), (const char*) resource);
2277 #else
2278 return wxIcon(name, bitmapType);
2279 #endif
2280 break;
2281 }
2282 }
2283 return wxNullIcon;
2284 }
2285 else
2286 {
2287 wxLogWarning(_("Icon resource specification %s not found."), (const char*) resource);
2288 return (wxIcon *) NULL;
2289 }
2290 }
2291
2292 wxMenu *wxResourceCreateMenu(wxItemResource *item)
2293 {
2294 wxMenu *menu = new wxMenu;
2295 wxNode *node = item->GetChildren().First();
2296 while (node)
2297 {
2298 wxItemResource *child = (wxItemResource *)node->Data();
2299 if ((child->GetType() != "") && (child->GetType() == "wxMenuSeparator"))
2300 menu->AppendSeparator();
2301 else if (child->GetChildren().Number() > 0)
2302 {
2303 wxMenu *subMenu = wxResourceCreateMenu(child);
2304 if (subMenu)
2305 menu->Append((int)child->GetValue1(), child->GetTitle(), subMenu, child->GetValue4());
2306 }
2307 else
2308 {
2309 menu->Append((int)child->GetValue1(), child->GetTitle(), child->GetValue4(), (child->GetValue2() != 0));
2310 }
2311 node = node->Next();
2312 }
2313 return menu;
2314 }
2315
2316 wxMenuBar *wxResourceCreateMenuBar(const wxString& resource, wxResourceTable *table, wxMenuBar *menuBar)
2317 {
2318 if (!table)
2319 table = wxDefaultResourceTable;
2320
2321 wxItemResource *menuResource = table->FindResource(resource);
2322 if (menuResource && (menuResource->GetType() != "") && (menuResource->GetType() == "wxMenu"))
2323 {
2324 if (!menuBar)
2325 menuBar = new wxMenuBar;
2326 wxNode *node = menuResource->GetChildren().First();
2327 while (node)
2328 {
2329 wxItemResource *child = (wxItemResource *)node->Data();
2330 wxMenu *menu = wxResourceCreateMenu(child);
2331 if (menu)
2332 menuBar->Append(menu, child->GetTitle());
2333 node = node->Next();
2334 }
2335 return menuBar;
2336 }
2337 return (wxMenuBar *) NULL;
2338 }
2339
2340 wxMenu *wxResourceCreateMenu(const wxString& resource, wxResourceTable *table)
2341 {
2342 if (!table)
2343 table = wxDefaultResourceTable;
2344
2345 wxItemResource *menuResource = table->FindResource(resource);
2346 if (menuResource && (menuResource->GetType() != "") && (menuResource->GetType() == "wxMenu"))
2347 // if (menuResource && (menuResource->GetType() == wxTYPE_MENU))
2348 return wxResourceCreateMenu(menuResource);
2349 return (wxMenu *) NULL;
2350 }
2351
2352 // Global equivalents (so don't have to refer to default table explicitly)
2353 bool wxResourceParseData(const wxString& resource, wxResourceTable *table)
2354 {
2355 if (!table)
2356 table = wxDefaultResourceTable;
2357
2358 return table->ParseResourceData(resource);
2359 }
2360
2361 bool wxResourceParseFile(const wxString& filename, wxResourceTable *table)
2362 {
2363 if (!table)
2364 table = wxDefaultResourceTable;
2365
2366 return table->ParseResourceFile(filename);
2367 }
2368
2369 // Register XBM/XPM data
2370 bool wxResourceRegisterBitmapData(const wxString& name, char bits[], int width, int height, wxResourceTable *table)
2371 {
2372 if (!table)
2373 table = wxDefaultResourceTable;
2374
2375 return table->RegisterResourceBitmapData(name, bits, width, height);
2376 }
2377
2378 bool wxResourceRegisterBitmapData(const wxString& name, char **data, wxResourceTable *table)
2379 {
2380 if (!table)
2381 table = wxDefaultResourceTable;
2382
2383 return table->RegisterResourceBitmapData(name, data);
2384 }
2385
2386 void wxResourceClear(wxResourceTable *table)
2387 {
2388 if (!table)
2389 table = wxDefaultResourceTable;
2390
2391 table->ClearTable();
2392 }
2393
2394 /*
2395 * Identifiers
2396 */
2397
2398 bool wxResourceAddIdentifier(const wxString& name, int value, wxResourceTable *table)
2399 {
2400 if (!table)
2401 table = wxDefaultResourceTable;
2402
2403 table->identifiers.Put(name, (wxObject *)value);
2404 return TRUE;
2405 }
2406
2407 int wxResourceGetIdentifier(const wxString& name, wxResourceTable *table)
2408 {
2409 if (!table)
2410 table = wxDefaultResourceTable;
2411
2412 return (int)table->identifiers.Get(name);
2413 }
2414
2415 /*
2416 * Parse #include file for #defines (only)
2417 */
2418
2419 bool wxResourceParseIncludeFile(const wxString& f, wxResourceTable *table)
2420 {
2421 if (!table)
2422 table = wxDefaultResourceTable;
2423
2424 FILE *fd = fopen(f, "r");
2425 if (!fd)
2426 {
2427 return FALSE;
2428 }
2429 while (wxGetResourceToken(fd))
2430 {
2431 if (strcmp(wxResourceBuffer, "#define") == 0)
2432 {
2433 wxGetResourceToken(fd);
2434 char *name = copystring(wxResourceBuffer);
2435 wxGetResourceToken(fd);
2436 char *value = copystring(wxResourceBuffer);
2437 if (isdigit(value[0]))
2438 {
2439 int val = (int)atol(value);
2440 wxResourceAddIdentifier(name, val, table);
2441 }
2442 delete[] name;
2443 delete[] value;
2444 }
2445 }
2446 fclose(fd);
2447 return TRUE;
2448 }
2449
2450 /*
2451 * Reading strings as if they were .wxr files
2452 */
2453
2454 static int getc_string(char *s)
2455 {
2456 int ch = s[wxResourceStringPtr];
2457 if (ch == 0)
2458 return EOF;
2459 else
2460 {
2461 wxResourceStringPtr ++;
2462 return ch;
2463 }
2464 }
2465
2466 static int ungetc_string()
2467 {
2468 wxResourceStringPtr --;
2469 return 0;
2470 }
2471
2472 bool wxEatWhiteSpaceString(char *s)
2473 {
2474 int ch = getc_string(s);
2475 if (ch == EOF)
2476 return TRUE;
2477
2478 if ((ch != ' ') && (ch != '/') && (ch != ' ') && (ch != 10) && (ch != 13) && (ch != 9))
2479 {
2480 ungetc_string();
2481 return TRUE;
2482 }
2483
2484 // Eat whitespace
2485 while (ch == ' ' || ch == 10 || ch == 13 || ch == 9)
2486 ch = getc_string(s);
2487 // Check for comment
2488 if (ch == '/')
2489 {
2490 ch = getc_string(s);
2491 if (ch == '*')
2492 {
2493 bool finished = FALSE;
2494 while (!finished)
2495 {
2496 ch = getc_string(s);
2497 if (ch == EOF)
2498 return FALSE;
2499 if (ch == '*')
2500 {
2501 int newCh = getc_string(s);
2502 if (newCh == '/')
2503 finished = TRUE;
2504 else
2505 {
2506 ungetc_string();
2507 }
2508 }
2509 }
2510 }
2511 else // False alarm
2512 return FALSE;
2513 }
2514 else if (ch != EOF)
2515 ungetc_string();
2516 return wxEatWhiteSpaceString(s);
2517 }
2518
2519 bool wxGetResourceTokenString(char *s)
2520 {
2521 if (!wxResourceBuffer)
2522 wxReallocateResourceBuffer();
2523 wxResourceBuffer[0] = 0;
2524 wxEatWhiteSpaceString(s);
2525
2526 int ch = getc_string(s);
2527 if (ch == '"')
2528 {
2529 // Get string
2530 wxResourceBufferCount = 0;
2531 ch = getc_string(s);
2532 while (ch != '"')
2533 {
2534 int actualCh = ch;
2535 if (ch == EOF)
2536 {
2537 wxResourceBuffer[wxResourceBufferCount] = 0;
2538 return FALSE;
2539 }
2540 // Escaped characters
2541 else if (ch == '\\')
2542 {
2543 int newCh = getc_string(s);
2544 if (newCh == '"')
2545 actualCh = '"';
2546 else if (newCh == 10)
2547 actualCh = 10;
2548 else
2549 {
2550 ungetc_string();
2551 }
2552 }
2553
2554 if (wxResourceBufferCount >= wxResourceBufferSize-1)
2555 wxReallocateResourceBuffer();
2556 wxResourceBuffer[wxResourceBufferCount] = (char)actualCh;
2557 wxResourceBufferCount ++;
2558 ch = getc_string(s);
2559 }
2560 wxResourceBuffer[wxResourceBufferCount] = 0;
2561 }
2562 else
2563 {
2564 wxResourceBufferCount = 0;
2565 // Any other token
2566 while (ch != ' ' && ch != EOF && ch != ' ' && ch != 13 && ch != 9 && ch != 10)
2567 {
2568 if (wxResourceBufferCount >= wxResourceBufferSize-1)
2569 wxReallocateResourceBuffer();
2570 wxResourceBuffer[wxResourceBufferCount] = (char)ch;
2571 wxResourceBufferCount ++;
2572
2573 ch = getc_string(s);
2574 }
2575 wxResourceBuffer[wxResourceBufferCount] = 0;
2576 if (ch == EOF)
2577 return FALSE;
2578 }
2579 return TRUE;
2580 }
2581
2582 /*
2583 * Files are in form:
2584 static char *name = "....";
2585 with possible comments.
2586 */
2587
2588 bool wxResourceReadOneResourceString(char *s, wxExprDatabase& db, bool *eof, wxResourceTable *table)
2589 {
2590 if (!table)
2591 table = wxDefaultResourceTable;
2592
2593 // static or #define
2594 if (!wxGetResourceTokenString(s))
2595 {
2596 *eof = TRUE;
2597 return FALSE;
2598 }
2599
2600 if (strcmp(wxResourceBuffer, "#define") == 0)
2601 {
2602 wxGetResourceTokenString(s);
2603 char *name = copystring(wxResourceBuffer);
2604 wxGetResourceTokenString(s);
2605 char *value = copystring(wxResourceBuffer);
2606 if (isalpha(value[0]))
2607 {
2608 int val = (int)atol(value);
2609 wxResourceAddIdentifier(name, val, table);
2610 }
2611 else
2612 {
2613 wxLogWarning(_("#define %s must be an integer."), name);
2614 delete[] name;
2615 delete[] value;
2616 return FALSE;
2617 }
2618 delete[] name;
2619 delete[] value;
2620
2621 return TRUE;
2622 }
2623 /*
2624 else if (strcmp(wxResourceBuffer, "#include") == 0)
2625 {
2626 wxGetResourceTokenString(s);
2627 char *name = copystring(wxResourceBuffer);
2628 char *actualName = name;
2629 if (name[0] == '"')
2630 actualName = name + 1;
2631 int len = strlen(name);
2632 if ((len > 0) && (name[len-1] == '"'))
2633 name[len-1] = 0;
2634 if (!wxResourceParseIncludeFile(actualName, table))
2635 {
2636 char buf[400];
2637 sprintf(buf, _("Could not find resource include file %s."), actualName);
2638 wxLogWarning(buf);
2639 }
2640 delete[] name;
2641 return TRUE;
2642 }
2643 */
2644 else if (strcmp(wxResourceBuffer, "static") != 0)
2645 {
2646 char buf[300];
2647 strcpy(buf, _("Found "));
2648 strncat(buf, wxResourceBuffer, 30);
2649 strcat(buf, _(", expected static, #include or #define\nwhilst parsing resource."));
2650 wxLogWarning(buf);
2651 return FALSE;
2652 }
2653
2654 // char
2655 if (!wxGetResourceTokenString(s))
2656 {
2657 wxLogWarning(_("Unexpected end of file whilst parsing resource."));
2658 *eof = TRUE;
2659 return FALSE;
2660 }
2661
2662 if (strcmp(wxResourceBuffer, "char") != 0)
2663 {
2664 wxLogWarning(_("Expected 'char' whilst parsing resource."));
2665 return FALSE;
2666 }
2667
2668 // *name
2669 if (!wxGetResourceTokenString(s))
2670 {
2671 wxLogWarning(_("Unexpected end of file whilst parsing resource."));
2672 *eof = TRUE;
2673 return FALSE;
2674 }
2675
2676 if (wxResourceBuffer[0] != '*')
2677 {
2678 wxLogWarning(_("Expected '*' whilst parsing resource."));
2679 return FALSE;
2680 }
2681 char nameBuf[100];
2682 strncpy(nameBuf, wxResourceBuffer+1, 99);
2683
2684 // =
2685 if (!wxGetResourceTokenString(s))
2686 {
2687 wxLogWarning(_("Unexpected end of file whilst parsing resource."));
2688 *eof = TRUE;
2689 return FALSE;
2690 }
2691
2692 if (strcmp(wxResourceBuffer, "=") != 0)
2693 {
2694 wxLogWarning(_("Expected '=' whilst parsing resource."));
2695 return FALSE;
2696 }
2697
2698 // String
2699 if (!wxGetResourceTokenString(s))
2700 {
2701 wxLogWarning(_("Unexpected end of file whilst parsing resource."));
2702 *eof = TRUE;
2703 return FALSE;
2704 }
2705 else
2706 {
2707 if (!db.ReadPrologFromString(wxResourceBuffer))
2708 {
2709 wxLogWarning(_("%s: ill-formed resource file syntax."), nameBuf);
2710 return FALSE;
2711 }
2712 }
2713 // Semicolon
2714 if (!wxGetResourceTokenString(s))
2715 {
2716 *eof = TRUE;
2717 }
2718 return TRUE;
2719 }
2720
2721 bool wxResourceParseString(char *s, wxResourceTable *table)
2722 {
2723 if (!table)
2724 table = wxDefaultResourceTable;
2725
2726 if (!s)
2727 return FALSE;
2728
2729 // Turn backslashes into spaces
2730 if (s)
2731 {
2732 int len = strlen(s);
2733 int i;
2734 for (i = 0; i < len; i++)
2735 if (s[i] == 92 && s[i+1] == 13)
2736 {
2737 s[i] = ' ';
2738 s[i+1] = ' ';
2739 }
2740 }
2741
2742 wxExprDatabase db;
2743 wxResourceStringPtr = 0;
2744
2745 bool eof = FALSE;
2746 while (wxResourceReadOneResourceString(s, db, &eof, table) && !eof)
2747 {
2748 // Loop
2749 }
2750 return wxResourceInterpretResources(*table, db);
2751 }
2752
2753 /*
2754 * resource loading facility
2755 */
2756
2757 bool wxWindow::LoadFromResource(wxWindow *parent, const wxString& resourceName, const wxResourceTable *table)
2758 {
2759 if (!table)
2760 table = wxDefaultResourceTable;
2761
2762 wxItemResource *resource = table->FindResource((const char *)resourceName);
2763 // if (!resource || (resource->GetType() != wxTYPE_DIALOG_BOX))
2764 if (!resource || !resource->GetType() ||
2765 ! ((strcmp(resource->GetType(), "wxDialog") == 0) || (strcmp(resource->GetType(), "wxPanel") == 0)))
2766 return FALSE;
2767
2768 wxString title(resource->GetTitle());
2769 long theWindowStyle = resource->GetStyle();
2770 bool isModal = (resource->GetValue1() != 0);
2771 int x = resource->GetX();
2772 int y = resource->GetY();
2773 int width = resource->GetWidth();
2774 int height = resource->GetHeight();
2775 wxString name = resource->GetName();
2776
2777 if (IsKindOf(CLASSINFO(wxDialog)))
2778 {
2779 wxDialog *dialogBox = (wxDialog *)this;
2780 long modalStyle = isModal ? wxDIALOG_MODAL : 0;
2781 if (!dialogBox->Create(parent, -1, title, wxPoint(x, y), wxSize(width, height), theWindowStyle|modalStyle, name))
2782 return FALSE;
2783
2784 // Only reset the client size if we know we're not going to do it again below.
2785 if ((resource->GetResourceStyle() & wxRESOURCE_DIALOG_UNITS) == 0)
2786 dialogBox->SetClientSize(width, height);
2787 }
2788 else if (IsKindOf(CLASSINFO(wxPanel)))
2789 {
2790 wxPanel* panel = (wxPanel *)this;
2791 if (!panel->Create(parent, -1, wxPoint(x, y), wxSize(width, height), theWindowStyle, name))
2792 return FALSE;
2793 }
2794 else
2795 {
2796 if (!this->Create(parent, -1, wxPoint(x, y), wxSize(width, height), theWindowStyle, name))
2797 return FALSE;
2798 }
2799
2800 if ((resource->GetResourceStyle() & wxRESOURCE_USE_DEFAULTS) != 0)
2801 {
2802 // No need to do this since it's done in wxPanel or wxDialog constructor.
2803 // SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
2804 }
2805 else
2806 {
2807 if (resource->GetFont().Ok())
2808 SetFont(resource->GetFont());
2809 if (resource->GetBackgroundColour().Ok())
2810 SetBackgroundColour(resource->GetBackgroundColour());
2811 }
2812
2813 // Should have some kind of font at this point
2814 if (!GetFont()->Ok())
2815 SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
2816 if (!GetBackgroundColour().Ok())
2817 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
2818
2819 // Only when we've created the window and set the font can we set the correct size,
2820 // if based on dialog units.
2821 if ((resource->GetResourceStyle() & wxRESOURCE_DIALOG_UNITS) != 0)
2822 {
2823 wxSize sz = ConvertDialogToPixels(wxSize(width, height));
2824 SetClientSize(sz.x, sz.y);
2825
2826 wxPoint pt = ConvertDialogToPixels(wxPoint(x, y));
2827 Move(pt.x, pt.y);
2828 }
2829
2830 // Now create children
2831 wxNode *node = resource->GetChildren().First();
2832 while (node)
2833 {
2834 wxItemResource *childResource = (wxItemResource *)node->Data();
2835
2836 (void) CreateItem(childResource, resource, table);
2837
2838 node = node->Next();
2839 }
2840 return TRUE;
2841 }
2842
2843 wxControl *wxWindow::CreateItem(const wxItemResource *resource, const wxItemResource* parentResource, const wxResourceTable *table)
2844 {
2845 if (!table)
2846 table = wxDefaultResourceTable;
2847 return table->CreateItem((wxWindow *)this, resource, parentResource);
2848 }
2849
2850 #endif // wxUSE_WX_RESOURCES