]> git.saurik.com Git - wxWidgets.git/blame - src/xrc/xmlres.cpp
Add missing WXK constants for the control keys
[wxWidgets.git] / src / xrc / xmlres.cpp
CommitLineData
78d14f80 1/////////////////////////////////////////////////////////////////////////////
88a7a4e1 2// Name: src/xrc/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
78d14f80
VS
11// For compilers that support precompilation, includes "wx.h".
12#include "wx/wxprec.h"
13
14#ifdef __BORLANDC__
15 #pragma hdrstop
16#endif
17
621be1ec 18#if wxUSE_XRC
a1e4ec87 19
88a7a4e1
WS
20#include "wx/xrc/xmlres.h"
21
22#ifndef WX_PRECOMP
23 #include "wx/intl.h"
e4db172a 24 #include "wx/log.h"
8e609c82 25 #include "wx/panel.h"
76b49cf4 26 #include "wx/frame.h"
fdf565fe 27 #include "wx/dialog.h"
9eddec69 28 #include "wx/settings.h"
0bca0373 29 #include "wx/bitmap.h"
155ecd4c 30 #include "wx/image.h"
02761f6c 31 #include "wx/module.h"
193d0c93 32 #include "wx/wxcrtvararg.h"
88a7a4e1
WS
33#endif
34
e7a3a5a5 35#ifndef __WXWINCE__
88a7a4e1 36 #include <locale.h>
e7a3a5a5 37#endif
1df61962 38
eb2d0d23 39#include "wx/vector.h"
78d14f80
VS
40#include "wx/wfstream.h"
41#include "wx/filesys.h"
317a0d73 42#include "wx/filename.h"
78d14f80
VS
43#include "wx/tokenzr.h"
44#include "wx/fontenum.h"
78d14f80 45#include "wx/fontmap.h"
af1337b0 46#include "wx/artprov.h"
326462ae 47#include "wx/imaglist.h"
2bb9a404 48#include "wx/dir.h"
34c6bbee 49#include "wx/xml/xml.h"
0526c8cc 50#include "wx/hashset.h"
9fc5a47c 51#include "wx/scopedptr.h"
78d14f80 52
9fc5a47c
VZ
53namespace
54{
55
56// Helper function to get modification time of either a wxFileSystem URI or
57// just a normal file name, depending on the build.
58#if wxUSE_DATETIME
59
60wxDateTime GetXRCFileModTime(const wxString& filename)
61{
62#if wxUSE_FILESYSTEM
63 wxFileSystem fsys;
64 wxScopedPtr<wxFSFile> file(fsys.OpenFile(filename));
65
66 return file ? file->GetModificationTime() : wxDateTime();
67#else // wxUSE_FILESYSTEM
68 return wxDateTime(wxFileModificationTime(filename));
69#endif // wxUSE_FILESYSTEM
70}
71
72#endif // wxUSE_DATETIME
73
74} // anonymous namespace
f35fdf7e 75
d265ec6b
VZ
76// Assign the given value to the specified entry or add a new value with this
77// name.
3cf2fe54 78static void XRCID_Assign(const wxString& str_id, int value);
d265ec6b 79
eb2d0d23
VS
80class wxXmlResourceDataRecord
81{
82public:
9fc5a47c
VZ
83 // Ctor takes ownership of the document pointer.
84 wxXmlResourceDataRecord(const wxString& File_,
85 wxXmlDocument *Doc_
86 )
87 : File(File_), Doc(Doc_)
88 {
eb2d0d23 89#if wxUSE_DATETIME
9fc5a47c 90 Time = GetXRCFileModTime(File);
eb2d0d23
VS
91#endif
92 }
9fc5a47c 93
eb2d0d23
VS
94 ~wxXmlResourceDataRecord() {delete Doc;}
95
96 wxString File;
97 wxXmlDocument *Doc;
98#if wxUSE_DATETIME
99 wxDateTime Time;
100#endif
9fc5a47c
VZ
101
102 wxDECLARE_NO_COPY_CLASS(wxXmlResourceDataRecord);
eb2d0d23
VS
103};
104
f396e5a7 105class wxXmlResourceDataRecords : public wxVector<wxXmlResourceDataRecord*>
eb2d0d23
VS
106{
107 // this is a class so that it can be forward-declared
108};
78d14f80 109
7a7fa93b 110WX_DECLARE_HASH_SET_PTR(int, wxIntegerHash, wxIntegerEqual, wxHashSetInt);
0526c8cc
VZ
111
112class wxIdRange // Holds data for a particular rangename
113{
114protected:
115 wxIdRange(const wxXmlNode* node,
116 const wxString& rname,
117 const wxString& startno,
118 const wxString& rsize);
119
120 // Note the existence of an item within the range
121 void NoteItem(const wxXmlNode* node, const wxString& item);
122
123 // The manager is telling us that it's finished adding items
124 void Finalise(const wxXmlNode* node);
125
126 wxString GetName() const { return m_name; }
127 bool IsFinalised() const { return m_finalised; }
128
129 const wxString m_name;
130 int m_start;
131 int m_end;
132 unsigned int m_size;
133 bool m_item_end_found;
134 bool m_finalised;
135 wxHashSetInt m_indices;
136
137 friend class wxIdRangeManager;
138};
139
140class wxIdRangeManager
141{
142public:
143 ~wxIdRangeManager();
144 // Gets the global resources object or creates one if none exists.
145 static wxIdRangeManager *Get();
146
147 // Sets the global resources object and returns a pointer to the previous
148 // one (may be NULL).
149 static wxIdRangeManager *Set(wxIdRangeManager *res);
150
151 // Create a new IDrange from this node
152 void AddRange(const wxXmlNode* node);
153 // Tell the IdRange that this item exists, and should be pre-allocated an ID
154 void NotifyRangeOfItem(const wxXmlNode* node, const wxString& item) const;
155 // Tells all IDranges that they're now complete, and can create their IDs
156 void FinaliseRanges(const wxXmlNode* node) const;
157 // Searches for a known IdRange matching 'name', returning its index or -1
158 int Find(const wxString& rangename) const;
0526c8cc
VZ
159
160protected:
161 wxIdRange* FindRangeForItem(const wxXmlNode* node,
162 const wxString& item,
163 wxString& value) const;
164 wxVector<wxIdRange*> m_IdRanges;
165
166private:
167 static wxIdRangeManager *ms_instance;
168};
169
23239d94
VZ
170namespace
171{
172
173// helper used by DoFindResource() and elsewhere: returns true if this is an
174// object or object_ref node
175//
176// node must be non-NULL
177inline bool IsObjectNode(wxXmlNode *node)
178{
179 return node->GetType() == wxXML_ELEMENT_NODE &&
180 (node->GetName() == wxS("object") ||
181 node->GetName() == wxS("object_ref"));
182}
183
a1eeda0b
VS
184// special XML attribute with name of input file, see GetFileNameFromNode()
185const char *ATTR_INPUT_FILENAME = "__wx:filename";
186
187// helper to get filename corresponding to an XML node
188wxString
1f6ea935 189GetFileNameFromNode(const wxXmlNode *node, const wxXmlResourceDataRecords& files)
a1eeda0b
VS
190{
191 // this loop does two things: it looks for ATTR_INPUT_FILENAME among
192 // parents and if it isn't used, it finds the root of the XML tree 'node'
193 // is in
194 for ( ;; )
195 {
196 // in some rare cases (specifically, when an <object_ref> is used, see
197 // wxXmlResource::CreateResFromNode() and MergeNodesOver()), we work
198 // with XML nodes that are not rooted in any document from 'files'
199 // (because a new node was created by CreateResFromNode() to merge the
200 // content of <object_ref> and the referenced <object>); in that case,
201 // we hack around the problem by putting the information about input
202 // file into a custom attribute
203 if ( node->HasAttribute(ATTR_INPUT_FILENAME) )
204 return node->GetAttribute(ATTR_INPUT_FILENAME);
205
206 if ( !node->GetParent() )
207 break; // we found the root of this XML tree
208
209 node = node->GetParent();
210 }
211
212 // NB: 'node' now points to the root of XML document
213
214 for ( wxXmlResourceDataRecords::const_iterator i = files.begin();
215 i != files.end(); ++i )
216 {
217 if ( (*i)->Doc->GetRoot() == node )
218 {
219 return (*i)->File;
220 }
221 }
222
223 return wxEmptyString; // not found
224}
225
23239d94
VZ
226} // anonymous namespace
227
78d14f80 228
824e8eaa
VS
229wxXmlResource *wxXmlResource::ms_instance = NULL;
230
231/*static*/ wxXmlResource *wxXmlResource::Get()
232{
233 if ( !ms_instance )
234 ms_instance = new wxXmlResource;
235 return ms_instance;
236}
237
238/*static*/ wxXmlResource *wxXmlResource::Set(wxXmlResource *res)
239{
240 wxXmlResource *old = ms_instance;
241 ms_instance = res;
242 return old;
243}
244
d4a724d4 245wxXmlResource::wxXmlResource(int flags, const wxString& domain)
78d14f80 246{
daa85ee3 247 m_flags = flags;
78d14f80 248 m_version = -1;
eb2d0d23 249 m_data = new wxXmlResourceDataRecords;
d7a80cf5 250 SetDomain(domain);
78d14f80
VS
251}
252
d4a724d4 253wxXmlResource::wxXmlResource(const wxString& filemask, int flags, const wxString& domain)
78d14f80 254{
daa85ee3 255 m_flags = flags;
78d14f80 256 m_version = -1;
eb2d0d23 257 m_data = new wxXmlResourceDataRecords;
d7a80cf5 258 SetDomain(domain);
78d14f80
VS
259 Load(filemask);
260}
261
262wxXmlResource::~wxXmlResource()
263{
264 ClearHandlers();
eb2d0d23 265
f396e5a7
VS
266 for ( wxXmlResourceDataRecords::iterator i = m_data->begin();
267 i != m_data->end(); ++i )
268 {
269 delete *i;
270 }
eb2d0d23 271 delete m_data;
78d14f80
VS
272}
273
d7a80cf5 274void wxXmlResource::SetDomain(const wxString& domain)
d4a724d4 275{
d7a80cf5 276 m_domain = domain;
d4a724d4
RD
277}
278
78d14f80 279
60fd818a
VZ
280/* static */
281wxString wxXmlResource::ConvertFileNameToURL(const wxString& filename)
282{
283 wxString fnd(filename);
284
285 // NB: as Load() and Unload() accept both filenames and URLs (should
286 // probably be changed to filenames only, but embedded resources
287 // currently rely on its ability to handle URLs - FIXME) we need to
288 // determine whether found name is filename and not URL and this is the
289 // fastest/simplest way to do it
290 if (wxFileName::FileExists(fnd))
291 {
292 // Make the name absolute filename, because the app may
293 // change working directory later:
294 wxFileName fn(fnd);
295 if (fn.IsRelative())
296 {
297 fn.MakeAbsolute();
298 fnd = fn.GetFullPath();
299 }
300#if wxUSE_FILESYSTEM
301 fnd = wxFileSystem::FileNameToURL(fnd);
302#endif
303 }
304
305 return fnd;
306}
307
308#if wxUSE_FILESYSTEM
309
310/* static */
311bool wxXmlResource::IsArchive(const wxString& filename)
312{
313 const wxString fnd = filename.Lower();
314
315 return fnd.Matches(wxT("*.zip")) || fnd.Matches(wxT("*.xrs"));
316}
317
318#endif // wxUSE_FILESYSTEM
319
04ae32cd
VS
320bool wxXmlResource::LoadFile(const wxFileName& file)
321{
159852ae 322#if wxUSE_FILESYSTEM
04ae32cd 323 return Load(wxFileSystem::FileNameToURL(file));
159852ae
VS
324#else
325 return Load(file.GetFullPath());
326#endif
04ae32cd
VS
327}
328
2bb9a404
VS
329bool wxXmlResource::LoadAllFiles(const wxString& dirname)
330{
331 bool ok = true;
332 wxArrayString files;
333
334 wxDir::GetAllFiles(dirname, &files, "*.xrc");
335
336 for ( wxArrayString::const_iterator i = files.begin(); i != files.end(); ++i )
337 {
338 if ( !LoadFile(*i) )
339 ok = false;
340 }
341
342 return ok;
343}
344
acd32ffc 345bool wxXmlResource::Load(const wxString& filemask_)
78d14f80 346{
acd32ffc
VS
347 wxString filemask = ConvertFileNameToURL(filemask_);
348
9fc5a47c
VZ
349 bool allOK = true;
350
78d14f80
VS
351#if wxUSE_FILESYSTEM
352 wxFileSystem fsys;
353# define wxXmlFindFirst fsys.FindFirst(filemask, wxFILE)
354# define wxXmlFindNext fsys.FindNext()
355#else
356# define wxXmlFindFirst wxFindFirstFile(filemask, wxFILE)
357# define wxXmlFindNext wxFindNextFile()
358#endif
e5bb501b
VZ
359 wxString fnd = wxXmlFindFirst;
360 if ( fnd.empty() )
361 {
362 wxLogError(_("Cannot load resources from '%s'."), filemask);
363 return false;
364 }
365
ec157c8f 366 while (!fnd.empty())
78d14f80
VS
367 {
368#if wxUSE_FILESYSTEM
60fd818a 369 if ( IsArchive(fnd) )
78d14f80 370 {
e5bb501b 371 if ( !Load(fnd + wxT("#zip:*.xrc")) )
9fc5a47c 372 allOK = false;
78d14f80 373 }
60fd818a
VZ
374 else // a single resource URL
375#endif // wxUSE_FILESYSTEM
78d14f80 376 {
9fc5a47c
VZ
377 wxXmlDocument * const doc = DoLoadFile(fnd);
378 if ( !doc )
379 allOK = false;
380 else
381 Data().push_back(new wxXmlResourceDataRecord(fnd, doc));
78d14f80
VS
382 }
383
e5bb501b 384 fnd = wxXmlFindNext;
78d14f80
VS
385 }
386# undef wxXmlFindFirst
387# undef wxXmlFindNext
e5bb501b 388
9fc5a47c 389 return allOK;
78d14f80
VS
390}
391
60fd818a
VZ
392bool wxXmlResource::Unload(const wxString& filename)
393{
394 wxASSERT_MSG( !wxIsWild(filename),
9a83f860 395 wxT("wildcards not supported by wxXmlResource::Unload()") );
60fd818a
VZ
396
397 wxString fnd = ConvertFileNameToURL(filename);
398#if wxUSE_FILESYSTEM
399 const bool isArchive = IsArchive(fnd);
400 if ( isArchive )
9a83f860 401 fnd += wxT("#zip:");
60fd818a
VZ
402#endif // wxUSE_FILESYSTEM
403
404 bool unloaded = false;
eb2d0d23
VS
405 for ( wxXmlResourceDataRecords::iterator i = Data().begin();
406 i != Data().end(); ++i )
60fd818a
VZ
407 {
408#if wxUSE_FILESYSTEM
409 if ( isArchive )
410 {
f396e5a7 411 if ( (*i)->File.StartsWith(fnd) )
60fd818a
VZ
412 unloaded = true;
413 // don't break from the loop, we can have other matching files
414 }
415 else // a single resource URL
416#endif // wxUSE_FILESYSTEM
417 {
f396e5a7 418 if ( (*i)->File == fnd )
60fd818a 419 {
f8b1df0b 420 delete *i;
eb2d0d23 421 Data().erase(i);
60fd818a
VZ
422 unloaded = true;
423
424 // no sense in continuing, there is only one file with this URL
425 break;
426 }
427 }
428 }
429
430 return unloaded;
431}
432
78d14f80 433
78d14f80
VS
434void wxXmlResource::AddHandler(wxXmlResourceHandler *handler)
435{
a3b9c43b
VZ
436 wxXmlResourceHandlerImpl *impl = new wxXmlResourceHandlerImpl(handler);
437 handler->SetImpl(impl);
eb2d0d23 438 m_handlers.push_back(handler);
78d14f80
VS
439 handler->SetParentResource(this);
440}
441
92e898b0
RD
442void wxXmlResource::InsertHandler(wxXmlResourceHandler *handler)
443{
a3b9c43b
VZ
444 wxXmlResourceHandlerImpl *impl = new wxXmlResourceHandlerImpl(handler);
445 handler->SetImpl(impl);
eb2d0d23 446 m_handlers.insert(m_handlers.begin(), handler);
92e898b0
RD
447 handler->SetParentResource(this);
448}
449
78d14f80
VS
450
451
452void wxXmlResource::ClearHandlers()
453{
eb2d0d23
VS
454 for ( wxVector<wxXmlResourceHandler*>::iterator i = m_handlers.begin();
455 i != m_handlers.end(); ++i )
456 delete *i;
457 m_handlers.clear();
78d14f80
VS
458}
459
460
78d14f80
VS
461wxMenu *wxXmlResource::LoadMenu(const wxString& name)
462{
463 return (wxMenu*)CreateResFromNode(FindResource(name, wxT("wxMenu")), NULL, NULL);
464}
465
466
467
4a1b9596 468wxMenuBar *wxXmlResource::LoadMenuBar(wxWindow *parent, const wxString& name)
78d14f80 469{
4a1b9596 470 return (wxMenuBar*)CreateResFromNode(FindResource(name, wxT("wxMenuBar")), parent, NULL);
78d14f80
VS
471}
472
473
474
4a1b9596 475#if wxUSE_TOOLBAR
78d14f80
VS
476wxToolBar *wxXmlResource::LoadToolBar(wxWindow *parent, const wxString& name)
477{
478 return (wxToolBar*)CreateResFromNode(FindResource(name, wxT("wxToolBar")), parent, NULL);
479}
4a1b9596 480#endif
78d14f80
VS
481
482
483wxDialog *wxXmlResource::LoadDialog(wxWindow *parent, const wxString& name)
484{
4dd75a6a 485 return (wxDialog*)CreateResFromNode(FindResource(name, wxT("wxDialog")), parent, NULL);
78d14f80
VS
486}
487
488bool wxXmlResource::LoadDialog(wxDialog *dlg, wxWindow *parent, const wxString& name)
489{
490 return CreateResFromNode(FindResource(name, wxT("wxDialog")), parent, dlg) != NULL;
491}
492
493
494
495wxPanel *wxXmlResource::LoadPanel(wxWindow *parent, const wxString& name)
496{
497 return (wxPanel*)CreateResFromNode(FindResource(name, wxT("wxPanel")), parent, NULL);
498}
499
500bool wxXmlResource::LoadPanel(wxPanel *panel, wxWindow *parent, const wxString& name)
501{
502 return CreateResFromNode(FindResource(name, wxT("wxPanel")), parent, panel) != NULL;
503}
504
92e898b0
RD
505wxFrame *wxXmlResource::LoadFrame(wxWindow* parent, const wxString& name)
506{
507 return (wxFrame*)CreateResFromNode(FindResource(name, wxT("wxFrame")), parent, NULL);
508}
509
78d14f80
VS
510bool wxXmlResource::LoadFrame(wxFrame* frame, wxWindow *parent, const wxString& name)
511{
512 return CreateResFromNode(FindResource(name, wxT("wxFrame")), parent, frame) != NULL;
513}
514
515wxBitmap wxXmlResource::LoadBitmap(const wxString& name)
516{
517 wxBitmap *bmp = (wxBitmap*)CreateResFromNode(
518 FindResource(name, wxT("wxBitmap")), NULL, NULL);
519 wxBitmap rt;
520
521 if (bmp) { rt = *bmp; delete bmp; }
522 return rt;
523}
524
525wxIcon wxXmlResource::LoadIcon(const wxString& name)
526{
527 wxIcon *icon = (wxIcon*)CreateResFromNode(
528 FindResource(name, wxT("wxIcon")), NULL, NULL);
529 wxIcon rt;
530
531 if (icon) { rt = *icon; delete icon; }
532 return rt;
533}
534
92e898b0 535
af0ac990
VZ
536wxObject *
537wxXmlResource::DoLoadObject(wxWindow *parent,
538 const wxString& name,
539 const wxString& classname,
540 bool recursive)
92e898b0 541{
af0ac990
VZ
542 wxXmlNode * const node = FindResource(name, classname, recursive);
543
544 return node ? DoCreateResFromNode(*node, parent, NULL) : NULL;
92e898b0
RD
545}
546
af0ac990
VZ
547bool
548wxXmlResource::DoLoadObject(wxObject *instance,
549 wxWindow *parent,
550 const wxString& name,
551 const wxString& classname,
552 bool recursive)
92e898b0 553{
af0ac990
VZ
554 wxXmlNode * const node = FindResource(name, classname, recursive);
555
556 return node && DoCreateResFromNode(*node, parent, instance) != NULL;
92e898b0
RD
557}
558
559
78d14f80
VS
560bool wxXmlResource::AttachUnknownControl(const wxString& name,
561 wxWindow *control, wxWindow *parent)
562{
563 if (parent == NULL)
564 parent = control->GetParent();
565 wxWindow *container = parent->FindWindow(name + wxT("_container"));
566 if (!container)
567 {
819559b2 568 wxLogError("Cannot find container for unknown control '%s'.", name);
f80ea77b 569 return false;
78d14f80
VS
570 }
571 return control->Reparent(container);
572}
573
574
77b2f9b1 575static void ProcessPlatformProperty(wxXmlNode *node)
78d14f80
VS
576{
577 wxString s;
578 bool isok;
579
580 wxXmlNode *c = node->GetChildren();
581 while (c)
582 {
f80ea77b 583 isok = false;
288b6107 584 if (!c->GetAttribute(wxT("platform"), &s))
f80ea77b 585 isok = true;
78d14f80
VS
586 else
587 {
2b5f62a0 588 wxStringTokenizer tkn(s, wxT(" |"));
78d14f80
VS
589
590 while (tkn.HasMoreTokens())
591 {
592 s = tkn.GetNextToken();
84389518 593#ifdef __WINDOWS__
d003330c
VS
594 if (s == wxT("win")) isok = true;
595#endif
b380439d 596#if defined(__MAC__) || defined(__APPLE__)
d003330c 597 if (s == wxT("mac")) isok = true;
b380439d
RD
598#elif defined(__UNIX__)
599 if (s == wxT("unix")) isok = true;
78d14f80 600#endif
d003330c
VS
601#ifdef __OS2__
602 if (s == wxT("os2")) isok = true;
603#endif
604
605 if (isok)
606 break;
78d14f80
VS
607 }
608 }
609
610 if (isok)
d7b1d73c 611 {
78d14f80 612 ProcessPlatformProperty(c);
d7b1d73c
VS
613 c = c->GetNext();
614 }
78d14f80
VS
615 else
616 {
d7b1d73c 617 wxXmlNode *c2 = c->GetNext();
db59a97c 618 node->RemoveChild(c);
78d14f80 619 delete c;
d7b1d73c 620 c = c2;
78d14f80 621 }
78d14f80
VS
622 }
623}
624
0526c8cc
VZ
625static void PreprocessForIdRanges(wxXmlNode *rootnode)
626{
627 // First go through the top level, looking for the names of ID ranges
628 // as processing items is a lot easier if names are already known
629 wxXmlNode *c = rootnode->GetChildren();
630 while (c)
631 {
632 if (c->GetName() == wxT("ids-range"))
633 wxIdRangeManager::Get()->AddRange(c);
634 c = c->GetNext();
635 }
636
637 // Next, examine every 'name' for the '[' that denotes an ID in a range
638 c = rootnode->GetChildren();
639 while (c)
640 {
641 wxString name = c->GetAttribute(wxT("name"));
642 if (name.find('[') != wxString::npos)
643 wxIdRangeManager::Get()->NotifyRangeOfItem(rootnode, name);
78d14f80 644
0526c8cc
VZ
645 // Do any children by recursion, then proceed to the next sibling
646 PreprocessForIdRanges(c);
647 c = c->GetNext();
648 }
649}
78d14f80 650
d614f51b 651bool wxXmlResource::UpdateResources()
78d14f80 652{
d614f51b 653 bool rt = true;
480505bc 654
eb2d0d23
VS
655 for ( wxXmlResourceDataRecords::iterator i = Data().begin();
656 i != Data().end(); ++i )
78d14f80 657 {
f396e5a7
VS
658 wxXmlResourceDataRecord* const rec = *i;
659
9fc5a47c 660 // Check if we need to reload this one.
78d14f80 661
9fc5a47c
VZ
662 // We never do it if this flag is specified.
663 if ( m_flags & wxXRC_NO_RELOADING )
664 continue;
665
666 // Otherwise check its modification time if we can.
667#if wxUSE_DATETIME
668 const wxDateTime lastModTime = GetXRCFileModTime(rec->File);
669
670 if ( lastModTime.IsValid() && lastModTime <= rec->Time )
671#else // !wxUSE_DATETIME
672 // Never reload the file contents: we can't know whether it changed or
673 // not in this build configuration and it would be unexpected and
674 // counter-productive to get a performance hit (due to constant
675 // reloading of XRC files) in a minimal wx build which is presumably
676 // used because of resource constraints of the current platform.
677#endif // wxUSE_DATETIME/!wxUSE_DATETIME
78d14f80 678 {
9fc5a47c
VZ
679 // No need to reload, the file wasn't modified since we did it
680 // last.
681 continue;
78d14f80
VS
682 }
683
9fc5a47c
VZ
684 wxXmlDocument * const doc = DoLoadFile(rec->File);
685 if ( !doc )
78d14f80 686 {
9fc5a47c
VZ
687 // Notice that we keep the old XML document: it seems better to
688 // preserve it instead of throwing it away if we have nothing to
689 // replace it with.
690 rt = false;
691 continue;
692 }
78d14f80 693
9fc5a47c
VZ
694 // Replace the old resource contents with the new one.
695 delete rec->Doc;
696 rec->Doc = doc;
78d14f80 697
9fc5a47c 698 // And, now that we loaded it successfully, update the last load time.
34af0de4 699#if wxUSE_DATETIME
9fc5a47c 700 rec->Time = lastModTime.IsValid() ? lastModTime : wxDateTime::Now();
34af0de4 701#endif // wxUSE_DATETIME
78d14f80 702 }
d614f51b
VS
703
704 return rt;
78d14f80
VS
705}
706
9fc5a47c
VZ
707wxXmlDocument *wxXmlResource::DoLoadFile(const wxString& filename)
708{
709 wxLogTrace(wxT("xrc"), wxT("opening file '%s'"), filename);
710
711 wxInputStream *stream = NULL;
712
713#if wxUSE_FILESYSTEM
714 wxFileSystem fsys;
715 wxScopedPtr<wxFSFile> file(fsys.OpenFile(filename));
716 if (file)
717 {
718 // Notice that we don't have ownership of the stream in this case, it
719 // remains owned by wxFSFile.
720 stream = file->GetStream();
721 }
722#else // !wxUSE_FILESYSTEM
723 wxFileInputStream fstream(filename);
724 stream = &fstream;
725#endif // wxUSE_FILESYSTEM/!wxUSE_FILESYSTEM
726
727 if ( !stream || !stream->IsOk() )
728 {
729 wxLogError(_("Cannot open resources file '%s'."), filename);
730 return NULL;
731 }
732
733 wxString encoding(wxT("UTF-8"));
734#if !wxUSE_UNICODE && wxUSE_INTL
735 if ( (GetFlags() & wxXRC_USE_LOCALE) == 0 )
736 {
737 // In case we are not using wxLocale to translate strings, convert the
738 // strings GUI's charset. This must not be done when wxXRC_USE_LOCALE
739 // is on, because it could break wxGetTranslation lookup.
740 encoding = wxLocale::GetSystemEncodingName();
741 }
742#endif
743
744 wxScopedPtr<wxXmlDocument> doc(new wxXmlDocument);
745 if (!doc->Load(*stream, encoding))
746 {
747 wxLogError(_("Cannot load resources from file '%s'."), filename);
748 return NULL;
749 }
750
751 wxXmlNode * const root = doc->GetRoot();
752 if (root->GetName() != wxT("resource"))
753 {
754 ReportError
755 (
756 root,
757 "invalid XRC resource, doesn't have root node <resource>"
758 );
759 return NULL;
760 }
761
762 long version;
763 int v1, v2, v3, v4;
764 wxString verstr = root->GetAttribute(wxT("version"), wxT("0.0.0.0"));
765 if (wxSscanf(verstr, wxT("%i.%i.%i.%i"), &v1, &v2, &v3, &v4) == 4)
766 version = v1*256*256*256+v2*256*256+v3*256+v4;
767 else
768 version = 0;
769 if (m_version == -1)
770 m_version = version;
771 if (m_version != version)
772 {
773 wxLogWarning("Resource files must have same version number.");
774 }
775
776 ProcessPlatformProperty(root);
777 PreprocessForIdRanges(root);
778 wxIdRangeManager::Get()->FinaliseRanges(root);
779
780 return doc.release();
781}
782
b272b6dc
RD
783wxXmlNode *wxXmlResource::DoFindResource(wxXmlNode *parent,
784 const wxString& name,
785 const wxString& classname,
23239d94 786 bool recursive) const
47793ab8 787{
47793ab8
VS
788 wxXmlNode *node;
789
790 // first search for match at the top-level nodes (as this is
791 // where the resource is most commonly looked for):
792 for (node = parent->GetChildren(); node; node = node->GetNext())
793 {
23239d94 794 if ( IsObjectNode(node) && node->GetAttribute(wxS("name")) == name )
2b5f62a0 795 {
23239d94
VZ
796 // empty class name matches everything
797 if ( classname.empty() )
2b5f62a0 798 return node;
23239d94
VZ
799
800 wxString cls(node->GetAttribute(wxS("class")));
801
288b6107 802 // object_ref may not have 'class' attribute:
23239d94 803 if (cls.empty() && node->GetName() == wxS("object_ref"))
2b5f62a0 804 {
23239d94 805 wxString refName = node->GetAttribute(wxS("ref"));
2b5f62a0
VZ
806 if (refName.empty())
807 continue;
23239d94
VZ
808
809 const wxXmlNode * const refNode = GetResourceNode(refName);
810 if ( refNode )
811 cls = refNode->GetAttribute(wxS("class"));
2b5f62a0 812 }
23239d94
VZ
813
814 if ( cls == classname )
815 return node;
2b5f62a0 816 }
47793ab8
VS
817 }
818
23239d94 819 // then recurse in child nodes
47793ab8 820 if ( recursive )
23239d94 821 {
47793ab8
VS
822 for (node = parent->GetChildren(); node; node = node->GetNext())
823 {
23239d94 824 if ( IsObjectNode(node) )
47793ab8 825 {
f80ea77b 826 wxXmlNode* found = DoFindResource(node, name, classname, true);
47793ab8
VS
827 if ( found )
828 return found;
829 }
830 }
23239d94 831 }
47793ab8 832
23239d94 833 return NULL;
47793ab8 834}
78d14f80 835
b272b6dc 836wxXmlNode *wxXmlResource::FindResource(const wxString& name,
47793ab8
VS
837 const wxString& classname,
838 bool recursive)
78d14f80 839{
23239d94
VZ
840 wxString path;
841 wxXmlNode * const
842 node = GetResourceNodeAndLocation(name, classname, recursive, &path);
843
844 if ( !node )
845 {
819559b2
VS
846 ReportError
847 (
848 NULL,
849 wxString::Format
850 (
851 "XRC resource \"%s\" (class \"%s\") not found",
852 name, classname
853 )
854 );
23239d94
VZ
855 }
856#if wxUSE_FILESYSTEM
857 else // node was found
858 {
859 // ensure that relative paths work correctly when loading this node
860 // (which should happen as soon as we return as FindResource() result
861 // is always passed to CreateResFromNode())
862 m_curFileSystem.ChangePathTo(path);
863 }
864#endif // wxUSE_FILESYSTEM
865
866 return node;
867}
868
869wxXmlNode *
870wxXmlResource::GetResourceNodeAndLocation(const wxString& name,
871 const wxString& classname,
872 bool recursive,
873 wxString *path) const
874{
0526c8cc 875 // ensure everything is up-to-date: this is needed to support on-demand
23239d94
VZ
876 // reloading of XRC files
877 const_cast<wxXmlResource *>(this)->UpdateResources();
78d14f80 878
eb2d0d23
VS
879 for ( wxXmlResourceDataRecords::const_iterator f = Data().begin();
880 f != Data().end(); ++f )
78d14f80 881 {
23239d94
VZ
882 wxXmlResourceDataRecord *const rec = *f;
883 wxXmlDocument * const doc = rec->Doc;
884 if ( !doc || !doc->GetRoot() )
47793ab8
VS
885 continue;
886
23239d94
VZ
887 wxXmlNode * const
888 found = DoFindResource(doc->GetRoot(), name, classname, recursive);
47793ab8
VS
889 if ( found )
890 {
23239d94
VZ
891 if ( path )
892 *path = rec->File;
893
47793ab8
VS
894 return found;
895 }
78d14f80
VS
896 }
897
78d14f80
VS
898 return NULL;
899}
900
a1eeda0b
VS
901static void MergeNodesOver(wxXmlNode& dest, wxXmlNode& overwriteWith,
902 const wxString& overwriteFilename)
47793ab8 903{
288b6107 904 // Merge attributes:
a1eeda0b 905 for ( wxXmlAttribute *attr = overwriteWith.GetAttributes();
288b6107 906 attr; attr = attr->GetNext() )
47793ab8 907 {
288b6107
VS
908 wxXmlAttribute *dattr;
909 for (dattr = dest.GetAttributes(); dattr; dattr = dattr->GetNext())
47793ab8 910 {
b272b6dc 911
288b6107 912 if ( dattr->GetName() == attr->GetName() )
47793ab8 913 {
288b6107 914 dattr->SetValue(attr->GetValue());
47793ab8
VS
915 break;
916 }
917 }
78d14f80 918
288b6107
VS
919 if ( !dattr )
920 dest.AddAttribute(attr->GetName(), attr->GetValue());
47793ab8
VS
921 }
922
923 // Merge child nodes:
a1eeda0b 924 for (wxXmlNode* node = overwriteWith.GetChildren(); node; node = node->GetNext())
47793ab8 925 {
288b6107 926 wxString name = node->GetAttribute(wxT("name"), wxEmptyString);
47793ab8
VS
927 wxXmlNode *dnode;
928
929 for (dnode = dest.GetChildren(); dnode; dnode = dnode->GetNext() )
930 {
931 if ( dnode->GetName() == node->GetName() &&
288b6107 932 dnode->GetAttribute(wxT("name"), wxEmptyString) == name &&
47793ab8
VS
933 dnode->GetType() == node->GetType() )
934 {
a1eeda0b 935 MergeNodesOver(*dnode, *node, overwriteFilename);
47793ab8
VS
936 break;
937 }
938 }
939
940 if ( !dnode )
b26a650c 941 {
a1eeda0b
VS
942 wxXmlNode *copyOfNode = new wxXmlNode(*node);
943 // remember referenced object's file, see GetFileNameFromNode()
944 copyOfNode->AddAttribute(ATTR_INPUT_FILENAME, overwriteFilename);
945
b26a650c 946 static const wxChar *AT_END = wxT("end");
288b6107 947 wxString insert_pos = node->GetAttribute(wxT("insert_at"), AT_END);
b26a650c
VZ
948 if ( insert_pos == AT_END )
949 {
a1eeda0b 950 dest.AddChild(copyOfNode);
b26a650c
VZ
951 }
952 else if ( insert_pos == wxT("begin") )
953 {
a1eeda0b 954 dest.InsertChild(copyOfNode, dest.GetChildren());
b26a650c
VZ
955 }
956 }
47793ab8
VS
957 }
958
a1eeda0b
VS
959 if ( dest.GetType() == wxXML_TEXT_NODE && overwriteWith.GetContent().length() )
960 dest.SetContent(overwriteWith.GetContent());
47793ab8 961}
78d14f80 962
af0ac990
VZ
963wxObject *
964wxXmlResource::DoCreateResFromNode(wxXmlNode& node,
965 wxObject *parent,
966 wxObject *instance,
967 wxXmlResourceHandler *handlerToUse)
78d14f80 968{
47793ab8 969 // handling of referenced resource
af0ac990 970 if ( node.GetName() == wxT("object_ref") )
47793ab8 971 {
af0ac990 972 wxString refName = node.GetAttribute(wxT("ref"), wxEmptyString);
f80ea77b 973 wxXmlNode* refNode = FindResource(refName, wxEmptyString, true);
47793ab8
VS
974
975 if ( !refNode )
976 {
819559b2
VS
977 ReportError
978 (
af0ac990 979 &node,
819559b2
VS
980 wxString::Format
981 (
982 "referenced object node with ref=\"%s\" not found",
983 refName
984 )
985 );
47793ab8
VS
986 return NULL;
987 }
988
9445e542
VS
989 const bool hasOnlyRefAttr = node.GetAttributes() != NULL &&
990 node.GetAttributes()->GetNext() == NULL;
991
992 if ( hasOnlyRefAttr && !node.GetChildren() )
44108a07
VS
993 {
994 // In the typical, simple case, <object_ref> is used to link
995 // to another node and doesn't have any content of its own that
996 // would overwrite linked object's properties. In this case,
997 // we can simply create the resource from linked node.
998
af0ac990 999 return DoCreateResFromNode(*refNode, parent, instance);
44108a07
VS
1000 }
1001 else
1002 {
1003 // In the more complicated (but rare) case, <object_ref> has
1004 // subnodes that partially overwrite content of the referenced
1005 // object. In this case, we need to merge both XML trees and
1006 // load the resource from result of the merge.
1007
1008 wxXmlNode copy(*refNode);
af0ac990 1009 MergeNodesOver(copy, node, GetFileNameFromNode(&node, Data()));
a1eeda0b 1010
44108a07
VS
1011 // remember referenced object's file, see GetFileNameFromNode()
1012 copy.AddAttribute(ATTR_INPUT_FILENAME,
1013 GetFileNameFromNode(refNode, Data()));
47793ab8 1014
af0ac990 1015 return DoCreateResFromNode(copy, parent, instance);
44108a07 1016 }
47793ab8
VS
1017 }
1018
317a0d73 1019 if (handlerToUse)
b380439d 1020 {
af0ac990 1021 if (handlerToUse->CanHandle(&node))
8e8a4e85 1022 {
af0ac990 1023 return handlerToUse->CreateResource(&node, parent, instance);
8e8a4e85 1024 }
317a0d73 1025 }
af0ac990 1026 else if (node.GetName() == wxT("object"))
b380439d 1027 {
eb2d0d23
VS
1028 for ( wxVector<wxXmlResourceHandler*>::iterator h = m_handlers.begin();
1029 h != m_handlers.end(); ++h )
317a0d73 1030 {
eb2d0d23 1031 wxXmlResourceHandler *handler = *h;
af0ac990
VZ
1032 if (handler->CanHandle(&node))
1033 return handler->CreateResource(&node, parent, instance);
78d14f80 1034 }
78d14f80
VS
1035 }
1036
819559b2
VS
1037 ReportError
1038 (
af0ac990 1039 &node,
819559b2
VS
1040 wxString::Format
1041 (
1042 "no handler found for XML node \"%s\" (class \"%s\")",
af0ac990
VZ
1043 node.GetName(),
1044 node.GetAttribute("class", wxEmptyString)
819559b2
VS
1045 )
1046 );
78d14f80
VS
1047 return NULL;
1048}
1049
0526c8cc
VZ
1050wxIdRange::wxIdRange(const wxXmlNode* node,
1051 const wxString& rname,
1052 const wxString& startno,
1053 const wxString& rsize)
1054 : m_name(rname),
1055 m_start(0),
1056 m_size(0),
1057 m_item_end_found(0),
1058 m_finalised(0)
1059{
1060 long l;
1061 if ( startno.ToLong(&l) )
1062 {
1063 if ( l >= 0 )
1064 {
1065 m_start = l;
1066 }
1067 else
1068 {
1069 wxXmlResource::Get()->ReportError
1070 (
1071 node,
1072 "a negative id-range start parameter was given"
1073 );
1074 }
1075 }
1076 else
1077 {
1078 wxXmlResource::Get()->ReportError
1079 (
1080 node,
1081 "the id-range start parameter was malformed"
1082 );
1083 }
1084
1085 unsigned long ul;
1086 if ( rsize.ToULong(&ul) )
1087 {
1088 m_size = ul;
1089 }
1090 else
1091 {
1092 wxXmlResource::Get()->ReportError
1093 (
1094 node,
1095 "the id-range size parameter was malformed"
1096 );
1097 }
1098}
1099
1100void wxIdRange::NoteItem(const wxXmlNode* node, const wxString& item)
1101{
1102 // Nothing gets added here, but the existence of each item is noted
1103 // thus getting an accurate count. 'item' will be either an integer e.g.
1104 // [0] [123]: will eventually create an XRCID as start+integer or [start]
1105 // or [end] which are synonyms for [0] or [range_size-1] respectively.
1106 wxString content(item.Mid(1, item.length()-2));
1107
1108 // Check that basename+item wasn't foo[]
1109 if (content.empty())
1110 {
1111 wxXmlResource::Get()->ReportError(node, "an empty id-range item found");
1112 return;
1113 }
1114
1115 if (content=="start")
1116 {
1117 // "start" means [0], so store that in the set
1118 if (m_indices.count(0) == 0)
1119 {
1120 m_indices.insert(0);
1121 }
1122 else
1123 {
1124 wxXmlResource::Get()->ReportError
1125 (
1126 node,
1127 "duplicate id-range item found"
1128 );
1129 }
1130 }
1131 else if (content=="end")
1132 {
1133 // We can't yet be certain which XRCID this will be equivalent to, so
1134 // just note that there's an item with this name, in case we need to
1135 // inc the range size
1136 m_item_end_found = true;
1137 }
1138 else
1139 {
1140 // Anything else will be an integer, or rubbish
1141 unsigned long l;
1142 if ( content.ToULong(&l) )
1143 {
1144 if (m_indices.count(l) == 0)
1145 {
1146 m_indices.insert(l);
1147 // Check that this item wouldn't fall outside the current range
1148 // extent
1149 if (l >= m_size)
1150 {
1151 m_size = l + 1;
1152 }
1153 }
1154 else
1155 {
1156 wxXmlResource::Get()->ReportError
1157 (
1158 node,
1159 "duplicate id-range item found"
1160 );
1161 }
1162
1163 }
1164 else
1165 {
1166 wxXmlResource::Get()->ReportError
1167 (
1168 node,
1169 "an id-range item had a malformed index"
1170 );
1171 }
1172 }
1173}
1174
1175void wxIdRange::Finalise(const wxXmlNode* node)
1176{
1177 wxCHECK_RET( !IsFinalised(),
1178 "Trying to finalise an already-finalised range" );
1179
1180 // Now we know about all the items, we can get an accurate range size
1181 // Expand any requested range-size if there were more items than would fit
1182 m_size = wxMax(m_size, m_indices.size());
1183
1184 // If an item is explicitly called foo[end], ensure it won't clash with
1185 // another item
1186 if ( m_item_end_found && m_indices.count(m_size-1) )
1187 ++m_size;
1188 if (m_size == 0)
1189 {
1190 // This will happen if someone creates a range but no items in this xrc
1191 // file Report the error and abort, but don't finalise, in case items
1192 // appear later
1193 wxXmlResource::Get()->ReportError
1194 (
1195 node,
1196 "trying to create an empty id-range"
1197 );
1198 return;
1199 }
1200
1201 if (m_start==0)
1202 {
1203 // This is the usual case, where the user didn't specify a start ID
1204 // So get the range using NewControlId().
1205 //
1206 // NB: negative numbers, but NewControlId already returns the most
1207 // negative
1208 m_start = wxWindow::NewControlId(m_size);
1209 wxCHECK_RET( m_start != wxID_NONE,
1210 "insufficient IDs available to create range" );
1211 m_end = m_start + m_size - 1;
1212 }
1213 else
1214 {
1215 // The user already specified a start value, which must be positive
1216 m_end = m_start + m_size - 1;
1217 }
1218
1219 // Create the XRCIDs
1220 for (int i=m_start; i <= m_end; ++i)
1221 {
d265ec6b
VZ
1222 // Ensure that we overwrite any existing value as otherwise
1223 // wxXmlResource::Unload() followed by Load() wouldn't work correctly.
1224 XRCID_Assign(m_name + wxString::Format("[%i]", i-m_start), i);
0526c8cc 1225
e7cad4b7
VZ
1226 wxLogTrace("xrcrange",
1227 "integer = %i %s now returns %i",
1228 i,
1229 m_name + wxString::Format("[%i]", i-m_start),
1230 XRCID((m_name + wxString::Format("[%i]", i-m_start)).mb_str()));
0526c8cc
VZ
1231 }
1232 // and these special ones
d265ec6b
VZ
1233 XRCID_Assign(m_name + "[start]", m_start);
1234 XRCID_Assign(m_name + "[end]", m_end);
0526c8cc
VZ
1235 wxLogTrace("xrcrange","%s[start] = %i %s[end] = %i",
1236 m_name.mb_str(),XRCID(wxString(m_name+"[start]").mb_str()),
1237 m_name.mb_str(),XRCID(wxString(m_name+"[end]").mb_str()));
1238
1239 m_finalised = true;
1240}
1241
1242wxIdRangeManager *wxIdRangeManager::ms_instance = NULL;
1243
1244/*static*/ wxIdRangeManager *wxIdRangeManager::Get()
1245{
1246 if ( !ms_instance )
1247 ms_instance = new wxIdRangeManager;
1248 return ms_instance;
1249}
1250
1251/*static*/ wxIdRangeManager *wxIdRangeManager::Set(wxIdRangeManager *res)
1252{
1253 wxIdRangeManager *old = ms_instance;
1254 ms_instance = res;
1255 return old;
1256}
1257
1258wxIdRangeManager::~wxIdRangeManager()
1259{
1260 for ( wxVector<wxIdRange*>::iterator i = m_IdRanges.begin();
1261 i != m_IdRanges.end(); ++i )
1262 {
1263 delete *i;
1264 }
1265 m_IdRanges.clear();
1266
1267 delete ms_instance;
1268}
1269
1270void wxIdRangeManager::AddRange(const wxXmlNode* node)
1271{
1272 wxString name = node->GetAttribute("name");
1273 wxString start = node->GetAttribute("start", "0");
1274 wxString size = node->GetAttribute("size", "0");
1275 if (name.empty())
1276 {
1277 wxXmlResource::Get()->ReportError
1278 (
1279 node,
1280 "xrc file contains an id-range without a name"
1281 );
1282 return;
1283 }
1284
1285 int index = Find(name);
1286 if (index == wxNOT_FOUND)
1287 {
1288 wxLogTrace("xrcrange",
1289 "Adding ID range, name=%s start=%s size=%s",
1290 name, start, size);
1291
1292 m_IdRanges.push_back(new wxIdRange(node, name, start, size));
1293 }
1294 else
1295 {
1296 // There was already a range with this name. Let's hope this is
1297 // from an Unload()/(re)Load(), not an unintentional duplication
1298 wxLogTrace("xrcrange",
1299 "Replacing ID range, name=%s start=%s size=%s",
1300 name, start, size);
1301
1302 wxIdRange* oldrange = m_IdRanges.at(index);
1303 m_IdRanges.at(index) = new wxIdRange(node, name, start, size);
1304 delete oldrange;
1305 }
1306}
1307
1308wxIdRange *
1309wxIdRangeManager::FindRangeForItem(const wxXmlNode* node,
1310 const wxString& item,
1311 wxString& value) const
1312{
1313 wxString basename = item.BeforeFirst('[');
1314 wxCHECK_MSG( !basename.empty(), NULL,
1315 "an id-range item without a range name" );
1316
1317 int index = Find(basename);
1318 if (index == wxNOT_FOUND)
1319 {
1320 // Don't assert just because we've found an unexpected foo[123]
1321 // Someone might just want such a name, nothing to do with ranges
1322 return NULL;
1323 }
1324
1325 value = item.Mid(basename.Len());
1326 if (value.at(value.length()-1)==']')
1327 {
1328 return m_IdRanges.at(index);
1329 }
1330 wxXmlResource::Get()->ReportError(node, "a malformed id-range item");
1331 return NULL;
1332}
1333
1334void
1335wxIdRangeManager::NotifyRangeOfItem(const wxXmlNode* node,
1336 const wxString& item) const
1337{
1338 wxString value;
1339 wxIdRange* range = FindRangeForItem(node, item, value);
1340 if (range)
1341 range->NoteItem(node, value);
1342}
1343
1344int wxIdRangeManager::Find(const wxString& rangename) const
1345{
1346 for ( int i=0; i < (int)m_IdRanges.size(); ++i )
1347 {
1348 if (m_IdRanges.at(i)->GetName() == rangename)
1349 return i;
1350 }
1351
1352 return wxNOT_FOUND;
1353}
1354
1355void wxIdRangeManager::FinaliseRanges(const wxXmlNode* node) const
1356{
1357 for ( wxVector<wxIdRange*>::const_iterator i = m_IdRanges.begin();
1358 i != m_IdRanges.end(); ++i )
1359 {
1360 // Check if this range has already been finalised. Quite possible,
1361 // as FinaliseRanges() gets called for each .xrc file loaded
1362 if (!(*i)->IsFinalised())
1363 {
1364 wxLogTrace("xrcrange", "Finalising ID range %s", (*i)->GetName());
1365 (*i)->Finalise(node);
1366 }
1367 }
1368}
1369
78d14f80 1370
eb2d0d23
VS
1371class wxXmlSubclassFactories : public wxVector<wxXmlSubclassFactory*>
1372{
1373 // this is a class so that it can be forward-declared
1374};
2b5f62a0 1375
eb2d0d23 1376wxXmlSubclassFactories *wxXmlResource::ms_subclassFactories = NULL;
2b5f62a0
VZ
1377
1378/*static*/ void wxXmlResource::AddSubclassFactory(wxXmlSubclassFactory *factory)
1379{
1380 if (!ms_subclassFactories)
1381 {
eb2d0d23 1382 ms_subclassFactories = new wxXmlSubclassFactories;
2b5f62a0 1383 }
eb2d0d23 1384 ms_subclassFactories->push_back(factory);
2b5f62a0
VZ
1385}
1386
1387class wxXmlSubclassFactoryCXX : public wxXmlSubclassFactory
1388{
1389public:
1390 ~wxXmlSubclassFactoryCXX() {}
1391
1392 wxObject *Create(const wxString& className)
1393 {
1394 wxClassInfo* classInfo = wxClassInfo::FindClass(className);
1395
1396 if (classInfo)
1397 return classInfo->CreateObject();
1398 else
1399 return NULL;
1400 }
1401};
1402
1403
1404
78d14f80 1405
a3b9c43b
VZ
1406wxXmlResourceHandlerImpl::wxXmlResourceHandlerImpl(wxXmlResourceHandler *handler)
1407 :wxXmlResourceHandlerImplBase(handler)
1408{
1409}
78d14f80 1410
a3b9c43b
VZ
1411wxObject *wxXmlResourceHandlerImpl::CreateResFromNode(wxXmlNode *node,
1412 wxObject *parent, wxObject *instance)
1413{
1414 return m_handler->m_resource->CreateResFromNode(node, parent, instance);
1415}
1416
1417#if wxUSE_FILESYSTEM
1418wxFileSystem& wxXmlResourceHandlerImpl::GetCurFileSystem()
1419{
1420 return m_handler->m_resource->GetCurFileSystem();
1421}
1422#endif
78d14f80
VS
1423
1424
a3b9c43b 1425wxObject *wxXmlResourceHandlerImpl::CreateResource(wxXmlNode *node, wxObject *parent, wxObject *instance)
78d14f80 1426{
a3b9c43b
VZ
1427 wxXmlNode *myNode = m_handler->m_node;
1428 wxString myClass = m_handler->m_class;
1429 wxObject *myParent = m_handler->m_parent, *myInstance = m_handler->m_instance;
1430 wxWindow *myParentAW = m_handler->m_parentAsWindow;
78d14f80 1431
a3b9c43b
VZ
1432 m_handler->m_instance = instance;
1433 if (!m_handler->m_instance && node->HasAttribute(wxT("subclass")) &&
1434 !(m_handler->m_resource->GetFlags() & wxXRC_NO_SUBCLASSING))
daa85ee3 1435 {
288b6107 1436 wxString subclass = node->GetAttribute(wxT("subclass"), wxEmptyString);
2b5f62a0 1437 if (!subclass.empty())
daa85ee3 1438 {
eb2d0d23
VS
1439 for (wxXmlSubclassFactories::iterator i = wxXmlResource::ms_subclassFactories->begin();
1440 i != wxXmlResource::ms_subclassFactories->end(); ++i)
2b5f62a0 1441 {
a3b9c43b
VZ
1442 m_handler->m_instance = (*i)->Create(subclass);
1443 if (m_handler->m_instance)
2b5f62a0
VZ
1444 break;
1445 }
daa85ee3 1446
a3b9c43b 1447 if (!m_handler->m_instance)
2b5f62a0 1448 {
288b6107 1449 wxString name = node->GetAttribute(wxT("name"), wxEmptyString);
819559b2
VS
1450 ReportError
1451 (
1452 node,
1453 wxString::Format
1454 (
1455 "subclass \"%s\" not found for resource \"%s\", not subclassing",
1456 subclass, name
1457 )
1458 );
2b5f62a0
VZ
1459 }
1460 }
daa85ee3
VS
1461 }
1462
a3b9c43b
VZ
1463 m_handler->m_node = node;
1464 m_handler->m_class = node->GetAttribute(wxT("class"), wxEmptyString);
1465 m_handler->m_parent = parent;
1466 m_handler->m_parentAsWindow = wxDynamicCast(m_handler->m_parent, wxWindow);
78d14f80 1467
a3b9c43b 1468 wxObject *returned = GetHandler()->DoCreateResource();
78d14f80 1469
a3b9c43b
VZ
1470 m_handler->m_node = myNode;
1471 m_handler->m_class = myClass;
1472 m_handler->m_parent = myParent; m_handler->m_parentAsWindow = myParentAW;
1473 m_handler->m_instance = myInstance;
78d14f80
VS
1474
1475 return returned;
1476}
1477
a3b9c43b 1478bool wxXmlResourceHandlerImpl::HasParam(const wxString& param)
78d14f80
VS
1479{
1480 return (GetParamNode(param) != NULL);
1481}
1482
1483
a3b9c43b 1484int wxXmlResourceHandlerImpl::GetStyle(const wxString& param, int defaults)
78d14f80
VS
1485{
1486 wxString s = GetParamValue(param);
1487
1488 if (!s) return defaults;
1489
2b5f62a0 1490 wxStringTokenizer tkn(s, wxT("| \t\n"), wxTOKEN_STRTOK);
78d14f80
VS
1491 int style = 0;
1492 int index;
1493 wxString fl;
1494 while (tkn.HasMoreTokens())
1495 {
1496 fl = tkn.GetNextToken();
a3b9c43b 1497 index = m_handler->m_styleNames.Index(fl);
78d14f80 1498 if (index != wxNOT_FOUND)
819559b2 1499 {
a3b9c43b 1500 style |= m_handler->m_styleValues[index];
819559b2 1501 }
78d14f80 1502 else
819559b2
VS
1503 {
1504 ReportParamError
1505 (
1506 param,
1507 wxString::Format("unknown style flag \"%s\"", fl)
1508 );
1509 }
78d14f80
VS
1510 }
1511 return style;
1512}
1513
1514
1515
a3b9c43b 1516wxString wxXmlResourceHandlerImpl::GetText(const wxString& param, bool translate)
78d14f80 1517{
7b56015f
VS
1518 wxXmlNode *parNode = GetParamNode(param);
1519 wxString str1(GetNodeContent(parNode));
78d14f80 1520 wxString str2;
424af7aa
VS
1521
1522 // "\\" wasn't translated to "\" prior to 2.5.3.0:
a3b9c43b 1523 const bool escapeBackslash = (m_handler->m_resource->CompareVersion(2,5,3,0) >= 0);
78d14f80 1524
b272b6dc
RD
1525 // VS: First version of XRC resources used $ instead of & (which is
1526 // illegal in XML), but later I realized that '_' fits this purpose
718cf160 1527 // much better (because &File means "File with F underlined").
a3b9c43b 1528 const wxChar amp_char = (m_handler->m_resource->CompareVersion(2,3,0,1) < 0)
424af7aa 1529 ? '$' : '_';
78d14f80 1530
424af7aa 1531 for ( wxString::const_iterator dt = str1.begin(); dt != str1.end(); ++dt )
78d14f80
VS
1532 {
1533 // Remap amp_char to &, map double amp_char to amp_char (for things
1534 // like "&File..." -- this is illegal in XML, so we use "_File..."):
424af7aa 1535 if ( *dt == amp_char )
78d14f80 1536 {
2148394a 1537 if ( dt+1 == str1.end() || *(++dt) == amp_char )
78d14f80
VS
1538 str2 << amp_char;
1539 else
1540 str2 << wxT('&') << *dt;
1541 }
984c33c9 1542 // Remap \n to CR, \r to LF, \t to TAB, \\ to \:
424af7aa
VS
1543 else if ( *dt == wxT('\\') )
1544 {
1545 switch ( (*(++dt)).GetValue() )
78d14f80 1546 {
984c33c9
VS
1547 case wxT('n'):
1548 str2 << wxT('\n');
1549 break;
e7a3a5a5 1550
984c33c9
VS
1551 case wxT('t'):
1552 str2 << wxT('\t');
1553 break;
e7a3a5a5 1554
984c33c9
VS
1555 case wxT('r'):
1556 str2 << wxT('\r');
1557 break;
1558
1559 case wxT('\\') :
1560 // "\\" wasn't translated to "\" prior to 2.5.3.0:
424af7aa 1561 if ( escapeBackslash )
984c33c9
VS
1562 {
1563 str2 << wxT('\\');
1564 break;
1565 }
1566 // else fall-through to default: branch below
e7a3a5a5 1567
984c33c9
VS
1568 default:
1569 str2 << wxT('\\') << *dt;
1570 break;
78d14f80 1571 }
424af7aa
VS
1572 }
1573 else
1574 {
1575 str2 << *dt;
1576 }
78d14f80 1577 }
b272b6dc 1578
a3b9c43b 1579 if (m_handler->m_resource->GetFlags() & wxXRC_USE_LOCALE)
7b56015f
VS
1580 {
1581 if (translate && parNode &&
288b6107 1582 parNode->GetAttribute(wxT("translate"), wxEmptyString) != wxT("0"))
7b56015f 1583 {
a3b9c43b 1584 return wxGetTranslation(str2, m_handler->m_resource->GetDomain());
7b56015f
VS
1585 }
1586 else
1587 {
1588#if wxUSE_UNICODE
1589 return str2;
1590#else
1591 // The string is internally stored as UTF-8, we have to convert
1592 // it into system's default encoding so that it can be displayed:
6251e0ea 1593 return wxString(str2.wc_str(wxConvUTF8), wxConvLocal);
7b56015f
VS
1594#endif
1595 }
1596 }
8516a98b
DS
1597
1598 // If wxXRC_USE_LOCALE is not set, then the string is already in
1599 // system's default encoding in ANSI build, so we don't have to
1600 // do anything special here.
1601 return str2;
78d14f80
VS
1602}
1603
1604
1605
a3b9c43b 1606long wxXmlResourceHandlerImpl::GetLong(const wxString& param, long defaultv)
78d14f80 1607{
9f5103f1 1608 long value = defaultv;
78d14f80
VS
1609 wxString str1 = GetParamValue(param);
1610
9f5103f1
VZ
1611 if (!str1.empty())
1612 {
1613 if (!str1.ToLong(&value))
1614 {
1615 ReportParamError
1616 (
1617 param,
1618 wxString::Format("invalid long specification \"%s\"", str1)
1619 );
1620 }
1621 }
78d14f80
VS
1622
1623 return value;
1624}
e7a3a5a5 1625
a3b9c43b 1626float wxXmlResourceHandlerImpl::GetFloat(const wxString& param, float defaultv)
1df61962 1627{
1d9473d3 1628 wxString str = GetParamValue(param);
1df61962 1629
9edb8fa0
VZ
1630 // strings in XRC always use C locale so make sure to use the
1631 // locale-independent wxString::ToCDouble() and not ToDouble() which uses
1632 // the current locale with a potentially different decimal point character
9f5103f1
VZ
1633 double value = defaultv;
1634 if (!str.empty())
1635 {
1636 if (!str.ToCDouble(&value))
1637 {
1638 ReportParamError
1639 (
1640 param,
1641 wxString::Format("invalid float specification \"%s\"", str)
1642 );
1643 }
1644 }
1df61962 1645
17a1ebd1 1646 return wx_truncate_cast(float, value);
1df61962 1647}
78d14f80 1648
af1337b0 1649
a3b9c43b 1650int wxXmlResourceHandlerImpl::GetID()
78d14f80 1651{
13de23f6 1652 return wxXmlResource::GetXRCID(GetName());
78d14f80
VS
1653}
1654
1655
af1337b0 1656
a3b9c43b 1657wxString wxXmlResourceHandlerImpl::GetName()
78d14f80 1658{
a3b9c43b 1659 return m_handler->m_node->GetAttribute(wxT("name"), wxT("-1"));
78d14f80
VS
1660}
1661
1662
1663
a3b9c43b 1664bool wxXmlResourceHandlerImpl::GetBoolAttr(const wxString& attr, bool defaultv)
8758875e
VZ
1665{
1666 wxString v;
a3b9c43b 1667 return m_handler->m_node->GetAttribute(attr, &v) ? v == '1' : defaultv;
8758875e
VZ
1668}
1669
a3b9c43b 1670bool wxXmlResourceHandlerImpl::GetBool(const wxString& param, bool defaultv)
78d14f80 1671{
8758875e 1672 const wxString v = GetParamValue(param);
8516a98b 1673
8758875e 1674 return v.empty() ? defaultv : (v == '1');
78d14f80
VS
1675}
1676
1677
1df61962
VS
1678static wxColour GetSystemColour(const wxString& name)
1679{
1680 if (!name.empty())
1681 {
1682 #define SYSCLR(clr) \
9a83f860 1683 if (name == wxT(#clr)) return wxSystemSettings::GetColour(clr);
1df61962
VS
1684 SYSCLR(wxSYS_COLOUR_SCROLLBAR)
1685 SYSCLR(wxSYS_COLOUR_BACKGROUND)
1686 SYSCLR(wxSYS_COLOUR_DESKTOP)
1687 SYSCLR(wxSYS_COLOUR_ACTIVECAPTION)
1688 SYSCLR(wxSYS_COLOUR_INACTIVECAPTION)
1689 SYSCLR(wxSYS_COLOUR_MENU)
1690 SYSCLR(wxSYS_COLOUR_WINDOW)
1691 SYSCLR(wxSYS_COLOUR_WINDOWFRAME)
1692 SYSCLR(wxSYS_COLOUR_MENUTEXT)
1693 SYSCLR(wxSYS_COLOUR_WINDOWTEXT)
1694 SYSCLR(wxSYS_COLOUR_CAPTIONTEXT)
1695 SYSCLR(wxSYS_COLOUR_ACTIVEBORDER)
1696 SYSCLR(wxSYS_COLOUR_INACTIVEBORDER)
1697 SYSCLR(wxSYS_COLOUR_APPWORKSPACE)
1698 SYSCLR(wxSYS_COLOUR_HIGHLIGHT)
1699 SYSCLR(wxSYS_COLOUR_HIGHLIGHTTEXT)
1700 SYSCLR(wxSYS_COLOUR_BTNFACE)
1701 SYSCLR(wxSYS_COLOUR_3DFACE)
1702 SYSCLR(wxSYS_COLOUR_BTNSHADOW)
1703 SYSCLR(wxSYS_COLOUR_3DSHADOW)
1704 SYSCLR(wxSYS_COLOUR_GRAYTEXT)
1705 SYSCLR(wxSYS_COLOUR_BTNTEXT)
1706 SYSCLR(wxSYS_COLOUR_INACTIVECAPTIONTEXT)
1707 SYSCLR(wxSYS_COLOUR_BTNHIGHLIGHT)
1708 SYSCLR(wxSYS_COLOUR_BTNHILIGHT)
1709 SYSCLR(wxSYS_COLOUR_3DHIGHLIGHT)
1710 SYSCLR(wxSYS_COLOUR_3DHILIGHT)
1711 SYSCLR(wxSYS_COLOUR_3DDKSHADOW)
1712 SYSCLR(wxSYS_COLOUR_3DLIGHT)
1713 SYSCLR(wxSYS_COLOUR_INFOTEXT)
1714 SYSCLR(wxSYS_COLOUR_INFOBK)
1715 SYSCLR(wxSYS_COLOUR_LISTBOX)
1716 SYSCLR(wxSYS_COLOUR_HOTLIGHT)
1717 SYSCLR(wxSYS_COLOUR_GRADIENTACTIVECAPTION)
1718 SYSCLR(wxSYS_COLOUR_GRADIENTINACTIVECAPTION)
1719 SYSCLR(wxSYS_COLOUR_MENUHILIGHT)
1720 SYSCLR(wxSYS_COLOUR_MENUBAR)
1721 #undef SYSCLR
1722 }
1723
1724 return wxNullColour;
1725}
78d14f80 1726
a3b9c43b 1727wxColour wxXmlResourceHandlerImpl::GetColour(const wxString& param, const wxColour& defaultv)
78d14f80
VS
1728{
1729 wxString v = GetParamValue(param);
984f1d84
VS
1730
1731 if ( v.empty() )
1732 return defaultv;
1733
68b4e4cf 1734 wxColour clr;
1df61962 1735
68b4e4cf
WS
1736 // wxString -> wxColour conversion
1737 if (!clr.Set(v))
78d14f80 1738 {
1df61962
VS
1739 // the colour doesn't use #RRGGBB format, check if it is symbolic
1740 // colour name:
68b4e4cf 1741 clr = GetSystemColour(v);
a1b806b9 1742 if (clr.IsOk())
1df61962 1743 return clr;
e7a3a5a5 1744
819559b2
VS
1745 ReportParamError
1746 (
1747 param,
1748 wxString::Format("incorrect colour specification \"%s\"", v)
1749 );
78d14f80
VS
1750 return wxNullColour;
1751 }
1752
68b4e4cf 1753 return clr;
78d14f80
VS
1754}
1755
1c60f644
VS
1756namespace
1757{
1758
1759// if 'param' has stock_id/stock_client, extracts them and returns true
1760bool GetStockArtAttrs(const wxXmlNode *paramNode,
1761 const wxString& defaultArtClient,
1762 wxString& art_id, wxString& art_client)
1763{
1764 if ( paramNode )
1765 {
1766 art_id = paramNode->GetAttribute("stock_id", "");
1767
1768 if ( !art_id.empty() )
1769 {
1770 art_id = wxART_MAKE_ART_ID_FROM_STR(art_id);
1771
1772 art_client = paramNode->GetAttribute("stock_client", "");
1773 if ( art_client.empty() )
1774 art_client = defaultArtClient;
1775 else
1776 art_client = wxART_MAKE_CLIENT_ID_FROM_STR(art_client);
78d14f80 1777
1c60f644
VS
1778 return true;
1779 }
1780 }
1781
1782 return false;
1783}
1784
1785} // anonymous namespace
78d14f80 1786
a3b9c43b 1787wxBitmap wxXmlResourceHandlerImpl::GetBitmap(const wxString& param,
db59a97c
VS
1788 const wxArtClient& defaultArtClient,
1789 wxSize size)
326462ae 1790{
9c1d2aa2
VZ
1791 // it used to be possible to pass an empty string here to indicate that the
1792 // bitmap name should be read from this node itself but this is not
1793 // supported any more because GetBitmap(m_node) can be used directly
1794 // instead
1795 wxASSERT_MSG( !param.empty(), "bitmap parameter name can't be empty" );
1796
1797 const wxXmlNode* const node = GetParamNode(param);
1798
1799 if ( !node )
1800 {
1801 // this is not an error as bitmap parameter could be optional
1802 return wxNullBitmap;
1803 }
1804
1805 return GetBitmap(node, defaultArtClient, size);
326462ae
VZ
1806}
1807
a3b9c43b 1808wxBitmap wxXmlResourceHandlerImpl::GetBitmap(const wxXmlNode* node,
326462ae
VZ
1809 const wxArtClient& defaultArtClient,
1810 wxSize size)
78d14f80 1811{
9c1d2aa2
VZ
1812 wxCHECK_MSG( node, wxNullBitmap, "bitmap node can't be NULL" );
1813
db59a97c 1814 /* If the bitmap is specified as stock item, query wxArtProvider for it: */
1c60f644 1815 wxString art_id, art_client;
326462ae 1816 if ( GetStockArtAttrs(node, defaultArtClient,
1c60f644 1817 art_id, art_client) )
af1337b0 1818 {
1c60f644 1819 wxBitmap stockArt(wxArtProvider::GetBitmap(art_id, art_client, size));
a1b806b9 1820 if ( stockArt.IsOk() )
1c60f644 1821 return stockArt;
af1337b0
JS
1822 }
1823
92e898b0 1824 /* ...or load the bitmap from file: */
326462ae 1825 wxString name = GetParamValue(node);
e7a3a5a5 1826 if (name.empty()) return wxNullBitmap;
78d14f80 1827#if wxUSE_FILESYSTEM
4532786e 1828 wxFSFile *fsfile = GetCurFileSystem().OpenFile(name, wxFS_READ | wxFS_SEEKABLE);
78d14f80
VS
1829 if (fsfile == NULL)
1830 {
819559b2
VS
1831 ReportParamError
1832 (
326462ae 1833 node->GetName(),
819559b2
VS
1834 wxString::Format("cannot open bitmap resource \"%s\"", name)
1835 );
78d14f80
VS
1836 return wxNullBitmap;
1837 }
1838 wxImage img(*(fsfile->GetStream()));
1839 delete fsfile;
1840#else
45f3249b 1841 wxImage img(name);
78d14f80 1842#endif
af1337b0 1843
a1b806b9 1844 if (!img.IsOk())
78d14f80 1845 {
819559b2
VS
1846 ReportParamError
1847 (
326462ae 1848 node->GetName(),
819559b2
VS
1849 wxString::Format("cannot create bitmap from \"%s\"", name)
1850 );
78d14f80
VS
1851 return wxNullBitmap;
1852 }
1853 if (!(size == wxDefaultSize)) img.Rescale(size.x, size.y);
b272b6dc 1854 return wxBitmap(img);
78d14f80
VS
1855}
1856
78d14f80 1857
a3b9c43b 1858wxIcon wxXmlResourceHandlerImpl::GetIcon(const wxString& param,
db59a97c
VS
1859 const wxArtClient& defaultArtClient,
1860 wxSize size)
326462ae 1861{
9c1d2aa2
VZ
1862 // see comment in GetBitmap(wxString) overload
1863 wxASSERT_MSG( !param.empty(), "icon parameter name can't be empty" );
1864
1865 const wxXmlNode* const node = GetParamNode(param);
1866
1867 if ( !node )
1868 {
1869 // this is not an error as icon parameter could be optional
1870 return wxIcon();
1871 }
1872
1873 return GetIcon(node, defaultArtClient, size);
326462ae
VZ
1874}
1875
a3b9c43b 1876wxIcon wxXmlResourceHandlerImpl::GetIcon(const wxXmlNode* node,
326462ae
VZ
1877 const wxArtClient& defaultArtClient,
1878 wxSize size)
78d14f80 1879{
78d14f80 1880 wxIcon icon;
326462ae 1881 icon.CopyFromBitmap(GetBitmap(node, defaultArtClient, size));
78d14f80
VS
1882 return icon;
1883}
1884
326462ae 1885
a3b9c43b 1886wxIconBundle wxXmlResourceHandlerImpl::GetIconBundle(const wxString& param,
1c60f644
VS
1887 const wxArtClient& defaultArtClient)
1888{
1889 wxString art_id, art_client;
1890 if ( GetStockArtAttrs(GetParamNode(param), defaultArtClient,
1891 art_id, art_client) )
1892 {
1893 wxIconBundle stockArt(wxArtProvider::GetIconBundle(art_id, art_client));
1894 if ( stockArt.IsOk() )
1895 return stockArt;
1896 }
1897
1898 const wxString name = GetParamValue(param);
1899 if ( name.empty() )
1900 return wxNullIconBundle;
1901
1902#if wxUSE_FILESYSTEM
1903 wxFSFile *fsfile = GetCurFileSystem().OpenFile(name, wxFS_READ | wxFS_SEEKABLE);
1904 if ( fsfile == NULL )
1905 {
1906 ReportParamError
1907 (
1908 param,
1909 wxString::Format("cannot open icon resource \"%s\"", name)
1910 );
1911 return wxNullIconBundle;
1912 }
1913
1914 wxIconBundle bundle(*(fsfile->GetStream()));
1915 delete fsfile;
1916#else
1917 wxIconBundle bundle(name);
1918#endif
1919
1920 if ( !bundle.IsOk() )
1921 {
1922 ReportParamError
1923 (
1924 param,
1925 wxString::Format("cannot create icon from \"%s\"", name)
1926 );
1927 return wxNullIconBundle;
1928 }
1929
1930 return bundle;
1931}
78d14f80
VS
1932
1933
a3b9c43b 1934wxImageList *wxXmlResourceHandlerImpl::GetImageList(const wxString& param)
326462ae
VZ
1935{
1936 wxXmlNode * const imagelist_node = GetParamNode(param);
1937 if ( !imagelist_node )
1938 return NULL;
1939
a3b9c43b
VZ
1940 wxXmlNode * const oldnode = m_handler->m_node;
1941 m_handler->m_node = imagelist_node;
326462ae 1942
fe97acf0
VZ
1943 // Get the size if we have it, otherwise we will use the size of the first
1944 // list element.
326462ae 1945 wxSize size = GetSize();
326462ae 1946
fe97acf0
VZ
1947 // Start adding images, we'll create the image list when adding the first
1948 // one.
1949 wxImageList * imagelist = NULL;
326462ae
VZ
1950 wxString parambitmap = wxT("bitmap");
1951 if ( HasParam(parambitmap) )
1952 {
a3b9c43b 1953 wxXmlNode *n = m_handler->m_node->GetChildren();
326462ae
VZ
1954 while (n)
1955 {
1956 if (n->GetType() == wxXML_ELEMENT_NODE && n->GetName() == parambitmap)
1957 {
0915bc4d 1958 wxIcon icon = GetIcon(n, wxART_OTHER, size);
fe97acf0
VZ
1959 if ( !imagelist )
1960 {
1961 // We need the real image list size to create it.
1962 if ( size == wxDefaultSize )
1963 size = icon.GetSize();
1964
1965 // We use the mask by default.
1966 bool mask = !HasParam(wxS("mask")) || GetBool(wxS("mask"));
1967
1968 imagelist = new wxImageList(size.x, size.y, mask);
1969 }
1970
326462ae 1971 // add icon instead of bitmap to keep the bitmap mask
fe97acf0 1972 imagelist->Add(icon);
326462ae
VZ
1973 }
1974 n = n->GetNext();
1975 }
1976 }
1977
a3b9c43b 1978 m_handler->m_node = oldnode;
326462ae
VZ
1979 return imagelist;
1980}
1981
a3b9c43b 1982wxXmlNode *wxXmlResourceHandlerImpl::GetParamNode(const wxString& param)
78d14f80 1983{
a3b9c43b 1984 wxCHECK_MSG(m_handler->m_node, NULL, wxT("You can't access handler data before it was initialized!"));
2b5f62a0 1985
a3b9c43b 1986 wxXmlNode *n = m_handler->m_node->GetChildren();
78d14f80
VS
1987
1988 while (n)
1989 {
1990 if (n->GetType() == wxXML_ELEMENT_NODE && n->GetName() == param)
cffff062
VZ
1991 {
1992 // TODO: check that there are no other properties/parameters with
1993 // the same name and log an error if there are (can't do this
1994 // right now as I'm not sure if it's not going to break code
1995 // using this function in unintentional way (i.e. for
1996 // accessing other things than properties), for example
1997 // wxBitmapComboBoxXmlHandler almost surely does
78d14f80 1998 return n;
cffff062 1999 }
78d14f80
VS
2000 n = n->GetNext();
2001 }
2002 return NULL;
2003}
2004
a3b9c43b 2005bool wxXmlResourceHandlerImpl::IsOfClass(wxXmlNode *node, const wxString& classname) const
2d672c46 2006{
8ec22772 2007 return node->GetAttribute(wxT("class")) == classname;
2d672c46
MW
2008}
2009
2010
2011
a3b9c43b 2012wxString wxXmlResourceHandlerImpl::GetNodeContent(const wxXmlNode *node)
78d14f80 2013{
326462ae 2014 const wxXmlNode *n = node;
78d14f80
VS
2015 if (n == NULL) return wxEmptyString;
2016 n = n->GetChildren();
2017
2018 while (n)
2019 {
2020 if (n->GetType() == wxXML_TEXT_NODE ||
2021 n->GetType() == wxXML_CDATA_SECTION_NODE)
2022 return n->GetContent();
2023 n = n->GetNext();
2024 }
2025 return wxEmptyString;
2026}
2027
2028
2029
a3b9c43b 2030wxString wxXmlResourceHandlerImpl::GetParamValue(const wxString& param)
78d14f80 2031{
e7a3a5a5 2032 if (param.empty())
a3b9c43b 2033 return GetNodeContent(m_handler->m_node);
78d14f80
VS
2034 else
2035 return GetNodeContent(GetParamNode(param));
2036}
2037
a3b9c43b 2038wxString wxXmlResourceHandlerImpl::GetParamValue(const wxXmlNode* node)
326462ae
VZ
2039{
2040 return GetNodeContent(node);
2041}
78d14f80
VS
2042
2043
a3b9c43b 2044wxSize wxXmlResourceHandlerImpl::GetSize(const wxString& param,
0c00c86f 2045 wxWindow *windowToUse)
78d14f80
VS
2046{
2047 wxString s = GetParamValue(param);
e7a3a5a5 2048 if (s.empty()) s = wxT("-1,-1");
78d14f80 2049 bool is_dlg;
d1f47235 2050 long sx, sy = 0;
78d14f80 2051
88a7a4e1 2052 is_dlg = s[s.length()-1] == wxT('d');
78d14f80
VS
2053 if (is_dlg) s.RemoveLast();
2054
2055 if (!s.BeforeFirst(wxT(',')).ToLong(&sx) ||
2056 !s.AfterLast(wxT(',')).ToLong(&sy))
2057 {
819559b2
VS
2058 ReportParamError
2059 (
2060 param,
2061 wxString::Format("cannot parse coordinates value \"%s\"", s)
2062 );
78d14f80
VS
2063 return wxDefaultSize;
2064 }
2065
2066 if (is_dlg)
2067 {
0c00c86f
VS
2068 if (windowToUse)
2069 {
2070 return wxDLG_UNIT(windowToUse, wxSize(sx, sy));
2071 }
a3b9c43b 2072 else if (m_handler->m_parentAsWindow)
0c00c86f 2073 {
a3b9c43b 2074 return wxDLG_UNIT(m_handler->m_parentAsWindow, wxSize(sx, sy));
0c00c86f 2075 }
78d14f80
VS
2076 else
2077 {
819559b2
VS
2078 ReportParamError
2079 (
2080 param,
2081 "cannot convert dialog units: dialog unknown"
2082 );
78d14f80
VS
2083 return wxDefaultSize;
2084 }
2085 }
8516a98b
DS
2086
2087 return wxSize(sx, sy);
78d14f80
VS
2088}
2089
2090
2091
a3b9c43b 2092wxPoint wxXmlResourceHandlerImpl::GetPosition(const wxString& param)
78d14f80
VS
2093{
2094 wxSize sz = GetSize(param);
2095 return wxPoint(sz.x, sz.y);
2096}
2097
2098
2099
a3b9c43b 2100wxCoord wxXmlResourceHandlerImpl::GetDimension(const wxString& param,
0c00c86f
VS
2101 wxCoord defaultv,
2102 wxWindow *windowToUse)
78d14f80
VS
2103{
2104 wxString s = GetParamValue(param);
e7a3a5a5 2105 if (s.empty()) return defaultv;
78d14f80
VS
2106 bool is_dlg;
2107 long sx;
2108
88a7a4e1 2109 is_dlg = s[s.length()-1] == wxT('d');
78d14f80
VS
2110 if (is_dlg) s.RemoveLast();
2111
2112 if (!s.ToLong(&sx))
2113 {
819559b2
VS
2114 ReportParamError
2115 (
2116 param,
2117 wxString::Format("cannot parse dimension value \"%s\"", s)
2118 );
78d14f80
VS
2119 return defaultv;
2120 }
2121
2122 if (is_dlg)
2123 {
0c00c86f
VS
2124 if (windowToUse)
2125 {
2126 return wxDLG_UNIT(windowToUse, wxSize(sx, 0)).x;
2127 }
a3b9c43b 2128 else if (m_handler->m_parentAsWindow)
0c00c86f 2129 {
a3b9c43b 2130 return wxDLG_UNIT(m_handler->m_parentAsWindow, wxSize(sx, 0)).x;
0c00c86f 2131 }
78d14f80
VS
2132 else
2133 {
819559b2
VS
2134 ReportParamError
2135 (
2136 param,
2137 "cannot convert dialog units: dialog unknown"
2138 );
78d14f80
VS
2139 return defaultv;
2140 }
2141 }
8516a98b
DS
2142
2143 return sx;
78d14f80
VS
2144}
2145
50c20291 2146wxDirection
a3b9c43b 2147wxXmlResourceHandlerImpl::GetDirection(const wxString& param, wxDirection dirDefault)
50c20291
VZ
2148{
2149 wxDirection dir;
2150
2151 const wxString dirstr = GetParamValue(param);
2152 if ( dirstr.empty() )
2153 dir = dirDefault;
2154 else if ( dirstr == "wxLEFT" )
2155 dir = wxLEFT;
2156 else if ( dirstr == "wxRIGHT" )
2157 dir = wxRIGHT;
2158 else if ( dirstr == "wxTOP" )
2159 dir = wxTOP;
2160 else if ( dirstr == "wxBOTTOM" )
2161 dir = wxBOTTOM;
2162 else
2163 {
2164 ReportError
2165 (
2166 GetParamNode(param),
2167 wxString::Format
2168 (
2169 "Invalid direction \"%s\": must be one of "
2170 "wxLEFT|wxRIGHT|wxTOP|wxBOTTOM.",
2171 dirstr
2172 )
2173 );
2174
2175 dir = dirDefault;
2176 }
2177
2178 return dir;
2179}
78d14f80 2180
1df61962
VS
2181// Get system font index using indexname
2182static wxFont GetSystemFont(const wxString& name)
2183{
2184 if (!name.empty())
2185 {
2186 #define SYSFNT(fnt) \
9a83f860 2187 if (name == wxT(#fnt)) return wxSystemSettings::GetFont(fnt);
1df61962
VS
2188 SYSFNT(wxSYS_OEM_FIXED_FONT)
2189 SYSFNT(wxSYS_ANSI_FIXED_FONT)
2190 SYSFNT(wxSYS_ANSI_VAR_FONT)
2191 SYSFNT(wxSYS_SYSTEM_FONT)
2192 SYSFNT(wxSYS_DEVICE_DEFAULT_FONT)
1df61962
VS
2193 SYSFNT(wxSYS_SYSTEM_FIXED_FONT)
2194 SYSFNT(wxSYS_DEFAULT_GUI_FONT)
2195 #undef SYSFNT
2196 }
2197
2198 return wxNullFont;
2199}
78d14f80 2200
a3b9c43b 2201wxFont wxXmlResourceHandlerImpl::GetFont(const wxString& param, wxWindow* parent)
78d14f80
VS
2202{
2203 wxXmlNode *font_node = GetParamNode(param);
2204 if (font_node == NULL)
2205 {
819559b2
VS
2206 ReportError(
2207 wxString::Format("cannot find font node \"%s\"", param));
78d14f80
VS
2208 return wxNullFont;
2209 }
2210
a3b9c43b
VZ
2211 wxXmlNode *oldnode = m_handler->m_node;
2212 m_handler->m_node = font_node;
78d14f80 2213
1df61962 2214 // font attributes:
78d14f80 2215
1df61962 2216 // size
94245f6d 2217 int isize = -1;
1df61962 2218 bool hasSize = HasParam(wxT("size"));
e7a3a5a5 2219 if (hasSize)
94245f6d 2220 isize = GetLong(wxT("size"), -1);
78d14f80 2221
1df61962
VS
2222 // style
2223 int istyle = wxNORMAL;
2224 bool hasStyle = HasParam(wxT("style"));
2225 if (hasStyle)
2226 {
2227 wxString style = GetParamValue(wxT("style"));
e7a3a5a5 2228 if (style == wxT("italic"))
1df61962 2229 istyle = wxITALIC;
e7a3a5a5 2230 else if (style == wxT("slant"))
1df61962 2231 istyle = wxSLANT;
9f5103f1
VZ
2232 else if (style != wxT("normal"))
2233 {
2234 ReportParamError
2235 (
2236 param,
2237 wxString::Format("unknown font style \"%s\"", style)
2238 );
2239 }
1df61962 2240 }
78d14f80 2241
1df61962
VS
2242 // weight
2243 int iweight = wxNORMAL;
2244 bool hasWeight = HasParam(wxT("weight"));
2245 if (hasWeight)
2246 {
2247 wxString weight = GetParamValue(wxT("weight"));
e7a3a5a5 2248 if (weight == wxT("bold"))
1df61962 2249 iweight = wxBOLD;
e7a3a5a5 2250 else if (weight == wxT("light"))
1df61962 2251 iweight = wxLIGHT;
9f5103f1
VZ
2252 else if (weight != wxT("normal"))
2253 {
2254 ReportParamError
2255 (
2256 param,
2257 wxString::Format("unknown font weight \"%s\"", weight)
2258 );
2259 }
1df61962 2260 }
e7a3a5a5 2261
1df61962
VS
2262 // underline
2263 bool hasUnderlined = HasParam(wxT("underlined"));
2264 bool underlined = hasUnderlined ? GetBool(wxT("underlined"), false) : false;
78d14f80 2265
1df61962
VS
2266 // family and facename
2267 int ifamily = wxDEFAULT;
2268 bool hasFamily = HasParam(wxT("family"));
2269 if (hasFamily)
78d14f80 2270 {
1df61962
VS
2271 wxString family = GetParamValue(wxT("family"));
2272 if (family == wxT("decorative")) ifamily = wxDECORATIVE;
2273 else if (family == wxT("roman")) ifamily = wxROMAN;
2274 else if (family == wxT("script")) ifamily = wxSCRIPT;
2275 else if (family == wxT("swiss")) ifamily = wxSWISS;
2276 else if (family == wxT("modern")) ifamily = wxMODERN;
2277 else if (family == wxT("teletype")) ifamily = wxTELETYPE;
9f5103f1
VZ
2278 else
2279 {
2280 ReportParamError
2281 (
2282 param,
2283 wxString::Format("unknown font family \"%s\"", family)
2284 );
2285 }
1df61962 2286 }
e7a3a5a5
WS
2287
2288
1df61962
VS
2289 wxString facename;
2290 bool hasFacename = HasParam(wxT("face"));
2291 if (hasFacename)
2292 {
2293 wxString faces = GetParamValue(wxT("face"));
1df61962 2294 wxStringTokenizer tk(faces, wxT(","));
63feebce
VS
2295#if wxUSE_FONTENUM
2296 wxArrayString facenames(wxFontEnumerator::GetFacenames());
1df61962 2297 while (tk.HasMoreTokens())
78d14f80 2298 {
6540132f 2299 int index = facenames.Index(tk.GetNextToken(), false);
1df61962
VS
2300 if (index != wxNOT_FOUND)
2301 {
6540132f 2302 facename = facenames[index];
1df61962
VS
2303 break;
2304 }
78d14f80 2305 }
63feebce
VS
2306#else // !wxUSE_FONTENUM
2307 // just use the first face name if we can't check its availability:
2308 if (tk.HasMoreTokens())
2309 facename = tk.GetNextToken();
2310#endif // wxUSE_FONTENUM/!wxUSE_FONTENUM
78d14f80
VS
2311 }
2312
1df61962
VS
2313 // encoding
2314 wxFontEncoding enc = wxFONTENCODING_DEFAULT;
2315 bool hasEncoding = HasParam(wxT("encoding"));
dc2575ba 2316#if wxUSE_FONTMAP
1df61962
VS
2317 if (hasEncoding)
2318 {
2319 wxString encoding = GetParamValue(wxT("encoding"));
2320 wxFontMapper mapper;
e7a3a5a5 2321 if (!encoding.empty())
1df61962
VS
2322 enc = mapper.CharsetToEncoding(encoding);
2323 if (enc == wxFONTENCODING_SYSTEM)
2324 enc = wxFONTENCODING_DEFAULT;
2325 }
dc2575ba 2326#endif // wxUSE_FONTMAP
78d14f80 2327
45df4bb6
VZ
2328 wxFont font;
2329
1df61962 2330 // is this font based on a system font?
45df4bb6
VZ
2331 if (HasParam(wxT("sysfont")))
2332 {
2333 font = GetSystemFont(GetParamValue(wxT("sysfont")));
9f5103f1
VZ
2334 if (HasParam(wxT("inherit")))
2335 {
2336 ReportParamError
2337 (
2338 param,
2339 "double specification of \"sysfont\" and \"inherit\""
2340 );
2341 }
45df4bb6
VZ
2342 }
2343 // or should the font of the widget be used?
2344 else if (GetBool(wxT("inherit"), false))
2345 {
2346 if (parent)
2347 font = parent->GetFont();
2348 else
9f5103f1
VZ
2349 {
2350 ReportParamError
2351 (
2352 param,
2353 "no parent window specified to derive the font from"
2354 );
2355 }
45df4bb6 2356 }
e7a3a5a5 2357
a1b806b9 2358 if (font.IsOk())
1df61962 2359 {
94245f6d 2360 if (hasSize && isize != -1)
9f5103f1 2361 {
94245f6d 2362 font.SetPointSize(isize);
9f5103f1
VZ
2363 if (HasParam(wxT("relativesize")))
2364 {
2365 ReportParamError
2366 (
2367 param,
2368 "double specification of \"size\" and \"relativesize\""
2369 );
2370 }
2371 }
1df61962 2372 else if (HasParam(wxT("relativesize")))
94245f6d 2373 font.SetPointSize(int(font.GetPointSize() *
1df61962 2374 GetFloat(wxT("relativesize"))));
e7a3a5a5 2375
1df61962 2376 if (hasStyle)
94245f6d 2377 font.SetStyle(istyle);
1df61962 2378 if (hasWeight)
94245f6d 2379 font.SetWeight(iweight);
1df61962 2380 if (hasUnderlined)
94245f6d 2381 font.SetUnderlined(underlined);
1df61962 2382 if (hasFamily)
94245f6d 2383 font.SetFamily(ifamily);
1df61962 2384 if (hasFacename)
94245f6d 2385 font.SetFaceName(facename);
1df61962 2386 if (hasEncoding)
94245f6d
VZ
2387 font.SetDefaultEncoding(enc);
2388 }
2389 else // not based on system font
2390 {
2391 font = wxFont(isize == -1 ? wxNORMAL_FONT->GetPointSize() : isize,
2392 ifamily, istyle, iweight,
2393 underlined, facename, enc);
1df61962 2394 }
8516a98b 2395
a3b9c43b 2396 m_handler->m_node = oldnode;
94245f6d 2397 return font;
78d14f80
VS
2398}
2399
2400
a3b9c43b 2401void wxXmlResourceHandlerImpl::SetupWindow(wxWindow *wnd)
78d14f80
VS
2402{
2403 //FIXME : add cursor
2404
2405 if (HasParam(wxT("exstyle")))
0099f343
JS
2406 // Have to OR it with existing style, since
2407 // some implementations (e.g. wxGTK) use the extra style
2408 // during creation
2409 wnd->SetExtraStyle(wnd->GetExtraStyle() | GetStyle(wxT("exstyle")));
78d14f80
VS
2410 if (HasParam(wxT("bg")))
2411 wnd->SetBackgroundColour(GetColour(wxT("bg")));
a7435c3e
VZ
2412 if (HasParam(wxT("ownbg")))
2413 wnd->SetOwnBackgroundColour(GetColour(wxT("ownbg")));
78d14f80
VS
2414 if (HasParam(wxT("fg")))
2415 wnd->SetForegroundColour(GetColour(wxT("fg")));
a7435c3e
VZ
2416 if (HasParam(wxT("ownfg")))
2417 wnd->SetOwnForegroundColour(GetColour(wxT("ownfg")));
78d14f80 2418 if (GetBool(wxT("enabled"), 1) == 0)
f80ea77b 2419 wnd->Enable(false);
78d14f80
VS
2420 if (GetBool(wxT("focused"), 0) == 1)
2421 wnd->SetFocus();
2422 if (GetBool(wxT("hidden"), 0) == 1)
f80ea77b 2423 wnd->Show(false);
78d14f80
VS
2424#if wxUSE_TOOLTIPS
2425 if (HasParam(wxT("tooltip")))
2426 wnd->SetToolTip(GetText(wxT("tooltip")));
2427#endif
2428 if (HasParam(wxT("font")))
45df4bb6 2429 wnd->SetFont(GetFont(wxT("font"), wnd));
a7435c3e 2430 if (HasParam(wxT("ownfont")))
45df4bb6 2431 wnd->SetOwnFont(GetFont(wxT("ownfont"), wnd));
b23030d6
JS
2432 if (HasParam(wxT("help")))
2433 wnd->SetHelpText(GetText(wxT("help")));
78d14f80
VS
2434}
2435
2436
a3b9c43b 2437void wxXmlResourceHandlerImpl::CreateChildren(wxObject *parent, bool this_hnd_only)
78d14f80 2438{
a3b9c43b 2439 for ( wxXmlNode *n = m_handler->m_node->GetChildren(); n; n = n->GetNext() )
78d14f80 2440 {
23239d94 2441 if ( IsObjectNode(n) )
78d14f80 2442 {
a3b9c43b
VZ
2443 m_handler->m_resource->DoCreateResFromNode(*n, parent, NULL,
2444 this_hnd_only ? this->GetHandler() : NULL);
2445 }
78d14f80
VS
2446 }
2447}
2448
2449
a3b9c43b 2450void wxXmlResourceHandlerImpl::CreateChildrenPrivately(wxObject *parent, wxXmlNode *rootnode)
78d14f80
VS
2451{
2452 wxXmlNode *root;
a3b9c43b 2453 if (rootnode == NULL) root = m_handler->m_node; else root = rootnode;
78d14f80
VS
2454 wxXmlNode *n = root->GetChildren();
2455
2456 while (n)
2457 {
a3b9c43b 2458 if (n->GetType() == wxXML_ELEMENT_NODE && GetHandler()->CanHandle(n))
78d14f80
VS
2459 {
2460 CreateResource(n, parent, NULL);
2461 }
2462 n = n->GetNext();
2463 }
2464}
2465
2466
819559b2
VS
2467//-----------------------------------------------------------------------------
2468// errors reporting
2469//-----------------------------------------------------------------------------
2470
a3b9c43b 2471void wxXmlResourceHandlerImpl::ReportError(const wxString& message)
819559b2 2472{
a3b9c43b 2473 m_handler->m_resource->ReportError(m_handler->m_node, message);
819559b2
VS
2474}
2475
a3b9c43b 2476void wxXmlResourceHandlerImpl::ReportError(wxXmlNode *context,
819559b2
VS
2477 const wxString& message)
2478{
a3b9c43b 2479 m_handler->m_resource->ReportError(context ? context : m_handler->m_node, message);
819559b2
VS
2480}
2481
a3b9c43b 2482void wxXmlResourceHandlerImpl::ReportParamError(const wxString& param,
819559b2
VS
2483 const wxString& message)
2484{
a3b9c43b 2485 m_handler->m_resource->ReportError(GetParamNode(param), message);
819559b2
VS
2486}
2487
1f6ea935 2488void wxXmlResource::ReportError(const wxXmlNode *context, const wxString& message)
819559b2
VS
2489{
2490 if ( !context )
2491 {
2492 DoReportError("", NULL, message);
2493 return;
2494 }
78d14f80 2495
819559b2
VS
2496 // We need to find out the file that 'context' is part of. Performance of
2497 // this code is not critical, so we simply find the root XML node and
2498 // compare it with all loaded XRC files.
2499 const wxString filename = GetFileNameFromNode(context, Data());
78d14f80 2500
819559b2
VS
2501 DoReportError(filename, context, message);
2502}
78d14f80 2503
1f6ea935 2504void wxXmlResource::DoReportError(const wxString& xrcFile, const wxXmlNode *position,
819559b2
VS
2505 const wxString& message)
2506{
2507 const int line = position ? position->GetLineNumber() : -1;
2508
2509 wxString loc;
2510 if ( !xrcFile.empty() )
2511 loc = xrcFile + ':';
2512 if ( line != -1 )
2513 loc += wxString::Format("%d:", line);
2514 if ( !loc.empty() )
2515 loc += ' ';
2516
2517 wxLogError("XRC error: %s%s", loc, message);
2518}
78d14f80
VS
2519
2520
819559b2
VS
2521//-----------------------------------------------------------------------------
2522// XRCID implementation
2523//-----------------------------------------------------------------------------
78d14f80 2524
5ed345b7 2525#define XRCID_TABLE_SIZE 1024
78d14f80
VS
2526
2527
5ed345b7 2528struct XRCID_record
78d14f80 2529{
cf2810aa
VZ
2530 /* Hold the id so that once an id is allocated for a name, it
2531 does not get created again by NewControlId at least
2532 until we are done with it */
2533 wxWindowIDRef id;
c560da98 2534 char *key;
5ed345b7 2535 XRCID_record *next;
78d14f80
VS
2536};
2537
5ed345b7 2538static XRCID_record *XRCID_Records[XRCID_TABLE_SIZE] = {NULL};
78d14f80 2539
d807030e
VZ
2540// Extremely simplistic hash function which probably ought to be replaced with
2541// wxStringHash::stringHash().
2542static inline unsigned XRCIdHash(const char *str_id)
78d14f80 2543{
d807030e 2544 unsigned index = 0;
78d14f80 2545
e03951b7 2546 for (const char *c = str_id; *c != '\0'; c++) index += (unsigned int)*c;
5ed345b7 2547 index %= XRCID_TABLE_SIZE;
78d14f80 2548
d807030e
VZ
2549 return index;
2550}
2551
3cf2fe54 2552static void XRCID_Assign(const wxString& str_id, int value)
d265ec6b 2553{
d7a46c4e 2554 const wxCharBuffer buf_id(str_id.mb_str());
3cf2fe54 2555 const unsigned index = XRCIdHash(buf_id);
d265ec6b
VZ
2556
2557
2558 XRCID_record *oldrec = NULL;
2559 for (XRCID_record *rec = XRCID_Records[index]; rec; rec = rec->next)
2560 {
3cf2fe54 2561 if (wxStrcmp(rec->key, buf_id) == 0)
d265ec6b
VZ
2562 {
2563 rec->id = value;
2564 return;
2565 }
2566 oldrec = rec;
2567 }
2568
2569 XRCID_record **rec_var = (oldrec == NULL) ?
2570 &XRCID_Records[index] : &oldrec->next;
2571 *rec_var = new XRCID_record;
2572 (*rec_var)->key = wxStrdup(str_id);
2573 (*rec_var)->id = value;
2574 (*rec_var)->next = NULL;
2575}
2576
d807030e
VZ
2577static int XRCID_Lookup(const char *str_id, int value_if_not_found = wxID_NONE)
2578{
2579 const unsigned index = XRCIdHash(str_id);
2580
2581
5ed345b7 2582 XRCID_record *oldrec = NULL;
5ed345b7 2583 for (XRCID_record *rec = XRCID_Records[index]; rec; rec = rec->next)
78d14f80 2584 {
00393283 2585 if (wxStrcmp(rec->key, str_id) == 0)
78d14f80
VS
2586 {
2587 return rec->id;
2588 }
78d14f80
VS
2589 oldrec = rec;
2590 }
2591
5ed345b7
VS
2592 XRCID_record **rec_var = (oldrec == NULL) ?
2593 &XRCID_Records[index] : &oldrec->next;
2594 *rec_var = new XRCID_record;
00393283 2595 (*rec_var)->key = wxStrdup(str_id);
78d14f80
VS
2596 (*rec_var)->next = NULL;
2597
c560da98 2598 char *end;
9b2a7469 2599 if (value_if_not_found != wxID_NONE)
13de23f6 2600 (*rec_var)->id = value_if_not_found;
85452d74
VS
2601 else
2602 {
13de23f6
VS
2603 int asint = wxStrtol(str_id, &end, 10);
2604 if (*str_id && *end == 0)
2605 {
2606 // if str_id was integer, keep it verbosely:
2607 (*rec_var)->id = asint;
2608 }
2609 else
2610 {
f35fdf7e 2611 (*rec_var)->id = wxWindowBase::NewControlId();
13de23f6 2612 }
85452d74
VS
2613 }
2614
78d14f80
VS
2615 return (*rec_var)->id;
2616}
2617
a58804ea 2618namespace
78d14f80 2619{
f35fdf7e 2620
a58804ea
VZ
2621// flag indicating whether standard XRC ids were already initialized
2622static bool gs_stdIDsAdded = false;
78d14f80 2623
a58804ea 2624void AddStdXRCID_Records()
13de23f6 2625{
c560da98 2626#define stdID(id) XRCID_Lookup(#id, id)
13de23f6 2627 stdID(-1);
c369d4f1
VS
2628
2629 stdID(wxID_ANY);
2630 stdID(wxID_SEPARATOR);
e7a3a5a5 2631
c369d4f1
VS
2632 stdID(wxID_OPEN);
2633 stdID(wxID_CLOSE);
2634 stdID(wxID_NEW);
2635 stdID(wxID_SAVE);
2636 stdID(wxID_SAVEAS);
2637 stdID(wxID_REVERT);
2638 stdID(wxID_EXIT);
2639 stdID(wxID_UNDO);
2640 stdID(wxID_REDO);
2641 stdID(wxID_HELP);
2642 stdID(wxID_PRINT);
2643 stdID(wxID_PRINT_SETUP);
e63f19ba 2644 stdID(wxID_PAGE_SETUP);
c369d4f1
VS
2645 stdID(wxID_PREVIEW);
2646 stdID(wxID_ABOUT);
2647 stdID(wxID_HELP_CONTENTS);
9a9c20be
VZ
2648 stdID(wxID_HELP_INDEX),
2649 stdID(wxID_HELP_SEARCH),
c369d4f1
VS
2650 stdID(wxID_HELP_COMMANDS);
2651 stdID(wxID_HELP_PROCEDURES);
2652 stdID(wxID_HELP_CONTEXT);
13de23f6 2653 stdID(wxID_CLOSE_ALL);
c369d4f1 2654 stdID(wxID_PREFERENCES);
9a9c20be 2655
d73195fd 2656 stdID(wxID_EDIT);
c369d4f1
VS
2657 stdID(wxID_CUT);
2658 stdID(wxID_COPY);
2659 stdID(wxID_PASTE);
2660 stdID(wxID_CLEAR);
2661 stdID(wxID_FIND);
2662 stdID(wxID_DUPLICATE);
2663 stdID(wxID_SELECTALL);
2664 stdID(wxID_DELETE);
2665 stdID(wxID_REPLACE);
2666 stdID(wxID_REPLACE_ALL);
2667 stdID(wxID_PROPERTIES);
9a9c20be 2668
c369d4f1
VS
2669 stdID(wxID_VIEW_DETAILS);
2670 stdID(wxID_VIEW_LARGEICONS);
2671 stdID(wxID_VIEW_SMALLICONS);
2672 stdID(wxID_VIEW_LIST);
2673 stdID(wxID_VIEW_SORTDATE);
2674 stdID(wxID_VIEW_SORTNAME);
2675 stdID(wxID_VIEW_SORTSIZE);
2676 stdID(wxID_VIEW_SORTTYPE);
9a9c20be
VZ
2677
2678
c369d4f1
VS
2679 stdID(wxID_FILE1);
2680 stdID(wxID_FILE2);
2681 stdID(wxID_FILE3);
2682 stdID(wxID_FILE4);
2683 stdID(wxID_FILE5);
2684 stdID(wxID_FILE6);
2685 stdID(wxID_FILE7);
2686 stdID(wxID_FILE8);
2687 stdID(wxID_FILE9);
9a9c20be
VZ
2688
2689
c369d4f1
VS
2690 stdID(wxID_OK);
2691 stdID(wxID_CANCEL);
2692 stdID(wxID_APPLY);
2693 stdID(wxID_YES);
2694 stdID(wxID_NO);
2695 stdID(wxID_STATIC);
2696 stdID(wxID_FORWARD);
2697 stdID(wxID_BACKWARD);
2698 stdID(wxID_DEFAULT);
2699 stdID(wxID_MORE);
2700 stdID(wxID_SETUP);
2701 stdID(wxID_RESET);
2702 stdID(wxID_CONTEXT_HELP);
2703 stdID(wxID_YESTOALL);
2704 stdID(wxID_NOTOALL);
2705 stdID(wxID_ABORT);
2706 stdID(wxID_RETRY);
2707 stdID(wxID_IGNORE);
2708 stdID(wxID_ADD);
2709 stdID(wxID_REMOVE);
9a9c20be 2710
c369d4f1
VS
2711 stdID(wxID_UP);
2712 stdID(wxID_DOWN);
2713 stdID(wxID_HOME);
2714 stdID(wxID_REFRESH);
2715 stdID(wxID_STOP);
2716 stdID(wxID_INDEX);
9a9c20be 2717
c369d4f1
VS
2718 stdID(wxID_BOLD);
2719 stdID(wxID_ITALIC);
2720 stdID(wxID_JUSTIFY_CENTER);
2721 stdID(wxID_JUSTIFY_FILL);
2722 stdID(wxID_JUSTIFY_RIGHT);
2723 stdID(wxID_JUSTIFY_LEFT);
2724 stdID(wxID_UNDERLINE);
2725 stdID(wxID_INDENT);
2726 stdID(wxID_UNINDENT);
2727 stdID(wxID_ZOOM_100);
2728 stdID(wxID_ZOOM_FIT);
2729 stdID(wxID_ZOOM_IN);
2730 stdID(wxID_ZOOM_OUT);
2731 stdID(wxID_UNDELETE);
2732 stdID(wxID_REVERT_TO_SAVED);
6b1eedc1
VZ
2733 stdID(wxID_CDROM);
2734 stdID(wxID_CONVERT);
2735 stdID(wxID_EXECUTE);
2736 stdID(wxID_FLOPPY);
2737 stdID(wxID_HARDDISK);
2738 stdID(wxID_BOTTOM);
2739 stdID(wxID_FIRST);
2740 stdID(wxID_LAST);
2741 stdID(wxID_TOP);
2742 stdID(wxID_INFO);
2743 stdID(wxID_JUMP_TO);
2744 stdID(wxID_NETWORK);
2745 stdID(wxID_SELECT_COLOR);
2746 stdID(wxID_SELECT_FONT);
2747 stdID(wxID_SORT_ASCENDING);
2748 stdID(wxID_SORT_DESCENDING);
2749 stdID(wxID_SPELL_CHECK);
2750 stdID(wxID_STRIKETHROUGH);
c369d4f1 2751
9a9c20be
VZ
2752
2753 stdID(wxID_SYSTEM_MENU);
2754 stdID(wxID_CLOSE_FRAME);
2755 stdID(wxID_MOVE_FRAME);
2756 stdID(wxID_RESIZE_FRAME);
2757 stdID(wxID_MAXIMIZE_FRAME);
2758 stdID(wxID_ICONIZE_FRAME);
2759 stdID(wxID_RESTORE_FRAME);
2760
2761
2762
2763 stdID(wxID_MDI_WINDOW_CASCADE);
2764 stdID(wxID_MDI_WINDOW_TILE_HORZ);
2765 stdID(wxID_MDI_WINDOW_TILE_VERT);
2766 stdID(wxID_MDI_WINDOW_ARRANGE_ICONS);
2767 stdID(wxID_MDI_WINDOW_PREV);
2768 stdID(wxID_MDI_WINDOW_NEXT);
13de23f6
VS
2769#undef stdID
2770}
78d14f80 2771
a58804ea 2772} // anonymous namespace
78d14f80
VS
2773
2774
a58804ea
VZ
2775/*static*/
2776int wxXmlResource::DoGetXRCID(const char *str_id, int value_if_not_found)
2777{
2778 if ( !gs_stdIDsAdded )
2779 {
2780 gs_stdIDsAdded = true;
2781 AddStdXRCID_Records();
2782 }
2783
2784 return XRCID_Lookup(str_id, value_if_not_found);
2785}
2786
2787/* static */
2788wxString wxXmlResource::FindXRCIDById(int numId)
2789{
2790 for ( int i = 0; i < XRCID_TABLE_SIZE; i++ )
2791 {
2792 for ( XRCID_record *rec = XRCID_Records[i]; rec; rec = rec->next )
2793 {
2794 if ( rec->id == numId )
2795 return wxString(rec->key);
2796 }
2797 }
2798
2799 return wxString();
2800}
2801
2802static void CleanXRCID_Record(XRCID_record *rec)
2803{
2804 if (rec)
2805 {
2806 CleanXRCID_Record(rec->next);
2807
2808 free(rec->key);
2809 delete rec;
2810 }
2811}
2812
2813static void CleanXRCID_Records()
2814{
2815 for (int i = 0; i < XRCID_TABLE_SIZE; i++)
2816 {
2817 CleanXRCID_Record(XRCID_Records[i]);
2818 XRCID_Records[i] = NULL;
2819 }
2820
2821 gs_stdIDsAdded = false;
2822}
78d14f80
VS
2823
2824
819559b2
VS
2825//-----------------------------------------------------------------------------
2826// module and globals
2827//-----------------------------------------------------------------------------
78d14f80 2828
fd230129
VZ
2829// normally we would do the cleanup from wxXmlResourceModule::OnExit() but it
2830// can happen that some XRC records have been created because of the use of
2831// XRCID() in event tables, which happens during static objects initialization,
2832// but then the application initialization failed and so the wx modules were
2833// neither initialized nor cleaned up -- this static object does the cleanup in
2834// this case
2835static struct wxXRCStaticCleanup
2836{
2837 ~wxXRCStaticCleanup() { CleanXRCID_Records(); }
2838} s_staticCleanup;
2839
78d14f80
VS
2840class wxXmlResourceModule: public wxModule
2841{
2842DECLARE_DYNAMIC_CLASS(wxXmlResourceModule)
2843public:
2844 wxXmlResourceModule() {}
824e8eaa
VS
2845 bool OnInit()
2846 {
2b5f62a0 2847 wxXmlResource::AddSubclassFactory(new wxXmlSubclassFactoryCXX);
f80ea77b 2848 return true;
824e8eaa 2849 }
78d14f80
VS
2850 void OnExit()
2851 {
1542c42e 2852 delete wxXmlResource::Set(NULL);
0526c8cc 2853 delete wxIdRangeManager::Set(NULL);
461932ae 2854 if(wxXmlResource::ms_subclassFactories)
eb2d0d23
VS
2855 {
2856 for ( wxXmlSubclassFactories::iterator i = wxXmlResource::ms_subclassFactories->begin();
2857 i != wxXmlResource::ms_subclassFactories->end(); ++i )
2858 {
2859 delete *i;
2860 }
2861 wxDELETE(wxXmlResource::ms_subclassFactories);
2862 }
5ed345b7 2863 CleanXRCID_Records();
78d14f80
VS
2864 }
2865};
2866
2867IMPLEMENT_DYNAMIC_CLASS(wxXmlResourceModule, wxModule)
2868
2869
2870// When wxXml is loaded dynamically after the application is already running
2871// then the built-in module system won't pick this one up. Add it manually.
2872void wxXmlInitResourceModule()
2873{
2874 wxModule* module = new wxXmlResourceModule;
2875 module->Init();
2876 wxModule::RegisterModule(module);
2877}
a1e4ec87
VS
2878
2879#endif // wxUSE_XRC