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