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