Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : */
9 :
10 : #ifndef _VCLBUILDER_HXX
11 : #define _VCLBUILDER_HXX
12 :
13 : #include <typeinfo>
14 : #include <vcl/dllapi.h>
15 : #include <vcl/window.hxx>
16 : #include <xmlreader/xmlreader.hxx>
17 : #include <map>
18 : #include <set>
19 : #include <stack>
20 : #include <vector>
21 :
22 : class ListBox;
23 : class NumericFormatter;
24 : class PopupMenu;
25 : class ScrollBar;
26 : class VclMultiLineEdit;
27 :
28 : class VCL_DLLPUBLIC VclBuilder
29 : {
30 : public:
31 : typedef std::map<OString, OString> stringmap;
32 : typedef Window* (*customMakeWidget)(Window *pParent, stringmap &rVec);
33 : private:
34 :
35 : struct PackingData
36 : {
37 : bool m_bVerticalOrient;
38 : sal_Int32 m_nPosition;
39 0 : PackingData(bool bVerticalOrient = false, sal_Int32 nPosition = -1)
40 : : m_bVerticalOrient(bVerticalOrient)
41 0 : , m_nPosition(nPosition)
42 : {
43 0 : }
44 : };
45 :
46 0 : struct WinAndId
47 : {
48 : OString m_sID;
49 : Window *m_pWindow;
50 : PackingData m_aPackingData;
51 0 : WinAndId(const OString &rId, Window *pWindow, bool bVertical)
52 : : m_sID(rId)
53 : , m_pWindow(pWindow)
54 0 : , m_aPackingData(bVertical)
55 : {
56 0 : }
57 : };
58 : std::vector<WinAndId> m_aChildren;
59 :
60 0 : struct MenuAndId
61 : {
62 : OString m_sID;
63 : PopupMenu *m_pMenu;
64 0 : MenuAndId(const OString &rId, PopupMenu *pMenu)
65 : : m_sID(rId)
66 0 : , m_pMenu(pMenu)
67 : {
68 0 : }
69 : };
70 : std::vector<MenuAndId> m_aMenus;
71 :
72 0 : struct StringPair
73 : {
74 : OString m_sID;
75 : OString m_sValue;
76 0 : StringPair(const OString &rId, const OString &rValue)
77 : : m_sID(rId)
78 0 : , m_sValue(rValue)
79 : {
80 0 : }
81 : };
82 :
83 : typedef StringPair RadioButtonGroupMap;
84 : typedef StringPair ButtonImageWidgetMap;
85 : typedef StringPair TextBufferMap;
86 : typedef StringPair WidgetAdjustmentMap;
87 : typedef StringPair ButtonMenuMap;
88 :
89 0 : struct ComboBoxModelMap
90 : {
91 : OString m_sID;
92 : OString m_sValue;
93 : sal_Int32 m_nActiveId;
94 0 : ComboBoxModelMap(const OString &rId, const OString &rValue, sal_Int32 nActiveId)
95 : : m_sID(rId)
96 : , m_sValue(rValue)
97 0 : , m_nActiveId(nActiveId)
98 : {
99 0 : }
100 : };
101 :
102 0 : struct ListStore
103 : {
104 : typedef std::vector<OString> row;
105 : std::vector<row> m_aEntries;
106 : };
107 : const ListStore* get_model_by_name(OString sID) const;
108 : static void mungeModel(ListBox &rTarget, const ListStore &rStore, sal_uInt16 nActiveId);
109 :
110 : typedef stringmap TextBuffer;
111 : const TextBuffer* get_buffer_by_name(OString sID) const;
112 : static void mungeTextBuffer(VclMultiLineEdit &rTarget, const TextBuffer &rTextBuffer);
113 :
114 : typedef stringmap Adjustment;
115 : const Adjustment *get_adjustment_by_name(OString sID) const;
116 : static void mungeSpinAdjustment(NumericFormatter &rTarget, const Adjustment &rAdjustment);
117 : static void mungeScrollAdjustment(ScrollBar &rTarget, const Adjustment &rAdjustment);
118 :
119 : typedef std::map<OString, OString> WidgetTranslations;
120 : typedef std::map<OString, WidgetTranslations> Translations;
121 :
122 : typedef std::map<OString, OString> StockMap;
123 :
124 0 : struct ParserState
125 : {
126 : std::vector<RadioButtonGroupMap> m_aGroupMaps;
127 :
128 : std::vector<ComboBoxModelMap> m_aModelMaps;
129 : std::map<OString, ListStore> m_aModels;
130 :
131 : std::vector<TextBufferMap> m_aTextBufferMaps;
132 : std::map<OString, TextBuffer> m_aTextBuffers;
133 :
134 : std::vector<WidgetAdjustmentMap> m_aSpinAdjustmentMaps;
135 : std::vector<WidgetAdjustmentMap> m_aScrollAdjustmentMaps;
136 : std::map<OString, Adjustment> m_aAdjustments;
137 :
138 : std::vector<ButtonImageWidgetMap> m_aButtonImageWidgetMaps;
139 : StockMap m_aStockMap;
140 :
141 : std::vector<ButtonMenuMap> m_aButtonMenuMaps;
142 :
143 : Translations m_aTranslations;
144 :
145 : std::map<Window*, Window*> m_aRedundantParentWidgets;
146 : };
147 :
148 : void loadTranslations(const com::sun::star::lang::Locale &rLocale, const OUString &rUri);
149 : OString getTranslation(const OString &rId, const OString &rProperty) const;
150 :
151 : OString m_sID;
152 : OString m_sHelpRoot;
153 : OString m_sProductName;
154 : Window *m_pParent;
155 : bool m_bToplevelHasDeferredInit;
156 : bool m_bToplevelParentFound;
157 : ParserState *m_pParserState;
158 :
159 : Window *get_by_name(OString sID);
160 : void delete_by_name(OString sID);
161 :
162 : PopupMenu *get_menu_by_name(OString sID);
163 :
164 : class sortIntoBestTabTraversalOrder
165 : : public std::binary_function<const Window*, const Window*, bool>
166 : {
167 : VclBuilder *m_pBuilder;
168 : public:
169 0 : sortIntoBestTabTraversalOrder(VclBuilder *pBuilder)
170 0 : : m_pBuilder(pBuilder)
171 : {
172 0 : }
173 : bool operator()(const Window *pA, const Window *pB) const;
174 : };
175 : public:
176 : VclBuilder(Window *pParent, OUString sUIRootDir, OUString sUIFile, OString sID = OString());
177 : ~VclBuilder();
178 : Window *get_widget_root();
179 : //sID must exist and be of type T
180 0 : template <typename T> T* get(T*& ret, OString sID)
181 : {
182 0 : Window *w = get_by_name(sID);
183 : SAL_WARN_IF(!w, "vcl.layout", "widget \"" << sID.getStr() << "\" not found in .ui");
184 : SAL_WARN_IF(!dynamic_cast<T*>(w),
185 : "vcl.layout", ".ui widget \"" << sID.getStr() << "\" needs to correspond to vcl type " << typeid(T).name());
186 : assert(w && dynamic_cast<T*>(w));
187 0 : ret = static_cast<T*>(w);
188 0 : return ret;
189 : }
190 : //sID may not exist, but must be of type T if it does
191 0 : template <typename T /*=Window if we had c++11*/> T* get(OString sID)
192 : {
193 0 : Window *w = get_by_name(sID);
194 : SAL_WARN_IF(w && !dynamic_cast<T*>(w),
195 : "vcl.layout", ".ui widget \"" << sID.getStr() << "\" needs to correspond to vcl type " << typeid(T).name());
196 : assert(!w || dynamic_cast<T*>(w));
197 0 : return static_cast<T*>(w);
198 : }
199 : OString get_by_window(const Window *pWindow) const;
200 : void delete_by_window(const Window *pWindow);
201 :
202 : //Convert _ gtk markup to ~ vcl markup
203 : static OString convertMnemonicMarkup(const OString &rIn);
204 :
205 : private:
206 : Window *insertObject(Window *pParent, const OString &rClass, const OString &rID,
207 : stringmap &rProps, stringmap &rPangoAttributes, std::vector<OString> &rItems);
208 :
209 : Window *makeObject(Window *pParent, const OString &rClass, const OString &rID,
210 : stringmap &rVec, const std::vector<OString> &rItems);
211 :
212 : bool extractGroup(const OString &id, stringmap &rVec);
213 : bool extractModel(const OString &id, stringmap &rVec);
214 : bool extractBuffer(const OString &id, stringmap &rVec);
215 : bool extractSpinAdjustment(const OString &id, stringmap &rVec);
216 : bool extractScrollAdjustment(const OString &id, stringmap &rVec);
217 : bool extractImage(const OString &id, stringmap &rMap);
218 : bool extractStock(const OString &id, stringmap &rMap);
219 :
220 : void handleTranslations(xmlreader::XmlReader &reader);
221 :
222 : void handleChild(Window *pParent, xmlreader::XmlReader &reader);
223 : Window* handleObject(Window *pParent, xmlreader::XmlReader &reader);
224 : void handlePacking(Window *pCurrent, xmlreader::XmlReader &reader);
225 : void applyPackingProperty(Window *pCurrent, xmlreader::XmlReader &reader);
226 : void collectProperty(xmlreader::XmlReader &reader, const OString &rID, stringmap &rVec);
227 : void collectPangoAttribute(xmlreader::XmlReader &reader, stringmap &rMap);
228 : void collectAccelerator(xmlreader::XmlReader &reader, stringmap &rMap);
229 :
230 : void insertMenuObject(PopupMenu *pParent, const OString &rClass, const OString &rID,
231 : stringmap &rProps, stringmap &rAccels);
232 : void handleMenuChild(PopupMenu *pParent, xmlreader::XmlReader &reader);
233 : void handleMenuObject(PopupMenu *pParent, xmlreader::XmlReader &reader);
234 :
235 : void handleListStore(xmlreader::XmlReader &reader, const OString &rID);
236 : void handleRow(xmlreader::XmlReader &reader, const OString &rID, sal_Int32 nRowIndex);
237 : void handleAdjustment(const OString &rID, stringmap &rProperties);
238 : void handleTextBuffer(const OString &rID, stringmap &rProperties);
239 : void handleTabChild(Window *pParent, xmlreader::XmlReader &reader);
240 : void handleMenu(xmlreader::XmlReader &reader, const OString &rID);
241 : std::vector<OString> handleItems(xmlreader::XmlReader &reader, const OString &rID);
242 :
243 : PackingData get_window_packing_data(const Window *pWindow) const;
244 : void set_window_packing_position(const Window *pWindow, sal_Int32 nPosition);
245 :
246 : //Helpers to retrofit all the existing code to the builder
247 : static void reorderWithinParent(Window &rWindow, sal_uInt16 nNewPosition);
248 : };
249 :
250 :
251 : //helper baseclass to ease retro fitting dialogs/tabpages that load a resource
252 : //to load a .ui file instead
253 : //
254 : //vcl requires the Window Children of a Parent Window to be destroyed before
255 : //the Parent Window. VclBuilderContainer owns the VclBuilder which owns the
256 : //Children Window. So the VclBuilderContainer dtor must be called before
257 : //the Parent Window dtor.
258 : //
259 : //i.e. class Dialog : public SystemWindow, public VclBuilderContainer
260 : //not class Dialog : public VclBuilderContainer, public SystemWindow
261 :
262 : class VCL_DLLPUBLIC VclBuilderContainer
263 : {
264 : protected:
265 : VclBuilder *m_pUIBuilder;
266 : public:
267 : VclBuilderContainer();
268 : virtual ~VclBuilderContainer();
269 : static OUString getUIRootDir();
270 0 : template <typename T> T* get(T*& ret, OString sID)
271 : {
272 0 : return m_pUIBuilder->get<T>(ret, sID);
273 : }
274 0 : template <typename T /*=Window if we had c++11*/> T* get(OString sID)
275 : {
276 0 : return m_pUIBuilder->get<T>(sID);
277 : }
278 : };
279 :
280 : /*
281 : * @return true if rValue is "True", "true", "1", etc.
282 : */
283 : bool VCL_DLLPUBLIC toBool(const OString &rValue);
284 :
285 : #endif
286 :
287 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|