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