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