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