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 INCLUDED_VCL_BUILDER_HXX
11 #define INCLUDED_VCL_BUILDER_HXX
14 #include <sal/log.hxx>
15 #include <unotools/resmgr.hxx>
16 #include <tools/fldunit.hxx>
17 #include <vcl/dllapi.h>
18 #include <vcl/window.hxx>
19 #include <vcl/vclptr.hxx>
20 #include <tools/wintypes.hxx>
21 #include <vcl/EnumContext.hxx>
27 # //some problem with MacOSX and a check define
37 class NumericFormatter
;
39 class SalInstanceBuilder
;
47 class VclMultiLineEdit
;
48 struct NotebookBarAddonsItem
;
49 namespace xmlreader
{ class XmlReader
; }
50 namespace com
{ namespace sun
{ namespace star
{ namespace frame
{ class XFrame
; } } } }
52 struct ComboBoxTextItem
56 ComboBoxTextItem(const OUString
& rItem
, const OString
& rId
)
63 class VCL_DLLPUBLIC VclBuilder
66 typedef std::map
<OString
, OUString
> stringmap
;
67 typedef std::map
<OString
, std::pair
<OString
, OString
>> accelmap
;
68 /// These functions create a new widget with parent pParent and return it in rRet
69 typedef void (*customMakeWidget
)(VclPtr
<vcl::Window
> &rRet
, const VclPtr
<vcl::Window
> &pParent
, stringmap
&rVec
);
72 VclBuilder(vcl::Window
* pParent
, const OUString
& sUIRootDir
, const OUString
& sUIFile
,
73 const OString
& sID
= OString(),
74 const css::uno::Reference
<css::frame::XFrame
>& rFrame
75 = css::uno::Reference
<css::frame::XFrame
>(),
77 const NotebookBarAddonsItem
* pNotebookBarAddonsItem
= nullptr);
79 ///releases references and disposes all children.
80 void disposeBuilder();
81 //sID must exist and be of type T
82 template <typename T
> T
* get(VclPtr
<T
>& ret
, const OString
& sID
);
84 //sID may not exist, but must be of type T if it does
85 template <typename T
/*= vcl::Window if we had c++11*/> T
* get(const OString
& sID
);
87 vcl::Window
* get_widget_root();
90 PopupMenu
* get_menu(const OString
& sID
);
92 //release ownership of pWindow, i.e. don't delete it
93 void drop_ownership(const vcl::Window
*pWindow
);
95 //see m_aDeferredProperties, you need this for toplevel dialogs
96 //which build themselves from their ctor. The properties on
97 //the top level are stored in m_aDeferredProperties and need
98 //to be applied post ctor
99 void setDeferredProperties();
101 /// return UI-File name (without '.ui')
102 const OString
& getUIFile() const
107 /// Pre-loads all modules containing UI information
108 static void preload();
111 VclBuilder(const VclBuilder
&) = delete;
112 VclBuilder
& operator=(const VclBuilder
&) = delete;
114 // owner for ListBox/ComboBox UserData
115 std::vector
<std::unique_ptr
<OUString
>> m_aUserData
;
117 //If the toplevel window has any properties which need to be set on it,
118 //but the toplevel is the owner of the builder, then its ctor
119 //has not been completed during the building, so properties for it
120 //are collected here and need to be set afterwards, e.g. during
122 stringmap m_aDeferredProperties
;
124 std::unique_ptr
<NotebookBarAddonsItem
> m_pNotebookBarAddonsItem
;
128 bool m_bVerticalOrient
;
129 sal_Int32 m_nPosition
;
130 PackingData(bool bVerticalOrient
= false)
131 : m_bVerticalOrient(bVerticalOrient
)
140 VclPtr
<vcl::Window
> m_pWindow
;
141 PackingData m_aPackingData
;
142 WinAndId(const OString
&rId
, vcl::Window
*pWindow
, bool bVertical
)
145 , m_aPackingData(bVertical
)
149 std::vector
<WinAndId
> m_aChildren
;
154 VclPtr
<Menu
> m_pMenu
;
155 MenuAndId(const OString
&rId
, Menu
*pMenu
);
158 std::vector
<MenuAndId
> m_aMenus
;
163 OString
const m_sValue
;
164 StringPair(const OString
&rId
, const OString
&rValue
)
175 UStringPair(const OString
&rId
, const OUString
&rValue
)
182 typedef StringPair RadioButtonGroupMap
;
184 struct ButtonImageWidgetMap
187 OUString
const m_sValue
;
189 ButtonImageWidgetMap(const OString
&rId
, const OUString
&rValue
, bool bRadio
)
197 typedef UStringPair TextBufferMap
;
198 typedef UStringPair WidgetAdjustmentMap
;
199 typedef UStringPair ButtonMenuMap
;
200 typedef UStringPair MnemonicWidgetMap
;
202 struct ComboBoxModelMap
205 OUString
const m_sValue
;
206 sal_Int32
const m_nActiveId
;
207 ComboBoxModelMap(const OString
&rId
, const OUString
&rValue
, sal_Int32 nActiveId
)
210 , m_nActiveId(nActiveId
)
217 typedef std::vector
<OUString
> row
;
218 std::vector
<row
> m_aEntries
;
221 const ListStore
* get_model_by_name(const OString
& sID
) const;
222 void mungeModel(ListBox
&rTarget
, const ListStore
&rStore
, sal_uInt16 nActiveId
);
223 void mungeModel(ComboBox
&rTarget
, const ListStore
&rStore
, sal_uInt16 nActiveId
);
224 void mungeModel(SvTabListBox
&rTarget
, const ListStore
&rStore
, sal_uInt16 nActiveId
);
226 typedef stringmap TextBuffer
;
227 const TextBuffer
* get_buffer_by_name(const OString
& sID
) const;
229 static void mungeTextBuffer(VclMultiLineEdit
&rTarget
, const TextBuffer
&rTextBuffer
);
231 typedef stringmap Adjustment
;
232 const Adjustment
* get_adjustment_by_name(const OString
& sID
) const;
234 static void mungeAdjustment(NumericFormatter
&rTarget
, const Adjustment
&rAdjustment
);
235 static void mungeAdjustment(FormattedField
&rTarget
, const Adjustment
&rAdjustment
);
236 static void mungeAdjustment(DateField
&rTarget
, const Adjustment
&rAdjustment
);
237 static void mungeAdjustment(TimeField
&rTarget
, const Adjustment
&rAdjustment
);
238 static void mungeAdjustment(ScrollBar
&rTarget
, const Adjustment
&rAdjustment
);
239 static void mungeAdjustment(Slider
&rTarget
, const Adjustment
&rAdjustment
);
245 stockinfo() : m_nSize(4) {}
248 typedef std::map
<OString
, stockinfo
> StockMap
;
252 std::vector
<OString
> m_aWidgets
;
253 stringmap m_aProperties
;
257 typedef std::map
<VclPtr
<vcl::Window
>, stringmap
> AtkMap
;
261 std::locale m_aResLocale
;
263 std::vector
<RadioButtonGroupMap
> m_aGroupMaps
;
265 std::vector
<ComboBoxModelMap
> m_aModelMaps
;
266 std::map
<OString
, ListStore
> m_aModels
;
268 std::vector
<TextBufferMap
> m_aTextBufferMaps
;
269 std::map
<OString
, TextBuffer
> m_aTextBuffers
;
271 std::vector
<WidgetAdjustmentMap
> m_aNumericFormatterAdjustmentMaps
;
272 std::vector
<WidgetAdjustmentMap
> m_aFormattedFormatterAdjustmentMaps
;
273 std::vector
<WidgetAdjustmentMap
> m_aTimeFormatterAdjustmentMaps
;
274 std::vector
<WidgetAdjustmentMap
> m_aDateFormatterAdjustmentMaps
;
275 std::vector
<WidgetAdjustmentMap
> m_aScrollAdjustmentMaps
;
276 std::vector
<WidgetAdjustmentMap
> m_aSliderAdjustmentMaps
;
278 std::map
<OString
, Adjustment
> m_aAdjustments
;
280 std::vector
<ButtonImageWidgetMap
> m_aButtonImageWidgetMaps
;
281 StockMap m_aStockMap
;
283 std::vector
<ButtonMenuMap
> m_aButtonMenuMaps
;
285 std::map
<VclPtr
<vcl::Window
>, VclPtr
<vcl::Window
>> m_aRedundantParentWidgets
;
287 std::vector
<SizeGroup
> m_aSizeGroups
;
291 std::vector
<MnemonicWidgetMap
> m_aMnemonicWidgetMaps
;
293 std::vector
< VclPtr
<VclExpander
> > m_aExpanderWidgets
;
295 std::vector
< VclPtr
<MessageDialog
> > m_aMessageDialogs
;
297 sal_uInt16 m_nLastToolbarId
;
299 sal_uInt16 m_nLastMenuItemId
;
306 ResHookProc
const m_pStringReplace
;
307 VclPtr
<vcl::Window
> m_pParent
;
308 bool m_bToplevelHasDeferredInit
;
309 bool m_bToplevelHasDeferredProperties
;
310 bool m_bToplevelParentFound
;
311 bool const m_bLegacy
;
312 std::unique_ptr
<ParserState
> m_pParserState
;
314 vcl::Window
*get_by_name(const OString
& sID
);
315 void delete_by_name(const OString
& sID
);
317 class sortIntoBestTabTraversalOrder
320 sortIntoBestTabTraversalOrder(VclBuilder
*pBuilder
)
321 : m_pBuilder(pBuilder
) {}
323 bool operator()(const vcl::Window
*pA
, const vcl::Window
*pB
) const;
326 VclBuilder
* const m_pBuilder
;
329 /// XFrame to be able to extract labels and other properties of the UNO commands (like of .uno:Bold).
330 css::uno::Reference
<css::frame::XFrame
> m_xFrame
;
332 DECL_LINK(ResponseHdl
, ::Button
*, void);
334 VclPtr
<vcl::Window
> insertObject(vcl::Window
*pParent
,
335 const OString
&rClass
, const OString
&rID
,
336 stringmap
&rProps
, stringmap
&rPangoAttributes
,
337 stringmap
&rAtkProps
);
339 VclPtr
<vcl::Window
> makeObject(vcl::Window
*pParent
,
340 const OString
&rClass
, const OString
&rID
,
343 void connectNumericFormatterAdjustment(const OString
&id
, const OUString
&rAdjustment
);
344 void connectFormattedFormatterAdjustment(const OString
&id
, const OUString
&rAdjustment
);
345 void connectTimeFormatterAdjustment(const OString
&id
, const OUString
&rAdjustment
);
346 void connectDateFormatterAdjustment(const OString
&id
, const OUString
&rAdjustment
);
348 void extractGroup(const OString
&id
, stringmap
&rVec
);
349 void extractModel(const OString
&id
, stringmap
&rVec
);
350 void extractBuffer(const OString
&id
, stringmap
&rVec
);
351 static bool extractAdjustmentToMap(const OString
&id
, stringmap
&rVec
, std::vector
<WidgetAdjustmentMap
>& rAdjustmentMap
);
352 void extractButtonImage(const OString
&id
, stringmap
&rMap
, bool bRadio
);
353 void extractStock(const OString
&id
, stringmap
&rMap
);
354 void extractMnemonicWidget(const OString
&id
, stringmap
&rMap
);
356 void handleChild(vcl::Window
*pParent
, xmlreader::XmlReader
&reader
);
357 VclPtr
<vcl::Window
> handleObject(vcl::Window
*pParent
, xmlreader::XmlReader
&reader
);
358 void handlePacking(vcl::Window
*pCurrent
, vcl::Window
*pParent
, xmlreader::XmlReader
&reader
);
359 static std::vector
<vcl::EnumContext::Context
> handleStyle(xmlreader::XmlReader
&reader
, int &nPriority
);
360 static OString
getStyleClass(xmlreader::XmlReader
&reader
);
361 void applyPackingProperty(vcl::Window
*pCurrent
, vcl::Window
*pParent
, xmlreader::XmlReader
&reader
);
362 void collectProperty(xmlreader::XmlReader
&reader
, stringmap
&rVec
) const;
363 static void collectPangoAttribute(xmlreader::XmlReader
&reader
, stringmap
&rMap
);
364 static void collectAtkRelationAttribute(xmlreader::XmlReader
&reader
, stringmap
&rMap
);
365 static void collectAtkRoleAttribute(xmlreader::XmlReader
&reader
, stringmap
&rMap
);
366 static void collectAccelerator(xmlreader::XmlReader
&reader
, accelmap
&rMap
);
368 void insertMenuObject(
371 const OString
&rClass
,
376 void handleMenuChild(Menu
*pParent
, xmlreader::XmlReader
&reader
);
377 void handleMenuObject(Menu
*pParent
, xmlreader::XmlReader
&reader
);
379 void handleListStore(xmlreader::XmlReader
&reader
, const OString
&rID
, const OString
&rClass
);
380 void handleRow(xmlreader::XmlReader
&reader
, const OString
&rID
);
381 void handleTabChild(vcl::Window
*pParent
, xmlreader::XmlReader
&reader
);
382 VclPtr
<Menu
> handleMenu(xmlreader::XmlReader
&reader
, const OString
&rID
, bool bMenuBar
);
383 std::vector
<ComboBoxTextItem
> handleItems(xmlreader::XmlReader
&reader
) const;
385 void handleSizeGroup(xmlreader::XmlReader
&reader
);
387 void handleAtkObject(xmlreader::XmlReader
&reader
, vcl::Window
*pWindow
);
389 void handleActionWidget(xmlreader::XmlReader
&reader
);
391 PackingData
get_window_packing_data(const vcl::Window
*pWindow
) const;
392 void set_window_packing_position(const vcl::Window
*pWindow
, sal_Int32 nPosition
);
394 static vcl::Window
* prepareWidgetOwnScrolling(vcl::Window
*pParent
, WinBits
&rWinStyle
);
395 void cleanupWidgetOwnScrolling(vcl::Window
*pScrollParent
, vcl::Window
*pWindow
, stringmap
&rMap
);
397 void set_response(const OString
& sID
, short nResponse
);
399 OString
get_by_window(const vcl::Window
*pWindow
) const;
400 void delete_by_window(vcl::Window
*pWindow
);
403 namespace BuilderUtils
405 //apply the properties of rProps to pWindow
406 VCL_DLLPUBLIC
void set_properties(vcl::Window
*pWindow
, const VclBuilder::stringmap
&rProps
);
408 //Convert _ gtk markup to ~ vcl markup
409 VCL_DLLPUBLIC OUString
convertMnemonicMarkup(const OUString
&rIn
);
411 VCL_DLLPUBLIC OUString
extractCustomProperty(VclBuilder::stringmap
&rMap
);
412 VCL_DLLPUBLIC FieldUnit
detectUnit(OUString
const&);
414 VCL_DLLPUBLIC
bool extractDropdown(VclBuilder::stringmap
&rMap
);
416 //add a default value of 25 width-chars to a map if width-chars not set
417 VCL_DLLPUBLIC
void ensureDefaultWidthChars(VclBuilder::stringmap
&rMap
);
419 //Helpers to retrofit all the existing code to the builder
420 VCL_DLLPUBLIC
void reorderWithinParent(std::vector
< vcl::Window
*>& rChilds
, bool bIsButtonBox
);
421 VCL_DLLPUBLIC
void reorderWithinParent(vcl::Window
&rWindow
, sal_uInt16 nNewPosition
);
423 //Convert an accessibility role name to accessibility role number
424 VCL_DLLPUBLIC sal_Int16
getRoleFromName(const OString
& roleName
);
427 template <typename T
>
428 inline T
* VclBuilder::get(VclPtr
<T
>& ret
, const OString
& sID
)
430 vcl::Window
*w
= get_by_name(sID
);
431 SAL_WARN_IF(!w
, "vcl.layout", "widget \"" << sID
<< "\" not found in .ui");
432 SAL_WARN_IF(!dynamic_cast<T
*>(w
),
433 "vcl.layout", ".ui widget \"" << sID
<< "\" needs to correspond to vcl type " << typeid(T
).name());
435 assert(dynamic_cast<T
*>(w
));
436 ret
= static_cast<T
*>(w
);
440 //sID may not exist, but must be of type T if it does
441 template <typename T
/*= vcl::Window if we had c++11*/>
442 inline T
* VclBuilder::get(const OString
& sID
)
444 vcl::Window
*w
= get_by_name(sID
);
445 SAL_WARN_IF(w
&& !dynamic_cast<T
*>(w
),
446 "vcl.layout", ".ui widget \"" << sID
<< "\" needs to correspond to vcl type " << typeid(T
).name());
447 assert(!w
|| dynamic_cast<T
*>(w
));
448 return static_cast<T
*>(w
);
452 //helper baseclass to ease retro fitting dialogs/tabpages that load a resource
453 //to load a .ui file instead
455 //vcl requires the Window Children of a Parent Window to be destroyed before
456 //the Parent Window. VclBuilderContainer owns the VclBuilder which owns the
457 //Children Window. So the VclBuilderContainer dtor must be called before
458 //the Parent Window dtor.
460 //i.e. class Dialog : public SystemWindow, public VclBuilderContainer
461 //not class Dialog : public VclBuilderContainer, public SystemWindow
463 //With the new 'dispose' framework, it is necessary to force the builder
464 //dispose before the Window dispose; so a Dialog::dispose() method would
465 //finish: disposeBuilder(); SystemWindow::dispose() to capture this ordering.
467 class VCL_DLLPUBLIC VclBuilderContainer
470 VclBuilderContainer();
471 virtual ~VclBuilderContainer();
472 void disposeBuilder();
474 static OUString
getUIRootDir();
476 template <typename T
> T
* get(VclPtr
<T
>& ret
, const OString
& sID
)
478 return m_pUIBuilder
->get
<T
>(ret
, sID
);
480 template <typename T
/*= vcl::Window if we had c++11*/> T
* get(const OString
& sID
)
482 return m_pUIBuilder
->get
<T
>(sID
);
484 void setDeferredProperties()
488 m_pUIBuilder
->setDeferredProperties();
490 OString
getUIFile() const
494 return m_pUIBuilder
->getUIFile();
501 std::unique_ptr
<VclBuilder
> m_pUIBuilder
;
503 friend class ::SalInstanceBuilder
;
504 friend class ::ScreenshotTest
;
508 * @return true if rValue is "True", "true", "1", etc.
510 bool VCL_DLLPUBLIC
toBool(const OUString
&rValue
);
514 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */