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