]> git.saurik.com Git - wxWidgets.git/blame - contrib/src/xml/xmlres.cpp
removed libxpm dependencies from makefiles
[wxWidgets.git] / contrib / src / xml / xmlres.cpp
CommitLineData
56d2f750
VS
1/////////////////////////////////////////////////////////////////////////////
2// Name: xmlres.cpp
3// Purpose: XML resources
4// Author: Vaclav Slavik
5// Created: 2000/03/05
6// RCS-ID: $Id$
7// Copyright: (c) 2000 Vaclav Slavik
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
11#ifdef __GNUG__
12#pragma implementation "xmlres.h"
13#endif
14
15// For compilers that support precompilation, includes "wx.h".
16#include "wx/wxprec.h"
17
18#ifdef __BORLANDC__
19 #pragma hdrstop
20#endif
21
22#include "wx/dialog.h"
23#include "wx/panel.h"
1486ed87 24#include "wx/frame.h"
56d2f750
VS
25#include "wx/wfstream.h"
26#include "wx/filesys.h"
27#include "wx/log.h"
28#include "wx/intl.h"
29#include "wx/tokenzr.h"
d33a1e8b 30#include "wx/fontenum.h"
56d2f750 31#include "wx/module.h"
792064e9
VS
32#include "wx/bitmap.h"
33#include "wx/image.h"
dc743689 34#include "wx/fontmap.h"
56d2f750
VS
35
36#include "wx/xml/xml.h"
37#include "wx/xml/xmlres.h"
38
39#include "wx/arrimpl.cpp"
40WX_DEFINE_OBJARRAY(wxXmlResourceDataRecords);
41
42
f485492a 43wxXmlResource::wxXmlResource(bool use_locale)
56d2f750 44{
ab708d5d
VS
45 m_handlers.DeleteContents(TRUE);
46 m_useLocale = use_locale;
e6b506bf 47 m_version = -1;
56d2f750
VS
48}
49
f485492a 50wxXmlResource::wxXmlResource(const wxString& filemask, bool use_locale)
56d2f750 51{
ab708d5d 52 m_useLocale = use_locale;
e6b506bf 53 m_version = -1;
ab708d5d 54 m_handlers.DeleteContents(TRUE);
0ceae932 55 Load(filemask);
56d2f750
VS
56}
57
58wxXmlResource::~wxXmlResource()
59{
60 ClearHandlers();
61}
62
63
0ceae932 64bool wxXmlResource::Load(const wxString& filemask)
56d2f750
VS
65{
66 wxString fnd;
67 wxXmlResourceDataRecord *drec;
0ceae932 68 bool iswild = wxIsWild(filemask);
e6b506bf 69 bool rt = TRUE;
56d2f750
VS
70
71#if wxUSE_FILESYSTEM
72 wxFileSystem fsys;
73# define wxXmlFindFirst fsys.FindFirst(filemask, wxFILE)
74# define wxXmlFindNext fsys.FindNext()
75#else
76# define wxXmlFindFirst wxFindFirstFile(filemask, wxFILE)
77# define wxXmlFindNext wxFindNextFile()
56d2f750 78#endif
0ceae932
VS
79 if (iswild)
80 fnd = wxXmlFindFirst;
81 else
82 fnd = filemask;
56d2f750
VS
83 while (!!fnd)
84 {
85#if wxUSE_FILESYSTEM
0ceae932
VS
86 if (filemask.Lower().Matches("*.zip") ||
87 filemask.Lower().Matches("*.rsc"))
56d2f750 88 {
e6b506bf
VS
89 rt = rt && Load(fnd + wxT("#zip:*.xmb"));
90 rt = rt && Load(fnd + wxT("#zip:*.xrc"));
56d2f750
VS
91 }
92 else
93#endif
94 {
95 drec = new wxXmlResourceDataRecord;
96 drec->File = fnd;
ab708d5d 97 m_data.Add(drec);
56d2f750 98 }
0ceae932
VS
99
100 if (iswild)
101 fnd = wxXmlFindNext;
102 else
103 fnd = wxEmptyString;
56d2f750
VS
104 }
105# undef wxXmlFindFirst
106# undef wxXmlFindNext
e6b506bf 107 return rt;
56d2f750
VS
108}
109
110
111
112void wxXmlResource::AddHandler(wxXmlResourceHandler *handler)
113{
ab708d5d 114 m_handlers.Append(handler);
56d2f750
VS
115 handler->SetParentResource(this);
116}
117
118
119
120void wxXmlResource::ClearHandlers()
121{
ab708d5d 122 m_handlers.Clear();
56d2f750
VS
123}
124
125
126
127wxMenu *wxXmlResource::LoadMenu(const wxString& name)
128{
e066e256 129 return (wxMenu*)CreateResFromNode(FindResource(name, wxT("wxMenu")), NULL, NULL);
56d2f750
VS
130}
131
132
133
134wxMenuBar *wxXmlResource::LoadMenuBar(const wxString& name)
135{
e066e256 136 return (wxMenuBar*)CreateResFromNode(FindResource(name, wxT("wxMenuBar")), NULL, NULL);
56d2f750
VS
137}
138
139
140
792064e9
VS
141wxToolBar *wxXmlResource::LoadToolBar(wxWindow *parent, const wxString& name)
142{
e066e256 143 return (wxToolBar*)CreateResFromNode(FindResource(name, wxT("wxToolBar")), parent, NULL);
792064e9
VS
144}
145
146
147
56d2f750
VS
148wxDialog *wxXmlResource::LoadDialog(wxWindow *parent, const wxString& name)
149{
150 wxDialog *dialog = new wxDialog;
151 if (!LoadDialog(dialog, parent, name))
152 { delete dialog; return NULL; }
153 else return dialog;
154}
155
156bool wxXmlResource::LoadDialog(wxDialog *dlg, wxWindow *parent, const wxString& name)
157{
e066e256 158 return CreateResFromNode(FindResource(name, wxT("wxDialog")), parent, dlg) != NULL;
56d2f750
VS
159}
160
161
162
163wxPanel *wxXmlResource::LoadPanel(wxWindow *parent, const wxString& name)
164{
e066e256 165 return (wxPanel*)CreateResFromNode(FindResource(name, wxT("wxPanel")), parent, NULL);
56d2f750
VS
166}
167
168bool wxXmlResource::LoadPanel(wxPanel *panel, wxWindow *parent, const wxString& name)
169{
e066e256 170 return CreateResFromNode(FindResource(name, wxT("wxPanel")), parent, panel) != NULL;
56d2f750
VS
171}
172
2e4c3bf8
GT
173bool wxXmlResource::LoadFrame(wxFrame* frame, wxWindow *parent, const wxString& name)
174{
175 return CreateResFromNode(FindResource(name, wxT("wxFrame")), parent, frame) != NULL;
176}
56d2f750 177
d33a1e8b
VS
178wxBitmap wxXmlResource::LoadBitmap(const wxString& name)
179{
180 wxBitmap *bmp = (wxBitmap*)CreateResFromNode(
e066e256 181 FindResource(name, wxT("wxBitmap")), NULL, NULL);
d33a1e8b
VS
182 wxBitmap rt;
183
184 if (bmp) { rt = *bmp; delete bmp; }
185 return rt;
186}
187
188wxIcon wxXmlResource::LoadIcon(const wxString& name)
189{
190 wxIcon *icon = (wxIcon*)CreateResFromNode(
e066e256 191 FindResource(name, wxT("wxIcon")), NULL, NULL);
d33a1e8b
VS
192 wxIcon rt;
193
194 if (icon) { rt = *icon; delete icon; }
195 return rt;
196}
197
198
199
29b5f2cc
VS
200void wxXmlResource::ProcessPlatformProperty(wxXmlNode *node)
201{
202 wxString s;
203 bool isok;
204
205 wxXmlNode *c = node->GetChildren();
206 while (c)
207 {
208 isok = FALSE;
a559d708 209 if (!c->GetPropVal(wxT("platform"), &s))
29b5f2cc
VS
210 isok = TRUE;
211 else
212 {
213 wxStringTokenizer tkn(s, " |");
214
215 while (tkn.HasMoreTokens())
216 {
217 s = tkn.GetNextToken();
218 if (
219#ifdef __WXMSW__
a559d708 220 s == wxString(wxT("win"))
29b5f2cc 221#elif defined(__UNIX__)
a559d708 222 s == wxString(wxT("unix"))
29b5f2cc 223#elif defined(__MAC__)
a559d708 224 s == wxString(wxT("mac"))
29b5f2cc 225#elif defined(__OS2__)
a559d708 226 s == wxString(wxT("os2"))
29b5f2cc
VS
227#else
228 FALSE
229#endif
230 ) isok = TRUE;
231 }
232 }
233
234 if (isok)
235 ProcessPlatformProperty(c);
236 else
237 {
238 node->RemoveChild(c);
239 delete c;
240 }
241
242 c = c->GetNext();
243 }
244}
245
246
247
56d2f750
VS
248void wxXmlResource::UpdateResources()
249{
250 bool modif;
251# if wxUSE_FILESYSTEM
16008659 252 wxFSFile *file = NULL;
56d2f750
VS
253 wxFileSystem fsys;
254# endif
255
ab708d5d 256 for (size_t i = 0; i < m_data.GetCount(); i++)
56d2f750 257 {
ab708d5d 258 modif = (m_data[i].Doc == NULL);
56d2f750
VS
259
260 if (!modif)
261 {
262# if wxUSE_FILESYSTEM
ab708d5d
VS
263 file = fsys.OpenFile(m_data[i].File);
264 modif = file && file->GetModificationTime() > m_data[i].Time;
56d2f750 265 if (!file)
ab708d5d 266 wxLogError(_("Cannot open file '%s'."), m_data[i].File.c_str());
16008659 267 wxDELETE(file);
56d2f750 268# else
ab708d5d 269 modif = wxDateTime(wxFileModificationTime(m_data[i].File)) > m_data[i].Time;
56d2f750
VS
270# endif
271 }
272
273 if (modif)
274 {
16008659 275 wxInputStream *stream = NULL;
56d2f750
VS
276
277# if wxUSE_FILESYSTEM
ab708d5d 278 file = fsys.OpenFile(m_data[i].File);
16008659
GT
279 if (file)
280 stream = file->GetStream();
56d2f750 281# else
ab708d5d 282 stream = new wxFileInputStream(m_data[i].File);
56d2f750
VS
283# endif
284
285 if (stream)
286 {
ab708d5d
VS
287 delete m_data[i].Doc;
288 m_data[i].Doc = new wxXmlDocument;
56d2f750 289 }
ab708d5d 290 if (!stream || !m_data[i].Doc->Load(*stream))
0ceae932 291 {
ab708d5d
VS
292 wxLogError(_("Cannot load resources from file '%s'."), m_data[i].File.c_str());
293 wxDELETE(m_data[i].Doc);
0ceae932 294 }
ab708d5d 295 else if (m_data[i].Doc->GetRoot()->GetName() != wxT("resource"))
0ceae932 296 {
ab708d5d
VS
297 wxLogError(_("Invalid XML resource '%s': doesn't have root node 'resource'."), m_data[i].File.c_str());
298 wxDELETE(m_data[i].Doc);
0ceae932
VS
299 }
300 else
16008659 301 {
e6b506bf
VS
302 long version;
303 int v1, v2, v3, v4;
304 wxString verstr = m_data[i].Doc->GetRoot()->GetPropVal(
305 wxT("version"), wxT("0.0.0.0"));
306 if (wxSscanf(verstr.c_str(), wxT("%i.%i.%i.%i"),
307 &v1, &v2, &v3, &v4) == 4)
308 version = v1*256*256*256+v2*256*256+v3*256+v4;
309 else
310 version = 0;
311 if (m_version == -1)
312 m_version = version;
313 if (m_version != version)
314 wxLogError(_("Resource files must have same version number!"));
315
ab708d5d
VS
316 ProcessPlatformProperty(m_data[i].Doc->GetRoot());
317 m_data[i].Time = file->GetModificationTime();
16008659 318 }
29b5f2cc 319
56d2f750 320# if wxUSE_FILESYSTEM
16008659 321 wxDELETE(file);
56d2f750 322# else
16008659 323 wxDELETE(stream);
56d2f750
VS
324# endif
325 }
326 }
327}
328
329
330
e066e256 331wxXmlNode *wxXmlResource::FindResource(const wxString& name, const wxString& classname)
56d2f750
VS
332{
333 UpdateResources(); //ensure everything is up-to-date
334
335 wxString dummy;
ab708d5d 336 for (size_t f = 0; f < m_data.GetCount(); f++)
56d2f750 337 {
ab708d5d
VS
338 if (m_data[f].Doc == NULL || m_data[f].Doc->GetRoot() == NULL) continue;
339 for (wxXmlNode *node = m_data[f].Doc->GetRoot()->GetChildren();
56d2f750 340 node; node = node->GetNext())
e066e256
VS
341 if (node->GetType() == wxXML_ELEMENT_NODE &&
342 (!classname ||
343 node->GetPropVal(wxT("class"), wxEmptyString) == classname) &&
344 node->GetName() == wxT("object") &&
345 node->GetPropVal(wxT("name"), &dummy) &&
346 dummy == name)
792064e9
VS
347 {
348#if wxUSE_FILESYSTEM
ab708d5d 349 m_curFileSystem.ChangePathTo(m_data[f].File);
792064e9 350#endif
56d2f750 351 return node;
792064e9 352 }
56d2f750
VS
353 }
354
e066e256
VS
355 wxLogError(_("XML resource '%s' (class '%s') not found!"),
356 name.c_str(), classname.c_str());
56d2f750
VS
357 return NULL;
358}
359
360
361
362wxObject *wxXmlResource::CreateResFromNode(wxXmlNode *node, wxObject *parent, wxObject *instance)
363{
364 if (node == NULL) return NULL;
365
366 wxXmlResourceHandler *handler;
367 wxObject *ret;
ab708d5d 368 wxNode * ND = m_handlers.GetFirst();
56d2f750
VS
369 while (ND)
370 {
371 handler = (wxXmlResourceHandler*)ND->GetData();
a559d708 372 if (node->GetName() == wxT("object") && handler->CanHandle(node))
56d2f750
VS
373 {
374 ret = handler->CreateResource(node, parent, instance);
375 if (ret) return ret;
376 }
377 ND = ND->GetNext();
378 }
379
e066e256
VS
380 wxLogError(_("No handler found for XML node '%s', class '%s'!"),
381 node->GetName().c_str(),
a559d708 382 node->GetPropVal(wxT("class"), wxEmptyString).c_str());
56d2f750
VS
383 return NULL;
384}
385
386
387
388
389
390
391
392
393
394wxXmlResourceHandler::wxXmlResourceHandler()
ab708d5d
VS
395 : m_node(NULL), m_parent(NULL), m_instance(NULL),
396 m_parentAsWindow(NULL), m_instanceAsWindow(NULL)
56d2f750
VS
397{}
398
399
400
401wxObject *wxXmlResourceHandler::CreateResource(wxXmlNode *node, wxObject *parent, wxObject *instance)
402{
ab708d5d
VS
403 wxXmlNode *myNode = m_node;
404 wxString myClass = m_class;
405 wxObject *myParent = m_parent, *myInstance = m_instance;
406 wxWindow *myParentAW = m_parentAsWindow, *myInstanceAW = m_instanceAsWindow;
56d2f750 407
ab708d5d
VS
408 m_node = node;
409 m_class = node->GetPropVal(wxT("class"), wxEmptyString);
410 m_parent = parent;
411 m_instance = instance;
412 m_parentAsWindow = wxDynamicCast(m_parent, wxWindow);
413 m_instanceAsWindow = wxDynamicCast(m_instance, wxWindow);
56d2f750
VS
414
415 wxObject *returned = DoCreateResource();
416
ab708d5d
VS
417 m_node = myNode;
418 m_class = myClass;
419 m_parent = myParent; m_parentAsWindow = myParentAW;
420 m_instance = myInstance; m_instanceAsWindow = myInstanceAW;
56d2f750
VS
421
422 return returned;
423}
424
425
56d2f750
VS
426void wxXmlResourceHandler::AddStyle(const wxString& name, int value)
427{
ab708d5d
VS
428 m_styleNames.Add(name);
429 m_styleValues.Add(value);
56d2f750
VS
430}
431
432
09dc1241
VS
433
434void wxXmlResourceHandler::AddWindowStyles()
435{
436 ADD_STYLE(wxSIMPLE_BORDER);
437 ADD_STYLE(wxSUNKEN_BORDER);
438 ADD_STYLE(wxDOUBLE_BORDER);
439 ADD_STYLE(wxRAISED_BORDER);
440 ADD_STYLE(wxSTATIC_BORDER);
441 ADD_STYLE(wxTRANSPARENT_WINDOW);
442 ADD_STYLE(wxWANTS_CHARS);
443 ADD_STYLE(wxNO_FULL_REPAINT_ON_RESIZE);
444}
445
446
447
56d2f750
VS
448bool wxXmlResourceHandler::HasParam(const wxString& param)
449{
450 return (GetParamNode(param) != NULL);
451}
452
453
454int wxXmlResourceHandler::GetStyle(const wxString& param, int defaults)
455{
456 wxString s = GetParamValue(param);
457
458 if (!s) return defaults;
459
a559d708 460 wxStringTokenizer tkn(s, wxT("| "), wxTOKEN_STRTOK);
56d2f750
VS
461 int style = 0;
462 int index;
463 wxString fl;
464 while (tkn.HasMoreTokens())
465 {
466 fl = tkn.GetNextToken();
ab708d5d 467 index = m_styleNames.Index(fl);
56d2f750 468 if (index != wxNOT_FOUND)
ab708d5d 469 style |= m_styleValues[index];
56d2f750
VS
470 else
471 wxLogError(_("Unknown style flag ") + fl);
472 }
473 return style;
474}
475
476
477
478wxString wxXmlResourceHandler::GetText(const wxString& param)
479{
480 wxString str1 = GetParamValue(param);
481 wxString str2;
482 const wxChar *dt;
e6b506bf
VS
483 wxChar amp_char;
484
485 // VS: First version of XML resources used $ instead of & (which is illegal in XML),
486 // but later I realized that '_' fits this purpose much better (because
487 // &File means "File with F underlined").
488 if (m_resource->CompareVersion(2,3,0,1) < 0)
489 amp_char = wxT('$');
490 else
491 amp_char = wxT('_');
56d2f750
VS
492
493 for (dt = str1.c_str(); *dt; dt++)
494 {
e6b506bf
VS
495 // Remap amp_char to &, map double amp_char to amp_char (for things
496 // like "&File..." -- this is illegal in XML, so we use "_File..."):
497 if (*dt == amp_char)
498 {
499 if ( *(++dt) == amp_char )
500 str2 << amp_char;
501 else
502 str2 << wxT('&') << *dt;
503 }
504 // Remap \n to CR, \r to LF, \t to TAB:
505 else if (*dt == wxT('\\'))
56d2f750
VS
506 switch (*(++dt))
507 {
e6b506bf
VS
508 case wxT('n') : str2 << wxT('\n'); break;
509 case wxT('t') : str2 << wxT('\t'); break;
510 case wxT('r') : str2 << wxT('\r'); break;
511 default : str2 << wxT('\\') << *dt; break;
56d2f750
VS
512 }
513 else str2 << *dt;
514 }
d33a1e8b 515
ab708d5d 516 if (m_resource->GetUseLocale())
d33a1e8b
VS
517 return wxGetTranslation(str2);
518 else
519 return str2;
56d2f750
VS
520}
521
522
523
524long wxXmlResourceHandler::GetLong(const wxString& param, long defaultv)
525{
526 long value;
527 wxString str1 = GetParamValue(param);
528
529 if (!str1.ToLong(&value))
530 value = defaultv;
531
532 return value;
533}
534
535
536int wxXmlResourceHandler::GetID()
537{
538 wxString sid = GetName();
539 long num;
540
a559d708 541 if (sid == wxT("-1")) return -1;
56d2f750 542 else if (sid.IsNumber() && sid.ToLong(&num)) return num;
a559d708 543#define stdID(id) else if (sid == wxT(#id)) return id
56d2f750
VS
544 stdID(wxID_OPEN); stdID(wxID_CLOSE); stdID(wxID_NEW);
545 stdID(wxID_SAVE); stdID(wxID_SAVEAS); stdID(wxID_REVERT);
546 stdID(wxID_EXIT); stdID(wxID_UNDO); stdID(wxID_REDO);
547 stdID(wxID_HELP); stdID(wxID_PRINT); stdID(wxID_PRINT_SETUP);
548 stdID(wxID_PREVIEW); stdID(wxID_ABOUT); stdID(wxID_HELP_CONTENTS);
549 stdID(wxID_HELP_COMMANDS); stdID(wxID_HELP_PROCEDURES);
550 stdID(wxID_CUT); stdID(wxID_COPY); stdID(wxID_PASTE);
551 stdID(wxID_CLEAR); stdID(wxID_FIND); stdID(wxID_DUPLICATE);
552 stdID(wxID_SELECTALL); stdID(wxID_OK); stdID(wxID_CANCEL);
553 stdID(wxID_APPLY); stdID(wxID_YES); stdID(wxID_NO);
554 stdID(wxID_STATIC); stdID(wxID_FORWARD); stdID(wxID_BACKWARD);
555 stdID(wxID_DEFAULT); stdID(wxID_MORE); stdID(wxID_SETUP);
556 stdID(wxID_RESET); stdID(wxID_HELP_CONTEXT);
557#undef stdID
558 else return XMLID(sid.c_str());
559}
560
561
562wxString wxXmlResourceHandler::GetName()
563{
ab708d5d 564 return m_node->GetPropVal(wxT("name"), wxT("-1"));
56d2f750
VS
565}
566
567
568
569bool wxXmlResourceHandler::GetBool(const wxString& param, bool defaultv)
570{
571 wxString v = GetParamValue(param);
572 v.MakeLower();
573 if (!v) return defaultv;
a559d708 574 else return (v == wxT("1"));
56d2f750
VS
575}
576
577
578
579wxColour wxXmlResourceHandler::GetColour(const wxString& param)
580{
581 wxString v = GetParamValue(param);
582 unsigned long tmp = 0;
583
fafa5632 584 if (v.Length() != 7 || v[0u] != wxT('#') ||
a559d708 585 wxSscanf(v.c_str(), wxT("#%lX"), &tmp) != 1)
56d2f750
VS
586 {
587 wxLogError(_("XML resource: Incorrect colour specification '%s' for property '%s'."),
588 v.c_str(), param.c_str());
589 return wxNullColour;
590 }
591
592 return wxColour((tmp & 0xFF0000) >> 16 ,
593 (tmp & 0x00FF00) >> 8,
594 (tmp & 0x0000FF));
595}
596
597
792064e9
VS
598
599wxBitmap wxXmlResourceHandler::GetBitmap(const wxString& param, wxSize size)
600{
601 wxString name = GetParamValue(param);
602 if (name.IsEmpty()) return wxNullBitmap;
603#if wxUSE_FILESYSTEM
604 wxFSFile *fsfile = GetCurFileSystem().OpenFile(name);
605 if (fsfile == NULL)
606 {
607 wxLogError(_("XML resource: Cannot create bitmap from '%s'."), param.mb_str());
608 return wxNullBitmap;
609 }
610 wxImage img(*(fsfile->GetStream()));
611 delete fsfile;
612#else
a559d708 613 wxImage img(GetParamValue(wxT("bitmap")));
792064e9
VS
614#endif
615 if (!img.Ok())
616 {
617 wxLogError(_("XML resource: Cannot create bitmap from '%s'."), param.mb_str());
618 return wxNullBitmap;
619 }
620 if (!(size == wxDefaultSize)) img.Rescale(size.x, size.y);
621 return img.ConvertToBitmap();
622}
623
624
625
626wxIcon wxXmlResourceHandler::GetIcon(const wxString& param, wxSize size)
627{
bebb14d5 628#if wxCHECK_VERSION(2,3,0) || defined(__WXMSW__)
792064e9
VS
629 wxIcon icon;
630 icon.CopyFromBitmap(GetBitmap(param, size));
631#else
632 wxIcon *iconpt;
633 wxBitmap bmppt = GetBitmap(param, size);
634 iconpt = (wxIcon*)(&bmppt);
635 wxIcon icon(*iconpt);
636#endif
637 return icon;
638}
639
640
641
56d2f750
VS
642wxXmlNode *wxXmlResourceHandler::GetParamNode(const wxString& param)
643{
ab708d5d 644 wxXmlNode *n = m_node->GetChildren();
56d2f750
VS
645
646 while (n)
647 {
648 if (n->GetType() == wxXML_ELEMENT_NODE && n->GetName() == param)
649 return n;
650 n = n->GetNext();
651 }
652 return NULL;
653}
654
655
656wxString wxXmlResourceHandler::GetNodeContent(wxXmlNode *node)
657{
658 wxXmlNode *n = node;
659 if (n == NULL) return wxEmptyString;
660 n = n->GetChildren();
661
662 while (n)
663 {
664 if (n->GetType() == wxXML_TEXT_NODE ||
665 n->GetType() == wxXML_CDATA_SECTION_NODE)
666 return n->GetContent();
667 n = n->GetNext();
668 }
669 return wxEmptyString;
670}
671
672
673
674wxString wxXmlResourceHandler::GetParamValue(const wxString& param)
675{
d33a1e8b 676 if (param.IsEmpty())
ab708d5d 677 return GetNodeContent(m_node);
d33a1e8b
VS
678 else
679 return GetNodeContent(GetParamNode(param));
56d2f750
VS
680}
681
682
683
684wxSize wxXmlResourceHandler::GetSize(const wxString& param)
685{
686 wxString s = GetParamValue(param);
a559d708 687 if (s.IsEmpty()) s = wxT("-1,-1");
56d2f750
VS
688 bool is_dlg;
689 long sx, sy;
690
a559d708 691 is_dlg = s[s.Length()-1] == wxT('d');
56d2f750
VS
692 if (is_dlg) s.RemoveLast();
693
a559d708
VS
694 if (!s.BeforeFirst(wxT(',')).ToLong(&sx) ||
695 !s.AfterLast(wxT(',')).ToLong(&sy))
56d2f750
VS
696 {
697 wxLogError(_("Cannot parse coordinates from '%s'."), s.mb_str());
698 return wxDefaultSize;
699 }
700
701 if (is_dlg)
702 {
ab708d5d
VS
703 if (m_instanceAsWindow)
704 return wxDLG_UNIT(m_instanceAsWindow, wxSize(sx, sy));
705 else if (m_parentAsWindow)
706 return wxDLG_UNIT(m_parentAsWindow, wxSize(sx, sy));
56d2f750
VS
707 else
708 {
709 wxLogError(_("Cannot convert dialog units: dialog unknown."));
710 return wxDefaultSize;
711 }
712 }
713 else return wxSize(sx, sy);
714}
715
716
717
718wxPoint wxXmlResourceHandler::GetPosition(const wxString& param)
719{
720 wxSize sz = GetSize(param);
721 return wxPoint(sz.x, sz.y);
722}
723
724
725
bebb14d5
VS
726wxCoord wxXmlResourceHandler::GetDimension(const wxString& param, wxCoord defaultv)
727{
728 wxString s = GetParamValue(param);
729 if (s.IsEmpty()) return defaultv;
730 bool is_dlg;
731 long sx;
732
a559d708 733 is_dlg = s[s.Length()-1] == wxT('d');
bebb14d5
VS
734 if (is_dlg) s.RemoveLast();
735
736 if (!s.ToLong(&sx))
737 {
738 wxLogError(_("Cannot parse dimension from '%s'."), s.mb_str());
739 return defaultv;
740 }
741
742 if (is_dlg)
743 {
ab708d5d
VS
744 if (m_instanceAsWindow)
745 return wxDLG_UNIT(m_instanceAsWindow, wxSize(sx, 0)).x;
746 else if (m_parentAsWindow)
747 return wxDLG_UNIT(m_parentAsWindow, wxSize(sx, 0)).x;
bebb14d5
VS
748 else
749 {
750 wxLogError(_("Cannot convert dialog units: dialog unknown."));
751 return defaultv;
752 }
753 }
754 else return sx;
755}
756
757
758
d33a1e8b
VS
759wxFont wxXmlResourceHandler::GetFont(const wxString& param)
760{
761 wxXmlNode *font_node = GetParamNode(param);
762 if (font_node == NULL)
763 {
dc743689 764 wxLogError(_("Cannot find font node '%s'."), param.mb_str());
d33a1e8b
VS
765 return wxNullFont;
766 }
767
ab708d5d
VS
768 wxXmlNode *oldnode = m_node;
769 m_node = font_node;
d33a1e8b 770
a559d708 771 long size = GetLong(wxT("size"), 12);
d33a1e8b 772
a559d708
VS
773 wxString style = GetParamValue(wxT("style"));
774 wxString weight = GetParamValue(wxT("weight"));
d33a1e8b 775 int istyle = wxNORMAL, iweight = wxNORMAL;
a559d708
VS
776 if (style == wxT("italic")) istyle = wxITALIC;
777 else if (style == wxT("slant")) istyle = wxSLANT;
778 if (weight == wxT("bold")) iweight = wxBOLD;
779 else if (weight == wxT("light")) iweight = wxLIGHT;
dc743689 780
a559d708 781 wxString family = GetParamValue(wxT("family"));
dc743689 782 int ifamily = wxDEFAULT;
a559d708
VS
783 if (family == wxT("decorative")) ifamily = wxDECORATIVE;
784 else if (family == wxT("roman")) ifamily = wxROMAN;
785 else if (family == wxT("script")) ifamily = wxSCRIPT;
786 else if (family == wxT("swiss")) ifamily = wxSWISS;
787 else if (family == wxT("modern")) ifamily = wxMODERN;
dc743689 788
a559d708 789 bool underlined = GetBool(wxT("underlined"), FALSE);
dc743689 790
a559d708 791 wxString encoding = GetParamValue(wxT("encoding"));
dc743689
VS
792 wxFontMapper mapper;
793 wxFontEncoding enc = wxFONTENCODING_DEFAULT;
794 if (!encoding.IsEmpty()) enc = mapper.CharsetToEncoding(encoding);
795 if (enc == wxFONTENCODING_SYSTEM) enc = wxFONTENCODING_SYSTEM;
796
a559d708 797 wxString faces = GetParamValue(wxT("face"));
d33a1e8b
VS
798 wxString facename = wxEmptyString;
799 wxFontEnumerator enu;
800 enu.EnumerateFacenames();
a559d708 801 wxStringTokenizer tk(faces, wxT(","));
d33a1e8b
VS
802 while (tk.HasMoreTokens())
803 {
804 int index = enu.GetFacenames()->Index(tk.GetNextToken(), FALSE);
805 if (index != wxNOT_FOUND)
806 {
807 facename = (*enu.GetFacenames())[index];
808 break;
809 }
810 }
811
ab708d5d 812 m_node = oldnode;
d33a1e8b 813
dc743689 814 wxFont font(size, ifamily, istyle, iweight, underlined, facename, enc);
d33a1e8b
VS
815 return font;
816}
817
818
56d2f750
VS
819void wxXmlResourceHandler::SetupWindow(wxWindow *wnd)
820{
d33a1e8b 821 //FIXME : add cursor
56d2f750 822
a559d708
VS
823 if (HasParam(wxT("exstyle")))
824 wnd->SetExtraStyle(GetStyle(wxT("exstyle")));
825 if (HasParam(wxT("bg")))
826 wnd->SetBackgroundColour(GetColour(wxT("bg")));
827 if (HasParam(wxT("fg")))
828 wnd->SetForegroundColour(GetColour(wxT("fg")));
829 if (GetBool(wxT("enabled"), 1) == 0)
56d2f750 830 wnd->Enable(FALSE);
a559d708 831 if (GetBool(wxT("focused"), 0) == 1)
56d2f750 832 wnd->SetFocus();
a559d708 833 if (GetBool(wxT("hidden"), 0) == 1)
56d2f750
VS
834 wnd->Show(FALSE);
835#if wxUSE_TOOLTIPS
a559d708
VS
836 if (HasParam(wxT("tooltip")))
837 wnd->SetToolTip(GetText(wxT("tooltip")));
56d2f750 838#endif
a559d708 839 if (HasParam(wxT("font")))
d33a1e8b 840 wnd->SetFont(GetFont());
56d2f750
VS
841}
842
843
e066e256 844void wxXmlResourceHandler::CreateChildren(wxObject *parent, bool this_hnd_only)
56d2f750 845{
ab708d5d 846 wxXmlNode *n = m_node->GetChildren();
792064e9 847
56d2f750
VS
848 while (n)
849 {
e066e256 850 if (n->GetType() == wxXML_ELEMENT_NODE &&
a559d708 851 n->GetName() == wxT("object"))
56d2f750 852 {
e066e256
VS
853 if (this_hnd_only && CanHandle(n))
854 CreateResource(n, parent, NULL);
56d2f750 855 else
ab708d5d 856 m_resource->CreateResFromNode(n, parent, NULL);
56d2f750
VS
857 }
858 n = n->GetNext();
859 }
860}
861
862
f485492a 863void wxXmlResourceHandler::CreateChildrenPrivately(wxObject *parent, wxXmlNode *rootnode)
e066e256
VS
864{
865 wxXmlNode *root;
ab708d5d 866 if (rootnode == NULL) root = m_node; else root = rootnode;
e066e256
VS
867 wxXmlNode *n = root->GetChildren();
868
869 while (n)
870 {
871 if (n->GetType() == wxXML_ELEMENT_NODE && CanHandle(n))
872 {
873 CreateResource(n, parent, NULL);
874 }
875 n = n->GetNext();
876 }
877}
878
879
56d2f750
VS
880
881
882
883
884
885
886
887// --------------- XMLID implementation -----------------------------
888
56d2f750
VS
889#define XMLID_TABLE_SIZE 1024
890
29b5f2cc 891
56d2f750
VS
892struct XMLID_record
893{
894 int id;
29b5f2cc 895 char *key;
56d2f750
VS
896 XMLID_record *next;
897};
898
899static XMLID_record *XMLID_Records[XMLID_TABLE_SIZE] = {NULL};
56d2f750
VS
900
901/*static*/ int wxXmlResource::GetXMLID(const char *str_id)
902{
429fdefc
VS
903 static int XMLID_LastID = wxID_HIGHEST;
904
56d2f750
VS
905 int index = 0;
906
907 for (const char *c = str_id; *c != '\0'; c++) index += (int)*c;
908 index %= XMLID_TABLE_SIZE;
909
910 XMLID_record *oldrec = NULL;
29b5f2cc 911 int matchcnt = 0;
56d2f750
VS
912 for (XMLID_record *rec = XMLID_Records[index]; rec; rec = rec->next)
913 {
29b5f2cc
VS
914 if (strcmp(rec->key, str_id) == 0)
915 {
29b5f2cc
VS
916 return rec->id;
917 }
918 matchcnt++;
56d2f750
VS
919 oldrec = rec;
920 }
921
922 XMLID_record **rec_var = (oldrec == NULL) ?
923 &XMLID_Records[index] : &oldrec->next;
924 *rec_var = new XMLID_record;
925 (*rec_var)->id = ++XMLID_LastID;
29b5f2cc 926 (*rec_var)->key = strdup(str_id);
56d2f750
VS
927 (*rec_var)->next = NULL;
928
929 return (*rec_var)->id;
930}
931
932
933static void CleanXMLID_Record(XMLID_record *rec)
934{
935 if (rec)
936 {
937 CleanXMLID_Record(rec->next);
29b5f2cc 938 free (rec->key);
56d2f750
VS
939 delete rec;
940 }
941}
942
943static void CleanXMLID_Records()
944{
945 for (int i = 0; i < XMLID_TABLE_SIZE; i++)
946 CleanXMLID_Record(XMLID_Records[i]);
947}
948
949
950
951
952
953
954
955
956// --------------- module and globals -----------------------------
957
958
959static wxXmlResource gs_XmlResource;
960
961wxXmlResource *wxTheXmlResource = &gs_XmlResource;
962
963
964class wxXmlResourceModule: public wxModule
965{
966DECLARE_DYNAMIC_CLASS(wxXmlResourceModule)
967public:
968 wxXmlResourceModule() {}
969 bool OnInit() {return TRUE;}
970 void OnExit()
971 {
972 wxTheXmlResource->ClearHandlers();
973 CleanXMLID_Records();
974 }
975};
976
977IMPLEMENT_DYNAMIC_CLASS(wxXmlResourceModule, wxModule)