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