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