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