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