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
18 #pragma interface "reseditr.h"
22 #include "wx/string.h"
23 #include "wx/layout.h"
24 #include "wx/resource.h"
25 #include "wx/toolbar.h"
26 #include "wx/imaglist.h"
27 #include "wx/treectrl.h"
28 #include "wx/proplist.h"
29 #include "wx/txtstrm.h"
33 #define RESED_DELETE 301
34 #define RESED_RECREATE 303
35 #define RESED_CLEAR 304
36 #define RESED_NEW_DIALOG 305
37 #define RESED_NEW_PANEL 306
38 #define RESED_TEST 310
39 #define RESED_CONVERT_WXRS 311 // Convert old WXRs to new
41 #define RESED_CONTENTS 320
43 #define IDC_TREECTRL 500
44 #define IDC_LISTCTRL 501
46 // For control list ('palette')
47 #define RESED_POINTER 0
48 #define RESED_BUTTON 1
49 #define RESED_BMPBUTTON 2
50 #define RESED_STATICTEXT 3
51 #define RESED_STATICBMP 4
52 #define RESED_STATICBOX 5
53 #define RESED_TEXTCTRL_SINGLE 6
54 #define RESED_TEXTCTRL_MULTIPLE 7
55 #define RESED_LISTBOX 8
56 #define RESED_CHOICE 9
57 #define RESED_COMBOBOX 10
58 #define RESED_CHECKBOX 11
59 #define RESED_SLIDER 12
60 #define RESED_GAUGE 13
61 #define RESED_RADIOBOX 14
62 #define RESED_RADIOBUTTON 15
63 #define RESED_SCROLLBAR 16
64 #define RESED_TREECTRL 17
65 #define RESED_LISTCTRL 18
66 #define RESED_SPINBUTTON 19
69 * Controls loading, saving, user interface of resource editor(s).
72 class wxResourceEditorFrame
;
74 class wxWindowPropertyInfo
;
75 class wxResourceEditorProjectTree
;
76 class wxResourceEditorControlList
;
79 #define wxHelpController wxWinHelpController
81 #define wxHelpController wxHTMLHelpController;
84 class wxHelpController
;
87 * The resourceTable contains a list of wxItemResources (which each may
88 * have further children, defining e.g. a dialog box with controls).
90 * We need to associate actual windows with each wxItemResource,
91 * instead of the current 'one current window' scheme.
93 * - We create a new dialog, create a wxItemResource,
94 * associate the dialog with wxItemResource via a hash table.
95 * Must be a hash table in case dialog is deleted without
96 * telling the resource manager.
97 * - When we save the resource after editing/closing the dialog,
98 * we check the wxItemResource/wxDialog and children for
99 * consistency (throw away items no longer in the wxDialog,
100 * create any new wxItemResources).
101 * - We save the wxItemResources via the wxPropertyInfo classes,
102 * so devolve the code to the appropriate class.
103 * This involves creating a new temporary wxPropertyInfo for
106 * We currently assume we only create one instance of a window for
107 * each wxItemResource. We will need to relax this when we're editing
113 class wxResourceTableWithSaving
: public wxResourceTable
116 wxResourceTableWithSaving():wxResourceTable()
118 // Add all known window styles
121 virtual bool Save(const wxString
& filename
);
122 virtual bool SaveResource(wxTextOutputStream
& stream
, wxItemResource
* item
, wxItemResource
* parentItem
);
124 void GeneratePanelStyleString(long windowStyle
, char *buf
);
125 void GenerateDialogStyleString(long windowStyle
, char *buf
);
127 void GenerateControlStyleString(const wxString
& windowClass
, long windowStyle
, char *buf
);
129 void OutputFont(wxTextOutputStream
& stream
, const wxFont
& font
);
130 wxControl
*CreateItem(wxPanel
*panel
, const wxItemResource
*childResource
, const wxItemResource
* parentResource
);
133 wxWindowStyleTable m_styleTable
;
136 class wxResourceEditorScrolledWindow
;
138 class wxResourceManager
: public wxObject
140 friend class wxResourceEditorFrame
;
144 ~wxResourceManager();
148 // Initializes the resource manager
151 // Load/save window size etc.
155 // Show or hide the resource editor frame, which displays a list
156 // of resources with ability to edit them.
157 virtual bool ShowResourceEditor(bool show
, wxWindow
*parent
= NULL
, const char *title
= "wxWindows Dialog Editor");
159 // Convert old WXRs to new
160 virtual bool ConvertWXRs();
161 bool DoConvertWXR(const wxString
& oldPath
, const wxString
& newPath
);
162 bool ChangeOldToNewResource(wxItemResource
* parent
, wxItemResource
* res
);
163 bool InsertLabelResource(wxItemResource
* parent
, wxItemResource
* res
);
166 virtual bool SaveAs();
167 virtual bool Save(const wxString
& filename
);
168 virtual bool Load(const wxString
& filename
);
169 virtual bool Clear(bool deleteWindows
= TRUE
, bool force
= TRUE
);
170 virtual void SetFrameTitle(const wxString
& filename
);
171 virtual void ClearCurrentDialog();
172 virtual bool New(bool loadFromFile
= TRUE
, const wxString
& filename
= "");
173 virtual bool SaveIfModified();
174 virtual void AlignItems(int flag
);
175 virtual void CopySize(int command
); // Copy width, height or both from first control
176 virtual void ToBackOrFront(bool toBack
);
177 virtual void DistributePositions(int command
); // Distribute controls evenly between first and last
178 virtual wxWindow
*FindParentOfSelection();
180 virtual wxFrame
*OnCreateEditorFrame(const char *title
);
181 virtual wxMenuBar
*OnCreateEditorMenuBar(wxFrame
*parent
);
182 virtual wxResourceEditorScrolledWindow
*OnCreateEditorPanel(wxFrame
*parent
);
183 virtual wxToolBar
*OnCreateToolBar(wxFrame
*parent
);
185 // Create a window information object for the give window
186 wxWindowPropertyInfo
* CreatePropertyInfoForWindow(wxWindow
*win
);
187 // Edit the given window
188 void EditWindow(wxWindow
*win
);
190 virtual void UpdateResourceList();
191 virtual void AddItemsRecursively(long parent
, wxItemResource
*resource
);
192 virtual bool EditSelectedResource();
193 virtual bool Edit(wxItemResource
*res
);
194 virtual bool CreateNewPanel();
195 virtual bool CreatePanelItem(wxItemResource
*panelResource
, wxPanel
*panel
, char *itemType
, int x
= 10, int y
= 10, bool isBitmap
= FALSE
);
196 virtual bool DeleteSelection();
197 virtual bool TestCurrentDialog(wxWindow
* parent
);
199 // Saves the window info into the resource, and deletes the
200 // handler. Doesn't actually disassociate the window from
201 // the resources. Replaces OnClose.
202 virtual bool SaveInfoAndDeleteHandler(wxWindow
* win
);
204 // Destroys the window. If this is the 'current' panel, NULLs the
206 virtual bool DeleteWindow(wxWindow
* win
);
207 virtual bool DeleteResource(wxItemResource
*res
);
208 virtual bool DeleteResource(wxWindow
*win
);
210 // Add bitmap resource if there isn't already one with this filename.
211 virtual wxString
AddBitmapResource(const wxString
& filename
);
213 // Delete the bitmap resource if it isn't being used by another resource.
214 virtual void PossiblyDeleteBitmapResource(const wxString
& resourceName
);
216 // Helper function for above
217 virtual bool IsBitmapResourceUsed(const wxString
& resourceName
);
219 wxItemResource
*FindBitmapResourceByFilename(const wxString
& filename
);
221 wxString
FindBitmapFilenameForResource(wxItemResource
*resource
);
223 // Is this window identifier in use?
224 bool IsSymbolUsed(wxItemResource
* thisResource
, wxWindowID id
) ;
226 // Is this window identifier compatible with the given name? (i.e.
227 // does it already exist under a different name)
228 bool IsIdentifierOK(const wxString
& name
, wxWindowID id
);
230 // Change all integer ids that match oldId, to newId.
231 // This is necessary if an id is changed for one resource - all resources
233 void ChangeIds(int oldId
, int newId
);
235 // If any resource ids were missing (or their symbol was missing),
236 // repair them i.e. give them new ids. Returns TRUE if any resource
238 bool RepairResourceIds();
240 // Deletes 'win' and creates a new window from the resource that
241 // was associated with it. E.g. if you can't change properties on the
242 // fly, you'll need to delete the window and create it again.
243 virtual wxWindow
*RecreateWindowFromResource(wxWindow
*win
, wxWindowPropertyInfo
*info
= NULL
, bool instantiateFirst
= TRUE
);
245 virtual bool RecreateSelection();
247 // Remove selection handles if this control is selected
248 void DeselectItemIfNecessary(wxWindow
*win
);
250 // Need to search through resource table removing this from
251 // any resource which has this as a parent.
252 virtual bool RemoveResourceFromParent(wxItemResource
*res
);
254 virtual bool EditDialog(wxDialog
*dialog
, wxWindow
*parent
);
256 void AddSelection(wxWindow
*win
);
257 void RemoveSelection(wxWindow
*win
);
259 virtual void MakeUniqueName(char *prefix
, char *buf
);
261 // (Dis)associate resource<->physical window
262 // Doesn't delete any windows.
263 virtual void AssociateResource(wxItemResource
*resource
, wxWindow
*win
);
264 virtual bool DisassociateResource(wxItemResource
*resource
);
265 virtual bool DisassociateResource(wxWindow
*win
);
266 virtual bool DisassociateWindows();
267 virtual wxItemResource
*FindResourceForWindow(wxWindow
*win
);
268 virtual wxWindow
*FindWindowForResource(wxItemResource
*resource
);
270 virtual bool InstantiateAllResourcesFromWindows();
271 virtual bool InstantiateResourceFromWindow(wxItemResource
*resource
, wxWindow
*window
, bool recurse
= FALSE
);
274 inline void SetEditorFrame(wxFrame
*fr
) { m_editorFrame
= fr
; }
275 inline void SetEditorToolBar(EditorToolBar
*tb
) { m_editorToolBar
= tb
; }
276 inline wxFrame
*GetEditorFrame() const { return m_editorFrame
; }
277 inline wxResourceEditorProjectTree
*GetEditorResourceTree() const { return m_editorResourceTree
; }
278 inline wxResourceEditorControlList
*GetEditorControlList() const { return m_editorControlList
; }
279 inline wxList
& GetSelections() { return m_selections
; }
280 inline wxMenu
*GetPopupMenu() const { return m_popupMenu
; }
282 inline wxHelpController
*GetHelpController() const { return m_helpController
; }
285 inline void Modify(bool mod
= TRUE
) { m_modified
= mod
; }
286 inline bool Modified() const { return m_modified
; }
288 inline wxResourceTable
& GetResourceTable() { return m_resourceTable
; }
289 inline wxHashTable
& GetResourceAssociations() { return m_resourceAssociations
; }
291 inline wxString
GetCurrentFilename() const { return m_currentFilename
; }
292 static wxResourceManager
* GetCurrentResourceManager() { return sm_currentResourceManager
; }
294 inline void SetSymbolFilename(const wxString
& s
) { m_symbolFilename
= s
; }
295 inline wxString
GetSymbolFilename() const { return m_symbolFilename
; }
297 inline wxRect
& GetPropertyWindowSize() { return m_propertyWindowSize
; }
298 inline wxRect
& GetResourceEditorWindowSize() { return m_resourceEditorWindowSize
; }
300 wxResourceSymbolTable
& GetSymbolTable() { return m_symbolTable
; }
302 // Generate a window id and a first stab at a name
303 int GenerateWindowId(const wxString
& prefix
, wxString
& idName
) ;
308 wxHelpController
* m_helpController
;
310 wxResourceTableWithSaving m_resourceTable
;
311 wxFrame
* m_editorFrame
;
312 wxResourceEditorScrolledWindow
* m_editorPanel
;
314 wxResourceEditorProjectTree
* m_editorResourceTree
;
315 wxResourceEditorControlList
* m_editorControlList
;
316 EditorToolBar
* m_editorToolBar
;
318 int m_symbolIdCounter
; // For generating window ids
320 wxHashTable m_resourceAssociations
;
322 wxString m_currentFilename
;
323 wxBitmap
* m_bitmapImage
; // Default for static bitmaps/buttons
325 wxImageList m_imageList
;
326 long m_rootDialogItem
; // Root of dialog hierarchy in tree (unused)
328 // Options to be saved/restored
329 wxString m_optionsResourceFilename
; // e.g. dialoged.ini, .dialogrc
330 wxRect m_propertyWindowSize
;
331 wxRect m_resourceEditorWindowSize
;
332 static wxResourceManager
* sm_currentResourceManager
;
334 // Symbol table with identifiers for controls
335 wxResourceSymbolTable m_symbolTable
;
336 // Filename for include file, e.g. resource.h
337 wxString m_symbolFilename
;
341 class wxResourceEditorFrame
: public wxFrame
344 DECLARE_CLASS(wxResourceEditorFrame
)
346 wxResourceManager
*manager
;
347 wxResourceEditorFrame(wxResourceManager
*resMan
, wxFrame
*parent
, const wxString
& title
,
348 const wxPoint
& pos
= wxDefaultPosition
, const wxSize
& size
= wxSize(600, 400),
349 long style
= wxDEFAULT_FRAME_STYLE
, const wxString
& name
= "frame");
350 ~wxResourceEditorFrame();
352 void OnCloseWindow(wxCloseEvent
& event
);
354 void OnNew(wxCommandEvent
& event
);
355 void OnOpen(wxCommandEvent
& event
);
356 void OnNewDialog(wxCommandEvent
& event
);
357 void OnClear(wxCommandEvent
& event
);
358 void OnSave(wxCommandEvent
& event
);
359 void OnSaveAs(wxCommandEvent
& event
);
360 void OnExit(wxCommandEvent
& event
);
361 void OnAbout(wxCommandEvent
& event
);
362 void OnContents(wxCommandEvent
& event
);
363 void OnDeleteSelection(wxCommandEvent
& event
);
364 void OnRecreateSelection(wxCommandEvent
& event
);
365 void OnTest(wxCommandEvent
& event
);
366 void OnConvertWXRs(wxCommandEvent
& event
);
368 DECLARE_EVENT_TABLE()
371 class wxResourceEditorScrolledWindow
: public wxScrolledWindow
374 wxResourceEditorScrolledWindow(wxWindow
*parent
, const wxPoint
& pos
= wxDefaultPosition
, const wxSize
& size
= wxDefaultSize
,
376 ~wxResourceEditorScrolledWindow();
378 void OnPaint(wxPaintEvent
& event
);
380 void DrawTitle(wxDC
& dc
);
383 inline int GetMarginX() { return m_marginX
; }
384 inline int GetMarginY() { return m_marginY
; }
387 wxWindow
* m_childWindow
;
389 int m_marginX
, m_marginY
;
391 DECLARE_EVENT_TABLE()
394 #define OBJECT_MENU_EDIT 1
395 #define OBJECT_MENU_DELETE 2
402 class EditorToolBar
: public wxToolBar
405 EditorToolBar(wxFrame
*frame
, const wxPoint
& pos
= wxPoint(0, 0), const wxSize
& size
= wxSize(0, 0),
406 long style
= wxTB_HORIZONTAL
);
407 bool OnLeftClick(int toolIndex
, bool toggled
);
408 void OnMouseEnter(int toolIndex
);
410 DECLARE_EVENT_TABLE()
414 #define TOOLBAR_LOAD_FILE 101
415 #define TOOLBAR_SAVE_FILE 102
416 #define TOOLBAR_NEW 103
417 #define TOOLBAR_TREE 105
418 #define TOOLBAR_HELP 106
421 #define TOOLBAR_FORMAT_HORIZ 110
422 #define TOOLBAR_FORMAT_HORIZ_LEFT_ALIGN 111
423 #define TOOLBAR_FORMAT_HORIZ_RIGHT_ALIGN 112
424 #define TOOLBAR_FORMAT_VERT 113
425 #define TOOLBAR_FORMAT_VERT_TOP_ALIGN 114
426 #define TOOLBAR_FORMAT_VERT_BOT_ALIGN 115
428 #define TOOLBAR_TO_FRONT 116
429 #define TOOLBAR_TO_BACK 117
430 #define TOOLBAR_COPY_SIZE 118
431 #define TOOLBAR_COPY_WIDTH 119
432 #define TOOLBAR_COPY_HEIGHT 120
433 #define TOOLBAR_DISTRIBUTE_HORIZ 121
434 #define TOOLBAR_DISTRIBUTE_VERT 122
437 * this class is used to store data associated with a tree item
439 class wxResourceTreeData
: public wxTreeItemData
442 wxResourceTreeData(wxItemResource
*resource
) { m_resource
= resource
; }
444 wxItemResource
*GetResource() const { return m_resource
; }
447 wxItemResource
*m_resource
;