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