]> git.saurik.com Git - wxWidgets.git/blame - src/xrc/xmlres.cpp
determine wxFSFile's MIME type when wxFSFile::GetMimeType() is called instead of...
[wxWidgets.git] / src / xrc / xmlres.cpp
CommitLineData
78d14f80 1/////////////////////////////////////////////////////////////////////////////
88a7a4e1 2// Name: src/xrc/xmlres.cpp
b5d6954b 3// Purpose: XRC resources
78d14f80
VS
4// Author: Vaclav Slavik
5// Created: 2000/03/05
6// RCS-ID: $Id$
7// Copyright: (c) 2000 Vaclav Slavik
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
78d14f80
VS
11// For compilers that support precompilation, includes "wx.h".
12#include "wx/wxprec.h"
13
14#ifdef __BORLANDC__
15 #pragma hdrstop
16#endif
17
621be1ec 18#if wxUSE_XRC
a1e4ec87 19
88a7a4e1
WS
20#include "wx/xrc/xmlres.h"
21
22#ifndef WX_PRECOMP
23 #include "wx/intl.h"
e4db172a 24 #include "wx/log.h"
8e609c82 25 #include "wx/panel.h"
76b49cf4 26 #include "wx/frame.h"
fdf565fe 27 #include "wx/dialog.h"
9eddec69 28 #include "wx/settings.h"
0bca0373 29 #include "wx/bitmap.h"
155ecd4c 30 #include "wx/image.h"
02761f6c 31 #include "wx/module.h"
193d0c93 32 #include "wx/wxcrtvararg.h"
88a7a4e1
WS
33#endif
34
e7a3a5a5 35#ifndef __WXWINCE__
88a7a4e1 36 #include <locale.h>
e7a3a5a5 37#endif
1df61962 38
78d14f80
VS
39#include "wx/wfstream.h"
40#include "wx/filesys.h"
317a0d73 41#include "wx/filename.h"
78d14f80
VS
42#include "wx/tokenzr.h"
43#include "wx/fontenum.h"
78d14f80 44#include "wx/fontmap.h"
af1337b0 45#include "wx/artprov.h"
78d14f80 46
34c6bbee 47#include "wx/xml/xml.h"
78d14f80
VS
48
49#include "wx/arrimpl.cpp"
17a1ebd1 50WX_DEFINE_OBJARRAY(wxXmlResourceDataRecords)
78d14f80
VS
51
52
824e8eaa
VS
53wxXmlResource *wxXmlResource::ms_instance = NULL;
54
55/*static*/ wxXmlResource *wxXmlResource::Get()
56{
57 if ( !ms_instance )
58 ms_instance = new wxXmlResource;
59 return ms_instance;
60}
61
62/*static*/ wxXmlResource *wxXmlResource::Set(wxXmlResource *res)
63{
64 wxXmlResource *old = ms_instance;
65 ms_instance = res;
66 return old;
67}
68
d4a724d4 69wxXmlResource::wxXmlResource(int flags, const wxString& domain)
78d14f80 70{
daa85ee3 71 m_flags = flags;
78d14f80 72 m_version = -1;
d7a80cf5 73 SetDomain(domain);
78d14f80
VS
74}
75
d4a724d4 76wxXmlResource::wxXmlResource(const wxString& filemask, int flags, const wxString& domain)
78d14f80 77{
daa85ee3 78 m_flags = flags;
78d14f80 79 m_version = -1;
d7a80cf5 80 SetDomain(domain);
78d14f80
VS
81 Load(filemask);
82}
83
84wxXmlResource::~wxXmlResource()
85{
86 ClearHandlers();
87}
88
d7a80cf5 89void wxXmlResource::SetDomain(const wxString& domain)
d4a724d4 90{
d7a80cf5 91 m_domain = domain;
d4a724d4
RD
92}
93
78d14f80 94
60fd818a
VZ
95/* static */
96wxString wxXmlResource::ConvertFileNameToURL(const wxString& filename)
97{
98 wxString fnd(filename);
99
100 // NB: as Load() and Unload() accept both filenames and URLs (should
101 // probably be changed to filenames only, but embedded resources
102 // currently rely on its ability to handle URLs - FIXME) we need to
103 // determine whether found name is filename and not URL and this is the
104 // fastest/simplest way to do it
105 if (wxFileName::FileExists(fnd))
106 {
107 // Make the name absolute filename, because the app may
108 // change working directory later:
109 wxFileName fn(fnd);
110 if (fn.IsRelative())
111 {
112 fn.MakeAbsolute();
113 fnd = fn.GetFullPath();
114 }
115#if wxUSE_FILESYSTEM
116 fnd = wxFileSystem::FileNameToURL(fnd);
117#endif
118 }
119
120 return fnd;
121}
122
123#if wxUSE_FILESYSTEM
124
125/* static */
126bool wxXmlResource::IsArchive(const wxString& filename)
127{
128 const wxString fnd = filename.Lower();
129
130 return fnd.Matches(wxT("*.zip")) || fnd.Matches(wxT("*.xrs"));
131}
132
133#endif // wxUSE_FILESYSTEM
134
78d14f80
VS
135bool wxXmlResource::Load(const wxString& filemask)
136{
137 wxString fnd;
138 wxXmlResourceDataRecord *drec;
139 bool iswild = wxIsWild(filemask);
f80ea77b 140 bool rt = true;
78d14f80
VS
141
142#if wxUSE_FILESYSTEM
143 wxFileSystem fsys;
144# define wxXmlFindFirst fsys.FindFirst(filemask, wxFILE)
145# define wxXmlFindNext fsys.FindNext()
146#else
147# define wxXmlFindFirst wxFindFirstFile(filemask, wxFILE)
148# define wxXmlFindNext wxFindNextFile()
149#endif
150 if (iswild)
151 fnd = wxXmlFindFirst;
152 else
153 fnd = filemask;
ec157c8f 154 while (!fnd.empty())
78d14f80 155 {
60fd818a 156 fnd = ConvertFileNameToURL(fnd);
b380439d 157
78d14f80 158#if wxUSE_FILESYSTEM
60fd818a 159 if ( IsArchive(fnd) )
78d14f80 160 {
f9d243a3 161 rt = rt && Load(fnd + wxT("#zip:*.xrc"));
78d14f80 162 }
60fd818a
VZ
163 else // a single resource URL
164#endif // wxUSE_FILESYSTEM
78d14f80
VS
165 {
166 drec = new wxXmlResourceDataRecord;
167 drec->File = fnd;
168 m_data.Add(drec);
169 }
170
171 if (iswild)
172 fnd = wxXmlFindNext;
173 else
174 fnd = wxEmptyString;
175 }
176# undef wxXmlFindFirst
177# undef wxXmlFindNext
d614f51b 178 return rt && UpdateResources();
78d14f80
VS
179}
180
60fd818a
VZ
181bool wxXmlResource::Unload(const wxString& filename)
182{
183 wxASSERT_MSG( !wxIsWild(filename),
184 _T("wildcards not supported by wxXmlResource::Unload()") );
185
186 wxString fnd = ConvertFileNameToURL(filename);
187#if wxUSE_FILESYSTEM
188 const bool isArchive = IsArchive(fnd);
189 if ( isArchive )
190 fnd += _T("#zip:");
191#endif // wxUSE_FILESYSTEM
192
193 bool unloaded = false;
194 const size_t count = m_data.GetCount();
195 for ( size_t i = 0; i < count; i++ )
196 {
197#if wxUSE_FILESYSTEM
198 if ( isArchive )
199 {
200 if ( m_data[i].File.StartsWith(fnd) )
201 unloaded = true;
202 // don't break from the loop, we can have other matching files
203 }
204 else // a single resource URL
205#endif // wxUSE_FILESYSTEM
206 {
207 if ( m_data[i].File == fnd )
208 {
209 m_data.RemoveAt(i);
210 unloaded = true;
211
212 // no sense in continuing, there is only one file with this URL
213 break;
214 }
215 }
216 }
217
218 return unloaded;
219}
220
78d14f80 221
854e189f 222IMPLEMENT_ABSTRACT_CLASS(wxXmlResourceHandler, wxObject)
78d14f80
VS
223
224void wxXmlResource::AddHandler(wxXmlResourceHandler *handler)
225{
8e8a4e85 226 m_handlers.Append(handler);
78d14f80
VS
227 handler->SetParentResource(this);
228}
229
92e898b0
RD
230void wxXmlResource::InsertHandler(wxXmlResourceHandler *handler)
231{
8e8a4e85 232 m_handlers.Insert(handler);
92e898b0
RD
233 handler->SetParentResource(this);
234}
235
78d14f80
VS
236
237
238void wxXmlResource::ClearHandlers()
239{
8e8a4e85 240 WX_CLEAR_LIST(wxList, m_handlers);
78d14f80
VS
241}
242
243
78d14f80
VS
244wxMenu *wxXmlResource::LoadMenu(const wxString& name)
245{
246 return (wxMenu*)CreateResFromNode(FindResource(name, wxT("wxMenu")), NULL, NULL);
247}
248
249
250
4a1b9596 251wxMenuBar *wxXmlResource::LoadMenuBar(wxWindow *parent, const wxString& name)
78d14f80 252{
4a1b9596 253 return (wxMenuBar*)CreateResFromNode(FindResource(name, wxT("wxMenuBar")), parent, NULL);
78d14f80
VS
254}
255
256
257
4a1b9596 258#if wxUSE_TOOLBAR
78d14f80
VS
259wxToolBar *wxXmlResource::LoadToolBar(wxWindow *parent, const wxString& name)
260{
261 return (wxToolBar*)CreateResFromNode(FindResource(name, wxT("wxToolBar")), parent, NULL);
262}
4a1b9596 263#endif
78d14f80
VS
264
265
266wxDialog *wxXmlResource::LoadDialog(wxWindow *parent, const wxString& name)
267{
4dd75a6a 268 return (wxDialog*)CreateResFromNode(FindResource(name, wxT("wxDialog")), parent, NULL);
78d14f80
VS
269}
270
271bool wxXmlResource::LoadDialog(wxDialog *dlg, wxWindow *parent, const wxString& name)
272{
273 return CreateResFromNode(FindResource(name, wxT("wxDialog")), parent, dlg) != NULL;
274}
275
276
277
278wxPanel *wxXmlResource::LoadPanel(wxWindow *parent, const wxString& name)
279{
280 return (wxPanel*)CreateResFromNode(FindResource(name, wxT("wxPanel")), parent, NULL);
281}
282
283bool wxXmlResource::LoadPanel(wxPanel *panel, wxWindow *parent, const wxString& name)
284{
285 return CreateResFromNode(FindResource(name, wxT("wxPanel")), parent, panel) != NULL;
286}
287
92e898b0
RD
288wxFrame *wxXmlResource::LoadFrame(wxWindow* parent, const wxString& name)
289{
290 return (wxFrame*)CreateResFromNode(FindResource(name, wxT("wxFrame")), parent, NULL);
291}
292
78d14f80
VS
293bool wxXmlResource::LoadFrame(wxFrame* frame, wxWindow *parent, const wxString& name)
294{
295 return CreateResFromNode(FindResource(name, wxT("wxFrame")), parent, frame) != NULL;
296}
297
298wxBitmap wxXmlResource::LoadBitmap(const wxString& name)
299{
300 wxBitmap *bmp = (wxBitmap*)CreateResFromNode(
301 FindResource(name, wxT("wxBitmap")), NULL, NULL);
302 wxBitmap rt;
303
304 if (bmp) { rt = *bmp; delete bmp; }
305 return rt;
306}
307
308wxIcon wxXmlResource::LoadIcon(const wxString& name)
309{
310 wxIcon *icon = (wxIcon*)CreateResFromNode(
311 FindResource(name, wxT("wxIcon")), NULL, NULL);
312 wxIcon rt;
313
314 if (icon) { rt = *icon; delete icon; }
315 return rt;
316}
317
92e898b0
RD
318
319wxObject *wxXmlResource::LoadObject(wxWindow *parent, const wxString& name, const wxString& classname)
320{
321 return CreateResFromNode(FindResource(name, classname), parent, NULL);
322}
323
324bool wxXmlResource::LoadObject(wxObject *instance, wxWindow *parent, const wxString& name, const wxString& classname)
325{
326 return CreateResFromNode(FindResource(name, classname), parent, instance) != NULL;
327}
328
329
78d14f80
VS
330bool wxXmlResource::AttachUnknownControl(const wxString& name,
331 wxWindow *control, wxWindow *parent)
332{
333 if (parent == NULL)
334 parent = control->GetParent();
335 wxWindow *container = parent->FindWindow(name + wxT("_container"));
336 if (!container)
337 {
338 wxLogError(_("Cannot find container for unknown control '%s'."), name.c_str());
f80ea77b 339 return false;
78d14f80
VS
340 }
341 return control->Reparent(container);
342}
343
344
77b2f9b1 345static void ProcessPlatformProperty(wxXmlNode *node)
78d14f80
VS
346{
347 wxString s;
348 bool isok;
349
350 wxXmlNode *c = node->GetChildren();
351 while (c)
352 {
f80ea77b 353 isok = false;
78d14f80 354 if (!c->GetPropVal(wxT("platform"), &s))
f80ea77b 355 isok = true;
78d14f80
VS
356 else
357 {
2b5f62a0 358 wxStringTokenizer tkn(s, wxT(" |"));
78d14f80
VS
359
360 while (tkn.HasMoreTokens())
361 {
362 s = tkn.GetNextToken();
84389518 363#ifdef __WINDOWS__
d003330c
VS
364 if (s == wxT("win")) isok = true;
365#endif
b380439d 366#if defined(__MAC__) || defined(__APPLE__)
d003330c 367 if (s == wxT("mac")) isok = true;
b380439d
RD
368#elif defined(__UNIX__)
369 if (s == wxT("unix")) isok = true;
78d14f80 370#endif
d003330c
VS
371#ifdef __OS2__
372 if (s == wxT("os2")) isok = true;
373#endif
374
375 if (isok)
376 break;
78d14f80
VS
377 }
378 }
379
380 if (isok)
d7b1d73c 381 {
78d14f80 382 ProcessPlatformProperty(c);
d7b1d73c
VS
383 c = c->GetNext();
384 }
78d14f80
VS
385 else
386 {
d7b1d73c 387 wxXmlNode *c2 = c->GetNext();
db59a97c 388 node->RemoveChild(c);
78d14f80 389 delete c;
d7b1d73c 390 c = c2;
78d14f80 391 }
78d14f80
VS
392 }
393}
394
395
396
d614f51b 397bool wxXmlResource::UpdateResources()
78d14f80 398{
d614f51b 399 bool rt = true;
78d14f80
VS
400 bool modif;
401# if wxUSE_FILESYSTEM
402 wxFSFile *file = NULL;
19d0f58d 403 wxUnusedVar(file);
78d14f80
VS
404 wxFileSystem fsys;
405# endif
406
480505bc
VS
407 wxString encoding(wxT("UTF-8"));
408#if !wxUSE_UNICODE && wxUSE_INTL
409 if ( (GetFlags() & wxXRC_USE_LOCALE) == 0 )
410 {
75d38380
VS
411 // In case we are not using wxLocale to translate strings, convert the
412 // strings GUI's charset. This must not be done when wxXRC_USE_LOCALE
413 // is on, because it could break wxGetTranslation lookup.
480505bc
VS
414 encoding = wxLocale::GetSystemEncodingName();
415 }
416#endif
417
78d14f80
VS
418 for (size_t i = 0; i < m_data.GetCount(); i++)
419 {
420 modif = (m_data[i].Doc == NULL);
421
648db587 422 if (!modif && !(m_flags & wxXRC_NO_RELOADING))
78d14f80
VS
423 {
424# if wxUSE_FILESYSTEM
425 file = fsys.OpenFile(m_data[i].File);
34af0de4 426# if wxUSE_DATETIME
78d14f80 427 modif = file && file->GetModificationTime() > m_data[i].Time;
34af0de4
VZ
428# else // wxUSE_DATETIME
429 modif = true;
430# endif // wxUSE_DATETIME
78d14f80 431 if (!file)
d614f51b 432 {
78d14f80 433 wxLogError(_("Cannot open file '%s'."), m_data[i].File.c_str());
d614f51b
VS
434 rt = false;
435 }
78d14f80 436 wxDELETE(file);
19d0f58d 437 wxUnusedVar(file);
34af0de4
VZ
438# else // wxUSE_FILESYSTEM
439# if wxUSE_DATETIME
78d14f80 440 modif = wxDateTime(wxFileModificationTime(m_data[i].File)) > m_data[i].Time;
34af0de4
VZ
441# else // wxUSE_DATETIME
442 modif = true;
443# endif // wxUSE_DATETIME
444# endif // wxUSE_FILESYSTEM
78d14f80
VS
445 }
446
447 if (modif)
448 {
648db587
VS
449 wxLogTrace(_T("xrc"),
450 _T("opening file '%s'"), m_data[i].File.c_str());
451
480505bc 452 wxInputStream *stream = NULL;
78d14f80
VS
453
454# if wxUSE_FILESYSTEM
455 file = fsys.OpenFile(m_data[i].File);
f80ea77b
WS
456 if (file)
457 stream = file->GetStream();
78d14f80
VS
458# else
459 stream = new wxFileInputStream(m_data[i].File);
460# endif
461
462 if (stream)
463 {
464 delete m_data[i].Doc;
465 m_data[i].Doc = new wxXmlDocument;
466 }
4d876ee3 467 if (!stream || !m_data[i].Doc->Load(*stream, encoding))
78d14f80 468 {
480505bc
VS
469 wxLogError(_("Cannot load resources from file '%s'."),
470 m_data[i].File.c_str());
78d14f80 471 wxDELETE(m_data[i].Doc);
d614f51b 472 rt = false;
78d14f80
VS
473 }
474 else if (m_data[i].Doc->GetRoot()->GetName() != wxT("resource"))
475 {
b5d6954b 476 wxLogError(_("Invalid XRC resource '%s': doesn't have root node 'resource'."), m_data[i].File.c_str());
78d14f80 477 wxDELETE(m_data[i].Doc);
d614f51b 478 rt = false;
78d14f80
VS
479 }
480 else
f80ea77b 481 {
78d14f80
VS
482 long version;
483 int v1, v2, v3, v4;
484 wxString verstr = m_data[i].Doc->GetRoot()->GetPropVal(
485 wxT("version"), wxT("0.0.0.0"));
486 if (wxSscanf(verstr.c_str(), wxT("%i.%i.%i.%i"),
487 &v1, &v2, &v3, &v4) == 4)
488 version = v1*256*256*256+v2*256*256+v3*256+v4;
489 else
490 version = 0;
491 if (m_version == -1)
492 m_version = version;
493 if (m_version != version)
d614f51b 494 {
78d14f80 495 wxLogError(_("Resource files must have same version number!"));
d614f51b
VS
496 rt = false;
497 }
78d14f80
VS
498
499 ProcessPlatformProperty(m_data[i].Doc->GetRoot());
34af0de4 500#if wxUSE_DATETIME
496f0a58 501#if wxUSE_FILESYSTEM
f80ea77b 502 m_data[i].Time = file->GetModificationTime();
34af0de4 503#else // wxUSE_FILESYSTEM
496f0a58 504 m_data[i].Time = wxDateTime(wxFileModificationTime(m_data[i].File));
34af0de4
VZ
505#endif // wxUSE_FILESYSTEM
506#endif // wxUSE_DATETIME
f80ea77b 507 }
78d14f80
VS
508
509# if wxUSE_FILESYSTEM
f80ea77b
WS
510 wxDELETE(file);
511 wxUnusedVar(file);
78d14f80 512# else
f80ea77b 513 wxDELETE(stream);
78d14f80
VS
514# endif
515 }
516 }
d614f51b
VS
517
518 return rt;
78d14f80
VS
519}
520
521
b272b6dc
RD
522wxXmlNode *wxXmlResource::DoFindResource(wxXmlNode *parent,
523 const wxString& name,
524 const wxString& classname,
47793ab8
VS
525 bool recursive)
526{
527 wxString dummy;
528 wxXmlNode *node;
529
530 // first search for match at the top-level nodes (as this is
531 // where the resource is most commonly looked for):
532 for (node = parent->GetChildren(); node; node = node->GetNext())
533 {
b272b6dc
RD
534 if ( node->GetType() == wxXML_ELEMENT_NODE &&
535 (node->GetName() == wxT("object") ||
47793ab8 536 node->GetName() == wxT("object_ref")) &&
2b5f62a0
VZ
537 node->GetPropVal(wxT("name"), &dummy) && dummy == name )
538 {
539 wxString cls(node->GetPropVal(wxT("class"), wxEmptyString));
540 if (!classname || cls == classname)
541 return node;
542 // object_ref may not have 'class' property:
543 if (cls.empty() && node->GetName() == wxT("object_ref"))
544 {
545 wxString refName = node->GetPropVal(wxT("ref"), wxEmptyString);
546 if (refName.empty())
547 continue;
f80ea77b 548 wxXmlNode* refNode = FindResource(refName, wxEmptyString, true);
2b5f62a0
VZ
549 if (refNode &&
550 refNode->GetPropVal(wxT("class"), wxEmptyString) == classname)
551 {
552 return node;
553 }
554 }
555 }
47793ab8
VS
556 }
557
558 if ( recursive )
559 for (node = parent->GetChildren(); node; node = node->GetNext())
560 {
b272b6dc
RD
561 if ( node->GetType() == wxXML_ELEMENT_NODE &&
562 (node->GetName() == wxT("object") ||
47793ab8
VS
563 node->GetName() == wxT("object_ref")) )
564 {
f80ea77b 565 wxXmlNode* found = DoFindResource(node, name, classname, true);
47793ab8
VS
566 if ( found )
567 return found;
568 }
569 }
570
571 return NULL;
572}
78d14f80 573
b272b6dc 574wxXmlNode *wxXmlResource::FindResource(const wxString& name,
47793ab8
VS
575 const wxString& classname,
576 bool recursive)
78d14f80
VS
577{
578 UpdateResources(); //ensure everything is up-to-date
579
580 wxString dummy;
581 for (size_t f = 0; f < m_data.GetCount(); f++)
582 {
47793ab8
VS
583 if ( m_data[f].Doc == NULL || m_data[f].Doc->GetRoot() == NULL )
584 continue;
585
b272b6dc 586 wxXmlNode* found = DoFindResource(m_data[f].Doc->GetRoot(),
47793ab8
VS
587 name, classname, recursive);
588 if ( found )
589 {
78d14f80 590#if wxUSE_FILESYSTEM
47793ab8 591 m_curFileSystem.ChangePathTo(m_data[f].File);
78d14f80 592#endif
47793ab8
VS
593 return found;
594 }
78d14f80
VS
595 }
596
b5d6954b 597 wxLogError(_("XRC resource '%s' (class '%s') not found!"),
78d14f80
VS
598 name.c_str(), classname.c_str());
599 return NULL;
600}
601
47793ab8
VS
602static void MergeNodes(wxXmlNode& dest, wxXmlNode& with)
603{
604 // Merge properties:
605 for (wxXmlProperty *prop = with.GetProperties(); prop; prop = prop->GetNext())
606 {
607 wxXmlProperty *dprop;
608 for (dprop = dest.GetProperties(); dprop; dprop = dprop->GetNext())
609 {
b272b6dc 610
47793ab8
VS
611 if ( dprop->GetName() == prop->GetName() )
612 {
613 dprop->SetValue(prop->GetValue());
614 break;
615 }
616 }
78d14f80 617
47793ab8
VS
618 if ( !dprop )
619 dest.AddProperty(prop->GetName(), prop->GetValue());
620 }
621
622 // Merge child nodes:
623 for (wxXmlNode* node = with.GetChildren(); node; node = node->GetNext())
624 {
625 wxString name = node->GetPropVal(wxT("name"), wxEmptyString);
626 wxXmlNode *dnode;
627
628 for (dnode = dest.GetChildren(); dnode; dnode = dnode->GetNext() )
629 {
630 if ( dnode->GetName() == node->GetName() &&
2b5f62a0 631 dnode->GetPropVal(wxT("name"), wxEmptyString) == name &&
47793ab8
VS
632 dnode->GetType() == node->GetType() )
633 {
634 MergeNodes(*dnode, *node);
635 break;
636 }
637 }
638
639 if ( !dnode )
b26a650c
VZ
640 {
641 static const wxChar *AT_END = wxT("end");
642 wxString insert_pos = node->GetPropVal(wxT("insert_at"), AT_END);
643 if ( insert_pos == AT_END )
644 {
645 dest.AddChild(new wxXmlNode(*node));
646 }
647 else if ( insert_pos == wxT("begin") )
648 {
649 dest.InsertChild(new wxXmlNode(*node), dest.GetChildren());
650 }
651 }
47793ab8
VS
652 }
653
0bca0373 654 if ( dest.GetType() == wxXML_TEXT_NODE && with.GetContent().length() )
47793ab8
VS
655 dest.SetContent(with.GetContent());
656}
78d14f80 657
317a0d73
VS
658wxObject *wxXmlResource::CreateResFromNode(wxXmlNode *node, wxObject *parent,
659 wxObject *instance,
660 wxXmlResourceHandler *handlerToUse)
78d14f80
VS
661{
662 if (node == NULL) return NULL;
663
47793ab8
VS
664 // handling of referenced resource
665 if ( node->GetName() == wxT("object_ref") )
666 {
667 wxString refName = node->GetPropVal(wxT("ref"), wxEmptyString);
f80ea77b 668 wxXmlNode* refNode = FindResource(refName, wxEmptyString, true);
47793ab8
VS
669
670 if ( !refNode )
671 {
b272b6dc 672 wxLogError(_("Referenced object node with ref=\"%s\" not found!"),
47793ab8
VS
673 refName.c_str());
674 return NULL;
675 }
676
677 wxXmlNode copy(*refNode);
678 MergeNodes(copy, *node);
679
680 return CreateResFromNode(&copy, parent, instance);
681 }
682
8e8a4e85
VS
683 wxXmlResourceHandler *handler;
684
317a0d73 685 if (handlerToUse)
b380439d 686 {
317a0d73 687 if (handlerToUse->CanHandle(node))
8e8a4e85 688 {
317a0d73 689 return handlerToUse->CreateResource(node, parent, instance);
8e8a4e85 690 }
317a0d73
VS
691 }
692 else if (node->GetName() == wxT("object"))
b380439d 693 {
8e8a4e85
VS
694 wxList::compatibility_iterator ND = m_handlers.GetFirst();
695 while (ND)
317a0d73 696 {
8e8a4e85 697 handler = (wxXmlResourceHandler*)ND->GetData();
317a0d73 698 if (handler->CanHandle(node))
8e8a4e85 699 {
317a0d73 700 return handler->CreateResource(node, parent, instance);
8e8a4e85
VS
701 }
702 ND = ND->GetNext();
78d14f80 703 }
78d14f80
VS
704 }
705
706 wxLogError(_("No handler found for XML node '%s', class '%s'!"),
707 node->GetName().c_str(),
708 node->GetPropVal(wxT("class"), wxEmptyString).c_str());
709 return NULL;
710}
711
712
2b5f62a0
VZ
713#include "wx/listimpl.cpp"
714WX_DECLARE_LIST(wxXmlSubclassFactory, wxXmlSubclassFactoriesList);
17a1ebd1 715WX_DEFINE_LIST(wxXmlSubclassFactoriesList)
2b5f62a0
VZ
716
717wxXmlSubclassFactoriesList *wxXmlResource::ms_subclassFactories = NULL;
718
719/*static*/ void wxXmlResource::AddSubclassFactory(wxXmlSubclassFactory *factory)
720{
721 if (!ms_subclassFactories)
722 {
723 ms_subclassFactories = new wxXmlSubclassFactoriesList;
2b5f62a0
VZ
724 }
725 ms_subclassFactories->Append(factory);
726}
727
728class wxXmlSubclassFactoryCXX : public wxXmlSubclassFactory
729{
730public:
731 ~wxXmlSubclassFactoryCXX() {}
732
733 wxObject *Create(const wxString& className)
734 {
735 wxClassInfo* classInfo = wxClassInfo::FindClass(className);
736
737 if (classInfo)
738 return classInfo->CreateObject();
739 else
740 return NULL;
741 }
742};
743
744
745
78d14f80 746
78d14f80
VS
747wxXmlResourceHandler::wxXmlResourceHandler()
748 : m_node(NULL), m_parent(NULL), m_instance(NULL),
9a8d8c5a 749 m_parentAsWindow(NULL)
78d14f80
VS
750{}
751
752
753
754wxObject *wxXmlResourceHandler::CreateResource(wxXmlNode *node, wxObject *parent, wxObject *instance)
755{
756 wxXmlNode *myNode = m_node;
757 wxString myClass = m_class;
758 wxObject *myParent = m_parent, *myInstance = m_instance;
9a8d8c5a 759 wxWindow *myParentAW = m_parentAsWindow;
78d14f80 760
daa85ee3 761 m_instance = instance;
b272b6dc 762 if (!m_instance && node->HasProp(wxT("subclass")) &&
daa85ee3
VS
763 !(m_resource->GetFlags() & wxXRC_NO_SUBCLASSING))
764 {
765 wxString subclass = node->GetPropVal(wxT("subclass"), wxEmptyString);
2b5f62a0 766 if (!subclass.empty())
daa85ee3 767 {
461932ae 768 for (wxXmlSubclassFactoriesList::compatibility_iterator i = wxXmlResource::ms_subclassFactories->GetFirst();
2b5f62a0
VZ
769 i; i = i->GetNext())
770 {
771 m_instance = i->GetData()->Create(subclass);
772 if (m_instance)
773 break;
774 }
daa85ee3 775
2b5f62a0
VZ
776 if (!m_instance)
777 {
778 wxString name = node->GetPropVal(wxT("name"), wxEmptyString);
779 wxLogError(_("Subclass '%s' not found for resource '%s', not subclassing!"),
780 subclass.c_str(), name.c_str());
781 }
782 }
daa85ee3
VS
783 }
784
78d14f80
VS
785 m_node = node;
786 m_class = node->GetPropVal(wxT("class"), wxEmptyString);
787 m_parent = parent;
78d14f80 788 m_parentAsWindow = wxDynamicCast(m_parent, wxWindow);
78d14f80
VS
789
790 wxObject *returned = DoCreateResource();
791
792 m_node = myNode;
793 m_class = myClass;
794 m_parent = myParent; m_parentAsWindow = myParentAW;
9a8d8c5a 795 m_instance = myInstance;
78d14f80
VS
796
797 return returned;
798}
799
800
801void wxXmlResourceHandler::AddStyle(const wxString& name, int value)
802{
803 m_styleNames.Add(name);
804 m_styleValues.Add(value);
805}
806
807
808
809void wxXmlResourceHandler::AddWindowStyles()
810{
9dc579b3 811 XRC_ADD_STYLE(wxCLIP_CHILDREN);
d54a3e73
VZ
812
813 // the border styles all have the old and new names, recognize both for now
814 XRC_ADD_STYLE(wxSIMPLE_BORDER); XRC_ADD_STYLE(wxBORDER_SIMPLE);
815 XRC_ADD_STYLE(wxSUNKEN_BORDER); XRC_ADD_STYLE(wxBORDER_SUNKEN);
816 XRC_ADD_STYLE(wxDOUBLE_BORDER); XRC_ADD_STYLE(wxBORDER_DOUBLE);
817 XRC_ADD_STYLE(wxRAISED_BORDER); XRC_ADD_STYLE(wxBORDER_RAISED);
818 XRC_ADD_STYLE(wxSTATIC_BORDER); XRC_ADD_STYLE(wxBORDER_STATIC);
819 XRC_ADD_STYLE(wxNO_BORDER); XRC_ADD_STYLE(wxBORDER_NONE);
820
daa85ee3
VS
821 XRC_ADD_STYLE(wxTRANSPARENT_WINDOW);
822 XRC_ADD_STYLE(wxWANTS_CHARS);
43840d8b 823 XRC_ADD_STYLE(wxTAB_TRAVERSAL);
daa85ee3 824 XRC_ADD_STYLE(wxNO_FULL_REPAINT_ON_RESIZE);
7539ba56 825 XRC_ADD_STYLE(wxFULL_REPAINT_ON_RESIZE);
162a4f93 826 XRC_ADD_STYLE(wxALWAYS_SHOW_SB);
9f4ed861 827 XRC_ADD_STYLE(wxWS_EX_BLOCK_EVENTS);
b0802e64 828 XRC_ADD_STYLE(wxWS_EX_VALIDATE_RECURSIVELY);
78d14f80
VS
829}
830
831
832
833bool wxXmlResourceHandler::HasParam(const wxString& param)
834{
835 return (GetParamNode(param) != NULL);
836}
837
838
839int wxXmlResourceHandler::GetStyle(const wxString& param, int defaults)
840{
841 wxString s = GetParamValue(param);
842
843 if (!s) return defaults;
844
2b5f62a0 845 wxStringTokenizer tkn(s, wxT("| \t\n"), wxTOKEN_STRTOK);
78d14f80
VS
846 int style = 0;
847 int index;
848 wxString fl;
849 while (tkn.HasMoreTokens())
850 {
851 fl = tkn.GetNextToken();
852 index = m_styleNames.Index(fl);
853 if (index != wxNOT_FOUND)
854 style |= m_styleValues[index];
855 else
856 wxLogError(_("Unknown style flag ") + fl);
857 }
858 return style;
859}
860
861
862
ee1046d1 863wxString wxXmlResourceHandler::GetText(const wxString& param, bool translate)
78d14f80 864{
7b56015f
VS
865 wxXmlNode *parNode = GetParamNode(param);
866 wxString str1(GetNodeContent(parNode));
78d14f80
VS
867 wxString str2;
868 const wxChar *dt;
869 wxChar amp_char;
870
b272b6dc
RD
871 // VS: First version of XRC resources used $ instead of & (which is
872 // illegal in XML), but later I realized that '_' fits this purpose
718cf160 873 // much better (because &File means "File with F underlined").
78d14f80
VS
874 if (m_resource->CompareVersion(2,3,0,1) < 0)
875 amp_char = wxT('$');
876 else
877 amp_char = wxT('_');
878
879 for (dt = str1.c_str(); *dt; dt++)
880 {
881 // Remap amp_char to &, map double amp_char to amp_char (for things
882 // like "&File..." -- this is illegal in XML, so we use "_File..."):
883 if (*dt == amp_char)
884 {
885 if ( *(++dt) == amp_char )
886 str2 << amp_char;
887 else
888 str2 << wxT('&') << *dt;
889 }
984c33c9 890 // Remap \n to CR, \r to LF, \t to TAB, \\ to \:
78d14f80
VS
891 else if (*dt == wxT('\\'))
892 switch (*(++dt))
893 {
984c33c9
VS
894 case wxT('n'):
895 str2 << wxT('\n');
896 break;
e7a3a5a5 897
984c33c9
VS
898 case wxT('t'):
899 str2 << wxT('\t');
900 break;
e7a3a5a5 901
984c33c9
VS
902 case wxT('r'):
903 str2 << wxT('\r');
904 break;
905
906 case wxT('\\') :
907 // "\\" wasn't translated to "\" prior to 2.5.3.0:
908 if (m_resource->CompareVersion(2,5,3,0) >= 0)
909 {
910 str2 << wxT('\\');
911 break;
912 }
913 // else fall-through to default: branch below
e7a3a5a5 914
984c33c9
VS
915 default:
916 str2 << wxT('\\') << *dt;
917 break;
78d14f80
VS
918 }
919 else str2 << *dt;
920 }
b272b6dc 921
7b56015f
VS
922 if (m_resource->GetFlags() & wxXRC_USE_LOCALE)
923 {
924 if (translate && parNode &&
925 parNode->GetPropVal(wxT("translate"), wxEmptyString) != wxT("0"))
926 {
d4a724d4 927 return wxGetTranslation(str2, m_resource->GetDomain());
7b56015f
VS
928 }
929 else
930 {
931#if wxUSE_UNICODE
932 return str2;
933#else
934 // The string is internally stored as UTF-8, we have to convert
935 // it into system's default encoding so that it can be displayed:
936 return wxString(str2.mb_str(wxConvUTF8), wxConvLocal);
937#endif
938 }
939 }
8516a98b
DS
940
941 // If wxXRC_USE_LOCALE is not set, then the string is already in
942 // system's default encoding in ANSI build, so we don't have to
943 // do anything special here.
944 return str2;
78d14f80
VS
945}
946
947
948
949long wxXmlResourceHandler::GetLong(const wxString& param, long defaultv)
950{
951 long value;
952 wxString str1 = GetParamValue(param);
953
954 if (!str1.ToLong(&value))
955 value = defaultv;
956
957 return value;
958}
e7a3a5a5 959
1df61962
VS
960float wxXmlResourceHandler::GetFloat(const wxString& param, float defaultv)
961{
962 double value;
963 wxString str1 = GetParamValue(param);
964
e7a3a5a5 965#ifndef __WXWINCE__
1df61962 966 const char *prevlocale = setlocale(LC_NUMERIC, "C");
e7a3a5a5
WS
967#endif
968
1df61962
VS
969 if (!str1.ToDouble(&value))
970 value = defaultv;
971
e7a3a5a5 972#ifndef __WXWINCE__
1df61962 973 setlocale(LC_NUMERIC, prevlocale);
e7a3a5a5 974#endif
78d14f80 975
17a1ebd1 976 return wx_truncate_cast(float, value);
1df61962 977}
78d14f80 978
af1337b0 979
78d14f80
VS
980int wxXmlResourceHandler::GetID()
981{
13de23f6 982 return wxXmlResource::GetXRCID(GetName());
78d14f80
VS
983}
984
985
af1337b0 986
78d14f80
VS
987wxString wxXmlResourceHandler::GetName()
988{
989 return m_node->GetPropVal(wxT("name"), wxT("-1"));
990}
991
992
993
994bool wxXmlResourceHandler::GetBool(const wxString& param, bool defaultv)
995{
996 wxString v = GetParamValue(param);
997 v.MakeLower();
998 if (!v) return defaultv;
8516a98b
DS
999
1000 return (v == wxT("1"));
78d14f80
VS
1001}
1002
1003
1df61962
VS
1004static wxColour GetSystemColour(const wxString& name)
1005{
1006 if (!name.empty())
1007 {
1008 #define SYSCLR(clr) \
1009 if (name == _T(#clr)) return wxSystemSettings::GetColour(clr);
1010 SYSCLR(wxSYS_COLOUR_SCROLLBAR)
1011 SYSCLR(wxSYS_COLOUR_BACKGROUND)
1012 SYSCLR(wxSYS_COLOUR_DESKTOP)
1013 SYSCLR(wxSYS_COLOUR_ACTIVECAPTION)
1014 SYSCLR(wxSYS_COLOUR_INACTIVECAPTION)
1015 SYSCLR(wxSYS_COLOUR_MENU)
1016 SYSCLR(wxSYS_COLOUR_WINDOW)
1017 SYSCLR(wxSYS_COLOUR_WINDOWFRAME)
1018 SYSCLR(wxSYS_COLOUR_MENUTEXT)
1019 SYSCLR(wxSYS_COLOUR_WINDOWTEXT)
1020 SYSCLR(wxSYS_COLOUR_CAPTIONTEXT)
1021 SYSCLR(wxSYS_COLOUR_ACTIVEBORDER)
1022 SYSCLR(wxSYS_COLOUR_INACTIVEBORDER)
1023 SYSCLR(wxSYS_COLOUR_APPWORKSPACE)
1024 SYSCLR(wxSYS_COLOUR_HIGHLIGHT)
1025 SYSCLR(wxSYS_COLOUR_HIGHLIGHTTEXT)
1026 SYSCLR(wxSYS_COLOUR_BTNFACE)
1027 SYSCLR(wxSYS_COLOUR_3DFACE)
1028 SYSCLR(wxSYS_COLOUR_BTNSHADOW)
1029 SYSCLR(wxSYS_COLOUR_3DSHADOW)
1030 SYSCLR(wxSYS_COLOUR_GRAYTEXT)
1031 SYSCLR(wxSYS_COLOUR_BTNTEXT)
1032 SYSCLR(wxSYS_COLOUR_INACTIVECAPTIONTEXT)
1033 SYSCLR(wxSYS_COLOUR_BTNHIGHLIGHT)
1034 SYSCLR(wxSYS_COLOUR_BTNHILIGHT)
1035 SYSCLR(wxSYS_COLOUR_3DHIGHLIGHT)
1036 SYSCLR(wxSYS_COLOUR_3DHILIGHT)
1037 SYSCLR(wxSYS_COLOUR_3DDKSHADOW)
1038 SYSCLR(wxSYS_COLOUR_3DLIGHT)
1039 SYSCLR(wxSYS_COLOUR_INFOTEXT)
1040 SYSCLR(wxSYS_COLOUR_INFOBK)
1041 SYSCLR(wxSYS_COLOUR_LISTBOX)
1042 SYSCLR(wxSYS_COLOUR_HOTLIGHT)
1043 SYSCLR(wxSYS_COLOUR_GRADIENTACTIVECAPTION)
1044 SYSCLR(wxSYS_COLOUR_GRADIENTINACTIVECAPTION)
1045 SYSCLR(wxSYS_COLOUR_MENUHILIGHT)
1046 SYSCLR(wxSYS_COLOUR_MENUBAR)
1047 #undef SYSCLR
1048 }
1049
1050 return wxNullColour;
1051}
78d14f80 1052
984f1d84 1053wxColour wxXmlResourceHandler::GetColour(const wxString& param, const wxColour& defaultv)
78d14f80
VS
1054{
1055 wxString v = GetParamValue(param);
984f1d84
VS
1056
1057 if ( v.empty() )
1058 return defaultv;
1059
68b4e4cf 1060 wxColour clr;
1df61962 1061
68b4e4cf
WS
1062 // wxString -> wxColour conversion
1063 if (!clr.Set(v))
78d14f80 1064 {
1df61962
VS
1065 // the colour doesn't use #RRGGBB format, check if it is symbolic
1066 // colour name:
68b4e4cf 1067 clr = GetSystemColour(v);
1df61962
VS
1068 if (clr.Ok())
1069 return clr;
e7a3a5a5 1070
b5d6954b 1071 wxLogError(_("XRC resource: Incorrect colour specification '%s' for property '%s'."),
78d14f80
VS
1072 v.c_str(), param.c_str());
1073 return wxNullColour;
1074 }
1075
68b4e4cf 1076 return clr;
78d14f80
VS
1077}
1078
1079
1080
92e898b0 1081wxBitmap wxXmlResourceHandler::GetBitmap(const wxString& param,
db59a97c
VS
1082 const wxArtClient& defaultArtClient,
1083 wxSize size)
78d14f80 1084{
db59a97c
VS
1085 /* If the bitmap is specified as stock item, query wxArtProvider for it: */
1086 wxXmlNode *bmpNode = GetParamNode(param);
1087 if ( bmpNode )
af1337b0 1088 {
db59a97c
VS
1089 wxString sid = bmpNode->GetPropVal(wxT("stock_id"), wxEmptyString);
1090 if ( !sid.empty() )
1091 {
b3e85292
VS
1092 wxString scl = bmpNode->GetPropVal(wxT("stock_client"), wxEmptyString);
1093 if (scl.empty())
1094 scl = defaultArtClient;
1095 else
1096 scl = wxART_MAKE_CLIENT_ID_FROM_STR(scl);
e7a3a5a5 1097
92e898b0 1098 wxBitmap stockArt =
db59a97c 1099 wxArtProvider::GetBitmap(wxART_MAKE_ART_ID_FROM_STR(sid),
b3e85292 1100 scl, size);
db59a97c
VS
1101 if ( stockArt.Ok() )
1102 return stockArt;
1103 }
af1337b0
JS
1104 }
1105
92e898b0 1106 /* ...or load the bitmap from file: */
78d14f80 1107 wxString name = GetParamValue(param);
e7a3a5a5 1108 if (name.empty()) return wxNullBitmap;
78d14f80 1109#if wxUSE_FILESYSTEM
4532786e 1110 wxFSFile *fsfile = GetCurFileSystem().OpenFile(name, wxFS_READ | wxFS_SEEKABLE);
78d14f80
VS
1111 if (fsfile == NULL)
1112 {
d0b50ad9
VS
1113 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."),
1114 name.c_str());
78d14f80
VS
1115 return wxNullBitmap;
1116 }
1117 wxImage img(*(fsfile->GetStream()));
1118 delete fsfile;
1119#else
45f3249b 1120 wxImage img(name);
78d14f80 1121#endif
af1337b0 1122
78d14f80
VS
1123 if (!img.Ok())
1124 {
1df61962 1125 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."),
45f3249b 1126 name.c_str());
78d14f80
VS
1127 return wxNullBitmap;
1128 }
1129 if (!(size == wxDefaultSize)) img.Rescale(size.x, size.y);
b272b6dc 1130 return wxBitmap(img);
78d14f80
VS
1131}
1132
9e29cd0a
RR
1133#if wxUSE_ANIMATIONCTRL
1134wxAnimation wxXmlResourceHandler::GetAnimation(const wxString& param)
1135{
1136 wxAnimation ani;
1137
1138 /* load the animation from file: */
1139 wxString name = GetParamValue(param);
1140 if (name.empty()) return wxNullAnimation;
1141#if wxUSE_FILESYSTEM
4532786e 1142 wxFSFile *fsfile = GetCurFileSystem().OpenFile(name, wxFS_READ | wxFS_SEEKABLE);
9e29cd0a
RR
1143 if (fsfile == NULL)
1144 {
1145 wxLogError(_("XRC resource: Cannot create animation from '%s'."),
1146 name.c_str());
1147 return wxNullAnimation;
1148 }
1149 ani.Load(*(fsfile->GetStream()));
1150 delete fsfile;
1151#else
1152 ani.LoadFile(name);
1153#endif
1154
1155 if (!ani.IsOk())
1156 {
1157 wxLogError(_("XRC resource: Cannot create animation from '%s'."),
1158 name.c_str());
1159 return wxNullAnimation;
1160 }
1161
1162 return ani;
1163}
1164#endif // wxUSE_ANIMATIONCTRL
1165
78d14f80
VS
1166
1167
92e898b0 1168wxIcon wxXmlResourceHandler::GetIcon(const wxString& param,
db59a97c
VS
1169 const wxArtClient& defaultArtClient,
1170 wxSize size)
78d14f80 1171{
78d14f80 1172 wxIcon icon;
db59a97c 1173 icon.CopyFromBitmap(GetBitmap(param, defaultArtClient, size));
78d14f80
VS
1174 return icon;
1175}
1176
1177
1178
1179wxXmlNode *wxXmlResourceHandler::GetParamNode(const wxString& param)
1180{
2b5f62a0
VZ
1181 wxCHECK_MSG(m_node, NULL, wxT("You can't access handler data before it was initialized!"));
1182
78d14f80
VS
1183 wxXmlNode *n = m_node->GetChildren();
1184
1185 while (n)
1186 {
1187 if (n->GetType() == wxXML_ELEMENT_NODE && n->GetName() == param)
1188 return n;
1189 n = n->GetNext();
1190 }
1191 return NULL;
1192}
1193
1194
2d672c46
MW
1195
1196bool wxXmlResourceHandler::IsOfClass(wxXmlNode *node, const wxString& classname)
1197{
1198 return node->GetPropVal(wxT("class"), wxEmptyString) == classname;
1199}
1200
1201
1202
78d14f80
VS
1203wxString wxXmlResourceHandler::GetNodeContent(wxXmlNode *node)
1204{
1205 wxXmlNode *n = node;
1206 if (n == NULL) return wxEmptyString;
1207 n = n->GetChildren();
1208
1209 while (n)
1210 {
1211 if (n->GetType() == wxXML_TEXT_NODE ||
1212 n->GetType() == wxXML_CDATA_SECTION_NODE)
1213 return n->GetContent();
1214 n = n->GetNext();
1215 }
1216 return wxEmptyString;
1217}
1218
1219
1220
1221wxString wxXmlResourceHandler::GetParamValue(const wxString& param)
1222{
e7a3a5a5 1223 if (param.empty())
78d14f80
VS
1224 return GetNodeContent(m_node);
1225 else
1226 return GetNodeContent(GetParamNode(param));
1227}
1228
1229
1230
0c00c86f
VS
1231wxSize wxXmlResourceHandler::GetSize(const wxString& param,
1232 wxWindow *windowToUse)
78d14f80
VS
1233{
1234 wxString s = GetParamValue(param);
e7a3a5a5 1235 if (s.empty()) s = wxT("-1,-1");
78d14f80 1236 bool is_dlg;
d1f47235 1237 long sx, sy = 0;
78d14f80 1238
88a7a4e1 1239 is_dlg = s[s.length()-1] == wxT('d');
78d14f80
VS
1240 if (is_dlg) s.RemoveLast();
1241
1242 if (!s.BeforeFirst(wxT(',')).ToLong(&sx) ||
1243 !s.AfterLast(wxT(',')).ToLong(&sy))
1244 {
00393283 1245 wxLogError(_("Cannot parse coordinates from '%s'."), s.c_str());
78d14f80
VS
1246 return wxDefaultSize;
1247 }
1248
1249 if (is_dlg)
1250 {
0c00c86f
VS
1251 if (windowToUse)
1252 {
1253 return wxDLG_UNIT(windowToUse, wxSize(sx, sy));
1254 }
1255 else if (m_parentAsWindow)
1256 {
78d14f80 1257 return wxDLG_UNIT(m_parentAsWindow, wxSize(sx, sy));
0c00c86f 1258 }
78d14f80
VS
1259 else
1260 {
1261 wxLogError(_("Cannot convert dialog units: dialog unknown."));
1262 return wxDefaultSize;
1263 }
1264 }
8516a98b
DS
1265
1266 return wxSize(sx, sy);
78d14f80
VS
1267}
1268
1269
1270
1271wxPoint wxXmlResourceHandler::GetPosition(const wxString& param)
1272{
1273 wxSize sz = GetSize(param);
1274 return wxPoint(sz.x, sz.y);
1275}
1276
1277
1278
0c00c86f
VS
1279wxCoord wxXmlResourceHandler::GetDimension(const wxString& param,
1280 wxCoord defaultv,
1281 wxWindow *windowToUse)
78d14f80
VS
1282{
1283 wxString s = GetParamValue(param);
e7a3a5a5 1284 if (s.empty()) return defaultv;
78d14f80
VS
1285 bool is_dlg;
1286 long sx;
1287
88a7a4e1 1288 is_dlg = s[s.length()-1] == wxT('d');
78d14f80
VS
1289 if (is_dlg) s.RemoveLast();
1290
1291 if (!s.ToLong(&sx))
1292 {
00393283 1293 wxLogError(_("Cannot parse dimension from '%s'."), s.c_str());
78d14f80
VS
1294 return defaultv;
1295 }
1296
1297 if (is_dlg)
1298 {
0c00c86f
VS
1299 if (windowToUse)
1300 {
1301 return wxDLG_UNIT(windowToUse, wxSize(sx, 0)).x;
1302 }
1303 else if (m_parentAsWindow)
1304 {
78d14f80 1305 return wxDLG_UNIT(m_parentAsWindow, wxSize(sx, 0)).x;
0c00c86f 1306 }
78d14f80
VS
1307 else
1308 {
1309 wxLogError(_("Cannot convert dialog units: dialog unknown."));
1310 return defaultv;
1311 }
1312 }
8516a98b
DS
1313
1314 return sx;
78d14f80
VS
1315}
1316
1317
1df61962
VS
1318// Get system font index using indexname
1319static wxFont GetSystemFont(const wxString& name)
1320{
1321 if (!name.empty())
1322 {
1323 #define SYSFNT(fnt) \
1324 if (name == _T(#fnt)) return wxSystemSettings::GetFont(fnt);
1325 SYSFNT(wxSYS_OEM_FIXED_FONT)
1326 SYSFNT(wxSYS_ANSI_FIXED_FONT)
1327 SYSFNT(wxSYS_ANSI_VAR_FONT)
1328 SYSFNT(wxSYS_SYSTEM_FONT)
1329 SYSFNT(wxSYS_DEVICE_DEFAULT_FONT)
1330 SYSFNT(wxSYS_DEFAULT_PALETTE)
1331 SYSFNT(wxSYS_SYSTEM_FIXED_FONT)
1332 SYSFNT(wxSYS_DEFAULT_GUI_FONT)
1333 #undef SYSFNT
1334 }
1335
1336 return wxNullFont;
1337}
78d14f80
VS
1338
1339wxFont wxXmlResourceHandler::GetFont(const wxString& param)
1340{
1341 wxXmlNode *font_node = GetParamNode(param);
1342 if (font_node == NULL)
1343 {
00393283 1344 wxLogError(_("Cannot find font node '%s'."), param.c_str());
78d14f80
VS
1345 return wxNullFont;
1346 }
1347
1348 wxXmlNode *oldnode = m_node;
1349 m_node = font_node;
1350
1df61962 1351 // font attributes:
78d14f80 1352
1df61962 1353 // size
94245f6d 1354 int isize = -1;
1df61962 1355 bool hasSize = HasParam(wxT("size"));
e7a3a5a5 1356 if (hasSize)
94245f6d 1357 isize = GetLong(wxT("size"), -1);
78d14f80 1358
1df61962
VS
1359 // style
1360 int istyle = wxNORMAL;
1361 bool hasStyle = HasParam(wxT("style"));
1362 if (hasStyle)
1363 {
1364 wxString style = GetParamValue(wxT("style"));
e7a3a5a5 1365 if (style == wxT("italic"))
1df61962 1366 istyle = wxITALIC;
e7a3a5a5 1367 else if (style == wxT("slant"))
1df61962
VS
1368 istyle = wxSLANT;
1369 }
78d14f80 1370
1df61962
VS
1371 // weight
1372 int iweight = wxNORMAL;
1373 bool hasWeight = HasParam(wxT("weight"));
1374 if (hasWeight)
1375 {
1376 wxString weight = GetParamValue(wxT("weight"));
e7a3a5a5 1377 if (weight == wxT("bold"))
1df61962 1378 iweight = wxBOLD;
e7a3a5a5 1379 else if (weight == wxT("light"))
1df61962
VS
1380 iweight = wxLIGHT;
1381 }
e7a3a5a5 1382
1df61962
VS
1383 // underline
1384 bool hasUnderlined = HasParam(wxT("underlined"));
1385 bool underlined = hasUnderlined ? GetBool(wxT("underlined"), false) : false;
78d14f80 1386
1df61962
VS
1387 // family and facename
1388 int ifamily = wxDEFAULT;
1389 bool hasFamily = HasParam(wxT("family"));
1390 if (hasFamily)
78d14f80 1391 {
1df61962
VS
1392 wxString family = GetParamValue(wxT("family"));
1393 if (family == wxT("decorative")) ifamily = wxDECORATIVE;
1394 else if (family == wxT("roman")) ifamily = wxROMAN;
1395 else if (family == wxT("script")) ifamily = wxSCRIPT;
1396 else if (family == wxT("swiss")) ifamily = wxSWISS;
1397 else if (family == wxT("modern")) ifamily = wxMODERN;
1398 else if (family == wxT("teletype")) ifamily = wxTELETYPE;
1399 }
e7a3a5a5
WS
1400
1401
1df61962
VS
1402 wxString facename;
1403 bool hasFacename = HasParam(wxT("face"));
1404 if (hasFacename)
1405 {
1406 wxString faces = GetParamValue(wxT("face"));
6540132f 1407 wxArrayString facenames(wxFontEnumerator::GetFacenames());
1df61962
VS
1408 wxStringTokenizer tk(faces, wxT(","));
1409 while (tk.HasMoreTokens())
78d14f80 1410 {
6540132f 1411 int index = facenames.Index(tk.GetNextToken(), false);
1df61962
VS
1412 if (index != wxNOT_FOUND)
1413 {
6540132f 1414 facename = facenames[index];
1df61962
VS
1415 break;
1416 }
78d14f80
VS
1417 }
1418 }
1419
1df61962
VS
1420 // encoding
1421 wxFontEncoding enc = wxFONTENCODING_DEFAULT;
1422 bool hasEncoding = HasParam(wxT("encoding"));
1423 if (hasEncoding)
1424 {
1425 wxString encoding = GetParamValue(wxT("encoding"));
1426 wxFontMapper mapper;
e7a3a5a5 1427 if (!encoding.empty())
1df61962
VS
1428 enc = mapper.CharsetToEncoding(encoding);
1429 if (enc == wxFONTENCODING_SYSTEM)
1430 enc = wxFONTENCODING_DEFAULT;
1431 }
78d14f80 1432
1df61962 1433 // is this font based on a system font?
94245f6d 1434 wxFont font = GetSystemFont(GetParamValue(wxT("sysfont")));
e7a3a5a5 1435
94245f6d 1436 if (font.Ok())
1df61962 1437 {
94245f6d
VZ
1438 if (hasSize && isize != -1)
1439 font.SetPointSize(isize);
1df61962 1440 else if (HasParam(wxT("relativesize")))
94245f6d 1441 font.SetPointSize(int(font.GetPointSize() *
1df61962 1442 GetFloat(wxT("relativesize"))));
e7a3a5a5 1443
1df61962 1444 if (hasStyle)
94245f6d 1445 font.SetStyle(istyle);
1df61962 1446 if (hasWeight)
94245f6d 1447 font.SetWeight(iweight);
1df61962 1448 if (hasUnderlined)
94245f6d 1449 font.SetUnderlined(underlined);
1df61962 1450 if (hasFamily)
94245f6d 1451 font.SetFamily(ifamily);
1df61962 1452 if (hasFacename)
94245f6d 1453 font.SetFaceName(facename);
1df61962 1454 if (hasEncoding)
94245f6d
VZ
1455 font.SetDefaultEncoding(enc);
1456 }
1457 else // not based on system font
1458 {
1459 font = wxFont(isize == -1 ? wxNORMAL_FONT->GetPointSize() : isize,
1460 ifamily, istyle, iweight,
1461 underlined, facename, enc);
1df61962 1462 }
8516a98b
DS
1463
1464 m_node = oldnode;
94245f6d 1465 return font;
78d14f80
VS
1466}
1467
1468
1469void wxXmlResourceHandler::SetupWindow(wxWindow *wnd)
1470{
1471 //FIXME : add cursor
1472
1473 if (HasParam(wxT("exstyle")))
0099f343
JS
1474 // Have to OR it with existing style, since
1475 // some implementations (e.g. wxGTK) use the extra style
1476 // during creation
1477 wnd->SetExtraStyle(wnd->GetExtraStyle() | GetStyle(wxT("exstyle")));
78d14f80
VS
1478 if (HasParam(wxT("bg")))
1479 wnd->SetBackgroundColour(GetColour(wxT("bg")));
1480 if (HasParam(wxT("fg")))
1481 wnd->SetForegroundColour(GetColour(wxT("fg")));
1482 if (GetBool(wxT("enabled"), 1) == 0)
f80ea77b 1483 wnd->Enable(false);
78d14f80
VS
1484 if (GetBool(wxT("focused"), 0) == 1)
1485 wnd->SetFocus();
1486 if (GetBool(wxT("hidden"), 0) == 1)
f80ea77b 1487 wnd->Show(false);
78d14f80
VS
1488#if wxUSE_TOOLTIPS
1489 if (HasParam(wxT("tooltip")))
1490 wnd->SetToolTip(GetText(wxT("tooltip")));
1491#endif
1492 if (HasParam(wxT("font")))
1493 wnd->SetFont(GetFont());
b23030d6
JS
1494 if (HasParam(wxT("help")))
1495 wnd->SetHelpText(GetText(wxT("help")));
78d14f80
VS
1496}
1497
1498
1499void wxXmlResourceHandler::CreateChildren(wxObject *parent, bool this_hnd_only)
1500{
1501 wxXmlNode *n = m_node->GetChildren();
1502
1503 while (n)
1504 {
1505 if (n->GetType() == wxXML_ELEMENT_NODE &&
0fa2e104 1506 (n->GetName() == wxT("object") || n->GetName() == wxT("object_ref")))
78d14f80 1507 {
317a0d73
VS
1508 m_resource->CreateResFromNode(n, parent, NULL,
1509 this_hnd_only ? this : NULL);
78d14f80
VS
1510 }
1511 n = n->GetNext();
1512 }
1513}
1514
1515
1516void wxXmlResourceHandler::CreateChildrenPrivately(wxObject *parent, wxXmlNode *rootnode)
1517{
1518 wxXmlNode *root;
1519 if (rootnode == NULL) root = m_node; else root = rootnode;
1520 wxXmlNode *n = root->GetChildren();
1521
1522 while (n)
1523 {
1524 if (n->GetType() == wxXML_ELEMENT_NODE && CanHandle(n))
1525 {
1526 CreateResource(n, parent, NULL);
1527 }
1528 n = n->GetNext();
1529 }
1530}
1531
1532
1533
1534
1535
1536
1537
5ed345b7 1538// --------------- XRCID implementation -----------------------------
78d14f80 1539
5ed345b7 1540#define XRCID_TABLE_SIZE 1024
78d14f80
VS
1541
1542
5ed345b7 1543struct XRCID_record
78d14f80
VS
1544{
1545 int id;
c560da98 1546 char *key;
5ed345b7 1547 XRCID_record *next;
78d14f80
VS
1548};
1549
5ed345b7 1550static XRCID_record *XRCID_Records[XRCID_TABLE_SIZE] = {NULL};
78d14f80 1551
c560da98 1552static int XRCID_Lookup(const char *str_id, int value_if_not_found = wxID_NONE)
78d14f80 1553{
78d14f80
VS
1554 int index = 0;
1555
c560da98 1556 for (const char *c = str_id; *c != '\0'; c++) index += (int)*c;
5ed345b7 1557 index %= XRCID_TABLE_SIZE;
78d14f80 1558
5ed345b7 1559 XRCID_record *oldrec = NULL;
5ed345b7 1560 for (XRCID_record *rec = XRCID_Records[index]; rec; rec = rec->next)
78d14f80 1561 {
00393283 1562 if (wxStrcmp(rec->key, str_id) == 0)
78d14f80
VS
1563 {
1564 return rec->id;
1565 }
78d14f80
VS
1566 oldrec = rec;
1567 }
1568
5ed345b7
VS
1569 XRCID_record **rec_var = (oldrec == NULL) ?
1570 &XRCID_Records[index] : &oldrec->next;
1571 *rec_var = new XRCID_record;
00393283 1572 (*rec_var)->key = wxStrdup(str_id);
78d14f80
VS
1573 (*rec_var)->next = NULL;
1574
c560da98 1575 char *end;
9b2a7469 1576 if (value_if_not_found != wxID_NONE)
13de23f6 1577 (*rec_var)->id = value_if_not_found;
85452d74
VS
1578 else
1579 {
13de23f6
VS
1580 int asint = wxStrtol(str_id, &end, 10);
1581 if (*str_id && *end == 0)
1582 {
1583 // if str_id was integer, keep it verbosely:
1584 (*rec_var)->id = asint;
1585 }
1586 else
1587 {
0fdd3cc8 1588 (*rec_var)->id = wxNewId();
13de23f6 1589 }
85452d74
VS
1590 }
1591
78d14f80
VS
1592 return (*rec_var)->id;
1593}
1594
3b2a000e
VS
1595static void AddStdXRCID_Records();
1596
9b2a7469 1597/*static*/
c560da98 1598int wxXmlResource::DoGetXRCID(const char *str_id, int value_if_not_found)
13de23f6 1599{
3b2a000e
VS
1600 static bool s_stdIDsAdded = false;
1601
1602 if ( !s_stdIDsAdded )
1603 {
1604 s_stdIDsAdded = true;
1605 AddStdXRCID_Records();
1606 }
1607
9b2a7469 1608 return XRCID_Lookup(str_id, value_if_not_found);
13de23f6
VS
1609}
1610
78d14f80 1611
5ed345b7 1612static void CleanXRCID_Record(XRCID_record *rec)
78d14f80
VS
1613{
1614 if (rec)
1615 {
5ed345b7 1616 CleanXRCID_Record(rec->next);
00393283 1617 free(rec->key);
78d14f80
VS
1618 delete rec;
1619 }
1620}
1621
5ed345b7 1622static void CleanXRCID_Records()
78d14f80 1623{
5ed345b7 1624 for (int i = 0; i < XRCID_TABLE_SIZE; i++)
139c5871 1625 {
5ed345b7 1626 CleanXRCID_Record(XRCID_Records[i]);
139c5871
VS
1627 XRCID_Records[i] = NULL;
1628 }
78d14f80
VS
1629}
1630
13de23f6
VS
1631static void AddStdXRCID_Records()
1632{
c560da98 1633#define stdID(id) XRCID_Lookup(#id, id)
13de23f6 1634 stdID(-1);
c369d4f1
VS
1635
1636 stdID(wxID_ANY);
1637 stdID(wxID_SEPARATOR);
e7a3a5a5 1638
c369d4f1
VS
1639 stdID(wxID_OPEN);
1640 stdID(wxID_CLOSE);
1641 stdID(wxID_NEW);
1642 stdID(wxID_SAVE);
1643 stdID(wxID_SAVEAS);
1644 stdID(wxID_REVERT);
1645 stdID(wxID_EXIT);
1646 stdID(wxID_UNDO);
1647 stdID(wxID_REDO);
1648 stdID(wxID_HELP);
1649 stdID(wxID_PRINT);
1650 stdID(wxID_PRINT_SETUP);
e63f19ba 1651 stdID(wxID_PAGE_SETUP);
c369d4f1
VS
1652 stdID(wxID_PREVIEW);
1653 stdID(wxID_ABOUT);
1654 stdID(wxID_HELP_CONTENTS);
1655 stdID(wxID_HELP_COMMANDS);
1656 stdID(wxID_HELP_PROCEDURES);
1657 stdID(wxID_HELP_CONTEXT);
13de23f6 1658 stdID(wxID_CLOSE_ALL);
c369d4f1
VS
1659 stdID(wxID_PREFERENCES);
1660 stdID(wxID_CUT);
1661 stdID(wxID_COPY);
1662 stdID(wxID_PASTE);
1663 stdID(wxID_CLEAR);
1664 stdID(wxID_FIND);
1665 stdID(wxID_DUPLICATE);
1666 stdID(wxID_SELECTALL);
1667 stdID(wxID_DELETE);
1668 stdID(wxID_REPLACE);
1669 stdID(wxID_REPLACE_ALL);
1670 stdID(wxID_PROPERTIES);
1671 stdID(wxID_VIEW_DETAILS);
1672 stdID(wxID_VIEW_LARGEICONS);
1673 stdID(wxID_VIEW_SMALLICONS);
1674 stdID(wxID_VIEW_LIST);
1675 stdID(wxID_VIEW_SORTDATE);
1676 stdID(wxID_VIEW_SORTNAME);
1677 stdID(wxID_VIEW_SORTSIZE);
1678 stdID(wxID_VIEW_SORTTYPE);
1679 stdID(wxID_FILE1);
1680 stdID(wxID_FILE2);
1681 stdID(wxID_FILE3);
1682 stdID(wxID_FILE4);
1683 stdID(wxID_FILE5);
1684 stdID(wxID_FILE6);
1685 stdID(wxID_FILE7);
1686 stdID(wxID_FILE8);
1687 stdID(wxID_FILE9);
1688 stdID(wxID_OK);
1689 stdID(wxID_CANCEL);
1690 stdID(wxID_APPLY);
1691 stdID(wxID_YES);
1692 stdID(wxID_NO);
1693 stdID(wxID_STATIC);
1694 stdID(wxID_FORWARD);
1695 stdID(wxID_BACKWARD);
1696 stdID(wxID_DEFAULT);
1697 stdID(wxID_MORE);
1698 stdID(wxID_SETUP);
1699 stdID(wxID_RESET);
1700 stdID(wxID_CONTEXT_HELP);
1701 stdID(wxID_YESTOALL);
1702 stdID(wxID_NOTOALL);
1703 stdID(wxID_ABORT);
1704 stdID(wxID_RETRY);
1705 stdID(wxID_IGNORE);
1706 stdID(wxID_ADD);
1707 stdID(wxID_REMOVE);
1708 stdID(wxID_UP);
1709 stdID(wxID_DOWN);
1710 stdID(wxID_HOME);
1711 stdID(wxID_REFRESH);
1712 stdID(wxID_STOP);
1713 stdID(wxID_INDEX);
1714 stdID(wxID_BOLD);
1715 stdID(wxID_ITALIC);
1716 stdID(wxID_JUSTIFY_CENTER);
1717 stdID(wxID_JUSTIFY_FILL);
1718 stdID(wxID_JUSTIFY_RIGHT);
1719 stdID(wxID_JUSTIFY_LEFT);
1720 stdID(wxID_UNDERLINE);
1721 stdID(wxID_INDENT);
1722 stdID(wxID_UNINDENT);
1723 stdID(wxID_ZOOM_100);
1724 stdID(wxID_ZOOM_FIT);
1725 stdID(wxID_ZOOM_IN);
1726 stdID(wxID_ZOOM_OUT);
1727 stdID(wxID_UNDELETE);
1728 stdID(wxID_REVERT_TO_SAVED);
1729 stdID(wxID_SYSTEM_MENU);
1730 stdID(wxID_CLOSE_FRAME);
1731 stdID(wxID_MOVE_FRAME);
1732 stdID(wxID_RESIZE_FRAME);
1733 stdID(wxID_MAXIMIZE_FRAME);
1734 stdID(wxID_ICONIZE_FRAME);
1735 stdID(wxID_RESTORE_FRAME);
1736
13de23f6
VS
1737#undef stdID
1738}
78d14f80
VS
1739
1740
1741
1742
1743
1744// --------------- module and globals -----------------------------
1745
78d14f80
VS
1746class wxXmlResourceModule: public wxModule
1747{
1748DECLARE_DYNAMIC_CLASS(wxXmlResourceModule)
1749public:
1750 wxXmlResourceModule() {}
824e8eaa
VS
1751 bool OnInit()
1752 {
2b5f62a0 1753 wxXmlResource::AddSubclassFactory(new wxXmlSubclassFactoryCXX);
f80ea77b 1754 return true;
824e8eaa 1755 }
78d14f80
VS
1756 void OnExit()
1757 {
1542c42e 1758 delete wxXmlResource::Set(NULL);
461932ae
MB
1759 if(wxXmlResource::ms_subclassFactories)
1760 WX_CLEAR_LIST(wxXmlSubclassFactoriesList, *wxXmlResource::ms_subclassFactories);
2b5f62a0 1761 wxDELETE(wxXmlResource::ms_subclassFactories);
5ed345b7 1762 CleanXRCID_Records();
78d14f80
VS
1763 }
1764};
1765
1766IMPLEMENT_DYNAMIC_CLASS(wxXmlResourceModule, wxModule)
1767
1768
1769// When wxXml is loaded dynamically after the application is already running
1770// then the built-in module system won't pick this one up. Add it manually.
1771void wxXmlInitResourceModule()
1772{
1773 wxModule* module = new wxXmlResourceModule;
1774 module->Init();
1775 wxModule::RegisterModule(module);
1776}
a1e4ec87
VS
1777
1778#endif // wxUSE_XRC