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