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