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"
32 #define RESED_DELETE 1
33 #define RESED_RECREATE 3
35 #define RESED_NEW_DIALOG 5
36 #define RESED_NEW_PANEL 6
39 #define RESED_CONTENTS 20
41 #define IDC_TREECTRL 100
42 #define IDC_LISTCTRL 101
44 // For control list ('palette')
45 #define RESED_POINTER 0
46 #define RESED_BUTTON 1
47 #define RESED_BMPBUTTON 2
48 #define RESED_STATICTEXT 3
49 #define RESED_STATICBMP 4
50 #define RESED_STATICBOX 5
51 #define RESED_TEXTCTRL_SINGLE 6
52 #define RESED_TEXTCTRL_MULTIPLE 7
53 #define RESED_LISTBOX 8
54 #define RESED_CHOICE 9
55 #define RESED_COMBOBOX 10
56 #define RESED_CHECKBOX 11
57 #define RESED_SLIDER 12
58 #define RESED_GAUGE 13
59 #define RESED_RADIOBOX 14
60 #define RESED_RADIOBUTTON 15
61 #define RESED_SCROLLBAR 16
62 #define RESED_TREECTRL 17
63 #define RESED_LISTCTRL 18
64 #define RESED_SPINBUTTON 19
67 * Controls loading, saving, user interface of resource editor(s).
70 class wxResourceEditorFrame
;
72 class wxWindowPropertyInfo
;
73 class wxResourceEditorProjectTree
;
74 class wxResourceEditorControlList
;
77 #define wxHelpController wxWinHelpController
79 #define wxHelpController wxXLPHelpController;
82 class wxHelpController
;
85 * The resourceTable contains a list of wxItemResources (which each may
86 * have further children, defining e.g. a dialog box with controls).
88 * We need to associate actual windows with each wxItemResource,
89 * instead of the current 'one current window' scheme.
91 * - We create a new dialog, create a wxItemResource,
92 * associate the dialog with wxItemResource via a hash table.
93 * Must be a hash table in case dialog is deleted without
94 * telling the resource manager.
95 * - When we save the resource after editing/closing the dialog,
96 * we check the wxItemResource/wxDialog and children for
97 * consistency (throw away items no longer in the wxDialog,
98 * create any new wxItemResources).
99 * - We save the wxItemResources via the wxPropertyInfo classes,
100 * so devolve the code to the appropriate class.
101 * This involves creating a new temporary wxPropertyInfo for
104 * We currently assume we only create one instance of a window for
105 * each wxItemResource. We will need to relax this when we're editing
111 class wxResourceTableWithSaving
: public wxResourceTable
114 wxResourceTableWithSaving():wxResourceTable()
116 // Add all known window styles
119 virtual bool Save(const wxString
& filename
);
120 virtual bool SaveResource(ostream
& stream
, wxItemResource
*item
);
122 void GeneratePanelStyleString(long windowStyle
, char *buf
);
123 void GenerateDialogStyleString(long windowStyle
, char *buf
);
126 void GenerateRadioBoxStyleString(long windowStyle, char *buf);
127 void GenerateMessageStyleString(long windowStyle, char *buf);
128 void GenerateTextStyleString(long windowStyle, char *buf);
129 void GenerateButtonStyleString(long windowStyle, char *buf);
130 void GenerateCheckBoxStyleString(long windowStyle, char *buf);
131 void GenerateRadioButtonStyleString(long windowStyle, char *buf);
132 void GenerateListBoxStyleString(long windowStyle, char *buf);
133 void GenerateSliderStyleString(long windowStyle, char *buf);
134 void GenerateGroupBoxStyleString(long windowStyle, char *buf);
135 void GenerateGaugeStyleString(long windowStyle, char *buf);
136 void GenerateChoiceStyleString(long windowStyle, char *buf);
137 void GenerateComboBoxStyleString(long windowStyle, char *buf);
138 void GenerateScrollBarStyleString(long windowStyle, char *buf);
141 void GenerateControlStyleString(const wxString
& windowClass
, long windowStyle
, char *buf
);
144 bool GenerateStyle(char *buf, long windowStyle, long flag, char *strStyle);
147 void OutputFont(ostream
& stream
, wxFont
*font
);
148 wxControl
*CreateItem(wxPanel
*panel
, wxItemResource
*childResource
);
151 wxWindowStyleTable m_styleTable
;
154 class wxResourceEditorScrolledWindow
;
156 class wxResourceManager
: public wxObject
158 friend class wxResourceEditorFrame
;
162 ~wxResourceManager();
166 // Initializes the resource manager
169 // Load/save window size etc.
173 // Show or hide the resource editor frame, which displays a list
174 // of resources with ability to edit them.
175 virtual bool ShowResourceEditor(bool show
, wxWindow
*parent
= NULL
, const char *title
= "wxWindows Dialog Editor");
178 virtual bool SaveAs();
179 virtual bool Save(const wxString
& filename
);
180 virtual bool Load(const wxString
& filename
);
181 virtual bool Clear(bool deleteWindows
= TRUE
, bool force
= TRUE
);
182 virtual void SetFrameTitle(const wxString
& filename
);
183 virtual void ClearCurrentDialog();
184 virtual bool New(bool loadFromFile
= TRUE
, const wxString
& filename
= "");
185 virtual bool SaveIfModified();
186 virtual void AlignItems(int flag
);
187 virtual void CopySize();
188 virtual void ToBackOrFront(bool toBack
);
189 virtual wxWindow
*FindParentOfSelection();
191 virtual wxFrame
*OnCreateEditorFrame(const char *title
);
192 virtual wxMenuBar
*OnCreateEditorMenuBar(wxFrame
*parent
);
193 virtual wxResourceEditorScrolledWindow
*OnCreateEditorPanel(wxFrame
*parent
);
194 virtual wxToolBarBase
*OnCreateToolBar(wxFrame
*parent
);
196 // Create a window information object for the give window
197 wxWindowPropertyInfo
* CreatePropertyInfoForWindow(wxWindow
*win
);
198 // Edit the given window
199 void EditWindow(wxWindow
*win
);
201 virtual void UpdateResourceList();
202 virtual void AddItemsRecursively(long parent
, wxItemResource
*resource
);
203 virtual bool EditSelectedResource();
204 virtual bool Edit(wxItemResource
*res
);
205 virtual bool CreateNewPanel();
206 virtual bool CreatePanelItem(wxItemResource
*panelResource
, wxPanel
*panel
, char *itemType
, int x
= 10, int y
= 10, bool isBitmap
= FALSE
);
207 virtual bool DeleteSelection();
208 virtual bool TestCurrentDialog(wxWindow
* parent
);
210 // Saves the window info into the resource, and deletes the
211 // handler. Doesn't actually disassociate the window from
212 // the resources. Replaces OnClose.
213 virtual bool SaveInfoAndDeleteHandler(wxWindow
* win
);
215 // Destroys the window. If this is the 'current' panel, NULLs the
217 virtual bool DeleteWindow(wxWindow
* win
);
218 virtual bool DeleteResource(wxItemResource
*res
);
219 virtual bool DeleteResource(wxWindow
*win
);
221 // Add bitmap resource if there isn't already one with this filename.
222 virtual char *AddBitmapResource(char *filename
);
224 // Delete the bitmap resource if it isn't being used by another resource.
225 virtual void PossiblyDeleteBitmapResource(char *resourceName
);
227 // Helper function for above
228 virtual bool IsBitmapResourceUsed(char *resourceName
);
230 wxItemResource
*FindBitmapResourceByFilename(char *filename
);
232 char *FindBitmapFilenameForResource(wxItemResource
*resource
);
234 // Is this window identifier in use?
235 bool IsSymbolUsed(wxItemResource
* thisResource
, wxWindowID id
) ;
237 // Is this window identifier compatible with the given name? (i.e.
238 // does it already exist under a different name)
239 bool IsIdentifierOK(const wxString
& name
, wxWindowID id
);
241 // Change all integer ids that match oldId, to newId.
242 // This is necessary if an id is changed for one resource - all resources
244 void ChangeIds(int oldId
, int newId
);
246 // If any resource ids were missing (or their symbol was missing),
247 // repair them i.e. give them new ids. Returns TRUE if any resource
249 bool RepairResourceIds();
251 // Deletes 'win' and creates a new window from the resource that
252 // was associated with it. E.g. if you can't change properties on the
253 // fly, you'll need to delete the window and create it again.
254 virtual wxWindow
*RecreateWindowFromResource(wxWindow
*win
, wxWindowPropertyInfo
*info
= NULL
);
256 virtual bool RecreateSelection();
258 // Need to search through resource table removing this from
259 // any resource which has this as a parent.
260 virtual bool RemoveResourceFromParent(wxItemResource
*res
);
262 virtual bool EditDialog(wxDialog
*dialog
, wxWindow
*parent
);
264 void AddSelection(wxWindow
*win
);
265 void RemoveSelection(wxWindow
*win
);
267 virtual void MakeUniqueName(char *prefix
, char *buf
);
269 // (Dis)associate resource<->physical window
270 // Doesn't delete any windows.
271 virtual void AssociateResource(wxItemResource
*resource
, wxWindow
*win
);
272 virtual bool DisassociateResource(wxItemResource
*resource
);
273 virtual bool DisassociateResource(wxWindow
*win
);
274 virtual bool DisassociateWindows();
275 virtual wxItemResource
*FindResourceForWindow(wxWindow
*win
);
276 virtual wxWindow
*FindWindowForResource(wxItemResource
*resource
);
278 virtual bool InstantiateAllResourcesFromWindows();
279 virtual bool InstantiateResourceFromWindow(wxItemResource
*resource
, wxWindow
*window
, bool recurse
= FALSE
);
282 inline void SetEditorFrame(wxFrame
*fr
) { m_editorFrame
= fr
; }
283 inline void SetEditorToolBar(EditorToolBar
*tb
) { m_editorToolBar
= tb
; }
284 inline wxFrame
*GetEditorFrame() const { return m_editorFrame
; }
285 inline wxResourceEditorProjectTree
*GetEditorResourceTree() const { return m_editorResourceTree
; }
286 inline wxResourceEditorControlList
*GetEditorControlList() const { return m_editorControlList
; }
287 inline wxList
& GetSelections() { return m_selections
; }
288 inline wxMenu
*GetPopupMenu() const { return m_popupMenu
; }
289 inline wxHelpController
*GetHelpController() const { return m_helpController
; }
291 inline void Modify(bool mod
= TRUE
) { m_modified
= mod
; }
292 inline bool Modified() const { return m_modified
; }
294 inline wxResourceTable
& GetResourceTable() { return m_resourceTable
; }
295 inline wxHashTable
& GetResourceAssociations() { return m_resourceAssociations
; }
297 inline wxString
GetCurrentFilename() const { return m_currentFilename
; }
298 static wxResourceManager
* GetCurrentResourceManager() { return sm_currentResourceManager
; }
300 inline void SetSymbolFilename(const wxString
& s
) { m_symbolFilename
= s
; }
301 inline wxString
GetSymbolFilename() const { return m_symbolFilename
; }
303 inline wxRect
& GetPropertyWindowSize() { return m_propertyWindowSize
; }
304 inline wxRect
& GetResourceEditorWindowSize() { return m_resourceEditorWindowSize
; }
306 wxResourceSymbolTable
& GetSymbolTable() { return m_symbolTable
; }
308 // Generate a window id and a first stab at a name
309 int GenerateWindowId(const wxString
& prefix
, wxString
& idName
) ;
313 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();
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
);
371 DECLARE_EVENT_TABLE()
374 class wxResourceEditorScrolledWindow
: public wxScrolledWindow
377 wxResourceEditorScrolledWindow(wxWindow
*parent
, const wxPoint
& pos
= wxDefaultPosition
, const wxSize
& size
= wxDefaultSize
,
379 ~wxResourceEditorScrolledWindow();
381 void OnScroll(wxScrollEvent
& event
);
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_EDIT 1
399 #define OBJECT_MENU_DELETE 2
406 class EditorToolBar
: public wxToolBar
409 EditorToolBar(wxFrame
*frame
, const wxPoint
& pos
= wxPoint(0, 0), const wxSize
& size
= wxSize(0, 0),
410 long style
= wxTB_HORIZONTAL
);
411 bool OnLeftClick(int toolIndex
, bool toggled
);
412 void OnMouseEnter(int toolIndex
);
413 void OnPaint(wxPaintEvent
& event
);
415 DECLARE_EVENT_TABLE()
419 #define TOOLBAR_LOAD_FILE 1
420 #define TOOLBAR_SAVE_FILE 2
421 #define TOOLBAR_NEW 3
422 #define TOOLBAR_TREE 5
423 #define TOOLBAR_HELP 6
426 #define TOOLBAR_FORMAT_HORIZ 10
427 #define TOOLBAR_FORMAT_HORIZ_LEFT_ALIGN 11
428 #define TOOLBAR_FORMAT_HORIZ_RIGHT_ALIGN 12
429 #define TOOLBAR_FORMAT_VERT 13
430 #define TOOLBAR_FORMAT_VERT_TOP_ALIGN 14
431 #define TOOLBAR_FORMAT_VERT_BOT_ALIGN 15
433 #define TOOLBAR_TO_FRONT 16
434 #define TOOLBAR_TO_BACK 17
435 #define TOOLBAR_COPY_SIZE 18