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