File: xrcconv.h

package info (click to toggle)
wxformbuilder 4.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,908 kB
  • sloc: cpp: 37,318; xml: 6,611; javascript: 1,353; python: 94; sh: 62; makefile: 62
file content (238 lines) | stat: -rw-r--r-- 9,215 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
///////////////////////////////////////////////////////////////////////////////
//
// wxFormBuilder - A Visual Dialog Editor for wxWidgets.
// Copyright (C) 2005 José Antonio Hurtado
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// Written by
//   José Antonio Hurtado - joseantonio.hurtado@gmail.com
//   Juan Antonio Ortega  - jortegalalmolda@gmail.com
//
///////////////////////////////////////////////////////////////////////////////

#ifndef SDK_PLUGIN_INTERFACE_XRCCONV_H
#define SDK_PLUGIN_INTERFACE_XRCCONV_H

#include <optional>

#include "component.h"


namespace tinyxml2
{
class XMLElement;
}


/**
 * @brief Base interface for XRC filter classes
 *
 * This class is only used to define common data types.
 */
class XrcFilter
{
public:
    enum class Type {
        Bool = 0,
        Integer,
        Float,
        String,
        Text,
        Point,
        Size,
        Bitmap,
        Colour,
        Font,
        Option,
        BitList,
        TextList,
    };

protected:
    ~XrcFilter() = default;
};


/**
 * @brief Filter for exporting an object to XRC format
 *
 * This class helps exporting an object to XRC format. It is just needed to setup
 * the properties names "mapping" with their types, the XML element will be
 * created in XRC format.
 *
 * Example:
 *
 * @code
 *
 * ObjectToXrcFilter filter(xrc, GetLibrary(), obj);
 * filter.AddProperty(XrcFilter::Type::Text, "label");
 * filter.AddProperty(XrcFilter::Type::BitList, "style");
 * filter.AddProperty(XrcFilter::Type::Bool, "default");
 *
 * @endcode
 */
class ObjectToXrcFilter : public XrcFilter
{
public:
    /**
     * @brief Construct a new Object To XRC Filter object
     *
     * @param xrcElement Output XRC element.
     *                   This should be an empty XML element of the target XRC structure, located at the correct position for the source object.
     * @param lib Component library to query additional data
     * @param obj Source object
     * @param className Class attribute of the XRC element.
     *                  If std::nullopt, this value is queried from obj. If not empty, the specified value is used. If empty, the attribute will not be added.
     * @param objectName Name attribute of the XRC element.
     *                   If std::nullopt, this value is queried from obj. If not empty, the specified value is used. If empty, the attribute will not be added.
     */
    ObjectToXrcFilter(
        tinyxml2::XMLElement* xrcElement,
        const IComponentLibrary* lib, const IObject* obj,
        std::optional<wxString> className = std::nullopt,
        std::optional<wxString> objectName = std::nullopt
    );

    /**
     * @brief Add a property
     *
     * @param propType Type of the XRC property. The value of the object property is interpreted accordingly.
     * @param objPropName Name of the object property
     * @param xrcPropName Name of the XRC property. If empty, objPropName is used.
     */
    void AddProperty(Type propType, const wxString& objPropName, const wxString& xrcPropName = wxEmptyString);
    /**
     * @brief Add a property with the given value
     *
     * @param xrcPropName Name of the XRC property
     * @param xrcPropValue Value of the XRC property
     * @param xrcFormat If true, escape the value according to the XRC string rules, otherwise pass it unmodified.
     */
    void AddPropertyValue(const wxString& xrcPropName, const wxString& xrcPropValue, bool xrcFormat = false);
    /**
     * @brief Add a property constructed from two source properties.
     *
     * The type of the source properties must be integer, the values get written as a XRC pair.
     *
     * @param objPropName1 Name of the first object property
     * @param objPropName2 Name of the second object property
     * @param xrcPropName Name of the XRC property
     */
    void AddPropertyPair(const wxString& objPropName1, const wxString& objPropName2, const wxString& xrcPropName);

    /**
     * @brief Add standard wxWindow properties
     */
    void AddWindowProperties();

private:
    void SetInteger(tinyxml2::XMLElement* element, int integer) const;
    void SetFloat(tinyxml2::XMLElement* element, double value) const;
    void SetText(tinyxml2::XMLElement* element, const wxString& text, bool xrcFormat = false) const;
    void SetColour(tinyxml2::XMLElement* element, const wxColour& colour) const;
    void SetFont(tinyxml2::XMLElement* element, const wxFontContainer& font) const;
    void SetStringList(tinyxml2::XMLElement* element, const wxArrayString& array, bool xrcFormat = false) const;

private:
    const IComponentLibrary* m_lib;
    const IObject* m_obj;

    tinyxml2::XMLElement* m_xrcElement;
};


/**
 * @brief Filter for exporting a XRC object to XFB format (Xml-FormBuilder)
 *
 * The usage is similar to the ObjectToXrcFilter filter. It is only
 * needed to add the properties with their related types.
 */
class XrcToXfbFilter : public XrcFilter
{
public:
    /**
     * @brief Construct a new XRC To XFB Filter object
     *
     * @param xfbElement Output XFB element.
     *                   This should be an empty XML element of the target XFB structure, located at the correct position for the XRC element.
     * @param lib Component library to query additional data
     * @param xrcElement Source XRC element
     * @param className Class attribute of the XFB element.
     *                  If std::nullopt, this value is queried from xrcElement. If not empty, the specified value is used. If empty, the attribute will not be added.
     * @param objectName Name attribute of the XFB element.
     *                   If std::nullopt, this value is queried from xrcElement. If not empty, the specified value is used. If empty, the attribute will not be added.
     */
    XrcToXfbFilter(
        tinyxml2::XMLElement* xfbElement,
        const IComponentLibrary* lib, const tinyxml2::XMLElement* xrcElement,
        std::optional<wxString> className = std::nullopt,
        std::optional<wxString> objectName = std::nullopt
    );

    /**
     * @brief Add a property
     *
     * @param propType Type of the XFB property. The value of the XRC property is interpreted accordingly.
     * @param xrcPropName Name of the XRC property
     * @param xfbPropName Name of the XFB property. If empty, xrcPropName is used.
     */
    void AddProperty(Type propType, const wxString& xrcPropName, const wxString& xfbPropName = wxEmptyString);
    /**
     * @brief Add a property with the given value
     *
     * @param xfbPropName Name of the XFB property
     * @param xfbPropValue Value of the XFB property
     * @param parseXrcText If true, unescape the value according to the XRC string rules, otherwise pass it unmodified.
     */
    void AddPropertyValue(const wxString& xfbPropName, const wxString& xfbPropValue, bool parseXrcText = false);
    /**
     * @brief Add a property and distribute the values to two XFB properties.
     *
     * The type of the source property must be an pair of integer, the values get written as two XFB integers.
     *
     * @param xrcPropName Name of the XRC property
     * @param xfbPropName1 Name of the first XFB property
     * @param xfbPropName2 Name of the first XFB property
     */
    void AddPropertyPair(const wxString& xrcPropName, const wxString& xfbPropName1, const wxString& xfbPropName2);

    /**
     * @brief Add standard wxWindow properties
     */
    void AddWindowProperties();

private:
    void SetIntegerProperty(tinyxml2::XMLElement* element, const wxString& name) const;
    void SetFloatProperty(tinyxml2::XMLElement* element, const wxString& name) const;
    void SetTextProperty(tinyxml2::XMLElement* element, const wxString& name, bool xrcFormat = false) const;
    void SetBitmapProperty(tinyxml2::XMLElement* element, const wxString& name) const;
    void SetColourProperty(tinyxml2::XMLElement* element, const wxString& name) const;
    void SetFontProperty(tinyxml2::XMLElement* element, const wxString& name) const;
    void SetOptionProperty(tinyxml2::XMLElement* element, const wxString& name) const;
    void SetBitlistProperty(tinyxml2::XMLElement* element, const wxString& name) const;
    void SetStringListProperty(tinyxml2::XMLElement* element, const wxString& name, bool xrcFormat = false) const;

    void AddStyleProperty();
    void AddExtraStyleProperty();

private:
    const IComponentLibrary* m_lib;
    const tinyxml2::XMLElement* m_xrcElement;

    tinyxml2::XMLElement* m_xfbElement;
};

#endif  // SDK_PLUGIN_INTERFACE_XRCCONV_H