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