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