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