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