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