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