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 1.6
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 1
34 #define RESED_RECREATE 3
36 #define RESED_NEW_DIALOG 5
37 #define RESED_NEW_PANEL 6
40 #define RESED_CONTENTS 20
42 #define IDC_TREECTRL 100
43 #define IDC_LISTCTRL 101
45 // For control list ('palette')
46 #define RESED_POINTER 0
47 #define RESED_BUTTON 1
48 #define RESED_BMPBUTTON 2
49 #define RESED_STATICTEXT 3
50 #define RESED_STATICBMP 4
51 #define RESED_STATICBOX 5
52 #define RESED_TEXTCTRL_SINGLE 6
53 #define RESED_TEXTCTRL_MULTIPLE 7
54 #define RESED_LISTBOX 8
55 #define RESED_CHOICE 9
56 #define RESED_COMBOBOX 10
57 #define RESED_CHECKBOX 11
58 #define RESED_SLIDER 12
59 #define RESED_GAUGE 13
60 #define RESED_RADIOBOX 14
61 #define RESED_RADIOBUTTON 15
62 #define RESED_SCROLLBAR 16
63 #define RESED_TREECTRL 17
64 #define RESED_LISTCTRL 18
65 #define RESED_SPINBUTTON 19
68 * Controls loading, saving, user interface of resource editor(s).
71 class wxResourceEditorFrame
;
73 class wxWindowPropertyInfo
;
74 class wxResourceEditorProjectTree
;
75 class wxResourceEditorControlList
;
78 #define wxHelpController wxWinHelpController
80 #define wxHelpController wxHTMLHelpController;
83 class wxHelpController
;
86 * The resourceTable contains a list of wxItemResources (which each may
87 * have further children, defining e.g. a dialog box with controls).
89 * We need to associate actual windows with each wxItemResource,
90 * instead of the current 'one current window' scheme.
92 * - We create a new dialog, create a wxItemResource,
93 * associate the dialog with wxItemResource via a hash table.
94 * Must be a hash table in case dialog is deleted without
95 * telling the resource manager.
96 * - When we save the resource after editing/closing the dialog,
97 * we check the wxItemResource/wxDialog and children for
98 * consistency (throw away items no longer in the wxDialog,
99 * create any new wxItemResources).
100 * - We save the wxItemResources via the wxPropertyInfo classes,
101 * so devolve the code to the appropriate class.
102 * This involves creating a new temporary wxPropertyInfo for
105 * We currently assume we only create one instance of a window for
106 * each wxItemResource. We will need to relax this when we're editing
112 class wxResourceTableWithSaving
: public wxResourceTable
115 wxResourceTableWithSaving():wxResourceTable()
117 // Add all known window styles
120 virtual bool Save(const wxString
& filename
);
121 virtual bool SaveResource(wxTextOutputStream
& stream
, wxItemResource
* item
, wxItemResource
* parentItem
);
123 void GeneratePanelStyleString(long windowStyle
, char *buf
);
124 void GenerateDialogStyleString(long windowStyle
, char *buf
);
127 void GenerateRadioBoxStyleString(long windowStyle, char *buf);
128 void GenerateMessageStyleString(long windowStyle, char *buf);
129 void GenerateTextStyleString(long windowStyle, char *buf);
130 void GenerateButtonStyleString(long windowStyle, char *buf);
131 void GenerateCheckBoxStyleString(long windowStyle, char *buf);
132 void GenerateRadioButtonStyleString(long windowStyle, char *buf);
133 void GenerateListBoxStyleString(long windowStyle, char *buf);
134 void GenerateSliderStyleString(long windowStyle, char *buf);
135 void GenerateGroupBoxStyleString(long windowStyle, char *buf);
136 void GenerateGaugeStyleString(long windowStyle, char *buf);
137 void GenerateChoiceStyleString(long windowStyle, char *buf);
138 void GenerateComboBoxStyleString(long windowStyle, char *buf);
139 void GenerateScrollBarStyleString(long windowStyle, char *buf);
142 void GenerateControlStyleString(const wxString
& windowClass
, long windowStyle
, char *buf
);
145 bool GenerateStyle(char *buf, long windowStyle, long flag, char *strStyle);
148 void OutputFont(wxTextOutputStream
& stream
, const wxFont
& font
);
149 wxControl
*CreateItem(wxPanel
*panel
, const wxItemResource
*childResource
, const wxItemResource
* parentResource
);
152 wxWindowStyleTable m_styleTable
;
155 class wxResourceEditorScrolledWindow
;
157 class wxResourceManager
: public wxObject
159 friend class wxResourceEditorFrame
;
163 ~wxResourceManager();
167 // Initializes the resource manager
170 // Load/save window size etc.
174 // Show or hide the resource editor frame, which displays a list
175 // of resources with ability to edit them.
176 virtual bool ShowResourceEditor(bool show
, wxWindow
*parent
= NULL
, const char *title
= "wxWindows Dialog Editor");
179 virtual bool SaveAs();
180 virtual bool Save(const wxString
& filename
);
181 virtual bool Load(const wxString
& filename
);
182 virtual bool Clear(bool deleteWindows
= TRUE
, bool force
= TRUE
);
183 virtual void SetFrameTitle(const wxString
& filename
);
184 virtual void ClearCurrentDialog();
185 virtual bool New(bool loadFromFile
= TRUE
, const wxString
& filename
= "");
186 virtual bool SaveIfModified();
187 virtual void AlignItems(int flag
);
188 virtual void CopySize();
189 virtual void ToBackOrFront(bool toBack
);
190 virtual wxWindow
*FindParentOfSelection();
192 virtual wxFrame
*OnCreateEditorFrame(const char *title
);
193 virtual wxMenuBar
*OnCreateEditorMenuBar(wxFrame
*parent
);
194 virtual wxResourceEditorScrolledWindow
*OnCreateEditorPanel(wxFrame
*parent
);
195 virtual wxToolBar
*OnCreateToolBar(wxFrame
*parent
);
197 // Create a window information object for the give window
198 wxWindowPropertyInfo
* CreatePropertyInfoForWindow(wxWindow
*win
);
199 // Edit the given window
200 void EditWindow(wxWindow
*win
);
202 virtual void UpdateResourceList();
203 virtual void AddItemsRecursively(long parent
, wxItemResource
*resource
);
204 virtual bool EditSelectedResource();
205 virtual bool Edit(wxItemResource
*res
);
206 virtual bool CreateNewPanel();
207 virtual bool CreatePanelItem(wxItemResource
*panelResource
, wxPanel
*panel
, char *itemType
, int x
= 10, int y
= 10, bool isBitmap
= FALSE
);
208 virtual bool DeleteSelection();
209 virtual bool TestCurrentDialog(wxWindow
* parent
);
211 // Saves the window info into the resource, and deletes the
212 // handler. Doesn't actually disassociate the window from
213 // the resources. Replaces OnClose.
214 virtual bool SaveInfoAndDeleteHandler(wxWindow
* win
);
216 // Destroys the window. If this is the 'current' panel, NULLs the
218 virtual bool DeleteWindow(wxWindow
* win
);
219 virtual bool DeleteResource(wxItemResource
*res
);
220 virtual bool DeleteResource(wxWindow
*win
);
222 // Add bitmap resource if there isn't already one with this filename.
223 virtual wxString
AddBitmapResource(const wxString
& filename
);
225 // Delete the bitmap resource if it isn't being used by another resource.
226 virtual void PossiblyDeleteBitmapResource(const wxString
& resourceName
);
228 // Helper function for above
229 virtual bool IsBitmapResourceUsed(const wxString
& resourceName
);
231 wxItemResource
*FindBitmapResourceByFilename(const wxString
& filename
);
233 wxString
FindBitmapFilenameForResource(wxItemResource
*resource
);
235 // Is this window identifier in use?
236 bool IsSymbolUsed(wxItemResource
* thisResource
, wxWindowID id
) ;
238 // Is this window identifier compatible with the given name? (i.e.
239 // does it already exist under a different name)
240 bool IsIdentifierOK(const wxString
& name
, wxWindowID id
);
242 // Change all integer ids that match oldId, to newId.
243 // This is necessary if an id is changed for one resource - all resources
245 void ChangeIds(int oldId
, int newId
);
247 // If any resource ids were missing (or their symbol was missing),
248 // repair them i.e. give them new ids. Returns TRUE if any resource
250 bool RepairResourceIds();
252 // Deletes 'win' and creates a new window from the resource that
253 // was associated with it. E.g. if you can't change properties on the
254 // fly, you'll need to delete the window and create it again.
255 virtual wxWindow
*RecreateWindowFromResource(wxWindow
*win
, wxWindowPropertyInfo
*info
= NULL
);
257 virtual bool RecreateSelection();
259 // Need to search through resource table removing this from
260 // any resource which has this as a parent.
261 virtual bool RemoveResourceFromParent(wxItemResource
*res
);
263 virtual bool EditDialog(wxDialog
*dialog
, wxWindow
*parent
);
265 void AddSelection(wxWindow
*win
);
266 void RemoveSelection(wxWindow
*win
);
268 virtual void MakeUniqueName(char *prefix
, char *buf
);
270 // (Dis)associate resource<->physical window
271 // Doesn't delete any windows.
272 virtual void AssociateResource(wxItemResource
*resource
, wxWindow
*win
);
273 virtual bool DisassociateResource(wxItemResource
*resource
);
274 virtual bool DisassociateResource(wxWindow
*win
);
275 virtual bool DisassociateWindows();
276 virtual wxItemResource
*FindResourceForWindow(wxWindow
*win
);
277 virtual wxWindow
*FindWindowForResource(wxItemResource
*resource
);
279 virtual bool InstantiateAllResourcesFromWindows();
280 virtual bool InstantiateResourceFromWindow(wxItemResource
*resource
, wxWindow
*window
, bool recurse
= FALSE
);
283 inline void SetEditorFrame(wxFrame
*fr
) { m_editorFrame
= fr
; }
284 inline void SetEditorToolBar(EditorToolBar
*tb
) { m_editorToolBar
= tb
; }
285 inline wxFrame
*GetEditorFrame() const { return m_editorFrame
; }
286 inline wxResourceEditorProjectTree
*GetEditorResourceTree() const { return m_editorResourceTree
; }
287 inline wxResourceEditorControlList
*GetEditorControlList() const { return m_editorControlList
; }
288 inline wxList
& GetSelections() { return m_selections
; }
289 inline wxMenu
*GetPopupMenu() const { return m_popupMenu
; }
291 inline wxHelpController
*GetHelpController() const { return m_helpController
; }
294 inline void Modify(bool mod
= TRUE
) { m_modified
= mod
; }
295 inline bool Modified() const { return m_modified
; }
297 inline wxResourceTable
& GetResourceTable() { return m_resourceTable
; }
298 inline wxHashTable
& GetResourceAssociations() { return m_resourceAssociations
; }
300 inline wxString
GetCurrentFilename() const { return m_currentFilename
; }
301 static wxResourceManager
* GetCurrentResourceManager() { return sm_currentResourceManager
; }
303 inline void SetSymbolFilename(const wxString
& s
) { m_symbolFilename
= s
; }
304 inline wxString
GetSymbolFilename() const { return m_symbolFilename
; }
306 inline wxRect
& GetPropertyWindowSize() { return m_propertyWindowSize
; }
307 inline wxRect
& GetResourceEditorWindowSize() { return m_resourceEditorWindowSize
; }
309 wxResourceSymbolTable
& GetSymbolTable() { return m_symbolTable
; }
311 // Generate a window id and a first stab at a name
312 int GenerateWindowId(const wxString
& prefix
, wxString
& idName
) ;
317 wxHelpController
* m_helpController
;
319 wxResourceTableWithSaving m_resourceTable
;
320 wxFrame
* m_editorFrame
;
321 wxResourceEditorScrolledWindow
* m_editorPanel
;
323 wxResourceEditorProjectTree
* m_editorResourceTree
;
324 wxResourceEditorControlList
* m_editorControlList
;
325 EditorToolBar
* m_editorToolBar
;
327 int m_symbolIdCounter
; // For generating window ids
329 wxHashTable m_resourceAssociations
;
331 wxString m_currentFilename
;
332 wxBitmap
* m_bitmapImage
; // Default for static bitmaps/buttons
334 wxImageList m_imageList
;
335 long m_rootDialogItem
; // Root of dialog hierarchy in tree (unused)
337 // Options to be saved/restored
338 wxString m_optionsResourceFilename
; // e.g. dialoged.ini, .dialogrc
339 wxRect m_propertyWindowSize
;
340 wxRect m_resourceEditorWindowSize
;
341 static wxResourceManager
* sm_currentResourceManager
;
343 // Symbol table with identifiers for controls
344 wxResourceSymbolTable m_symbolTable
;
345 // Filename for include file, e.g. resource.h
346 wxString m_symbolFilename
;
350 class wxResourceEditorFrame
: public wxFrame
353 DECLARE_CLASS(wxResourceEditorFrame
)
355 wxResourceManager
*manager
;
356 wxResourceEditorFrame(wxResourceManager
*resMan
, wxFrame
*parent
, const wxString
& title
,
357 const wxPoint
& pos
= wxDefaultPosition
, const wxSize
& size
= wxSize(600, 400),
358 long style
= wxDEFAULT_FRAME_STYLE
, const wxString
& name
= "frame");
359 ~wxResourceEditorFrame();
361 void OnCloseWindow(wxCloseEvent
& event
);
363 void OnNew(wxCommandEvent
& event
);
364 void OnOpen(wxCommandEvent
& event
);
365 void OnNewDialog(wxCommandEvent
& event
);
366 void OnClear(wxCommandEvent
& event
);
367 void OnSave(wxCommandEvent
& event
);
368 void OnSaveAs(wxCommandEvent
& event
);
369 void OnExit(wxCommandEvent
& event
);
370 void OnAbout(wxCommandEvent
& event
);
371 void OnContents(wxCommandEvent
& event
);
372 void OnDeleteSelection(wxCommandEvent
& event
);
373 void OnRecreateSelection(wxCommandEvent
& event
);
374 void OnTest(wxCommandEvent
& event
);
376 DECLARE_EVENT_TABLE()
379 class wxResourceEditorScrolledWindow
: public wxScrolledWindow
382 wxResourceEditorScrolledWindow(wxWindow
*parent
, const wxPoint
& pos
= wxDefaultPosition
, const wxSize
& size
= wxDefaultSize
,
384 ~wxResourceEditorScrolledWindow();
386 void OnPaint(wxPaintEvent
& event
);
388 void DrawTitle(wxDC
& dc
);
391 inline int GetMarginX() { return m_marginX
; }
392 inline int GetMarginY() { return m_marginY
; }
395 wxWindow
* m_childWindow
;
397 int m_marginX
, m_marginY
;
399 DECLARE_EVENT_TABLE()
402 #define OBJECT_MENU_EDIT 1
403 #define OBJECT_MENU_DELETE 2
410 class EditorToolBar
: public wxToolBar
413 EditorToolBar(wxFrame
*frame
, const wxPoint
& pos
= wxPoint(0, 0), const wxSize
& size
= wxSize(0, 0),
414 long style
= wxTB_HORIZONTAL
);
415 bool OnLeftClick(int toolIndex
, bool toggled
);
416 void OnMouseEnter(int toolIndex
);
418 DECLARE_EVENT_TABLE()
422 #define TOOLBAR_LOAD_FILE 1
423 #define TOOLBAR_SAVE_FILE 2
424 #define TOOLBAR_NEW 3
425 #define TOOLBAR_TREE 5
426 #define TOOLBAR_HELP 6
429 #define TOOLBAR_FORMAT_HORIZ 10
430 #define TOOLBAR_FORMAT_HORIZ_LEFT_ALIGN 11
431 #define TOOLBAR_FORMAT_HORIZ_RIGHT_ALIGN 12
432 #define TOOLBAR_FORMAT_VERT 13
433 #define TOOLBAR_FORMAT_VERT_TOP_ALIGN 14
434 #define TOOLBAR_FORMAT_VERT_BOT_ALIGN 15
436 #define TOOLBAR_TO_FRONT 16
437 #define TOOLBAR_TO_BACK 17
438 #define TOOLBAR_COPY_SIZE 18
441 * this class is used to store data associated with a tree item
443 class wxResourceTreeData
: public wxTreeItemData
446 wxResourceTreeData(wxItemResource
*resource
) { m_resource
= resource
; }
448 wxItemResource
*GetResource() const { return m_resource
; }
451 wxItemResource
*m_resource
;