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