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/.
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 .
22 #include <sal/config.h>
24 #include <o3tl/safeint.hxx>
25 #include <vcl/virdev.hxx>
26 #include <vcl/timer.hxx>
27 #include <vcl/idle.hxx>
28 #include <vcl/vclptr.hxx>
29 #include <tools/debug.hxx>
30 #include <vcl/svtaccessiblefactory.hxx>
31 #include <vcl/toolkit/ivctrl.hxx>
32 #include <vcl/toolkit/scrbar.hxx>
41 class SvtIconChoiceCtrl
;
42 class SvxIconChoiceCtrlEntry
;
43 class IcnViewEdit_Impl
;
44 class IcnGridMap_Impl
;
49 #define PAINTFLAG_HOR_CENTERED 0x0001
50 #define PAINTFLAG_VER_CENTERED 0x0002
52 enum class IconChoiceFlags
{
55 SelectingRect
= 0x0002,
57 DownDeselect
= 0x0008,
58 EntryListPosValid
= 0x0010,
59 ClearingSelection
= 0x0020,
63 template<> struct typed_flags
<IconChoiceFlags
> : is_typed_flags
<IconChoiceFlags
, 0x007f> {};
67 // distances from window borders
68 #define LROFFS_WINBORDER 4
69 #define TBOFFS_WINBORDER 4
70 // for the bounding rectangle
71 #define LROFFS_BOUND 2
72 #define TBOFFS_BOUND 2
73 // distance icon to text
74 #define HOR_DIST_BMP_STRING 3
75 #define VER_DIST_BMP_STRING 3
76 // width offset of highlight rectangle for Text
79 #define DEFAULT_MAX_VIRT_WIDTH 200
80 #define DEFAULT_MAX_VIRT_HEIGHT 200
82 #define VIEWMODE_MASK (WB_ICON | WB_SMALLICON | WB_DETAILS)
85 enum class IcnViewFieldType
92 // Data about the focus of entries
96 tools::Rectangle aRect
;
101 typedef sal_uLong GridId
;
103 #define GRID_NOT_FOUND (GridId(ULONG_MAX))
105 // Implementation-class of IconChoiceCtrl
108 typedef std::map
<sal_uInt16
, std::unique_ptr
<SvxIconChoiceCtrlColumnInfo
>> SvxIconChoiceCtrlColumnInfoMap
;
109 typedef std::vector
<SvxIconChoiceCtrlEntry
*> SvxIconChoiceCtrlEntryPtrVec
;
111 class SvxIconChoiceCtrl_Impl
113 friend class IcnCursor_Impl
;
114 friend class IcnGridMap_Impl
;
116 std::vector
< std::unique_ptr
<SvxIconChoiceCtrlEntry
> > maEntries
;
117 VclPtr
<ScrollBar
> aVerSBar
;
118 VclPtr
<ScrollBar
> aHorSBar
;
119 VclPtr
<ScrollBarBox
> aScrBarBox
;
120 tools::Rectangle aCurSelectionRect
;
121 std::vector
<tools::Rectangle
> aSelectedRectList
;
122 Idle aAutoArrangeIdle
;
123 Idle aDocRectChangedIdle
;
124 Idle aVisRectChangedIdle
;
125 Idle aCallSelectHdlIdle
;
126 Size aVirtOutputSize
;
128 Size aDefaultTextSize
;
129 Size aOutputSize
; // Pixel
130 VclPtr
<SvtIconChoiceCtrl
> pView
;
131 std::unique_ptr
<IcnCursor_Impl
> pImpCursor
;
132 std::unique_ptr
<IcnGridMap_Impl
> pGridMap
;
133 tools::Long nMaxVirtWidth
; // max. width aVirtOutputSize for ALIGN_TOP
134 tools::Long nMaxVirtHeight
; // max. height aVirtOutputSize for ALIGN_LEFT
135 std::vector
< SvxIconChoiceCtrlEntry
* > maZOrderList
;
136 std::unique_ptr
<SvxIconChoiceCtrlColumnInfoMap
> m_pColumns
;
138 tools::Long nMaxBoundHeight
; // height of highest BoundRects
139 IconChoiceFlags nFlags
;
140 DrawTextFlags nCurTextDrawFlags
;
141 ImplSVEvent
* nUserEventAdjustScrBars
;
142 SvxIconChoiceCtrlEntry
* pCurHighlightFrame
;
143 bool bHighlightFramePressed
;
144 SvxIconChoiceCtrlEntry
* pHead
= nullptr; // top left entry
145 SvxIconChoiceCtrlEntry
* pCursor
;
146 SvxIconChoiceCtrlEntry
* pHdlEntry
;
147 SvxIconChoiceCtrlEntry
* pAnchor
; // for selection
148 LocalFocus aFocus
; // Data for focusrect
149 ::vcl::AccessibleFactoryAccess aAccFactory
;
151 SvxIconChoiceCtrlTextMode eTextMode
;
152 SelectionMode eSelectionMode
;
153 sal_Int32 nSelectionCount
;
154 SvxIconChoiceCtrlPositionMode ePositionMode
;
155 bool bBoundRectsDirty
;
158 void ShowCursor( bool bShow
);
160 void ImpArrange( bool bKeepPredecessors
);
161 void AdjustVirtSize( const tools::Rectangle
& );
162 void ResetVirtSize();
163 void CheckScrollBars();
165 DECL_LINK( ScrollUpDownHdl
, ScrollBar
*, void );
166 DECL_LINK( ScrollLeftRightHdl
, ScrollBar
*, void );
167 DECL_LINK( UserEventHdl
, void*, void );
168 DECL_LINK( AutoArrangeHdl
, Timer
*, void );
169 DECL_LINK( DocRectChangedHdl
, Timer
*, void );
170 DECL_LINK( VisRectChangedHdl
, Timer
*, void );
171 DECL_LINK( CallSelectHdlHdl
, Timer
*, void );
173 void AdjustScrollBars();
174 void PositionScrollBars( tools::Long nRealWidth
, tools::Long nRealHeight
);
175 static tools::Long
GetScrollBarPageSize( tools::Long nVisibleRange
)
177 return ((nVisibleRange
*75)/100);
179 tools::Long
GetScrollBarLineSize() const
181 return nMaxBoundHeight
/ 2;
183 bool HandleScrollCommand( const CommandEvent
& rCmd
);
184 void ToDocPos( Point
& rPosPixel
)
186 rPosPixel
-= pView
->GetMapMode().GetOrigin();
188 void InitScrollBarBox();
189 void ToggleSelection( SvxIconChoiceCtrlEntry
* );
190 void DeselectAllBut( SvxIconChoiceCtrlEntry
const * );
191 void Center( SvxIconChoiceCtrlEntry
* pEntry
) const;
192 void CallSelectHandler();
194 SvxIconChoiceCtrlEntry
* pEntry1
,
195 SvxIconChoiceCtrlEntry
* pEntry2
,
197 std::vector
<tools::Rectangle
>* pOtherRects
201 SvxIconChoiceCtrlEntry
const * pStart
,
202 SvxIconChoiceCtrlEntry
const * pEnd
,
206 void AddSelectedRect( const tools::Rectangle
& );
207 void AddSelectedRect(
208 SvxIconChoiceCtrlEntry
* pEntry1
,
209 SvxIconChoiceCtrlEntry
* pEntry2
212 void ClearSelectedRectList();
213 tools::Rectangle
CalcMaxTextRect( const SvxIconChoiceCtrlEntry
* pEntry
) const;
215 void ClipAtVirtOutRect( tools::Rectangle
& rRect
) const;
216 GridId
GetPredecessorGrid( const Point
& rDocPos
) const;
218 void InitPredecessors();
219 void ClearPredecessors();
221 bool CheckVerScrollBar();
222 bool CheckHorScrollBar();
223 void CancelUserEvents();
225 SvxIconChoiceCtrlEntry
* pEntry
,
228 void RepaintSelectedEntries();
229 void SetListPositions();
230 void SetDefaultTextSize();
231 bool IsAutoArrange() const
233 return (ePositionMode
== SvxIconChoiceCtrlPositionMode::AutoArrange
);
235 void DocRectChanged() { aDocRectChangedIdle
.Start(); }
236 void VisRectChanged() { aVisRectChangedIdle
.Start(); }
237 void SetOrigin( const Point
& );
239 void ShowFocus ( tools::Rectangle
const & rRect
);
240 void DrawFocusRect(vcl::RenderContext
& rRenderContext
);
242 bool IsMnemonicChar( sal_Unicode cChar
, sal_uLong
& rPos
) const;
244 // Copy assignment is forbidden and not implemented.
245 SvxIconChoiceCtrl_Impl (const SvxIconChoiceCtrl_Impl
&) = delete;
246 SvxIconChoiceCtrl_Impl
& operator= (const SvxIconChoiceCtrl_Impl
&) = delete;
252 tools::Long nHorSBarHeight
;
253 tools::Long nVerSBarWidth
;
255 SvxIconChoiceCtrl_Impl( SvtIconChoiceCtrl
* pView
, WinBits nWinStyle
);
256 ~SvxIconChoiceCtrl_Impl();
258 void SetSelectionMode(SelectionMode eMode
)
260 eSelectionMode
= eMode
;
263 void Clear( bool bInCtor
);
264 void SetStyle( WinBits nWinStyle
);
265 WinBits
GetStyle() const { return nWinBits
; }
266 void InsertEntry( std::unique_ptr
<SvxIconChoiceCtrlEntry
>, size_t nPos
);
267 void RemoveEntry( size_t nPos
);
271 SvxIconChoiceCtrlEntry
*,
273 bool bAddToSelection
= false
275 void Paint(vcl::RenderContext
& rRenderContext
, const tools::Rectangle
& rRect
);
276 bool MouseButtonDown( const MouseEvent
& );
277 bool MouseButtonUp( const MouseEvent
& );
278 bool MouseMove( const MouseEvent
&);
279 bool RequestHelp( const HelpEvent
& rHEvt
);
281 SvxIconChoiceCtrlEntry
* pOldCursor
,
282 SvxIconChoiceCtrlEntry
* pNewCursor
,
286 bool KeyInput( const KeyEvent
& );
290 void SetUpdateMode( bool bUpdate
);
291 bool GetUpdateMode() const { return bUpdateMode
; }
292 void PaintEntry(SvxIconChoiceCtrlEntry
*, const Point
&, vcl::RenderContext
& rRenderContext
);
295 SvxIconChoiceCtrlEntry
* pEntry
,
299 void InvalidateEntry( SvxIconChoiceCtrlEntry
* );
301 void SetNoSelection();
303 SvxIconChoiceCtrlEntry
* GetCurEntry() const { return pCursor
; }
304 void SetCursor( SvxIconChoiceCtrlEntry
* );
306 SvxIconChoiceCtrlEntry
* GetEntry( const Point
& rDocPos
, bool bHit
= false );
308 void MakeEntryVisible( SvxIconChoiceCtrlEntry
* pEntry
, bool bBound
= true );
311 bool bKeepPredecessors
,
312 tools::Long nSetMaxVirtWidth
,
313 tools::Long nSetMaxVirtHeight
316 tools::Rectangle
CalcFocusRect( SvxIconChoiceCtrlEntry
* );
317 tools::Rectangle
CalcBmpRect( SvxIconChoiceCtrlEntry
*, const Point
* pPos
= nullptr );
318 tools::Rectangle
CalcTextRect(
319 SvxIconChoiceCtrlEntry
*,
320 const Point
* pPos
= nullptr,
321 const OUString
* pStr
= nullptr
324 tools::Long
CalcBoundingWidth() const;
325 tools::Long
CalcBoundingHeight() const;
326 Size
CalcBoundingSize() const;
327 void FindBoundingRect( SvxIconChoiceCtrlEntry
* pEntry
);
328 void SetBoundingRect_Impl(
329 SvxIconChoiceCtrlEntry
* pEntry
,
331 const Size
& rBoundingSize
333 // recalculates all invalid BoundRects
334 void RecalcAllBoundingRectsSmart();
335 const tools::Rectangle
& GetEntryBoundRect( SvxIconChoiceCtrlEntry
* );
336 void InvalidateBoundingRect( tools::Rectangle
& rRect
)
338 rRect
.SetRight(LONG_MAX
);
339 bBoundRectsDirty
= true;
341 static bool IsBoundingRectValid( const tools::Rectangle
& rRect
) { return ( rRect
.Right() != LONG_MAX
); }
343 static void PaintEmphasis(const tools::Rectangle
& rRect1
, bool bSelected
,
344 vcl::RenderContext
& rRenderContext
);
346 void PaintItem(const tools::Rectangle
& rRect
, IcnViewFieldType eItem
, SvxIconChoiceCtrlEntry
* pEntry
,
347 sal_uInt16 nPaintFlags
, vcl::RenderContext
& rRenderContext
);
349 // recalculates all BoundingRects if bMustRecalcBoundingRects == true
350 void CheckBoundingRects() { if (bBoundRectsDirty
) RecalcAllBoundingRectsSmart(); }
351 void Command( const CommandEvent
& rCEvt
);
352 void ToTop( SvxIconChoiceCtrlEntry
* );
354 sal_Int32
GetSelectionCount() const;
355 void SetGrid( const Size
& );
356 Size
GetMinGrid() const;
357 void Scroll( tools::Long nDeltaX
, tools::Long nDeltaY
);
358 const Size
& GetItemSize( IcnViewFieldType
) const;
363 std::vector
<tools::Rectangle
>* pSelectedRectList
,
364 const tools::Rectangle
& rEntryBoundRect
368 const tools::Rectangle
&,
370 std::vector
<tools::Rectangle
>* pOtherRects
374 const tools::Rectangle
& rDocPos
,
375 bool bInScrollBarEvent
=false
379 void SetEntryTextMode(
380 SvxIconChoiceCtrlTextMode
,
381 SvxIconChoiceCtrlEntry
* pEntry
384 size_t GetEntryCount() const { return maEntries
.size(); }
385 SvxIconChoiceCtrlEntry
* GetEntry( size_t nPos
)
387 return maEntries
[ nPos
].get();
389 SvxIconChoiceCtrlEntry
* GetEntry( size_t nPos
) const
391 return maEntries
[ nPos
].get();
393 SvxIconChoiceCtrlEntry
* GetFirstSelectedEntry() const;
394 sal_Int32
GetEntryListPos( SvxIconChoiceCtrlEntry
const * ) const;
396 tools::Rectangle
GetOutputRect() const;
398 void SetEntryPredecessor(SvxIconChoiceCtrlEntry
* pEntry
,SvxIconChoiceCtrlEntry
* pPredecessor
);
399 // only delivers valid results when in AutoArrange mode!
400 SvxIconChoiceCtrlEntry
* FindEntryPredecessor( SvxIconChoiceCtrlEntry
* pEntry
, const Point
& );
402 void SetPositionMode( SvxIconChoiceCtrlPositionMode
);
404 void SetColumn( sal_uInt16 nIndex
, const SvxIconChoiceCtrlColumnInfo
& );
405 const SvxIconChoiceCtrlColumnInfo
* GetColumn( sal_uInt16 nIndex
) const;
407 void SetEntryHighlightFrame(
408 SvxIconChoiceCtrlEntry
* pEntry
,
409 bool bKeepHighlightFlags
411 void DrawHighlightFrame(vcl::RenderContext
& rRenderContext
, const tools::Rectangle
& rBmpRect
);
413 void CallEventListeners( VclEventId nEvent
, void* pData
);
415 ::vcl::IAccessibleFactory
& GetAccessibleFactory()
417 return aAccFactory
.getFactory();
421 typedef std::map
<sal_uInt16
, SvxIconChoiceCtrlEntryPtrVec
> IconChoiceMap
;
425 SvxIconChoiceCtrl_Impl
* pView
;
426 std::unique_ptr
<IconChoiceMap
> xColumns
;
427 std::unique_ptr
<IconChoiceMap
> xRows
;
432 SvxIconChoiceCtrlEntry
* pCurEntry
;
435 void Create() { if( !xColumns
) ImplCreate(); }
437 sal_uInt16
GetSortListPos(
438 SvxIconChoiceCtrlEntryPtrVec
& rList
,
441 SvxIconChoiceCtrlEntry
* SearchCol(
449 SvxIconChoiceCtrlEntry
* SearchRow(
458 explicit IcnCursor_Impl( SvxIconChoiceCtrl_Impl
* pOwner
);
462 // for Cursortravelling etc.
463 SvxIconChoiceCtrlEntry
* GoLeftRight( SvxIconChoiceCtrlEntry
*, bool bRight
);
464 SvxIconChoiceCtrlEntry
* GoUpDown( SvxIconChoiceCtrlEntry
*, bool bDown
);
465 SvxIconChoiceCtrlEntry
* GoPageUpDown( SvxIconChoiceCtrlEntry
*, bool bDown
);
468 class IcnGridMap_Impl
470 tools::Rectangle _aLastOccupiedGrid
;
471 SvxIconChoiceCtrl_Impl
* _pView
;
472 std::unique_ptr
<bool[]> _pGridMap
;
473 sal_uInt16 _nGridCols
, _nGridRows
;
477 void Create() { if(!_pGridMap
) Create_Impl(); }
479 void GetMinMapSize( sal_uInt16
& rDX
, sal_uInt16
& rDY
) const;
482 explicit IcnGridMap_Impl(SvxIconChoiceCtrl_Impl
* pView
);
487 GridId
GetGrid( const Point
& rDocPos
);
488 GridId
GetGrid( sal_uInt16 nGridX
, sal_uInt16 nGridY
);
489 GridId
GetUnoccupiedGrid();
491 void OccupyGrids( const SvxIconChoiceCtrlEntry
* );
492 void OccupyGrid( GridId nId
)
494 DBG_ASSERT(!_pGridMap
|| nId
<o3tl::make_unsigned(_nGridCols
*_nGridRows
),"OccupyGrid: Bad GridId");
495 if(_pGridMap
&& nId
< o3tl::make_unsigned(_nGridCols
*_nGridRows
) )
496 _pGridMap
[ nId
] = true;
499 tools::Rectangle
GetGridRect( GridId
);
500 void GetGridCoord( GridId
, sal_uInt16
& rGridX
, sal_uInt16
& rGridY
);
501 static sal_uLong
GetGridCount(
502 const Size
& rSizePixel
,
503 sal_uInt16 nGridWidth
,
504 sal_uInt16 nGridHeight
507 void OutputSizeChanged();
510 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */