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