]> git.saurik.com Git - wxWidgets.git/blob - utils/dialoged/src/reseditr.cpp
wx-config is configurable...
[wxWidgets.git] / utils / dialoged / src / reseditr.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: reseditr.cpp
3 // Purpose: Resource editor class
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "reseditr.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/wx.h"
25
26 #include "wx/checkbox.h"
27 #include "wx/button.h"
28 #include "wx/choice.h"
29 #include "wx/listbox.h"
30 #include "wx/radiobox.h"
31 #include "wx/statbox.h"
32 #include "wx/gauge.h"
33 #include "wx/slider.h"
34 #include "wx/textctrl.h"
35 #include "wx/menu.h"
36 #include "wx/toolbar.h"
37 #endif
38
39 #include "wx/scrolbar.h"
40 #include "wx/config.h"
41
42 #include <ctype.h>
43 #include <stdlib.h>
44 #include <math.h>
45 #include <string.h>
46
47 #ifdef __WXMSW__
48 #include "wx/help.h"
49 #endif
50
51 #include "reseditr.h"
52 #include "winprop.h"
53 #include "dlghndlr.h"
54 #include "edtree.h"
55 #include "edlist.h"
56
57 wxResourceManager *wxResourceManager::sm_currentResourceManager = NULL;
58
59 #if defined(__WXGTK__) || defined(__WXMOTIF__)
60 #include "bitmaps/load.xpm"
61 #include "bitmaps/save.xpm"
62 #include "bitmaps/new.xpm"
63 #include "bitmaps/vert.xpm"
64 #include "bitmaps/alignt.xpm"
65 #include "bitmaps/alignb.xpm"
66 #include "bitmaps/horiz.xpm"
67 #include "bitmaps/alignl.xpm"
68 #include "bitmaps/alignr.xpm"
69 #include "bitmaps/copysize.xpm"
70 #include "bitmaps/tofront.xpm"
71 #include "bitmaps/toback.xpm"
72 #include "bitmaps/help.xpm"
73 #include "bitmaps/wxwin.xpm"
74
75 #include "bitmaps/dialog.xpm"
76 #include "bitmaps/folder1.xpm"
77 #include "bitmaps/folder2.xpm"
78 #include "bitmaps/buttonsm.xpm"
79 #endif
80
81 /*
82 * Resource manager
83 */
84
85 wxResourceManager::wxResourceManager():
86 m_imageList(16, 16, TRUE)
87 {
88 sm_currentResourceManager = this;
89 m_editorFrame = NULL;
90 m_editorPanel = NULL;
91 m_popupMenu = NULL;
92 m_editorResourceTree = NULL;
93 m_editorControlList = NULL;
94 m_nameCounter = 1;
95 m_symbolIdCounter = 99;
96 m_modified = FALSE;
97 m_currentFilename = "";
98 m_symbolFilename = "";
99 m_editorToolBar = NULL;
100
101 // Default window positions
102 m_resourceEditorWindowSize.width = 500;
103 m_resourceEditorWindowSize.height = 450;
104
105 m_resourceEditorWindowSize.x = 0;
106 m_resourceEditorWindowSize.y = 0;
107
108 m_propertyWindowSize.width = 300;
109 m_propertyWindowSize.height = 300;
110
111 #ifdef __WXMSW__
112 m_helpController = NULL;
113 #endif
114
115 m_bitmapImage = NULL;
116 m_rootDialogItem = 0;
117 }
118
119 wxResourceManager::~wxResourceManager()
120 {
121 sm_currentResourceManager = NULL;
122 SaveOptions();
123
124 #ifdef __WXMSW__
125 if (m_helpController)
126 {
127 m_helpController->Quit();
128 delete m_helpController;
129 m_helpController = NULL;
130 }
131 #endif
132
133 delete m_bitmapImage;
134 delete m_popupMenu;
135 }
136
137 bool wxResourceManager::Initialize()
138 {
139 // Set up the resource filename for each platform.
140 // TODO: This shold be replaced by wxConfig usage.
141 #ifdef __WXMSW__
142 // dialoged.ini in the Windows directory
143 wxString windowsDir = wxGetOSDirectory();
144 windowsDir += "\\dialoged.ini" ;
145
146 m_optionsResourceFilename = windowsDir;
147 #elif defined(__WXGTK__) || defined(__WXMOTIF__)
148 wxGetHomeDir( &m_optionsResourceFilename );
149 m_optionsResourceFilename += "/.dialogedrc";
150 #else
151 #error "Unsupported platform."
152 #endif
153
154 LoadOptions();
155
156 #ifdef __WXMSW__
157 m_helpController = new wxHelpController;
158 m_helpController->Initialize("dialoged");
159 #endif
160
161 m_popupMenu = new wxMenu;
162 m_popupMenu->Append(OBJECT_MENU_EDIT, "Edit properties");
163 m_popupMenu->Append(OBJECT_MENU_DELETE, "Delete object");
164
165 if (!m_bitmapImage)
166 {
167 #ifdef __WXMSW__
168 m_bitmapImage = new wxBitmap("WXWINBMP", wxBITMAP_TYPE_BMP_RESOURCE);
169 #endif
170 #if defined(__WXGTK__) || defined(__WXMOTIF__)
171 m_bitmapImage = new wxBitmap( wxwin_xpm );
172 #endif
173 }
174
175 // Initialize the image list icons
176 #ifdef __WXMSW__
177 wxIcon icon1("DIALOG_ICON", wxBITMAP_TYPE_ICO_RESOURCE, 16, 16);
178 wxIcon icon2("FOLDER1_ICON", wxBITMAP_TYPE_ICO_RESOURCE, 16, 16);
179 wxIcon icon3("FOLDER2_ICON", wxBITMAP_TYPE_ICO_RESOURCE, 16, 16);
180 wxIcon icon4("BUTTONSM_ICON", wxBITMAP_TYPE_ICO_RESOURCE, 16, 16);
181 #else
182 wxIcon icon1( dialog_xpm );
183 wxIcon icon2( folder1_xpm );
184 wxIcon icon3( folder2_xpm );
185 wxIcon icon4( buttonsm_xpm );
186 #endif
187 m_imageList.Add(icon1);
188 m_imageList.Add(icon2);
189 m_imageList.Add(icon3);
190 m_imageList.Add(icon4);
191
192 m_symbolTable.AddStandardSymbols();
193
194 return TRUE;
195 }
196
197 bool wxResourceManager::LoadOptions()
198 {
199 wxConfig config("DialogEd", "wxWindows");
200
201 config.Read("editorWindowX", &m_resourceEditorWindowSize.x);
202 config.Read("editorWindowY", &m_resourceEditorWindowSize.y);
203 config.Read("editorWindowWidth", &m_resourceEditorWindowSize.width);
204 config.Read("editorWindowHeight", &m_resourceEditorWindowSize.height);
205 config.Read("propertyWindowX", &m_propertyWindowSize.x);
206 config.Read("propertyWindowY", &m_propertyWindowSize.y);
207 config.Read("propertyWindowWidth", &m_propertyWindowSize.width);
208 config.Read("propertyWindowHeight", &m_propertyWindowSize.height);
209
210 /*
211 wxGetResource("DialogEd", "editorWindowX", &m_resourceEditorWindowSize.x, m_optionsResourceFilename.GetData());
212 wxGetResource("DialogEd", "editorWindowY", &m_resourceEditorWindowSize.y, m_optionsResourceFilename.GetData());
213 wxGetResource("DialogEd", "editorWindowWidth", &m_resourceEditorWindowSize.width, m_optionsResourceFilename.GetData());
214 wxGetResource("DialogEd", "editorWindowHeight", &m_resourceEditorWindowSize.height, m_optionsResourceFilename.GetData());
215 wxGetResource("DialogEd", "propertyWindowX", &m_propertyWindowSize.x, m_optionsResourceFilename.GetData());
216 wxGetResource("DialogEd", "propertyWindowY", &m_propertyWindowSize.y, m_optionsResourceFilename.GetData());
217 wxGetResource("DialogEd", "propertyWindowWidth", &m_propertyWindowSize.width, m_optionsResourceFilename.GetData());
218 wxGetResource("DialogEd", "propertyWindowHeight", &m_propertyWindowSize.height, m_optionsResourceFilename.GetData());
219 */
220 return TRUE;
221 }
222
223 bool wxResourceManager::SaveOptions()
224 {
225 wxConfig config("DialogEd", "wxWindows");
226
227 config.Write("editorWindowX", (long) m_resourceEditorWindowSize.x);
228 config.Write("editorWindowY", (long) m_resourceEditorWindowSize.y);
229 config.Write("editorWindowWidth", (long) m_resourceEditorWindowSize.width);
230 config.Write("editorWindowHeight", (long) m_resourceEditorWindowSize.height);
231 config.Write("propertyWindowX", (long) m_propertyWindowSize.x);
232 config.Write("propertyWindowY", (long) m_propertyWindowSize.y);
233 config.Write("propertyWindowWidth", (long) m_propertyWindowSize.width);
234 config.Write("propertyWindowHeight", (long) m_propertyWindowSize.height);
235 /*
236 wxWriteResource("DialogEd", "editorWindowX", m_resourceEditorWindowSize.x, m_optionsResourceFilename.GetData());
237 wxWriteResource("DialogEd", "editorWindowY", m_resourceEditorWindowSize.y, m_optionsResourceFilename.GetData());
238 wxWriteResource("DialogEd", "editorWindowWidth", m_resourceEditorWindowSize.width, m_optionsResourceFilename.GetData());
239 wxWriteResource("DialogEd", "editorWindowHeight", m_resourceEditorWindowSize.height, m_optionsResourceFilename.GetData());
240
241 wxWriteResource("DialogEd", "propertyWindowX", m_propertyWindowSize.x, m_optionsResourceFilename.GetData());
242 wxWriteResource("DialogEd", "propertyWindowY", m_propertyWindowSize.y, m_optionsResourceFilename.GetData());
243 wxWriteResource("DialogEd", "propertyWindowWidth", m_propertyWindowSize.width, m_optionsResourceFilename.GetData());
244 wxWriteResource("DialogEd", "propertyWindowHeight", m_propertyWindowSize.height, m_optionsResourceFilename.GetData());
245 */
246
247 return TRUE;
248 }
249
250 // Show or hide the resource editor frame, which displays a list
251 // of resources with ability to edit them.
252 bool wxResourceManager::ShowResourceEditor(bool show, wxWindow *WXUNUSED(parent), const char *title)
253 {
254 if (show)
255 {
256 if (m_editorFrame)
257 {
258 m_editorFrame->Iconize(FALSE);
259 m_editorFrame->Show(TRUE);
260 return TRUE;
261 }
262 m_editorFrame = OnCreateEditorFrame(title);
263 SetFrameTitle("");
264 wxMenuBar *menuBar = OnCreateEditorMenuBar(m_editorFrame);
265 m_editorFrame->SetMenuBar(menuBar);
266
267 m_editorToolBar = (EditorToolBar *)OnCreateToolBar(m_editorFrame);
268 m_editorControlList = new wxResourceEditorControlList(m_editorFrame, IDC_LISTCTRL, wxPoint(0, 0), wxSize(-1, -1));
269 m_editorResourceTree = new wxResourceEditorProjectTree(m_editorFrame, IDC_TREECTRL, wxPoint(0, 0), wxSize(-1, -1),
270 wxTR_HAS_BUTTONS);
271 m_editorPanel = OnCreateEditorPanel(m_editorFrame);
272
273 m_editorResourceTree->SetImageList(& m_imageList);
274
275 // Constraints for toolbar
276 wxLayoutConstraints *c = new wxLayoutConstraints;
277 c->left.SameAs (m_editorFrame, wxLeft, 0);
278 c->top.SameAs (m_editorFrame, wxTop, 0);
279 c->right.SameAs (m_editorFrame, wxRight, 0);
280 c->bottom.Unconstrained();
281 c->width.Unconstrained();
282 c->height.Absolute(28);
283 m_editorToolBar->SetConstraints(c);
284
285 // Constraints for listbox
286 c = new wxLayoutConstraints;
287 c->left.SameAs (m_editorFrame, wxLeft, 0);
288 c->top.SameAs (m_editorToolBar, wxBottom, 0);
289 c->right.Absolute (150);
290 c->bottom.SameAs (m_editorControlList, wxTop, 0);
291 c->width.Unconstrained();
292 c->height.Unconstrained();
293 m_editorResourceTree->SetConstraints(c);
294
295 // Constraints for panel
296 c = new wxLayoutConstraints;
297 c->left.SameAs (m_editorResourceTree, wxRight, 0);
298 c->top.SameAs (m_editorToolBar, wxBottom, 0);
299 c->right.SameAs (m_editorFrame, wxRight, 0);
300 c->bottom.SameAs (m_editorControlList, wxTop, 0);
301 c->width.Unconstrained();
302 c->height.Unconstrained();
303 m_editorPanel->SetConstraints(c);
304
305 // Constraints for control list (bottom window)
306 c = new wxLayoutConstraints;
307 c->left.SameAs (m_editorFrame, wxLeft, 0);
308 c->right.SameAs (m_editorFrame, wxRight, 0);
309 c->bottom.SameAs (m_editorFrame, wxBottom, 0);
310 c->width.Unconstrained();
311 #if defined(__WXGTK__) || defined(__WXMOTIF__)
312 c->height.Absolute(120);
313 #else
314 c->height.Absolute(60);
315 #endif
316
317 m_editorControlList->SetConstraints(c);
318
319 m_editorFrame->SetAutoLayout(TRUE);
320
321 UpdateResourceList();
322
323 m_editorFrame->Show(TRUE);
324 return TRUE;
325 }
326 else
327 {
328 if (m_editorFrame->Close())
329 {
330 m_editorFrame = NULL;
331 m_editorPanel = NULL;
332 }
333 }
334 return TRUE;
335 }
336
337 void wxResourceManager::SetFrameTitle(const wxString& filename)
338 {
339 if (m_editorFrame)
340 {
341 if (filename == wxString(""))
342 m_editorFrame->SetTitle("wxWindows Dialog Editor - untitled");
343 else
344 {
345 wxString str("wxWindows Dialog Editor - ");
346 wxString str2(wxFileNameFromPath(WXSTRINGCAST filename));
347 str += str2;
348 m_editorFrame->SetTitle(str);
349 }
350 }
351 }
352
353 bool wxResourceManager::Save()
354 {
355 if (m_currentFilename == wxString(""))
356 return SaveAs();
357 else
358 return Save(m_currentFilename);
359 }
360
361 bool wxResourceManager::Save(const wxString& filename)
362 {
363 // Ensure all visible windows are saved to their resources
364 m_currentFilename = filename;
365 SetFrameTitle(m_currentFilename);
366 InstantiateAllResourcesFromWindows();
367 if (m_resourceTable.Save(filename))
368 {
369 m_symbolTable.WriteIncludeFile(m_symbolFilename);
370 Modify(FALSE);
371 return TRUE;
372 }
373 else
374 return FALSE;
375 }
376
377 bool wxResourceManager::SaveAs()
378 {
379 wxString s(wxFileSelector("Save resource file", wxPathOnly(WXSTRINGCAST m_currentFilename), wxFileNameFromPath(WXSTRINGCAST m_currentFilename),
380 "wxr", "*.wxr", wxSAVE | wxOVERWRITE_PROMPT, wxTheApp->GetTopWindow()));
381
382 if (s.IsNull() || s == "")
383 return FALSE;
384
385 m_currentFilename = s;
386 wxStripExtension(m_currentFilename);
387 m_currentFilename += ".wxr";
388
389 // Construct include filename from this file
390 m_symbolFilename = m_currentFilename;
391
392 wxStripExtension(m_symbolFilename);
393 m_symbolFilename += ".h";
394
395 Save(m_currentFilename);
396 return TRUE;
397 }
398
399 bool wxResourceManager::SaveIfModified()
400 {
401 if (Modified())
402 return Save();
403 else return TRUE;
404 }
405
406 bool wxResourceManager::Load(const wxString& filename)
407 {
408 return New(TRUE, filename);
409 }
410
411 bool wxResourceManager::New(bool loadFromFile, const wxString& filename)
412 {
413 if (!Clear(TRUE, FALSE))
414 return FALSE;
415
416 m_symbolTable.AddStandardSymbols();
417
418 if (loadFromFile)
419 {
420 wxString str = filename;
421 if (str == wxString(""))
422 {
423 wxString f(wxFileSelector("Open resource file", NULL, NULL, "wxr", "*.wxr", 0, wxTheApp->GetTopWindow()));
424 if (!f.IsNull() && f != "")
425 str = f;
426 else
427 return FALSE;
428 }
429
430 if (!m_resourceTable.ParseResourceFile(str))
431 {
432 wxMessageBox("Could not read file.", "Resource file load error", wxOK | wxICON_EXCLAMATION);
433 return FALSE;
434 }
435 m_currentFilename = str;
436
437 SetFrameTitle(m_currentFilename);
438
439 UpdateResourceList();
440
441 // Construct include filename from this file
442 m_symbolFilename = m_currentFilename;
443
444 wxStripExtension(m_symbolFilename);
445 m_symbolFilename += ".h";
446
447 if (!m_symbolTable.ReadIncludeFile(m_symbolFilename))
448 {
449 wxString str("Could not find include file ");
450 str += m_symbolFilename;
451 str += ".\nDialog Editor maintains a header file containing id symbols to be used in the application.\n";
452 str += "The next time this .wxr file is saved, a header file will be saved also.";
453 wxMessageBox(str, "Dialog Editor Warning", wxOK );
454
455 m_symbolIdCounter = 99;
456 }
457 else
458 {
459 // Set the id counter to the last known id
460 m_symbolIdCounter = m_symbolTable.FindHighestId();
461 }
462
463 // Now check in case some (or all) resources don't have resource ids, or they
464 // don't match the .h file, or something of that nature.
465 bool altered = RepairResourceIds();
466 if (altered)
467 {
468 wxMessageBox("Some resources have had new identifiers associated with them, since they were missing.",
469 "Dialog Editor Warning", wxOK );
470 Modify(TRUE);
471 }
472 else
473 Modify(FALSE);
474
475 return TRUE;
476 }
477 else
478 {
479 SetFrameTitle("");
480 m_currentFilename = "";
481 }
482 Modify(FALSE);
483
484 return TRUE;
485 }
486
487 bool wxResourceManager::Clear(bool WXUNUSED(deleteWindows), bool force)
488 {
489 wxPropertyInfo::CloseWindow();
490
491 if (!force && Modified())
492 {
493 int ans = wxMessageBox("Save modified resource file?", "Dialog Editor", wxYES_NO | wxCANCEL);
494 if (ans == wxCANCEL)
495 return FALSE;
496 if (ans == wxYES)
497 if (!SaveIfModified())
498 return FALSE;
499 if (ans == wxNO)
500 Modify(FALSE);
501 }
502
503 ClearCurrentDialog();
504 DisassociateWindows();
505
506 m_symbolTable.Clear();
507 m_resourceTable.ClearTable();
508 UpdateResourceList();
509
510 return TRUE;
511 }
512
513 bool wxResourceManager::DisassociateWindows()
514 {
515 m_resourceTable.BeginFind();
516 wxNode *node;
517 while ((node = m_resourceTable.Next()))
518 {
519 wxItemResource *res = (wxItemResource *)node->Data();
520 DisassociateResource(res);
521 }
522
523 return TRUE;
524 }
525
526 void wxResourceManager::AssociateResource(wxItemResource *resource, wxWindow *win)
527 {
528 if (!m_resourceAssociations.Get((long)resource))
529 m_resourceAssociations.Put((long)resource, win);
530
531 wxNode *node = resource->GetChildren().First();
532 wxNode* node2 = win->GetChildren().First();
533 while (node && node2)
534 {
535 wxItemResource *child = (wxItemResource *)node->Data();
536 wxWindow* childWindow = (wxWindow*) node2->Data();
537
538 if (child->GetId() != childWindow->GetId())
539 {
540 wxString msg;
541 msg.Printf("AssociateResource: error when associating child window %ld with resource %ld", child->GetId(), childWindow->GetId());
542 wxMessageBox(msg, "Dialog Editor problem", wxOK);
543 }
544 else if (childWindow->GetName() != child->GetName())
545 {
546 wxString msg;
547 msg.Printf("AssociateResource: error when associating child window with resource %s", child->GetName() ? (const char*) child->GetName() : "(unnamed)");
548 wxMessageBox(msg, "Dialog Editor problem", wxOK);
549 }
550 else
551 {
552 AssociateResource(child, childWindow);
553 }
554
555 // New code to avoid the problem of duplicate ids and names. We simply
556 // traverse the child windows and child resources in parallel,
557 // checking for any mismatch.
558 #if 0
559 wxWindow *childWindow = (wxWindow *)m_resourceAssociations.Get((long)child);
560 if (!childWindow)
561 // childWindow = win->FindWindow(child->GetName());
562 childWindow = win->FindWindow(child->GetId());
563 if (childWindow)
564 AssociateResource(child, childWindow);
565 else
566 {
567 wxString msg;
568 msg.Printf("AssociateResource: cannot find child window %s", child->GetName() ? (const char*) child->GetName() : "(unnamed)");
569 wxMessageBox(msg, "Dialog Editor problem", wxOK);
570 }
571 #endif
572 node = node->Next();
573 node2 = node2->Next();
574 }
575 }
576
577 bool wxResourceManager::DisassociateResource(wxItemResource *resource)
578 {
579 wxWindow *win = FindWindowForResource(resource);
580 if (!win)
581 return FALSE;
582
583 // Disassociate children of window
584 wxNode *node = win->GetChildren().First();
585 while (node)
586 {
587 wxWindow *child = (wxWindow *)node->Data();
588 if (child->IsKindOf(CLASSINFO(wxControl)))
589 DisassociateResource(child);
590 node = node->Next();
591 }
592
593 RemoveSelection(win);
594 m_resourceAssociations.Delete((long)resource);
595 return TRUE;
596 }
597
598 bool wxResourceManager::DisassociateResource(wxWindow *win)
599 {
600 wxItemResource *res = FindResourceForWindow(win);
601 if (res)
602 return DisassociateResource(res);
603 else
604 return FALSE;
605 }
606
607 // Saves the window info into the resource, and deletes the
608 // handler. Doesn't actually disassociate the window from
609 // the resources. Replaces OnClose.
610 bool wxResourceManager::SaveInfoAndDeleteHandler(wxWindow* win)
611 {
612 wxItemResource *res = FindResourceForWindow(win);
613
614 if (win->IsKindOf(CLASSINFO(wxPanel)))
615 {
616 wxResourceEditorDialogHandler* handler = (wxResourceEditorDialogHandler*) win->GetEventHandler();
617 win->PopEventHandler();
618
619 // Now reset all child event handlers
620 wxNode *node = win->GetChildren().First();
621 while ( node )
622 {
623 wxWindow *child = (wxWindow *)node->Data();
624 wxEvtHandler *childHandler = child->GetEventHandler();
625 if ( child->IsKindOf(CLASSINFO(wxControl)) && childHandler != child )
626 {
627 child->PopEventHandler(TRUE);
628 }
629 node = node->Next();
630 }
631 delete handler;
632 }
633 else
634 {
635 win->PopEventHandler(TRUE);
636 }
637
638 // Save the information
639 InstantiateResourceFromWindow(res, win, TRUE);
640
641 // DisassociateResource(win);
642
643 return TRUE;
644 }
645
646 // Destroys the window. If this is the 'current' panel, NULLs the
647 // variable.
648 bool wxResourceManager::DeleteWindow(wxWindow* win)
649 {
650 bool clearDisplay = FALSE;
651 if (m_editorPanel->m_childWindow == win)
652 {
653 m_editorPanel->m_childWindow = NULL;
654 clearDisplay = TRUE;
655 }
656
657 win->Destroy();
658
659 if (clearDisplay)
660 m_editorPanel->Clear();
661
662 return TRUE;
663 }
664
665 wxItemResource *wxResourceManager::FindResourceForWindow(wxWindow *win)
666 {
667 m_resourceAssociations.BeginFind();
668 wxNode *node;
669 while ((node = m_resourceAssociations.Next()))
670 {
671 wxWindow *w = (wxWindow *)node->Data();
672 if (w == win)
673 {
674 return (wxItemResource *)node->GetKeyInteger();
675 }
676 }
677 return NULL;
678 }
679
680 wxWindow *wxResourceManager::FindWindowForResource(wxItemResource *resource)
681 {
682 return (wxWindow *)m_resourceAssociations.Get((long)resource);
683 }
684
685
686 void wxResourceManager::MakeUniqueName(char *prefix, char *buf)
687 {
688 while (TRUE)
689 {
690 sprintf(buf, "%s%d", prefix, m_nameCounter);
691 m_nameCounter ++;
692
693 if (!m_resourceTable.FindResource(buf))
694 return;
695 }
696 }
697
698 wxFrame *wxResourceManager::OnCreateEditorFrame(const char *title)
699 {
700 /*
701 int frameWidth = 420;
702 int frameHeight = 300;
703 */
704
705 wxResourceEditorFrame *frame = new wxResourceEditorFrame(this, NULL, title,
706 wxPoint(m_resourceEditorWindowSize.x, m_resourceEditorWindowSize.y),
707 wxSize(m_resourceEditorWindowSize.width, m_resourceEditorWindowSize.height),
708 wxDEFAULT_FRAME_STYLE);
709
710 frame->CreateStatusBar(1);
711
712 frame->SetAutoLayout(TRUE);
713 #ifdef __WXMSW__
714 frame->SetIcon(wxIcon("DIALOGEDICON"));
715 #endif
716 return frame;
717 }
718
719 wxMenuBar *wxResourceManager::OnCreateEditorMenuBar(wxFrame *WXUNUSED(parent))
720 {
721 wxMenuBar *menuBar = new wxMenuBar;
722
723 wxMenu *fileMenu = new wxMenu;
724 fileMenu->Append(RESED_NEW_DIALOG, "New &dialog", "Create a new dialog");
725 fileMenu->AppendSeparator();
726 fileMenu->Append(wxID_NEW, "&New project", "Clear the current project");
727 fileMenu->Append(wxID_OPEN, "&Open...", "Load a resource file");
728 fileMenu->Append(wxID_SAVE, "&Save", "Save a resource file");
729 fileMenu->Append(wxID_SAVEAS, "Save &As...", "Save a resource file as...");
730 fileMenu->Append(RESED_CLEAR, "&Clear", "Clear current resources");
731 fileMenu->AppendSeparator();
732 fileMenu->Append(wxID_EXIT, "E&xit", "Exit resource editor");
733
734 wxMenu *editMenu = new wxMenu;
735 editMenu->Append(RESED_TEST, "&Test Dialog", "Test dialog");
736 editMenu->Append(RESED_RECREATE, "&Recreate", "Recreate the selected resource(s)");
737 editMenu->Append(RESED_DELETE, "&Delete", "Delete the selected resource(s)");
738
739 wxMenu *helpMenu = new wxMenu;
740 helpMenu->Append(RESED_CONTENTS, "&Help topics", "Invokes the on-line help");
741 helpMenu->AppendSeparator();
742 helpMenu->Append(wxID_ABOUT, "&About", "About wxWindows Dialog Editor");
743
744 menuBar->Append(fileMenu, "&File");
745 menuBar->Append(editMenu, "&Edit");
746 menuBar->Append(helpMenu, "&Help");
747
748 return menuBar;
749 }
750
751 wxResourceEditorScrolledWindow *wxResourceManager::OnCreateEditorPanel(wxFrame *parent)
752 {
753 wxResourceEditorScrolledWindow *panel = new wxResourceEditorScrolledWindow(parent, wxDefaultPosition, wxDefaultSize,
754 // wxSUNKEN_BORDER|wxCLIP_CHILDREN);
755 #ifdef __WXMOTIF__
756 wxBORDER);
757 #else
758 wxSUNKEN_BORDER);
759 #endif
760
761 panel->SetScrollbars(10, 10, 100, 100);
762
763 return panel;
764 }
765
766 wxToolBar *wxResourceManager::OnCreateToolBar(wxFrame *parent)
767 {
768 // Load palette bitmaps
769 #ifdef __WXMSW__
770 wxBitmap ToolbarLoadBitmap("LOADTOOL");
771 wxBitmap ToolbarSaveBitmap("SAVETOOL");
772 wxBitmap ToolbarNewBitmap("NEWTOOL");
773 wxBitmap ToolbarVertBitmap("VERTTOOL");
774 wxBitmap ToolbarAlignTBitmap("ALIGNTTOOL");
775 wxBitmap ToolbarAlignBBitmap("ALIGNBTOOL");
776 wxBitmap ToolbarHorizBitmap("HORIZTOOL");
777 wxBitmap ToolbarAlignLBitmap("ALIGNLTOOL");
778 wxBitmap ToolbarAlignRBitmap("ALIGNRTOOL");
779 wxBitmap ToolbarCopySizeBitmap("COPYSIZETOOL");
780 wxBitmap ToolbarToBackBitmap("TOBACKTOOL");
781 wxBitmap ToolbarToFrontBitmap("TOFRONTTOOL");
782 wxBitmap ToolbarHelpBitmap("HELPTOOL");
783 #endif
784 #if defined(__WXGTK__) || defined(__WXMOTIF__)
785 wxBitmap ToolbarLoadBitmap( load_xpm );
786 wxBitmap ToolbarSaveBitmap( save_xpm);
787 wxBitmap ToolbarNewBitmap( new_xpm );
788 wxBitmap ToolbarVertBitmap( vert_xpm );
789 wxBitmap ToolbarAlignTBitmap( alignt_xpm );
790 wxBitmap ToolbarAlignBBitmap( alignb_xpm );
791 wxBitmap ToolbarHorizBitmap( horiz_xpm );
792 wxBitmap ToolbarAlignLBitmap( alignl_xpm );
793 wxBitmap ToolbarAlignRBitmap( alignr_xpm );
794 wxBitmap ToolbarCopySizeBitmap( copysize_xpm );
795 wxBitmap ToolbarToBackBitmap( toback_xpm );
796 wxBitmap ToolbarToFrontBitmap( tofront_xpm );
797 wxBitmap ToolbarHelpBitmap( help_xpm );
798 #endif
799
800 // Create the toolbar
801 EditorToolBar *toolbar = new EditorToolBar(parent, wxPoint(0, 0), wxSize(-1, -1), wxNO_BORDER|wxTB_HORIZONTAL);
802 toolbar->SetMargins(2, 2);
803
804 #ifdef __WXMSW__
805 int width = 24;
806 int dx = 2;
807 int gap = 6;
808 #else
809 int width = 24; // ToolbarLoadBitmap->GetWidth(); ???
810 int dx = 2;
811 int gap = 6;
812 #endif
813 int currentX = gap;
814 toolbar->AddSeparator();
815 toolbar->AddTool(TOOLBAR_NEW, ToolbarNewBitmap, wxNullBitmap,
816 FALSE, currentX, -1, NULL, "New dialog");
817 currentX += width + dx;
818 toolbar->AddTool(TOOLBAR_LOAD_FILE, ToolbarLoadBitmap, wxNullBitmap,
819 FALSE, currentX, -1, NULL, "Load");
820 currentX += width + dx;
821 toolbar->AddTool(TOOLBAR_SAVE_FILE, ToolbarSaveBitmap, wxNullBitmap,
822 FALSE, currentX, -1, NULL, "Save");
823 currentX += width + dx + gap;
824 toolbar->AddSeparator();
825 toolbar->AddTool(TOOLBAR_FORMAT_HORIZ, ToolbarVertBitmap, wxNullBitmap,
826 FALSE, currentX, -1, NULL, "Horizontal align");
827 currentX += width + dx;
828 toolbar->AddTool(TOOLBAR_FORMAT_VERT_TOP_ALIGN, ToolbarAlignTBitmap, wxNullBitmap,
829 FALSE, currentX, -1, NULL, "Top align");
830 currentX += width + dx;
831 toolbar->AddTool(TOOLBAR_FORMAT_VERT_BOT_ALIGN, ToolbarAlignBBitmap, wxNullBitmap,
832 FALSE, currentX, -1, NULL, "Bottom align");
833 currentX += width + dx;
834 toolbar->AddTool(TOOLBAR_FORMAT_VERT, ToolbarHorizBitmap, wxNullBitmap,
835 FALSE, currentX, -1, NULL, "Vertical align");
836 currentX += width + dx;
837 toolbar->AddTool(TOOLBAR_FORMAT_HORIZ_LEFT_ALIGN, ToolbarAlignLBitmap, wxNullBitmap,
838 FALSE, currentX, -1, NULL, "Left align");
839 currentX += width + dx;
840 toolbar->AddTool(TOOLBAR_FORMAT_HORIZ_RIGHT_ALIGN, ToolbarAlignRBitmap, wxNullBitmap,
841 FALSE, currentX, -1, NULL, "Right align");
842 currentX += width + dx;
843 toolbar->AddTool(TOOLBAR_COPY_SIZE, ToolbarCopySizeBitmap, wxNullBitmap,
844 FALSE, currentX, -1, NULL, "Copy size");
845 currentX += width + dx + gap;
846 toolbar->AddSeparator();
847 toolbar->AddTool(TOOLBAR_TO_FRONT, ToolbarToFrontBitmap, wxNullBitmap,
848 FALSE, currentX, -1, NULL, "To front");
849 currentX += width + dx;
850 toolbar->AddTool(TOOLBAR_TO_BACK, ToolbarToBackBitmap, wxNullBitmap,
851 FALSE, currentX, -1, NULL, "To back");
852 currentX += width + dx + gap;
853
854 toolbar->AddSeparator();
855 toolbar->AddTool(TOOLBAR_HELP, ToolbarHelpBitmap, wxNullBitmap,
856 FALSE, currentX, -1, NULL, "Help");
857 currentX += width + dx;
858
859 toolbar->Realize();
860
861 return toolbar;
862 }
863
864 void wxResourceManager::UpdateResourceList()
865 {
866 if (!m_editorResourceTree)
867 return;
868
869 m_editorResourceTree->SetInvalid(TRUE);
870 m_editorResourceTree->DeleteAllItems();
871
872 long id = m_editorResourceTree->AddRoot("Dialogs", 1, 2);
873
874 m_resourceTable.BeginFind();
875 wxNode *node;
876 while ((node = m_resourceTable.Next()))
877 {
878 wxItemResource *res = (wxItemResource *)node->Data();
879 wxString resType(res->GetType());
880 if (resType == "wxDialog" || resType == "wxDialogBox" || resType == "wxPanel" || resType == "wxBitmap")
881 {
882 AddItemsRecursively(id, res);
883 }
884 }
885 m_editorResourceTree->Expand(id);
886 m_editorResourceTree->SetInvalid(FALSE);
887 }
888
889 void wxResourceManager::AddItemsRecursively(long parent, wxItemResource *resource)
890 {
891 wxString theString("");
892 theString = resource->GetName();
893
894 int imageId = 0;
895 wxString resType(resource->GetType());
896 if (resType == "wxDialog" || resType == "wxDialogBox" || resType == "wxPanel")
897 imageId = 0;
898 else
899 imageId = 3;
900
901 long id = m_editorResourceTree->AppendItem(parent, theString, imageId );
902
903 m_editorResourceTree->SetItemData(id, new wxResourceTreeData(resource));
904
905 if (strcmp(resource->GetType(), "wxBitmap") != 0)
906 {
907 wxNode *node = resource->GetChildren().First();
908 while (node)
909 {
910 wxItemResource *res = (wxItemResource *)node->Data();
911 AddItemsRecursively(id, res);
912 node = node->Next();
913 }
914 }
915 // m_editorResourceTree->ExpandItem(id, wxTREE_EXPAND_EXPAND);
916 }
917
918 bool wxResourceManager::EditSelectedResource()
919 {
920 int sel = m_editorResourceTree->GetSelection();
921 if (sel != 0)
922 {
923 wxResourceTreeData *data = (wxResourceTreeData *)m_editorResourceTree->GetItemData(sel);
924 wxItemResource *res = data->GetResource();
925 return Edit(res);
926 }
927 return FALSE;
928 }
929
930 bool wxResourceManager::Edit(wxItemResource *res)
931 {
932 wxPropertyInfo::CloseWindow();
933
934 ClearCurrentDialog();
935
936 wxString resType(res->GetType());
937 wxPanel *panel = (wxPanel *)FindWindowForResource(res);
938
939 if (panel)
940 {
941 wxMessageBox("Should not find panel in wxResourceManager::Edit");
942 return FALSE;
943 }
944 else
945 {
946 // long style = res->GetStyle();
947 // res->SetStyle(style|wxRAISED_BORDER);
948 panel = new wxPanel;
949 wxResourceEditorDialogHandler *handler = new wxResourceEditorDialogHandler(panel, res, panel->GetEventHandler(),
950 this);
951
952 panel->LoadFromResource(m_editorPanel, res->GetName(), &m_resourceTable);
953
954 panel->PushEventHandler(handler);
955
956 // res->SetStyle(style);
957 handler->AddChildHandlers(); // Add event handlers for all controls
958 AssociateResource(res, panel);
959
960 m_editorPanel->m_childWindow = panel;
961 panel->Move(m_editorPanel->GetMarginX(), m_editorPanel->GetMarginY());
962 panel->Show(TRUE);
963 panel->Refresh();
964
965 wxClientDC dc(m_editorPanel);
966 m_editorPanel->DrawTitle(dc);
967 }
968 return FALSE;
969 }
970
971 bool wxResourceManager::CreateNewPanel()
972 {
973 wxPropertyInfo::CloseWindow();
974
975 ClearCurrentDialog();
976
977 char buf[256];
978 MakeUniqueName("dialog", buf);
979
980 wxItemResource *resource = new wxItemResource;
981 resource->SetType("wxDialog");
982 resource->SetName(buf);
983 resource->SetTitle(buf);
984 resource->SetResourceStyle(wxRESOURCE_USE_DEFAULTS);
985 resource->SetResourceStyle(wxRESOURCE_DIALOG_UNITS);
986
987 wxString newIdName;
988 int id = GenerateWindowId("ID_DIALOG", newIdName);
989 resource->SetId(id);
990
991 // This is now guaranteed to be unique, so just add to symbol table
992 m_symbolTable.AddSymbol(newIdName, id);
993
994 m_resourceTable.AddResource(resource);
995
996 wxSize size(400, 300);
997
998 wxPanel *panel = new wxPanel(m_editorPanel, -1,
999 wxPoint(m_editorPanel->GetMarginX(), m_editorPanel->GetMarginY()),
1000 size, wxRAISED_BORDER|wxDEFAULT_DIALOG_STYLE, buf);
1001 m_editorPanel->m_childWindow = panel;
1002
1003 resource->SetStyle(panel->GetWindowStyleFlag());
1004
1005 // Store dialog units in resource
1006 size = panel->ConvertPixelsToDialog(size);
1007
1008 resource->SetSize(10, 10, size.x, size.y);
1009
1010 // For editing in situ we will need to use the hash table to ensure
1011 // we don't dereference invalid pointers.
1012 // resourceWindowTable.Put((long)resource, panel);
1013
1014 wxResourceEditorDialogHandler *handler = new wxResourceEditorDialogHandler(panel, resource, panel->GetEventHandler(),
1015 this);
1016 panel->PushEventHandler(handler);
1017
1018 AssociateResource(resource, panel);
1019 UpdateResourceList();
1020
1021 Modify(TRUE);
1022 m_editorPanel->m_childWindow->Refresh();
1023
1024 // panel->Refresh();
1025
1026 wxClientDC dc(m_editorPanel);
1027 m_editorPanel->DrawTitle(dc);
1028
1029 return TRUE;
1030 }
1031
1032 bool wxResourceManager::CreatePanelItem(wxItemResource *panelResource, wxPanel *panel, char *iType, int x, int y, bool isBitmap)
1033 {
1034 char buf[256];
1035 if (!panel->IsKindOf(CLASSINFO(wxPanel)) && !panel->IsKindOf(CLASSINFO(wxDialog)))
1036 return FALSE;
1037
1038 Modify(TRUE);
1039
1040 wxItemResource *res = new wxItemResource;
1041 wxControl *newItem = NULL;
1042
1043 if ((panelResource->GetResourceStyle() & wxRESOURCE_DIALOG_UNITS) != 0)
1044 {
1045 wxPoint pt = panel->ConvertPixelsToDialog(wxPoint(x, y));
1046 res->SetSize(pt.x, pt.y, -1, -1);
1047 }
1048 else res->SetSize(x, y, -1, -1);
1049
1050 res->SetType(iType);
1051
1052 wxString prefix;
1053
1054 wxString itemType(iType);
1055
1056 if (itemType == "wxButton")
1057 {
1058 prefix = "ID_BUTTON";
1059 MakeUniqueName("button", buf);
1060 res->SetName(buf);
1061 if (isBitmap)
1062 newItem = new wxBitmapButton(panel, -1, * m_bitmapImage, wxPoint(x, y), wxSize(-1, -1), wxBU_AUTODRAW, wxDefaultValidator, buf);
1063 else
1064 newItem = new wxButton(panel, -1, "Button", wxPoint(x, y), wxSize(-1, -1), 0, wxDefaultValidator, buf);
1065 }
1066 if (itemType == "wxBitmapButton")
1067 {
1068 prefix = "ID_BITMAPBUTTON";
1069 MakeUniqueName("button", buf);
1070 res->SetName(buf);
1071 newItem = new wxBitmapButton(panel, -1, * m_bitmapImage, wxPoint(x, y), wxSize(-1, -1), wxBU_AUTODRAW, wxDefaultValidator, buf);
1072 }
1073 else if (itemType == "wxMessage" || itemType == "wxStaticText")
1074 {
1075 prefix = "ID_STATIC";
1076 MakeUniqueName("statictext", buf);
1077 res->SetName(buf);
1078 if (isBitmap)
1079 newItem = new wxStaticBitmap(panel, -1, * m_bitmapImage, wxPoint(x, y), wxSize(0, 0), 0, buf);
1080 else
1081 newItem = new wxStaticText(panel, -1, "Static", wxPoint(x, y), wxSize(-1, -1), 0, buf);
1082 }
1083 else if (itemType == "wxStaticBitmap")
1084 {
1085 prefix = "ID_STATICBITMAP";
1086 MakeUniqueName("static", buf);
1087 res->SetName(buf);
1088 newItem = new wxStaticBitmap(panel, -1, * m_bitmapImage, wxPoint(x, y), wxSize(-1, -1), 0, buf);
1089 }
1090 else if (itemType == "wxCheckBox")
1091 {
1092 prefix = "ID_CHECKBOX";
1093 MakeUniqueName("checkbox", buf);
1094 res->SetName(buf);
1095 newItem = new wxCheckBox(panel, -1, "Checkbox", wxPoint(x, y), wxSize(-1, -1), 0, wxDefaultValidator, buf);
1096 }
1097 else if (itemType == "wxListBox")
1098 {
1099 prefix = "ID_LISTBOX";
1100 MakeUniqueName("listbox", buf);
1101 res->SetName(buf);
1102 newItem = new wxListBox(panel, -1, wxPoint(x, y), wxSize(-1, -1), 0, NULL, 0, wxDefaultValidator, buf);
1103 }
1104 else if (itemType == "wxRadioBox")
1105 {
1106 prefix = "ID_RADIOBOX";
1107 MakeUniqueName("radiobox", buf);
1108 res->SetName(buf);
1109 wxString names[] = { "One", "Two" };
1110 newItem = new wxRadioBox(panel, -1, "Radiobox", wxPoint(x, y), wxSize(-1, -1), 2, names, 2,
1111 wxHORIZONTAL, wxDefaultValidator, buf);
1112 res->SetStringValues(wxStringList("One", "Two", NULL));
1113 }
1114 else if (itemType == "wxRadioButton")
1115 {
1116 prefix = "ID_RADIOBUTTON";
1117 MakeUniqueName("radiobutton", buf);
1118 res->SetName(buf);
1119 wxString names[] = { "One", "Two" };
1120 newItem = new wxRadioButton(panel, -1, "Radiobutton", wxPoint(x, y), wxSize(-1, -1),
1121 0, wxDefaultValidator, buf);
1122 }
1123 else if (itemType == "wxChoice")
1124 {
1125 prefix = "ID_CHOICE";
1126 MakeUniqueName("choice", buf);
1127 res->SetName(buf);
1128 newItem = new wxChoice(panel, -1, wxPoint(x, y), wxSize(-1, -1), 0, NULL, 0, wxDefaultValidator, buf);
1129 }
1130 else if (itemType == "wxComboBox")
1131 {
1132 prefix = "ID_COMBOBOX";
1133 MakeUniqueName("combobox", buf);
1134 res->SetName(buf);
1135 newItem = new wxComboBox(panel, -1, "", wxPoint(x, y), wxSize(-1, -1), 0, NULL, wxCB_DROPDOWN, wxDefaultValidator, buf);
1136 }
1137 else if (itemType == "wxGroupBox" || itemType == "wxStaticBox")
1138 {
1139 prefix = "ID_STATICBOX";
1140 MakeUniqueName("staticbox", buf);
1141 res->SetName(buf);
1142 newItem = new wxStaticBox(panel, -1, "Static", wxPoint(x, y), wxSize(200, 200), 0, buf);
1143 }
1144 else if (itemType == "wxGauge")
1145 {
1146 prefix = "ID_GAUGE";
1147 MakeUniqueName("gauge", buf);
1148 res->SetName(buf);
1149 newItem = new wxGauge(panel, -1, 10, wxPoint(x, y), wxSize(80, 30), wxHORIZONTAL, wxDefaultValidator, buf);
1150 }
1151 else if (itemType == "wxSlider")
1152 {
1153 prefix = "ID_SLIDER";
1154 MakeUniqueName("slider", buf);
1155 res->SetName(buf);
1156 newItem = new wxSlider(panel, -1, 1, 1, 10, wxPoint(x, y), wxSize(120, -1), wxHORIZONTAL, wxDefaultValidator, buf);
1157 }
1158 else if (itemType == "wxText" || itemType == "wxTextCtrl (single-line)")
1159 {
1160 prefix = "ID_TEXTCTRL";
1161 MakeUniqueName("textctrl", buf);
1162 res->SetName(buf);
1163 res->SetType("wxTextCtrl");
1164 newItem = new wxTextCtrl(panel, -1, "", wxPoint(x, y), wxSize(120, -1), 0, wxDefaultValidator, buf);
1165 }
1166 else if (itemType == "wxMultiText" || itemType == "wxTextCtrl (multi-line)")
1167 {
1168 prefix = "ID_TEXTCTRL";
1169 MakeUniqueName("textctrl", buf);
1170 res->SetName(buf);
1171 res->SetType("wxTextCtrl");
1172 newItem = new wxTextCtrl(panel, -1, "", wxPoint(x, y), wxSize(120, 100), wxTE_MULTILINE, wxDefaultValidator, buf);
1173 }
1174 else if (itemType == "wxScrollBar")
1175 {
1176 prefix = "ID_SCROLLBAR";
1177 MakeUniqueName("scrollbar", buf);
1178 res->SetName(buf);
1179 newItem = new wxScrollBar(panel, -1, wxPoint(x, y), wxSize(140, -1), wxHORIZONTAL, wxDefaultValidator, buf);
1180 }
1181 if (!newItem)
1182 return FALSE;
1183
1184 int actualW, actualH;
1185 newItem->GetSize(&actualW, &actualH);
1186 wxSize actualSize(actualW, actualH);
1187
1188 if ((panelResource->GetResourceStyle() & wxRESOURCE_DIALOG_UNITS) != 0)
1189 {
1190 actualSize = panel->ConvertPixelsToDialog(actualSize);
1191 }
1192 res->SetSize(res->GetX(), res->GetY(), actualSize.x, actualSize.y);
1193
1194 wxString newIdName;
1195 int id = GenerateWindowId(prefix, newIdName);
1196 res->SetId(id);
1197
1198 // This is now guaranteed to be unique, so just add to symbol table
1199 m_symbolTable.AddSymbol(newIdName, id);
1200
1201 newItem->PushEventHandler(new wxResourceEditorControlHandler(newItem, newItem));
1202
1203 res->SetStyle(newItem->GetWindowStyleFlag());
1204 AssociateResource(res, newItem);
1205 panelResource->GetChildren().Append(res);
1206
1207 UpdateResourceList();
1208
1209 return TRUE;
1210 }
1211
1212 void wxResourceManager::ClearCurrentDialog()
1213 {
1214 if (m_editorPanel->m_childWindow)
1215 {
1216 SaveInfoAndDeleteHandler(m_editorPanel->m_childWindow);
1217 DisassociateResource(m_editorPanel->m_childWindow);
1218 DeleteWindow(m_editorPanel->m_childWindow);
1219 m_editorPanel->m_childWindow = NULL;
1220 m_editorPanel->Clear();
1221 }
1222 }
1223
1224 bool wxResourceManager::TestCurrentDialog(wxWindow* parent)
1225 {
1226 if (m_editorPanel->m_childWindow)
1227 {
1228 wxItemResource* item = FindResourceForWindow(m_editorPanel->m_childWindow);
1229 if (!item)
1230 return FALSE;
1231
1232 // Make sure the resources are up-to-date w.r.t. the window
1233 InstantiateResourceFromWindow(item, m_editorPanel->m_childWindow, TRUE);
1234
1235 wxDialog* dialog = new wxDialog;
1236 bool success = FALSE;
1237 if (dialog->LoadFromResource(parent, item->GetName(), & m_resourceTable))
1238 {
1239 dialog->Centre();
1240 dialog->ShowModal();
1241 success = TRUE;
1242 }
1243 return success;
1244 }
1245 return FALSE;
1246 }
1247
1248 // Find the first dialog or panel for which
1249 // there is a selected panel item.
1250 wxWindow *wxResourceManager::FindParentOfSelection()
1251 {
1252 m_resourceTable.BeginFind();
1253 wxNode *node;
1254 while ((node = m_resourceTable.Next()))
1255 {
1256 wxItemResource *res = (wxItemResource *)node->Data();
1257 wxWindow *win = FindWindowForResource(res);
1258 if (win)
1259 {
1260 wxNode *node1 = win->GetChildren().First();
1261 while (node1)
1262 {
1263 wxControl *item = (wxControl *)node1->Data();
1264 wxResourceEditorControlHandler *childHandler = (wxResourceEditorControlHandler *)item->GetEventHandler();
1265 if (item->IsKindOf(CLASSINFO(wxControl)) && childHandler->IsSelected())
1266 return win;
1267 node1 = node1->Next();
1268 }
1269 }
1270 }
1271 return NULL;
1272 }
1273
1274 // Format the panel items according to 'flag'
1275 void wxResourceManager::AlignItems(int flag)
1276 {
1277 wxWindow *win = FindParentOfSelection();
1278 if (!win)
1279 return;
1280
1281 wxNode *node = GetSelections().First();
1282 if (!node)
1283 return;
1284
1285 wxControl *firstSelection = (wxControl *)node->Data();
1286 if (firstSelection->GetParent() != win)
1287 return;
1288
1289 int firstX, firstY;
1290 int firstW, firstH;
1291 firstSelection->GetPosition(&firstX, &firstY);
1292 firstSelection->GetSize(&firstW, &firstH);
1293 int centreX = (int)(firstX + (firstW / 2));
1294 int centreY = (int)(firstY + (firstH / 2));
1295
1296 while ((node = node->Next()))
1297 {
1298 wxControl *item = (wxControl *)node->Data();
1299 if (item->GetParent() == win)
1300 {
1301 int x, y, w, h;
1302 item->GetPosition(&x, &y);
1303 item->GetSize(&w, &h);
1304
1305 int newX, newY;
1306
1307 switch (flag)
1308 {
1309 case TOOLBAR_FORMAT_HORIZ:
1310 {
1311 newX = x;
1312 newY = (int)(centreY - (h/2.0));
1313 break;
1314 }
1315 case TOOLBAR_FORMAT_VERT:
1316 {
1317 newX = (int)(centreX - (w/2.0));
1318 newY = y;
1319 break;
1320 }
1321 case TOOLBAR_FORMAT_HORIZ_LEFT_ALIGN:
1322 {
1323 newX = firstX;
1324 newY = y;
1325 break;
1326 }
1327 case TOOLBAR_FORMAT_VERT_TOP_ALIGN:
1328 {
1329 newX = x;
1330 newY = firstY;
1331 break;
1332 }
1333 case TOOLBAR_FORMAT_HORIZ_RIGHT_ALIGN:
1334 {
1335 newX = firstX + firstW - w;
1336 newY = y;
1337 break;
1338 }
1339 case TOOLBAR_FORMAT_VERT_BOT_ALIGN:
1340 {
1341 newX = x;
1342 newY = firstY + firstH - h;
1343 break;
1344 }
1345 default:
1346 newX = x; newY = y;
1347 break;
1348 }
1349
1350 wxItemResource* resource = wxResourceManager::GetCurrentResourceManager()->FindResourceForWindow(item);
1351 wxItemResource* parentResource = wxResourceManager::GetCurrentResourceManager()->FindResourceForWindow(item->GetParent());
1352
1353 item->SetSize(newX, newY, w, h);
1354
1355 // Also update the associated resource
1356 // We need to convert to dialog units if this is not a dialog or panel, but
1357 // the parent resource specifies dialog units.
1358 if (parentResource->GetResourceStyle() & wxRESOURCE_DIALOG_UNITS)
1359 {
1360 wxPoint pt = item->GetParent()->ConvertPixelsToDialog(wxPoint(newX, newY));
1361 newX = pt.x; newY = pt.y;
1362 wxSize sz = item->GetParent()->ConvertPixelsToDialog(wxSize(w, h));
1363 w = sz.x; h = sz.y;
1364 }
1365 resource->SetSize(newX, newY, w, h);
1366 }
1367 }
1368 win->Refresh();
1369 }
1370
1371 // Copy the first image's size to subsequent images
1372 void wxResourceManager::CopySize()
1373 {
1374 wxWindow *win = FindParentOfSelection();
1375 if (!win)
1376 return;
1377
1378 wxNode *node = GetSelections().First();
1379 if (!node)
1380 return;
1381
1382 wxControl *firstSelection = (wxControl *)node->Data();
1383 if (firstSelection->GetParent() != win)
1384 return;
1385
1386 int firstX, firstY;
1387 int firstW, firstH;
1388 firstSelection->GetPosition(&firstX, &firstY);
1389 firstSelection->GetSize(&firstW, &firstH);
1390
1391 while ((node = node->Next()))
1392 {
1393 wxControl *item = (wxControl *)node->Data();
1394 if (item->GetParent() == win)
1395 {
1396 item->SetSize(-1, -1, firstW, firstH);
1397
1398 int fw = firstW;
1399 int fh = firstH;
1400
1401 wxItemResource* resource = wxResourceManager::GetCurrentResourceManager()->FindResourceForWindow(item);
1402 wxItemResource* parentResource = wxResourceManager::GetCurrentResourceManager()->FindResourceForWindow(item->GetParent());
1403
1404 // Also update the associated resource
1405 // We need to convert to dialog units if this is not a dialog or panel, but
1406 // the parent resource specifies dialog units.
1407 if (parentResource->GetResourceStyle() & wxRESOURCE_DIALOG_UNITS)
1408 {
1409 wxSize sz = item->GetParent()->ConvertPixelsToDialog(wxSize(firstW, firstH));
1410 fw = sz.x; fh = sz.y;
1411 }
1412 resource->SetSize(resource->GetX(), resource->GetY(), fw, fh);
1413
1414 }
1415 }
1416 win->Refresh();
1417 }
1418
1419 void wxResourceManager::ToBackOrFront(bool toBack)
1420 {
1421 wxWindow *win = FindParentOfSelection();
1422 if (!win)
1423 return;
1424 wxItemResource *winResource = FindResourceForWindow(win);
1425
1426 wxNode *node = GetSelections().First();
1427 while (node)
1428 {
1429 wxControl *item = (wxControl *)node->Data();
1430 wxItemResource *itemResource = FindResourceForWindow(item);
1431 if (item->GetParent() == win)
1432 {
1433 win->GetChildren().DeleteObject(item);
1434 if (winResource)
1435 winResource->GetChildren().DeleteObject(itemResource);
1436 if (toBack)
1437 {
1438 win->GetChildren().Insert(item);
1439 if (winResource)
1440 winResource->GetChildren().Insert(itemResource);
1441 }
1442 else
1443 {
1444 win->GetChildren().Append(item);
1445 if (winResource)
1446 winResource->GetChildren().Append(itemResource);
1447 }
1448 }
1449 node = node->Next();
1450 }
1451 // win->Refresh();
1452 }
1453
1454 void wxResourceManager::AddSelection(wxWindow *win)
1455 {
1456 if (!m_selections.Member(win))
1457 m_selections.Append(win);
1458 }
1459
1460 void wxResourceManager::RemoveSelection(wxWindow *win)
1461 {
1462 m_selections.DeleteObject(win);
1463 }
1464
1465 void wxResourceManager::DeselectItemIfNecessary(wxWindow *win)
1466 {
1467 if (win->IsKindOf(CLASSINFO(wxControl)) && (win->GetEventHandler() != win))
1468 {
1469 // Deselect and refresh window in case we leave selection
1470 // handles behind
1471 wxControl *item = (wxControl *)win;
1472 wxResourceEditorControlHandler *childHandler = (wxResourceEditorControlHandler *)item->GetEventHandler();
1473 if (childHandler->IsSelected())
1474 {
1475 wxResourceManager::GetCurrentResourceManager()->RemoveSelection(item);
1476 childHandler->SelectItem(FALSE);
1477 #ifndef __WXGTK__
1478 item->GetParent()->Refresh();
1479 #endif
1480 }
1481 }
1482 }
1483
1484 // Need to search through resource table removing this from
1485 // any resource which has this as a parent.
1486 bool wxResourceManager::RemoveResourceFromParent(wxItemResource *res)
1487 {
1488 m_resourceTable.BeginFind();
1489 wxNode *node;
1490 while ((node = m_resourceTable.Next()))
1491 {
1492 wxItemResource *thisRes = (wxItemResource *)node->Data();
1493 if (thisRes->GetChildren().Member(res))
1494 {
1495 thisRes->GetChildren().DeleteObject(res);
1496 return TRUE;
1497 }
1498 }
1499 return FALSE;
1500 }
1501
1502 bool wxResourceManager::DeleteResource(wxItemResource *res)
1503 {
1504 if (!res)
1505 return FALSE;
1506
1507 RemoveResourceFromParent(res);
1508
1509 wxNode *node = res->GetChildren().First();
1510 while (node)
1511 {
1512 wxNode *next = node->Next();
1513 wxItemResource *child = (wxItemResource *)node->Data();
1514 DeleteResource(child);
1515 node = next;
1516 }
1517
1518 // If this is a button or message resource, delete the
1519 // associate bitmap resource if not being used.
1520 wxString resType(res->GetType());
1521
1522 /* shouldn't have to do this now bitmaps are ref-counted
1523 if ((resType == "wxMessage" || resType == "wxStaticBitmap" || resType == "wxButton" || resType == "wxBitmapButton") && res->GetValue4())
1524 {
1525 PossiblyDeleteBitmapResource(res->GetValue4());
1526 }
1527 */
1528
1529 // Remove symbol from table if appropriate
1530 if (!IsSymbolUsed(res, res->GetId()))
1531 {
1532 m_symbolTable.RemoveSymbol(res->GetId());
1533 }
1534
1535 m_resourceTable.Delete(res->GetName());
1536 delete res;
1537 Modify(TRUE);
1538 return TRUE;
1539 }
1540
1541 bool wxResourceManager::DeleteResource(wxWindow *win)
1542 {
1543 DeselectItemIfNecessary(win);
1544
1545 wxItemResource *res = FindResourceForWindow(win);
1546
1547 DisassociateResource(res);
1548 DeleteResource(res);
1549 UpdateResourceList();
1550
1551 return TRUE;
1552 }
1553
1554 // Will eventually have bitmap type information, for different
1555 // kinds of bitmap.
1556 wxString wxResourceManager::AddBitmapResource(const wxString& filename)
1557 {
1558 wxItemResource *resource = FindBitmapResourceByFilename(filename);
1559 if (!resource)
1560 {
1561 char buf[256];
1562 MakeUniqueName("bitmap", buf);
1563 resource = new wxItemResource;
1564 resource->SetType("wxBitmap");
1565 resource->SetName(buf);
1566
1567 // A bitmap resource has one or more children, specifying
1568 // alternative bitmaps.
1569 wxItemResource *child = new wxItemResource;
1570 child->SetType("wxBitmap");
1571 child->SetName(filename);
1572 child->SetValue1(wxBITMAP_TYPE_BMP);
1573 child->SetValue2(RESOURCE_PLATFORM_ANY);
1574 child->SetValue3(0); // Depth
1575 child->SetSize(0,0,0,0);
1576 resource->GetChildren().Append(child);
1577
1578 m_resourceTable.AddResource(resource);
1579
1580 UpdateResourceList();
1581 }
1582 if (resource)
1583 return resource->GetName();
1584 else
1585 return wxEmptyString;
1586 }
1587
1588 // Delete the bitmap resource if it isn't being used by another resource.
1589 void wxResourceManager::PossiblyDeleteBitmapResource(const wxString& resourceName)
1590 {
1591 if (!IsBitmapResourceUsed(resourceName))
1592 {
1593 wxItemResource *res = m_resourceTable.FindResource(resourceName);
1594 DeleteResource(res);
1595 UpdateResourceList();
1596 }
1597 }
1598
1599 bool wxResourceManager::IsBitmapResourceUsed(const wxString& resourceName)
1600 {
1601 m_resourceTable.BeginFind();
1602 wxNode *node;
1603 while ((node = m_resourceTable.Next()))
1604 {
1605 wxItemResource *res = (wxItemResource *)node->Data();
1606 wxString resType(res->GetType());
1607 if (resType == "wxDialog")
1608 {
1609 wxNode *node1 = res->GetChildren().First();
1610 while (node1)
1611 {
1612 wxItemResource *child = (wxItemResource *)node1->Data();
1613 wxString childResType(child->GetType());
1614
1615 if ((childResType == "wxMessage" || childResType == "wxButton") &&
1616 child->GetValue4() &&
1617 (strcmp(child->GetValue4(), resourceName) == 0))
1618 return TRUE;
1619 node1 = node1->Next();
1620 }
1621 }
1622 }
1623 return FALSE;
1624 }
1625
1626 // Given a wxButton or wxMessage, find the corresponding bitmap filename.
1627 wxString wxResourceManager::FindBitmapFilenameForResource(wxItemResource *resource)
1628 {
1629 if (!resource || (resource->GetValue4() == ""))
1630 return wxEmptyString;
1631 wxItemResource *bitmapResource = m_resourceTable.FindResource(resource->GetValue4());
1632 if (!bitmapResource)
1633 return wxEmptyString;
1634
1635 wxNode *node = bitmapResource->GetChildren().First();
1636 while (node)
1637 {
1638 // Eventually augment this to return a bitmap of the right kind or something...
1639 // Maybe the root of the filename remains the same, so it doesn't matter which we
1640 // pick up. Otherwise how do we specify multiple filenames... too boring...
1641 wxItemResource *child = (wxItemResource *)node->Data();
1642 return child->GetName();
1643
1644 //node = node->Next();
1645 }
1646 return wxEmptyString;
1647 }
1648
1649 wxItemResource *wxResourceManager::FindBitmapResourceByFilename(const wxString& filename)
1650 {
1651 m_resourceTable.BeginFind();
1652 wxNode *node;
1653 while ((node = m_resourceTable.Next()))
1654 {
1655 wxItemResource *res = (wxItemResource *)node->Data();
1656 wxString resType(res->GetType());
1657 if (resType == "wxBitmap")
1658 {
1659 wxNode *node1 = res->GetChildren().First();
1660 while (node1)
1661 {
1662 wxItemResource *child = (wxItemResource *)node1->Data();
1663 if (child->GetName() && (strcmp(child->GetName(), filename) == 0))
1664 return res;
1665 node1 = node1->Next();
1666 }
1667 }
1668 }
1669 return NULL;
1670 }
1671
1672 // Is this window identifier symbol in use?
1673 // Let's assume that we can't have 2 names for the same integer id.
1674 // Therefore we can tell by the integer id whether the symbol is
1675 // in use.
1676 bool wxResourceManager::IsSymbolUsed(wxItemResource* thisResource, wxWindowID id)
1677 {
1678 m_resourceTable.BeginFind();
1679 wxNode *node;
1680 while ((node = m_resourceTable.Next()))
1681 {
1682 wxItemResource *res = (wxItemResource *)node->Data();
1683
1684 wxString resType(res->GetType());
1685 if (resType == "wxDialog" || resType == "wxDialogBox" || resType == "wxPanel")
1686 {
1687 if ((res != thisResource) && (res->GetId() == id))
1688 return TRUE;
1689
1690 wxNode *node1 = res->GetChildren().First();
1691 while (node1)
1692 {
1693 wxItemResource *child = (wxItemResource *)node1->Data();
1694 if ((child != thisResource) && (child->GetId() == id))
1695 return TRUE;
1696 node1 = node1->Next();
1697 }
1698 }
1699 }
1700 return FALSE;
1701 }
1702
1703 // Is this window identifier compatible with the given name? (i.e.
1704 // does it already exist under a different name)
1705 bool wxResourceManager::IsIdentifierOK(const wxString& name, wxWindowID id)
1706 {
1707 if (m_symbolTable.SymbolExists(name))
1708 {
1709 int foundId = m_symbolTable.GetIdForSymbol(name);
1710 if (foundId != id)
1711 return FALSE;
1712 }
1713 return TRUE;
1714 }
1715
1716 // Change all integer ids that match oldId, to newId.
1717 // This is necessary if an id is changed for one resource - all resources
1718 // must be changed.
1719 void wxResourceManager::ChangeIds(int oldId, int newId)
1720 {
1721 m_resourceTable.BeginFind();
1722 wxNode *node;
1723 while ((node = m_resourceTable.Next()))
1724 {
1725 wxItemResource *res = (wxItemResource *)node->Data();
1726
1727 wxString resType(res->GetType());
1728 if (resType == "wxDialog" || resType == "wxDialogBox" || resType == "wxPanel")
1729 {
1730 if (res->GetId() == oldId)
1731 res->SetId(newId);
1732
1733 wxNode *node1 = res->GetChildren().First();
1734 while (node1)
1735 {
1736 wxItemResource *child = (wxItemResource *)node1->Data();
1737 if (child->GetId() == oldId)
1738 child->SetId(newId);
1739
1740 node1 = node1->Next();
1741 }
1742 }
1743 }
1744 }
1745
1746 // If any resource ids were missing (or their symbol was missing),
1747 // repair them i.e. give them new ids. Returns TRUE if any resource
1748 // needed repairing.
1749 bool wxResourceManager::RepairResourceIds()
1750 {
1751 bool repaired = FALSE;
1752
1753 m_resourceTable.BeginFind();
1754 wxNode *node;
1755 while ((node = m_resourceTable.Next()))
1756 {
1757 wxItemResource *res = (wxItemResource *)node->Data();
1758 wxString resType(res->GetType());
1759 if (resType == "wxDialog" || resType == "wxDialogBox" || resType == "wxPanel")
1760 {
1761
1762 if ( (res->GetId() == 0) || ((res->GetId() > 0) && !m_symbolTable.IdExists(res->GetId())) )
1763 {
1764 wxString newSymbolName;
1765 int newId = GenerateWindowId("ID_DIALOG", newSymbolName) ;
1766
1767 if (res->GetId() == 0)
1768 {
1769 res->SetId(newId);
1770 m_symbolTable.AddSymbol(newSymbolName, newId);
1771 }
1772 else
1773 {
1774 m_symbolTable.AddSymbol(newSymbolName, res->GetId());
1775 }
1776
1777 repaired = TRUE;
1778 }
1779
1780 wxNode *node1 = res->GetChildren().First();
1781 while (node1)
1782 {
1783 wxItemResource *child = (wxItemResource *)node1->Data();
1784
1785 if ( (child->GetId() == 0) || ((child->GetId() > 0) && !m_symbolTable.IdExists(child->GetId())) )
1786 {
1787 wxString newSymbolName;
1788 int newId = GenerateWindowId("ID_CONTROL", newSymbolName) ;
1789
1790 if (child->GetId() == 0)
1791 {
1792 child->SetId(newId);
1793 m_symbolTable.AddSymbol(newSymbolName, newId);
1794 }
1795 else
1796 {
1797 m_symbolTable.AddSymbol(newSymbolName, child->GetId());
1798 }
1799
1800 repaired = TRUE;
1801 }
1802
1803 node1 = node1->Next();
1804 }
1805 }
1806 }
1807 return repaired;
1808 }
1809
1810
1811 // Deletes 'win' and creates a new window from the resource that
1812 // was associated with it. E.g. if you can't change properties on the
1813 // fly, you'll need to delete the window and create it again.
1814 wxWindow *wxResourceManager::RecreateWindowFromResource(wxWindow *win, wxWindowPropertyInfo *info, bool instantiateFirst)
1815 {
1816 wxItemResource *resource = FindResourceForWindow(win);
1817
1818 // Put the current window properties into the wxItemResource object
1819
1820 wxWindowPropertyInfo *newInfo = NULL;
1821 if (!info)
1822 {
1823 newInfo = CreatePropertyInfoForWindow(win);
1824 info = newInfo;
1825 }
1826
1827 // May not always want to copy values back from the resource
1828 if (instantiateFirst)
1829 info->InstantiateResource(resource);
1830
1831 wxWindow *newWin = NULL;
1832 wxWindow *parent = win->GetParent();
1833 wxItemResource* parentResource = NULL;
1834 if (parent)
1835 parentResource = FindResourceForWindow(parent);
1836
1837 if (win->IsKindOf(CLASSINFO(wxPanel)))
1838 {
1839 Edit(resource);
1840 newWin = FindWindowForResource(resource);
1841 }
1842 else
1843 {
1844 DisassociateResource(resource);
1845 if (win->GetEventHandler() != win)
1846 win->PopEventHandler(TRUE);
1847
1848 DeleteWindow(win);
1849 newWin = m_resourceTable.CreateItem((wxPanel *)parent, resource, parentResource);
1850 newWin->PushEventHandler(new wxResourceEditorControlHandler((wxControl*) newWin, (wxControl*) newWin));
1851 AssociateResource(resource, newWin);
1852 UpdateResourceList();
1853 }
1854
1855 if (info)
1856 info->SetPropertyWindow(newWin);
1857
1858 if (newInfo)
1859 delete newInfo;
1860
1861 return newWin;
1862 }
1863
1864 // Delete resource highlighted in the listbox
1865 bool wxResourceManager::DeleteSelection()
1866 {
1867 int sel = m_editorResourceTree->GetSelection();
1868 if (sel != 0)
1869 {
1870 wxResourceTreeData *data = (wxResourceTreeData *)m_editorResourceTree->GetItemData(sel);
1871 wxItemResource *res = data->GetResource();
1872 wxWindow *win = FindWindowForResource(res);
1873 if (win)
1874 {
1875 DeleteResource(win);
1876 DeleteWindow(win);
1877 UpdateResourceList();
1878 Modify(TRUE);
1879 }
1880 return TRUE;
1881 }
1882
1883 return FALSE;
1884 }
1885
1886 // Delete resource highlighted in the listbox
1887 bool wxResourceManager::RecreateSelection()
1888 {
1889 wxNode *node = GetSelections().First();
1890 while (node)
1891 {
1892 wxControl *item = (wxControl *)node->Data();
1893 wxResourceEditorControlHandler *childHandler = (wxResourceEditorControlHandler *)item->GetEventHandler();
1894 wxNode *next = node->Next();
1895 childHandler->SelectItem(FALSE);
1896
1897 RemoveSelection(item);
1898
1899 RecreateWindowFromResource(item);
1900
1901 node = next;
1902 }
1903 return TRUE;
1904 }
1905
1906 bool wxResourceManager::EditDialog(wxDialog *WXUNUSED(dialog), wxWindow *WXUNUSED(parent))
1907 {
1908 return FALSE;
1909 }
1910
1911 // Ensures that all currently shown windows are saved to resources,
1912 // e.g. just before writing to a .wxr file.
1913 bool wxResourceManager::InstantiateAllResourcesFromWindows()
1914 {
1915 m_resourceTable.BeginFind();
1916 wxNode *node;
1917 while ((node = m_resourceTable.Next()))
1918 {
1919 wxItemResource *res = (wxItemResource *)node->Data();
1920 wxString resType(res->GetType());
1921
1922 if (resType == "wxDialog")
1923 {
1924 wxWindow *win = (wxWindow *)FindWindowForResource(res);
1925 if (win)
1926 InstantiateResourceFromWindow(res, win, TRUE);
1927 }
1928 else if (resType == "wxPanel")
1929 {
1930 wxWindow *win = (wxWindow *)FindWindowForResource(res);
1931 if (win)
1932 InstantiateResourceFromWindow(res, win, TRUE);
1933 }
1934 }
1935 return TRUE;
1936 }
1937
1938 bool wxResourceManager::InstantiateResourceFromWindow(wxItemResource *resource, wxWindow *window, bool recurse)
1939 {
1940 wxWindowPropertyInfo *info = CreatePropertyInfoForWindow(window);
1941 info->SetResource(resource);
1942 info->InstantiateResource(resource);
1943 delete info;
1944
1945 if (recurse)
1946 {
1947 wxNode *node = resource->GetChildren().First();
1948 while (node)
1949 {
1950 wxItemResource *child = (wxItemResource *)node->Data();
1951 wxWindow *childWindow = FindWindowForResource(child);
1952
1953 if (!childWindow)
1954 {
1955 char buf[200];
1956 sprintf(buf, "Could not find window %s", (const char*) child->GetName());
1957 wxMessageBox(buf, "Dialog Editor problem", wxOK);
1958 }
1959 else
1960 InstantiateResourceFromWindow(child, childWindow, recurse);
1961 node = node->Next();
1962 }
1963 }
1964
1965 return TRUE;
1966 }
1967
1968 // Create a window information object for the give window
1969 wxWindowPropertyInfo *wxResourceManager::CreatePropertyInfoForWindow(wxWindow *win)
1970 {
1971 wxWindowPropertyInfo *info = NULL;
1972 if (win->IsKindOf(CLASSINFO(wxScrollBar)))
1973 {
1974 info = new wxScrollBarPropertyInfo(win);
1975 }
1976 else if (win->IsKindOf(CLASSINFO(wxStaticBox)))
1977 {
1978 info = new wxGroupBoxPropertyInfo(win);
1979 }
1980 else if (win->IsKindOf(CLASSINFO(wxCheckBox)))
1981 {
1982 info = new wxCheckBoxPropertyInfo(win);
1983 }
1984 else if (win->IsKindOf(CLASSINFO(wxSlider)))
1985 {
1986 info = new wxSliderPropertyInfo(win);
1987 }
1988 else if (win->IsKindOf(CLASSINFO(wxGauge)))
1989 {
1990 info = new wxGaugePropertyInfo(win);
1991 }
1992 else if (win->IsKindOf(CLASSINFO(wxListBox)))
1993 {
1994 info = new wxListBoxPropertyInfo(win);
1995 }
1996 else if (win->IsKindOf(CLASSINFO(wxRadioBox)))
1997 {
1998 info = new wxRadioBoxPropertyInfo(win);
1999 }
2000 else if (win->IsKindOf(CLASSINFO(wxRadioButton)))
2001 {
2002 info = new wxRadioButtonPropertyInfo(win);
2003 }
2004 else if (win->IsKindOf(CLASSINFO(wxComboBox)))
2005 {
2006 info = new wxComboBoxPropertyInfo(win);
2007 }
2008 else if (win->IsKindOf(CLASSINFO(wxChoice)))
2009 {
2010 info = new wxChoicePropertyInfo(win);
2011 }
2012 else if (win->IsKindOf(CLASSINFO(wxBitmapButton)))
2013 {
2014 info = new wxBitmapButtonPropertyInfo(win);
2015 }
2016 else if (win->IsKindOf(CLASSINFO(wxButton)))
2017 {
2018 info = new wxButtonPropertyInfo(win);
2019 }
2020 else if (win->IsKindOf(CLASSINFO(wxStaticBitmap)))
2021 {
2022 info = new wxStaticBitmapPropertyInfo(win);
2023 }
2024 else if (win->IsKindOf(CLASSINFO(wxStaticText)))
2025 {
2026 info = new wxStaticTextPropertyInfo(win);
2027 }
2028 else if (win->IsKindOf(CLASSINFO(wxTextCtrl)))
2029 {
2030 info = new wxTextPropertyInfo(win);
2031 }
2032 else if (win->IsKindOf(CLASSINFO(wxPanel)))
2033 {
2034 info = new wxPanelPropertyInfo(win);
2035 }
2036 else
2037 {
2038 info = new wxWindowPropertyInfo(win);
2039 }
2040 return info;
2041 }
2042
2043 // Edit the given window
2044 void wxResourceManager::EditWindow(wxWindow *win)
2045 {
2046 wxWindowPropertyInfo *info = CreatePropertyInfoForWindow(win);
2047 if (info)
2048 {
2049 info->SetResource(FindResourceForWindow(win));
2050 wxString str("Editing ");
2051 str += win->GetClassInfo()->GetClassName();
2052 str += ": ";
2053 if (win->GetName() != "")
2054 str += win->GetName();
2055 else
2056 str += "properties";
2057 info->Edit(NULL, str);
2058 }
2059 }
2060
2061 // Generate a window id and a first stab at a name
2062 int wxResourceManager::GenerateWindowId(const wxString& prefix, wxString& idName)
2063 {
2064 m_symbolIdCounter ++;
2065 while (m_symbolTable.IdExists(m_symbolIdCounter))
2066 m_symbolIdCounter ++;
2067
2068 int nameId = m_symbolIdCounter;
2069
2070 wxString str;
2071 str.Printf("%d", nameId);
2072 idName = prefix + str;
2073
2074 while (m_symbolTable.SymbolExists(idName))
2075 {
2076 nameId ++;
2077 str.Printf("%d", nameId);
2078 idName = prefix + str;
2079 }
2080
2081 return m_symbolIdCounter;
2082 }
2083
2084
2085 /*
2086 * Resource editor frame
2087 */
2088
2089 IMPLEMENT_CLASS(wxResourceEditorFrame, wxFrame)
2090
2091 BEGIN_EVENT_TABLE(wxResourceEditorFrame, wxFrame)
2092 EVT_MENU(wxID_NEW, wxResourceEditorFrame::OnNew)
2093 EVT_MENU(RESED_NEW_DIALOG, wxResourceEditorFrame::OnNewDialog)
2094 EVT_MENU(wxID_OPEN, wxResourceEditorFrame::OnOpen)
2095 EVT_MENU(RESED_CLEAR, wxResourceEditorFrame::OnClear)
2096 EVT_MENU(wxID_SAVE, wxResourceEditorFrame::OnSave)
2097 EVT_MENU(wxID_SAVEAS, wxResourceEditorFrame::OnSaveAs)
2098 EVT_MENU(wxID_EXIT, wxResourceEditorFrame::OnExit)
2099 EVT_MENU(wxID_ABOUT, wxResourceEditorFrame::OnAbout)
2100 EVT_MENU(RESED_CONTENTS, wxResourceEditorFrame::OnContents)
2101 EVT_MENU(RESED_DELETE, wxResourceEditorFrame::OnDeleteSelection)
2102 EVT_MENU(RESED_RECREATE, wxResourceEditorFrame::OnRecreateSelection)
2103 EVT_MENU(RESED_TEST, wxResourceEditorFrame::OnTest)
2104 EVT_CLOSE(wxResourceEditorFrame::OnCloseWindow)
2105 END_EVENT_TABLE()
2106
2107 wxResourceEditorFrame::wxResourceEditorFrame(wxResourceManager *resMan, wxFrame *parent, const wxString& title,
2108 const wxPoint& pos, const wxSize& size, long style, const wxString& name):
2109 wxFrame(parent, -1, title, pos, size, style, name)
2110 {
2111 manager = resMan;
2112 }
2113
2114 wxResourceEditorFrame::~wxResourceEditorFrame()
2115 {
2116 }
2117
2118 void wxResourceEditorFrame::OnNew(wxCommandEvent& WXUNUSED(event))
2119 {
2120 manager->New(FALSE);
2121 }
2122
2123 void wxResourceEditorFrame::OnNewDialog(wxCommandEvent& WXUNUSED(event))
2124 {
2125 manager->CreateNewPanel();
2126 }
2127
2128 void wxResourceEditorFrame::OnOpen(wxCommandEvent& WXUNUSED(event))
2129 {
2130 manager->New(TRUE);
2131 }
2132
2133 void wxResourceEditorFrame::OnClear(wxCommandEvent& WXUNUSED(event))
2134 {
2135 manager->Clear(TRUE, FALSE);
2136 }
2137
2138 void wxResourceEditorFrame::OnSave(wxCommandEvent& WXUNUSED(event))
2139 {
2140 manager->Save();
2141 }
2142
2143 void wxResourceEditorFrame::OnSaveAs(wxCommandEvent& WXUNUSED(event))
2144 {
2145 manager->SaveAs();
2146 }
2147
2148 void wxResourceEditorFrame::OnExit(wxCommandEvent& WXUNUSED(event))
2149 {
2150 manager->Clear(TRUE, FALSE) ;
2151 this->Destroy();
2152 }
2153
2154 void wxResourceEditorFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
2155 {
2156 char buf[300];
2157 sprintf(buf, "wxWindows Dialog Editor %.1f\nAuthor: Julian Smart <julian.smart@ukonline.co.uk>\nJulian Smart (c) 1996-1999", wxDIALOG_EDITOR_VERSION);
2158 wxMessageBox(buf, "About Dialog Editor", wxOK|wxCENTRE);
2159 }
2160
2161 void wxResourceEditorFrame::OnTest(wxCommandEvent& WXUNUSED(event))
2162 {
2163 manager->TestCurrentDialog(this);
2164 }
2165
2166 void wxResourceEditorFrame::OnContents(wxCommandEvent& WXUNUSED(event))
2167 {
2168 #ifdef __WXMSW__
2169 wxBeginBusyCursor();
2170 manager->GetHelpController()->LoadFile();
2171 manager->GetHelpController()->DisplayContents();
2172 wxEndBusyCursor();
2173 #endif
2174 }
2175
2176 void wxResourceEditorFrame::OnDeleteSelection(wxCommandEvent& WXUNUSED(event))
2177 {
2178 manager->DeleteSelection();
2179 }
2180
2181 void wxResourceEditorFrame::OnRecreateSelection(wxCommandEvent& WXUNUSED(event))
2182 {
2183 manager->RecreateSelection();
2184 }
2185
2186 void wxResourceEditorFrame::OnCloseWindow(wxCloseEvent& event)
2187 {
2188 wxPropertyInfo::CloseWindow();
2189 if (manager->Modified())
2190 {
2191 if (!manager->Clear(TRUE, FALSE))
2192 {
2193 event.Veto();
2194 return;
2195 }
2196 }
2197
2198 if (!IsIconized())
2199 {
2200 int w, h;
2201 GetSize(&w, &h);
2202 manager->m_resourceEditorWindowSize.width = w;
2203 manager->m_resourceEditorWindowSize.height = h;
2204
2205 int x, y;
2206 GetPosition(&x, &y);
2207
2208 manager->m_resourceEditorWindowSize.x = x;
2209 manager->m_resourceEditorWindowSize.y = y;
2210 }
2211 manager->SetEditorFrame(NULL);
2212 manager->SetEditorToolBar(NULL);
2213
2214 this->Destroy();
2215 }
2216
2217 /*
2218 * Resource editor window that contains the dialog/panel being edited
2219 */
2220
2221 BEGIN_EVENT_TABLE(wxResourceEditorScrolledWindow, wxScrolledWindow)
2222 EVT_PAINT(wxResourceEditorScrolledWindow::OnPaint)
2223 END_EVENT_TABLE()
2224
2225 wxResourceEditorScrolledWindow::wxResourceEditorScrolledWindow(wxWindow *parent, const wxPoint& pos, const wxSize& size,
2226 long style):
2227 wxScrolledWindow(parent, -1, pos, size, style)
2228 {
2229 m_marginX = 10;
2230 m_marginY = 40;
2231 m_childWindow = NULL;
2232
2233 SetBackgroundColour(* wxWHITE);
2234 }
2235
2236 wxResourceEditorScrolledWindow::~wxResourceEditorScrolledWindow()
2237 {
2238 }
2239
2240 void wxResourceEditorScrolledWindow::OnPaint(wxPaintEvent& WXUNUSED(event))
2241 {
2242 wxPaintDC dc(this);
2243
2244 DrawTitle(dc);
2245 }
2246
2247 void wxResourceEditorScrolledWindow::DrawTitle(wxDC& dc)
2248 {
2249 if (m_childWindow)
2250 {
2251 wxItemResource* res = wxResourceManager::GetCurrentResourceManager()->FindResourceForWindow(m_childWindow);
2252 if (res)
2253 {
2254 wxString str(res->GetTitle());
2255 int x, y;
2256 ViewStart(& x, & y);
2257
2258 wxFont font(10, wxSWISS, wxNORMAL, wxBOLD);
2259 dc.SetFont(font);
2260 dc.SetBackgroundMode(wxTRANSPARENT);
2261 dc.SetTextForeground(wxColour(0, 0, 0));
2262
2263 long w, h;
2264 dc.GetTextExtent(str, & w, & h);
2265
2266 dc.DrawText(str, m_marginX + (- x * 10), m_marginY + (- y * 10) - h - 5);
2267 }
2268 }
2269 }
2270
2271 // Popup menu callback
2272 void ObjectMenuProc(wxMenu *menu, wxCommandEvent& event)
2273 {
2274 wxWindow *data = (wxWindow *)menu->GetClientData();
2275 if (!data)
2276 return;
2277
2278 switch (event.GetInt())
2279 {
2280 case OBJECT_MENU_EDIT:
2281 {
2282 wxResourceManager::GetCurrentResourceManager()->EditWindow(data);
2283 break;
2284 }
2285 case OBJECT_MENU_DELETE:
2286 {
2287 wxResourceManager::GetCurrentResourceManager()->DeselectItemIfNecessary(data);
2288
2289 wxResourceManager::GetCurrentResourceManager()->SaveInfoAndDeleteHandler(data);
2290 wxResourceManager::GetCurrentResourceManager()->DeleteResource(data);
2291 wxResourceManager::GetCurrentResourceManager()->DeleteWindow(data);
2292 break;
2293 }
2294 default:
2295 break;
2296 }
2297 }
2298
2299 /*
2300 * Main toolbar
2301 *
2302 */
2303
2304 BEGIN_EVENT_TABLE(EditorToolBar, wxToolBar)
2305 // EVT_PAINT(EditorToolBar::OnPaint)
2306 END_EVENT_TABLE()
2307
2308 EditorToolBar::EditorToolBar(wxFrame *frame, const wxPoint& pos, const wxSize& size,
2309 long style):
2310 wxToolBar(frame, -1, pos, size, style)
2311 {
2312 }
2313
2314 bool EditorToolBar::OnLeftClick(int toolIndex, bool WXUNUSED(toggled))
2315 {
2316 wxResourceManager *manager = wxResourceManager::GetCurrentResourceManager();
2317
2318 switch (toolIndex)
2319 {
2320 case TOOLBAR_LOAD_FILE:
2321 {
2322 manager->New(TRUE);
2323 break;
2324 }
2325 case TOOLBAR_NEW:
2326 {
2327 manager->CreateNewPanel();
2328 break;
2329 }
2330 case TOOLBAR_SAVE_FILE:
2331 {
2332 manager->Save();
2333 break;
2334 }
2335 case TOOLBAR_HELP:
2336 {
2337 #ifdef __WXMSW__
2338 wxBeginBusyCursor();
2339 manager->GetHelpController()->LoadFile();
2340 manager->GetHelpController()->DisplayContents();
2341 wxEndBusyCursor();
2342 #endif
2343 break;
2344 }
2345 case TOOLBAR_FORMAT_HORIZ:
2346 {
2347 manager->AlignItems(TOOLBAR_FORMAT_HORIZ);
2348 break;
2349 }
2350 case TOOLBAR_FORMAT_HORIZ_LEFT_ALIGN:
2351 {
2352 manager->AlignItems(TOOLBAR_FORMAT_HORIZ_LEFT_ALIGN);
2353 break;
2354 }
2355 case TOOLBAR_FORMAT_HORIZ_RIGHT_ALIGN:
2356 {
2357 manager->AlignItems(TOOLBAR_FORMAT_HORIZ_RIGHT_ALIGN);
2358 break;
2359 }
2360 case TOOLBAR_FORMAT_VERT:
2361 {
2362 manager->AlignItems(TOOLBAR_FORMAT_VERT);
2363 break;
2364 }
2365 case TOOLBAR_FORMAT_VERT_TOP_ALIGN:
2366 {
2367 manager->AlignItems(TOOLBAR_FORMAT_VERT_TOP_ALIGN);
2368 break;
2369 }
2370 case TOOLBAR_FORMAT_VERT_BOT_ALIGN:
2371 {
2372 manager->AlignItems(TOOLBAR_FORMAT_VERT_BOT_ALIGN);
2373 break;
2374 }
2375 case TOOLBAR_COPY_SIZE:
2376 {
2377 manager->CopySize();
2378 break;
2379 }
2380 case TOOLBAR_TO_BACK:
2381 {
2382 manager->ToBackOrFront(TRUE);
2383 break;
2384 }
2385 case TOOLBAR_TO_FRONT:
2386 {
2387 manager->ToBackOrFront(FALSE);
2388 break;
2389 }
2390 default:
2391 break;
2392 }
2393 return TRUE;
2394 }
2395
2396 void EditorToolBar::OnMouseEnter(int toolIndex)
2397 {
2398 wxFrame *frame = (wxFrame *)GetParent();
2399
2400 if (!frame) return;
2401
2402 if (toolIndex > -1)
2403 {
2404 switch (toolIndex)
2405 {
2406 case TOOLBAR_LOAD_FILE:
2407 frame->SetStatusText("Load project file");
2408 break;
2409 case TOOLBAR_SAVE_FILE:
2410 frame->SetStatusText("Save project file");
2411 break;
2412 case TOOLBAR_NEW:
2413 frame->SetStatusText("Create a new resource");
2414 break;
2415 case TOOLBAR_FORMAT_HORIZ:
2416 frame->SetStatusText("Align items horizontally");
2417 break;
2418 case TOOLBAR_FORMAT_VERT:
2419 frame->SetStatusText("Align items vertically");
2420 break;
2421 case TOOLBAR_FORMAT_HORIZ_LEFT_ALIGN:
2422 frame->SetStatusText("Left-align items");
2423 break;
2424 case TOOLBAR_FORMAT_HORIZ_RIGHT_ALIGN:
2425 frame->SetStatusText("Right-align items");
2426 break;
2427 case TOOLBAR_FORMAT_VERT_TOP_ALIGN:
2428 frame->SetStatusText("Top-align items");
2429 break;
2430 case TOOLBAR_FORMAT_VERT_BOT_ALIGN:
2431 frame->SetStatusText("Bottom-align items");
2432 break;
2433 case TOOLBAR_COPY_SIZE:
2434 frame->SetStatusText("Copy size from first selection");
2435 break;
2436 case TOOLBAR_TO_FRONT:
2437 frame->SetStatusText("Put image to front");
2438 break;
2439 case TOOLBAR_TO_BACK:
2440 frame->SetStatusText("Put image to back");
2441 break;
2442 case TOOLBAR_HELP:
2443 frame->SetStatusText("Display help contents");
2444 break;
2445 default:
2446 break;
2447 }
2448 }
2449 else frame->SetStatusText("");
2450 }
2451