1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Resource editor class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
15 #define wxDIALOG_EDITOR_VERSION 2.1
17 #if defined(__GNUG__) && !defined(__APPLE__)
18 #pragma interface "reseditr.h"
21 #include "wx/deprecated/setup.h"
24 #include "wx/string.h"
25 #include "wx/layout.h"
26 #include "wx/toolbar.h"
27 #include "wx/imaglist.h"
28 #include "wx/treectrl.h"
29 #include "wx/txtstrm.h"
31 #include "wx/deprecated/resource.h"
32 #include "wx/deprecated/proplist.h"
37 #define RESED_DELETE 301
38 #define RESED_RECREATE 303
39 #define RESED_CLEAR 304
40 #define RESED_NEW_DIALOG 305
41 #define RESED_NEW_PANEL 306
42 #define RESED_TEST 310
43 #define RESED_CONVERT_WXRS 311 // Convert old WXRs to new
45 #define RESED_CONTENTS 320
47 #define IDC_TREECTRL 500
48 #define IDC_LISTCTRL 501
50 // For control list ('palette')
51 #define RESED_POINTER 0
52 #define RESED_BUTTON 1
53 #define RESED_BMPBUTTON 2
54 #define RESED_STATICTEXT 3
55 #define RESED_STATICBMP 4
56 #define RESED_STATICBOX 5
57 #define RESED_TEXTCTRL_SINGLE 6
58 #define RESED_TEXTCTRL_MULTIPLE 7
59 #define RESED_LISTBOX 8
60 #define RESED_CHOICE 9
61 #define RESED_COMBOBOX 10
62 #define RESED_CHECKBOX 11
63 #define RESED_SLIDER 12
64 #define RESED_GAUGE 13
65 #define RESED_RADIOBOX 14
66 #define RESED_RADIOBUTTON 15
67 #define RESED_SCROLLBAR 16
68 #define RESED_TREECTRL 17
69 #define RESED_LISTCTRL 18
70 #define RESED_SPINBUTTON 19
73 * Controls loading, saving, user interface of resource editor(s).
76 class wxResourceEditorFrame
;
78 class wxWindowPropertyInfo
;
79 class wxResourceEditorProjectTree
;
80 class wxResourceEditorControlList
;
83 #define wxHelpController wxWinHelpController
85 #define wxHelpController wxHTMLHelpController;
88 class wxHelpController
;
91 * The resourceTable contains a list of wxItemResources (which each may
92 * have further children, defining e.g. a dialog box with controls).
94 * We need to associate actual windows with each wxItemResource,
95 * instead of the current 'one current window' scheme.
97 * - We create a new dialog, create a wxItemResource,
98 * associate the dialog with wxItemResource via a hash table.
99 * Must be a hash table in case dialog is deleted without
100 * telling the resource manager.
101 * - When we save the resource after editing/closing the dialog,
102 * we check the wxItemResource/wxDialog and children for
103 * consistency (throw away items no longer in the wxDialog,
104 * create any new wxItemResources).
105 * - We save the wxItemResources via the wxPropertyInfo classes,
106 * so devolve the code to the appropriate class.
107 * This involves creating a new temporary wxPropertyInfo for
110 * We currently assume we only create one instance of a window for
111 * each wxItemResource. We will need to relax this when we're editing
117 class wxResourceTableWithSaving
: public wxResourceTable
120 wxResourceTableWithSaving():wxResourceTable()
122 // Add all known window styles
125 virtual bool Save(const wxString
& filename
);
126 virtual bool SaveResource(wxTextOutputStream
& stream
, wxItemResource
* item
, wxItemResource
* parentItem
);
128 void GeneratePanelStyleString(long windowStyle
, char *buf
);
129 void GenerateDialogStyleString(long windowStyle
, char *buf
);
131 void GenerateControlStyleString(const wxString
& windowClass
, long windowStyle
, char *buf
);
133 void OutputFont(wxTextOutputStream
& stream
, const wxFont
& font
);
134 wxControl
*CreateItem(wxPanel
*panel
, const wxItemResource
*childResource
, const wxItemResource
* parentResource
);
137 wxWindowStyleTable m_styleTable
;
140 class wxResourceEditorScrolledWindow
;
142 class wxResourceManager
: public wxObject
144 friend class wxResourceEditorFrame
;
148 ~wxResourceManager();
152 // Initializes the resource manager
155 // Load/save window size etc.
159 // Show or hide the resource editor frame, which displays a list
160 // of resources with ability to edit them.
161 virtual bool ShowResourceEditor(bool show
, wxWindow
*parent
= NULL
, const char *title
= "wxWindows Dialog Editor");
163 // Convert old WXRs to new
164 virtual bool ConvertWXRs();
165 bool DoConvertWXR(const wxString
& oldPath
, const wxString
& newPath
);
166 bool ChangeOldToNewResource(wxItemResource
* parent
, wxItemResource
* res
);
167 bool InsertLabelResource(wxItemResource
* parent
, wxItemResource
* res
);
170 virtual bool SaveAs();
171 virtual bool Save(const wxString
& filename
);
172 virtual bool Load(const wxString
& filename
);
173 virtual bool Clear(bool deleteWindows
= TRUE
, bool force
= TRUE
);
174 virtual void SetFrameTitle(const wxString
& filename
);
175 virtual void ClearCurrentDialog();
176 virtual bool New(bool loadFromFile
= TRUE
, const wxString
& filename
= "");
177 virtual bool SaveIfModified();
178 virtual void AlignItems(int flag
);
179 virtual void CopySize(int command
); // Copy width, height or both from first control
180 virtual void ToBackOrFront(bool toBack
);
181 virtual void DistributePositions(int command
); // Distribute controls evenly between first and last
182 virtual wxWindow
*FindParentOfSelection();
184 virtual wxFrame
*OnCreateEditorFrame(const char *title
);
185 virtual wxMenuBar
*OnCreateEditorMenuBar(wxFrame
*parent
);
186 virtual wxResourceEditorScrolledWindow
*OnCreateEditorPanel(wxFrame
*parent
);
187 virtual wxToolBar
*OnCreateToolBar(wxFrame
*parent
);
189 // Create a window information object for the given window
190 wxWindowPropertyInfo
* CreatePropertyInfoForWindow(wxWindow
*win
);
191 // Edit the given window
192 void EditWindow(wxWindow
*win
);
194 virtual void UpdateResourceList();
195 virtual void AddItemsRecursively(long parent
, wxItemResource
*resource
);
196 virtual bool EditSelectedResource();
197 virtual bool Edit(wxItemResource
*res
);
198 virtual bool CreateNewPanel();
199 virtual bool CreatePanelItem(wxItemResource
*panelResource
, wxPanel
*panel
, char *itemType
, int x
= 10, int y
= 10, bool isBitmap
= FALSE
);
200 virtual bool DeleteSelection();
201 virtual bool TestCurrentDialog(wxWindow
* parent
);
203 // Saves the window info into the resource, and deletes the
204 // handler. Doesn't actually disassociate the window from
205 // the resources. Replaces OnClose.
206 virtual bool SaveInfoAndDeleteHandler(wxWindow
* win
);
208 // Destroys the window. If this is the 'current' panel, NULLs the
210 virtual bool DeleteWindow(wxWindow
* win
);
211 virtual bool DeleteResource(wxItemResource
*res
);
212 virtual bool DeleteResource(wxWindow
*win
);
214 // Add bitmap resource if there isn't already one with this filename.
215 virtual wxString
AddBitmapResource(const wxString
& filename
);
217 // Delete the bitmap resource if it isn't being used by another resource.
218 virtual void PossiblyDeleteBitmapResource(const wxString
& resourceName
);
220 // Helper function for above
221 virtual bool IsBitmapResourceUsed(const wxString
& resourceName
);
223 wxItemResource
*FindBitmapResourceByFilename(const wxString
& filename
);
225 wxString
FindBitmapFilenameForResource(wxItemResource
*resource
);
227 // Is this window identifier in use?
228 bool IsSymbolUsed(wxItemResource
* thisResource
, wxWindowID id
) ;
230 // Is this window identifier compatible with the given name? (i.e.
231 // does it already exist under a different name)
232 bool IsIdentifierOK(const wxString
& name
, wxWindowID id
);
234 // Change all integer ids that match oldId, to newId.
235 // This is necessary if an id is changed for one resource - all resources
237 void ChangeIds(int oldId
, int newId
);
239 // If any resource ids were missing (or their symbol was missing),
240 // repair them i.e. give them new ids. Returns TRUE if any resource
242 bool RepairResourceIds();
244 // Deletes 'win' and creates a new window from the resource that
245 // was associated with it. E.g. if you can't change properties on the
246 // fly, you'll need to delete the window and create it again.
247 virtual wxWindow
*RecreateWindowFromResource(wxWindow
*win
, wxWindowPropertyInfo
*info
= NULL
, bool instantiateFirst
= TRUE
);
249 virtual bool RecreateSelection();
251 // Remove selection handles if this control is selected
252 void DeselectItemIfNecessary(wxWindow
*win
);
254 // Need to search through resource table removing this from
255 // any resource which has this as a parent.
256 virtual bool RemoveResourceFromParent(wxItemResource
*res
);
258 virtual bool EditDialog(wxDialog
*dialog
, wxWindow
*parent
);
260 void AddSelection(wxWindow
*win
);
261 void RemoveSelection(wxWindow
*win
);
263 virtual void MakeUniqueName(char *prefix
, char *buf
);
265 // (Dis)associate resource<->physical window
266 // Doesn't delete any windows.
267 virtual void AssociateResource(wxItemResource
*resource
, wxWindow
*win
);
268 virtual bool DisassociateResource(wxItemResource
*resource
);
269 virtual bool DisassociateResource(wxWindow
*win
);
270 virtual bool DisassociateWindows();
271 virtual wxItemResource
*FindResourceForWindow(wxWindow
*win
);
272 virtual wxWindow
*FindWindowForResource(wxItemResource
*resource
);
274 virtual bool InstantiateAllResourcesFromWindows();
275 virtual bool InstantiateResourceFromWindow(wxItemResource
*resource
, wxWindow
*window
, bool recurse
= FALSE
);
278 inline void SetEditorFrame(wxFrame
*fr
) { m_editorFrame
= fr
; }
279 inline void SetEditorToolBar(EditorToolBar
*tb
) { m_editorToolBar
= tb
; }
280 inline wxFrame
*GetEditorFrame() const { return m_editorFrame
; }
281 inline wxResourceEditorProjectTree
*GetEditorResourceTree() const { return m_editorResourceTree
; }
282 inline wxResourceEditorControlList
*GetEditorControlList() const { return m_editorControlList
; }
283 inline wxList
& GetSelections() { return m_selections
; }
284 inline wxMenu
*GetPopupMenu() const { return m_popupMenu
; }
286 inline wxHelpController
*GetHelpController() const { return m_helpController
; }
289 inline void Modify(bool mod
= TRUE
) { m_modified
= mod
; }
290 inline bool Modified() const { return m_modified
; }
292 inline wxResourceTable
& GetResourceTable() { return m_resourceTable
; }
293 inline wxHashTable
& GetResourceAssociations() { return m_resourceAssociations
; }
295 inline wxString
GetCurrentFilename() const { return m_currentFilename
; }
296 static wxResourceManager
* GetCurrentResourceManager() { return sm_currentResourceManager
; }
298 inline void SetSymbolFilename(const wxString
& s
) { m_symbolFilename
= s
; }
299 inline wxString
GetSymbolFilename() const { return m_symbolFilename
; }
301 inline wxRect
& GetPropertyWindowSize() { return m_propertyWindowSize
; }
302 inline wxRect
& GetResourceEditorWindowSize() { return m_resourceEditorWindowSize
; }
304 wxResourceSymbolTable
& GetSymbolTable() { return m_symbolTable
; }
306 // Generate a window id and a first stab at a name
307 int GenerateWindowId(const wxString
& prefix
, wxString
& idName
) ;
312 wxHelpController
* m_helpController
;
314 wxResourceTableWithSaving m_resourceTable
;
315 wxFrame
* m_editorFrame
;
316 wxResourceEditorScrolledWindow
* m_editorPanel
;
318 wxResourceEditorProjectTree
* m_editorResourceTree
;
319 wxResourceEditorControlList
* m_editorControlList
;
320 EditorToolBar
* m_editorToolBar
;
322 int m_symbolIdCounter
; // For generating window ids
324 wxHashTable m_resourceAssociations
;
326 wxString m_currentFilename
;
327 wxBitmap
* m_bitmapImage
; // Default for static bitmaps/buttons
329 wxImageList m_imageList
;
330 long m_rootDialogItem
; // Root of dialog hierarchy in tree (unused)
332 // Options to be saved/restored
333 wxString m_optionsResourceFilename
; // e.g. dialoged.ini, .dialogrc
334 wxRect m_propertyWindowSize
;
335 wxRect m_resourceEditorWindowSize
;
336 static wxResourceManager
* sm_currentResourceManager
;
338 // Symbol table with identifiers for controls
339 wxResourceSymbolTable m_symbolTable
;
340 // Filename for include file, e.g. resource.h
341 wxString m_symbolFilename
;
345 class wxResourceEditorFrame
: public wxFrame
348 DECLARE_CLASS(wxResourceEditorFrame
)
350 wxResourceManager
*manager
;
351 wxResourceEditorFrame(wxResourceManager
*resMan
, wxFrame
*parent
, const wxString
& title
,
352 const wxPoint
& pos
= wxDefaultPosition
, const wxSize
& size
= wxSize(600, 400),
353 long style
= wxDEFAULT_FRAME_STYLE
, const wxString
& name
= "frame");
354 ~wxResourceEditorFrame();
356 void OnCloseWindow(wxCloseEvent
& event
);
358 void OnNew(wxCommandEvent
& event
);
359 void OnOpen(wxCommandEvent
& event
);
360 void OnNewDialog(wxCommandEvent
& event
);
361 void OnClear(wxCommandEvent
& event
);
362 void OnSave(wxCommandEvent
& event
);
363 void OnSaveAs(wxCommandEvent
& event
);
364 void OnExit(wxCommandEvent
& event
);
365 void OnAbout(wxCommandEvent
& event
);
366 void OnContents(wxCommandEvent
& event
);
367 void OnDeleteSelection(wxCommandEvent
& event
);
368 void OnRecreateSelection(wxCommandEvent
& event
);
369 void OnTest(wxCommandEvent
& event
);
370 void OnConvertWXRs(wxCommandEvent
& event
);
372 DECLARE_EVENT_TABLE()
375 class wxResourceEditorScrolledWindow
: public wxScrolledWindow
378 wxResourceEditorScrolledWindow(wxWindow
*parent
, const wxPoint
& pos
= wxDefaultPosition
, const wxSize
& size
= wxDefaultSize
,
380 ~wxResourceEditorScrolledWindow();
382 void OnPaint(wxPaintEvent
& event
);
384 void DrawTitle(wxDC
& dc
);
387 inline int GetMarginX() { return m_marginX
; }
388 inline int GetMarginY() { return m_marginY
; }
391 wxWindow
* m_childWindow
;
393 int m_marginX
, m_marginY
;
395 DECLARE_EVENT_TABLE()
398 #define OBJECT_MENU_TITLE 1
399 #define OBJECT_MENU_EDIT 2
400 #define OBJECT_MENU_DELETE 3
407 class EditorToolBar
: public wxToolBar
410 EditorToolBar(wxFrame
*frame
, const wxPoint
& pos
= wxPoint(0, 0), const wxSize
& size
= wxSize(0, 0),
411 long style
= wxTB_HORIZONTAL
);
412 bool OnLeftClick(int toolIndex
, bool toggled
);
413 void OnMouseEnter(int toolIndex
);
415 DECLARE_EVENT_TABLE()
419 #define TOOLBAR_LOAD_FILE 101
420 #define TOOLBAR_SAVE_FILE 102
421 #define TOOLBAR_NEW 103
422 #define TOOLBAR_TREE 105
423 #define TOOLBAR_HELP 106
426 #define TOOLBAR_FORMAT_HORIZ 110
427 #define TOOLBAR_FORMAT_HORIZ_LEFT_ALIGN 111
428 #define TOOLBAR_FORMAT_HORIZ_RIGHT_ALIGN 112
429 #define TOOLBAR_FORMAT_VERT 113
430 #define TOOLBAR_FORMAT_VERT_TOP_ALIGN 114
431 #define TOOLBAR_FORMAT_VERT_BOT_ALIGN 115
433 #define TOOLBAR_TO_FRONT 116
434 #define TOOLBAR_TO_BACK 117
435 #define TOOLBAR_COPY_SIZE 118
436 #define TOOLBAR_COPY_WIDTH 119
437 #define TOOLBAR_COPY_HEIGHT 120
438 #define TOOLBAR_DISTRIBUTE_HORIZ 121
439 #define TOOLBAR_DISTRIBUTE_VERT 122
442 * this class is used to store data associated with a tree item
444 class wxResourceTreeData
: public wxTreeItemData
447 wxResourceTreeData(wxItemResource
*resource
) { m_resource
= resource
; }
449 wxItemResource
*GetResource() const { return m_resource
; }
452 wxItemResource
*m_resource
;