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