]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/xrc/xmlres.cpp | |
3 | // Purpose: XRC resources | |
4 | // Author: Vaclav Slavik | |
5 | // Created: 2000/03/05 | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) 2000 Vaclav Slavik | |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | // For compilers that support precompilation, includes "wx.h". | |
12 | #include "wx/wxprec.h" | |
13 | ||
14 | #ifdef __BORLANDC__ | |
15 | #pragma hdrstop | |
16 | #endif | |
17 | ||
18 | #if wxUSE_XRC | |
19 | ||
20 | #include "wx/xrc/xmlres.h" | |
21 | ||
22 | #ifndef WX_PRECOMP | |
23 | #include "wx/intl.h" | |
24 | #include "wx/log.h" | |
25 | #include "wx/panel.h" | |
26 | #include "wx/frame.h" | |
27 | #include "wx/dialog.h" | |
28 | #include "wx/settings.h" | |
29 | #include "wx/bitmap.h" | |
30 | #include "wx/image.h" | |
31 | #include "wx/module.h" | |
32 | #include "wx/wxcrtvararg.h" | |
33 | #endif | |
34 | ||
35 | #ifndef __WXWINCE__ | |
36 | #include <locale.h> | |
37 | #endif | |
38 | ||
39 | #include "wx/vector.h" | |
40 | #include "wx/wfstream.h" | |
41 | #include "wx/filesys.h" | |
42 | #include "wx/filename.h" | |
43 | #include "wx/tokenzr.h" | |
44 | #include "wx/fontenum.h" | |
45 | #include "wx/fontmap.h" | |
46 | #include "wx/artprov.h" | |
47 | #include "wx/imaglist.h" | |
48 | #include "wx/dir.h" | |
49 | #include "wx/xml/xml.h" | |
50 | #include "wx/hashset.h" | |
51 | #include "wx/scopedptr.h" | |
52 | ||
53 | namespace | |
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 | ||
60 | wxDateTime 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 | |
75 | ||
76 | // Assign the given value to the specified entry or add a new value with this | |
77 | // name. | |
78 | static void XRCID_Assign(const wxString& str_id, int value); | |
79 | ||
80 | class wxXmlResourceDataRecord | |
81 | { | |
82 | public: | |
83 | // Ctor takes ownership of the document pointer. | |
84 | wxXmlResourceDataRecord(const wxString& File_, | |
85 | wxXmlDocument *Doc_ | |
86 | ) | |
87 | : File(File_), Doc(Doc_) | |
88 | { | |
89 | #if wxUSE_DATETIME | |
90 | Time = GetXRCFileModTime(File); | |
91 | #endif | |
92 | } | |
93 | ||
94 | ~wxXmlResourceDataRecord() {delete Doc;} | |
95 | ||
96 | wxString File; | |
97 | wxXmlDocument *Doc; | |
98 | #if wxUSE_DATETIME | |
99 | wxDateTime Time; | |
100 | #endif | |
101 | ||
102 | wxDECLARE_NO_COPY_CLASS(wxXmlResourceDataRecord); | |
103 | }; | |
104 | ||
105 | class wxXmlResourceDataRecords : public wxVector<wxXmlResourceDataRecord*> | |
106 | { | |
107 | // this is a class so that it can be forward-declared | |
108 | }; | |
109 | ||
110 | WX_DECLARE_HASH_SET_PTR(int, wxIntegerHash, wxIntegerEqual, wxHashSetInt); | |
111 | ||
112 | class wxIdRange // Holds data for a particular rangename | |
113 | { | |
114 | protected: | |
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 | ||
140 | class wxIdRangeManager | |
141 | { | |
142 | public: | |
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; | |
159 | ||
160 | protected: | |
161 | wxIdRange* FindRangeForItem(const wxXmlNode* node, | |
162 | const wxString& item, | |
163 | wxString& value) const; | |
164 | wxVector<wxIdRange*> m_IdRanges; | |
165 | ||
166 | private: | |
167 | static wxIdRangeManager *ms_instance; | |
168 | }; | |
169 | ||
170 | namespace | |
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 | |
177 | inline 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 | ||
184 | // special XML attribute with name of input file, see GetFileNameFromNode() | |
185 | const char *ATTR_INPUT_FILENAME = "__wx:filename"; | |
186 | ||
187 | // helper to get filename corresponding to an XML node | |
188 | wxString | |
189 | GetFileNameFromNode(const wxXmlNode *node, const wxXmlResourceDataRecords& files) | |
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 | ||
226 | } // anonymous namespace | |
227 | ||
228 | ||
229 | wxXmlResource *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 | ||
245 | wxXmlResource::wxXmlResource(int flags, const wxString& domain) | |
246 | { | |
247 | m_flags = flags; | |
248 | m_version = -1; | |
249 | m_data = new wxXmlResourceDataRecords; | |
250 | SetDomain(domain); | |
251 | } | |
252 | ||
253 | wxXmlResource::wxXmlResource(const wxString& filemask, int flags, const wxString& domain) | |
254 | { | |
255 | m_flags = flags; | |
256 | m_version = -1; | |
257 | m_data = new wxXmlResourceDataRecords; | |
258 | SetDomain(domain); | |
259 | Load(filemask); | |
260 | } | |
261 | ||
262 | wxXmlResource::~wxXmlResource() | |
263 | { | |
264 | ClearHandlers(); | |
265 | ||
266 | for ( wxXmlResourceDataRecords::iterator i = m_data->begin(); | |
267 | i != m_data->end(); ++i ) | |
268 | { | |
269 | delete *i; | |
270 | } | |
271 | delete m_data; | |
272 | } | |
273 | ||
274 | void wxXmlResource::SetDomain(const wxString& domain) | |
275 | { | |
276 | m_domain = domain; | |
277 | } | |
278 | ||
279 | ||
280 | /* static */ | |
281 | wxString 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 */ | |
311 | bool 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 | ||
320 | bool wxXmlResource::LoadFile(const wxFileName& file) | |
321 | { | |
322 | #if wxUSE_FILESYSTEM | |
323 | return Load(wxFileSystem::FileNameToURL(file)); | |
324 | #else | |
325 | return Load(file.GetFullPath()); | |
326 | #endif | |
327 | } | |
328 | ||
329 | bool 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 | ||
345 | bool wxXmlResource::Load(const wxString& filemask_) | |
346 | { | |
347 | wxString filemask = ConvertFileNameToURL(filemask_); | |
348 | ||
349 | bool allOK = true; | |
350 | ||
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 | |
359 | wxString fnd = wxXmlFindFirst; | |
360 | if ( fnd.empty() ) | |
361 | { | |
362 | wxLogError(_("Cannot load resources from '%s'."), filemask); | |
363 | return false; | |
364 | } | |
365 | ||
366 | while (!fnd.empty()) | |
367 | { | |
368 | #if wxUSE_FILESYSTEM | |
369 | if ( IsArchive(fnd) ) | |
370 | { | |
371 | if ( !Load(fnd + wxT("#zip:*.xrc")) ) | |
372 | allOK = false; | |
373 | } | |
374 | else // a single resource URL | |
375 | #endif // wxUSE_FILESYSTEM | |
376 | { | |
377 | wxXmlDocument * const doc = DoLoadFile(fnd); | |
378 | if ( !doc ) | |
379 | allOK = false; | |
380 | else | |
381 | Data().push_back(new wxXmlResourceDataRecord(fnd, doc)); | |
382 | } | |
383 | ||
384 | fnd = wxXmlFindNext; | |
385 | } | |
386 | # undef wxXmlFindFirst | |
387 | # undef wxXmlFindNext | |
388 | ||
389 | return allOK; | |
390 | } | |
391 | ||
392 | bool wxXmlResource::Unload(const wxString& filename) | |
393 | { | |
394 | wxASSERT_MSG( !wxIsWild(filename), | |
395 | wxT("wildcards not supported by wxXmlResource::Unload()") ); | |
396 | ||
397 | wxString fnd = ConvertFileNameToURL(filename); | |
398 | #if wxUSE_FILESYSTEM | |
399 | const bool isArchive = IsArchive(fnd); | |
400 | if ( isArchive ) | |
401 | fnd += wxT("#zip:"); | |
402 | #endif // wxUSE_FILESYSTEM | |
403 | ||
404 | bool unloaded = false; | |
405 | for ( wxXmlResourceDataRecords::iterator i = Data().begin(); | |
406 | i != Data().end(); ++i ) | |
407 | { | |
408 | #if wxUSE_FILESYSTEM | |
409 | if ( isArchive ) | |
410 | { | |
411 | if ( (*i)->File.StartsWith(fnd) ) | |
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 | { | |
418 | if ( (*i)->File == fnd ) | |
419 | { | |
420 | delete *i; | |
421 | Data().erase(i); | |
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 | ||
433 | ||
434 | void wxXmlResource::AddHandler(wxXmlResourceHandler *handler) | |
435 | { | |
436 | wxXmlResourceHandlerImpl *impl = new wxXmlResourceHandlerImpl(handler); | |
437 | handler->SetImpl(impl); | |
438 | m_handlers.push_back(handler); | |
439 | handler->SetParentResource(this); | |
440 | } | |
441 | ||
442 | void wxXmlResource::InsertHandler(wxXmlResourceHandler *handler) | |
443 | { | |
444 | wxXmlResourceHandlerImpl *impl = new wxXmlResourceHandlerImpl(handler); | |
445 | handler->SetImpl(impl); | |
446 | m_handlers.insert(m_handlers.begin(), handler); | |
447 | handler->SetParentResource(this); | |
448 | } | |
449 | ||
450 | ||
451 | ||
452 | void wxXmlResource::ClearHandlers() | |
453 | { | |
454 | for ( wxVector<wxXmlResourceHandler*>::iterator i = m_handlers.begin(); | |
455 | i != m_handlers.end(); ++i ) | |
456 | delete *i; | |
457 | m_handlers.clear(); | |
458 | } | |
459 | ||
460 | ||
461 | wxMenu *wxXmlResource::LoadMenu(const wxString& name) | |
462 | { | |
463 | return (wxMenu*)CreateResFromNode(FindResource(name, wxT("wxMenu")), NULL, NULL); | |
464 | } | |
465 | ||
466 | ||
467 | ||
468 | wxMenuBar *wxXmlResource::LoadMenuBar(wxWindow *parent, const wxString& name) | |
469 | { | |
470 | return (wxMenuBar*)CreateResFromNode(FindResource(name, wxT("wxMenuBar")), parent, NULL); | |
471 | } | |
472 | ||
473 | ||
474 | ||
475 | #if wxUSE_TOOLBAR | |
476 | wxToolBar *wxXmlResource::LoadToolBar(wxWindow *parent, const wxString& name) | |
477 | { | |
478 | return (wxToolBar*)CreateResFromNode(FindResource(name, wxT("wxToolBar")), parent, NULL); | |
479 | } | |
480 | #endif | |
481 | ||
482 | ||
483 | wxDialog *wxXmlResource::LoadDialog(wxWindow *parent, const wxString& name) | |
484 | { | |
485 | return (wxDialog*)CreateResFromNode(FindResource(name, wxT("wxDialog")), parent, NULL); | |
486 | } | |
487 | ||
488 | bool wxXmlResource::LoadDialog(wxDialog *dlg, wxWindow *parent, const wxString& name) | |
489 | { | |
490 | return CreateResFromNode(FindResource(name, wxT("wxDialog")), parent, dlg) != NULL; | |
491 | } | |
492 | ||
493 | ||
494 | ||
495 | wxPanel *wxXmlResource::LoadPanel(wxWindow *parent, const wxString& name) | |
496 | { | |
497 | return (wxPanel*)CreateResFromNode(FindResource(name, wxT("wxPanel")), parent, NULL); | |
498 | } | |
499 | ||
500 | bool wxXmlResource::LoadPanel(wxPanel *panel, wxWindow *parent, const wxString& name) | |
501 | { | |
502 | return CreateResFromNode(FindResource(name, wxT("wxPanel")), parent, panel) != NULL; | |
503 | } | |
504 | ||
505 | wxFrame *wxXmlResource::LoadFrame(wxWindow* parent, const wxString& name) | |
506 | { | |
507 | return (wxFrame*)CreateResFromNode(FindResource(name, wxT("wxFrame")), parent, NULL); | |
508 | } | |
509 | ||
510 | bool wxXmlResource::LoadFrame(wxFrame* frame, wxWindow *parent, const wxString& name) | |
511 | { | |
512 | return CreateResFromNode(FindResource(name, wxT("wxFrame")), parent, frame) != NULL; | |
513 | } | |
514 | ||
515 | wxBitmap 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 | ||
525 | wxIcon 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 | ||
535 | ||
536 | wxObject * | |
537 | wxXmlResource::DoLoadObject(wxWindow *parent, | |
538 | const wxString& name, | |
539 | const wxString& classname, | |
540 | bool recursive) | |
541 | { | |
542 | wxXmlNode * const node = FindResource(name, classname, recursive); | |
543 | ||
544 | return node ? DoCreateResFromNode(*node, parent, NULL) : NULL; | |
545 | } | |
546 | ||
547 | bool | |
548 | wxXmlResource::DoLoadObject(wxObject *instance, | |
549 | wxWindow *parent, | |
550 | const wxString& name, | |
551 | const wxString& classname, | |
552 | bool recursive) | |
553 | { | |
554 | wxXmlNode * const node = FindResource(name, classname, recursive); | |
555 | ||
556 | return node && DoCreateResFromNode(*node, parent, instance) != NULL; | |
557 | } | |
558 | ||
559 | ||
560 | bool 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 | { | |
568 | wxLogError("Cannot find container for unknown control '%s'.", name); | |
569 | return false; | |
570 | } | |
571 | return control->Reparent(container); | |
572 | } | |
573 | ||
574 | ||
575 | static void ProcessPlatformProperty(wxXmlNode *node) | |
576 | { | |
577 | wxString s; | |
578 | bool isok; | |
579 | ||
580 | wxXmlNode *c = node->GetChildren(); | |
581 | while (c) | |
582 | { | |
583 | isok = false; | |
584 | if (!c->GetAttribute(wxT("platform"), &s)) | |
585 | isok = true; | |
586 | else | |
587 | { | |
588 | wxStringTokenizer tkn(s, wxT(" |")); | |
589 | ||
590 | while (tkn.HasMoreTokens()) | |
591 | { | |
592 | s = tkn.GetNextToken(); | |
593 | #ifdef __WINDOWS__ | |
594 | if (s == wxT("win")) isok = true; | |
595 | #endif | |
596 | #if defined(__MAC__) || defined(__APPLE__) | |
597 | if (s == wxT("mac")) isok = true; | |
598 | #elif defined(__UNIX__) | |
599 | if (s == wxT("unix")) isok = true; | |
600 | #endif | |
601 | #ifdef __OS2__ | |
602 | if (s == wxT("os2")) isok = true; | |
603 | #endif | |
604 | ||
605 | if (isok) | |
606 | break; | |
607 | } | |
608 | } | |
609 | ||
610 | if (isok) | |
611 | { | |
612 | ProcessPlatformProperty(c); | |
613 | c = c->GetNext(); | |
614 | } | |
615 | else | |
616 | { | |
617 | wxXmlNode *c2 = c->GetNext(); | |
618 | node->RemoveChild(c); | |
619 | delete c; | |
620 | c = c2; | |
621 | } | |
622 | } | |
623 | } | |
624 | ||
625 | static 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); | |
644 | ||
645 | // Do any children by recursion, then proceed to the next sibling | |
646 | PreprocessForIdRanges(c); | |
647 | c = c->GetNext(); | |
648 | } | |
649 | } | |
650 | ||
651 | bool wxXmlResource::UpdateResources() | |
652 | { | |
653 | bool rt = true; | |
654 | ||
655 | for ( wxXmlResourceDataRecords::iterator i = Data().begin(); | |
656 | i != Data().end(); ++i ) | |
657 | { | |
658 | wxXmlResourceDataRecord* const rec = *i; | |
659 | ||
660 | // Check if we need to reload this one. | |
661 | ||
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 | |
678 | { | |
679 | // No need to reload, the file wasn't modified since we did it | |
680 | // last. | |
681 | continue; | |
682 | } | |
683 | ||
684 | wxXmlDocument * const doc = DoLoadFile(rec->File); | |
685 | if ( !doc ) | |
686 | { | |
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 | } | |
693 | ||
694 | // Replace the old resource contents with the new one. | |
695 | delete rec->Doc; | |
696 | rec->Doc = doc; | |
697 | ||
698 | // And, now that we loaded it successfully, update the last load time. | |
699 | #if wxUSE_DATETIME | |
700 | rec->Time = lastModTime.IsValid() ? lastModTime : wxDateTime::Now(); | |
701 | #endif // wxUSE_DATETIME | |
702 | } | |
703 | ||
704 | return rt; | |
705 | } | |
706 | ||
707 | wxXmlDocument *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 | ||
783 | wxXmlNode *wxXmlResource::DoFindResource(wxXmlNode *parent, | |
784 | const wxString& name, | |
785 | const wxString& classname, | |
786 | bool recursive) const | |
787 | { | |
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 | { | |
794 | if ( IsObjectNode(node) && node->GetAttribute(wxS("name")) == name ) | |
795 | { | |
796 | // empty class name matches everything | |
797 | if ( classname.empty() ) | |
798 | return node; | |
799 | ||
800 | wxString cls(node->GetAttribute(wxS("class"))); | |
801 | ||
802 | // object_ref may not have 'class' attribute: | |
803 | if (cls.empty() && node->GetName() == wxS("object_ref")) | |
804 | { | |
805 | wxString refName = node->GetAttribute(wxS("ref")); | |
806 | if (refName.empty()) | |
807 | continue; | |
808 | ||
809 | const wxXmlNode * const refNode = GetResourceNode(refName); | |
810 | if ( refNode ) | |
811 | cls = refNode->GetAttribute(wxS("class")); | |
812 | } | |
813 | ||
814 | if ( cls == classname ) | |
815 | return node; | |
816 | } | |
817 | } | |
818 | ||
819 | // then recurse in child nodes | |
820 | if ( recursive ) | |
821 | { | |
822 | for (node = parent->GetChildren(); node; node = node->GetNext()) | |
823 | { | |
824 | if ( IsObjectNode(node) ) | |
825 | { | |
826 | wxXmlNode* found = DoFindResource(node, name, classname, true); | |
827 | if ( found ) | |
828 | return found; | |
829 | } | |
830 | } | |
831 | } | |
832 | ||
833 | return NULL; | |
834 | } | |
835 | ||
836 | wxXmlNode *wxXmlResource::FindResource(const wxString& name, | |
837 | const wxString& classname, | |
838 | bool recursive) | |
839 | { | |
840 | wxString path; | |
841 | wxXmlNode * const | |
842 | node = GetResourceNodeAndLocation(name, classname, recursive, &path); | |
843 | ||
844 | if ( !node ) | |
845 | { | |
846 | ReportError | |
847 | ( | |
848 | NULL, | |
849 | wxString::Format | |
850 | ( | |
851 | "XRC resource \"%s\" (class \"%s\") not found", | |
852 | name, classname | |
853 | ) | |
854 | ); | |
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 | ||
869 | wxXmlNode * | |
870 | wxXmlResource::GetResourceNodeAndLocation(const wxString& name, | |
871 | const wxString& classname, | |
872 | bool recursive, | |
873 | wxString *path) const | |
874 | { | |
875 | // ensure everything is up-to-date: this is needed to support on-demand | |
876 | // reloading of XRC files | |
877 | const_cast<wxXmlResource *>(this)->UpdateResources(); | |
878 | ||
879 | for ( wxXmlResourceDataRecords::const_iterator f = Data().begin(); | |
880 | f != Data().end(); ++f ) | |
881 | { | |
882 | wxXmlResourceDataRecord *const rec = *f; | |
883 | wxXmlDocument * const doc = rec->Doc; | |
884 | if ( !doc || !doc->GetRoot() ) | |
885 | continue; | |
886 | ||
887 | wxXmlNode * const | |
888 | found = DoFindResource(doc->GetRoot(), name, classname, recursive); | |
889 | if ( found ) | |
890 | { | |
891 | if ( path ) | |
892 | *path = rec->File; | |
893 | ||
894 | return found; | |
895 | } | |
896 | } | |
897 | ||
898 | return NULL; | |
899 | } | |
900 | ||
901 | static void MergeNodesOver(wxXmlNode& dest, wxXmlNode& overwriteWith, | |
902 | const wxString& overwriteFilename) | |
903 | { | |
904 | // Merge attributes: | |
905 | for ( wxXmlAttribute *attr = overwriteWith.GetAttributes(); | |
906 | attr; attr = attr->GetNext() ) | |
907 | { | |
908 | wxXmlAttribute *dattr; | |
909 | for (dattr = dest.GetAttributes(); dattr; dattr = dattr->GetNext()) | |
910 | { | |
911 | ||
912 | if ( dattr->GetName() == attr->GetName() ) | |
913 | { | |
914 | dattr->SetValue(attr->GetValue()); | |
915 | break; | |
916 | } | |
917 | } | |
918 | ||
919 | if ( !dattr ) | |
920 | dest.AddAttribute(attr->GetName(), attr->GetValue()); | |
921 | } | |
922 | ||
923 | // Merge child nodes: | |
924 | for (wxXmlNode* node = overwriteWith.GetChildren(); node; node = node->GetNext()) | |
925 | { | |
926 | wxString name = node->GetAttribute(wxT("name"), wxEmptyString); | |
927 | wxXmlNode *dnode; | |
928 | ||
929 | for (dnode = dest.GetChildren(); dnode; dnode = dnode->GetNext() ) | |
930 | { | |
931 | if ( dnode->GetName() == node->GetName() && | |
932 | dnode->GetAttribute(wxT("name"), wxEmptyString) == name && | |
933 | dnode->GetType() == node->GetType() ) | |
934 | { | |
935 | MergeNodesOver(*dnode, *node, overwriteFilename); | |
936 | break; | |
937 | } | |
938 | } | |
939 | ||
940 | if ( !dnode ) | |
941 | { | |
942 | wxXmlNode *copyOfNode = new wxXmlNode(*node); | |
943 | // remember referenced object's file, see GetFileNameFromNode() | |
944 | copyOfNode->AddAttribute(ATTR_INPUT_FILENAME, overwriteFilename); | |
945 | ||
946 | static const wxChar *AT_END = wxT("end"); | |
947 | wxString insert_pos = node->GetAttribute(wxT("insert_at"), AT_END); | |
948 | if ( insert_pos == AT_END ) | |
949 | { | |
950 | dest.AddChild(copyOfNode); | |
951 | } | |
952 | else if ( insert_pos == wxT("begin") ) | |
953 | { | |
954 | dest.InsertChild(copyOfNode, dest.GetChildren()); | |
955 | } | |
956 | } | |
957 | } | |
958 | ||
959 | if ( dest.GetType() == wxXML_TEXT_NODE && overwriteWith.GetContent().length() ) | |
960 | dest.SetContent(overwriteWith.GetContent()); | |
961 | } | |
962 | ||
963 | wxObject * | |
964 | wxXmlResource::DoCreateResFromNode(wxXmlNode& node, | |
965 | wxObject *parent, | |
966 | wxObject *instance, | |
967 | wxXmlResourceHandler *handlerToUse) | |
968 | { | |
969 | // handling of referenced resource | |
970 | if ( node.GetName() == wxT("object_ref") ) | |
971 | { | |
972 | wxString refName = node.GetAttribute(wxT("ref"), wxEmptyString); | |
973 | wxXmlNode* refNode = FindResource(refName, wxEmptyString, true); | |
974 | ||
975 | if ( !refNode ) | |
976 | { | |
977 | ReportError | |
978 | ( | |
979 | &node, | |
980 | wxString::Format | |
981 | ( | |
982 | "referenced object node with ref=\"%s\" not found", | |
983 | refName | |
984 | ) | |
985 | ); | |
986 | return NULL; | |
987 | } | |
988 | ||
989 | const bool hasOnlyRefAttr = node.GetAttributes() != NULL && | |
990 | node.GetAttributes()->GetNext() == NULL; | |
991 | ||
992 | if ( hasOnlyRefAttr && !node.GetChildren() ) | |
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 | ||
999 | return DoCreateResFromNode(*refNode, parent, instance); | |
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); | |
1009 | MergeNodesOver(copy, node, GetFileNameFromNode(&node, Data())); | |
1010 | ||
1011 | // remember referenced object's file, see GetFileNameFromNode() | |
1012 | copy.AddAttribute(ATTR_INPUT_FILENAME, | |
1013 | GetFileNameFromNode(refNode, Data())); | |
1014 | ||
1015 | return DoCreateResFromNode(copy, parent, instance); | |
1016 | } | |
1017 | } | |
1018 | ||
1019 | if (handlerToUse) | |
1020 | { | |
1021 | if (handlerToUse->CanHandle(&node)) | |
1022 | { | |
1023 | return handlerToUse->CreateResource(&node, parent, instance); | |
1024 | } | |
1025 | } | |
1026 | else if (node.GetName() == wxT("object")) | |
1027 | { | |
1028 | for ( wxVector<wxXmlResourceHandler*>::iterator h = m_handlers.begin(); | |
1029 | h != m_handlers.end(); ++h ) | |
1030 | { | |
1031 | wxXmlResourceHandler *handler = *h; | |
1032 | if (handler->CanHandle(&node)) | |
1033 | return handler->CreateResource(&node, parent, instance); | |
1034 | } | |
1035 | } | |
1036 | ||
1037 | ReportError | |
1038 | ( | |
1039 | &node, | |
1040 | wxString::Format | |
1041 | ( | |
1042 | "no handler found for XML node \"%s\" (class \"%s\")", | |
1043 | node.GetName(), | |
1044 | node.GetAttribute("class", wxEmptyString) | |
1045 | ) | |
1046 | ); | |
1047 | return NULL; | |
1048 | } | |
1049 | ||
1050 | wxIdRange::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 | ||
1100 | void 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 | ||
1175 | void 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 | { | |
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); | |
1225 | ||
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())); | |
1231 | } | |
1232 | // and these special ones | |
1233 | XRCID_Assign(m_name + "[start]", m_start); | |
1234 | XRCID_Assign(m_name + "[end]", m_end); | |
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 | ||
1242 | wxIdRangeManager *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 | ||
1258 | wxIdRangeManager::~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 | ||
1270 | void 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 | ||
1308 | wxIdRange * | |
1309 | wxIdRangeManager::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 | ||
1334 | void | |
1335 | wxIdRangeManager::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 | ||
1344 | int 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 | ||
1355 | void 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 | ||
1370 | ||
1371 | class wxXmlSubclassFactories : public wxVector<wxXmlSubclassFactory*> | |
1372 | { | |
1373 | // this is a class so that it can be forward-declared | |
1374 | }; | |
1375 | ||
1376 | wxXmlSubclassFactories *wxXmlResource::ms_subclassFactories = NULL; | |
1377 | ||
1378 | /*static*/ void wxXmlResource::AddSubclassFactory(wxXmlSubclassFactory *factory) | |
1379 | { | |
1380 | if (!ms_subclassFactories) | |
1381 | { | |
1382 | ms_subclassFactories = new wxXmlSubclassFactories; | |
1383 | } | |
1384 | ms_subclassFactories->push_back(factory); | |
1385 | } | |
1386 | ||
1387 | class wxXmlSubclassFactoryCXX : public wxXmlSubclassFactory | |
1388 | { | |
1389 | public: | |
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 | ||
1405 | ||
1406 | wxXmlResourceHandlerImpl::wxXmlResourceHandlerImpl(wxXmlResourceHandler *handler) | |
1407 | :wxXmlResourceHandlerImplBase(handler) | |
1408 | { | |
1409 | } | |
1410 | ||
1411 | wxObject *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 | |
1418 | wxFileSystem& wxXmlResourceHandlerImpl::GetCurFileSystem() | |
1419 | { | |
1420 | return m_handler->m_resource->GetCurFileSystem(); | |
1421 | } | |
1422 | #endif | |
1423 | ||
1424 | ||
1425 | wxObject *wxXmlResourceHandlerImpl::CreateResource(wxXmlNode *node, wxObject *parent, wxObject *instance) | |
1426 | { | |
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; | |
1431 | ||
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)) | |
1435 | { | |
1436 | wxString subclass = node->GetAttribute(wxT("subclass"), wxEmptyString); | |
1437 | if (!subclass.empty()) | |
1438 | { | |
1439 | for (wxXmlSubclassFactories::iterator i = wxXmlResource::ms_subclassFactories->begin(); | |
1440 | i != wxXmlResource::ms_subclassFactories->end(); ++i) | |
1441 | { | |
1442 | m_handler->m_instance = (*i)->Create(subclass); | |
1443 | if (m_handler->m_instance) | |
1444 | break; | |
1445 | } | |
1446 | ||
1447 | if (!m_handler->m_instance) | |
1448 | { | |
1449 | wxString name = node->GetAttribute(wxT("name"), wxEmptyString); | |
1450 | ReportError | |
1451 | ( | |
1452 | node, | |
1453 | wxString::Format | |
1454 | ( | |
1455 | "subclass \"%s\" not found for resource \"%s\", not subclassing", | |
1456 | subclass, name | |
1457 | ) | |
1458 | ); | |
1459 | } | |
1460 | } | |
1461 | } | |
1462 | ||
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); | |
1467 | ||
1468 | wxObject *returned = GetHandler()->DoCreateResource(); | |
1469 | ||
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; | |
1474 | ||
1475 | return returned; | |
1476 | } | |
1477 | ||
1478 | bool wxXmlResourceHandlerImpl::HasParam(const wxString& param) | |
1479 | { | |
1480 | return (GetParamNode(param) != NULL); | |
1481 | } | |
1482 | ||
1483 | ||
1484 | int wxXmlResourceHandlerImpl::GetStyle(const wxString& param, int defaults) | |
1485 | { | |
1486 | wxString s = GetParamValue(param); | |
1487 | ||
1488 | if (!s) return defaults; | |
1489 | ||
1490 | wxStringTokenizer tkn(s, wxT("| \t\n"), wxTOKEN_STRTOK); | |
1491 | int style = 0; | |
1492 | int index; | |
1493 | wxString fl; | |
1494 | while (tkn.HasMoreTokens()) | |
1495 | { | |
1496 | fl = tkn.GetNextToken(); | |
1497 | index = m_handler->m_styleNames.Index(fl); | |
1498 | if (index != wxNOT_FOUND) | |
1499 | { | |
1500 | style |= m_handler->m_styleValues[index]; | |
1501 | } | |
1502 | else | |
1503 | { | |
1504 | ReportParamError | |
1505 | ( | |
1506 | param, | |
1507 | wxString::Format("unknown style flag \"%s\"", fl) | |
1508 | ); | |
1509 | } | |
1510 | } | |
1511 | return style; | |
1512 | } | |
1513 | ||
1514 | ||
1515 | ||
1516 | wxString wxXmlResourceHandlerImpl::GetText(const wxString& param, bool translate) | |
1517 | { | |
1518 | wxXmlNode *parNode = GetParamNode(param); | |
1519 | wxString str1(GetNodeContent(parNode)); | |
1520 | wxString str2; | |
1521 | ||
1522 | // "\\" wasn't translated to "\" prior to 2.5.3.0: | |
1523 | const bool escapeBackslash = (m_handler->m_resource->CompareVersion(2,5,3,0) >= 0); | |
1524 | ||
1525 | // VS: First version of XRC resources used $ instead of & (which is | |
1526 | // illegal in XML), but later I realized that '_' fits this purpose | |
1527 | // much better (because &File means "File with F underlined"). | |
1528 | const wxChar amp_char = (m_handler->m_resource->CompareVersion(2,3,0,1) < 0) | |
1529 | ? '$' : '_'; | |
1530 | ||
1531 | for ( wxString::const_iterator dt = str1.begin(); dt != str1.end(); ++dt ) | |
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..."): | |
1535 | if ( *dt == amp_char ) | |
1536 | { | |
1537 | if ( dt+1 == str1.end() || *(++dt) == amp_char ) | |
1538 | str2 << amp_char; | |
1539 | else | |
1540 | str2 << wxT('&') << *dt; | |
1541 | } | |
1542 | // Remap \n to CR, \r to LF, \t to TAB, \\ to \: | |
1543 | else if ( *dt == wxT('\\') ) | |
1544 | { | |
1545 | switch ( (*(++dt)).GetValue() ) | |
1546 | { | |
1547 | case wxT('n'): | |
1548 | str2 << wxT('\n'); | |
1549 | break; | |
1550 | ||
1551 | case wxT('t'): | |
1552 | str2 << wxT('\t'); | |
1553 | break; | |
1554 | ||
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: | |
1561 | if ( escapeBackslash ) | |
1562 | { | |
1563 | str2 << wxT('\\'); | |
1564 | break; | |
1565 | } | |
1566 | // else fall-through to default: branch below | |
1567 | ||
1568 | default: | |
1569 | str2 << wxT('\\') << *dt; | |
1570 | break; | |
1571 | } | |
1572 | } | |
1573 | else | |
1574 | { | |
1575 | str2 << *dt; | |
1576 | } | |
1577 | } | |
1578 | ||
1579 | if (m_handler->m_resource->GetFlags() & wxXRC_USE_LOCALE) | |
1580 | { | |
1581 | if (translate && parNode && | |
1582 | parNode->GetAttribute(wxT("translate"), wxEmptyString) != wxT("0")) | |
1583 | { | |
1584 | return wxGetTranslation(str2, m_handler->m_resource->GetDomain()); | |
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: | |
1593 | return wxString(str2.wc_str(wxConvUTF8), wxConvLocal); | |
1594 | #endif | |
1595 | } | |
1596 | } | |
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; | |
1602 | } | |
1603 | ||
1604 | ||
1605 | ||
1606 | long wxXmlResourceHandlerImpl::GetLong(const wxString& param, long defaultv) | |
1607 | { | |
1608 | long value = defaultv; | |
1609 | wxString str1 = GetParamValue(param); | |
1610 | ||
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 | } | |
1622 | ||
1623 | return value; | |
1624 | } | |
1625 | ||
1626 | float wxXmlResourceHandlerImpl::GetFloat(const wxString& param, float defaultv) | |
1627 | { | |
1628 | wxString str = GetParamValue(param); | |
1629 | ||
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 | |
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 | } | |
1645 | ||
1646 | return wx_truncate_cast(float, value); | |
1647 | } | |
1648 | ||
1649 | ||
1650 | int wxXmlResourceHandlerImpl::GetID() | |
1651 | { | |
1652 | return wxXmlResource::GetXRCID(GetName()); | |
1653 | } | |
1654 | ||
1655 | ||
1656 | ||
1657 | wxString wxXmlResourceHandlerImpl::GetName() | |
1658 | { | |
1659 | return m_handler->m_node->GetAttribute(wxT("name"), wxT("-1")); | |
1660 | } | |
1661 | ||
1662 | ||
1663 | ||
1664 | bool wxXmlResourceHandlerImpl::GetBoolAttr(const wxString& attr, bool defaultv) | |
1665 | { | |
1666 | wxString v; | |
1667 | return m_handler->m_node->GetAttribute(attr, &v) ? v == '1' : defaultv; | |
1668 | } | |
1669 | ||
1670 | bool wxXmlResourceHandlerImpl::GetBool(const wxString& param, bool defaultv) | |
1671 | { | |
1672 | const wxString v = GetParamValue(param); | |
1673 | ||
1674 | return v.empty() ? defaultv : (v == '1'); | |
1675 | } | |
1676 | ||
1677 | ||
1678 | static wxColour GetSystemColour(const wxString& name) | |
1679 | { | |
1680 | if (!name.empty()) | |
1681 | { | |
1682 | #define SYSCLR(clr) \ | |
1683 | if (name == wxT(#clr)) return wxSystemSettings::GetColour(clr); | |
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 | } | |
1726 | ||
1727 | wxColour wxXmlResourceHandlerImpl::GetColour(const wxString& param, const wxColour& defaultv) | |
1728 | { | |
1729 | wxString v = GetParamValue(param); | |
1730 | ||
1731 | if ( v.empty() ) | |
1732 | return defaultv; | |
1733 | ||
1734 | wxColour clr; | |
1735 | ||
1736 | // wxString -> wxColour conversion | |
1737 | if (!clr.Set(v)) | |
1738 | { | |
1739 | // the colour doesn't use #RRGGBB format, check if it is symbolic | |
1740 | // colour name: | |
1741 | clr = GetSystemColour(v); | |
1742 | if (clr.IsOk()) | |
1743 | return clr; | |
1744 | ||
1745 | ReportParamError | |
1746 | ( | |
1747 | param, | |
1748 | wxString::Format("incorrect colour specification \"%s\"", v) | |
1749 | ); | |
1750 | return wxNullColour; | |
1751 | } | |
1752 | ||
1753 | return clr; | |
1754 | } | |
1755 | ||
1756 | namespace | |
1757 | { | |
1758 | ||
1759 | // if 'param' has stock_id/stock_client, extracts them and returns true | |
1760 | bool 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); | |
1777 | ||
1778 | return true; | |
1779 | } | |
1780 | } | |
1781 | ||
1782 | return false; | |
1783 | } | |
1784 | ||
1785 | } // anonymous namespace | |
1786 | ||
1787 | wxBitmap wxXmlResourceHandlerImpl::GetBitmap(const wxString& param, | |
1788 | const wxArtClient& defaultArtClient, | |
1789 | wxSize size) | |
1790 | { | |
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); | |
1806 | } | |
1807 | ||
1808 | wxBitmap wxXmlResourceHandlerImpl::GetBitmap(const wxXmlNode* node, | |
1809 | const wxArtClient& defaultArtClient, | |
1810 | wxSize size) | |
1811 | { | |
1812 | wxCHECK_MSG( node, wxNullBitmap, "bitmap node can't be NULL" ); | |
1813 | ||
1814 | /* If the bitmap is specified as stock item, query wxArtProvider for it: */ | |
1815 | wxString art_id, art_client; | |
1816 | if ( GetStockArtAttrs(node, defaultArtClient, | |
1817 | art_id, art_client) ) | |
1818 | { | |
1819 | wxBitmap stockArt(wxArtProvider::GetBitmap(art_id, art_client, size)); | |
1820 | if ( stockArt.IsOk() ) | |
1821 | return stockArt; | |
1822 | } | |
1823 | ||
1824 | /* ...or load the bitmap from file: */ | |
1825 | wxString name = GetParamValue(node); | |
1826 | if (name.empty()) return wxNullBitmap; | |
1827 | #if wxUSE_FILESYSTEM | |
1828 | wxFSFile *fsfile = GetCurFileSystem().OpenFile(name, wxFS_READ | wxFS_SEEKABLE); | |
1829 | if (fsfile == NULL) | |
1830 | { | |
1831 | ReportParamError | |
1832 | ( | |
1833 | node->GetName(), | |
1834 | wxString::Format("cannot open bitmap resource \"%s\"", name) | |
1835 | ); | |
1836 | return wxNullBitmap; | |
1837 | } | |
1838 | wxImage img(*(fsfile->GetStream())); | |
1839 | delete fsfile; | |
1840 | #else | |
1841 | wxImage img(name); | |
1842 | #endif | |
1843 | ||
1844 | if (!img.IsOk()) | |
1845 | { | |
1846 | ReportParamError | |
1847 | ( | |
1848 | node->GetName(), | |
1849 | wxString::Format("cannot create bitmap from \"%s\"", name) | |
1850 | ); | |
1851 | return wxNullBitmap; | |
1852 | } | |
1853 | if (!(size == wxDefaultSize)) img.Rescale(size.x, size.y); | |
1854 | return wxBitmap(img); | |
1855 | } | |
1856 | ||
1857 | ||
1858 | wxIcon wxXmlResourceHandlerImpl::GetIcon(const wxString& param, | |
1859 | const wxArtClient& defaultArtClient, | |
1860 | wxSize size) | |
1861 | { | |
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); | |
1874 | } | |
1875 | ||
1876 | wxIcon wxXmlResourceHandlerImpl::GetIcon(const wxXmlNode* node, | |
1877 | const wxArtClient& defaultArtClient, | |
1878 | wxSize size) | |
1879 | { | |
1880 | wxIcon icon; | |
1881 | icon.CopyFromBitmap(GetBitmap(node, defaultArtClient, size)); | |
1882 | return icon; | |
1883 | } | |
1884 | ||
1885 | ||
1886 | wxIconBundle wxXmlResourceHandlerImpl::GetIconBundle(const wxString& param, | |
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 | } | |
1932 | ||
1933 | ||
1934 | wxImageList *wxXmlResourceHandlerImpl::GetImageList(const wxString& param) | |
1935 | { | |
1936 | wxXmlNode * const imagelist_node = GetParamNode(param); | |
1937 | if ( !imagelist_node ) | |
1938 | return NULL; | |
1939 | ||
1940 | wxXmlNode * const oldnode = m_handler->m_node; | |
1941 | m_handler->m_node = imagelist_node; | |
1942 | ||
1943 | // Get the size if we have it, otherwise we will use the size of the first | |
1944 | // list element. | |
1945 | wxSize size = GetSize(); | |
1946 | ||
1947 | // Start adding images, we'll create the image list when adding the first | |
1948 | // one. | |
1949 | wxImageList * imagelist = NULL; | |
1950 | wxString parambitmap = wxT("bitmap"); | |
1951 | if ( HasParam(parambitmap) ) | |
1952 | { | |
1953 | wxXmlNode *n = m_handler->m_node->GetChildren(); | |
1954 | while (n) | |
1955 | { | |
1956 | if (n->GetType() == wxXML_ELEMENT_NODE && n->GetName() == parambitmap) | |
1957 | { | |
1958 | wxIcon icon = GetIcon(n, wxART_OTHER, size); | |
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 | ||
1971 | // add icon instead of bitmap to keep the bitmap mask | |
1972 | imagelist->Add(icon); | |
1973 | } | |
1974 | n = n->GetNext(); | |
1975 | } | |
1976 | } | |
1977 | ||
1978 | m_handler->m_node = oldnode; | |
1979 | return imagelist; | |
1980 | } | |
1981 | ||
1982 | wxXmlNode *wxXmlResourceHandlerImpl::GetParamNode(const wxString& param) | |
1983 | { | |
1984 | wxCHECK_MSG(m_handler->m_node, NULL, wxT("You can't access handler data before it was initialized!")); | |
1985 | ||
1986 | wxXmlNode *n = m_handler->m_node->GetChildren(); | |
1987 | ||
1988 | while (n) | |
1989 | { | |
1990 | if (n->GetType() == wxXML_ELEMENT_NODE && n->GetName() == param) | |
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 | |
1998 | return n; | |
1999 | } | |
2000 | n = n->GetNext(); | |
2001 | } | |
2002 | return NULL; | |
2003 | } | |
2004 | ||
2005 | bool wxXmlResourceHandlerImpl::IsOfClass(wxXmlNode *node, const wxString& classname) const | |
2006 | { | |
2007 | return node->GetAttribute(wxT("class")) == classname; | |
2008 | } | |
2009 | ||
2010 | ||
2011 | ||
2012 | wxString wxXmlResourceHandlerImpl::GetNodeContent(const wxXmlNode *node) | |
2013 | { | |
2014 | const wxXmlNode *n = node; | |
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 | ||
2030 | wxString wxXmlResourceHandlerImpl::GetParamValue(const wxString& param) | |
2031 | { | |
2032 | if (param.empty()) | |
2033 | return GetNodeContent(m_handler->m_node); | |
2034 | else | |
2035 | return GetNodeContent(GetParamNode(param)); | |
2036 | } | |
2037 | ||
2038 | wxString wxXmlResourceHandlerImpl::GetParamValue(const wxXmlNode* node) | |
2039 | { | |
2040 | return GetNodeContent(node); | |
2041 | } | |
2042 | ||
2043 | ||
2044 | wxSize wxXmlResourceHandlerImpl::GetSize(const wxString& param, | |
2045 | wxWindow *windowToUse) | |
2046 | { | |
2047 | wxString s = GetParamValue(param); | |
2048 | if (s.empty()) s = wxT("-1,-1"); | |
2049 | bool is_dlg; | |
2050 | long sx, sy = 0; | |
2051 | ||
2052 | is_dlg = s[s.length()-1] == wxT('d'); | |
2053 | if (is_dlg) s.RemoveLast(); | |
2054 | ||
2055 | if (!s.BeforeFirst(wxT(',')).ToLong(&sx) || | |
2056 | !s.AfterLast(wxT(',')).ToLong(&sy)) | |
2057 | { | |
2058 | ReportParamError | |
2059 | ( | |
2060 | param, | |
2061 | wxString::Format("cannot parse coordinates value \"%s\"", s) | |
2062 | ); | |
2063 | return wxDefaultSize; | |
2064 | } | |
2065 | ||
2066 | if (is_dlg) | |
2067 | { | |
2068 | if (windowToUse) | |
2069 | { | |
2070 | return wxDLG_UNIT(windowToUse, wxSize(sx, sy)); | |
2071 | } | |
2072 | else if (m_handler->m_parentAsWindow) | |
2073 | { | |
2074 | return wxDLG_UNIT(m_handler->m_parentAsWindow, wxSize(sx, sy)); | |
2075 | } | |
2076 | else | |
2077 | { | |
2078 | ReportParamError | |
2079 | ( | |
2080 | param, | |
2081 | "cannot convert dialog units: dialog unknown" | |
2082 | ); | |
2083 | return wxDefaultSize; | |
2084 | } | |
2085 | } | |
2086 | ||
2087 | return wxSize(sx, sy); | |
2088 | } | |
2089 | ||
2090 | ||
2091 | ||
2092 | wxPoint wxXmlResourceHandlerImpl::GetPosition(const wxString& param) | |
2093 | { | |
2094 | wxSize sz = GetSize(param); | |
2095 | return wxPoint(sz.x, sz.y); | |
2096 | } | |
2097 | ||
2098 | ||
2099 | ||
2100 | wxCoord wxXmlResourceHandlerImpl::GetDimension(const wxString& param, | |
2101 | wxCoord defaultv, | |
2102 | wxWindow *windowToUse) | |
2103 | { | |
2104 | wxString s = GetParamValue(param); | |
2105 | if (s.empty()) return defaultv; | |
2106 | bool is_dlg; | |
2107 | long sx; | |
2108 | ||
2109 | is_dlg = s[s.length()-1] == wxT('d'); | |
2110 | if (is_dlg) s.RemoveLast(); | |
2111 | ||
2112 | if (!s.ToLong(&sx)) | |
2113 | { | |
2114 | ReportParamError | |
2115 | ( | |
2116 | param, | |
2117 | wxString::Format("cannot parse dimension value \"%s\"", s) | |
2118 | ); | |
2119 | return defaultv; | |
2120 | } | |
2121 | ||
2122 | if (is_dlg) | |
2123 | { | |
2124 | if (windowToUse) | |
2125 | { | |
2126 | return wxDLG_UNIT(windowToUse, wxSize(sx, 0)).x; | |
2127 | } | |
2128 | else if (m_handler->m_parentAsWindow) | |
2129 | { | |
2130 | return wxDLG_UNIT(m_handler->m_parentAsWindow, wxSize(sx, 0)).x; | |
2131 | } | |
2132 | else | |
2133 | { | |
2134 | ReportParamError | |
2135 | ( | |
2136 | param, | |
2137 | "cannot convert dialog units: dialog unknown" | |
2138 | ); | |
2139 | return defaultv; | |
2140 | } | |
2141 | } | |
2142 | ||
2143 | return sx; | |
2144 | } | |
2145 | ||
2146 | wxDirection | |
2147 | wxXmlResourceHandlerImpl::GetDirection(const wxString& param, wxDirection dirDefault) | |
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 | } | |
2180 | ||
2181 | // Get system font index using indexname | |
2182 | static wxFont GetSystemFont(const wxString& name) | |
2183 | { | |
2184 | if (!name.empty()) | |
2185 | { | |
2186 | #define SYSFNT(fnt) \ | |
2187 | if (name == wxT(#fnt)) return wxSystemSettings::GetFont(fnt); | |
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) | |
2193 | SYSFNT(wxSYS_SYSTEM_FIXED_FONT) | |
2194 | SYSFNT(wxSYS_DEFAULT_GUI_FONT) | |
2195 | #undef SYSFNT | |
2196 | } | |
2197 | ||
2198 | return wxNullFont; | |
2199 | } | |
2200 | ||
2201 | wxFont wxXmlResourceHandlerImpl::GetFont(const wxString& param, wxWindow* parent) | |
2202 | { | |
2203 | wxXmlNode *font_node = GetParamNode(param); | |
2204 | if (font_node == NULL) | |
2205 | { | |
2206 | ReportError( | |
2207 | wxString::Format("cannot find font node \"%s\"", param)); | |
2208 | return wxNullFont; | |
2209 | } | |
2210 | ||
2211 | wxXmlNode *oldnode = m_handler->m_node; | |
2212 | m_handler->m_node = font_node; | |
2213 | ||
2214 | // font attributes: | |
2215 | ||
2216 | // size | |
2217 | int isize = -1; | |
2218 | bool hasSize = HasParam(wxT("size")); | |
2219 | if (hasSize) | |
2220 | isize = GetLong(wxT("size"), -1); | |
2221 | ||
2222 | // style | |
2223 | int istyle = wxNORMAL; | |
2224 | bool hasStyle = HasParam(wxT("style")); | |
2225 | if (hasStyle) | |
2226 | { | |
2227 | wxString style = GetParamValue(wxT("style")); | |
2228 | if (style == wxT("italic")) | |
2229 | istyle = wxITALIC; | |
2230 | else if (style == wxT("slant")) | |
2231 | istyle = wxSLANT; | |
2232 | else if (style != wxT("normal")) | |
2233 | { | |
2234 | ReportParamError | |
2235 | ( | |
2236 | param, | |
2237 | wxString::Format("unknown font style \"%s\"", style) | |
2238 | ); | |
2239 | } | |
2240 | } | |
2241 | ||
2242 | // weight | |
2243 | int iweight = wxNORMAL; | |
2244 | bool hasWeight = HasParam(wxT("weight")); | |
2245 | if (hasWeight) | |
2246 | { | |
2247 | wxString weight = GetParamValue(wxT("weight")); | |
2248 | if (weight == wxT("bold")) | |
2249 | iweight = wxBOLD; | |
2250 | else if (weight == wxT("light")) | |
2251 | iweight = wxLIGHT; | |
2252 | else if (weight != wxT("normal")) | |
2253 | { | |
2254 | ReportParamError | |
2255 | ( | |
2256 | param, | |
2257 | wxString::Format("unknown font weight \"%s\"", weight) | |
2258 | ); | |
2259 | } | |
2260 | } | |
2261 | ||
2262 | // underline | |
2263 | bool hasUnderlined = HasParam(wxT("underlined")); | |
2264 | bool underlined = hasUnderlined ? GetBool(wxT("underlined"), false) : false; | |
2265 | ||
2266 | // family and facename | |
2267 | int ifamily = wxDEFAULT; | |
2268 | bool hasFamily = HasParam(wxT("family")); | |
2269 | if (hasFamily) | |
2270 | { | |
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; | |
2278 | else | |
2279 | { | |
2280 | ReportParamError | |
2281 | ( | |
2282 | param, | |
2283 | wxString::Format("unknown font family \"%s\"", family) | |
2284 | ); | |
2285 | } | |
2286 | } | |
2287 | ||
2288 | ||
2289 | wxString facename; | |
2290 | bool hasFacename = HasParam(wxT("face")); | |
2291 | if (hasFacename) | |
2292 | { | |
2293 | wxString faces = GetParamValue(wxT("face")); | |
2294 | wxStringTokenizer tk(faces, wxT(",")); | |
2295 | #if wxUSE_FONTENUM | |
2296 | wxArrayString facenames(wxFontEnumerator::GetFacenames()); | |
2297 | while (tk.HasMoreTokens()) | |
2298 | { | |
2299 | int index = facenames.Index(tk.GetNextToken(), false); | |
2300 | if (index != wxNOT_FOUND) | |
2301 | { | |
2302 | facename = facenames[index]; | |
2303 | break; | |
2304 | } | |
2305 | } | |
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 | |
2311 | } | |
2312 | ||
2313 | // encoding | |
2314 | wxFontEncoding enc = wxFONTENCODING_DEFAULT; | |
2315 | bool hasEncoding = HasParam(wxT("encoding")); | |
2316 | #if wxUSE_FONTMAP | |
2317 | if (hasEncoding) | |
2318 | { | |
2319 | wxString encoding = GetParamValue(wxT("encoding")); | |
2320 | wxFontMapper mapper; | |
2321 | if (!encoding.empty()) | |
2322 | enc = mapper.CharsetToEncoding(encoding); | |
2323 | if (enc == wxFONTENCODING_SYSTEM) | |
2324 | enc = wxFONTENCODING_DEFAULT; | |
2325 | } | |
2326 | #endif // wxUSE_FONTMAP | |
2327 | ||
2328 | wxFont font; | |
2329 | ||
2330 | // is this font based on a system font? | |
2331 | if (HasParam(wxT("sysfont"))) | |
2332 | { | |
2333 | font = GetSystemFont(GetParamValue(wxT("sysfont"))); | |
2334 | if (HasParam(wxT("inherit"))) | |
2335 | { | |
2336 | ReportParamError | |
2337 | ( | |
2338 | param, | |
2339 | "double specification of \"sysfont\" and \"inherit\"" | |
2340 | ); | |
2341 | } | |
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 | |
2349 | { | |
2350 | ReportParamError | |
2351 | ( | |
2352 | param, | |
2353 | "no parent window specified to derive the font from" | |
2354 | ); | |
2355 | } | |
2356 | } | |
2357 | ||
2358 | if (font.IsOk()) | |
2359 | { | |
2360 | if (hasSize && isize != -1) | |
2361 | { | |
2362 | font.SetPointSize(isize); | |
2363 | if (HasParam(wxT("relativesize"))) | |
2364 | { | |
2365 | ReportParamError | |
2366 | ( | |
2367 | param, | |
2368 | "double specification of \"size\" and \"relativesize\"" | |
2369 | ); | |
2370 | } | |
2371 | } | |
2372 | else if (HasParam(wxT("relativesize"))) | |
2373 | font.SetPointSize(int(font.GetPointSize() * | |
2374 | GetFloat(wxT("relativesize")))); | |
2375 | ||
2376 | if (hasStyle) | |
2377 | font.SetStyle(istyle); | |
2378 | if (hasWeight) | |
2379 | font.SetWeight(iweight); | |
2380 | if (hasUnderlined) | |
2381 | font.SetUnderlined(underlined); | |
2382 | if (hasFamily) | |
2383 | font.SetFamily(ifamily); | |
2384 | if (hasFacename) | |
2385 | font.SetFaceName(facename); | |
2386 | if (hasEncoding) | |
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); | |
2394 | } | |
2395 | ||
2396 | m_handler->m_node = oldnode; | |
2397 | return font; | |
2398 | } | |
2399 | ||
2400 | ||
2401 | void wxXmlResourceHandlerImpl::SetupWindow(wxWindow *wnd) | |
2402 | { | |
2403 | //FIXME : add cursor | |
2404 | ||
2405 | if (HasParam(wxT("exstyle"))) | |
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"))); | |
2410 | if (HasParam(wxT("bg"))) | |
2411 | wnd->SetBackgroundColour(GetColour(wxT("bg"))); | |
2412 | if (HasParam(wxT("ownbg"))) | |
2413 | wnd->SetOwnBackgroundColour(GetColour(wxT("ownbg"))); | |
2414 | if (HasParam(wxT("fg"))) | |
2415 | wnd->SetForegroundColour(GetColour(wxT("fg"))); | |
2416 | if (HasParam(wxT("ownfg"))) | |
2417 | wnd->SetOwnForegroundColour(GetColour(wxT("ownfg"))); | |
2418 | if (GetBool(wxT("enabled"), 1) == 0) | |
2419 | wnd->Enable(false); | |
2420 | if (GetBool(wxT("focused"), 0) == 1) | |
2421 | wnd->SetFocus(); | |
2422 | if (GetBool(wxT("hidden"), 0) == 1) | |
2423 | wnd->Show(false); | |
2424 | #if wxUSE_TOOLTIPS | |
2425 | if (HasParam(wxT("tooltip"))) | |
2426 | wnd->SetToolTip(GetText(wxT("tooltip"))); | |
2427 | #endif | |
2428 | if (HasParam(wxT("font"))) | |
2429 | wnd->SetFont(GetFont(wxT("font"), wnd)); | |
2430 | if (HasParam(wxT("ownfont"))) | |
2431 | wnd->SetOwnFont(GetFont(wxT("ownfont"), wnd)); | |
2432 | if (HasParam(wxT("help"))) | |
2433 | wnd->SetHelpText(GetText(wxT("help"))); | |
2434 | } | |
2435 | ||
2436 | ||
2437 | void wxXmlResourceHandlerImpl::CreateChildren(wxObject *parent, bool this_hnd_only) | |
2438 | { | |
2439 | for ( wxXmlNode *n = m_handler->m_node->GetChildren(); n; n = n->GetNext() ) | |
2440 | { | |
2441 | if ( IsObjectNode(n) ) | |
2442 | { | |
2443 | m_handler->m_resource->DoCreateResFromNode(*n, parent, NULL, | |
2444 | this_hnd_only ? this->GetHandler() : NULL); | |
2445 | } | |
2446 | } | |
2447 | } | |
2448 | ||
2449 | ||
2450 | void wxXmlResourceHandlerImpl::CreateChildrenPrivately(wxObject *parent, wxXmlNode *rootnode) | |
2451 | { | |
2452 | wxXmlNode *root; | |
2453 | if (rootnode == NULL) root = m_handler->m_node; else root = rootnode; | |
2454 | wxXmlNode *n = root->GetChildren(); | |
2455 | ||
2456 | while (n) | |
2457 | { | |
2458 | if (n->GetType() == wxXML_ELEMENT_NODE && GetHandler()->CanHandle(n)) | |
2459 | { | |
2460 | CreateResource(n, parent, NULL); | |
2461 | } | |
2462 | n = n->GetNext(); | |
2463 | } | |
2464 | } | |
2465 | ||
2466 | ||
2467 | //----------------------------------------------------------------------------- | |
2468 | // errors reporting | |
2469 | //----------------------------------------------------------------------------- | |
2470 | ||
2471 | void wxXmlResourceHandlerImpl::ReportError(const wxString& message) | |
2472 | { | |
2473 | m_handler->m_resource->ReportError(m_handler->m_node, message); | |
2474 | } | |
2475 | ||
2476 | void wxXmlResourceHandlerImpl::ReportError(wxXmlNode *context, | |
2477 | const wxString& message) | |
2478 | { | |
2479 | m_handler->m_resource->ReportError(context ? context : m_handler->m_node, message); | |
2480 | } | |
2481 | ||
2482 | void wxXmlResourceHandlerImpl::ReportParamError(const wxString& param, | |
2483 | const wxString& message) | |
2484 | { | |
2485 | m_handler->m_resource->ReportError(GetParamNode(param), message); | |
2486 | } | |
2487 | ||
2488 | void wxXmlResource::ReportError(const wxXmlNode *context, const wxString& message) | |
2489 | { | |
2490 | if ( !context ) | |
2491 | { | |
2492 | DoReportError("", NULL, message); | |
2493 | return; | |
2494 | } | |
2495 | ||
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()); | |
2500 | ||
2501 | DoReportError(filename, context, message); | |
2502 | } | |
2503 | ||
2504 | void wxXmlResource::DoReportError(const wxString& xrcFile, const wxXmlNode *position, | |
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 | } | |
2519 | ||
2520 | ||
2521 | //----------------------------------------------------------------------------- | |
2522 | // XRCID implementation | |
2523 | //----------------------------------------------------------------------------- | |
2524 | ||
2525 | #define XRCID_TABLE_SIZE 1024 | |
2526 | ||
2527 | ||
2528 | struct XRCID_record | |
2529 | { | |
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; | |
2534 | char *key; | |
2535 | XRCID_record *next; | |
2536 | }; | |
2537 | ||
2538 | static XRCID_record *XRCID_Records[XRCID_TABLE_SIZE] = {NULL}; | |
2539 | ||
2540 | // Extremely simplistic hash function which probably ought to be replaced with | |
2541 | // wxStringHash::stringHash(). | |
2542 | static inline unsigned XRCIdHash(const char *str_id) | |
2543 | { | |
2544 | unsigned index = 0; | |
2545 | ||
2546 | for (const char *c = str_id; *c != '\0'; c++) index += (unsigned int)*c; | |
2547 | index %= XRCID_TABLE_SIZE; | |
2548 | ||
2549 | return index; | |
2550 | } | |
2551 | ||
2552 | static void XRCID_Assign(const wxString& str_id, int value) | |
2553 | { | |
2554 | const wxCharBuffer buf_id(str_id.mb_str()); | |
2555 | const unsigned index = XRCIdHash(buf_id); | |
2556 | ||
2557 | ||
2558 | XRCID_record *oldrec = NULL; | |
2559 | for (XRCID_record *rec = XRCID_Records[index]; rec; rec = rec->next) | |
2560 | { | |
2561 | if (wxStrcmp(rec->key, buf_id) == 0) | |
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 | ||
2577 | static int XRCID_Lookup(const char *str_id, int value_if_not_found = wxID_NONE) | |
2578 | { | |
2579 | const unsigned index = XRCIdHash(str_id); | |
2580 | ||
2581 | ||
2582 | XRCID_record *oldrec = NULL; | |
2583 | for (XRCID_record *rec = XRCID_Records[index]; rec; rec = rec->next) | |
2584 | { | |
2585 | if (wxStrcmp(rec->key, str_id) == 0) | |
2586 | { | |
2587 | return rec->id; | |
2588 | } | |
2589 | oldrec = rec; | |
2590 | } | |
2591 | ||
2592 | XRCID_record **rec_var = (oldrec == NULL) ? | |
2593 | &XRCID_Records[index] : &oldrec->next; | |
2594 | *rec_var = new XRCID_record; | |
2595 | (*rec_var)->key = wxStrdup(str_id); | |
2596 | (*rec_var)->next = NULL; | |
2597 | ||
2598 | char *end; | |
2599 | if (value_if_not_found != wxID_NONE) | |
2600 | (*rec_var)->id = value_if_not_found; | |
2601 | else | |
2602 | { | |
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 | { | |
2611 | (*rec_var)->id = wxWindowBase::NewControlId(); | |
2612 | } | |
2613 | } | |
2614 | ||
2615 | return (*rec_var)->id; | |
2616 | } | |
2617 | ||
2618 | namespace | |
2619 | { | |
2620 | ||
2621 | // flag indicating whether standard XRC ids were already initialized | |
2622 | static bool gs_stdIDsAdded = false; | |
2623 | ||
2624 | void AddStdXRCID_Records() | |
2625 | { | |
2626 | #define stdID(id) XRCID_Lookup(#id, id) | |
2627 | stdID(-1); | |
2628 | ||
2629 | stdID(wxID_ANY); | |
2630 | stdID(wxID_SEPARATOR); | |
2631 | ||
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); | |
2644 | stdID(wxID_PAGE_SETUP); | |
2645 | stdID(wxID_PREVIEW); | |
2646 | stdID(wxID_ABOUT); | |
2647 | stdID(wxID_HELP_CONTENTS); | |
2648 | stdID(wxID_HELP_INDEX), | |
2649 | stdID(wxID_HELP_SEARCH), | |
2650 | stdID(wxID_HELP_COMMANDS); | |
2651 | stdID(wxID_HELP_PROCEDURES); | |
2652 | stdID(wxID_HELP_CONTEXT); | |
2653 | stdID(wxID_CLOSE_ALL); | |
2654 | stdID(wxID_PREFERENCES); | |
2655 | ||
2656 | stdID(wxID_EDIT); | |
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); | |
2668 | ||
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); | |
2677 | ||
2678 | ||
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); | |
2688 | ||
2689 | ||
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); | |
2710 | ||
2711 | stdID(wxID_UP); | |
2712 | stdID(wxID_DOWN); | |
2713 | stdID(wxID_HOME); | |
2714 | stdID(wxID_REFRESH); | |
2715 | stdID(wxID_STOP); | |
2716 | stdID(wxID_INDEX); | |
2717 | ||
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); | |
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); | |
2751 | ||
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); | |
2769 | #undef stdID | |
2770 | } | |
2771 | ||
2772 | } // anonymous namespace | |
2773 | ||
2774 | ||
2775 | /*static*/ | |
2776 | int 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 */ | |
2788 | wxString 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 | ||
2802 | static 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 | ||
2813 | static 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 | } | |
2823 | ||
2824 | ||
2825 | //----------------------------------------------------------------------------- | |
2826 | // module and globals | |
2827 | //----------------------------------------------------------------------------- | |
2828 | ||
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 | |
2835 | static struct wxXRCStaticCleanup | |
2836 | { | |
2837 | ~wxXRCStaticCleanup() { CleanXRCID_Records(); } | |
2838 | } s_staticCleanup; | |
2839 | ||
2840 | class wxXmlResourceModule: public wxModule | |
2841 | { | |
2842 | DECLARE_DYNAMIC_CLASS(wxXmlResourceModule) | |
2843 | public: | |
2844 | wxXmlResourceModule() {} | |
2845 | bool OnInit() | |
2846 | { | |
2847 | wxXmlResource::AddSubclassFactory(new wxXmlSubclassFactoryCXX); | |
2848 | return true; | |
2849 | } | |
2850 | void OnExit() | |
2851 | { | |
2852 | delete wxXmlResource::Set(NULL); | |
2853 | delete wxIdRangeManager::Set(NULL); | |
2854 | if(wxXmlResource::ms_subclassFactories) | |
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 | } | |
2863 | CleanXRCID_Records(); | |
2864 | } | |
2865 | }; | |
2866 | ||
2867 | IMPLEMENT_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. | |
2872 | void wxXmlInitResourceModule() | |
2873 | { | |
2874 | wxModule* module = new wxXmlResourceModule; | |
2875 | module->Init(); | |
2876 | wxModule::RegisterModule(module); | |
2877 | } | |
2878 | ||
2879 | #endif // wxUSE_XRC |