bump product version to 4.2.0.1
[LibreOffice.git] / include / vcl / builder.hxx
blob8cd25a0ce287b696e9de6fdf5fd5383ca3bd8c03
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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 */
10 #ifndef INCLUDED_VCL_BUILDER_HXX
11 #define INCLUDED_VCL_BUILDER_HXX
13 #include <typeinfo>
14 #include <osl/module.hxx>
15 #include <tools/resmgr.hxx>
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>
26 #include <com/sun/star/frame/XFrame.hpp>
28 class ListBox;
29 class NumericFormatter;
30 class PopupMenu;
31 class ScrollBar;
32 class DateField;
33 class TimeField;
34 class VclExpander;
35 class VclMultiLineEdit;
37 class VCL_DLLPUBLIC VclBuilder: private boost::noncopyable
39 public:
40 typedef std::map<OString, OString> stringmap;
41 typedef ::Window* (*customMakeWidget)(::Window *pParent, stringmap &rVec);
42 private:
43 typedef boost::ptr_map<OUString, osl::Module> ModuleMap;
44 //We store these until the builder is deleted, that way we can use the
45 //ui-previewer on custom widgets and guarantee the modules they are from
46 //exist for the duration of the dialog
47 ModuleMap m_aModuleMap;
49 //If the toplevel window has any properties which need to be set on it,
50 //but the toplevel is the owner of the builder, then its ctor
51 //has not been completed during the building, so properties for it
52 //are collected here and need to be set afterwards, e.g. during
53 //Show or Execute
54 stringmap m_aDeferredProperties;
56 struct PackingData
58 bool m_bVerticalOrient;
59 sal_Int32 m_nPosition;
60 PackingData(bool bVerticalOrient = false, sal_Int32 nPosition = -1)
61 : m_bVerticalOrient(bVerticalOrient)
62 , m_nPosition(nPosition)
67 struct WinAndId
69 OString m_sID;
70 ::Window *m_pWindow;
71 short m_nResponseId;
72 PackingData m_aPackingData;
73 WinAndId(const OString &rId, ::Window *pWindow, bool bVertical)
74 : m_sID(rId)
75 , m_pWindow(pWindow)
76 , m_nResponseId(RET_CANCEL)
77 , m_aPackingData(bVertical)
81 std::vector<WinAndId> m_aChildren;
83 struct MenuAndId
85 OString m_sID;
86 PopupMenu *m_pMenu;
87 MenuAndId(const OString &rId, PopupMenu *pMenu)
88 : m_sID(rId)
89 , m_pMenu(pMenu)
93 std::vector<MenuAndId> m_aMenus;
95 struct StringPair
97 OString m_sID;
98 OString m_sValue;
99 StringPair(const OString &rId, const OString &rValue)
100 : m_sID(rId)
101 , m_sValue(rValue)
106 typedef StringPair RadioButtonGroupMap;
108 struct ButtonImageWidgetMap
110 OString m_sID;
111 OString m_sValue;
112 bool m_bRadio;
113 ButtonImageWidgetMap(const OString &rId, const OString &rValue, bool bRadio)
114 : m_sID(rId)
115 , m_sValue(rValue)
116 , m_bRadio(bRadio)
121 typedef StringPair TextBufferMap;
122 typedef StringPair WidgetAdjustmentMap;
123 typedef StringPair ButtonMenuMap;
124 typedef StringPair MnemonicWidgetMap;
126 struct ComboBoxModelMap
128 OString m_sID;
129 OString m_sValue;
130 sal_Int32 m_nActiveId;
131 ComboBoxModelMap(const OString &rId, const OString &rValue, sal_Int32 nActiveId)
132 : m_sID(rId)
133 , m_sValue(rValue)
134 , m_nActiveId(nActiveId)
139 struct ListStore
141 typedef std::vector<OString> row;
142 std::vector<row> m_aEntries;
144 const ListStore* get_model_by_name(OString sID) const;
145 static void mungeModel(ListBox &rTarget, const ListStore &rStore, sal_uInt16 nActiveId);
147 typedef stringmap TextBuffer;
148 const TextBuffer* get_buffer_by_name(OString sID) const;
149 static void mungeTextBuffer(VclMultiLineEdit &rTarget, const TextBuffer &rTextBuffer);
151 typedef stringmap Adjustment;
152 const Adjustment *get_adjustment_by_name(OString sID) const;
153 static void mungeAdjustment(NumericFormatter &rTarget, const Adjustment &rAdjustment);
154 static void mungeAdjustment(DateField &rTarget, const Adjustment &rAdjustment);
155 static void mungeAdjustment(TimeField &rTarget, const Adjustment &rAdjustment);
156 static void mungeAdjustment(ScrollBar &rTarget, const Adjustment &rAdjustment);
158 typedef std::map<OString, OString> WidgetTranslations;
159 typedef std::map<OString, WidgetTranslations> Translations;
161 struct stockinfo
163 OString m_sStock;
164 int m_nSize;
165 stockinfo() : m_nSize(4) {}
168 typedef std::map<OString, stockinfo> StockMap;
170 struct SizeGroup
172 OString m_sID;
173 std::vector<OString> m_aWidgets;
174 stringmap m_aProperties;
175 SizeGroup(const OString &rId)
176 : m_sID(rId)
181 typedef std::map< ::Window*, stringmap> AtkMap;
183 struct ParserState
185 std::vector<RadioButtonGroupMap> m_aGroupMaps;
187 std::vector<ComboBoxModelMap> m_aModelMaps;
188 std::map<OString, ListStore> m_aModels;
190 std::vector<TextBufferMap> m_aTextBufferMaps;
191 std::map<OString, TextBuffer> m_aTextBuffers;
193 std::vector<WidgetAdjustmentMap> m_aNumericFormatterAdjustmentMaps;
194 std::vector<WidgetAdjustmentMap> m_aTimeFormatterAdjustmentMaps;
195 std::vector<WidgetAdjustmentMap> m_aDateFormatterAdjustmentMaps;
196 std::vector<WidgetAdjustmentMap> m_aScrollAdjustmentMaps;
197 std::map<OString, Adjustment> m_aAdjustments;
199 std::vector<ButtonImageWidgetMap> m_aButtonImageWidgetMaps;
200 StockMap m_aStockMap;
202 std::vector<ButtonMenuMap> m_aButtonMenuMaps;
204 Translations m_aTranslations;
206 std::map< ::Window*, ::Window*> m_aRedundantParentWidgets;
208 std::vector<SizeGroup> m_aSizeGroups;
210 AtkMap m_aAtkInfo;
212 std::vector<MnemonicWidgetMap> m_aMnemonicWidgetMaps;
214 std::vector<VclExpander*> m_aExpanderWidgets;
217 void loadTranslations(const LanguageTag &rLanguageTag, const OUString &rUri);
218 OString getTranslation(const OString &rId, const OString &rProperty) const;
220 OString m_sID;
221 OString m_sHelpRoot;
222 ResHookProc m_pStringReplace;
223 ::Window *m_pParent;
224 bool m_bToplevelHasDeferredInit;
225 bool m_bToplevelHasDeferredProperties;
226 bool m_bToplevelParentFound;
227 ParserState *m_pParserState;
229 ::Window *get_by_name(OString sID);
230 void delete_by_name(OString sID);
232 class sortIntoBestTabTraversalOrder
233 : public std::binary_function<const ::Window*, const ::Window*, bool>
235 VclBuilder *m_pBuilder;
236 public:
237 sortIntoBestTabTraversalOrder(VclBuilder *pBuilder)
238 : m_pBuilder(pBuilder)
241 bool operator()(const ::Window *pA, const ::Window *pB) const;
244 /// XFrame to be able to extract labels and other properties of the UNO commands (like of .uno:Bold).
245 com::sun::star::uno::Reference<com::sun::star::frame::XFrame> m_xFrame;
247 public:
248 VclBuilder(::Window *pParent, OUString sUIRootDir, OUString sUIFile,
249 OString sID = OString(),
250 const com::sun::star::uno::Reference<com::sun::star::frame::XFrame> &rFrame = com::sun::star::uno::Reference<com::sun::star::frame::XFrame>());
251 ~VclBuilder();
252 ::Window *get_widget_root();
253 //sID must exist and be of type T
254 template <typename T> T* get(T*& ret, OString sID)
256 ::Window *w = get_by_name(sID);
257 SAL_WARN_IF(!w, "vcl.layout", "widget \"" << sID.getStr() << "\" not found in .ui");
258 SAL_WARN_IF(!dynamic_cast<T*>(w),
259 "vcl.layout", ".ui widget \"" << sID.getStr() << "\" needs to correspond to vcl type " << typeid(T).name());
260 assert(w && dynamic_cast<T*>(w));
261 ret = static_cast<T*>(w);
262 return ret;
264 PopupMenu* get_menu(PopupMenu*& ret, OString sID)
266 ret = get_menu(sID);
267 SAL_WARN_IF(!ret, "vcl.layout", "menu \"" << sID.getStr() << "\" not found in .ui");
268 assert(ret);
269 return ret;
271 //sID may not exist, but must be of type T if it does
272 template <typename T /*= ::Window if we had c++11*/> T* get(OString sID)
274 ::Window *w = get_by_name(sID);
275 SAL_WARN_IF(w && !dynamic_cast<T*>(w),
276 "vcl.layout", ".ui widget \"" << sID.getStr() << "\" needs to correspond to vcl type " << typeid(T).name());
277 assert(!w || dynamic_cast<T*>(w));
278 return static_cast<T*>(w);
280 //sID may not exist
281 PopupMenu* get_menu(OString sID);
283 //given an sID return the response value for that widget
284 short get_response(const ::Window *pWindow) const;
286 OString get_by_window(const ::Window *pWindow) const;
287 void delete_by_window(const ::Window *pWindow);
289 //apply the properties of rProps to pWindow
290 static void set_properties(::Window *pWindow, const stringmap &rProps);
292 //Convert _ gtk markup to ~ vcl markup
293 static OString convertMnemonicMarkup(const OString &rIn);
295 static OString extractCustomProperty(stringmap &rMap);
297 static bool extractDropdown(stringmap &rMap);
299 //add a default value of 25 width-chars to a map if width-chars not set
300 static void ensureDefaultWidthChars(VclBuilder::stringmap &rMap);
302 //see m_aDeferredProperties, you need this for toplevel dialogs
303 //which build themselves from their ctor. The properties on
304 //the top level are stored in m_aDeferredProperties and need
305 //to be applied post ctor
306 void setDeferredProperties();
308 //Helpers to retrofit all the existing code to the builder
309 static void reorderWithinParent(std::vector< ::Window*>& rChilds, bool bIsButtonBox);
310 static void reorderWithinParent(::Window &rWindow, sal_uInt16 nNewPosition);
311 private:
312 ::Window *insertObject(::Window *pParent,
313 const OString &rClass, const OString &rID,
314 stringmap &rProps, stringmap &rPangoAttributes,
315 stringmap &rAtkProps, std::vector<OString> &rItems);
317 ::Window *makeObject(::Window *pParent,
318 const OString &rClass, const OString &rID,
319 stringmap &rVec, const std::vector<OString> &rItems);
321 void connectNumericFormatterAdjustment(const OString &id, const OString &rAdjustment);
322 void connectTimeFormatterAdjustment(const OString &id, const OString &rAdjustment);
323 void connectDateFormatterAdjustment(const OString &id, const OString &rAdjustment);
325 bool extractGroup(const OString &id, stringmap &rVec);
326 bool extractModel(const OString &id, stringmap &rVec);
327 bool extractBuffer(const OString &id, stringmap &rVec);
328 bool extractScrollAdjustment(const OString &id, stringmap &rVec);
329 bool extractButtonImage(const OString &id, stringmap &rMap, bool bRadio);
330 bool extractStock(const OString &id, stringmap &rMap);
331 void extractMnemonicWidget(const OString &id, stringmap &rMap);
333 void handleTranslations(xmlreader::XmlReader &reader);
335 void handleChild(::Window *pParent, xmlreader::XmlReader &reader);
336 ::Window* handleObject(::Window *pParent, xmlreader::XmlReader &reader);
337 void handlePacking(::Window *pCurrent, xmlreader::XmlReader &reader);
338 void applyPackingProperty(::Window *pCurrent, xmlreader::XmlReader &reader);
339 void collectProperty(xmlreader::XmlReader &reader, const OString &rID, stringmap &rVec);
340 void collectPangoAttribute(xmlreader::XmlReader &reader, stringmap &rMap);
341 void collectAtkAttribute(xmlreader::XmlReader &reader, stringmap &rMap);
342 void collectAccelerator(xmlreader::XmlReader &reader, stringmap &rMap);
344 void insertMenuObject(PopupMenu *pParent, const OString &rClass, const OString &rID,
345 stringmap &rProps, stringmap &rAccels);
346 void handleMenuChild(PopupMenu *pParent, xmlreader::XmlReader &reader);
347 void handleMenuObject(PopupMenu *pParent, xmlreader::XmlReader &reader);
349 void handleListStore(xmlreader::XmlReader &reader, const OString &rID);
350 void handleRow(xmlreader::XmlReader &reader, const OString &rID, sal_Int32 nRowIndex);
351 void handleAdjustment(const OString &rID, stringmap &rProperties);
352 void handleTextBuffer(const OString &rID, stringmap &rProperties);
353 void handleTabChild(::Window *pParent, xmlreader::XmlReader &reader);
354 void handleMenu(xmlreader::XmlReader &reader, const OString &rID);
355 std::vector<OString> handleItems(xmlreader::XmlReader &reader, const OString &rID);
357 void handleSizeGroup(xmlreader::XmlReader &reader, const OString &rID);
359 void handleAtkObject(xmlreader::XmlReader &reader, const OString &rID, ::Window *pWindow);
361 void handleActionWidget(xmlreader::XmlReader &reader);
363 PackingData get_window_packing_data(const ::Window *pWindow) const;
364 void set_window_packing_position(const ::Window *pWindow, sal_Int32 nPosition);
366 ::Window* prepareWidgetOwnScrolling(::Window *pParent, WinBits &rWinStyle);
367 void cleanupWidgetOwnScrolling(::Window *pScrollParent, ::Window *pWindow, stringmap &rMap);
369 void set_response(OString sID, short nResponse);
373 //helper baseclass to ease retro fitting dialogs/tabpages that load a resource
374 //to load a .ui file instead
376 //vcl requires the Window Children of a Parent Window to be destroyed before
377 //the Parent Window. VclBuilderContainer owns the VclBuilder which owns the
378 //Children Window. So the VclBuilderContainer dtor must be called before
379 //the Parent Window dtor.
381 //i.e. class Dialog : public SystemWindow, public VclBuilderContainer
382 //not class Dialog : public VclBuilderContainer, public SystemWindow
384 class VCL_DLLPUBLIC VclBuilderContainer
386 protected:
387 VclBuilder *m_pUIBuilder;
388 public:
389 VclBuilderContainer();
390 virtual ~VclBuilderContainer();
391 static OUString getUIRootDir();
392 bool hasBuilder() const { return m_pUIBuilder != NULL; }
393 template <typename T> T* get(T*& ret, OString sID)
395 return m_pUIBuilder->get<T>(ret, sID);
397 template <typename T /*= ::Window if we had c++11*/> T* get(OString sID)
399 return m_pUIBuilder->get<T>(sID);
401 PopupMenu* get_menu(PopupMenu*& ret, OString sID)
403 return m_pUIBuilder->get_menu(ret, sID);
405 PopupMenu* get_menu(OString sID)
407 return m_pUIBuilder->get_menu(sID);
409 void setDeferredProperties()
411 if (!m_pUIBuilder)
412 return;
413 m_pUIBuilder->setDeferredProperties();
418 * @return true if rValue is "True", "true", "1", etc.
420 bool VCL_DLLPUBLIC toBool(const OString &rValue);
422 #endif
424 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */