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