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