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