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