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