1 /*
2 * This file is part of wxSmith plugin for Code::Blocks Studio
3 * Copyright (C) 2006-2007  Bartlomiej Swiecki
4 *
5 * wxSmith is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * wxSmith is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with wxSmith. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * $Revision: 11384 $
19 * $Id: wxsitemresdata.h 11384 2018-04-29 15:37:23Z fuscated $
20 * $HeadURL: svn://svn.code.sf.net/p/codeblocks/code/branches/release-20.xx/src/plugins/contrib/wxSmith/wxwidgets/wxsitemresdata.h $
21 */
22 
23 #ifndef WXSITEMRESDATA_H
24 #define WXSITEMRESDATA_H
25 
26 #include "wxsitem.h"
27 #include "wxsparent.h"
28 #include "wxsitemres.h"
29 #include "wxscorrector.h"
30 #include "wxsitemundobuffer.h"
31 #include "wxsitemresdataobject.h"
32 #include "../wxsresourcetree.h"
33 
34 class wxsTool;
35 class wxsItemEditor;
36 
37 /** \brief Class holding data for item resources and operating on it */
38 class wxsItemResData
39 {
40     public:
41 
42         /** \brief Ctor
43          *
44          * Constructor tries to load resource. Depending on what
45          * parameters are empty strings, given type of file is assumed.
46          *  - If Wxs, Src and Hdr file names are empty, it's only Xrc file.
47          *  - If Xrc is empty but no other, it's resource not using Xrc file
48          *  - If all file names are not empty, it's resource using Xrc file
49          *  - Other combinations are invalid.
50          *
51          * Parameters passed to constructor are GLOBAL paths (opposite to
52          * wxsItemRes where names are relative to .cbp's directory).
53          */
54         wxsItemResData(
55             const wxString& WxsFileName,
56             const wxString& SrcFileName,
57             const wxString& HdrFileName,
58             const wxString& XrcFileName,
59             const wxString& ClassName,
60             const wxString& ClassType,
61             wxsCodingLang   Language,
62             bool UseForwardDeclarations,
63             bool WithTranslation,
64             wxsResourceItemId TreeId,
65             wxsItemEditor*  Editor,
66             wxsItemResFunctions* Functions
67             );
68 
69         /** \brief Dctor
70          *
71          * \note When wxsItemResData is deleted and the
72          *       data is in modified state (was not saved after modification),
73          *       all changes are lost. Please call Save() to avoid this.
74          */
75         virtual ~wxsItemResData();
76 
77         /** \brief Loading resource
78          *
79          * This function (Re)loads resource from
80          * files. Current resource content is moved into
81          * undo buffer and resource data is replaced by the one
82          * loaded from file.
83          */
84         bool Load();
85 
86         /** \brief Saving resource
87          *
88          * This function saves resource to wxs file
89          * (note that Src / Hdr / Xrc files are not saved
90          * because they're updated after each resource change)
91          */
92         bool Save();
93 
94         /** \brief Checking if resource was loaded properly
95          *
96          * This function may be used to check if resource was properly
97          * loaded. It may be especially usual after checking if
98          * constructor has loaded all data properly.
99          */
IsOk()100         inline bool IsOk() { return m_IsOK; }
101 
102         /** \brief Function starting change of resource data
103          *
104          * This function Notifies that resource is going to change.
105          * It locks data from other changes. Each resource
106          * change must be finished with call to EndChange function.
107          * Between BeginChange and EndChange call there should not
108          * be any call to gui item, so do not jump out of event
109          * function before EndChange is called.
110          *
111          * Change of resource is any operation made on any wxsItem
112          * class inside the resource (including the root class).
113          */
114         void BeginChange();
115 
116         /** \brief Function ending change of resource data
117          *
118          * This function must be paired with BeginChange() call.
119          * it notifies that change of resource has been finished
120          * and that it's good time to update all data on the screen
121          * ans store new undo buffer entry.
122          */
123         void EndChange();
124 
125         /// Causes the Quick properties to be rebuild when EndChange is called.
126         /// This is needed to prevent accessing freed memory.
127         void MarkExtraDataChanged();
128 
129         /** \brief Checking if item has modified state */
GetModified()130         inline bool GetModified() { return m_Undo.IsModified(); }
131 
132         /** \brief Getting root item
133          *  \return pointer to item on success, 0 when data wasn't initialized properly
134          */
GetRootItem()135         inline wxsItem* GetRootItem() { return m_RootItem; }
136 
137         /** \brief Getting main item of selection */
GetRootSelection()138         inline wxsItem* GetRootSelection() { return m_RootSelection; }
139 
140         /** \brief Getting properties filter based on current edit  mode */
GetPropertiesFilter()141         inline int GetPropertiesFilter() { return m_PropertiesFilter; }
142 
143         /** \brief Getting name of wxs file (global path) */
GetWxsFileName()144         inline const wxString& GetWxsFileName() { return m_WxsFileName; }
145 
146         /** \brief Getting name of source file (global path) */
GetSrcFileName()147         inline const wxString& GetSrcFileName() { return m_SrcFileName; }
148 
149         /** \brief Getting name of header file (global path) */
GetHdrFileName()150         inline const wxString& GetHdrFileName() { return m_HdrFileName; }
151 
152         /** \brief Getting name of XRC file (global path) */
GetXrcFileName()153         inline const wxString& GetXrcFileName() { return m_XrcFileName; }
154 
155         /** \brief Getting name of class of edited resource */
GetClassName()156         inline const wxString& GetClassName() { return m_ClassName; }
157 
158         /** \brief Getting name class used as base for this resource (like wxDialog) */
GetClassType()159         inline const wxString& GetClassType() { return m_ClassType; }
160 
161         /** \brief Getting state of internationalize, this allows for translation */
GetTranslation()162         inline bool GetTranslation() { return m_Translation; }
163 
164         /** \brief Getting language used in resource */
GetLanguage()165         inline wxsCodingLang GetLanguage() { return m_Language; }
166 
167         /** \brief Searching for tree id in main resource tree for given item */
GetTreeId(wxsResourceItemId & Id,wxsItem * Item)168         inline bool GetTreeId(wxsResourceItemId& Id,wxsItem* Item) { return FindId(Id,Item); }
169 
170         /** \brief Showing popup menu from given item */
GetEditor()171         inline wxsItemEditor* GetEditor() { return m_Editor; }
172 
173         /* ************************ */
174         /*  Undo buffer operations  */
175         /* ************************ */
176 
177         /** \brief Checking if can Undo */
CanUndo()178         inline bool CanUndo() { return m_Undo.CanUndo(); }
179 
180         /** \brief Checking if can Redo */
CanRedo()181         inline bool CanRedo() { return m_Undo.CanRedo(); }
182 
183         /** \brief Undoing */
Undo()184         inline void Undo() { SetXmlData(m_Undo.Undo()); }
185 
186         /** \brief Redoing */
Redo()187         inline void Redo() { SetXmlData(m_Undo.Redo()); }
188 
189         /** \brief Checking if current content is read only */
IsReadOnly()190         inline bool IsReadOnly() { return m_ReadOnly; }
191 
192         /* ********************** */
193         /*  Clipboard operations  */
194         /* ********************** */
195 
196         /** \brief Checking if we can paste current clipboard content */
197         bool CanPaste();
198 
199         /** \brief Cutting current selection to clipboard */
200         void Cut();
201 
202         /** \brief Copying current selection to clipboard */
203         void Copy();
204 
205         /** \brief Pasting components from clipboard
206          *  \param Parent parent for new items
207          *  \param Position initial position for new items
208          */
209         void Paste(wxsParent* Parent,int Position);
210 
211 
212         /* ********************** */
213         /*  Selection operations  */
214         /* ********************** */
215 
216         /** \brief Checking of there's any selection */
217         bool AnySelected();
218 
219         /** \brief Selecting one item */
220         bool SelectItem(wxsItem* Item,bool UnselectOther);
221 
222         /** \brief Getting last selected item or 0 if there's no valid selection */
GetLastSelection()223         inline wxsItem* GetLastSelection() { return m_RootSelection; }
224 
225         /* ******************* */
226         /*  Operating on data  */
227         /* ******************* */
228 
229         /** \brief Adding new item
230          *
231          * This function tries to add new item into
232          * given position. If it's possible, new item
233          * is added and true is returned. If it's
234          * impossible, new item is deleted internally
235          * and function returns false.
236          * \note To add tool item use InsertNewTool
237          * \param New new item
238          * \param Parent item which will become parent of New
239          * \param Position position inside Parent (if <0 or  out of range,
240          *        appending New at the end of Parent's children)
241          */
242         bool InsertNew(wxsItem* New,wxsParent* Parent,int Position);
243 
244         /** \brief Adding new tool
245          *
246          * This function adds new tool into this resource.
247          * Since tools require special treatment, they
248          * need separate function.
249          * \param Tool new tool
250          * \return true on success, false otherwise
251          */
252         bool InsertNewTool(wxsTool* Tool);
253 
254         /** \brief Deleting all selected items */
255         void DeleteSelected();
256 
257         /** \brief Getting number of tools */
GetToolsCount()258         inline int GetToolsCount() { return (int)m_Tools.Count(); }
259 
260         /** \brief Getting tool at given index */
GetTool(int Index)261         inline wxsTool* GetTool(int Index) { return ((Index>=0)&&(Index<GetToolsCount())) ? m_Tools[Index] : 0; }
262 
263         /* ******************* */
264         /*  Preview functions  */
265         /* ******************* */
266 
267         /** \brief Checking if there's preview already */
IsPreview()268         inline bool IsPreview() { return m_Preview!=0; }
269 
270         /** \brief Showing preview of current resource content */
271         bool ShowPreview();
272 
273         /** \brief Closing window with current resource content */
274         bool HidePreview();
275 
276         /** \brief Function notifying that preview has been closed externally */
NotifyPreviewClosed()277         inline void NotifyPreviewClosed() { m_Preview = 0; }
278 
279         /* *********************** */
280         /*  Notification handlers  */
281         /* *********************** */
282 
283         /** \brief Notification of change of data
284          *
285          * This function is called from wxsItem objects
286          * notifying about change of such item.
287          */
288         void NotifyChange(wxsItem* Changed);
289 
290     private:
291 
292         WX_DECLARE_STRING_HASH_MAP(TiXmlElement*,IdToXmlMapT);
293         WX_DECLARE_HASH_MAP(wxsItem*,wxsResourceItemId,wxPointerHash,wxPointerEqual,ItemToIdMapT);
294         WX_DEFINE_ARRAY(wxsTool*,ToolArrayT);
295 
296         /** \brief Generating string with xml data for this item
297          *  \note used when creating undo entries
298          */
299         wxString GetXmlData();
300 
301         /** \brief Restoring resource data from string with xml data */
302         bool SetXmlData(const wxString& XmlData);
303 
304         /** \brief Rebuilding all files kept up-to-date after change in resource */
305         void RebuildFiles();
306 
307         /** \brief Rebuilding sources for this resource */
308         void RebuildSourceCode();
309 
310         /** \brief Rebuilding XRC file managed by this resource */
311         bool RebuildXrcFile();
312 
313         // Various loading functions
314         bool SilentLoad();
315         bool LoadInFileMode();
316         bool LoadInMixedMode();
317         bool LoadInSourceMode();
318         void UpdateExtraDataReq(wxsItem* Item,IdToXmlMapT& Map);
319         void RecreateRootItem();
320         void LoadToolsReq(TiXmlElement* Node,bool IsXRC,bool IsExtra);
321 
322         // Various saving function
323         bool SaveInFileMode();
324         bool SaveInMixedMode();
325         bool SaveInSourceMode();
326         void SaveExtraDataReq(wxsItem* Item,TiXmlElement* Node);
327 
328         // Some misc functions
StoreUndo()329         inline void StoreUndo() { m_Undo.StoreChange(GetXmlData()); }
330         bool ValidateRootSelection();
331         bool ValidateRootSelectionReq(wxsItem* Item,wxsItem*& NewSelection);
332         void CopyReq(wxsItem* Item,wxsItemResDataObject* Data);
333         bool AnySelectedReq(wxsItem* Item);
334         void StoreTreeExpandState();
335         void StoreTreeExpandStateReq(wxsItem* Item);
336         void RestoreTreeExpandAndSelectionState();
337         void RestoreTreeExpandAndSelectionStateReq(wxsItem* Item);
338         void DeleteSelectedReq(wxsItem* Item);
339         void RebuildTree();
340         void StoreTreeIds();
341         void StoreTreeIdsReq(wxsItem* Item);
342         bool FindId(wxsResourceItemId& Id,wxsItem* Item);
343         void DetectAutoCodeBlocks();
344 
345         // Functions used by RebuildSourceCode
346         wxString DeclarationsCode(wxsCoderContext* Ctx);
347         wxString IdentifiersCode(wxsCoderContext* Ctx);
348         wxString InitializeCode(wxsCoderContext* Ctx);
349         wxString IdInitCode(wxsCoderContext* Ctx);
350         wxString HeadersCode(wxsCoderContext* Ctx);
351         wxString HeadersNoPCHCode(wxsCoderContext* Ctx);
352         wxString HeadersAllCode(wxsCoderContext* Ctx);
353         wxString InternalHeadersCode(wxsCoderContext* Ctx);
354         wxString InternalHeadersNoPCHCode(wxsCoderContext* Ctx);
355         wxString InternalHeadersAllCode(wxsCoderContext* Ctx);
356         wxString XRCLoadingCode();
357 
358         // Wrappers to m_Functions functionality
BuildExactPreview(wxWindow * Parent)359         inline wxWindow* BuildExactPreview(wxWindow* Parent) { return m_Functions->OnBuildExactPreview(Parent,this); }
360 
361         wxString m_WxsFileName;
362         wxString m_SrcFileName;
363         wxString m_HdrFileName;
364         wxString m_XrcFileName;
365         bool     m_Translation; // Generate strings with _() macro instead of _T() macro
366         wxString m_ClassName;
367         wxString m_ClassType;
368         wxsCodingLang m_Language;
369         wxsResourceItemId m_TreeId;
370         wxsResourceItemId m_ToolsId;
371         bool m_ToolsNodeIsExpanded;
372         ItemToIdMapT m_IdMap;
373         wxsItemEditor* m_Editor;
374         wxsItemResFunctions* m_Functions;
375 
376         wxsItem* m_RootItem;
377         wxsItem* m_RootSelection;
378         ToolArrayT m_Tools;
379         long m_PropertiesFilter;
380 
381         wxWindow* m_Preview;
382 
383         wxsItemUndoBuffer m_Undo;
384         wxsCorrector m_Corrector;
385 
386         bool m_IsOK;
387         bool m_IsEventTable;
388         int m_LockCount;
389 
390         bool m_ReadOnly;
391         bool m_ExtraIsInvalid = false;
392 };
393 
394 #endif
395