1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
10 #ifndef _VCLBUILDER_HXX
11 #define _VCLBUILDER_HXX
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>
23 #include <boost/noncopyable.hpp>
24 #include <boost/ptr_container/ptr_map.hpp>
26 #include <com/sun/star/frame/XFrame.hpp>
29 class NumericFormatter
;
35 class VclMultiLineEdit
;
37 class VCL_DLLPUBLIC VclBuilder
: private boost::noncopyable
40 typedef std::map
<OString
, OString
> stringmap
;
41 typedef Window
* (*customMakeWidget
)(Window
*pParent
, stringmap
&rVec
);
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
54 stringmap m_aDeferredProperties
;
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
)
72 PackingData m_aPackingData
;
73 WinAndId(const OString
&rId
, Window
*pWindow
, bool bVertical
)
76 , m_nResponseId(RET_CANCEL
)
77 , m_aPackingData(bVertical
)
81 std::vector
<WinAndId
> m_aChildren
;
87 MenuAndId(const OString
&rId
, PopupMenu
*pMenu
)
93 std::vector
<MenuAndId
> m_aMenus
;
99 StringPair(const OString
&rId
, const OString
&rValue
)
106 typedef StringPair RadioButtonGroupMap
;
108 struct ButtonImageWidgetMap
113 ButtonImageWidgetMap(const OString
&rId
, const OString
&rValue
, bool bRadio
)
121 typedef StringPair TextBufferMap
;
122 typedef StringPair WidgetAdjustmentMap
;
123 typedef StringPair ButtonMenuMap
;
124 typedef StringPair MnemonicWidgetMap
;
126 struct ComboBoxModelMap
130 sal_Int32 m_nActiveId
;
131 ComboBoxModelMap(const OString
&rId
, const OString
&rValue
, sal_Int32 nActiveId
)
134 , m_nActiveId(nActiveId
)
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
;
165 stockinfo() : m_nSize(4) {}
168 typedef std::map
<OString
, stockinfo
> StockMap
;
173 std::vector
<OString
> m_aWidgets
;
174 stringmap m_aProperties
;
175 SizeGroup(const OString
&rId
)
181 typedef std::map
<Window
*, stringmap
> AtkMap
;
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
;
212 std::vector
<MnemonicWidgetMap
> m_aMnemonicWidgetMaps
;
214 std::vector
<VclExpander
*> m_aExpanderWidgets
;
217 void loadTranslations(const com::sun::star::lang::Locale
&rLocale
, const OUString
&rUri
);
218 OString
getTranslation(const OString
&rId
, const OString
&rProperty
) const;
222 ResHookProc m_pStringReplace
;
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
;
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
;
248 VclBuilder(Window
*pParent
, OUString sUIRootDir
, OUString sUIFile
, OString sID
= OString(),
249 const com::sun::star::uno::Reference
<com::sun::star::frame::XFrame
> &rFrame
= com::sun::star::uno::Reference
<com::sun::star::frame::XFrame
>());
251 Window
*get_widget_root();
252 //sID must exist and be of type T
253 template <typename T
> T
* get(T
*& ret
, OString sID
)
255 Window
*w
= get_by_name(sID
);
256 SAL_WARN_IF(!w
, "vcl.layout", "widget \"" << sID
.getStr() << "\" not found in .ui");
257 SAL_WARN_IF(!dynamic_cast<T
*>(w
),
258 "vcl.layout", ".ui widget \"" << sID
.getStr() << "\" needs to correspond to vcl type " << typeid(T
).name());
259 assert(w
&& dynamic_cast<T
*>(w
));
260 ret
= static_cast<T
*>(w
);
263 PopupMenu
* get_menu(PopupMenu
*& ret
, OString sID
)
266 SAL_WARN_IF(!ret
, "vcl.layout", "menu \"" << sID
.getStr() << "\" not found in .ui");
270 //sID may not exist, but must be of type T if it does
271 template <typename T
/*=Window if we had c++11*/> T
* get(OString sID
)
273 Window
*w
= get_by_name(sID
);
274 SAL_WARN_IF(w
&& !dynamic_cast<T
*>(w
),
275 "vcl.layout", ".ui widget \"" << sID
.getStr() << "\" needs to correspond to vcl type " << typeid(T
).name());
276 assert(!w
|| dynamic_cast<T
*>(w
));
277 return static_cast<T
*>(w
);
280 PopupMenu
* get_menu(OString sID
);
282 //given an sID return the response value for that widget
283 short get_response(const Window
*pWindow
) const;
285 OString
get_by_window(const Window
*pWindow
) const;
286 void delete_by_window(const Window
*pWindow
);
288 //apply the properties of rProps to pWindow
289 static void set_properties(Window
*pWindow
, const stringmap
&rProps
);
291 //Convert _ gtk markup to ~ vcl markup
292 static OString
convertMnemonicMarkup(const OString
&rIn
);
294 static OString
extractCustomProperty(stringmap
&rMap
);
296 //add a default value of 25 width-chars to a map if width-chars not set
297 static void ensureDefaultWidthChars(VclBuilder::stringmap
&rMap
);
299 //see m_aDeferredProperties, you need this for toplevel dialogs
300 //which build themselves from their ctor. The properties on
301 //the top level are stored in m_aDeferredProperties and need
302 //to be applied post ctor
303 void setDeferredProperties();
305 //Helpers to retrofit all the existing code to the builder
306 static void reorderWithinParent(std::vector
<Window
*>& rChilds
, bool bIsButtonBox
);
307 static void reorderWithinParent(Window
&rWindow
, sal_uInt16 nNewPosition
);
309 Window
*insertObject(Window
*pParent
, const OString
&rClass
, const OString
&rID
,
310 stringmap
&rProps
, stringmap
&rPangoAttributes
,
311 stringmap
&rAtkProps
, std::vector
<OString
> &rItems
);
313 Window
*makeObject(Window
*pParent
, const OString
&rClass
, const OString
&rID
,
314 stringmap
&rVec
, const std::vector
<OString
> &rItems
);
316 void connectNumericFormatterAdjustment(const OString
&id
, const OString
&rAdjustment
);
317 void connectTimeFormatterAdjustment(const OString
&id
, const OString
&rAdjustment
);
318 void connectDateFormatterAdjustment(const OString
&id
, const OString
&rAdjustment
);
320 bool extractGroup(const OString
&id
, stringmap
&rVec
);
321 bool extractModel(const OString
&id
, stringmap
&rVec
);
322 bool extractBuffer(const OString
&id
, stringmap
&rVec
);
323 bool extractScrollAdjustment(const OString
&id
, stringmap
&rVec
);
324 bool extractButtonImage(const OString
&id
, stringmap
&rMap
, bool bRadio
);
325 bool extractStock(const OString
&id
, stringmap
&rMap
);
326 void extractMnemonicWidget(const OString
&id
, stringmap
&rMap
);
328 void handleTranslations(xmlreader::XmlReader
&reader
);
330 void handleChild(Window
*pParent
, xmlreader::XmlReader
&reader
);
331 Window
* handleObject(Window
*pParent
, xmlreader::XmlReader
&reader
);
332 void handlePacking(Window
*pCurrent
, xmlreader::XmlReader
&reader
);
333 void applyPackingProperty(Window
*pCurrent
, xmlreader::XmlReader
&reader
);
334 void collectProperty(xmlreader::XmlReader
&reader
, const OString
&rID
, stringmap
&rVec
);
335 void collectPangoAttribute(xmlreader::XmlReader
&reader
, stringmap
&rMap
);
336 void collectAtkAttribute(xmlreader::XmlReader
&reader
, stringmap
&rMap
);
337 void collectAccelerator(xmlreader::XmlReader
&reader
, stringmap
&rMap
);
339 void insertMenuObject(PopupMenu
*pParent
, const OString
&rClass
, const OString
&rID
,
340 stringmap
&rProps
, stringmap
&rAccels
);
341 void handleMenuChild(PopupMenu
*pParent
, xmlreader::XmlReader
&reader
);
342 void handleMenuObject(PopupMenu
*pParent
, xmlreader::XmlReader
&reader
);
344 void handleListStore(xmlreader::XmlReader
&reader
, const OString
&rID
);
345 void handleRow(xmlreader::XmlReader
&reader
, const OString
&rID
, sal_Int32 nRowIndex
);
346 void handleAdjustment(const OString
&rID
, stringmap
&rProperties
);
347 void handleTextBuffer(const OString
&rID
, stringmap
&rProperties
);
348 void handleTabChild(Window
*pParent
, xmlreader::XmlReader
&reader
);
349 void handleMenu(xmlreader::XmlReader
&reader
, const OString
&rID
);
350 std::vector
<OString
> handleItems(xmlreader::XmlReader
&reader
, const OString
&rID
);
352 void handleSizeGroup(xmlreader::XmlReader
&reader
, const OString
&rID
);
354 void handleAtkObject(xmlreader::XmlReader
&reader
, const OString
&rID
, Window
*pWindow
);
356 void handleActionWidget(xmlreader::XmlReader
&reader
);
358 PackingData
get_window_packing_data(const Window
*pWindow
) const;
359 void set_window_packing_position(const Window
*pWindow
, sal_Int32 nPosition
);
361 Window
* prepareWidgetOwnScrolling(Window
*pParent
, WinBits
&rWinStyle
);
362 void cleanupWidgetOwnScrolling(Window
*pScrollParent
, Window
*pWindow
, stringmap
&rMap
);
364 void set_response(OString sID
, short nResponse
);
368 //helper baseclass to ease retro fitting dialogs/tabpages that load a resource
369 //to load a .ui file instead
371 //vcl requires the Window Children of a Parent Window to be destroyed before
372 //the Parent Window. VclBuilderContainer owns the VclBuilder which owns the
373 //Children Window. So the VclBuilderContainer dtor must be called before
374 //the Parent Window dtor.
376 //i.e. class Dialog : public SystemWindow, public VclBuilderContainer
377 //not class Dialog : public VclBuilderContainer, public SystemWindow
379 class VCL_DLLPUBLIC VclBuilderContainer
382 VclBuilder
*m_pUIBuilder
;
384 VclBuilderContainer();
385 virtual ~VclBuilderContainer();
386 static OUString
getUIRootDir();
387 bool hasBuilder() const { return m_pUIBuilder
!= NULL
; }
388 template <typename T
> T
* get(T
*& ret
, OString sID
)
390 return m_pUIBuilder
->get
<T
>(ret
, sID
);
392 template <typename T
/*=Window if we had c++11*/> T
* get(OString sID
)
394 return m_pUIBuilder
->get
<T
>(sID
);
396 PopupMenu
* get_menu(PopupMenu
*& ret
, OString sID
)
398 return m_pUIBuilder
->get_menu(ret
, sID
);
400 PopupMenu
* get_menu(OString sID
)
402 return m_pUIBuilder
->get_menu(sID
);
404 void setDeferredProperties()
408 m_pUIBuilder
->setDeferredProperties();
413 * @return true if rValue is "True", "true", "1", etc.
415 bool VCL_DLLPUBLIC
toBool(const OString
&rValue
);
419 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */