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