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 <osl/module.hxx>
15 : #include <tools/resmgr.hxx> //for poxy ResHookProc typedef
16 : #include <vcl/dllapi.h>
17 : #include <vcl/window.hxx>
18 : #include <xmlreader/xmlreader.hxx>
19 : #include <map>
20 : #include <set>
21 : #include <stack>
22 : #include <vector>
23 : #include <boost/noncopyable.hpp>
24 : #include <boost/ptr_container/ptr_map.hpp>
25 :
26 : #include <com/sun/star/frame/XFrame.hpp>
27 :
28 : class ListBox;
29 : class NumericFormatter;
30 : class PopupMenu;
31 : class ScrollBar;
32 : class DateField;
33 : class TimeField;
34 : class VclMultiLineEdit;
35 :
36 : class VCL_DLLPUBLIC VclBuilder: private boost::noncopyable
37 : {
38 : public:
39 : typedef std::map<OString, OString> stringmap;
40 : typedef Window* (*customMakeWidget)(Window *pParent, stringmap &rVec);
41 : private:
42 : typedef boost::ptr_map<OUString, osl::Module> ModuleMap;
43 : //We store these until the builder is deleted, that way we can use the
44 : //ui-previewer on custom widgets and guarantee the modules they are from
45 : //exist for the duration of the dialog
46 : ModuleMap m_aModuleMap;
47 :
48 : //If the toplevel window has any properties which need to be set on it,
49 : //but the toplevel is the owner of the builder, then its ctor
50 : //has not been completed during the building, so properties for it
51 : //are collected here and need to be set afterwards, e.g. during
52 : //Show or Execute
53 : stringmap m_aDeferredProperties;
54 :
55 : struct PackingData
56 : {
57 : bool m_bVerticalOrient;
58 : sal_Int32 m_nPosition;
59 0 : PackingData(bool bVerticalOrient = false, sal_Int32 nPosition = -1)
60 : : m_bVerticalOrient(bVerticalOrient)
61 0 : , m_nPosition(nPosition)
62 : {
63 0 : }
64 : };
65 :
66 0 : struct WinAndId
67 : {
68 : OString m_sID;
69 : Window *m_pWindow;
70 : short m_nResponseId;
71 : PackingData m_aPackingData;
72 0 : WinAndId(const OString &rId, Window *pWindow, bool bVertical)
73 : : m_sID(rId)
74 : , m_pWindow(pWindow)
75 : , m_nResponseId(RET_CANCEL)
76 0 : , m_aPackingData(bVertical)
77 : {
78 0 : }
79 : };
80 : std::vector<WinAndId> m_aChildren;
81 :
82 0 : struct MenuAndId
83 : {
84 : OString m_sID;
85 : PopupMenu *m_pMenu;
86 0 : MenuAndId(const OString &rId, PopupMenu *pMenu)
87 : : m_sID(rId)
88 0 : , m_pMenu(pMenu)
89 : {
90 0 : }
91 : };
92 : std::vector<MenuAndId> m_aMenus;
93 :
94 0 : struct StringPair
95 : {
96 : OString m_sID;
97 : OString m_sValue;
98 0 : StringPair(const OString &rId, const OString &rValue)
99 : : m_sID(rId)
100 0 : , m_sValue(rValue)
101 : {
102 0 : }
103 : };
104 :
105 : typedef StringPair RadioButtonGroupMap;
106 :
107 0 : struct ButtonImageWidgetMap
108 : {
109 : OString m_sID;
110 : OString m_sValue;
111 : bool m_bRadio;
112 0 : ButtonImageWidgetMap(const OString &rId, const OString &rValue, bool bRadio)
113 : : m_sID(rId)
114 : , m_sValue(rValue)
115 0 : , m_bRadio(bRadio)
116 : {
117 0 : }
118 : };
119 :
120 : typedef StringPair TextBufferMap;
121 : typedef StringPair WidgetAdjustmentMap;
122 : typedef StringPair ButtonMenuMap;
123 : typedef StringPair MnemonicWidgetMap;
124 :
125 0 : struct ComboBoxModelMap
126 : {
127 : OString m_sID;
128 : OString m_sValue;
129 : sal_Int32 m_nActiveId;
130 0 : ComboBoxModelMap(const OString &rId, const OString &rValue, sal_Int32 nActiveId)
131 : : m_sID(rId)
132 : , m_sValue(rValue)
133 0 : , m_nActiveId(nActiveId)
134 : {
135 0 : }
136 : };
137 :
138 0 : struct ListStore
139 : {
140 : typedef std::vector<OString> row;
141 : std::vector<row> m_aEntries;
142 : };
143 : const ListStore* get_model_by_name(OString sID) const;
144 : static void mungeModel(ListBox &rTarget, const ListStore &rStore, sal_uInt16 nActiveId);
145 :
146 : typedef stringmap TextBuffer;
147 : const TextBuffer* get_buffer_by_name(OString sID) const;
148 : static void mungeTextBuffer(VclMultiLineEdit &rTarget, const TextBuffer &rTextBuffer);
149 :
150 : typedef stringmap Adjustment;
151 : const Adjustment *get_adjustment_by_name(OString sID) const;
152 : static void mungeAdjustment(NumericFormatter &rTarget, const Adjustment &rAdjustment);
153 : static void mungeAdjustment(DateField &rTarget, const Adjustment &rAdjustment);
154 : static void mungeAdjustment(TimeField &rTarget, const Adjustment &rAdjustment);
155 : static void mungeAdjustment(ScrollBar &rTarget, const Adjustment &rAdjustment);
156 :
157 : typedef std::map<OString, OString> WidgetTranslations;
158 : typedef std::map<OString, WidgetTranslations> Translations;
159 :
160 0 : struct stockinfo
161 : {
162 : OString m_sStock;
163 : int m_nSize;
164 0 : stockinfo() : m_nSize(4) {}
165 : };
166 :
167 : typedef std::map<OString, stockinfo> StockMap;
168 :
169 0 : struct SizeGroup
170 : {
171 : OString m_sID;
172 : std::vector<OString> m_aWidgets;
173 : stringmap m_aProperties;
174 0 : SizeGroup(const OString &rId)
175 0 : : m_sID(rId)
176 : {
177 0 : }
178 : };
179 :
180 : typedef std::map<Window*, stringmap> AtkMap;
181 :
182 0 : struct ParserState
183 : {
184 : std::vector<RadioButtonGroupMap> m_aGroupMaps;
185 :
186 : std::vector<ComboBoxModelMap> m_aModelMaps;
187 : std::map<OString, ListStore> m_aModels;
188 :
189 : std::vector<TextBufferMap> m_aTextBufferMaps;
190 : std::map<OString, TextBuffer> m_aTextBuffers;
191 :
192 : std::vector<WidgetAdjustmentMap> m_aNumericFormatterAdjustmentMaps;
193 : std::vector<WidgetAdjustmentMap> m_aTimeFormatterAdjustmentMaps;
194 : std::vector<WidgetAdjustmentMap> m_aDateFormatterAdjustmentMaps;
195 : std::vector<WidgetAdjustmentMap> m_aScrollAdjustmentMaps;
196 : std::map<OString, Adjustment> m_aAdjustments;
197 :
198 : std::vector<ButtonImageWidgetMap> m_aButtonImageWidgetMaps;
199 : StockMap m_aStockMap;
200 :
201 : std::vector<ButtonMenuMap> m_aButtonMenuMaps;
202 :
203 : Translations m_aTranslations;
204 :
205 : std::map<Window*, Window*> m_aRedundantParentWidgets;
206 :
207 : std::vector<SizeGroup> m_aSizeGroups;
208 :
209 : AtkMap m_aAtkInfo;
210 :
211 : std::vector<MnemonicWidgetMap> m_aMnemonicWidgetMaps;
212 : };
213 :
214 : void loadTranslations(const com::sun::star::lang::Locale &rLocale, const OUString &rUri);
215 : OString getTranslation(const OString &rId, const OString &rProperty) const;
216 :
217 : OString m_sID;
218 : OString m_sHelpRoot;
219 : ResHookProc m_pStringReplace;
220 : Window *m_pParent;
221 : bool m_bToplevelHasDeferredInit;
222 : bool m_bToplevelHasDeferredProperties;
223 : bool m_bToplevelParentFound;
224 : ParserState *m_pParserState;
225 :
226 : Window *get_by_name(OString sID);
227 : void delete_by_name(OString sID);
228 :
229 : class sortIntoBestTabTraversalOrder
230 : : public std::binary_function<const Window*, const Window*, bool>
231 : {
232 : VclBuilder *m_pBuilder;
233 : public:
234 0 : sortIntoBestTabTraversalOrder(VclBuilder *pBuilder)
235 0 : : m_pBuilder(pBuilder)
236 : {
237 0 : }
238 : bool operator()(const Window *pA, const Window *pB) const;
239 : };
240 :
241 : /// XFrame to be able to extract labels and other properties of the UNO commands (like of .uno:Bold).
242 : com::sun::star::uno::Reference<com::sun::star::frame::XFrame> m_xFrame;
243 :
244 : public:
245 : VclBuilder(Window *pParent, OUString sUIRootDir, OUString sUIFile, OString sID = OString(),
246 : const com::sun::star::uno::Reference<com::sun::star::frame::XFrame> &rFrame = com::sun::star::uno::Reference<com::sun::star::frame::XFrame>());
247 : ~VclBuilder();
248 : Window *get_widget_root();
249 : //sID must exist and be of type T
250 0 : template <typename T> T* get(T*& ret, OString sID)
251 : {
252 0 : Window *w = get_by_name(sID);
253 : SAL_WARN_IF(!w, "vcl.layout", "widget \"" << sID.getStr() << "\" not found in .ui");
254 : SAL_WARN_IF(!dynamic_cast<T*>(w),
255 : "vcl.layout", ".ui widget \"" << sID.getStr() << "\" needs to correspond to vcl type " << typeid(T).name());
256 : assert(w && dynamic_cast<T*>(w));
257 0 : ret = static_cast<T*>(w);
258 0 : return ret;
259 : }
260 : PopupMenu* get_menu(PopupMenu*& ret, OString sID)
261 : {
262 : ret = get_menu(sID);
263 : SAL_WARN_IF(!ret, "vcl.layout", "menu \"" << sID.getStr() << "\" not found in .ui");
264 : assert(ret);
265 : return ret;
266 : }
267 : //sID may not exist, but must be of type T if it does
268 0 : template <typename T /*=Window if we had c++11*/> T* get(OString sID)
269 : {
270 0 : Window *w = get_by_name(sID);
271 : SAL_WARN_IF(w && !dynamic_cast<T*>(w),
272 : "vcl.layout", ".ui widget \"" << sID.getStr() << "\" needs to correspond to vcl type " << typeid(T).name());
273 : assert(!w || dynamic_cast<T*>(w));
274 0 : return static_cast<T*>(w);
275 : }
276 : //sID may not exist
277 : PopupMenu* get_menu(OString sID);
278 :
279 : //given an sID return the response value for that widget
280 : short get_response(const Window *pWindow) const;
281 :
282 : OString get_by_window(const Window *pWindow) const;
283 : void delete_by_window(const Window *pWindow);
284 :
285 : //apply the properties of rProps to pWindow
286 : static void set_properties(Window *pWindow, const stringmap &rProps);
287 :
288 : //Convert _ gtk markup to ~ vcl markup
289 : static OString convertMnemonicMarkup(const OString &rIn);
290 :
291 : static OString extractCustomProperty(stringmap &rMap);
292 :
293 : static bool extractDropdown(stringmap &rMap);
294 :
295 : //add a default value of 25 width-chars to a map if width-chars not set
296 : static void ensureDefaultWidthChars(VclBuilder::stringmap &rMap);
297 :
298 : //see m_aDeferredProperties, you need this for toplevel dialogs
299 : //which build themselves from their ctor. The properties on
300 : //the top level are stored in m_aDeferredProperties and need
301 : //to be applied post ctor
302 : void setDeferredProperties();
303 :
304 : //Helpers to retrofit all the existing code to the builder
305 : static void reorderWithinParent(std::vector<Window*>& rChilds, bool bIsButtonBox);
306 : static void reorderWithinParent(Window &rWindow, sal_uInt16 nNewPosition);
307 : private:
308 : Window *insertObject(Window *pParent, const OString &rClass, const OString &rID,
309 : stringmap &rProps, stringmap &rPangoAttributes,
310 : stringmap &rAtkProps, std::vector<OString> &rItems);
311 :
312 : Window *makeObject(Window *pParent, const OString &rClass, const OString &rID,
313 : stringmap &rVec, const std::vector<OString> &rItems);
314 :
315 : void connectNumericFormatterAdjustment(const OString &id, const OString &rAdjustment);
316 : void connectTimeFormatterAdjustment(const OString &id, const OString &rAdjustment);
317 : void connectDateFormatterAdjustment(const OString &id, const OString &rAdjustment);
318 :
319 : bool extractGroup(const OString &id, stringmap &rVec);
320 : bool extractModel(const OString &id, stringmap &rVec);
321 : bool extractBuffer(const OString &id, stringmap &rVec);
322 : bool extractScrollAdjustment(const OString &id, stringmap &rVec);
323 : bool extractButtonImage(const OString &id, stringmap &rMap, bool bRadio);
324 : bool extractStock(const OString &id, stringmap &rMap);
325 : void extractMnemonicWidget(const OString &id, stringmap &rMap);
326 :
327 : void handleTranslations(xmlreader::XmlReader &reader);
328 :
329 : void handleChild(Window *pParent, xmlreader::XmlReader &reader);
330 : Window* handleObject(Window *pParent, xmlreader::XmlReader &reader);
331 : void handlePacking(Window *pCurrent, xmlreader::XmlReader &reader);
332 : void applyPackingProperty(Window *pCurrent, xmlreader::XmlReader &reader);
333 : void collectProperty(xmlreader::XmlReader &reader, const OString &rID, stringmap &rVec);
334 : void collectPangoAttribute(xmlreader::XmlReader &reader, stringmap &rMap);
335 : void collectAtkAttribute(xmlreader::XmlReader &reader, stringmap &rMap);
336 : void collectAccelerator(xmlreader::XmlReader &reader, stringmap &rMap);
337 :
338 : void insertMenuObject(PopupMenu *pParent, const OString &rClass, const OString &rID,
339 : stringmap &rProps, stringmap &rAccels);
340 : void handleMenuChild(PopupMenu *pParent, xmlreader::XmlReader &reader);
341 : void handleMenuObject(PopupMenu *pParent, xmlreader::XmlReader &reader);
342 :
343 : void handleListStore(xmlreader::XmlReader &reader, const OString &rID);
344 : void handleRow(xmlreader::XmlReader &reader, const OString &rID, sal_Int32 nRowIndex);
345 : void handleAdjustment(const OString &rID, stringmap &rProperties);
346 : void handleTextBuffer(const OString &rID, stringmap &rProperties);
347 : void handleTabChild(Window *pParent, xmlreader::XmlReader &reader);
348 : void handleMenu(xmlreader::XmlReader &reader, const OString &rID);
349 : std::vector<OString> handleItems(xmlreader::XmlReader &reader, const OString &rID);
350 :
351 : void handleSizeGroup(xmlreader::XmlReader &reader, const OString &rID);
352 :
353 : void handleAtkObject(xmlreader::XmlReader &reader, const OString &rID, Window *pWindow);
354 :
355 : void handleActionWidget(xmlreader::XmlReader &reader);
356 :
357 : PackingData get_window_packing_data(const Window *pWindow) const;
358 : void set_window_packing_position(const Window *pWindow, sal_Int32 nPosition);
359 :
360 : Window* prepareWidgetOwnScrolling(Window *pParent, WinBits &rWinStyle);
361 : void cleanupWidgetOwnScrolling(Window *pScrollParent, Window *pWindow, stringmap &rMap);
362 :
363 : void set_response(OString sID, short nResponse);
364 : };
365 :
366 :
367 : //helper baseclass to ease retro fitting dialogs/tabpages that load a resource
368 : //to load a .ui file instead
369 : //
370 : //vcl requires the Window Children of a Parent Window to be destroyed before
371 : //the Parent Window. VclBuilderContainer owns the VclBuilder which owns the
372 : //Children Window. So the VclBuilderContainer dtor must be called before
373 : //the Parent Window dtor.
374 : //
375 : //i.e. class Dialog : public SystemWindow, public VclBuilderContainer
376 : //not class Dialog : public VclBuilderContainer, public SystemWindow
377 :
378 : class VCL_DLLPUBLIC VclBuilderContainer
379 : {
380 : protected:
381 : VclBuilder *m_pUIBuilder;
382 : public:
383 : VclBuilderContainer();
384 : virtual ~VclBuilderContainer();
385 : static OUString getUIRootDir();
386 0 : bool hasBuilder() const { return m_pUIBuilder != NULL; }
387 0 : template <typename T> T* get(T*& ret, OString sID)
388 : {
389 0 : return m_pUIBuilder->get<T>(ret, sID);
390 : }
391 0 : template <typename T /*=Window if we had c++11*/> T* get(OString sID)
392 : {
393 0 : return m_pUIBuilder->get<T>(sID);
394 : }
395 : PopupMenu* get_menu(PopupMenu*& ret, OString sID)
396 : {
397 : return m_pUIBuilder->get_menu(ret, sID);
398 : }
399 0 : PopupMenu* get_menu(OString sID)
400 : {
401 0 : return m_pUIBuilder->get_menu(sID);
402 : }
403 6 : void setDeferredProperties()
404 : {
405 6 : if (!m_pUIBuilder)
406 12 : return;
407 0 : m_pUIBuilder->setDeferredProperties();
408 : }
409 : };
410 :
411 : /*
412 : * @return true if rValue is "True", "true", "1", etc.
413 : */
414 : bool VCL_DLLPUBLIC toBool(const OString &rValue);
415 :
416 : #endif
417 :
418 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|