tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / include / svtools / valueset.hxx
blob2244dfc805af9a3ed9283085390c8243107c28d2
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/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #pragma once
22 #include <svtools/svtdllapi.h>
23 #include <vcl/wintypes.hxx>
24 #include <vcl/customweld.hxx>
25 #include <vcl/image.hxx>
26 #include <rtl/ref.hxx>
27 #include <memory>
28 #include <vector>
30 class MouseEvent;
31 class TrackingEvent;
32 class HelpEvent;
33 class KeyEvent;
34 class DataChangedEvent;
35 class ScrollBar;
36 class UserDrawEvent;
37 class VirtualDevice;
38 class ValueSetAcc;
40 struct ValueSetItem;
42 enum class DrawFrameStyle;
44 /*************************************************************************
46 Description
47 ============
49 class ValueSet
51 This class allows the selection of an item. In the process items are
52 drawn side by side. The selection of items can be more clear than in a
53 ListBox shape for example in case of colors or samples.
54 The amount of columns drawn by the control and whether the items
55 should be encircled can be specified. Optional a NoSelection or name
56 field could be shown. By default image and color items are supported.
57 Items could be drawn by oneself if InsertItem() is only called with
58 an ID. To achieve this the UserDraw handler needs to be overridden. The
59 description text could be specified afterwards in case of UserDraw
60 and any other items.
62 Cross references
64 class ListBox
66 --------------------------------------------------------------------------
68 WinBits
70 WB_FLATVALUESET Flat Look (if you set WB_ITEMBORDER or WB_DOUBLEBORDER,
71 then you get extra border space, but the Borders
72 aren't painted),
73 WB_ITEMBORDER Items will be bordered
74 WB_DOUBLEBORDER Items will be bordered twice. Additionally WB_ITEMBORDER
75 has to be set, as otherwise this WinBit wouldn't have any
76 effect. It is needed if there are items with a white
77 background, since otherwise the 3D effect wouldn't be
78 recognizable.
79 WB_NAMEFIELD There is a namefield, where the name of an item will be
80 shown.
81 WB_NONEFIELD There is a NoSelection field which can be selected if
82 0 is passed along with SelectItem. Respectively
83 GetSelectedItemId() returns 0 if this field or nothing
84 is selected. This field shows the text which is specified
85 by SetText() respectively no one, if no text was set. With
86 SetNoSelection() the selection can be disabled.
87 WB_VSCROLL A scrollbar will be always shown. The visible number of
88 lines have to be specified with SetLineCount() if this
89 flag is set.
90 WB_BORDER A border will be drawn around the window.
91 WB_TABSTOP It is possible to jump into the ValueSet with the tab key.
92 WB_NOTABSTOP It is not possible to jump into the ValueSet with the
93 tab key.
94 WB_NO_DIRECTSELECT Cursor travelling doesn't call select immediately. To
95 execute the selection <RETURN> has to be pressed.
96 --------------------------------------------------------------------------
98 The number of columns must be either set with SetColCount() or
99 SetItemWidth(). If the number of columns is specified by SetColCount()
100 the width of the items will be calculated by the visible range.
101 If the items should have a static width, it has to be specified
102 with SetItemWidth(). In this case the number of columns will be calculated
103 by the visible range.
105 The number of rows is given by the number of items / number of columns. The
106 number of visible rows must either specified by SetLineCount() or
107 SetItemWidth(). If the number of visible rows is specified by SetLineCount(),
108 the height of the items will be calculated from the visible height. If the
109 items should have a fixed height it has to be specified with SetItemHeight().
110 In this case the number of visible rows is then calculated from the visible
111 height. If the number of visible rows is neither specified by SetLineCount()
112 nor by SetItemHeight() all rows will be shown. The height of the items will
113 be calculated by the visible height. If the number of visible rows is
114 specified by SetLineCount() or SetItemHeight() ValueSet does scroll
115 automatically when more lines are available, as are visible. If scrolling
116 should be also possible with a ScrollBar WB_VSCROLL needs to be set.
118 The distance between the items can be increased by SetExtraSpacing(). The
119 distance, which will be shown between two items (both in x and in y), is
120 measured in pixels.
122 The exact window size for a specific item size can be calculated by
123 CalcWindowSizePixel(). To do this all relevant data (number of columns/...)
124 have to be specified and if no number of rows was set, all items need to
125 be inserted. If the window was created with WB_BORDER/Border=sal_True the
126 size has to be specified with SetOutputSizePixel(). In other cases different
127 size-methods can be used. With CalcItemSize() the inner and outer size of
128 an item could be calculated (for this the free space defined by
129 SetExtraSpacing() will not be included).
131 The background color could be specified by SetColor(), with which the image
132 or UserDraw items will be underlaid. If no color is specified the color
133 of other windows (WindowColor) will be used for the background.
135 --------------------------------------------------------------------------
137 At first all items should be inserted and only then Show() should be called
138 since the output area will be precomputed. If this is not done the first
139 Paint will appear a little bit slower. Therefore the Control, if it is loaded
140 from the resource and only supplied with items during runtime, should be
141 loaded with Hide = sal_True and then displayed with Show().
143 In case of a visible Control the creation of the new output area could be
144 activated before Paint by calling Format().
146 --------------------------------------------------------------------------
148 If Drag and Drop will be called from the ValueSet the Command-Handler has to
149 be overridden. From this StartDrag needs to be called. If this method returns
150 sal_True the drag-process could be initiated by ExecuteDrag(), otherwise no
151 processing will take place. This method makes sure that ValueSet stops its
152 processing and as appropriate selects the entry. Therefore the calling of
153 Select-Handler within this function must be expected.
155 For dropping QueryDrop() and Drop() need to be overridden and ShowDropPos()
156 and HideDropPos() should be called within these methods.
157 To show the insertion point ShowDropPos() has to be called within the
158 QueryDrop-Handler. ShowDropPos() also scrolls the ValueSet if the passed
159 position is located at the window border. Furthermore ShowDropPos() returns
160 the position, at which the item should be inserted respectively which
161 insertion point was shown. If no insertion point was determined
162 VALUESET_ITEM_NOTFOUND will be returned. If the window was left during dragging
163 or the drag process is terminated HideDropPos() should be called in any case.
165 --------------------------------------------------------------------------
167 This class is currently still in the SV-Tools. That's why the ValueSet needs
168 to be loaded as a Control out of the resource and the desired WinBits have
169 to be set (before Show) with SetStyle().
171 *************************************************************************/
173 typedef std::vector<std::unique_ptr<ValueSetItem>> ValueItemList;
175 #define WB_ITEMBORDER (WinBits(0x00010000))
176 #define WB_DOUBLEBORDER (WinBits(0x00020000))
177 #define WB_NAMEFIELD (WinBits(0x00040000))
178 #define WB_NONEFIELD (WinBits(0x00080000))
179 #define WB_FLATVALUESET (WinBits(0x02000000))
180 #define WB_NO_DIRECTSELECT (WinBits(0x04000000))
181 #define WB_MENUSTYLEVALUESET (WinBits(0x08000000))
183 #define VALUESET_APPEND (size_t(-1))
184 #define VALUESET_ITEM_NOTFOUND (size_t(-1))
186 class SVT_DLLPUBLIC ValueSet : public weld::CustomWidgetController
188 private:
189 ScopedVclPtr<VirtualDevice> maVirDev;
190 rtl::Reference<ValueSetAcc> mxAccessible;
191 ValueItemList mItemList;
192 std::unique_ptr<ValueSetItem> mpNoneItem;
193 std::unique_ptr<weld::ScrolledWindow> mxScrolledWindow;
194 tools::Rectangle maNoneItemRect;
195 tools::Rectangle maItemListRect;
196 tools::Long mnItemWidth;
197 tools::Long mnItemHeight;
198 tools::Long mnTextOffset;
199 tools::Long mnVisLines;
200 tools::Long mnLines;
201 tools::Long mnUserItemWidth;
202 tools::Long mnUserItemHeight;
203 sal_uInt16 mnSelItemId;
204 int mnSavedItemId;
205 sal_uInt16 mnHighItemId;
206 sal_uInt16 mnCols;
207 sal_uInt16 mnCurCol;
208 sal_uInt16 mnUserCols;
209 sal_uInt16 mnUserVisLines;
210 sal_uInt16 mnFirstLine;
211 sal_uInt16 mnSpacing;
212 sal_uInt16 mnMargin;
213 DrawFrameStyle mnFrameStyle;
214 Color maColor;
215 OUString maText;
216 WinBits mnStyle;
217 Link<ValueSet*,void> maDoubleClickHdl;
218 Link<ValueSet*,void> maSelectHdl;
220 bool mbFormat : 1;
221 bool mbHighlight : 1;
222 bool mbNoSelection : 1;
223 bool mbDoubleSel : 1;
224 bool mbScroll : 1;
225 bool mbFullMode : 1;
226 bool mbEdgeBlending : 1;
227 bool mbHasVisibleItems : 1;
229 friend class ValueItemAcc;
230 friend class ValueSetAcc;
232 SVT_DLLPRIVATE void ImplDeleteItems();
233 SVT_DLLPRIVATE void ImplFormatItem(vcl::RenderContext const & rRenderContext, ValueSetItem* pItem, tools::Rectangle aRect);
234 SVT_DLLPRIVATE void ImplDrawItemText(vcl::RenderContext& rRenderContext, const OUString& rStr);
235 // nItemId is the item to draw selected, but if nothing is selected something else may be drawn as selected instead, the item to draw
236 // selected is returned
237 SVT_DLLPRIVATE ValueSetItem* ImplGetDrawSelectItem(sal_uInt16 nItemId, const bool bFocus, tools::Rectangle& rRect);
238 SVT_DLLPRIVATE void ImplDrawSelect(vcl::RenderContext& rRenderContext,
239 const tools::Rectangle& rRect, const ValueSetItem* pItem,
240 const bool bFocus, const bool bDrawSel,
241 const bool bSelected, const bool bHover);
242 SVT_DLLPRIVATE void ImplDrawSelect(vcl::RenderContext& rRenderContext);
243 SVT_DLLPRIVATE void ImplHighlightItem(sal_uInt16 nItemId);
244 SVT_DLLPRIVATE void ImplDraw(vcl::RenderContext& rRenderContext);
245 SVT_DLLPRIVATE size_t ImplGetItem( const Point& rPoint ) const;
246 SVT_DLLPRIVATE ValueSetItem* ImplGetItem( size_t nPos );
247 SVT_DLLPRIVATE ValueSetItem* ImplGetFirstItem();
248 SVT_DLLPRIVATE sal_uInt16 ImplGetVisibleItemCount() const;
249 SVT_DLLPRIVATE void ImplInsertItem( std::unique_ptr<ValueSetItem> pItem, const size_t nPos );
250 SVT_DLLPRIVATE tools::Rectangle ImplGetItemRect( size_t nPos ) const;
251 SVT_DLLPRIVATE void ImplFireAccessibleEvent( short nEventId, const css::uno::Any& rOldValue, const css::uno::Any& rNewValue );
252 SVT_DLLPRIVATE bool ImplHasAccessibleListeners() const;
253 SVT_DLLPRIVATE void ImplTracking(bool bLeaveWindow, const Point& rPos);
254 SVT_DLLPRIVATE void QueueReformat();
255 SVT_DLLPRIVATE void SetFirstLine(sal_uInt16 nNewFirstLine); // set mnFirstLine and update scrollbar to match
256 SVT_DLLPRIVATE void RecalcScrollBar();
257 SVT_DLLPRIVATE bool TurnOffScrollBar();
258 SVT_DLLPRIVATE void TurnOnScrollBar();
259 DECL_DLLPRIVATE_LINK(ImplScrollHdl, weld::ScrolledWindow&, void);
261 Size GetLargestItemSize();
263 ValueSet (const ValueSet &) = delete;
264 ValueSet & operator= (const ValueSet &) = delete;
266 protected:
267 virtual css::uno::Reference<css::accessibility::XAccessible> CreateAccessible() override;
269 public:
270 ValueSet(std::unique_ptr<weld::ScrolledWindow> pScrolledWindow);
271 virtual ~ValueSet() override;
273 virtual void SetDrawingArea(weld::DrawingArea* pDrawingArea) override;
275 virtual bool MouseButtonDown( const MouseEvent& rMEvt ) override;
276 virtual bool MouseButtonUp( const MouseEvent& rMEvt ) override;
277 virtual bool MouseMove( const MouseEvent& rMEvt ) override;
278 virtual bool KeyInput( const KeyEvent& rKEvt ) override;
279 virtual void Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle& rRect) override;
280 virtual void GetFocus() override;
281 virtual void LoseFocus() override;
282 virtual void Resize() override;
283 virtual void StyleUpdated() override;
284 virtual void Show() override;
285 virtual void Hide() override;
286 virtual OUString RequestHelp(tools::Rectangle& rHelpRect) override;
288 virtual void Select();
289 virtual void UserDraw( const UserDrawEvent& rUDEvt );
291 OUString const & GetText() const { return maText; }
292 void SetText(const OUString& rText) { maText = rText; }
293 void SetStyle(WinBits nStyle);
294 WinBits GetStyle() const { return mnStyle; }
296 void SetOptimalSize();
298 /// Insert @rImage item.
299 void InsertItem(sal_uInt16 nItemId, const Image& rImage);
300 /// Insert @rImage item with @rStr as either a legend or tooltip depending on @bShowLegend.
301 void InsertItem(sal_uInt16 nItemId, const Image& rImage,
302 const OUString& rStr, size_t nPos = VALUESET_APPEND, bool bShowLegend = false);
303 /// Insert an @rColor item with @rStr tooltip.
304 void InsertItem(sal_uInt16 nItemId, const Color& rColor,
305 const OUString& rStr);
306 /// Insert an User Drawn item.
307 void InsertItem(sal_uInt16 nItemId, size_t nPos = VALUESET_APPEND);
308 /// Insert an User Drawn item with @rStr tooltip.
309 void InsertItem(sal_uInt16 nItemId, const OUString& rStr, size_t nPos);
310 void RemoveItem(sal_uInt16 nItemId);
312 void Clear();
314 size_t GetItemCount() const;
315 size_t GetItemPos( sal_uInt16 nItemId ) const;
316 sal_uInt16 GetItemId( size_t nPos ) const;
317 sal_uInt16 GetItemId( const Point& rPos ) const;
318 tools::Rectangle GetItemRect( sal_uInt16 nItemId ) const;
319 void EnableFullItemMode( bool bFullMode );
321 void SetColCount( sal_uInt16 nNewCols = 1 );
322 sal_uInt16 GetColCount() const
324 return mnUserCols;
326 void SetLineCount( sal_uInt16 nNewLines = 0 );
327 sal_uInt16 GetLineCount() const
329 return mnUserVisLines;
331 void SetItemWidth( tools::Long nItemWidth );
332 void SetItemHeight( tools::Long nLineHeight );
334 void SelectItem( sal_uInt16 nItemId );
335 sal_uInt16 GetSelectedItemId() const
337 return mnSelItemId;
339 size_t GetSelectItemPos() const
341 return GetItemPos( mnSelItemId );
343 bool IsItemSelected( sal_uInt16 nItemId ) const
345 return !mbNoSelection && (nItemId == mnSelItemId);
347 void SetNoSelection();
348 bool IsNoSelection() const
350 return mbNoSelection;
352 sal_uInt16 GetHighlightedItemId() const { return mnHighItemId; }
354 void SetItemImage( sal_uInt16 nItemId, const Image& rImage );
355 Image GetItemImage( sal_uInt16 nItemId ) const;
356 void SetItemColor( sal_uInt16 nItemId, const Color& rColor );
357 Color GetItemColor( sal_uInt16 nItemId ) const;
358 void SetItemData( sal_uInt16 nItemId, void* pData );
359 void* GetItemData( sal_uInt16 nItemId ) const;
360 void SetItemText( sal_uInt16 nItemId, const OUString& rStr );
361 const OUString & GetItemText( sal_uInt16 nItemId ) const;
362 void SetColor( const Color& rColor );
363 void SetColor()
365 SetColor(COL_TRANSPARENT);
367 bool IsColor() const
369 return !maColor.IsTransparent();
372 void SetExtraSpacing( sal_uInt16 nNewSpacing );
373 void SetMargin( sal_uInt16 nNewMargin );
375 void Format(vcl::RenderContext const & rRenderContext);
376 void SetFormat();
378 Size CalcWindowSizePixel(const Size& rItemSize,
379 sal_uInt16 nCalcCols = 0,
380 sal_uInt16 nCalcLines = 0) const;
381 Size CalcItemSizePixel(const Size& rSize) const;
382 int GetScrollWidth() const;
384 void SetSelectHdl(const Link<ValueSet*,void>& rLink)
386 maSelectHdl = rLink;
389 void SetDoubleClickHdl(const Link<ValueSet*,void>& rLink)
391 maDoubleClickHdl = rLink;
394 bool GetEdgeBlending() const
396 return mbEdgeBlending;
398 void SetEdgeBlending(bool bNew);
400 void SaveValue()
402 mnSavedItemId = IsNoSelection() ? -1 : GetSelectedItemId();
405 bool IsValueChangedFromSaved() const
407 int nItemId = IsNoSelection() ? -1 : GetSelectedItemId();
408 return mnSavedItemId != nItemId;
411 virtual FactoryFunction GetUITestFactory() const override;
414 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */