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