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