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