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