LibreOffice Module vcl (master) 1
builder.hxx
Go to the documentation of this file.
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 INCLUDED_VCL_BUILDER_HXX
11#define INCLUDED_VCL_BUILDER_HXX
12
13#include <typeinfo>
14#include <sal/log.hxx>
15#include <unotools/resmgr.hxx>
16#include <tools/fldunit.hxx>
17#include <vcl/dllapi.h>
18#include <utility>
19#include <vcl/window.hxx>
20#include <vcl/vclptr.hxx>
21#include <vcl/toolboxid.hxx>
22#include <vcl/wintypes.hxx>
23#include <vcl/EnumContext.hxx>
24
25#include <memory>
26#include <map>
27#include <string_view>
28#include <vector>
29#ifdef check
30# //some problem with MacOSX and a check define
31# undef check
32#endif
33
34class Button;
35class ComboBox;
36class FormattedField;
37class ListBox;
38class Menu;
39class MessageDialog;
40class NumericFormatter;
41class PopupMenu;
43class ScreenshotTest;
44class ScrollBar;
45class SvTabListBox;
46class Slider;
47class DateField;
48class TimeField;
49class ToolBox;
50class VclExpander;
53namespace xmlreader { class XmlReader; }
54namespace com::sun::star::frame { class XFrame; }
55
57{
58 OUString m_sItem;
59 OUString m_sId;
60 ComboBoxTextItem(OUString sItem, OUString sId)
61 : m_sItem(std::move(sItem))
62 , m_sId(std::move(sId))
63 {
64 }
65};
66
69{
70public:
71 typedef std::map<OUString, OUString> stringmap;
72 typedef std::map<OUString, std::pair<OUString, OUString>> accelmap;
74 typedef void (*customMakeWidget)(VclPtr<vcl::Window> &rRet, const VclPtr<vcl::Window> &pParent, stringmap &rVec);
75
76public:
77 VclBuilder(vcl::Window* pParent, const OUString& sUIRootDir, const OUString& sUIFile,
78 OUString sID = {},
79 css::uno::Reference<css::frame::XFrame> xFrame
80 = css::uno::Reference<css::frame::XFrame>(),
81 bool bLegacy = true,
82 const NotebookBarAddonsItem* pNotebookBarAddonsItem = nullptr);
85 void disposeBuilder();
86 //sID must exist and be of type T
87 template <typename T> T* get(VclPtr<T>& ret, const OUString& sID);
88
89 //sID may not exist, but must be of type T if it does
90 template <typename T = vcl::Window> T* get(const OUString& sID);
91
92 vcl::Window* get_widget_root();
93
94 //sID may not exist
95 PopupMenu* get_menu(std::u16string_view sID);
96
97 //release ownership of pWindow, i.e. don't delete it
98 void drop_ownership(const vcl::Window *pWindow);
99
100 //see m_aDeferredProperties, you need this for toplevel dialogs
101 //which build themselves from their ctor. The properties on
102 //the top level are stored in m_aDeferredProperties and need
103 //to be applied post ctor
104 void setDeferredProperties();
105
107 const OUString& getUIFile() const
108 {
109 return m_sHelpRoot;
110 }
111
112 static SymbolType mapStockToSymbol(std::u16string_view icon_name);
113
114private:
115 VclBuilder(const VclBuilder&) = delete;
116 VclBuilder& operator=(const VclBuilder&) = delete;
117
118 // owner for ListBox/ComboBox UserData
119 std::vector<std::unique_ptr<OUString>> m_aUserData;
120
121 //If the toplevel window has any properties which need to be set on it,
122 //but the toplevel is the owner of the builder, then its ctor
123 //has not been completed during the building, so properties for it
124 //are collected here and need to be set afterwards, e.g. during
125 //Show or Execute
127
128 std::unique_ptr<NotebookBarAddonsItem> m_pNotebookBarAddonsItem;
129
131 {
133 sal_Int32 m_nPosition;
134 PackingData(bool bVerticalOrient = false)
135 : m_bVerticalOrient(bVerticalOrient)
136 , m_nPosition(-1)
137 {
138 }
139 };
140
141 struct WinAndId
142 {
143 OUString m_sID;
146 WinAndId(OUString sId, vcl::Window *pWindow, bool bVertical)
147 : m_sID(std::move(sId))
148 , m_pWindow(pWindow)
149 , m_aPackingData(bVertical)
150 {
151 }
152 };
153 std::vector<WinAndId> m_aChildren;
154
156 {
157 OUString m_sID;
159 MenuAndId(OUString sId, Menu *pMenu);
160 };
161 std::vector<MenuAndId> m_aMenus;
162
164 {
165 OUString m_sID;
166 OUString m_sValue;
167 StringPair(OUString sId, OUString sValue)
168 : m_sID(std::move(sId))
169 , m_sValue(std::move(sValue))
170 {
171 }
172 };
173
175 {
176 OUString m_sID;
177 OUString m_sValue;
178 UStringPair(OUString sId, OUString sValue)
179 : m_sID(std::move(sId))
180 , m_sValue(std::move(sValue))
181 {
182 }
183 };
184
186
188 {
189 OUString m_sID;
190 OUString m_sValue;
192 ButtonImageWidgetMap(OUString sId, OUString sValue, bool bRadio)
193 : m_sID(std::move(sId))
194 , m_sValue(std::move(sValue))
195 , m_bRadio(bRadio)
196 {
197 }
198 };
199
204
206 {
207 OUString m_sID;
208 OUString m_sValue;
209 sal_Int32 m_nActiveId;
210 ComboBoxModelMap(OUString sId, OUString sValue, sal_Int32 nActiveId)
211 : m_sID(std::move(sId))
212 , m_sValue(std::move(sValue))
213 , m_nActiveId(nActiveId)
214 {
215 }
216 };
217
219 {
220 typedef std::vector<OUString> row;
221 std::vector<row> m_aEntries;
222 };
223
224 const ListStore* get_model_by_name(const OUString& sID) const;
225 void mungeModel(ListBox &rTarget, const ListStore &rStore, sal_uInt16 nActiveId);
226 void mungeModel(ComboBox &rTarget, const ListStore &rStore, sal_uInt16 nActiveId);
227 void mungeModel(SvTabListBox &rTarget, const ListStore &rStore, sal_uInt16 nActiveId);
228
230 const TextBuffer* get_buffer_by_name(const OUString& sID) const;
231
232 static void mungeTextBuffer(VclMultiLineEdit &rTarget, const TextBuffer &rTextBuffer);
233
235 const Adjustment* get_adjustment_by_name(const OUString& sID) const;
236
237 static void mungeAdjustment(NumericFormatter &rTarget, const Adjustment &rAdjustment);
238 static void mungeAdjustment(FormattedField &rTarget, const Adjustment &rAdjustment);
239 static void mungeAdjustment(ScrollBar &rTarget, const Adjustment &rAdjustment);
240 static void mungeAdjustment(Slider &rTarget, const Adjustment &rAdjustment);
241
242 typedef std::map<OUString, int> ImageSizeMap;
243
245 {
246 std::vector<OUString> m_aWidgets;
249 };
250
251
253 {
254 std::locale m_aResLocale;
255
256 std::vector<RadioButtonGroupMap> m_aGroupMaps;
257
258 std::vector<ComboBoxModelMap> m_aModelMaps;
259 std::map<OUString, ListStore> m_aModels;
260
261 std::vector<TextBufferMap> m_aTextBufferMaps;
262 std::map<OUString, TextBuffer> m_aTextBuffers;
263
264 std::vector<WidgetAdjustmentMap> m_aNumericFormatterAdjustmentMaps;
265 std::vector<WidgetAdjustmentMap> m_aFormattedFormatterAdjustmentMaps;
266 std::vector<WidgetAdjustmentMap> m_aScrollAdjustmentMaps;
267 std::vector<WidgetAdjustmentMap> m_aSliderAdjustmentMaps;
268
269 std::map<OUString, Adjustment> m_aAdjustments;
270
271 std::vector<ButtonImageWidgetMap> m_aButtonImageWidgetMaps;
273
274 std::vector<ButtonMenuMap> m_aButtonMenuMaps;
275
276 std::map<VclPtr<vcl::Window>, VclPtr<vcl::Window>> m_aRedundantParentWidgets;
277
278 std::vector<SizeGroup> m_aSizeGroups;
279
280 std::map<VclPtr<vcl::Window>, stringmap> m_aAtkInfo;
281
282 std::vector<MnemonicWidgetMap> m_aMnemonicWidgetMaps;
283
284 std::vector< VclPtr<VclExpander> > m_aExpanderWidgets;
285
286 std::vector< VclPtr<MessageDialog> > m_aMessageDialogs;
287
289
291
292 ParserState();
293 };
294
295 OUString m_sID;
296 OUString m_sHelpRoot;
303 std::unique_ptr<ParserState> m_pParserState;
304
305 vcl::Window *get_by_name(std::u16string_view sID);
306 void delete_by_name(const OUString& sID);
307
309 {
310 public:
312 : m_pBuilder(pBuilder) {}
313
314 bool operator()(const vcl::Window *pA, const vcl::Window *pB) const;
315
316 private:
318 };
319
321 css::uno::Reference<css::frame::XFrame> m_xFrame;
322
323private:
324 VclPtr<vcl::Window> insertObject(vcl::Window *pParent,
325 const OUString &rClass, const OUString &rID,
326 stringmap &rProps, stringmap &rPangoAttributes,
327 stringmap &rAtkProps);
328
329 VclPtr<vcl::Window> makeObject(vcl::Window *pParent,
330 const OUString &rClass, const OUString &rID,
331 stringmap &rVec);
332
333 void connectNumericFormatterAdjustment(const OUString &id, const OUString &rAdjustment);
334 void connectFormattedFormatterAdjustment(const OUString &id, const OUString &rAdjustment);
335
336 static int getImageSize(const stringmap &rMap);
337
338 void extractGroup(const OUString &id, stringmap &rVec);
339 void extractModel(const OUString &id, stringmap &rVec);
340 void extractBuffer(const OUString &id, stringmap &rVec);
341 static bool extractAdjustmentToMap(const OUString &id, stringmap &rVec, std::vector<WidgetAdjustmentMap>& rAdjustmentMap);
342 void extractButtonImage(const OUString &id, stringmap &rMap, bool bRadio);
343 void extractMnemonicWidget(const OUString &id, stringmap &rMap);
344
345 // either pParent or pAtkProps must be set, pParent for a child of a widget, pAtkProps for
346 // collecting the atk info for a GtkMenuItem
347 void handleChild(vcl::Window *pParent, stringmap *pAtkProps, xmlreader::XmlReader &reader);
348 VclPtr<vcl::Window> handleObject(vcl::Window *pParent, stringmap *pAtkProps, xmlreader::XmlReader &reader);
349 void handlePacking(vcl::Window *pCurrent, vcl::Window *pParent, xmlreader::XmlReader &reader);
350 static std::vector<vcl::EnumContext::Context> handleStyle(xmlreader::XmlReader &reader, int &nPriority);
351 static OUString getStyleClass(xmlreader::XmlReader &reader);
352 void applyPackingProperty(vcl::Window *pCurrent, vcl::Window *pParent, xmlreader::XmlReader &reader);
353 void collectProperty(xmlreader::XmlReader &reader, stringmap &rVec) const;
354 static void collectPangoAttribute(xmlreader::XmlReader &reader, stringmap &rMap);
355 static void collectAtkRelationAttribute(xmlreader::XmlReader &reader, stringmap &rMap);
356 static void collectAtkRoleAttribute(xmlreader::XmlReader &reader, stringmap &rMap);
357 static void collectAccelerator(xmlreader::XmlReader &reader, accelmap &rMap);
358
359 void insertMenuObject(
360 Menu *pParent,
361 PopupMenu *pSubMenu,
362 const OUString &rClass,
363 const OUString &rID,
364 stringmap &rProps,
365 stringmap &rAtkProps,
366 accelmap &rAccels);
367
368 void handleMenuChild(Menu *pParent, xmlreader::XmlReader &reader);
369 void handleMenuObject(Menu *pParent, xmlreader::XmlReader &reader);
370
371 void handleListStore(xmlreader::XmlReader &reader, const OUString &rID, std::u16string_view rClass);
372 void handleRow(xmlreader::XmlReader &reader, const OUString &rID);
373 void handleTabChild(vcl::Window *pParent, xmlreader::XmlReader &reader);
374 VclPtr<Menu> handleMenu(xmlreader::XmlReader &reader, const OUString &rID, bool bMenuBar);
375 std::vector<ComboBoxTextItem> handleItems(xmlreader::XmlReader &reader) const;
376
377 void handleSizeGroup(xmlreader::XmlReader &reader);
378
379 stringmap handleAtkObject(xmlreader::XmlReader &reader) const;
380
381 static void applyAtkProperties(vcl::Window *pWindow, const stringmap& rProperties);
382
383 void handleActionWidget(xmlreader::XmlReader &reader);
384
385 PackingData get_window_packing_data(const vcl::Window *pWindow) const;
386 void set_window_packing_position(const vcl::Window *pWindow, sal_Int32 nPosition);
387
388 static vcl::Window* prepareWidgetOwnScrolling(vcl::Window *pParent, WinBits &rWinStyle);
389 void cleanupWidgetOwnScrolling(vcl::Window *pScrollParent, vcl::Window *pWindow, stringmap &rMap);
390
391 void set_response(std::u16string_view sID, short nResponse);
392
393 OUString get_by_window(const vcl::Window *pWindow) const;
394 void delete_by_window(vcl::Window *pWindow);
395};
396
397namespace BuilderUtils
398{
399 //apply the properties of rProps to pWindow
401
402 //Convert _ gtk markup to ~ vcl markup
403 VCL_DLLPUBLIC OUString convertMnemonicMarkup(std::u16string_view rIn);
404
406
408
409 //add a default value of 25 width-chars to a map if width-chars not set
411
412 //Helpers to retrofit all the existing code to the builder
413 VCL_DLLPUBLIC void reorderWithinParent(std::vector< vcl::Window*>& rChilds, bool bIsButtonBox);
414 VCL_DLLPUBLIC void reorderWithinParent(vcl::Window &rWindow, sal_uInt16 nNewPosition);
415
416 //Convert an accessibility role name to accessibility role number
417 VCL_DLLPUBLIC sal_Int16 getRoleFromName(const OUString& roleName);
418}
419
420template <typename T>
421inline T* VclBuilder::get(VclPtr<T>& ret, const OUString& sID)
422{
423 vcl::Window *w = get_by_name(sID);
424 SAL_WARN_IF(!w, "vcl.layout", "widget \"" << sID << "\" not found in .ui");
425 SAL_WARN_IF(!dynamic_cast<T*>(w),
426 "vcl.layout", ".ui widget \"" << sID << "\" needs to correspond to vcl type " << typeid(T).name());
427 assert(w);
428 assert(dynamic_cast<T*>(w));
429 ret = static_cast<T*>(w);
430 return ret.get();
431}
432
433//sID may not exist, but must be of type T if it does
434template <typename T>
435inline T* VclBuilder::get(const OUString& sID)
436{
437 vcl::Window *w = get_by_name(sID);
438 SAL_WARN_IF(w && !dynamic_cast<T*>(w),
439 "vcl.layout", ".ui widget \"" << sID << "\" needs to correspond to vcl type " << typeid(T).name());
440 assert(!w || dynamic_cast<T*>(w));
441 return static_cast<T*>(w);
442}
443
444/*
445 * @return true if rValue is "True", "true", "1", etc.
446 */
447VCL_DLLPUBLIC bool toBool(std::u16string_view rValue);
448
449#endif
450
451/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Int32 m_nPosition
VCL_DLLPUBLIC bool toBool(std::u16string_view rValue)
Definition: layout.cxx:1415
A widget used to choose from a list of items and which has an entry.
Definition: combobox.hxx:39
A widget used to choose from a list of items and which has no entry.
Definition: lstbox.hxx:83
Definition: menu.hxx:116
A toolbar: contains all those icons, typically below the menu bar.
Definition: toolbox.hxx:74
sortIntoBestTabTraversalOrder(VclBuilder *pBuilder)
Definition: builder.hxx:311
Creates a hierarchy of vcl::Windows (widgets) from a .ui file for dialogs, sidebar,...
Definition: builder.hxx:69
T * get(VclPtr< T > &ret, const OUString &sID)
Definition: builder.hxx:421
OUString m_sHelpRoot
Definition: builder.hxx:296
stringmap m_aDeferredProperties
Definition: builder.hxx:126
UStringPair WidgetAdjustmentMap
Definition: builder.hxx:201
VclPtr< vcl::Window > m_pParent
Definition: builder.hxx:298
css::uno::Reference< css::frame::XFrame > m_xFrame
XFrame to be able to extract labels and other properties of the UNO commands (like of ....
Definition: builder.hxx:321
VclBuilder(const VclBuilder &)=delete
bool m_bToplevelHasDeferredInit
Definition: builder.hxx:299
std::vector< WinAndId > m_aChildren
Definition: builder.hxx:153
const OUString & getUIFile() const
return UI-File name (without '.ui')
Definition: builder.hxx:107
bool m_bToplevelHasDeferredProperties
Definition: builder.hxx:300
std::vector< std::unique_ptr< OUString > > m_aUserData
Definition: builder.hxx:119
ResHookProc m_pStringReplace
Definition: builder.hxx:297
bool m_bToplevelParentFound
Definition: builder.hxx:301
UStringPair ButtonMenuMap
Definition: builder.hxx:202
std::map< OUString, OUString > stringmap
Definition: builder.hxx:71
OUString m_sID
Definition: builder.hxx:295
std::map< OUString, int > ImageSizeMap
Definition: builder.hxx:242
StringPair RadioButtonGroupMap
Definition: builder.hxx:185
std::vector< MenuAndId > m_aMenus
Definition: builder.hxx:161
std::unique_ptr< ParserState > m_pParserState
Definition: builder.hxx:303
VclBuilder & operator=(const VclBuilder &)=delete
UStringPair TextBufferMap
Definition: builder.hxx:200
std::map< OUString, std::pair< OUString, OUString > > accelmap
Definition: builder.hxx:72
UStringPair MnemonicWidgetMap
Definition: builder.hxx:203
bool m_bLegacy
Definition: builder.hxx:302
stringmap Adjustment
Definition: builder.hxx:234
stringmap TextBuffer
Definition: builder.hxx:229
vcl::Window * get_by_name(std::u16string_view sID)
Definition: builder.cxx:4032
std::unique_ptr< NotebookBarAddonsItem > m_pNotebookBarAddonsItem
Definition: builder.hxx:128
reference_type * get() const
Get the body.
Definition: vclptr.hxx:143
#define VCL_DLLPUBLIC
Definition: dllapi.h:29
const char * name
#define SAL_WARN_IF(condition, area, stream)
void ensureDefaultWidthChars(VclBuilder::stringmap &rMap)
Definition: builder.cxx:2279
sal_Int16 getRoleFromName(const OUString &roleName)
Definition: builder.cxx:2331
void set_properties(vcl::Window *pWindow, const VclBuilder::stringmap &rProps)
Definition: builder.cxx:2244
OUString convertMnemonicMarkup(std::u16string_view rIn)
Definition: builder.cxx:2250
bool extractDropdown(VclBuilder::stringmap &rMap)
Definition: builder.cxx:2287
OUString extractCustomProperty(VclBuilder::stringmap &rMap)
Definition: builder.cxx:2267
void reorderWithinParent(vcl::Window &rWindow, sal_uInt16 nNewPosition)
Definition: builder.cxx:2299
css::uno::Reference< css::linguistic2::XProofreadingIterator > get(css::uno::Reference< css::uno::XComponentContext > const &context)
sal_Int32 w
OUString(* ResHookProc)(const OUString &rStr)
OUString m_sItem
Definition: builder.hxx:58
ComboBoxTextItem(OUString sItem, OUString sId)
Definition: builder.hxx:60
OUString m_sId
Definition: builder.hxx:59
ButtonImageWidgetMap(OUString sId, OUString sValue, bool bRadio)
Definition: builder.hxx:192
ComboBoxModelMap(OUString sId, OUString sValue, sal_Int32 nActiveId)
Definition: builder.hxx:210
std::vector< row > m_aEntries
Definition: builder.hxx:221
std::vector< OUString > row
Definition: builder.hxx:220
VclPtr< Menu > m_pMenu
Definition: builder.hxx:158
PackingData(bool bVerticalOrient=false)
Definition: builder.hxx:134
ToolBoxItemId m_nLastToolbarId
Definition: builder.hxx:288
std::vector< MnemonicWidgetMap > m_aMnemonicWidgetMaps
Definition: builder.hxx:282
std::vector< ButtonImageWidgetMap > m_aButtonImageWidgetMaps
Definition: builder.hxx:271
std::map< OUString, TextBuffer > m_aTextBuffers
Definition: builder.hxx:262
std::map< OUString, ListStore > m_aModels
Definition: builder.hxx:259
ImageSizeMap m_aImageSizeMap
Definition: builder.hxx:272
std::vector< WidgetAdjustmentMap > m_aScrollAdjustmentMaps
Definition: builder.hxx:266
std::map< OUString, Adjustment > m_aAdjustments
Definition: builder.hxx:269
std::vector< RadioButtonGroupMap > m_aGroupMaps
Definition: builder.hxx:256
std::vector< VclPtr< MessageDialog > > m_aMessageDialogs
Definition: builder.hxx:286
std::vector< SizeGroup > m_aSizeGroups
Definition: builder.hxx:278
std::vector< WidgetAdjustmentMap > m_aFormattedFormatterAdjustmentMaps
Definition: builder.hxx:265
std::vector< TextBufferMap > m_aTextBufferMaps
Definition: builder.hxx:261
std::locale m_aResLocale
Definition: builder.hxx:254
std::map< VclPtr< vcl::Window >, stringmap > m_aAtkInfo
Definition: builder.hxx:280
std::vector< WidgetAdjustmentMap > m_aSliderAdjustmentMaps
Definition: builder.hxx:267
sal_uInt16 m_nLastMenuItemId
Definition: builder.hxx:290
std::vector< ButtonMenuMap > m_aButtonMenuMaps
Definition: builder.hxx:274
std::map< VclPtr< vcl::Window >, VclPtr< vcl::Window > > m_aRedundantParentWidgets
Definition: builder.hxx:276
std::vector< ComboBoxModelMap > m_aModelMaps
Definition: builder.hxx:258
std::vector< WidgetAdjustmentMap > m_aNumericFormatterAdjustmentMaps
Definition: builder.hxx:264
std::vector< VclPtr< VclExpander > > m_aExpanderWidgets
Definition: builder.hxx:284
std::vector< OUString > m_aWidgets
Definition: builder.hxx:246
stringmap m_aProperties
Definition: builder.hxx:247
StringPair(OUString sId, OUString sValue)
Definition: builder.hxx:167
UStringPair(OUString sId, OUString sValue)
Definition: builder.hxx:178
WinAndId(OUString sId, vcl::Window *pWindow, bool bVertical)
Definition: builder.hxx:146
PackingData m_aPackingData
Definition: builder.hxx:145
VclPtr< vcl::Window > m_pWindow
Definition: builder.hxx:144
Reference< XFrame > xFrame
weld::Builder * m_pBuilder
OUString sId
SymbolType
Definition: vclenum.hxx:74
sal_Int64 WinBits
Definition: wintypes.hxx:109