]> git.saurik.com Git - wxWidgets.git/blame - utils/dialoged/src/reseditr.cpp
Added sys/types.h needed for Solaris.
[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__
457814b5
JS
690 wxIcon *icon = new wxIcon("DIALOGEDICON");
691 frame->SetIcon(icon);
692#endif
693 return frame;
694}
695
b127f301 696wxMenuBar *wxResourceManager::OnCreateEditorMenuBar(wxFrame *WXUNUSED(parent))
457814b5
JS
697{
698 wxMenuBar *menuBar = new wxMenuBar;
699
700 wxMenu *fileMenu = new wxMenu;
701 fileMenu->Append(RESED_NEW_DIALOG, "New &dialog", "Create a new dialog");
457814b5
JS
702 fileMenu->AppendSeparator();
703 fileMenu->Append(wxID_NEW, "&New project", "Clear the current project");
704 fileMenu->Append(wxID_OPEN, "&Open...", "Load a resource file");
705 fileMenu->Append(wxID_SAVE, "&Save", "Save a resource file");
706 fileMenu->Append(wxID_SAVEAS, "Save &As...", "Save a resource file as...");
707 fileMenu->Append(RESED_CLEAR, "&Clear", "Clear current resources");
708 fileMenu->AppendSeparator();
709 fileMenu->Append(wxID_EXIT, "E&xit", "Exit resource editor");
710
711 wxMenu *editMenu = new wxMenu;
ae8351fc 712 editMenu->Append(RESED_TEST, "&Test Dialog", "Test dialog");
457814b5
JS
713 editMenu->Append(RESED_RECREATE, "&Recreate", "Recreate the selected resource(s)");
714 editMenu->Append(RESED_DELETE, "&Delete", "Delete the selected resource(s)");
457814b5
JS
715
716 wxMenu *helpMenu = new wxMenu;
717 helpMenu->Append(RESED_CONTENTS, "&Help topics", "Invokes the on-line help");
718 helpMenu->AppendSeparator();
719 helpMenu->Append(wxID_ABOUT, "&About", "About wxWindows Dialog Editor");
720
721 menuBar->Append(fileMenu, "&File");
722 menuBar->Append(editMenu, "&Edit");
723 menuBar->Append(helpMenu, "&Help");
724
725 return menuBar;
726}
727
ae8351fc 728wxResourceEditorScrolledWindow *wxResourceManager::OnCreateEditorPanel(wxFrame *parent)
457814b5 729{
ae8351fc
JS
730 wxResourceEditorScrolledWindow *panel = new wxResourceEditorScrolledWindow(parent, wxDefaultPosition, wxDefaultSize,
731// wxSUNKEN_BORDER|wxCLIP_CHILDREN);
732 wxSUNKEN_BORDER);
457814b5 733
ae8351fc 734 panel->SetScrollbars(10, 10, 100, 100);
457814b5
JS
735
736 return panel;
737}
738
163f0dbe 739wxToolBar *wxResourceManager::OnCreateToolBar(wxFrame *parent)
457814b5
JS
740{
741 // Load palette bitmaps
2049ba38 742#ifdef __WXMSW__
ae8351fc
JS
743 wxBitmap ToolbarLoadBitmap("LOADTOOL");
744 wxBitmap ToolbarSaveBitmap("SAVETOOL");
745 wxBitmap ToolbarNewBitmap("NEWTOOL");
746 wxBitmap ToolbarVertBitmap("VERTTOOL");
747 wxBitmap ToolbarAlignTBitmap("ALIGNTTOOL");
748 wxBitmap ToolbarAlignBBitmap("ALIGNBTOOL");
749 wxBitmap ToolbarHorizBitmap("HORIZTOOL");
750 wxBitmap ToolbarAlignLBitmap("ALIGNLTOOL");
751 wxBitmap ToolbarAlignRBitmap("ALIGNRTOOL");
752 wxBitmap ToolbarCopySizeBitmap("COPYSIZETOOL");
753 wxBitmap ToolbarToBackBitmap("TOBACKTOOL");
754 wxBitmap ToolbarToFrontBitmap("TOFRONTTOOL");
755 wxBitmap ToolbarHelpBitmap("HELPTOOL");
457814b5 756#endif
b127f301
RR
757#ifdef __WXGTK__
758 wxBitmap ToolbarLoadBitmap( load_xpm );
759 wxBitmap ToolbarSaveBitmap( save_xpm);
760 wxBitmap ToolbarNewBitmap( new_xpm );
d5c462fd
RR
761 wxBitmap ToolbarVertBitmap( vert_xpm );
762 wxBitmap ToolbarAlignTBitmap( alignt_xpm );
763 wxBitmap ToolbarAlignBBitmap( alignb_xpm );
764 wxBitmap ToolbarHorizBitmap( horiz_xpm );
765 wxBitmap ToolbarAlignLBitmap( alignl_xpm );
766 wxBitmap ToolbarAlignRBitmap( alignr_xpm );
767 wxBitmap ToolbarCopySizeBitmap( copysize_xpm );
768 wxBitmap ToolbarToBackBitmap( toback_xpm );
769 wxBitmap ToolbarToFrontBitmap( tofront_xpm );
770 wxBitmap ToolbarHelpBitmap( help_xpm );
457814b5
JS
771#endif
772
773 // Create the toolbar
f449ef69 774 EditorToolBar *toolbar = new EditorToolBar(parent, wxPoint(0, 0), wxSize(-1, -1), wxNO_BORDER|wxTB_HORIZONTAL);
457814b5 775 toolbar->SetMargins(2, 2);
457814b5 776
2049ba38 777#ifdef __WXMSW__
457814b5
JS
778 int width = 24;
779 int dx = 2;
780 int gap = 6;
781#else
b127f301 782 int width = 24; // ToolbarLoadBitmap->GetWidth(); ???
457814b5
JS
783 int dx = 2;
784 int gap = 6;
785#endif
786 int currentX = gap;
787 toolbar->AddSeparator();
788 toolbar->AddTool(TOOLBAR_NEW, ToolbarNewBitmap, (wxBitmap *)NULL,
789 FALSE, (float)currentX, -1, NULL, "New dialog");
790 currentX += width + dx;
791 toolbar->AddTool(TOOLBAR_LOAD_FILE, ToolbarLoadBitmap, (wxBitmap *)NULL,
792 FALSE, (float)currentX, -1, NULL, "Load");
793 currentX += width + dx;
794 toolbar->AddTool(TOOLBAR_SAVE_FILE, ToolbarSaveBitmap, (wxBitmap *)NULL,
795 FALSE, (float)currentX, -1, NULL, "Save");
796 currentX += width + dx + gap;
797 toolbar->AddSeparator();
798 toolbar->AddTool(TOOLBAR_FORMAT_HORIZ, ToolbarVertBitmap, (wxBitmap *)NULL,
799 FALSE, (float)currentX, -1, NULL, "Horizontal align");
800 currentX += width + dx;
801 toolbar->AddTool(TOOLBAR_FORMAT_VERT_TOP_ALIGN, ToolbarAlignTBitmap, (wxBitmap *)NULL,
802 FALSE, (float)currentX, -1, NULL, "Top align");
803 currentX += width + dx;
804 toolbar->AddTool(TOOLBAR_FORMAT_VERT_BOT_ALIGN, ToolbarAlignBBitmap, (wxBitmap *)NULL,
805 FALSE, (float)currentX, -1, NULL, "Bottom align");
806 currentX += width + dx;
807 toolbar->AddTool(TOOLBAR_FORMAT_VERT, ToolbarHorizBitmap, (wxBitmap *)NULL,
808 FALSE, (float)currentX, -1, NULL, "Vertical align");
809 currentX += width + dx;
810 toolbar->AddTool(TOOLBAR_FORMAT_HORIZ_LEFT_ALIGN, ToolbarAlignLBitmap, (wxBitmap *)NULL,
811 FALSE, (float)currentX, -1, NULL, "Left align");
812 currentX += width + dx;
813 toolbar->AddTool(TOOLBAR_FORMAT_HORIZ_RIGHT_ALIGN, ToolbarAlignRBitmap, (wxBitmap *)NULL,
814 FALSE, (float)currentX, -1, NULL, "Right align");
815 currentX += width + dx;
816 toolbar->AddTool(TOOLBAR_COPY_SIZE, ToolbarCopySizeBitmap, (wxBitmap *)NULL,
817 FALSE, (float)currentX, -1, NULL, "Copy size");
818 currentX += width + dx + gap;
819 toolbar->AddSeparator();
820 toolbar->AddTool(TOOLBAR_TO_FRONT, ToolbarToFrontBitmap, (wxBitmap *)NULL,
821 FALSE, (float)currentX, -1, NULL, "To front");
822 currentX += width + dx;
823 toolbar->AddTool(TOOLBAR_TO_BACK, ToolbarToBackBitmap, (wxBitmap *)NULL,
824 FALSE, (float)currentX, -1, NULL, "To back");
825 currentX += width + dx + gap;
457814b5 826
457814b5
JS
827 toolbar->AddSeparator();
828 toolbar->AddTool(TOOLBAR_HELP, ToolbarHelpBitmap, (wxBitmap *)NULL,
829 FALSE, (float)currentX, -1, NULL, "Help");
830 currentX += width + dx;
831
f449ef69 832 toolbar->Realize();
457814b5
JS
833
834 return toolbar;
457814b5
JS
835}
836
ae8351fc 837void wxResourceManager::UpdateResourceList()
457814b5 838{
ae8351fc
JS
839 if (!m_editorResourceTree)
840 return;
841
842 m_editorResourceTree->SetInvalid(TRUE);
843 m_editorResourceTree->DeleteAllItems();
844
845 long id = m_editorResourceTree->InsertItem(0, "Dialogs"
2049ba38 846#ifdef __WXMSW__
ae8351fc
JS
847 , 1, 2
848#endif
849 );
850
851 m_resourceTable.BeginFind();
457814b5 852 wxNode *node;
ae8351fc 853 while (node = m_resourceTable.Next())
457814b5
JS
854 {
855 wxItemResource *res = (wxItemResource *)node->Data();
856 wxString resType(res->GetType());
457814b5
JS
857 if (resType == "wxDialog" || resType == "wxDialogBox" || resType == "wxPanel" || resType == "wxBitmap")
858 {
ae8351fc 859 AddItemsRecursively(id, res);
457814b5
JS
860 }
861 }
ae8351fc
JS
862 m_editorResourceTree->ExpandItem(id, wxTREE_EXPAND_EXPAND);
863 m_editorResourceTree->SetInvalid(FALSE);
457814b5
JS
864}
865
ae8351fc 866void wxResourceManager::AddItemsRecursively(long parent, wxItemResource *resource)
457814b5 867{
457814b5 868 wxString theString("");
ae8351fc
JS
869 theString = resource->GetName();
870
871 int imageId = 0;
872 wxString resType(resource->GetType());
873 if (resType == "wxDialog" || resType == "wxDialogBox" || resType == "wxPanel")
874 imageId = 0;
875 else
876 imageId = 3;
457814b5 877
ae8351fc 878 long id = m_editorResourceTree->InsertItem(parent, theString
2049ba38 879#ifdef __WXMSW__
ae8351fc
JS
880 , imageId
881#endif
882 );
883
884 m_editorResourceTree->SetItemData(id, (long) resource);
457814b5
JS
885
886 if (strcmp(resource->GetType(), "wxBitmap") != 0)
887 {
888 wxNode *node = resource->GetChildren().First();
889 while (node)
890 {
891 wxItemResource *res = (wxItemResource *)node->Data();
ae8351fc 892 AddItemsRecursively(id, res);
457814b5
JS
893 node = node->Next();
894 }
895 }
ae8351fc 896 m_editorResourceTree->ExpandItem(id, wxTREE_EXPAND_EXPAND);
457814b5
JS
897}
898
ae8351fc 899bool wxResourceManager::EditSelectedResource()
457814b5 900{
ae8351fc 901 int sel = m_editorResourceTree->GetSelection();
03f68f12 902 if (sel != 0)
457814b5 903 {
ae8351fc 904 wxItemResource *res = (wxItemResource *)m_editorResourceTree->GetItemData(sel);
457814b5
JS
905 return Edit(res);
906 }
907 return FALSE;
908}
909
910bool wxResourceManager::Edit(wxItemResource *res)
911{
ae8351fc
JS
912 ClearCurrentDialog();
913
457814b5 914 wxString resType(res->GetType());
ae8351fc 915 wxPanel *panel = (wxPanel *)FindWindowForResource(res);
457814b5 916
ae8351fc
JS
917 if (panel)
918 {
919 wxMessageBox("Should not find panel in wxResourceManager::Edit");
920 return FALSE;
921 }
922 else
923 {
924 long style = res->GetStyle();
925 res->SetStyle(style|wxRAISED_BORDER);
457814b5
JS
926 panel = new wxPanel;
927 wxResourceEditorDialogHandler *handler = new wxResourceEditorDialogHandler(panel, res, panel->GetEventHandler(),
928 this);
457814b5 929
ae8351fc 930 panel->LoadFromResource(m_editorPanel, res->GetName(), &m_resourceTable);
457814b5 931
ae8351fc 932 panel->PushEventHandler(handler);
457814b5 933
ae8351fc
JS
934 res->SetStyle(style);
935 handler->AddChildHandlers(); // Add event handlers for all controls
936 AssociateResource(res, panel);
457814b5 937
ae8351fc
JS
938 m_editorPanel->m_childWindow = panel;
939 panel->Move(m_editorPanel->GetMarginX(), m_editorPanel->GetMarginY());
940 panel->Show(TRUE);
941 panel->Refresh();
457814b5 942
ae8351fc
JS
943 wxClientDC dc(m_editorPanel);
944 m_editorPanel->DrawTitle(dc);
945 }
946 return FALSE;
457814b5
JS
947}
948
ae8351fc 949bool wxResourceManager::CreateNewPanel()
457814b5 950{
ae8351fc
JS
951 ClearCurrentDialog();
952
457814b5 953 char buf[256];
bbcdf8bc 954 MakeUniqueName("dialog", buf);
457814b5
JS
955
956 wxItemResource *resource = new wxItemResource;
bbcdf8bc 957 resource->SetType("wxDialog");
457814b5
JS
958 resource->SetName(buf);
959 resource->SetTitle(buf);
5de76427
JS
960
961 wxString newIdName;
962 int id = GenerateWindowId("ID_DIALOG", newIdName);
963 resource->SetId(id);
964
965 // This is now guaranteed to be unique, so just add to symbol table
966 m_symbolTable.AddSymbol(newIdName, id);
967
ae8351fc 968 m_resourceTable.AddResource(resource);
457814b5 969
ae8351fc
JS
970 wxPanel *panel = new wxPanel(m_editorPanel, -1,
971 wxPoint(m_editorPanel->GetMarginX(), m_editorPanel->GetMarginY()),
bbcdf8bc 972 wxSize(400, 300), wxRAISED_BORDER|wxDEFAULT_DIALOG_STYLE, buf);
ae8351fc 973 m_editorPanel->m_childWindow = panel;
457814b5 974
bbcdf8bc 975 resource->SetStyle(panel->GetWindowStyleFlag());
ae8351fc 976 resource->SetSize(10, 10, 400, 300);
457814b5
JS
977
978 // For editing in situ we will need to use the hash table to ensure
979 // we don't dereference invalid pointers.
980// resourceWindowTable.Put((long)resource, panel);
981
982 wxResourceEditorDialogHandler *handler = new wxResourceEditorDialogHandler(panel, resource, panel->GetEventHandler(),
983 this);
984 panel->PushEventHandler(handler);
985
457814b5 986 AssociateResource(resource, panel);
457814b5
JS
987 UpdateResourceList();
988
989 Modify(TRUE);
ae8351fc
JS
990 m_editorPanel->m_childWindow->Refresh();
991
992// panel->Refresh();
993
994 wxClientDC dc(m_editorPanel);
995 m_editorPanel->DrawTitle(dc);
996
457814b5
JS
997 return TRUE;
998}
999
1000bool wxResourceManager::CreatePanelItem(wxItemResource *panelResource, wxPanel *panel, char *iType, int x, int y, bool isBitmap)
1001{
1002 char buf[256];
1003 if (!panel->IsKindOf(CLASSINFO(wxPanel)) && !panel->IsKindOf(CLASSINFO(wxDialog)))
1004 return FALSE;
1005
1006 Modify(TRUE);
1007
1008 wxItemResource *res = new wxItemResource;
1009 wxControl *newItem = NULL;
1010 res->SetSize(x, y, -1, -1);
1011 res->SetType(iType);
5de76427
JS
1012
1013 wxString prefix;
457814b5
JS
1014
1015 wxString itemType(iType);
1016
1017 if (itemType == "wxButton")
1018 {
5de76427 1019 prefix = "ID_BUTTON";
457814b5
JS
1020 MakeUniqueName("button", buf);
1021 res->SetName(buf);
1022 if (isBitmap)
ae8351fc 1023 newItem = new wxBitmapButton(panel, -1, m_bitmapImage, wxPoint(x, y), wxSize(-1, -1), 0, wxDefaultValidator, buf);
457814b5
JS
1024 else
1025 newItem = new wxButton(panel, -1, "Button", wxPoint(x, y), wxSize(-1, -1), 0, wxDefaultValidator, buf);
1026 }
1027 if (itemType == "wxBitmapButton")
1028 {
5de76427 1029 prefix = "ID_BITMAPBUTTON";
457814b5
JS
1030 MakeUniqueName("button", buf);
1031 res->SetName(buf);
ae8351fc 1032 newItem = new wxBitmapButton(panel, -1, m_bitmapImage, wxPoint(x, y), wxSize(-1, -1), 0, wxDefaultValidator, buf);
457814b5
JS
1033 }
1034 else if (itemType == "wxMessage" || itemType == "wxStaticText")
1035 {
5de76427 1036 prefix = "ID_STATIC";
9c331ded 1037 MakeUniqueName("statictext", buf);
457814b5
JS
1038 res->SetName(buf);
1039 if (isBitmap)
ae8351fc 1040 newItem = new wxStaticBitmap(panel, -1, m_bitmapImage, wxPoint(x, y), wxSize(0, 0), 0, buf);
457814b5 1041 else
9c331ded 1042 newItem = new wxStaticText(panel, -1, "Static", wxPoint(x, y), wxSize(-1, -1), 0, buf);
457814b5
JS
1043 }
1044 else if (itemType == "wxStaticBitmap")
1045 {
5de76427 1046 prefix = "ID_STATICBITMAP";
9c331ded 1047 MakeUniqueName("static", buf);
457814b5 1048 res->SetName(buf);
ae8351fc 1049 newItem = new wxStaticBitmap(panel, -1, m_bitmapImage, wxPoint(x, y), wxSize(-1, -1), 0, buf);
457814b5
JS
1050 }
1051 else if (itemType == "wxCheckBox")
1052 {
5de76427 1053 prefix = "ID_CHECKBOX";
457814b5
JS
1054 MakeUniqueName("checkbox", buf);
1055 res->SetName(buf);
1056 newItem = new wxCheckBox(panel, -1, "Checkbox", wxPoint(x, y), wxSize(-1, -1), 0, wxDefaultValidator, buf);
1057 }
1058 else if (itemType == "wxListBox")
1059 {
bbcdf8bc 1060 prefix = "ID_LISTBOX";
457814b5
JS
1061 MakeUniqueName("listbox", buf);
1062 res->SetName(buf);
1063 newItem = new wxListBox(panel, -1, wxPoint(x, y), wxSize(-1, -1), 0, NULL, 0, wxDefaultValidator, buf);
1064 }
1065 else if (itemType == "wxRadioBox")
1066 {
5de76427 1067 prefix = "ID_RADIOBOX";
457814b5
JS
1068 MakeUniqueName("radiobox", buf);
1069 res->SetName(buf);
1070 wxString names[] = { "One", "Two" };
1071 newItem = new wxRadioBox(panel, -1, "Radiobox", wxPoint(x, y), wxSize(-1, -1), 2, names, 2,
1072 wxHORIZONTAL, wxDefaultValidator, buf);
1073 res->SetStringValues(new wxStringList("One", "Two", NULL));
1074 }
03f68f12
JS
1075 else if (itemType == "wxRadioButton")
1076 {
5de76427 1077 prefix = "ID_RADIOBUTTON";
03f68f12
JS
1078 MakeUniqueName("radiobutton", buf);
1079 res->SetName(buf);
1080 wxString names[] = { "One", "Two" };
1081 newItem = new wxRadioButton(panel, -1, "Radiobutton", wxPoint(x, y), wxSize(-1, -1),
1082 0, wxDefaultValidator, buf);
1083 }
457814b5
JS
1084 else if (itemType == "wxChoice")
1085 {
5de76427 1086 prefix = "ID_CHOICE";
457814b5
JS
1087 MakeUniqueName("choice", buf);
1088 res->SetName(buf);
1089 newItem = new wxChoice(panel, -1, wxPoint(x, y), wxSize(-1, -1), 0, NULL, 0, wxDefaultValidator, buf);
1090 }
9c331ded
JS
1091 else if (itemType == "wxComboBox")
1092 {
1093 prefix = "ID_COMBOBOX";
1094 MakeUniqueName("combobox", buf);
1095 res->SetName(buf);
1096 newItem = new wxComboBox(panel, -1, "", wxPoint(x, y), wxSize(-1, -1), 0, NULL, wxCB_DROPDOWN, wxDefaultValidator, buf);
1097 }
457814b5
JS
1098 else if (itemType == "wxGroupBox" || itemType == "wxStaticBox")
1099 {
5de76427 1100 prefix = "ID_STATICBOX";
9c331ded 1101 MakeUniqueName("staticbox", buf);
457814b5 1102 res->SetName(buf);
9c331ded 1103 newItem = new wxStaticBox(panel, -1, "Static", wxPoint(x, y), wxSize(200, 200), 0, buf);
457814b5
JS
1104 }
1105 else if (itemType == "wxGauge")
1106 {
5de76427 1107 prefix = "ID_GAUGE";
457814b5
JS
1108 MakeUniqueName("gauge", buf);
1109 res->SetName(buf);
1110 newItem = new wxGauge(panel, -1, 10, wxPoint(x, y), wxSize(80, 30), wxHORIZONTAL, wxDefaultValidator, buf);
1111 }
1112 else if (itemType == "wxSlider")
1113 {
5de76427 1114 prefix = "ID_SLIDER";
457814b5
JS
1115 MakeUniqueName("slider", buf);
1116 res->SetName(buf);
1117 newItem = new wxSlider(panel, -1, 1, 1, 10, wxPoint(x, y), wxSize(120, -1), wxHORIZONTAL, wxDefaultValidator, buf);
1118 }
ae8351fc 1119 else if (itemType == "wxText" || itemType == "wxTextCtrl (single-line)")
457814b5 1120 {
5de76427 1121 prefix = "ID_TEXTCTRL";
ae8351fc 1122 MakeUniqueName("textctrl", buf);
457814b5 1123 res->SetName(buf);
ae8351fc 1124 res->SetType("wxTextCtrl");
457814b5
JS
1125 newItem = new wxTextCtrl(panel, -1, "", wxPoint(x, y), wxSize(120, -1), 0, wxDefaultValidator, buf);
1126 }
ae8351fc 1127 else if (itemType == "wxMultiText" || itemType == "wxTextCtrl (multi-line)")
457814b5 1128 {
5de76427 1129 prefix = "ID_TEXTCTRL";
ae8351fc 1130 MakeUniqueName("textctrl", buf);
457814b5 1131 res->SetName(buf);
ae8351fc
JS
1132 res->SetType("wxTextCtrl");
1133 newItem = new wxTextCtrl(panel, -1, "", wxPoint(x, y), wxSize(120, 100), wxTE_MULTILINE, wxDefaultValidator, buf);
457814b5 1134 }
457814b5
JS
1135 else if (itemType == "wxScrollBar")
1136 {
5de76427 1137 prefix = "ID_SCROLLBAR";
457814b5
JS
1138 MakeUniqueName("scrollbar", buf);
1139 res->SetName(buf);
1140 newItem = new wxScrollBar(panel, -1, wxPoint(x, y), wxSize(140, -1), wxHORIZONTAL, wxDefaultValidator, buf);
1141 }
1142 if (!newItem)
1143 return FALSE;
1144
5de76427
JS
1145 wxString newIdName;
1146 int id = GenerateWindowId(prefix, newIdName);
1147 res->SetId(id);
1148
1149 // This is now guaranteed to be unique, so just add to symbol table
1150 m_symbolTable.AddSymbol(newIdName, id);
1151
457814b5
JS
1152 newItem->PushEventHandler(new wxResourceEditorControlHandler(newItem, newItem));
1153
1154 res->SetStyle(newItem->GetWindowStyleFlag());
1155 AssociateResource(res, newItem);
1156 panelResource->GetChildren().Append(res);
1157
1158 UpdateResourceList();
1159
1160 return TRUE;
1161}
1162
ae8351fc
JS
1163void wxResourceManager::ClearCurrentDialog()
1164{
1165 if (m_editorPanel->m_childWindow)
1166 {
1167 SaveInfoAndDeleteHandler(m_editorPanel->m_childWindow);
1168 DisassociateResource(m_editorPanel->m_childWindow);
1169 DeleteWindow(m_editorPanel->m_childWindow);
1170 m_editorPanel->m_childWindow = NULL;
1171 m_editorPanel->Clear();
1172 }
1173}
1174
03f68f12
JS
1175bool wxResourceManager::TestCurrentDialog(wxWindow* parent)
1176{
1177 if (m_editorPanel->m_childWindow)
1178 {
1179 wxItemResource* item = FindResourceForWindow(m_editorPanel->m_childWindow);
1180 if (!item)
1181 return FALSE;
1182
1183 // Make sure the resources are up-to-date w.r.t. the window
1184 InstantiateResourceFromWindow(item, m_editorPanel->m_childWindow, TRUE);
1185
1186 wxDialog* dialog = new wxDialog;
1187 long oldStyle = item->GetStyle();
1188 bool success = FALSE;
bbcdf8bc 1189// item->SetStyle(wxDEFAULT_DIALOG_STYLE);
03f68f12
JS
1190 if (dialog->LoadFromResource(parent, item->GetName(), & m_resourceTable))
1191 {
1192 dialog->Centre();
1193 dialog->ShowModal();
1194 success = TRUE;
1195 }
bbcdf8bc 1196// item->SetStyle(oldStyle);
03f68f12
JS
1197 return success;
1198 }
1199 return FALSE;
1200}
1201
457814b5
JS
1202// Find the first dialog or panel for which
1203// there is a selected panel item.
ae8351fc 1204wxWindow *wxResourceManager::FindParentOfSelection()
457814b5 1205{
ae8351fc 1206 m_resourceTable.BeginFind();
457814b5 1207 wxNode *node;
ae8351fc 1208 while (node = m_resourceTable.Next())
457814b5
JS
1209 {
1210 wxItemResource *res = (wxItemResource *)node->Data();
1211 wxWindow *win = FindWindowForResource(res);
1212 if (win)
1213 {
1214 wxNode *node1 = win->GetChildren()->First();
1215 while (node1)
1216 {
1217 wxControl *item = (wxControl *)node1->Data();
1218 wxResourceEditorControlHandler *childHandler = (wxResourceEditorControlHandler *)item->GetEventHandler();
1219 if (item->IsKindOf(CLASSINFO(wxControl)) && childHandler->IsSelected())
1220 return win;
1221 node1 = node1->Next();
1222 }
1223 }
1224 }
1225 return NULL;
1226}
1227
1228// Format the panel items according to 'flag'
1229void wxResourceManager::AlignItems(int flag)
1230{
1231 wxWindow *win = FindParentOfSelection();
1232 if (!win)
1233 return;
1234
1235 wxNode *node = GetSelections().First();
1236 if (!node)
1237 return;
1238
1239 wxControl *firstSelection = (wxControl *)node->Data();
1240 if (firstSelection->GetParent() != win)
1241 return;
1242
1243 int firstX, firstY;
1244 int firstW, firstH;
1245 firstSelection->GetPosition(&firstX, &firstY);
1246 firstSelection->GetSize(&firstW, &firstH);
1247 int centreX = (int)(firstX + (firstW / 2));
1248 int centreY = (int)(firstY + (firstH / 2));
1249
1250 while (node = node->Next())
1251 {
1252 wxControl *item = (wxControl *)node->Data();
1253 if (item->GetParent() == win)
1254 {
1255 int x, y, w, h;
1256 item->GetPosition(&x, &y);
1257 item->GetSize(&w, &h);
1258
1259 int newX, newY;
1260
1261 switch (flag)
1262 {
1263 case TOOLBAR_FORMAT_HORIZ:
1264 {
1265 newX = x;
1266 newY = (int)(centreY - (h/2.0));
1267 break;
1268 }
1269 case TOOLBAR_FORMAT_VERT:
1270 {
1271 newX = (int)(centreX - (w/2.0));
1272 newY = y;
1273 break;
1274 }
1275 case TOOLBAR_FORMAT_HORIZ_LEFT_ALIGN:
1276 {
1277 newX = firstX;
1278 newY = y;
1279 break;
1280 }
1281 case TOOLBAR_FORMAT_VERT_TOP_ALIGN:
1282 {
1283 newX = x;
1284 newY = firstY;
1285 break;
1286 }
1287 case TOOLBAR_FORMAT_HORIZ_RIGHT_ALIGN:
1288 {
1289 newX = firstX + firstW - w;
1290 newY = y;
1291 break;
1292 }
1293 case TOOLBAR_FORMAT_VERT_BOT_ALIGN:
1294 {
1295 newX = x;
1296 newY = firstY + firstH - h;
1297 break;
1298 }
1299 default:
1300 newX = x; newY = y;
1301 break;
1302 }
1303
1304 item->SetSize(newX, newY, w, h);
1305 }
1306 }
1307 win->Refresh();
1308}
1309
1310// Copy the first image's size to subsequent images
ae8351fc 1311void wxResourceManager::CopySize()
457814b5
JS
1312{
1313 wxWindow *win = FindParentOfSelection();
1314 if (!win)
1315 return;
1316
1317 wxNode *node = GetSelections().First();
1318 if (!node)
1319 return;
1320
1321 wxControl *firstSelection = (wxControl *)node->Data();
1322 if (firstSelection->GetParent() != win)
1323 return;
1324
1325 int firstX, firstY;
1326 int firstW, firstH;
1327 firstSelection->GetPosition(&firstX, &firstY);
1328 firstSelection->GetSize(&firstW, &firstH);
1329 int centreX = (int)(firstX + (firstW / 2));
1330 int centreY = (int)(firstY + (firstH / 2));
1331
1332 while (node = node->Next())
1333 {
1334 wxControl *item = (wxControl *)node->Data();
1335 if (item->GetParent() == win)
1336 item->SetSize(-1, -1, firstW, firstH);
1337 }
1338 win->Refresh();
1339}
1340
1341void wxResourceManager::ToBackOrFront(bool toBack)
1342{
1343 wxWindow *win = FindParentOfSelection();
1344 if (!win)
1345 return;
1346 wxItemResource *winResource = FindResourceForWindow(win);
1347
1348 wxNode *node = GetSelections().First();
1349 while (node)
1350 {
1351 wxControl *item = (wxControl *)node->Data();
1352 wxItemResource *itemResource = FindResourceForWindow(item);
1353 if (item->GetParent() == win)
1354 {
1355 win->GetChildren()->DeleteObject(item);
1356 if (winResource)
1357 winResource->GetChildren().DeleteObject(itemResource);
1358 if (toBack)
1359 {
1360 win->GetChildren()->Insert(item);
1361 if (winResource)
1362 winResource->GetChildren().Insert(itemResource);
1363 }
1364 else
1365 {
1366 win->GetChildren()->Append(item);
1367 if (winResource)
1368 winResource->GetChildren().Append(itemResource);
1369 }
1370 }
1371 node = node->Next();
1372 }
1373// win->Refresh();
1374}
1375
1376void wxResourceManager::AddSelection(wxWindow *win)
1377{
ae8351fc
JS
1378 if (!m_selections.Member(win))
1379 m_selections.Append(win);
457814b5
JS
1380}
1381
1382void wxResourceManager::RemoveSelection(wxWindow *win)
1383{
ae8351fc 1384 m_selections.DeleteObject(win);
457814b5
JS
1385}
1386
1387// Need to search through resource table removing this from
1388// any resource which has this as a parent.
1389bool wxResourceManager::RemoveResourceFromParent(wxItemResource *res)
1390{
ae8351fc 1391 m_resourceTable.BeginFind();
457814b5 1392 wxNode *node;
ae8351fc 1393 while (node = m_resourceTable.Next())
457814b5
JS
1394 {
1395 wxItemResource *thisRes = (wxItemResource *)node->Data();
1396 if (thisRes->GetChildren().Member(res))
1397 {
1398 thisRes->GetChildren().DeleteObject(res);
1399 return TRUE;
1400 }
1401 }
1402 return FALSE;
1403}
1404
1405bool wxResourceManager::DeleteResource(wxItemResource *res)
1406{
1407 if (!res)
1408 return FALSE;
1409
1410 RemoveResourceFromParent(res);
1411
1412 wxNode *node = res->GetChildren().First();
1413 while (node)
1414 {
1415 wxNode *next = node->Next();
1416 wxItemResource *child = (wxItemResource *)node->Data();
1417 DeleteResource(child);
1418 node = next;
1419 }
1420
1421 // If this is a button or message resource, delete the
1422 // associate bitmap resource if not being used.
1423 wxString resType(res->GetType());
1424
ae8351fc 1425 if ((resType == "wxMessage" || resType == "wxStaticBitmap" || resType == "wxButton" || resType == "wxBitmapButton") && res->GetValue4())
457814b5
JS
1426 {
1427 PossiblyDeleteBitmapResource(res->GetValue4());
1428 }
1429
5de76427
JS
1430 // Remove symbol from table if appropriate
1431 if (!IsSymbolUsed(res, res->GetId()))
1432 {
1433 m_symbolTable.RemoveSymbol(res->GetId());
1434 }
1435
ae8351fc 1436 m_resourceTable.Delete(res->GetName());
457814b5
JS
1437 delete res;
1438 Modify(TRUE);
1439 return TRUE;
1440}
1441
ae8351fc 1442bool wxResourceManager::DeleteResource(wxWindow *win)
457814b5
JS
1443{
1444 if (win->IsKindOf(CLASSINFO(wxControl)))
1445 {
1446 // Deselect and refresh window in case we leave selection
1447 // handles behind
1448 wxControl *item = (wxControl *)win;
1449 wxResourceEditorControlHandler *childHandler = (wxResourceEditorControlHandler *)item->GetEventHandler();
1450 if (childHandler->IsSelected())
1451 {
1452 RemoveSelection(item);
1453 childHandler->SelectItem(FALSE);
c058d771 1454#ifndef __WXGTK__
457814b5 1455 item->GetParent()->Refresh();
c058d771 1456#endif
457814b5
JS
1457 }
1458 }
1459
1460 wxItemResource *res = FindResourceForWindow(win);
1461
ae8351fc 1462 DisassociateResource(res);
457814b5
JS
1463 DeleteResource(res);
1464 UpdateResourceList();
1465
457814b5
JS
1466 return TRUE;
1467}
1468
1469// Will eventually have bitmap type information, for different
1470// kinds of bitmap.
1471char *wxResourceManager::AddBitmapResource(char *filename)
1472{
1473 wxItemResource *resource = FindBitmapResourceByFilename(filename);
1474 if (!resource)
1475 {
1476 char buf[256];
1477 MakeUniqueName("bitmap", buf);
1478 resource = new wxItemResource;
1479 resource->SetType("wxBitmap");
1480 resource->SetName(buf);
1481
1482 // A bitmap resource has one or more children, specifying
1483 // alternative bitmaps.
1484 wxItemResource *child = new wxItemResource;
1485 child->SetType("wxBitmap");
1486 child->SetName(filename);
1487 child->SetValue1(wxBITMAP_TYPE_BMP);
1488 child->SetValue2(RESOURCE_PLATFORM_ANY);
1489 child->SetValue3(0); // Depth
1490 child->SetSize(0,0,0,0);
1491 resource->GetChildren().Append(child);
1492
ae8351fc 1493 m_resourceTable.AddResource(resource);
457814b5
JS
1494
1495 UpdateResourceList();
1496 }
1497 if (resource)
1498 return resource->GetName();
1499 else
1500 return NULL;
1501}
1502
1503 // Delete the bitmap resource if it isn't being used by another resource.
1504void wxResourceManager::PossiblyDeleteBitmapResource(char *resourceName)
1505{
1506 if (!IsBitmapResourceUsed(resourceName))
1507 {
ae8351fc 1508 wxItemResource *res = m_resourceTable.FindResource(resourceName);
457814b5
JS
1509 DeleteResource(res);
1510 UpdateResourceList();
1511 }
1512}
1513
1514bool wxResourceManager::IsBitmapResourceUsed(char *resourceName)
1515{
ae8351fc 1516 m_resourceTable.BeginFind();
457814b5 1517 wxNode *node;
ae8351fc 1518 while (node = m_resourceTable.Next())
457814b5
JS
1519 {
1520 wxItemResource *res = (wxItemResource *)node->Data();
1521 wxString resType(res->GetType());
1522 if (resType == "wxDialog")
1523 {
1524 wxNode *node1 = res->GetChildren().First();
1525 while (node1)
1526 {
1527 wxItemResource *child = (wxItemResource *)node1->Data();
1528 wxString childResType(child->GetType());
1529
1530 if ((childResType == "wxMessage" || childResType == "wxButton") &&
1531 child->GetValue4() &&
1532 (strcmp(child->GetValue4(), resourceName) == 0))
1533 return TRUE;
1534 node1 = node1->Next();
1535 }
1536 }
1537 }
1538 return FALSE;
1539}
1540
1541// Given a wxButton or wxMessage, find the corresponding bitmap filename.
1542char *wxResourceManager::FindBitmapFilenameForResource(wxItemResource *resource)
1543{
1544 if (!resource || !resource->GetValue4())
1545 return NULL;
ae8351fc 1546 wxItemResource *bitmapResource = m_resourceTable.FindResource(resource->GetValue4());
457814b5
JS
1547 if (!bitmapResource)
1548 return NULL;
1549
1550 wxNode *node = bitmapResource->GetChildren().First();
1551 while (node)
1552 {
1553 // Eventually augment this to return a bitmap of the right kind or something...
1554 // Maybe the root of the filename remains the same, so it doesn't matter which we
1555 // pick up. Otherwise how do we specify multiple filenames... too boring...
1556 wxItemResource *child = (wxItemResource *)node->Data();
1557 return child->GetName();
1558
1559 node = node->Next();
1560 }
1561 return NULL;
1562}
1563
1564wxItemResource *wxResourceManager::FindBitmapResourceByFilename(char *filename)
1565{
ae8351fc 1566 m_resourceTable.BeginFind();
457814b5 1567 wxNode *node;
ae8351fc 1568 while (node = m_resourceTable.Next())
457814b5
JS
1569 {
1570 wxItemResource *res = (wxItemResource *)node->Data();
1571 wxString resType(res->GetType());
1572 if (resType == "wxBitmap")
1573 {
1574 wxNode *node1 = res->GetChildren().First();
1575 while (node1)
1576 {
1577 wxItemResource *child = (wxItemResource *)node1->Data();
1578 if (child->GetName() && (strcmp(child->GetName(), filename) == 0))
1579 return res;
1580 node1 = node1->Next();
1581 }
1582 }
1583 }
1584 return NULL;
1585}
1586
5de76427
JS
1587// Is this window identifier symbol in use?
1588// Let's assume that we can't have 2 names for the same integer id.
1589// Therefore we can tell by the integer id whether the symbol is
1590// in use.
1591bool wxResourceManager::IsSymbolUsed(wxItemResource* thisResource, wxWindowID id)
1592{
1593 m_resourceTable.BeginFind();
1594 wxNode *node;
1595 while (node = m_resourceTable.Next())
1596 {
1597 wxItemResource *res = (wxItemResource *)node->Data();
5de76427
JS
1598
1599 wxString resType(res->GetType());
1600 if (resType == "wxDialog" || resType == "wxDialogBox" || resType == "wxPanel")
1601 {
bbcdf8bc
JS
1602 if ((res != thisResource) && (res->GetId() == id))
1603 return TRUE;
1604
5de76427
JS
1605 wxNode *node1 = res->GetChildren().First();
1606 while (node1)
1607 {
1608 wxItemResource *child = (wxItemResource *)node1->Data();
1609 if ((child != thisResource) && (child->GetId() == id))
1610 return TRUE;
1611 node1 = node1->Next();
1612 }
1613 }
1614 }
1615 return FALSE;
1616}
1617
1618// Is this window identifier compatible with the given name? (i.e.
1619// does it already exist under a different name)
1620bool wxResourceManager::IsIdentifierOK(const wxString& name, wxWindowID id)
1621{
1622 if (m_symbolTable.SymbolExists(name))
1623 {
1624 int foundId = m_symbolTable.GetIdForSymbol(name);
1625 if (foundId != id)
1626 return FALSE;
1627 }
1628 return TRUE;
1629}
1630
1631// Change all integer ids that match oldId, to newId.
1632// This is necessary if an id is changed for one resource - all resources
1633// must be changed.
1634void wxResourceManager::ChangeIds(int oldId, int newId)
1635{
1636 m_resourceTable.BeginFind();
1637 wxNode *node;
1638 while (node = m_resourceTable.Next())
1639 {
1640 wxItemResource *res = (wxItemResource *)node->Data();
5de76427
JS
1641
1642 wxString resType(res->GetType());
1643 if (resType == "wxDialog" || resType == "wxDialogBox" || resType == "wxPanel")
1644 {
bbcdf8bc
JS
1645 if (res->GetId() == oldId)
1646 res->SetId(newId);
1647
5de76427
JS
1648 wxNode *node1 = res->GetChildren().First();
1649 while (node1)
1650 {
1651 wxItemResource *child = (wxItemResource *)node1->Data();
1652 if (child->GetId() == oldId)
1653 child->SetId(newId);
1654
1655 node1 = node1->Next();
1656 }
1657 }
1658 }
1659}
1660
bbcdf8bc
JS
1661// If any resource ids were missing (or their symbol was missing),
1662// repair them i.e. give them new ids. Returns TRUE if any resource
1663// needed repairing.
1664bool wxResourceManager::RepairResourceIds()
1665{
1666 bool repaired = FALSE;
1667
1668 m_resourceTable.BeginFind();
1669 wxNode *node;
1670 while (node = m_resourceTable.Next())
1671 {
1672 wxItemResource *res = (wxItemResource *)node->Data();
1673 wxString resType(res->GetType());
1674 if (resType == "wxDialog" || resType == "wxDialogBox" || resType == "wxPanel")
1675 {
1676
1677 if ( (res->GetId() == 0) || ((res->GetId() > 0) && !m_symbolTable.IdExists(res->GetId())) )
1678 {
1679 wxString newSymbolName;
1680 int newId = GenerateWindowId("ID_DIALOG", newSymbolName) ;
1681
1682 if (res->GetId() == 0)
1683 {
1684 res->SetId(newId);
1685 m_symbolTable.AddSymbol(newSymbolName, newId);
1686 }
1687 else
1688 {
1689 m_symbolTable.AddSymbol(newSymbolName, res->GetId());
1690 }
1691
1692 repaired = TRUE;
1693 }
1694
1695 wxNode *node1 = res->GetChildren().First();
1696 while (node1)
1697 {
1698 wxItemResource *child = (wxItemResource *)node1->Data();
1699
1700 if ( (child->GetId() == 0) || ((child->GetId() > 0) && !m_symbolTable.IdExists(child->GetId())) )
1701 {
1702 wxString newSymbolName;
1703 int newId = GenerateWindowId("ID_CONTROL", newSymbolName) ;
1704
1705 if (child->GetId() == 0)
1706 {
1707 child->SetId(newId);
1708 m_symbolTable.AddSymbol(newSymbolName, newId);
1709 }
1710 else
1711 {
1712 m_symbolTable.AddSymbol(newSymbolName, child->GetId());
1713 }
1714
1715 repaired = TRUE;
1716 }
1717
1718 node1 = node1->Next();
1719 }
1720 }
1721 }
1722 return repaired;
1723}
1724
1725
457814b5
JS
1726 // Deletes 'win' and creates a new window from the resource that
1727 // was associated with it. E.g. if you can't change properties on the
1728 // fly, you'll need to delete the window and create it again.
1729wxWindow *wxResourceManager::RecreateWindowFromResource(wxWindow *win, wxWindowPropertyInfo *info)
1730{
1731 wxItemResource *resource = FindResourceForWindow(win);
1732
1733 // Put the current window properties into the wxItemResource object
1734
1735 wxWindowPropertyInfo *newInfo = NULL;
1736 if (!info)
1737 {
ae8351fc 1738 newInfo = CreatePropertyInfoForWindow(win);
457814b5
JS
1739 info = newInfo;
1740 }
1741
1742 info->InstantiateResource(resource);
1743
1744 wxWindow *newWin = NULL;
1745 wxWindow *parent = win->GetParent();
1746
1747 if (win->IsKindOf(CLASSINFO(wxPanel)))
1748 {
457814b5
JS
1749 Edit(resource);
1750 newWin = FindWindowForResource(resource);
1751 }
1752 else
1753 {
ae8351fc 1754 DisassociateResource(resource);
9c331ded
JS
1755 if (win->GetEventHandler() != win)
1756 win->PopEventHandler(TRUE);
1757
ae8351fc
JS
1758 DeleteWindow(win);
1759 newWin = m_resourceTable.CreateItem((wxPanel *)parent, resource);
9c331ded 1760 newWin->PushEventHandler(new wxResourceEditorControlHandler((wxControl*) newWin, (wxControl*) newWin));
457814b5
JS
1761 AssociateResource(resource, newWin);
1762 UpdateResourceList();
1763 }
1764
1765 if (info)
1766 info->SetPropertyWindow(newWin);
1767
1768 if (newInfo)
1769 delete newInfo;
1770
1771 return newWin;
1772}
1773
1774// Delete resource highlighted in the listbox
ae8351fc 1775bool wxResourceManager::DeleteSelection()
457814b5 1776{
ae8351fc 1777 int sel = m_editorResourceTree->GetSelection();
03f68f12 1778 if (sel != 0)
457814b5 1779 {
ae8351fc 1780 wxItemResource *res = (wxItemResource *)m_editorResourceTree->GetItemData(sel);
457814b5 1781 wxWindow *win = FindWindowForResource(res);
ae8351fc 1782 if (win)
457814b5 1783 {
ae8351fc
JS
1784 DeleteResource(win);
1785 DeleteWindow(win);
1786 UpdateResourceList();
1787 Modify(TRUE);
457814b5 1788 }
ae8351fc 1789 return TRUE;
457814b5
JS
1790 }
1791
1792 return FALSE;
1793}
1794
1795// Delete resource highlighted in the listbox
ae8351fc 1796bool wxResourceManager::RecreateSelection()
457814b5
JS
1797{
1798 wxNode *node = GetSelections().First();
1799 while (node)
1800 {
1801 wxControl *item = (wxControl *)node->Data();
1802 wxResourceEditorControlHandler *childHandler = (wxResourceEditorControlHandler *)item->GetEventHandler();
1803 wxNode *next = node->Next();
1804 childHandler->SelectItem(FALSE);
1805
1806 RemoveSelection(item);
1807
1808 RecreateWindowFromResource(item);
1809
1810 node = next;
1811 }
1812 return TRUE;
1813}
1814
1815bool wxResourceManager::EditDialog(wxDialog *dialog, wxWindow *parent)
1816{
1817 return FALSE;
1818}
1819
457814b5
JS
1820// Ensures that all currently shown windows are saved to resources,
1821// e.g. just before writing to a .wxr file.
ae8351fc 1822bool wxResourceManager::InstantiateAllResourcesFromWindows()
457814b5 1823{
ae8351fc 1824 m_resourceTable.BeginFind();
457814b5 1825 wxNode *node;
ae8351fc 1826 while (node = m_resourceTable.Next())
457814b5
JS
1827 {
1828 wxItemResource *res = (wxItemResource *)node->Data();
1829 wxString resType(res->GetType());
1830
1831 if (resType == "wxDialog")
1832 {
1833 wxWindow *win = (wxWindow *)FindWindowForResource(res);
1834 if (win)
1835 InstantiateResourceFromWindow(res, win, TRUE);
1836 }
1837 else if (resType == "wxPanel")
1838 {
1839 wxWindow *win = (wxWindow *)FindWindowForResource(res);
1840 if (win)
1841 InstantiateResourceFromWindow(res, win, TRUE);
1842 }
1843 }
1844 return TRUE;
1845}
1846
1847bool wxResourceManager::InstantiateResourceFromWindow(wxItemResource *resource, wxWindow *window, bool recurse)
1848{
ae8351fc 1849 wxWindowPropertyInfo *info = CreatePropertyInfoForWindow(window);
457814b5
JS
1850 info->SetResource(resource);
1851 info->InstantiateResource(resource);
1852 delete info;
1853
1854 if (recurse)
1855 {
1856 wxNode *node = resource->GetChildren().First();
1857 while (node)
1858 {
1859 wxItemResource *child = (wxItemResource *)node->Data();
1860 wxWindow *childWindow = FindWindowForResource(child);
1861
1862 if (!childWindow)
1863 {
1864 char buf[200];
1865 sprintf(buf, "Could not find window %s", child->GetName());
1866 wxMessageBox(buf, "Dialog Editor problem", wxOK);
1867 }
1868 else
1869 InstantiateResourceFromWindow(child, childWindow, recurse);
1870 node = node->Next();
1871 }
1872 }
1873
1874 return TRUE;
1875}
1876
ae8351fc
JS
1877// Create a window information object for the give window
1878wxWindowPropertyInfo *wxResourceManager::CreatePropertyInfoForWindow(wxWindow *win)
1879{
1880 wxWindowPropertyInfo *info = NULL;
1881 if (win->IsKindOf(CLASSINFO(wxScrollBar)))
1882 {
1883 info = new wxScrollBarPropertyInfo(win);
1884 }
1885 else if (win->IsKindOf(CLASSINFO(wxStaticBox)))
1886 {
1887 info = new wxGroupBoxPropertyInfo(win);
1888 }
1889 else if (win->IsKindOf(CLASSINFO(wxCheckBox)))
1890 {
1891 info = new wxCheckBoxPropertyInfo(win);
1892 }
1893 else if (win->IsKindOf(CLASSINFO(wxSlider)))
1894 {
1895 info = new wxSliderPropertyInfo(win);
1896 }
1897 else if (win->IsKindOf(CLASSINFO(wxGauge)))
1898 {
1899 info = new wxGaugePropertyInfo(win);
1900 }
1901 else if (win->IsKindOf(CLASSINFO(wxListBox)))
1902 {
1903 info = new wxListBoxPropertyInfo(win);
1904 }
1905 else if (win->IsKindOf(CLASSINFO(wxRadioBox)))
1906 {
1907 info = new wxRadioBoxPropertyInfo(win);
1908 }
03f68f12
JS
1909 else if (win->IsKindOf(CLASSINFO(wxRadioButton)))
1910 {
1911 info = new wxRadioButtonPropertyInfo(win);
1912 }
ae8351fc
JS
1913 else if (win->IsKindOf(CLASSINFO(wxChoice)))
1914 {
1915 info = new wxChoicePropertyInfo(win);
1916 }
9c331ded
JS
1917 else if (win->IsKindOf(CLASSINFO(wxComboBox)))
1918 {
1919 info = new wxComboBoxPropertyInfo(win);
1920 }
ae8351fc
JS
1921 else if (win->IsKindOf(CLASSINFO(wxButton)))
1922 {
1923 info = new wxButtonPropertyInfo(win);
1924 }
1925 else if (win->IsKindOf(CLASSINFO(wxBitmapButton)))
1926 {
1927 info = new wxBitmapButtonPropertyInfo(win);
1928 }
1929 else if (win->IsKindOf(CLASSINFO(wxStaticText)))
1930 {
1931 info = new wxStaticTextPropertyInfo(win);
1932 }
1933 else if (win->IsKindOf(CLASSINFO(wxStaticBitmap)))
1934 {
1935 info = new wxStaticBitmapPropertyInfo(win);
1936 }
1937 else if (win->IsKindOf(CLASSINFO(wxTextCtrl)))
1938 {
1939 info = new wxTextPropertyInfo(win);
1940 }
1941 else if (win->IsKindOf(CLASSINFO(wxPanel)))
1942 {
1943 info = new wxPanelPropertyInfo(win);
1944 }
1945 else
1946 {
1947 info = new wxWindowPropertyInfo(win);
1948 }
1949 return info;
1950}
1951
1952// Edit the given window
1953void wxResourceManager::EditWindow(wxWindow *win)
1954{
1955 wxWindowPropertyInfo *info = CreatePropertyInfoForWindow(win);
1956 if (info)
1957 {
1958 info->SetResource(FindResourceForWindow(win));
1959 wxString str("Editing ");
1960 str += win->GetClassInfo()->GetClassName();
1961 str += ": ";
1962 if (win->GetName() != "")
1963 str += win->GetName();
1964 else
1965 str += "properties";
1966 info->Edit(NULL, str);
1967 }
1968}
1969
5de76427
JS
1970// Generate a window id and a first stab at a name
1971int wxResourceManager::GenerateWindowId(const wxString& prefix, wxString& idName)
1972{
1973 m_symbolIdCounter ++;
1974 while (m_symbolTable.IdExists(m_symbolIdCounter))
1975 m_symbolIdCounter ++;
1976
1977 int nameId = m_symbolIdCounter;
1978
1979 wxString str;
1980 str.Printf("%d", nameId);
1981 idName = prefix + str;
1982
1983 while (m_symbolTable.SymbolExists(idName))
1984 {
1985 nameId ++;
1986 str.Printf("%d", nameId);
1987 idName = prefix + str;
1988 }
1989
1990 return m_symbolIdCounter;
1991}
1992
457814b5
JS
1993
1994/*
1995 * Resource editor frame
1996 */
ae8351fc
JS
1997
1998IMPLEMENT_CLASS(wxResourceEditorFrame, wxFrame)
1999
2000BEGIN_EVENT_TABLE(wxResourceEditorFrame, wxFrame)
2001 EVT_MENU(wxID_NEW, wxResourceEditorFrame::OnNew)
2002 EVT_MENU(RESED_NEW_DIALOG, wxResourceEditorFrame::OnNewDialog)
2003 EVT_MENU(wxID_OPEN, wxResourceEditorFrame::OnOpen)
2004 EVT_MENU(RESED_CLEAR, wxResourceEditorFrame::OnClear)
2005 EVT_MENU(wxID_SAVE, wxResourceEditorFrame::OnSave)
2006 EVT_MENU(wxID_SAVEAS, wxResourceEditorFrame::OnSaveAs)
2007 EVT_MENU(wxID_EXIT, wxResourceEditorFrame::OnExit)
2008 EVT_MENU(wxID_ABOUT, wxResourceEditorFrame::OnAbout)
2009 EVT_MENU(RESED_CONTENTS, wxResourceEditorFrame::OnContents)
2010 EVT_MENU(RESED_DELETE, wxResourceEditorFrame::OnDeleteSelection)
2011 EVT_MENU(RESED_RECREATE, wxResourceEditorFrame::OnRecreateSelection)
2012 EVT_MENU(RESED_TEST, wxResourceEditorFrame::OnTest)
2013END_EVENT_TABLE()
2014
2015wxResourceEditorFrame::wxResourceEditorFrame(wxResourceManager *resMan, wxFrame *parent, const wxString& title,
2016 const wxPoint& pos, const wxSize& size, long style, const wxString& name):
2017 wxFrame(parent, -1, title, pos, size, style, name)
457814b5
JS
2018{
2019 manager = resMan;
2020}
2021
ae8351fc 2022wxResourceEditorFrame::~wxResourceEditorFrame()
457814b5
JS
2023{
2024}
2025
b127f301 2026void wxResourceEditorFrame::OnNew(wxCommandEvent& WXUNUSED(event))
457814b5 2027{
457814b5 2028 manager->New(FALSE);
ae8351fc
JS
2029}
2030
b127f301 2031void wxResourceEditorFrame::OnNewDialog(wxCommandEvent& WXUNUSED(event))
ae8351fc 2032{
457814b5 2033 manager->CreateNewPanel();
ae8351fc
JS
2034}
2035
b127f301 2036void wxResourceEditorFrame::OnOpen(wxCommandEvent& WXUNUSED(event))
ae8351fc 2037{
457814b5 2038 manager->New(TRUE);
ae8351fc
JS
2039}
2040
b127f301 2041void wxResourceEditorFrame::OnClear(wxCommandEvent& WXUNUSED(event))
ae8351fc 2042{
457814b5 2043 manager->Clear(TRUE, FALSE);
ae8351fc
JS
2044}
2045
b127f301 2046void wxResourceEditorFrame::OnSave(wxCommandEvent& WXUNUSED(event))
ae8351fc 2047{
457814b5 2048 manager->Save();
ae8351fc
JS
2049}
2050
b127f301 2051void wxResourceEditorFrame::OnSaveAs(wxCommandEvent& WXUNUSED(event))
ae8351fc 2052{
457814b5 2053 manager->SaveAs();
ae8351fc
JS
2054}
2055
b127f301 2056void wxResourceEditorFrame::OnExit(wxCommandEvent& WXUNUSED(event))
ae8351fc 2057{
457814b5
JS
2058 manager->Clear(TRUE, FALSE) ;
2059 this->Close();
ae8351fc
JS
2060}
2061
b127f301 2062void wxResourceEditorFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
ae8351fc 2063{
457814b5
JS
2064 char buf[300];
2065 sprintf(buf, "wxWindows Dialog Editor %.1f\nAuthor: Julian Smart J.Smart@ed.ac.uk\nJulian Smart (c) 1996", wxDIALOG_EDITOR_VERSION);
ae8351fc
JS
2066 wxMessageBox(buf, "About Dialog Editor", wxOK|wxCENTRE);
2067}
2068
b127f301 2069void wxResourceEditorFrame::OnTest(wxCommandEvent& WXUNUSED(event))
ae8351fc 2070{
03f68f12 2071 manager->TestCurrentDialog(this);
ae8351fc
JS
2072}
2073
b127f301 2074void wxResourceEditorFrame::OnContents(wxCommandEvent& WXUNUSED(event))
ae8351fc 2075{
d0fff5cb 2076#ifdef __WXMSW__
457814b5 2077 wxBeginBusyCursor();
ae8351fc
JS
2078 manager->GetHelpController()->LoadFile();
2079 manager->GetHelpController()->DisplayContents();
457814b5 2080 wxEndBusyCursor();
b127f301 2081#endif
ae8351fc
JS
2082}
2083
b127f301 2084void wxResourceEditorFrame::OnDeleteSelection(wxCommandEvent& WXUNUSED(event))
ae8351fc 2085{
457814b5 2086 manager->DeleteSelection();
ae8351fc
JS
2087}
2088
b127f301 2089void wxResourceEditorFrame::OnRecreateSelection(wxCommandEvent& WXUNUSED(event))
ae8351fc 2090{
457814b5 2091 manager->RecreateSelection();
457814b5
JS
2092}
2093
ae8351fc 2094bool wxResourceEditorFrame::OnClose()
457814b5
JS
2095{
2096 if (manager->Modified())
2097 {
457814b5
JS
2098 if (!manager->Clear(TRUE, FALSE))
2099 return FALSE;
2100 }
2101
2102 if (!Iconized())
2103 {
2104 int w, h;
2105 GetSize(&w, &h);
ae8351fc
JS
2106 manager->m_resourceEditorWindowSize.width = w;
2107 manager->m_resourceEditorWindowSize.height = h;
457814b5
JS
2108
2109 int x, y;
2110 GetPosition(&x, &y);
2111
ae8351fc
JS
2112 manager->m_resourceEditorWindowSize.x = x;
2113 manager->m_resourceEditorWindowSize.y = y;
457814b5
JS
2114 }
2115 manager->SetEditorFrame(NULL);
2116 manager->SetEditorToolBar(NULL);
457814b5
JS
2117
2118 return TRUE;
2119}
2120
2121/*
ae8351fc 2122 * Resource editor window that contains the dialog/panel being edited
457814b5 2123 */
ae8351fc
JS
2124
2125BEGIN_EVENT_TABLE(wxResourceEditorScrolledWindow, wxScrolledWindow)
2126 EVT_SCROLL(wxResourceEditorScrolledWindow::OnScroll)
2127 EVT_PAINT(wxResourceEditorScrolledWindow::OnPaint)
2128END_EVENT_TABLE()
2129
2130wxResourceEditorScrolledWindow::wxResourceEditorScrolledWindow(wxWindow *parent, const wxPoint& pos, const wxSize& size,
2131 long style):
2132 wxScrolledWindow(parent, -1, pos, size, style)
457814b5 2133{
ae8351fc
JS
2134 m_marginX = 10;
2135 m_marginY = 40;
2136 m_childWindow = NULL;
457814b5
JS
2137}
2138
ae8351fc 2139wxResourceEditorScrolledWindow::~wxResourceEditorScrolledWindow()
457814b5
JS
2140{
2141}
2142
ae8351fc 2143void wxResourceEditorScrolledWindow::OnScroll(wxScrollEvent& event)
457814b5 2144{
ae8351fc 2145 wxScrolledWindow::OnScroll(event);
457814b5 2146
ae8351fc
JS
2147 int x, y;
2148 ViewStart(& x, & y);
2149
2150 if (m_childWindow)
2151 m_childWindow->Move(m_marginX + (- x * 10), m_marginY + (- y * 10));
2152}
2153
b127f301 2154void wxResourceEditorScrolledWindow::OnPaint(wxPaintEvent& WXUNUSED(event))
ae8351fc
JS
2155{
2156 wxPaintDC dc(this);
2157
2158 DrawTitle(dc);
2159}
2160
2161void wxResourceEditorScrolledWindow::DrawTitle(wxDC& dc)
2162{
2163 if (m_childWindow)
2164 {
2165 wxItemResource* res = wxResourceManager::GetCurrentResourceManager()->FindResourceForWindow(m_childWindow);
2166 if (res)
2167 {
2168 wxString str(res->GetTitle());
2169 int x, y;
2170 ViewStart(& x, & y);
2171
2172 wxFont font(10, wxSWISS, wxNORMAL, wxBOLD);
2173 dc.SetFont(font);
2174 dc.SetBackgroundMode(wxTRANSPARENT);
2175 dc.SetTextForeground(wxColour(0, 0, 0));
2176
2177 long w, h;
2178 dc.GetTextExtent(str, & w, & h);
2179
2180 dc.DrawText(str, m_marginX + (- x * 10), m_marginY + (- y * 10) - h - 5);
2181 }
2182 }
457814b5
JS
2183}
2184
2185// Popup menu callback
2186void ObjectMenuProc(wxMenu& menu, wxCommandEvent& event)
2187{
2188 wxWindow *data = (wxWindow *)menu.GetClientData();
2189 if (!data)
2190 return;
2191
2192 switch (event.GetInt())
2193 {
2194 case OBJECT_MENU_EDIT:
2195 {
ae8351fc 2196 wxResourceManager::GetCurrentResourceManager()->EditWindow(data);
457814b5
JS
2197 break;
2198 }
2199 case OBJECT_MENU_DELETE:
2200 {
ae8351fc
JS
2201 wxResourceManager::GetCurrentResourceManager()->SaveInfoAndDeleteHandler(data);
2202 wxResourceManager::GetCurrentResourceManager()->DeleteResource(data);
2203 wxResourceManager::GetCurrentResourceManager()->DeleteWindow(data);
457814b5
JS
2204 break;
2205 }
2206 default:
2207 break;
2208 }
2209}
2210
457814b5
JS
2211/*
2212 * Main toolbar
2213 *
2214 */
2215
9d3221ab
RR
2216#ifdef __WXGTK__ // I don't dare to delete it...
2217
2218BEGIN_EVENT_TABLE(EditorToolBar, wxToolBar)
2219END_EVENT_TABLE()
2220
2221#else
2222
ae8351fc 2223BEGIN_EVENT_TABLE(EditorToolBar, wxToolBar)
457814b5
JS
2224 EVT_PAINT(EditorToolBar::OnPaint)
2225END_EVENT_TABLE()
2226
9d3221ab
RR
2227#endif
2228
ae8351fc 2229EditorToolBar::EditorToolBar(wxFrame *frame, const wxPoint& pos, const wxSize& size,
f449ef69
JS
2230 long style):
2231 wxToolBar(frame, -1, pos, size, style)
457814b5
JS
2232{
2233}
2234
2235bool EditorToolBar::OnLeftClick(int toolIndex, bool toggled)
2236{
ae8351fc
JS
2237 wxResourceManager *manager = wxResourceManager::GetCurrentResourceManager();
2238
457814b5
JS
2239 switch (toolIndex)
2240 {
2241 case TOOLBAR_LOAD_FILE:
2242 {
2243 manager->New(TRUE);
2244 break;
2245 }
2246 case TOOLBAR_NEW:
2247 {
ae8351fc 2248 manager->CreateNewPanel();
457814b5
JS
2249 break;
2250 }
2251 case TOOLBAR_SAVE_FILE:
2252 {
2253 manager->Save();
2254 break;
2255 }
2256 case TOOLBAR_HELP:
2257 {
d0fff5cb 2258#ifdef __WXMSW__
457814b5 2259 wxBeginBusyCursor();
ae8351fc
JS
2260 manager->GetHelpController()->LoadFile();
2261 manager->GetHelpController()->DisplayContents();
457814b5 2262 wxEndBusyCursor();
b127f301 2263#endif
457814b5
JS
2264 break;
2265 }
2266 case TOOLBAR_FORMAT_HORIZ:
2267 {
2268 manager->AlignItems(TOOLBAR_FORMAT_HORIZ);
2269 break;
2270 }
2271 case TOOLBAR_FORMAT_HORIZ_LEFT_ALIGN:
2272 {
2273 manager->AlignItems(TOOLBAR_FORMAT_HORIZ_LEFT_ALIGN);
2274 break;
2275 }
2276 case TOOLBAR_FORMAT_HORIZ_RIGHT_ALIGN:
2277 {
2278 manager->AlignItems(TOOLBAR_FORMAT_HORIZ_RIGHT_ALIGN);
2279 break;
2280 }
2281 case TOOLBAR_FORMAT_VERT:
2282 {
2283 manager->AlignItems(TOOLBAR_FORMAT_VERT);
2284 break;
2285 }
2286 case TOOLBAR_FORMAT_VERT_TOP_ALIGN:
2287 {
2288 manager->AlignItems(TOOLBAR_FORMAT_VERT_TOP_ALIGN);
2289 break;
2290 }
2291 case TOOLBAR_FORMAT_VERT_BOT_ALIGN:
2292 {
2293 manager->AlignItems(TOOLBAR_FORMAT_VERT_BOT_ALIGN);
2294 break;
2295 }
2296 case TOOLBAR_COPY_SIZE:
2297 {
2298 manager->CopySize();
2299 break;
2300 }
2301 case TOOLBAR_TO_BACK:
2302 {
2303 manager->ToBackOrFront(TRUE);
2304 break;
2305 }
2306 case TOOLBAR_TO_FRONT:
2307 {
2308 manager->ToBackOrFront(FALSE);
2309 break;
2310 }
2311 default:
2312 break;
2313 }
2314 return TRUE;
2315}
2316
2317void EditorToolBar::OnMouseEnter(int toolIndex)
2318{
2319 wxFrame *frame = (wxFrame *)GetParent();
2320
2321 if (!frame) return;
2322
2323 if (toolIndex > -1)
2324 {
2325 switch (toolIndex)
2326 {
2327 case TOOLBAR_LOAD_FILE:
2328 frame->SetStatusText("Load project file");
2329 break;
2330 case TOOLBAR_SAVE_FILE:
2331 frame->SetStatusText("Save project file");
2332 break;
2333 case TOOLBAR_NEW:
2334 frame->SetStatusText("Create a new resource");
2335 break;
2336 case TOOLBAR_FORMAT_HORIZ:
2337 frame->SetStatusText("Align items horizontally");
2338 break;
2339 case TOOLBAR_FORMAT_VERT:
2340 frame->SetStatusText("Align items vertically");
2341 break;
2342 case TOOLBAR_FORMAT_HORIZ_LEFT_ALIGN:
2343 frame->SetStatusText("Left-align items");
2344 break;
2345 case TOOLBAR_FORMAT_HORIZ_RIGHT_ALIGN:
2346 frame->SetStatusText("Right-align items");
2347 break;
2348 case TOOLBAR_FORMAT_VERT_TOP_ALIGN:
2349 frame->SetStatusText("Top-align items");
2350 break;
2351 case TOOLBAR_FORMAT_VERT_BOT_ALIGN:
2352 frame->SetStatusText("Bottom-align items");
2353 break;
2354 case TOOLBAR_COPY_SIZE:
2355 frame->SetStatusText("Copy size from first selection");
2356 break;
2357 case TOOLBAR_TO_FRONT:
2358 frame->SetStatusText("Put image to front");
2359 break;
2360 case TOOLBAR_TO_BACK:
2361 frame->SetStatusText("Put image to back");
2362 break;
2363 case TOOLBAR_HELP:
2364 frame->SetStatusText("Display help contents");
2365 break;
2366 default:
2367 break;
2368 }
2369 }
2370 else frame->SetStatusText("");
2371}
2372