xref: /core/vcl/source/window/toolbox.cxx (revision 33111d13)
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  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include <vcl/toolbox.hxx>
21 #include <vcl/event.hxx>
22 #include <vcl/decoview.hxx>
23 #include <vcl/toolkit/floatwin.hxx>
24 #include <vcl/svapp.hxx>
25 #include <vcl/help.hxx>
26 #include <vcl/mnemonic.hxx>
27 #include <vcl/gradient.hxx>
28 #include <vcl/layout.hxx>
29 #include <vcl/menu.hxx>
30 #include <vcl/settings.hxx>
31 #include <vcl/ptrstyle.hxx>
32 #include <bitmaps.hlst>
33 #include <toolbarvalue.hxx>
34 
35 #include <tools/poly.hxx>
36 #include <sal/log.hxx>
37 #include <o3tl/string_view.hxx>
38 #include <osl/diagnose.h>
39 
40 #include <accel.hxx>
41 #include <svdata.hxx>
42 #include <window.h>
43 #include <toolbox.h>
44 #include <spin.hxx>
45 #if defined(_WIN32)
46 #include <svsys.h>
47 #endif
48 
49 #include <cstdlib>
50 #include <map>
51 #include <string_view>
52 #include <vector>
53 #include <math.h>
54 
55 #include "impldockingwrapper.hxx"
56 
57 #define SMALLBUTTON_HSIZE           7
58 #define SMALLBUTTON_VSIZE           7
59 
60 #define SMALLBUTTON_OFF_NORMAL_X    3
61 #define SMALLBUTTON_OFF_NORMAL_Y    3
62 
63 #define TB_TEXTOFFSET           2
64 #define TB_IMAGETEXTOFFSET      3
65 #define TB_LINESPACING          3
66 #define TB_SPIN_SIZE            14
67 #define TB_SPIN_OFFSET          2
68 #define TB_BORDER_OFFSET1       4
69 #define TB_BORDER_OFFSET2       2
70 #define TB_MAXLINES             5
71 #define TB_MAXNOSCROLL          32765
72 
73 #define TB_DRAGWIDTH            8  // the default width of the drag grip
74 
75 #define TB_CALCMODE_HORZ        1
76 #define TB_CALCMODE_VERT        2
77 #define TB_CALCMODE_FLOAT       3
78 
79 #define TB_WBLINESIZING         (WB_SIZEABLE | WB_DOCKABLE | WB_SCROLL)
80 
81 #define DOCK_LINEHSIZE          (sal_uInt16(0x0001))
82 #define DOCK_LINEVSIZE          (sal_uInt16(0x0002))
83 #define DOCK_LINERIGHT          (sal_uInt16(0x1000))
84 #define DOCK_LINEBOTTOM         (sal_uInt16(0x2000))
85 #define DOCK_LINELEFT           (sal_uInt16(0x4000))
86 #define DOCK_LINETOP            (sal_uInt16(0x8000))
87 #define DOCK_LINEOFFSET         3
88 
89 class ImplTBDragMgr
90 {
91 private:
92     VclPtr<ToolBox> mpDragBox;
93     Point           maMouseOff;
94     tools::Rectangle       maRect;
95     tools::Rectangle       maStartRect;
96     Accelerator     maAccel;
97     sal_uInt16      mnLineMode;
98     ToolBox::ImplToolItems::size_type mnStartLines;
99 
100     ImplTBDragMgr(const ImplTBDragMgr&) = delete;
101     ImplTBDragMgr& operator=(const ImplTBDragMgr&) = delete;
102 
103 public:
104                     ImplTBDragMgr();
105 
106     void            StartDragging( ToolBox* pDragBox, const Point& rPos, const tools::Rectangle& rRect, sal_uInt16 nLineMode );
107     void            Dragging( const Point& rPos );
108     void            EndDragging( bool bOK = true );
109     DECL_LINK( SelectHdl, Accelerator&, void );
110 };
111 
112 
ImplGetTBDragMgr()113 static ImplTBDragMgr* ImplGetTBDragMgr()
114 {
115     ImplSVData* pSVData = ImplGetSVData();
116     if ( !pSVData->maCtrlData.mpTBDragMgr )
117         pSVData->maCtrlData.mpTBDragMgr = new ImplTBDragMgr;
118     return pSVData->maCtrlData.mpTBDragMgr;
119 }
120 
ImplGetDragWidth(const vcl::Window & rWindow,bool bHorz)121 int ToolBox::ImplGetDragWidth( const vcl::Window& rWindow, bool bHorz )
122 {
123     return ImplGetDragWidth(*rWindow.GetOutDev(), bHorz);
124 }
ImplGetDragWidth(const vcl::RenderContext & rRenderContext,bool bHorz)125 int ToolBox::ImplGetDragWidth( const vcl::RenderContext& rRenderContext, bool bHorz )
126 {
127     int nWidth = TB_DRAGWIDTH;
128     if( rRenderContext.IsNativeControlSupported( ControlType::Toolbar, ControlPart::Entire ) )
129     {
130 
131         ImplControlValue aControlValue;
132         tools::Rectangle aContent, aBound;
133         tools::Rectangle aArea( Point(), rRenderContext.GetOutputSizePixel() );
134 
135         if ( rRenderContext.GetNativeControlRegion(ControlType::Toolbar,
136                 bHorz ? ControlPart::ThumbVert : ControlPart::ThumbHorz,
137                 aArea, ControlState::NONE, aControlValue, aBound, aContent) )
138         {
139             nWidth = bHorz ? aContent.GetWidth() : aContent.GetHeight();
140         }
141     }
142 
143     // increase the hit area of the drag handle according to DPI scale factor
144     nWidth *= rRenderContext.GetDPIScaleFactor();
145 
146     return nWidth;
147 }
148 
ImplGetDragWidth() const149 int ToolBox::ImplGetDragWidth() const
150 {
151     return ToolBox::ImplGetDragWidth( *this, mbHorz );
152 }
153 
determineButtonType(ImplToolItem const * pItem,ButtonType defaultType)154 static ButtonType determineButtonType( ImplToolItem const * pItem, ButtonType defaultType )
155 {
156     ButtonType tmpButtonType = defaultType;
157     ToolBoxItemBits nBits = pItem->mnBits & ( ToolBoxItemBits::TEXT_ONLY | ToolBoxItemBits::ICON_ONLY );
158     if ( nBits != ToolBoxItemBits::NONE ) // item has custom setting
159     {
160         tmpButtonType = ButtonType::SYMBOLTEXT;
161         if ( nBits == ToolBoxItemBits::TEXT_ONLY )
162             tmpButtonType = ButtonType::TEXT;
163         else if ( nBits == ToolBoxItemBits::ICON_ONLY )
164             tmpButtonType = ButtonType::SYMBOLONLY;
165     }
166     return tmpButtonType;
167 }
168 
ImplUpdateDragArea() const169 void ToolBox::ImplUpdateDragArea() const
170 {
171     ImplDockingWindowWrapper *pWrapper = ImplGetDockingManager()->GetDockingWindowWrapper( this );
172     if( pWrapper )
173     {
174         if ( ImplIsFloatingMode() || pWrapper->IsLocked() )
175             pWrapper->SetDragArea( tools::Rectangle() );
176         else
177         {
178             if( meAlign == WindowAlign::Top || meAlign == WindowAlign::Bottom )
179                 pWrapper->SetDragArea( tools::Rectangle( 0, 0, ImplGetDragWidth(), GetOutputSizePixel().Height() ) );
180             else
181                 pWrapper->SetDragArea( tools::Rectangle( 0, 0, GetOutputSizePixel().Width(), ImplGetDragWidth() ) );
182         }
183     }
184 }
185 
ImplCalcBorder(WindowAlign eAlign,tools::Long & rLeft,tools::Long & rTop,tools::Long & rRight,tools::Long & rBottom) const186 void ToolBox::ImplCalcBorder( WindowAlign eAlign, tools::Long& rLeft, tools::Long& rTop,
187                               tools::Long& rRight, tools::Long& rBottom ) const
188 {
189     if( ImplIsFloatingMode() || !(mnWinStyle & WB_BORDER) )
190     {
191         // no border in floating mode
192         rLeft = rTop = rRight = rBottom = 0;
193         return;
194     }
195 
196     ImplDockingWindowWrapper *pWrapper = ImplGetDockingManager()->GetDockingWindowWrapper( this );
197 
198     // reserve DragArea only for dockable toolbars
199     int dragwidth = ( pWrapper && !pWrapper->IsLocked() ) ? ImplGetDragWidth() : 0;
200 
201     // no shadow border for dockable toolbars and toolbars with WB_NOSHADOW bit set, e.g. Calc's formulabar
202     int borderwidth = ( pWrapper || mnWinStyle & WB_NOSHADOW ) ? 0 : 2;
203 
204     if ( eAlign == WindowAlign::Top )
205     {
206         rLeft   = borderwidth+dragwidth;
207         rTop    = borderwidth;
208         rRight  = borderwidth;
209         rBottom = 0;
210     }
211     else if ( eAlign == WindowAlign::Left )
212     {
213         rLeft   = borderwidth;
214         rTop    = borderwidth+dragwidth;
215         rRight  = 0;
216         rBottom = borderwidth;
217     }
218     else if ( eAlign == WindowAlign::Bottom )
219     {
220         rLeft   = borderwidth+dragwidth;
221         rTop    = 0;
222         rRight  = borderwidth;
223         rBottom = borderwidth;
224     }
225     else
226     {
227         rLeft   = 0;
228         rTop    = borderwidth+dragwidth;
229         rRight  = borderwidth;
230         rBottom = borderwidth;
231     }
232 }
233 
ImplCheckUpdate()234 void ToolBox::ImplCheckUpdate()
235 {
236     // remove any pending invalidates to avoid
237     // have them triggered when paint is locked (see mpData->mbIsPaintLocked)
238     // which would result in erasing the background only and not painting any items
239     // this must not be done when we're already in Paint()
240 
241     // this is only required for transparent toolbars (see ImplDrawTransparentBackground() )
242     if( !IsBackground() && HasPaintEvent() && !IsInPaint() )
243         PaintImmediately();
244 }
245 
ImplDrawGrip(vcl::RenderContext & rRenderContext,const tools::Rectangle & aDragArea,int nDragWidth,WindowAlign eAlign,bool bHorz)246 void ToolBox::ImplDrawGrip(vcl::RenderContext& rRenderContext,
247         const tools::Rectangle &aDragArea, int nDragWidth, WindowAlign eAlign, bool bHorz)
248 {
249     bool bNativeOk = false;
250     const ControlPart ePart = bHorz ? ControlPart::ThumbVert : ControlPart::ThumbHorz;
251     const Size aSz( rRenderContext.GetOutputSizePixel() );
252     if (rRenderContext.IsNativeControlSupported(ControlType::Toolbar, ePart))
253     {
254         ToolbarValue aToolbarValue;
255         aToolbarValue.maGripRect = aDragArea;
256 
257         tools::Rectangle aCtrlRegion(Point(), aSz);
258 
259         bNativeOk = rRenderContext.DrawNativeControl( ControlType::Toolbar, ePart,
260                                         aCtrlRegion, ControlState::ENABLED, aToolbarValue, OUString() );
261     }
262 
263     if( bNativeOk )
264         return;
265 
266     const StyleSettings& rStyleSettings = rRenderContext.GetSettings().GetStyleSettings();
267     rRenderContext.SetLineColor(rStyleSettings.GetShadowColor());
268     rRenderContext.SetFillColor(rStyleSettings.GetShadowColor());
269 
270     float fScaleFactor = rRenderContext.GetDPIScaleFactor();
271 
272     if (eAlign == WindowAlign::Top || eAlign == WindowAlign::Bottom)
273     {
274         int height = static_cast<int>(0.6 * aSz.Height() + 0.5);
275         int i = (aSz.Height() - height) / 2;
276         height += i;
277         while (i <= height)
278         {
279             int x = nDragWidth / 2;
280             rRenderContext.DrawEllipse(tools::Rectangle(Point(x, i), Size(2 * fScaleFactor, 2 * fScaleFactor)));
281             i += 4 * fScaleFactor;
282         }
283     }
284     else
285     {
286         int width = static_cast<int>(0.6 * aSz.Width() + 0.5);
287         int i = (aSz.Width() - width) / 2;
288         width += i;
289         while (i <= width)
290         {
291             int y = nDragWidth / 2;
292             rRenderContext.DrawEllipse(tools::Rectangle(Point(i, y), Size(2 * fScaleFactor, 2 * fScaleFactor)));
293             i += 4 * fScaleFactor;
294         }
295     }
296 }
297 
ImplDrawGrip(vcl::RenderContext & rRenderContext)298 void ToolBox::ImplDrawGrip(vcl::RenderContext& rRenderContext)
299 {
300     ImplDockingWindowWrapper *pWrapper = ImplGetDockingManager()->GetDockingWindowWrapper(this);
301     if( pWrapper && !pWrapper->GetDragArea().IsEmpty() )
302     {
303         // execute pending paint requests
304         ImplCheckUpdate();
305         ImplDrawGrip( rRenderContext, pWrapper->GetDragArea(),
306                       ImplGetDragWidth(), meAlign, mbHorz );
307     }
308 }
309 
ImplDrawGradientBackground(vcl::RenderContext & rRenderContext)310 void ToolBox::ImplDrawGradientBackground(vcl::RenderContext& rRenderContext)
311 {
312     // draw a nice gradient
313 
314     Color startCol, endCol;
315     const StyleSettings rSettings = rRenderContext.GetSettings().GetStyleSettings();
316 
317     startCol = rSettings.GetFaceGradientColor();
318     endCol = rSettings.GetFaceColor();
319     if (rSettings.GetHighContrastMode())
320         // no 'extreme' gradient when high contrast
321         startCol = endCol;
322 
323     Gradient g;
324     g.SetAngle(Degree10(mbHorz ? 0 : 900));
325     g.SetStyle(css::awt::GradientStyle_LINEAR);
326 
327     g.SetStartColor(startCol);
328     g.SetEndColor(endCol);
329 
330     bool bLineColor = rRenderContext.IsLineColor();
331     Color aOldCol = rRenderContext.GetLineColor();
332     rRenderContext.SetLineColor(rRenderContext.GetSettings().GetStyleSettings().GetShadowColor());
333 
334     Size aFullSz(GetOutputSizePixel());
335     Size aLineSz(aFullSz);
336 
337     // use the linesize only when floating
338     // full window height is used when docked (single line)
339     if (ImplIsFloatingMode())
340     {
341         tools::Long nLineSize;
342         if (mbHorz)
343         {
344             nLineSize = mnMaxItemHeight;
345             if (mnWinHeight > mnMaxItemHeight)
346                 nLineSize = mnWinHeight;
347 
348             aLineSz.setHeight( nLineSize );
349         }
350         else
351         {
352             nLineSize = mnMaxItemWidth;
353             aLineSz.setWidth( nLineSize );
354         }
355     }
356 
357     tools::Long nLeft, nTop, nRight, nBottom;
358     ImplCalcBorder(meAlign, nLeft, nTop, nRight, nBottom);
359 
360     Size aTopLineSz(aLineSz);
361     Size aBottomLineSz(aLineSz);
362 
363     if (mnWinStyle & WB_BORDER)
364     {
365         if (mbHorz)
366         {
367             aTopLineSz.AdjustHeight(TB_BORDER_OFFSET2 + nTop );
368             aBottomLineSz.AdjustHeight(TB_BORDER_OFFSET2 + nBottom );
369 
370             if (mnCurLines == 1)
371                 aTopLineSz.AdjustHeight(TB_BORDER_OFFSET2 + nBottom );
372         }
373         else
374         {
375             aTopLineSz.AdjustWidth(TB_BORDER_OFFSET1 + nLeft );
376             aBottomLineSz.AdjustWidth(TB_BORDER_OFFSET1 + nRight );
377 
378             if (mnCurLines == 1)
379                 aTopLineSz.AdjustWidth(TB_BORDER_OFFSET1 + nLeft );
380         }
381     }
382 
383     if (mbLineSpacing)
384     {
385         if (mbHorz)
386         {
387             aLineSz.AdjustHeight(TB_LINESPACING );
388             if (mnCurLines > 1)
389                 aTopLineSz.AdjustHeight(TB_LINESPACING );
390         }
391         else
392         {
393             aLineSz.AdjustWidth(TB_LINESPACING );
394             if (mnCurLines > 1)
395                 aTopLineSz.AdjustWidth(TB_LINESPACING );
396         }
397     }
398 
399     if (mbHorz)
400     {
401         tools::Long y = 0;
402 
403         rRenderContext.DrawGradient(tools::Rectangle(0, y, aTopLineSz.Width(), y + aTopLineSz.Height()), g);
404         y += aTopLineSz.Height();
405 
406         while (y < (mnDY - aBottomLineSz.Height()))
407         {
408             rRenderContext.DrawGradient(tools::Rectangle(0, y, aLineSz.Width(), y + aLineSz.Height()), g);
409             y += aLineSz.Height();
410         }
411 
412         rRenderContext.DrawGradient(tools::Rectangle(0, y, aBottomLineSz.Width(), y + aBottomLineSz.Height()), g);
413     }
414     else
415     {
416         tools::Long x = 0;
417 
418         rRenderContext.DrawGradient(tools::Rectangle(x, 0, x + aTopLineSz.Width(), aTopLineSz.Height()), g);
419         x += aTopLineSz.Width();
420 
421         while (x < (mnDX - aBottomLineSz.Width()))
422         {
423             rRenderContext.DrawGradient(tools::Rectangle(x, 0, x + aLineSz.Width(), aLineSz.Height()), g);
424             x += aLineSz.Width();
425         }
426 
427         rRenderContext.DrawGradient(tools::Rectangle( x, 0, x + aBottomLineSz.Width(), aBottomLineSz.Height()), g);
428     }
429 
430     if( bLineColor )
431         rRenderContext.SetLineColor( aOldCol );
432 
433 }
434 
ImplDrawNativeBackground(vcl::RenderContext & rRenderContext) const435 bool ToolBox::ImplDrawNativeBackground(vcl::RenderContext& rRenderContext) const
436 {
437     // use NWF
438     tools::Rectangle aCtrlRegion(Point(), GetOutputSizePixel());
439 
440     return rRenderContext.DrawNativeControl( ControlType::Toolbar, mbHorz ? ControlPart::DrawBackgroundHorz : ControlPart::DrawBackgroundVert,
441                                     aCtrlRegion, ControlState::ENABLED, ImplControlValue(), OUString() );
442 }
443 
ImplDrawTransparentBackground(const vcl::Region & rRegion)444 void ToolBox::ImplDrawTransparentBackground(const vcl::Region &rRegion)
445 {
446     // just invalidate to trigger paint of the parent
447     const bool bOldPaintLock = mpData->mbIsPaintLocked;
448     mpData->mbIsPaintLocked = true;
449 
450     // send an invalidate to the first opaque parent and invalidate the whole hierarchy from there (noclipchildren)
451     Invalidate(rRegion, InvalidateFlags::Update | InvalidateFlags::NoClipChildren);
452 
453     mpData->mbIsPaintLocked = bOldPaintLock;
454 }
455 
ImplDrawConstantBackground(vcl::RenderContext & rRenderContext,const vcl::Region & rRegion,bool bIsInPopupMode)456 void ToolBox::ImplDrawConstantBackground(vcl::RenderContext& rRenderContext, const vcl::Region &rRegion, bool bIsInPopupMode)
457 {
458     // draw a constant color
459     if (!bIsInPopupMode)
460     {
461         // default background
462         rRenderContext.Erase(rRegion.GetBoundRect());
463     }
464     else
465     {
466         // use different color in popupmode
467         const StyleSettings rSettings = rRenderContext.GetSettings().GetStyleSettings();
468         Wallpaper aWallpaper(rSettings.GetFaceGradientColor());
469         rRenderContext.DrawWallpaper(rRegion.GetBoundRect(), aWallpaper);
470     }
471 }
472 
ImplDrawBackground(vcl::RenderContext & rRenderContext,const tools::Rectangle & rRect)473 void ToolBox::ImplDrawBackground(vcl::RenderContext& rRenderContext, const tools::Rectangle& rRect)
474 {
475     // execute pending paint requests
476     ImplCheckUpdate();
477 
478     ImplDockingWindowWrapper* pWrapper = ImplGetDockingManager()->GetDockingWindowWrapper(this);
479     bool bIsInPopupMode = ImplIsInPopupMode();
480 
481     vcl::Region aPaintRegion(rRect);
482 
483     // make sure we do not invalidate/erase too much
484     if (IsInPaint())
485         aPaintRegion.Intersect(GetOutDev()->GetActiveClipRegion());
486 
487     rRenderContext.Push(vcl::PushFlags::CLIPREGION);
488     rRenderContext.IntersectClipRegion( aPaintRegion );
489 
490     if (!pWrapper)
491     {
492         // no gradient for ordinary toolbars (not dockable)
493         if( !IsBackground() && !IsInPaint() )
494             ImplDrawTransparentBackground(aPaintRegion);
495         else
496             ImplDrawConstantBackground(rRenderContext, aPaintRegion, bIsInPopupMode);
497     }
498     else
499     {
500         // toolbars known to the dockingmanager will be drawn using NWF or a gradient
501         // docked toolbars are transparent and NWF is already used in the docking area which is their common background
502         // so NWF is used here for floating toolbars only
503         bool bNativeOk = false;
504         if( ImplIsFloatingMode() && rRenderContext.IsNativeControlSupported( ControlType::Toolbar, ControlPart::Entire) )
505             bNativeOk = ImplDrawNativeBackground(rRenderContext);
506         if (!bNativeOk)
507         {
508             const StyleSettings rSetting = Application::GetSettings().GetStyleSettings();
509             const bool isHeader = GetAlign() == WindowAlign::Top && !rSetting.GetPersonaHeader().IsEmpty();
510             const bool isFooter = GetAlign() == WindowAlign::Bottom && !rSetting.GetPersonaFooter().IsEmpty();
511             if (!IsBackground() || isHeader || isFooter)
512             {
513                 if (!IsInPaint())
514                     ImplDrawTransparentBackground(aPaintRegion);
515             }
516             else
517                 ImplDrawGradientBackground(rRenderContext);
518         }
519     }
520 
521     // restore clip region
522     rRenderContext.Pop();
523 }
524 
ImplErase(vcl::RenderContext & rRenderContext,const tools::Rectangle & rRect,bool bHighlight,bool bHasOpenPopup)525 void ToolBox::ImplErase(vcl::RenderContext& rRenderContext, const tools::Rectangle &rRect, bool bHighlight, bool bHasOpenPopup)
526 {
527     // the background of non NWF buttons is painted in a constant color
528     // to have the same highlight color (transparency in DrawSelectionBackground())
529     // items with open popups will also painted using a constant color
530     if (!mpData->mbNativeButtons &&
531         (bHighlight || !(GetStyle() & WB_3DLOOK)))
532     {
533         if (GetStyle() & WB_3DLOOK)
534         {
535             rRenderContext.Push(vcl::PushFlags::LINECOLOR | vcl::PushFlags::FILLCOLOR);
536             rRenderContext.SetLineColor();
537             if (bHasOpenPopup)
538                 // choose the same color as the popup will use
539                 rRenderContext.SetFillColor(rRenderContext.GetSettings().GetStyleSettings().GetFaceColor());
540             else
541                 rRenderContext.SetFillColor(rRenderContext.GetSettings().GetStyleSettings().GetWindowColor());
542 
543             rRenderContext.DrawRect(rRect);
544             rRenderContext.Pop();
545         }
546         else
547             ImplDrawBackground(rRenderContext, rRect);
548     }
549     else
550         ImplDrawBackground(rRenderContext, rRect);
551 }
552 
ImplDrawBorder(vcl::RenderContext & rRenderContext)553 void ToolBox::ImplDrawBorder(vcl::RenderContext& rRenderContext)
554 {
555     const StyleSettings& rStyleSettings = rRenderContext.GetSettings().GetStyleSettings();
556     tools::Long nDX = mnDX;
557     tools::Long nDY = mnDY;
558 
559     ImplDockingWindowWrapper* pWrapper = ImplGetDockingManager()->GetDockingWindowWrapper(this);
560 
561     // draw borders for ordinary toolbars only (not dockable), do not draw borders for toolbars with WB_NOSHADOW bit set,
562     // e.g. Calc's formulabar
563 
564     if( pWrapper || mnWinStyle & WB_NOSHADOW )
565         return;
566 
567     if (meAlign == WindowAlign::Bottom)
568     {
569         // draw bottom border
570         rRenderContext.SetLineColor( rStyleSettings.GetShadowColor() );
571         rRenderContext.DrawLine( Point( 0, nDY-2 ), Point( nDX-1, nDY-2 ) );
572         rRenderContext.SetLineColor( rStyleSettings.GetLightColor() );
573         rRenderContext.DrawLine( Point( 0, nDY-1 ), Point( nDX-1, nDY-1 ) );
574     }
575     else
576     {
577         // draw top border
578         rRenderContext.SetLineColor( rStyleSettings.GetShadowColor() );
579         rRenderContext.DrawLine( Point( 0, 0 ), Point( nDX-1, 0 ) );
580         rRenderContext.SetLineColor( rStyleSettings.GetLightColor() );
581         rRenderContext.DrawLine( Point( 0, 1 ), Point( nDX-1, 1 ) );
582 
583         if (meAlign == WindowAlign::Left || meAlign == WindowAlign::Right)
584         {
585             if (meAlign == WindowAlign::Left)
586             {
587                 // draw left-bottom border
588                 rRenderContext.SetLineColor( rStyleSettings.GetShadowColor() );
589                 rRenderContext.DrawLine( Point( 0, 0 ), Point( 0, nDY-1 ) );
590                 rRenderContext.DrawLine( Point( 0, nDY-2 ), Point( nDX-1, nDY-2 ) );
591                 rRenderContext.SetLineColor( rStyleSettings.GetLightColor() );
592                 rRenderContext.DrawLine( Point( 1, 1 ), Point( 1, nDY-3 ) );
593                 rRenderContext.DrawLine( Point( 0, nDY-1 ), Point( nDX-1, nDY-1 ) );
594             }
595             else
596             {
597                 // draw right-bottom border
598                 rRenderContext.SetLineColor( rStyleSettings.GetShadowColor() );
599                 rRenderContext.DrawLine( Point( nDX-2, 0 ), Point( nDX-2, nDY-3 ) );
600                 rRenderContext.DrawLine( Point( 0, nDY-2 ), Point( nDX-2, nDY-2 ) );
601                 rRenderContext.SetLineColor( rStyleSettings.GetLightColor() );
602                 rRenderContext.DrawLine( Point( nDX-1, 0 ), Point( nDX-1, nDY-1 ) );
603                 rRenderContext.DrawLine( Point( 0, nDY-1 ), Point( nDX-1, nDY-1 ) );
604             }
605         }
606     }
607 
608     if ( meAlign == WindowAlign::Bottom || meAlign == WindowAlign::Top )
609     {
610         // draw right border
611         rRenderContext.SetLineColor( rStyleSettings.GetShadowColor() );
612         rRenderContext.DrawLine( Point( nDX-2, 0 ), Point( nDX-2, nDY-1 ) );
613         rRenderContext.SetLineColor( rStyleSettings.GetLightColor() );
614         rRenderContext.DrawLine( Point( nDX-1, 0 ), Point( nDX-1, nDY-1 ) );
615     }
616 }
617 
ImplIsFixedControl(const ImplToolItem * pItem)618 static bool ImplIsFixedControl( const ImplToolItem *pItem )
619 {
620     return ( pItem->mpWindow &&
621             (pItem->mbNonInteractiveWindow ||
622              pItem->mpWindow->GetType() == WindowType::FIXEDTEXT ||
623              pItem->mpWindow->GetType() == WindowType::FIXEDLINE ||
624              pItem->mpWindow->GetType() == WindowType::GROUPBOX) );
625 }
626 
ImplGetFirstClippedItem() const627 const ImplToolItem *ToolBox::ImplGetFirstClippedItem() const
628 {
629     for (auto & item : mpData->m_aItems)
630     {
631         if( item.IsClipped() )
632             return &item;
633     }
634     return nullptr;
635 }
636 
ImplCalcSize(ImplToolItems::size_type nCalcLines,sal_uInt16 nCalcMode)637 Size ToolBox::ImplCalcSize( ImplToolItems::size_type nCalcLines, sal_uInt16 nCalcMode )
638 {
639     sal_Int32            nMax;
640     tools::Long            nLeft = 0;
641     tools::Long            nTop = 0;
642     tools::Long            nRight = 0;
643     tools::Long            nBottom = 0;
644     Size            aSize;
645     WindowAlign     eOldAlign = meAlign;
646     bool            bOldHorz = mbHorz;
647     bool            bOldAssumeDocked = mpData->mbAssumeDocked;
648     bool            bOldAssumeFloating = mpData->mbAssumeFloating;
649 
650     if ( nCalcMode )
651     {
652         bool bOldFloatingMode = ImplIsFloatingMode();
653 
654         mpData->mbAssumeDocked = false;
655         mpData->mbAssumeFloating = false;
656 
657         if ( nCalcMode == TB_CALCMODE_HORZ )
658         {
659             mpData->mbAssumeDocked = true;   // force non-floating mode during calculation
660             ImplCalcBorder( WindowAlign::Top, nLeft, nTop, nRight, nBottom );
661             mbHorz = true;
662             if ( mbHorz != bOldHorz )
663                 meAlign = WindowAlign::Top;
664         }
665         else if ( nCalcMode == TB_CALCMODE_VERT )
666         {
667             mpData->mbAssumeDocked = true;   // force non-floating mode during calculation
668             ImplCalcBorder( WindowAlign::Left, nLeft, nTop, nRight, nBottom );
669             mbHorz = false;
670             if ( mbHorz != bOldHorz )
671                 meAlign = WindowAlign::Left;
672         }
673         else if ( nCalcMode == TB_CALCMODE_FLOAT )
674         {
675             mpData->mbAssumeFloating = true;   // force non-floating mode during calculation
676             nLeft = nTop = nRight = nBottom = 0;
677             mbHorz = true;
678             if ( mbHorz != bOldHorz )
679                 meAlign = WindowAlign::Top;
680         }
681 
682         if ( (meAlign != eOldAlign) || (mbHorz != bOldHorz) ||
683              (ImplIsFloatingMode() != bOldFloatingMode ) )
684             mbCalc = true;
685     }
686     else
687         ImplCalcBorder( meAlign, nLeft, nTop, nRight, nBottom );
688 
689     ImplCalcItem();
690 
691     if( !nCalcMode && ImplIsFloatingMode() )
692     {
693         aSize = ImplCalcFloatSize( nCalcLines );
694     }
695     else
696     {
697         if ( mbHorz )
698         {
699             if ( mnWinHeight > mnMaxItemHeight )
700                 aSize.setHeight( nCalcLines * mnWinHeight );
701             else
702                 aSize.setHeight( nCalcLines * mnMaxItemHeight );
703 
704             if ( mbLineSpacing )
705                 aSize.AdjustHeight((nCalcLines-1)*TB_LINESPACING );
706 
707             if ( mnWinStyle & WB_BORDER )
708                 aSize.AdjustHeight((TB_BORDER_OFFSET2*2) + nTop + nBottom );
709 
710             nMax = 0;
711             ImplCalcBreaks( TB_MAXNOSCROLL, &nMax, mbHorz );
712             if ( nMax )
713                 aSize.AdjustWidth(nMax );
714 
715             if ( mnWinStyle & WB_BORDER )
716                 aSize.AdjustWidth((TB_BORDER_OFFSET1*2) + nLeft + nRight );
717         }
718         else
719         {
720             aSize.setWidth( nCalcLines * mnMaxItemWidth );
721 
722             if ( mbLineSpacing )
723                 aSize.AdjustWidth((nCalcLines-1)*TB_LINESPACING );
724 
725             if ( mnWinStyle & WB_BORDER )
726                 aSize.AdjustWidth((TB_BORDER_OFFSET2*2) + nLeft + nRight );
727 
728             nMax = 0;
729             ImplCalcBreaks( TB_MAXNOSCROLL, &nMax, mbHorz );
730             if ( nMax )
731                 aSize.AdjustHeight(nMax );
732 
733             if ( mnWinStyle & WB_BORDER )
734                 aSize.AdjustHeight((TB_BORDER_OFFSET1*2) + nTop + nBottom );
735         }
736     }
737     // restore previous values
738     if ( nCalcMode )
739     {
740         mpData->mbAssumeDocked = bOldAssumeDocked;
741         mpData->mbAssumeFloating = bOldAssumeFloating;
742         if ( (meAlign != eOldAlign) || (mbHorz != bOldHorz) )
743         {
744             meAlign  = eOldAlign;
745             mbHorz   = bOldHorz;
746             mbCalc   = true;
747         }
748     }
749 
750     return aSize;
751 }
752 
ImplCalcFloatSizes()753 void ToolBox::ImplCalcFloatSizes()
754 {
755     if ( !maFloatSizes.empty() )
756         return;
757 
758     // calculate the minimal size, i.e. where the biggest item just fits
759     tools::Long            nCalcSize = 0;
760 
761     for (auto const& item : mpData->m_aItems)
762     {
763         if ( item.mbVisible )
764         {
765             if ( item.mpWindow )
766             {
767                 tools::Long nTempSize = item.mpWindow->GetSizePixel().Width();
768                 if ( nTempSize > nCalcSize )
769                     nCalcSize = nTempSize;
770             }
771             else
772             {
773                 if( item.maItemSize.Width() > nCalcSize )
774                     nCalcSize = item.maItemSize.Width();
775             }
776         }
777     }
778 
779     // calc an upper bound for ImplCalcBreaks below
780     tools::Long upperBoundWidth = nCalcSize * mpData->m_aItems.size();
781 
782     ImplToolItems::size_type nLines;
783     ImplToolItems::size_type nCalcLines;
784     ImplToolItems::size_type nTempLines;
785     sal_Int32    nMaxLineWidth;
786     nCalcLines = ImplCalcBreaks( nCalcSize, &nMaxLineWidth, true );
787 
788     maFloatSizes.reserve( nCalcLines );
789 
790     nTempLines = nLines = nCalcLines;
791     while ( nLines )
792     {
793         tools::Long nHeight = ImplCalcSize( nTempLines, TB_CALCMODE_FLOAT ).Height();
794 
795         ImplToolSize aSize;
796         aSize.mnWidth  = nMaxLineWidth+(TB_BORDER_OFFSET1*2);
797         aSize.mnHeight = nHeight;
798         aSize.mnLines  = nTempLines;
799         maFloatSizes.push_back( aSize );
800         nLines--;
801         if ( nLines )
802         {
803             do
804             {
805                 nCalcSize += mnMaxItemWidth;
806                 nTempLines = ImplCalcBreaks( nCalcSize, &nMaxLineWidth, true );
807             }
808             while ((nCalcSize < upperBoundWidth) && (nLines < nTempLines)); // implies nTempLines>1
809             if ( nTempLines < nLines )
810                 nLines = nTempLines;
811         }
812     }
813 }
814 
ImplCalcFloatSize(ImplToolItems::size_type & rLines)815 Size ToolBox::ImplCalcFloatSize( ImplToolItems::size_type& rLines )
816 {
817     ImplCalcFloatSizes();
818 
819     if ( !rLines )
820     {
821         rLines = mnFloatLines;
822         if ( !rLines )
823             rLines = mnLines;
824     }
825 
826     sal_uInt16 i = 0;
827     while ( i + 1u < maFloatSizes.size() && rLines < maFloatSizes[i].mnLines )
828     {
829         i++;
830     }
831 
832     Size aSize( maFloatSizes[i].mnWidth, maFloatSizes[i].mnHeight );
833     rLines = maFloatSizes[i].mnLines;
834 
835     return aSize;
836 }
837 
ImplCalcMinMaxFloatSize(Size & rMinSize,Size & rMaxSize)838 void ToolBox::ImplCalcMinMaxFloatSize( Size& rMinSize, Size& rMaxSize )
839 {
840     ImplCalcFloatSizes();
841 
842     sal_uInt16 i = 0;
843     rMinSize = Size( maFloatSizes[i].mnWidth, maFloatSizes[i].mnHeight );
844     rMaxSize = Size( maFloatSizes[i].mnWidth, maFloatSizes[i].mnHeight );
845     while ( ++i < maFloatSizes.size() )
846     {
847         if( maFloatSizes[i].mnWidth < rMinSize.Width() )
848             rMinSize.setWidth( maFloatSizes[i].mnWidth );
849         if( maFloatSizes[i].mnHeight < rMinSize.Height() )
850             rMinSize.setHeight( maFloatSizes[i].mnHeight );
851 
852         if( maFloatSizes[i].mnWidth > rMaxSize.Width() )
853             rMaxSize.setWidth( maFloatSizes[i].mnWidth );
854         if( maFloatSizes[i].mnHeight > rMaxSize.Height() )
855             rMaxSize.setHeight( maFloatSizes[i].mnHeight );
856     }
857 }
858 
ImplSetMinMaxFloatSize()859 void ToolBox::ImplSetMinMaxFloatSize()
860 {
861     ImplDockingWindowWrapper *pWrapper = ImplGetDockingManager()->GetDockingWindowWrapper( this );
862     Size aMinSize, aMaxSize;
863     ImplCalcMinMaxFloatSize( aMinSize, aMaxSize );
864     if( pWrapper )
865     {
866         pWrapper->SetMinOutputSizePixel( aMinSize );
867         pWrapper->SetMaxOutputSizePixel( aMaxSize );
868         pWrapper->ShowMenuTitleButton( bool( GetMenuType() & ToolBoxMenuType::Customize) );
869     }
870     else
871     {
872         // TODO: change SetMinOutputSizePixel to be not inline
873         SetMinOutputSizePixel( aMinSize );
874         SetMaxOutputSizePixel( aMaxSize );
875     }
876 }
877 
ImplCalcLines(tools::Long nToolSize) const878 ToolBox::ImplToolItems::size_type ToolBox::ImplCalcLines( tools::Long nToolSize ) const
879 {
880     tools::Long nLineHeight;
881 
882     if ( mbHorz )
883     {
884         if ( mnWinHeight > mnMaxItemHeight )
885             nLineHeight = mnWinHeight;
886         else
887             nLineHeight = mnMaxItemHeight;
888     }
889     else
890         nLineHeight = mnMaxItemWidth;
891 
892     if ( mnWinStyle & WB_BORDER )
893         nToolSize -= TB_BORDER_OFFSET2*2;
894 
895     if ( mbLineSpacing )
896     {
897         nLineHeight += TB_LINESPACING;
898         nToolSize += TB_LINESPACING;
899     }
900 
901     // #i91917# always report at least one line
902     tools::Long nLines = nToolSize/nLineHeight;
903     if( nLines < 1 )
904         nLines = 1;
905 
906     return nLines;
907 }
908 
ImplTestLineSize(const Point & rPos) const909 sal_uInt16 ToolBox::ImplTestLineSize( const Point& rPos ) const
910 {
911     if ( !ImplIsFloatingMode() &&
912          (!mbScroll || (mnLines > 1) || (mnCurLines > mnVisLines)) )
913     {
914         WindowAlign eAlign = GetAlign();
915 
916         if ( eAlign == WindowAlign::Left )
917         {
918             if ( rPos.X() > mnDX-DOCK_LINEOFFSET )
919                 return DOCK_LINEHSIZE | DOCK_LINERIGHT;
920         }
921         else if ( eAlign == WindowAlign::Top )
922         {
923             if ( rPos.Y() > mnDY-DOCK_LINEOFFSET )
924                 return DOCK_LINEVSIZE | DOCK_LINEBOTTOM;
925         }
926         else if ( eAlign == WindowAlign::Right )
927         {
928             if ( rPos.X() < DOCK_LINEOFFSET )
929                 return DOCK_LINEHSIZE | DOCK_LINELEFT;
930         }
931         else if ( eAlign == WindowAlign::Bottom )
932         {
933             if ( rPos.Y() < DOCK_LINEOFFSET )
934                 return DOCK_LINEVSIZE | DOCK_LINETOP;
935         }
936     }
937 
938     return 0;
939 }
940 
ImplLineSizing(const Point & rPos,tools::Rectangle & rRect,sal_uInt16 nLineMode)941 void ToolBox::ImplLineSizing( const Point& rPos, tools::Rectangle& rRect, sal_uInt16 nLineMode )
942 {
943     bool    bHorz;
944     tools::Long    nOneLineSize;
945     tools::Long    nCurSize;
946     tools::Long    nMaxSize;
947     tools::Long    nSize;
948     Size    aSize;
949 
950     if ( nLineMode & DOCK_LINERIGHT )
951     {
952         nCurSize = rPos.X() - rRect.Left();
953         bHorz = false;
954     }
955     else if ( nLineMode & DOCK_LINEBOTTOM )
956     {
957         nCurSize = rPos.Y() - rRect.Top();
958         bHorz = true;
959     }
960     else if ( nLineMode & DOCK_LINELEFT )
961     {
962         nCurSize = rRect.Right() - rPos.X();
963         bHorz = false;
964     }
965     else if ( nLineMode & DOCK_LINETOP )
966     {
967         nCurSize = rRect.Bottom() - rPos.Y();
968         bHorz = true;
969     }
970     else {
971         OSL_FAIL( "ImplLineSizing: Trailing else" );
972         nCurSize = 0;
973         bHorz = false;
974     }
975 
976     Size    aWinSize = GetSizePixel();
977     ImplToolItems::size_type nMaxLines = std::max(mnLines, mnCurLines);
978     if ( nMaxLines > TB_MAXLINES )
979         nMaxLines = TB_MAXLINES;
980     if ( bHorz )
981     {
982         nOneLineSize = ImplCalcSize( 1 ).Height();
983         nMaxSize = - 20;
984         if ( nMaxSize < aWinSize.Height() )
985             nMaxSize = aWinSize.Height();
986     }
987     else
988     {
989         nOneLineSize = ImplCalcSize( 1 ).Width();
990         nMaxSize = - 20;
991         if ( nMaxSize < aWinSize.Width() )
992             nMaxSize = aWinSize.Width();
993     }
994 
995     ImplToolItems::size_type i = 1;
996     if ( nCurSize <= nOneLineSize )
997         nSize = nOneLineSize;
998     else
999     {
1000         nSize = 0;
1001         while ( (nSize < nCurSize) && (i < nMaxLines) )
1002         {
1003             i++;
1004             aSize = ImplCalcSize( i );
1005             if ( bHorz )
1006                 nSize = aSize.Height();
1007             else
1008                 nSize = aSize.Width();
1009             if ( nSize > nMaxSize )
1010             {
1011                 i--;
1012                 aSize = ImplCalcSize( i );
1013                 if ( bHorz )
1014                     nSize = aSize.Height();
1015                 else
1016                     nSize = aSize.Width();
1017                 break;
1018             }
1019         }
1020     }
1021 
1022     if ( nLineMode & DOCK_LINERIGHT )
1023         rRect.SetRight( rRect.Left()+nSize-1 );
1024     else if ( nLineMode & DOCK_LINEBOTTOM )
1025         rRect.SetBottom( rRect.Top()+nSize-1 );
1026     else if ( nLineMode & DOCK_LINELEFT )
1027         rRect.SetLeft( rRect.Right()-nSize );
1028     else
1029         rRect.SetTop( rRect.Bottom()-nSize );
1030 
1031     mnDockLines = i;
1032 }
1033 
ImplTBDragMgr()1034 ImplTBDragMgr::ImplTBDragMgr()
1035     : mpDragBox(nullptr)
1036     , mnLineMode(0)
1037     , mnStartLines(0)
1038 {
1039     maAccel.InsertItem( KEY_RETURN, vcl::KeyCode( KEY_RETURN ) );
1040     maAccel.InsertItem( KEY_ESCAPE, vcl::KeyCode( KEY_ESCAPE ) );
1041     maAccel.SetSelectHdl( LINK( this, ImplTBDragMgr, SelectHdl ) );
1042 }
1043 
StartDragging(ToolBox * pToolBox,const Point & rPos,const tools::Rectangle & rRect,sal_uInt16 nDragLineMode)1044 void ImplTBDragMgr::StartDragging( ToolBox* pToolBox,
1045                                    const Point& rPos, const tools::Rectangle& rRect,
1046                                    sal_uInt16 nDragLineMode )
1047 {
1048     mpDragBox = pToolBox;
1049     pToolBox->CaptureMouse();
1050     pToolBox->mbDragging = true;
1051     Application::InsertAccel( &maAccel );
1052 
1053     mnLineMode = nDragLineMode;
1054     mnStartLines = pToolBox->mnDockLines;
1055 
1056     // calculate MouseOffset
1057     maMouseOff.setX( rRect.Left() - rPos.X() );
1058     maMouseOff.setY( rRect.Top() - rPos.Y() );
1059     maRect = rRect;
1060     maStartRect = rRect;
1061     pToolBox->ShowTracking( maRect );
1062 }
1063 
Dragging(const Point & rPos)1064 void ImplTBDragMgr::Dragging( const Point& rPos )
1065 {
1066     mpDragBox->ImplLineSizing( rPos, maRect, mnLineMode );
1067     Point aOff = mpDragBox->OutputToScreenPixel( Point() );
1068     maRect.Move( aOff.X(), aOff.Y() );
1069     mpDragBox->Docking( rPos, maRect );
1070     maRect.Move( -aOff.X(), -aOff.Y() );
1071     mpDragBox->ShowTracking( maRect );
1072 }
1073 
EndDragging(bool bOK)1074 void ImplTBDragMgr::EndDragging( bool bOK )
1075 {
1076     mpDragBox->HideTracking();
1077     if (mpDragBox->IsMouseCaptured())
1078         mpDragBox->ReleaseMouse();
1079     mpDragBox->mbDragging = false;
1080     Application::RemoveAccel( &maAccel );
1081 
1082     if ( !bOK )
1083     {
1084         mpDragBox->mnDockLines = mnStartLines;
1085         mpDragBox->EndDocking( maStartRect, false );
1086     }
1087     else
1088         mpDragBox->EndDocking( maRect, false );
1089     mnStartLines = 0;
1090 
1091     mpDragBox = nullptr;
1092 }
1093 
IMPL_LINK(ImplTBDragMgr,SelectHdl,Accelerator &,rAccel,void)1094 IMPL_LINK( ImplTBDragMgr, SelectHdl, Accelerator&, rAccel, void )
1095 {
1096     if ( rAccel.GetCurItemId() == KEY_ESCAPE )
1097         EndDragging( false );
1098     else
1099         EndDragging();
1100 }
1101 
ImplInitToolBoxData()1102 void ToolBox::ImplInitToolBoxData()
1103 {
1104     // initialize variables
1105     ImplGetWindowImpl()->mbToolBox  = true;
1106     mpData.reset(new ImplToolBoxPrivateData);
1107 
1108     mpFloatWin            = nullptr;
1109     mnDX                  = 0;
1110     mnDY                  = 0;
1111     mnMaxItemWidth        = 0;
1112     mnMaxItemHeight       = 0;
1113     mnWinHeight           = 0;
1114     mnLeftBorder          = 0;
1115     mnTopBorder           = 0;
1116     mnRightBorder         = 0;
1117     mnBottomBorder        = 0;
1118     mnLastResizeDY        = 0;
1119     mnHighItemId          = ToolBoxItemId(0);
1120     mnCurItemId           = ToolBoxItemId(0);
1121     mnDownItemId          = ToolBoxItemId(0);
1122     mnCurPos              = ITEM_NOTFOUND;
1123     mnLines               = 1;
1124     mnCurLine             = 1;
1125     mnCurLines            = 1;
1126     mnVisLines            = 1;
1127     mnFloatLines          = 0;
1128     mnDockLines           = 0;
1129     mnMouseModifier       = 0;
1130     mbDrag                = false;
1131     mbUpper               = false;
1132     mbLower               = false;
1133     mbIn                  = false;
1134     mbCalc                = true;
1135     mbFormat              = false;
1136     mbFullPaint           = false;
1137     mbHorz                = true;
1138     mbScroll              = false;
1139     mbLastFloatMode       = false;
1140     mbCustomize           = false;
1141     mbDragging            = false;
1142     mbIsKeyEvent          = false;
1143     mbChangingHighlight   = false;
1144     mbLineSpacing         = false;
1145     mbIsArranged          = false;
1146     meButtonType          = ButtonType::SYMBOLONLY;
1147     meAlign               = WindowAlign::Top;
1148     meDockAlign           = WindowAlign::Top;
1149     meLastStyle           = PointerStyle::Arrow;
1150     mnWinStyle            = 0;
1151     meLayoutMode          = ToolBoxLayoutMode::Normal;
1152     meTextPosition        = ToolBoxTextPosition::Right;
1153     mnLastFocusItemId     = ToolBoxItemId(0);
1154     mnActivateCount       = 0;
1155 
1156     mpIdle.reset(new Idle("vcl::ToolBox maIdle update"));
1157     mpIdle->SetPriority( TaskPriority::RESIZE );
1158     mpIdle->SetInvokeHandler( LINK( this, ToolBox, ImplUpdateHdl ) );
1159 
1160     // set timeout and handler for dropdown items
1161     mpData->maDropdownTimer.SetTimeout( 250 );
1162     mpData->maDropdownTimer.SetInvokeHandler( LINK( this, ToolBox, ImplDropdownLongClickHdl ) );
1163 }
1164 
ImplInit(vcl::Window * pParent,WinBits nStyle)1165 void ToolBox::ImplInit( vcl::Window* pParent, WinBits nStyle )
1166 {
1167     // initialize variables
1168     mbScroll          = (nStyle & WB_SCROLL) != 0;
1169     mnWinStyle        = nStyle;
1170 
1171     DockingWindow::ImplInit( pParent, nStyle & ~WB_BORDER );
1172 
1173     // dockingwindow's ImplInit removes some bits, so restore them here to allow keyboard handling for toolbars
1174     ImplGetWindowImpl()->mnStyle |= WB_TABSTOP|WB_NODIALOGCONTROL; // always set WB_TABSTOP for ToolBars
1175     ImplGetWindowImpl()->mnStyle &= ~WB_DIALOGCONTROL;
1176 
1177     ImplInitSettings(true, true, true);
1178 }
1179 
ApplyForegroundSettings(vcl::RenderContext & rRenderContext,const StyleSettings & rStyleSettings)1180 void ToolBox::ApplyForegroundSettings(vcl::RenderContext& rRenderContext, const StyleSettings& rStyleSettings)
1181 {
1182     Color aColor;
1183     if (IsControlForeground())
1184         aColor = GetControlForeground();
1185     else if (Window::GetStyle() & WB_3DLOOK)
1186         aColor = rStyleSettings.GetButtonTextColor();
1187     else
1188         aColor = rStyleSettings.GetWindowTextColor();
1189     rRenderContext.SetTextColor(aColor);
1190     rRenderContext.SetTextFillColor();
1191 }
1192 
ApplyBackgroundSettings(vcl::RenderContext & rRenderContext,const StyleSettings & rStyleSettings)1193 void ToolBox::ApplyBackgroundSettings(vcl::RenderContext& rRenderContext, const StyleSettings& rStyleSettings)
1194 {
1195     if (IsControlBackground())
1196     {
1197         rRenderContext.SetBackground(GetControlBackground());
1198         SetPaintTransparent(false);
1199         SetParentClipMode();
1200     }
1201     else
1202     {
1203         if (rRenderContext.IsNativeControlSupported(ControlType::Toolbar, ControlPart::Entire)
1204             || (GetAlign() == WindowAlign::Top && !Application::GetSettings().GetStyleSettings().GetPersonaHeader().IsEmpty())
1205             || (GetAlign() == WindowAlign::Bottom && !Application::GetSettings().GetStyleSettings().GetPersonaFooter().IsEmpty()))
1206         {
1207             rRenderContext.SetBackground();
1208             rRenderContext.SetTextColor(rStyleSettings.GetToolTextColor());
1209             SetPaintTransparent(true);
1210             SetParentClipMode(ParentClipMode::NoClip);
1211             mpData->maDisplayBackground = Wallpaper(rStyleSettings.GetFaceColor());
1212         }
1213         else
1214         {
1215             Color aColor;
1216             if (Window::GetStyle() & WB_3DLOOK)
1217                 aColor = rStyleSettings.GetFaceColor();
1218             else
1219                 aColor = rStyleSettings.GetWindowColor();
1220             rRenderContext.SetBackground(aColor);
1221             SetPaintTransparent(false);
1222             SetParentClipMode();
1223         }
1224     }
1225 }
1226 
ApplySettings(vcl::RenderContext & rRenderContext)1227 void ToolBox::ApplySettings(vcl::RenderContext& rRenderContext)
1228 {
1229     mpData->mbNativeButtons = rRenderContext.IsNativeControlSupported(ControlType::Toolbar, ControlPart::Button);
1230 
1231     const StyleSettings& rStyleSettings = rRenderContext.GetSettings().GetStyleSettings();
1232 
1233     ApplyControlFont(rRenderContext, rStyleSettings.GetToolFont());
1234     ApplyForegroundSettings(rRenderContext, rStyleSettings);
1235     ApplyBackgroundSettings(rRenderContext, rStyleSettings);
1236 }
1237 
ImplInitSettings(bool bFont,bool bForeground,bool bBackground)1238 void ToolBox::ImplInitSettings(bool bFont, bool bForeground, bool bBackground)
1239 {
1240     mpData->mbNativeButtons = IsNativeControlSupported( ControlType::Toolbar, ControlPart::Button );
1241 
1242     const StyleSettings& rStyleSettings = GetSettings().GetStyleSettings();
1243 
1244     if (bFont)
1245         ApplyControlFont(*GetOutDev(), rStyleSettings.GetToolFont());
1246     if (bForeground || bFont)
1247         ApplyForegroundSettings(*GetOutDev(), rStyleSettings);
1248     if (bBackground)
1249     {
1250         ApplyBackgroundSettings(*GetOutDev(), rStyleSettings);
1251         EnableChildTransparentMode(IsPaintTransparent());
1252     }
1253 }
1254 
doDeferredInit(WinBits nBits)1255 void ToolBox::doDeferredInit(WinBits nBits)
1256 {
1257     VclPtr<vcl::Window> pParent = mpDialogParent;
1258     mpDialogParent = nullptr;
1259     ImplInit(pParent, nBits);
1260     mbIsDeferredInit = false;
1261 }
1262 
queue_resize(StateChangedType eReason)1263 void ToolBox::queue_resize(StateChangedType eReason)
1264 {
1265     Window::queue_resize(eReason);
1266 }
1267 
ToolBox(vcl::Window * pParent,WinBits nStyle)1268 ToolBox::ToolBox( vcl::Window* pParent, WinBits nStyle ) :
1269     DockingWindow( WindowType::TOOLBOX, "vcl::ToolBox maLayoutIdle" )
1270 {
1271     ImplInitToolBoxData();
1272     ImplInit( pParent, nStyle );
1273 }
1274 
ToolBox(vcl::Window * pParent,const OUString & rID,const OUString & rUIXMLDescription,const css::uno::Reference<css::frame::XFrame> & rFrame)1275 ToolBox::ToolBox(vcl::Window* pParent, const OUString& rID,
1276     const OUString& rUIXMLDescription, const css::uno::Reference<css::frame::XFrame> &rFrame)
1277     : DockingWindow(WindowType::TOOLBOX, "vcl::ToolBox maLayoutIdle")
1278 {
1279     ImplInitToolBoxData();
1280 
1281     loadUI(pParent, rID, rUIXMLDescription, rFrame);
1282 
1283     // calculate size of floating windows and switch if the
1284     // toolbox is initially in floating mode
1285     if ( ImplIsFloatingMode() )
1286         mbHorz = true;
1287     else
1288         Resize();
1289 
1290     if (!(GetStyle() & WB_HIDE))
1291         Show();
1292 }
1293 
~ToolBox()1294 ToolBox::~ToolBox()
1295 {
1296     disposeOnce();
1297 }
1298 
dispose()1299 void ToolBox::dispose()
1300 {
1301     // #103005# make sure our activate/deactivate balance is right
1302     while( mnActivateCount > 0 )
1303         Deactivate();
1304 
1305     // terminate popupmode if the floating window is
1306     // still connected
1307     if ( mpFloatWin )
1308         mpFloatWin->EndPopupMode( FloatWinPopupEndFlags::Cancel );
1309     mpFloatWin = nullptr;
1310 
1311     // delete private data
1312     mpData.reset();
1313 
1314     ImplSVData* pSVData = ImplGetSVData();
1315     delete pSVData->maCtrlData.mpTBDragMgr;
1316     pSVData->maCtrlData.mpTBDragMgr = nullptr;
1317 
1318     mpFloatWin.clear();
1319 
1320     mpIdle.reset();
1321 
1322     DockingWindow::dispose();
1323 }
1324 
ImplGetItem(ToolBoxItemId nItemId) const1325 ImplToolItem* ToolBox::ImplGetItem( ToolBoxItemId nItemId ) const
1326 {
1327     if (!mpData)
1328         return nullptr;
1329 
1330     for (auto & item : mpData->m_aItems)
1331     {
1332         if ( item.mnId == nItemId )
1333             return &item;
1334     }
1335 
1336     return nullptr;
1337 }
1338 
ImplAddButtonBorder(tools::Long & rWidth,tools::Long & rHeight,bool bNativeButtons)1339 static void ImplAddButtonBorder( tools::Long &rWidth, tools::Long& rHeight, bool bNativeButtons )
1340 {
1341     rWidth += SMALLBUTTON_HSIZE;
1342     rHeight += SMALLBUTTON_VSIZE;
1343 
1344     if( bNativeButtons )
1345     {
1346         // give more border space for rounded buttons
1347         rWidth += 2;
1348         rHeight += 4;
1349     }
1350 }
1351 
ImplCalcItem()1352 bool ToolBox::ImplCalcItem()
1353 {
1354     // recalc required ?
1355     if ( !mbCalc )
1356         return false;
1357 
1358     OutputDevice *pDefault = Application::GetDefaultDevice();
1359     float fScaleFactor = pDefault ? pDefault->GetDPIScaleFactor() : 1.0;
1360 
1361     tools::Long            nDefWidth;
1362     tools::Long            nDefHeight;
1363     tools::Long            nMaxWidth = 0;
1364     tools::Long            nMaxHeight = 0;
1365     tools::Long            nMinWidth   = 6;
1366     tools::Long            nMinHeight  = 6;
1367     tools::Long            nDropDownArrowWidth = TB_DROPDOWNARROWWIDTH * fScaleFactor;
1368 #ifdef IOS
1369     nDropDownArrowWidth *= 3;
1370 #endif
1371 
1372     // set defaults if image or text is needed but empty
1373     nDefWidth  = GetDefaultImageSize().Width();
1374     nDefHeight = GetDefaultImageSize().Height();
1375 
1376     mnWinHeight = 0;
1377     // determine minimum size necessary in NWF
1378     {
1379         tools::Rectangle aRect( Point( 0, 0 ), Size( nMinWidth, nMinHeight ) );
1380         tools::Rectangle aReg( aRect );
1381         ImplControlValue aVal;
1382         tools::Rectangle aNativeBounds, aNativeContent;
1383         if( IsNativeControlSupported( ControlType::Toolbar, ControlPart::Button ) )
1384         {
1385             if( GetNativeControlRegion( ControlType::Toolbar, ControlPart::Button,
1386                                         aReg,
1387                                         ControlState::ENABLED | ControlState::ROLLOVER,
1388                                         aVal,
1389                                         aNativeBounds, aNativeContent ) )
1390             {
1391                 aRect = aNativeBounds;
1392                 if( aRect.GetWidth() > nMinWidth )
1393                     nMinWidth = aRect.GetWidth();
1394                 if( aRect.GetHeight() > nMinHeight )
1395                     nMinHeight = aRect.GetHeight();
1396                 if( nDropDownArrowWidth < nMinWidth )
1397                     nDropDownArrowWidth = nMinWidth;
1398                 if( nMinWidth > mpData->mnMenuButtonWidth )
1399                     mpData->mnMenuButtonWidth = nMinWidth;
1400                 else if( nMinWidth < TB_MENUBUTTON_SIZE )
1401                     mpData->mnMenuButtonWidth = TB_MENUBUTTON_SIZE;
1402             }
1403         }
1404 
1405         // also calculate the area for comboboxes, drop down list boxes and spinfields
1406         // as these are often inserted into toolboxes; set mnWinHeight to the
1407         // greater of those values to prevent toolbar flickering (#i103385#)
1408         aRect = tools::Rectangle( Point( 0, 0 ), Size( nMinWidth, nMinHeight ) );
1409         aReg = aRect;
1410         if( GetNativeControlRegion( ControlType::Combobox, ControlPart::Entire,
1411                                     aReg,
1412                                     ControlState::ENABLED | ControlState::ROLLOVER,
1413                                     aVal,
1414                                     aNativeBounds, aNativeContent ) )
1415         {
1416             aRect = aNativeBounds;
1417             if( aRect.GetHeight() > mnWinHeight )
1418                 mnWinHeight = aRect.GetHeight();
1419         }
1420         aRect = tools::Rectangle( Point( 0, 0 ), Size( nMinWidth, nMinHeight ) );
1421         aReg = aRect;
1422         if( GetNativeControlRegion( ControlType::Listbox, ControlPart::Entire,
1423                                     aReg,
1424                                     ControlState::ENABLED | ControlState::ROLLOVER,
1425                                     aVal,
1426                                     aNativeBounds, aNativeContent ) )
1427         {
1428             aRect = aNativeBounds;
1429             if( aRect.GetHeight() > mnWinHeight )
1430                 mnWinHeight = aRect.GetHeight();
1431         }
1432         aRect = tools::Rectangle( Point( 0, 0 ), Size( nMinWidth, nMinHeight ) );
1433         aReg = aRect;
1434         if( GetNativeControlRegion( ControlType::Spinbox, ControlPart::Entire,
1435                                     aReg,
1436                                     ControlState::ENABLED | ControlState::ROLLOVER,
1437                                     aVal,
1438                                     aNativeBounds, aNativeContent ) )
1439         {
1440             aRect = aNativeBounds;
1441             if( aRect.GetHeight() > mnWinHeight )
1442                 mnWinHeight = aRect.GetHeight();
1443         }
1444     }
1445 
1446     if ( ! mpData->m_aItems.empty() )
1447     {
1448         for (auto & item : mpData->m_aItems)
1449         {
1450             item.mbVisibleText = false;  // indicates if text will definitely be drawn, influences dropdown pos
1451 
1452             if ( item.meType == ToolBoxItemType::BUTTON )
1453             {
1454                 bool bImage;
1455                 bool bText;
1456 
1457                 // check if image and/or text exists
1458                 bImage = !!item.maImage;
1459                 bText = !item.maText.isEmpty();
1460                 ButtonType tmpButtonType = determineButtonType( &item, meButtonType ); // default to toolbox setting
1461                 if ( bImage || bText )
1462                 {
1463 
1464                     item.mbEmptyBtn = false;
1465 
1466                     if ( tmpButtonType == ButtonType::SYMBOLONLY )
1467                     {
1468                         // we're drawing images only
1469                         if ( bImage || !bText )
1470                         {
1471                             item.maItemSize = item.maImage.GetSizePixel();
1472                         }
1473                         else
1474                         {
1475                             item.maItemSize = Size( GetOutDev()->GetCtrlTextWidth( item.maText )+TB_TEXTOFFSET,
1476                                                    GetTextHeight() );
1477                             item.mbVisibleText = true;
1478                         }
1479                     }
1480                     else if ( tmpButtonType == ButtonType::TEXT )
1481                     {
1482                         // we're drawing text only
1483                         if ( bText || !bImage )
1484                         {
1485                             item.maItemSize = Size( GetOutDev()->GetCtrlTextWidth( item.maText )+TB_TEXTOFFSET,
1486                                                    GetTextHeight() );
1487                             item.mbVisibleText = true;
1488                         }
1489                         else
1490                         {
1491                             item.maItemSize = item.maImage.GetSizePixel();
1492                         }
1493                     }
1494                     else
1495                     {
1496                         // we're drawing images and text
1497                         item.maItemSize.setWidth( bText ? GetOutDev()->GetCtrlTextWidth( item.maText )+TB_TEXTOFFSET : 0 );
1498                         item.maItemSize.setHeight( bText ? GetTextHeight() : 0 );
1499 
1500                         if ( meTextPosition == ToolBoxTextPosition::Right )
1501                         {
1502                             // leave space between image and text
1503                             if( bText )
1504                                 item.maItemSize.AdjustWidth(TB_IMAGETEXTOFFSET );
1505 
1506                             // image and text side by side
1507                             item.maItemSize.AdjustWidth(item.maImage.GetSizePixel().Width() );
1508                             if ( item.maImage.GetSizePixel().Height() > item.maItemSize.Height() )
1509                                 item.maItemSize.setHeight( item.maImage.GetSizePixel().Height() );
1510                         }
1511                         else
1512                         {
1513                             // leave space between image and text
1514                             if( bText )
1515                                 item.maItemSize.AdjustHeight(TB_IMAGETEXTOFFSET );
1516 
1517                             // text below image
1518                             item.maItemSize.AdjustHeight(item.maImage.GetSizePixel().Height() );
1519                             if ( item.maImage.GetSizePixel().Width() > item.maItemSize.Width() )
1520                                 item.maItemSize.setWidth( item.maImage.GetSizePixel().Width() );
1521                         }
1522 
1523                         item.mbVisibleText = bText;
1524                     }
1525                 }
1526                 else
1527                 {   // no image and no text
1528                     item.maItemSize = Size( nDefWidth, nDefHeight );
1529                     item.mbEmptyBtn = true;
1530                 }
1531 
1532                 // save the content size
1533                 item.maContentSize = item.maItemSize;
1534 
1535                 // if required, take window height into consideration
1536                 if ( item.mpWindow )
1537                 {
1538                     tools::Long nHeight = item.mpWindow->GetSizePixel().Height();
1539                     if ( nHeight > mnWinHeight )
1540                         mnWinHeight = nHeight;
1541                 }
1542 
1543                 // add in drop down arrow
1544                 if( item.mnBits & ToolBoxItemBits::DROPDOWN )
1545                 {
1546                     item.maItemSize.AdjustWidth(nDropDownArrowWidth );
1547                     item.mnDropDownArrowWidth = nDropDownArrowWidth;
1548                 }
1549 
1550                 // text items will be rotated in vertical mode
1551                 // -> swap width and height
1552                 if( item.mbVisibleText && !mbHorz )
1553                 {
1554                     tools::Long tmp = item.maItemSize.Width();
1555                     item.maItemSize.setWidth( item.maItemSize.Height() );
1556                     item.maItemSize.setHeight( tmp );
1557 
1558                     tmp = item.maContentSize.Width();
1559                     item.maContentSize.setWidth( item.maContentSize.Height() );
1560                     item.maContentSize.setHeight( tmp );
1561                 }
1562             }
1563             else if ( item.meType == ToolBoxItemType::SPACE )
1564             {
1565                 item.maItemSize = Size( nDefWidth, nDefHeight );
1566                 item.maContentSize = item.maItemSize;
1567             }
1568 
1569             if ( item.meType == ToolBoxItemType::BUTTON || item.meType == ToolBoxItemType::SPACE )
1570             {
1571                 // add borders
1572                 tools::Long w = item.maItemSize.Width();
1573                 tools::Long h = item.maItemSize.Height();
1574                 ImplAddButtonBorder( w, h, mpData->mbNativeButtons );
1575                 item.maItemSize.setWidth(w);
1576                 item.maItemSize.setHeight(h);
1577 
1578                 if( item.meType == ToolBoxItemType::BUTTON )
1579                 {
1580                     tools::Long nMinW = std::max(nMinWidth, item.maMinimalItemSize.Width());
1581                     tools::Long nMinH = std::max(nMinHeight, item.maMinimalItemSize.Height());
1582 
1583                     tools::Long nGrowContentWidth = 0;
1584                     tools::Long nGrowContentHeight = 0;
1585 
1586                     if( item.maItemSize.Width() < nMinW )
1587                     {
1588                         nGrowContentWidth = nMinW - item.maItemSize.Width();
1589                         item.maItemSize.setWidth( nMinW );
1590                     }
1591                     if( item.maItemSize.Height() < nMinH )
1592                     {
1593                         nGrowContentHeight = nMinH - item.maItemSize.Height();
1594                         item.maItemSize.setHeight( nMinH );
1595                     }
1596 
1597                     // grow the content size by the additional available space
1598                     item.maContentSize.AdjustWidth(nGrowContentWidth );
1599                     item.maContentSize.AdjustHeight(nGrowContentHeight );
1600                 }
1601 
1602                 // keep track of max item size
1603                 if ( item.maItemSize.Width() > nMaxWidth )
1604                     nMaxWidth = item.maItemSize.Width();
1605                 if ( item.maItemSize.Height() > nMaxHeight )
1606                     nMaxHeight = item.maItemSize.Height();
1607             }
1608         }
1609     }
1610     else
1611     {
1612         nMaxWidth  = nDefWidth;
1613         nMaxHeight = nDefHeight;
1614 
1615         ImplAddButtonBorder( nMaxWidth, nMaxHeight, mpData->mbNativeButtons );
1616     }
1617 
1618     if( !ImplIsFloatingMode() && GetToolboxButtonSize() != ToolBoxButtonSize::DontCare
1619         && ( meTextPosition == ToolBoxTextPosition::Right ) )
1620     {
1621         // make sure all vertical toolbars have the same width and horizontal have the same height
1622         // this depends on the used button sizes
1623         // as this is used for alignment of multiple toolbars
1624         // it is only required for docked toolbars
1625 
1626         tools::Long nFixedWidth = nDefWidth+nDropDownArrowWidth;
1627         tools::Long nFixedHeight = nDefHeight;
1628         ImplAddButtonBorder( nFixedWidth, nFixedHeight, mpData->mbNativeButtons );
1629 
1630         if( mbHorz )
1631             nMaxHeight = nFixedHeight;
1632         else
1633             nMaxWidth = nFixedWidth;
1634     }
1635 
1636     mbCalc = false;
1637     mbFormat = true;
1638 
1639     // do we have to recalc the sizes ?
1640     if ( (nMaxWidth != mnMaxItemWidth) || (nMaxHeight != mnMaxItemHeight) )
1641     {
1642         mnMaxItemWidth  = nMaxWidth;
1643         mnMaxItemHeight = nMaxHeight;
1644 
1645         return true;
1646     }
1647     else
1648         return false;
1649 }
1650 
ImplCalcBreaks(tools::Long nWidth,sal_Int32 * pMaxLineWidth,bool bCalcHorz) const1651 ToolBox::ImplToolItems::size_type ToolBox::ImplCalcBreaks( tools::Long nWidth, sal_Int32* pMaxLineWidth, bool bCalcHorz ) const
1652 {
1653     sal_uLong           nLineStart = 0;
1654     sal_uLong           nGroupStart = 0;
1655     tools::Long            nLineWidth = 0;
1656     tools::Long            nCurWidth;
1657     tools::Long            nLastGroupLineWidth = 0;
1658     tools::Long            nMaxLineWidth = 0;
1659     ImplToolItems::size_type nLines = 1;
1660     bool            bWindow;
1661     bool            bBreak = false;
1662     tools::Long            nWidthTotal = nWidth;
1663     tools::Long nMenuWidth = 0;
1664 
1665     // when docked the menubutton will be in the first line
1666     if( IsMenuEnabled() && !ImplIsFloatingMode() )
1667         nMenuWidth = mpData->maMenubuttonItem.maItemSize.Width();
1668 
1669     // we need to know which item is the last visible one to be able to add
1670     // the menu width in case we are unable to show all the items
1671     ImplToolItems::iterator it, lastVisible;
1672     for ( it = mpData->m_aItems.begin(); it != mpData->m_aItems.end(); ++it )
1673     {
1674         if ( it->mbVisible )
1675             lastVisible = it;
1676     }
1677 
1678     it = mpData->m_aItems.begin();
1679     while ( it != mpData->m_aItems.end() )
1680     {
1681         it->mbBreak = bBreak;
1682         bBreak = false;
1683 
1684         if ( it->mbVisible )
1685         {
1686             bWindow     = false;
1687             bBreak      = false;
1688             nCurWidth   = 0;
1689 
1690             if ( it->meType == ToolBoxItemType::BUTTON || it->meType == ToolBoxItemType::SPACE )
1691             {
1692                 if ( bCalcHorz )
1693                     nCurWidth = it->maItemSize.Width();
1694                 else
1695                     nCurWidth = it->maItemSize.Height();
1696 
1697                 if ( it->mpWindow && bCalcHorz )
1698                 {
1699                     tools::Long nWinItemWidth = it->mpWindow->GetSizePixel().Width();
1700                     if ( !mbScroll || (nWinItemWidth <= nWidthTotal) )
1701                     {
1702                         nCurWidth = nWinItemWidth;
1703                         bWindow   = true;
1704                     }
1705                     else
1706                     {
1707                         if ( it->mbEmptyBtn )
1708                         {
1709                             nCurWidth = 0;
1710                         }
1711                     }
1712                 }
1713 
1714                 // in case we are able to show all the items, we do not want
1715                 // to show the toolbar's menu; otherwise yes
1716                 if ( ( ( it == lastVisible ) && (nLineWidth+nCurWidth > nWidthTotal) && mbScroll ) ||
1717                      ( ( it != lastVisible ) && (nLineWidth+nCurWidth+nMenuWidth > nWidthTotal) && mbScroll ) )
1718                     bBreak = true;
1719             }
1720             else if ( it->meType == ToolBoxItemType::SEPARATOR )
1721             {
1722                 nCurWidth = it->mnSepSize;
1723                 if ( !ImplIsFloatingMode() && ( it != lastVisible ) && (nLineWidth+nCurWidth+nMenuWidth > nWidthTotal) )
1724                     bBreak = true;
1725             }
1726             // treat breaks as separators, except when using old style toolbars (ie. no menu button)
1727             else if ( (it->meType == ToolBoxItemType::BREAK) && !IsMenuEnabled() )
1728                 bBreak = true;
1729 
1730             if ( bBreak )
1731             {
1732                 nLines++;
1733 
1734                 // Add break before the entire group or take group apart?
1735                 if ( (it->meType == ToolBoxItemType::BREAK) ||
1736                      (nLineStart == nGroupStart) )
1737                 {
1738                     if ( nLineWidth > nMaxLineWidth )
1739                         nMaxLineWidth = nLineWidth;
1740 
1741                     nLineWidth = 0;
1742                     nLineStart = it - mpData->m_aItems.begin();
1743                     nGroupStart = nLineStart;
1744                     it->mbBreak = true;
1745                     bBreak = false;
1746                 }
1747                 else
1748                 {
1749                     if ( nLastGroupLineWidth > nMaxLineWidth )
1750                         nMaxLineWidth = nLastGroupLineWidth;
1751 
1752                     // if the break is added before the group, set it to
1753                     // beginning of line and re-calculate
1754                     nLineWidth = 0;
1755                     nLineStart = nGroupStart;
1756                     it = mpData->m_aItems.begin() + nGroupStart;
1757                     continue;
1758                 }
1759             }
1760             else
1761             {
1762                 if( ImplIsFloatingMode() || !IsMenuEnabled() ) // no group breaking when being docked single-line
1763                 {
1764                     if ( (it->meType != ToolBoxItemType::BUTTON) || bWindow )
1765                     {
1766                         // found separator or break
1767                         nLastGroupLineWidth = nLineWidth;
1768                         nGroupStart = it - mpData->m_aItems.begin();
1769                         if ( !bWindow )
1770                             nGroupStart++;
1771                     }
1772                 }
1773             }
1774 
1775             nLineWidth += nCurWidth;
1776         }
1777 
1778         ++it;
1779     }
1780 
1781     if ( pMaxLineWidth )
1782     {
1783         if ( nLineWidth > nMaxLineWidth )
1784             nMaxLineWidth = nLineWidth;
1785 
1786         if( ImplIsFloatingMode() && !ImplIsInPopupMode() )
1787         {
1788             // leave enough space to display buttons in the decoration
1789             tools::Long aMinWidth = 2 * GetSettings().GetStyleSettings().GetFloatTitleHeight();
1790             if( nMaxLineWidth < aMinWidth )
1791                 nMaxLineWidth = aMinWidth;
1792         }
1793         *pMaxLineWidth = nMaxLineWidth;
1794     }
1795 
1796     return nLines;
1797 }
1798 
ImplGetOptimalFloatingSize()1799 Size ToolBox::ImplGetOptimalFloatingSize()
1800 {
1801     if( !ImplIsFloatingMode() )
1802         return Size();
1803 
1804     Size aCurrentSize( mnDX, mnDY );
1805     Size aSize1( aCurrentSize );
1806     Size aSize2( aCurrentSize );
1807 
1808     // try to preserve current height
1809 
1810     // calc number of floating lines for current window height
1811     ImplToolItems::size_type nFloatLinesHeight = ImplCalcLines( mnDY );
1812     // calc window size according to this number
1813     aSize1 = ImplCalcFloatSize( nFloatLinesHeight );
1814 
1815     if( aCurrentSize == aSize1 )
1816         return aSize1;
1817 
1818     // try to preserve current width
1819 
1820     tools::Long nLineHeight = std::max( mnWinHeight, mnMaxItemHeight );
1821     int nBorderX = 2*TB_BORDER_OFFSET1 + mnLeftBorder + mnRightBorder;
1822     int nBorderY = 2*TB_BORDER_OFFSET2 + mnTopBorder + mnBottomBorder;
1823     Size aSz( aCurrentSize );
1824     sal_Int32 maxX;
1825     ImplToolItems::size_type nLines = ImplCalcBreaks( aSz.Width()-nBorderX, &maxX, mbHorz );
1826 
1827     ImplToolItems::size_type manyLines = 1000;
1828     Size aMinimalFloatSize = ImplCalcFloatSize( manyLines );
1829 
1830     aSz.setHeight( nBorderY + nLineHeight * nLines );
1831     // line space when more than one line
1832     if ( mbLineSpacing )
1833         aSz.AdjustHeight((nLines-1)*TB_LINESPACING );
1834 
1835     aSz.setWidth( nBorderX + maxX );
1836 
1837     // avoid clipping of any items
1838     if( aSz.Width() < aMinimalFloatSize.Width() )
1839         aSize2 = ImplCalcFloatSize( nLines );
1840     else
1841         aSize2 = aSz;
1842 
1843     if( aCurrentSize == aSize2 )
1844         return aSize2;
1845 
1846     // set the size with the smallest delta as the current size
1847     tools::Long dx1 = std::abs( mnDX - aSize1.Width() );
1848     tools::Long dy1 = std::abs( mnDY - aSize1.Height() );
1849 
1850     tools::Long dx2 = std::abs( mnDX - aSize2.Width() );
1851     tools::Long dy2 = std::abs( mnDY - aSize2.Height() );
1852 
1853     if( dx1*dy1 < dx2*dy2 )
1854         aCurrentSize = aSize1;
1855     else
1856         aCurrentSize = aSize2;
1857 
1858     return aCurrentSize;
1859 }
1860 
1861 namespace
1862 {
lcl_hideDoubleSeparators(ToolBox::ImplToolItems & rItems)1863 void lcl_hideDoubleSeparators( ToolBox::ImplToolItems& rItems )
1864 {
1865     bool bLastSep( true );
1866     ToolBox::ImplToolItems::iterator it;
1867     for ( it = rItems.begin(); it != rItems.end(); ++it )
1868     {
1869         if ( it->meType == ToolBoxItemType::SEPARATOR )
1870         {
1871             it->mbVisible = false;
1872             if ( !bLastSep )
1873             {
1874                 // check if any visible items have to appear behind it
1875                 if (std::any_of(it + 1, rItems.end(), [](const ImplToolItem& rItem) {
1876                         return (rItem.meType == ToolBoxItemType::BUTTON) && rItem.mbVisible; }))
1877                     it->mbVisible = true;
1878             }
1879             bLastSep = true;
1880         }
1881         else if ( it->mbVisible )
1882             bLastSep = false;
1883     }
1884 }
1885 }
1886 
ImplFormat(bool bResize)1887 void ToolBox::ImplFormat( bool bResize )
1888 {
1889     // Has to re-formatted
1890     if ( !mbFormat )
1891         return;
1892 
1893     mpData->ImplClearLayoutData();
1894 
1895     // recalculate positions and sizes
1896     tools::Rectangle       aEmptyRect;
1897     tools::Long            nLineSize;
1898     tools::Long            nLeft;
1899     tools::Long            nTop;
1900     tools::Long            nMax;   // width of layoutarea in pixels
1901     ImplToolItems::size_type nFormatLine;
1902     bool            bMustFullPaint;
1903 
1904     ImplDockingWindowWrapper *pWrapper = ImplGetDockingManager()->GetDockingWindowWrapper( this );
1905     bool bIsInPopupMode = ImplIsInPopupMode();
1906 
1907     maFloatSizes.clear();
1908 
1909     // compute border sizes
1910     ImplCalcBorder( meAlign, mnLeftBorder, mnTopBorder, mnRightBorder, mnBottomBorder );
1911 
1912     // update drag area (where the 'grip' will be placed)
1913     tools::Rectangle aOldDragRect;
1914     if( pWrapper )
1915         aOldDragRect = pWrapper->GetDragArea();
1916     ImplUpdateDragArea();
1917 
1918     bMustFullPaint = ImplCalcItem();
1919 
1920     // calculate new size during interactive resize or
1921     // set computed size when formatting only
1922     if ( ImplIsFloatingMode() )
1923     {
1924         if ( bResize )
1925             mnFloatLines = ImplCalcLines( mnDY );
1926         else
1927             SetOutputSizePixel( ImplGetOptimalFloatingSize() );
1928     }
1929 
1930     // Horizontal
1931     if ( mbHorz )
1932     {
1933         tools::Long nBottom;
1934         // nLineSize: height of a single line, will fit highest item
1935         nLineSize = mnMaxItemHeight;
1936 
1937         if ( mnWinHeight > mnMaxItemHeight )
1938             nLineSize = mnWinHeight;
1939 
1940         if ( mbScroll )
1941         {
1942             nMax        = mnDX;
1943             mnVisLines  = ImplCalcLines( mnDY );
1944         }
1945         else
1946         {
1947             // layout over all lines
1948             mnVisLines  = mnLines;
1949             nMax        = TB_MAXNOSCROLL;
1950         }
1951 
1952         // add in all border offsets
1953         if ( mnWinStyle & WB_BORDER )
1954         {
1955             nLeft       = TB_BORDER_OFFSET1 + mnLeftBorder;
1956             nTop        = TB_BORDER_OFFSET2 + mnTopBorder;
1957             nBottom     = TB_BORDER_OFFSET1 + mnBottomBorder;
1958             nMax       -= nLeft + TB_BORDER_OFFSET1 + mnRightBorder;
1959         }
1960         else
1961         {
1962             nLeft       = 0;
1963             nTop        = 0;
1964             nBottom     = 0;
1965         }
1966 
1967         // adjust linesize if docked in single-line mode (i.e. when using a clipped item menu)
1968         // we have to center all items in the window height
1969         if( IsMenuEnabled() && !ImplIsFloatingMode() )
1970         {
1971             tools::Long  nWinHeight = mnDY - nTop - nBottom;
1972             if( nWinHeight > nLineSize )
1973                 nLineSize = nWinHeight;
1974         }
1975     }
1976     else
1977     {
1978         tools::Long nRight;
1979         nLineSize = mnMaxItemWidth;
1980 
1981         if ( mbScroll )
1982         {
1983             mnVisLines  = ImplCalcLines( mnDX );
1984             nMax        = mnDY;
1985         }
1986         else
1987         {
1988             mnVisLines  = mnLines;
1989             nMax        = TB_MAXNOSCROLL;
1990         }
1991 
1992         if ( mnWinStyle & WB_BORDER )
1993         {
1994             nTop        = TB_BORDER_OFFSET1 + mnTopBorder;
1995             nLeft       = TB_BORDER_OFFSET2 + mnLeftBorder;
1996             nRight      = TB_BORDER_OFFSET2 + mnRightBorder;
1997             nMax       -= nTop + TB_BORDER_OFFSET1 + mnBottomBorder;
1998         }
1999         else
2000         {
2001             nLeft       = 0;
2002             nTop        = 0;
2003             nRight      = 0;
2004         }
2005 
2006         // adjust linesize if docked in single-line mode (i.e. when using a clipped item menu)
2007         // we have to center all items in the window height
2008         if( !ImplIsFloatingMode() && IsMenuEnabled() )
2009         {
2010             tools::Long  nWinWidth = mnDX - nLeft - nRight;
2011             if( nWinWidth > nLineSize )
2012                 nLineSize = nWinWidth;
2013         }
2014     }
2015 
2016     // no calculation if the window has no size (nMax=0)
2017     // non scrolling toolboxes must be computed though
2018     if ( (nMax <= 0) && mbScroll )
2019     {
2020         mnVisLines   = 1;
2021         mnCurLine    = 1;
2022         mnCurLines   = 1;
2023 
2024         for (auto & item : mpData->m_aItems)
2025         {
2026             item.maRect = aEmptyRect;
2027         }
2028 
2029         maLowerRect = aEmptyRect;
2030         maUpperRect = aEmptyRect;
2031     }
2032     else
2033     {
2034         // init start values
2035         tools::Long nX = nLeft;    // top-left offset
2036         tools::Long nY = nTop;
2037         nFormatLine = 1;
2038 
2039         // save old scroll rectangles and reset them
2040         tools::Rectangle aOldLowerRect = maLowerRect;
2041         tools::Rectangle aOldUpperRect = maUpperRect;
2042         tools::Rectangle aOldMenubuttonRect = mpData->maMenubuttonItem.maRect;
2043         maUpperRect = aEmptyRect;
2044         maLowerRect = aEmptyRect;
2045         mpData->maMenubuttonItem.maRect = aEmptyRect;
2046 
2047         // do we have any toolbox items at all ?
2048         if ( !mpData->m_aItems.empty() || IsMenuEnabled() )
2049         {
2050             lcl_hideDoubleSeparators( mpData->m_aItems );
2051 
2052             // compute line breaks and visible lines give the current window width (nMax)
2053             // the break indicators will be stored within each item (it->mbBreak)
2054             mnCurLines = ImplCalcBreaks( nMax, nullptr, mbHorz );
2055 
2056             // check for scrollbar buttons or dropdown menu
2057             // (if a menu is enabled, this will be used to store clipped
2058             //  items and no scroll buttons will appear)
2059             if ( (!ImplIsFloatingMode() && (mnCurLines > mnVisLines) && mbScroll ) ||
2060                 IsMenuEnabled() )
2061             {
2062                 // compute linebreaks again, incorporating scrollbar buttons
2063                 if( !IsMenuEnabled() )
2064                 {
2065                     nMax -= TB_SPIN_SIZE+TB_SPIN_OFFSET;
2066                     mnCurLines = ImplCalcBreaks( nMax, nullptr, mbHorz );
2067                 }
2068 
2069                 // compute scroll rectangles or menu button
2070                 if ( mbHorz )
2071                 {
2072                     if( IsMenuEnabled() && !ImplHasExternalMenubutton() && !bIsInPopupMode )
2073                     {
2074                         if( !ImplIsFloatingMode() )
2075                         {
2076                             mpData->maMenubuttonItem.maRect.SetRight( mnDX - 2 );
2077                             mpData->maMenubuttonItem.maRect.SetTop( nTop );
2078                             mpData->maMenubuttonItem.maRect.SetBottom( mnDY-mnBottomBorder-TB_BORDER_OFFSET2-1 );
2079                         }
2080                         else
2081                         {
2082                             mpData->maMenubuttonItem.maRect.SetRight( mnDX - mnRightBorder-TB_BORDER_OFFSET1-1 );
2083                             mpData->maMenubuttonItem.maRect.SetTop( nTop );
2084                             mpData->maMenubuttonItem.maRect.SetBottom( mnDY-mnBottomBorder-TB_BORDER_OFFSET2-1 );
2085                         }
2086                         mpData->maMenubuttonItem.maRect.SetLeft( mpData->maMenubuttonItem.maRect.Right() - mpData->mnMenuButtonWidth );
2087                     }
2088                     else
2089                     {
2090                         maUpperRect.SetLeft( nLeft+nMax+TB_SPIN_OFFSET );
2091                         maUpperRect.SetRight( maUpperRect.Left()+TB_SPIN_SIZE-1 );
2092                         maUpperRect.SetTop( nTop );
2093                         maLowerRect.SetBottom( mnDY-mnBottomBorder-TB_BORDER_OFFSET2-1 );
2094                         maLowerRect.SetLeft( maUpperRect.Left() );
2095                         maLowerRect.SetRight( maUpperRect.Right() );
2096                         maUpperRect.SetBottom( maUpperRect.Top() +
2097                                             (maLowerRect.Bottom()-maUpperRect.Top())/2 );
2098                         maLowerRect.SetTop( maUpperRect.Bottom() );
2099                     }
2100                 }
2101                 else
2102                 {
2103                     if( IsMenuEnabled() && !ImplHasExternalMenubutton() && !bIsInPopupMode )
2104                     {
2105                         if( !ImplIsFloatingMode() )
2106                         {
2107                             mpData->maMenubuttonItem.maRect.SetBottom( mnDY - 2 );
2108                             mpData->maMenubuttonItem.maRect.SetLeft( nLeft );
2109                             mpData->maMenubuttonItem.maRect.SetRight( mnDX-mnRightBorder-TB_BORDER_OFFSET2-1 );
2110                         }
2111                         else
2112                         {
2113                             mpData->maMenubuttonItem.maRect.SetBottom( mnDY - mnBottomBorder-TB_BORDER_OFFSET1-1 );
2114                             mpData->maMenubuttonItem.maRect.SetLeft( nLeft );
2115                             mpData->maMenubuttonItem.maRect.SetRight( mnDX-mnRightBorder-TB_BORDER_OFFSET2-1 );
2116                         }
2117                         mpData->maMenubuttonItem.maRect.SetTop( mpData->maMenubuttonItem.maRect.Bottom() - mpData->mnMenuButtonWidth );
2118                     }
2119                     else
2120                     {
2121                         maUpperRect.SetTop( nTop+nMax+TB_SPIN_OFFSET );
2122                         maUpperRect.SetBottom( maUpperRect.Top()+TB_SPIN_SIZE-1 );
2123                         maUpperRect.SetLeft( nLeft );
2124                         maLowerRect.SetRight( mnDX-mnRightBorder-TB_BORDER_OFFSET2-1 );
2125                         maLowerRect.SetTop( maUpperRect.Top() );
2126                         maLowerRect.SetBottom( maUpperRect.Bottom() );
2127                         maUpperRect.SetRight( maUpperRect.Left() +
2128                                             (maLowerRect.Right()-maUpperRect.Left())/2 );
2129                         maLowerRect.SetLeft( maUpperRect.Right() );
2130                     }
2131                 }
2132             }
2133 
2134             // no scrolling when there is a "more"-menu
2135             // anything will "fit" in a single line then
2136             if( IsMenuEnabled() )
2137                 mnCurLines = 1;
2138 
2139             // determine the currently visible line
2140             if ( mnVisLines >= mnCurLines )
2141                 mnCurLine = 1;
2142             else if ( mnCurLine+mnVisLines-1 > mnCurLines )
2143                 mnCurLine = mnCurLines - (mnVisLines-1);
2144 
2145             tools::Long firstItemCenter = 0;
2146             for (auto & item : mpData->m_aItems)
2147             {
2148                 item.mbShowWindow = false;
2149 
2150                 // check for line break and advance nX/nY accordingly
2151                 if ( item.mbBreak )
2152                 {
2153                     nFormatLine++;
2154 
2155                     // increment starting with the second line
2156                     if ( nFormatLine > mnCurLine )
2157                     {
2158                         if ( mbHorz )
2159                         {
2160                             nX = nLeft;
2161                             if ( mbLineSpacing )
2162                                 nY += nLineSize+TB_LINESPACING;
2163                             else
2164                                 nY += nLineSize;
2165                         }
2166                         else
2167                         {
2168                             nY = nTop;
2169                             if ( mbLineSpacing )
2170                                 nX += nLineSize+TB_LINESPACING;
2171                             else
2172                                 nX += nLineSize;
2173                         }
2174                     }
2175                 }
2176 
2177                 if ( !item.mbVisible || (nFormatLine < mnCurLine) ||
2178                      (nFormatLine > mnCurLine+mnVisLines-1) )
2179                      // item is not visible
2180                     item.maCalcRect = aEmptyRect;
2181                 else
2182                 {
2183                     // 1. determine current item width/height
2184                     // take window size and orientation into account, because this affects the size of item windows
2185 
2186                     Size aCurrentItemSize( item.GetSize( mbHorz, mbScroll, nMax, Size(mnMaxItemWidth, mnMaxItemHeight) ) );
2187 
2188                     // 2. position item rect and use size from step 1
2189                     // items will be centered horizontally (if mbHorz) or vertically
2190                     // advance nX and nY accordingly
2191 
2192                     if ( mbHorz )
2193                     {
2194                         // In special mode Locked horizontal positions of all items remain unchanged.
2195 
2196                         if ( mbIsArranged && meLayoutMode == ToolBoxLayoutMode::Locked && mnLines == 1 && item.maRect.Left() > 0 )
2197                             nX = item.maRect.Left();
2198                         item.maCalcRect.SetLeft( nX );
2199 
2200                         // In special mode Locked first item's vertical position remains unchanged. Consecutive items vertical
2201                         // positions are centered around first item's vertical position. If an item's height exceeds available
2202                         // space, item's vertical position remains unchanged too.
2203 
2204                         if ( mbIsArranged && meLayoutMode == ToolBoxLayoutMode::Locked && mnLines == 1 )
2205                             if ( firstItemCenter > 0 )
2206                                 if ( firstItemCenter-aCurrentItemSize.Height()/2 > nY  )
2207                                     item.maCalcRect.SetTop( firstItemCenter-aCurrentItemSize.Height()/2 );
2208                                 else
2209                                     item.maCalcRect.SetTop( item.maRect.Top() );
2210                             else
2211                             {
2212                                 item.maCalcRect.SetTop( item.maRect.Top() );
2213                                 firstItemCenter = item.maRect.Top()+aCurrentItemSize.Height()/2;
2214                             }
2215                         else
2216                             item.maCalcRect.SetTop( nY+(nLineSize-aCurrentItemSize.Height())/2 );
2217                         item.maCalcRect.SetRight( nX+aCurrentItemSize.Width()-1 );
2218                         item.maCalcRect.SetBottom( item.maCalcRect.Top()+aCurrentItemSize.Height()-1 );
2219                         nX += aCurrentItemSize.Width();
2220                     }
2221                     else
2222                     {
2223                         item.maCalcRect.SetLeft( nX+(nLineSize-aCurrentItemSize.Width())/2 );
2224                         item.maCalcRect.SetTop( nY );
2225                         item.maCalcRect.SetRight( item.maCalcRect.Left()+aCurrentItemSize.Width()-1 );
2226                         item.maCalcRect.SetBottom( nY+aCurrentItemSize.Height()-1 );
2227                         nY += aCurrentItemSize.Height();
2228                     }
2229                 }
2230 
2231                 // position window items into calculated item rect
2232                 if ( item.mpWindow )
2233                 {
2234                     if ( item.mbShowWindow )
2235                     {
2236                         Point aPos( item.maCalcRect.Left(), item.maCalcRect.Top() );
2237 
2238                         assert( item.maCalcRect.Top() >= 0 );
2239 
2240                         item.mpWindow->SetPosPixel( aPos );
2241                         item.mpWindow->Show();
2242                     }
2243                     else
2244                         item.mpWindow->Hide();
2245                 }
2246             } // end of loop over all items
2247             mbIsArranged = true;
2248         }
2249         else
2250             // we have no toolbox items
2251             mnCurLines = 1;
2252 
2253         if( IsMenuEnabled() && ImplIsFloatingMode() && !ImplHasExternalMenubutton() && !bIsInPopupMode )
2254         {
2255             // custom menu will be the last button in floating mode
2256             ImplToolItem &rIt = mpData->maMenubuttonItem;
2257 
2258             if ( mbHorz )
2259             {
2260                 rIt.maRect.SetLeft( nX+TB_MENUBUTTON_OFFSET );
2261                 rIt.maRect.SetTop( nY );
2262                 rIt.maRect.SetRight( rIt.maRect.Left() + mpData->mnMenuButtonWidth );
2263                 rIt.maRect.SetBottom( nY+nLineSize-1 );
2264                 nX += rIt.maItemSize.Width();
2265             }
2266             else
2267             {
2268                 rIt.maRect.SetLeft( nX );
2269                 rIt.maRect.SetTop( nY+TB_MENUBUTTON_OFFSET );
2270                 rIt.maRect.SetRight( nX+nLineSize-1 );
2271                 rIt.maRect.SetBottom( rIt.maRect.Top() + mpData->mnMenuButtonWidth );
2272                 nY += rIt.maItemSize.Height();
2273             }
2274         }
2275 
2276         // if toolbox visible trigger paint for changed regions
2277         if ( IsVisible() && !mbFullPaint )
2278         {
2279             if ( bMustFullPaint )
2280             {
2281                 maPaintRect = tools::Rectangle( mnLeftBorder, mnTopBorder,
2282                                          mnDX-mnRightBorder, mnDY-mnBottomBorder );
2283             }
2284             else
2285             {
2286                 if ( aOldLowerRect != maLowerRect )
2287                 {
2288                     maPaintRect.Union( maLowerRect );
2289                     maPaintRect.Union( aOldLowerRect );
2290                 }
2291                 if ( aOldUpperRect != maUpperRect )
2292                 {
2293                     maPaintRect.Union( maUpperRect );
2294                     maPaintRect.Union( aOldUpperRect );
2295                 }
2296                 if ( aOldMenubuttonRect != mpData->maMenubuttonItem.maRect )
2297                 {
2298                     maPaintRect.Union( mpData->maMenubuttonItem.maRect );
2299                     maPaintRect.Union( aOldMenubuttonRect );
2300                 }
2301                 if ( pWrapper && aOldDragRect != pWrapper->GetDragArea() )
2302                 {
2303                     maPaintRect.Union( pWrapper->GetDragArea() );
2304                     maPaintRect.Union( aOldDragRect );
2305                 }
2306 
2307                 for (auto const& item : mpData->m_aItems)
2308                 {
2309                     if ( item.maRect != item.maCalcRect )
2310                     {
2311                         maPaintRect.Union( item.maRect );
2312                         maPaintRect.Union( item.maCalcRect );
2313                     }
2314                 }
2315             }
2316 
2317             Invalidate( maPaintRect );
2318         }
2319 
2320         // store the new calculated item rects
2321         maPaintRect = aEmptyRect;
2322         for (auto & item : mpData->m_aItems)
2323             item.maRect = item.maCalcRect;
2324     }
2325 
2326     // indicate formatting is done
2327     mbFormat = false;
2328 }
2329 
IMPL_LINK_NOARG(ToolBox,ImplDropdownLongClickHdl,Timer *,void)2330 IMPL_LINK_NOARG(ToolBox, ImplDropdownLongClickHdl, Timer *, void)
2331 {
2332     if (mnCurPos == ITEM_NOTFOUND ||
2333         !(mpData->m_aItems[ mnCurPos ].mnBits & ToolBoxItemBits::DROPDOWN))
2334         return;
2335 
2336     mpData->mbDropDownByKeyboard = false;
2337     mpData->maDropdownClickHdl.Call( this );
2338 
2339     // do not reset data if the dropdown handler opened a floating window
2340     // see ImplFloatControl()
2341     if( !mpFloatWin )
2342     {
2343         // no floater was opened
2344         Deactivate();
2345         InvalidateItem(mnCurPos);
2346 
2347         mnCurPos         = ITEM_NOTFOUND;
2348         mnCurItemId      = ToolBoxItemId(0);
2349         mnDownItemId     = ToolBoxItemId(0);
2350         mnMouseModifier  = 0;
2351         mnHighItemId     = ToolBoxItemId(0);
2352     }
2353 }
2354 
IMPL_LINK_NOARG(ToolBox,ImplUpdateHdl,Timer *,void)2355 IMPL_LINK_NOARG(ToolBox, ImplUpdateHdl, Timer *, void)
2356 {
2357 
2358     if( mbFormat && mpData )
2359         ImplFormat();
2360 }
2361 
ImplDrawMoreIndicator(vcl::RenderContext & rRenderContext,const tools::Rectangle & rRect)2362 static void ImplDrawMoreIndicator(vcl::RenderContext& rRenderContext, const tools::Rectangle& rRect)
2363 {
2364     const Image pImage(StockImage::Yes, CHEVRON);
2365     Size aImageSize = pImage.GetSizePixel();
2366     tools::Long x = rRect.Left() + (rRect.getOpenWidth() - aImageSize.Width())/2;
2367     tools::Long y = rRect.Top() + (rRect.getOpenHeight() - aImageSize.Height())/2;
2368     DrawImageFlags nImageStyle = DrawImageFlags::NONE;
2369 
2370     rRenderContext.DrawImage(Point(x,y), pImage, nImageStyle);
2371 }
2372 
ImplDrawDropdownArrow(vcl::RenderContext & rRenderContext,const tools::Rectangle & rDropDownRect,bool bSetColor,bool bRotate)2373 static void ImplDrawDropdownArrow(vcl::RenderContext& rRenderContext, const tools::Rectangle& rDropDownRect, bool bSetColor, bool bRotate )
2374 {
2375     bool bLineColor = rRenderContext.IsLineColor();
2376     bool bFillColor = rRenderContext.IsFillColor();
2377     Color aOldFillColor = rRenderContext.GetFillColor();
2378     Color aOldLineColor = rRenderContext.GetLineColor();
2379     rRenderContext.SetLineColor();
2380 
2381     if ( bSetColor )
2382     {
2383         if (rRenderContext.GetSettings().GetStyleSettings().GetFaceColor().IsDark())
2384             rRenderContext.SetFillColor(COL_WHITE);
2385         else
2386             rRenderContext.SetFillColor(COL_BLACK);
2387     }
2388 
2389     tools::Polygon aPoly(4);
2390 
2391     // the assumption is, that the width always specifies the size of the expected arrow.
2392     const tools::Long nMargin = round(2 * rRenderContext.GetDPIScaleFactor());
2393     const tools::Long nSize = rDropDownRect.getOpenWidth() - 2 * nMargin;
2394     const tools::Long nHalfSize = (nSize + 1) / 2;
2395     const tools::Long x = rDropDownRect.Left() + nMargin + (bRotate ? (rDropDownRect.getOpenWidth() - nHalfSize) / 2 : 0);
2396     const tools::Long y = rDropDownRect.Top() + nMargin + (rDropDownRect.getOpenHeight() - (bRotate ? nSize : nHalfSize)) / 2;
2397 
2398     aPoly.SetPoint(Point(x, y), 0);
2399     if (bRotate) // >
2400     {
2401         aPoly.SetPoint(Point(x, y + nSize), 1);
2402         aPoly.SetPoint(Point(x + nHalfSize, y + nHalfSize), 2);
2403     }
2404     else // v
2405     {
2406         aPoly.SetPoint(Point(x + nHalfSize, y + nHalfSize), 1);
2407         aPoly.SetPoint(Point(x + nSize, y), 2);
2408     }
2409     aPoly.SetPoint(Point(x, y), 3);
2410 
2411     auto aaflags = rRenderContext.GetAntialiasing();
2412     rRenderContext.SetAntialiasing(AntialiasingFlags::Enable);
2413     rRenderContext.DrawPolygon( aPoly );
2414     rRenderContext.SetAntialiasing(aaflags);
2415 
2416     if( bFillColor )
2417         rRenderContext.SetFillColor(aOldFillColor);
2418     else
2419         rRenderContext.SetFillColor();
2420     if( bLineColor )
2421         rRenderContext.SetLineColor(aOldLineColor);
2422     else
2423         rRenderContext.SetLineColor();
2424 }
2425 
ImplDrawMenuButton(vcl::RenderContext & rRenderContext,bool bHighlight)2426 void ToolBox::ImplDrawMenuButton(vcl::RenderContext& rRenderContext, bool bHighlight)
2427 {
2428     if (mpData->maMenubuttonItem.maRect.IsEmpty())
2429         return;
2430 
2431     // #i53937# paint menu button only if necessary
2432     if (!ImplHasClippedItems())
2433         return;
2434 
2435     // execute pending paint requests
2436     ImplCheckUpdate();
2437 
2438     rRenderContext.Push(vcl::PushFlags::FILLCOLOR | vcl::PushFlags::LINECOLOR);
2439 
2440     // draw the 'more' indicator / button (>>)
2441     ImplErase(rRenderContext, mpData->maMenubuttonItem.maRect, bHighlight);
2442 
2443     if (bHighlight)
2444         ImplDrawButton(rRenderContext, mpData->maMenubuttonItem.maRect, 2, false, true, false );
2445 
2446     if (ImplHasClippedItems())
2447         ImplDrawMoreIndicator(rRenderContext, mpData->maMenubuttonItem.maRect);
2448 
2449     // store highlight state
2450     mpData->mbMenubuttonSelected = bHighlight;
2451 
2452     // restore colors
2453     rRenderContext.Pop();
2454 }
2455 
ImplDrawSpin(vcl::RenderContext & rRenderContext)2456 void ToolBox::ImplDrawSpin(vcl::RenderContext& rRenderContext)
2457 {
2458     bool    bTmpUpper;
2459     bool    bTmpLower;
2460 
2461     if ( maUpperRect.IsEmpty() || maLowerRect.IsEmpty() )
2462         return;
2463 
2464     bTmpUpper = mnCurLine > 1;
2465 
2466     bTmpLower = mnCurLine+mnVisLines-1 < mnCurLines;
2467 
2468     if ( !IsEnabled() )
2469     {
2470         bTmpUpper = false;
2471         bTmpLower = false;
2472     }
2473 
2474     ImplDrawUpDownButtons(rRenderContext, maUpperRect, maLowerRect,
2475                           false/*bUpperIn*/, false/*bLowerIn*/, bTmpUpper, bTmpLower, !mbHorz);
2476 }
2477 
ImplDrawSeparator(vcl::RenderContext & rRenderContext,ImplToolItems::size_type nPos,const tools::Rectangle & rRect)2478 void ToolBox::ImplDrawSeparator(vcl::RenderContext& rRenderContext, ImplToolItems::size_type nPos, const tools::Rectangle& rRect)
2479 {
2480     if ( nPos >= mpData->m_aItems.size() - 1 )
2481         // no separator if it's the last item
2482         return;
2483 
2484     ImplToolItem* pItem = &mpData->m_aItems[nPos];
2485     ImplToolItem* pPreviousItem = &mpData->m_aItems[nPos-1];
2486     ImplToolItem* pNextItem = &mpData->m_aItems[nPos+1];
2487 
2488     if ( ( pPreviousItem->mbShowWindow && pNextItem->mbShowWindow ) || pNextItem->mbBreak )
2489         // no separator between two windows or before a break
2490         return;
2491 
2492     bool bNativeOk = false;
2493     ControlPart nPart = IsHorizontal() ? ControlPart::SeparatorVert : ControlPart::SeparatorHorz;
2494     if (rRenderContext.IsNativeControlSupported(ControlType::Toolbar, nPart))
2495     {
2496         ImplControlValue aControlValue;
2497         bNativeOk = rRenderContext.DrawNativeControl(ControlType::Toolbar, nPart, rRect, ControlState::NONE, aControlValue, OUString());
2498     }
2499 
2500     /* Draw the widget only if it can't be drawn natively. */
2501     if (bNativeOk)
2502         return;
2503 
2504     tools::Long nCenterPos, nSlim;
2505     const StyleSettings& rStyleSettings = rRenderContext.GetSettings().GetStyleSettings();
2506     rRenderContext.SetLineColor(rStyleSettings.GetSeparatorColor());
2507     if (IsHorizontal())
2508     {
2509         nSlim = (pItem->maRect.Bottom() - pItem->maRect.Top ()) / 4;
2510         nCenterPos = pItem->maRect.Center().X();
2511         rRenderContext.DrawLine(Point(nCenterPos, pItem->maRect.Top() + nSlim),
2512                                 Point(nCenterPos, pItem->maRect.Bottom() - nSlim));
2513     }
2514     else
2515     {
2516         nSlim = (pItem->maRect.Right() - pItem->maRect.Left ()) / 4;
2517         nCenterPos = pItem->maRect.Center().Y();
2518         rRenderContext.DrawLine(Point(pItem->maRect.Left() + nSlim, nCenterPos),
2519                                 Point(pItem->maRect.Right() - nSlim, nCenterPos));
2520     }
2521 }
2522 
ImplDrawButton(vcl::RenderContext & rRenderContext,const tools::Rectangle & rRect,sal_uInt16 highlight,bool bChecked,bool bEnabled,bool bIsWindow)2523 void ToolBox::ImplDrawButton(vcl::RenderContext& rRenderContext, const tools::Rectangle &rRect, sal_uInt16 highlight,
2524                              bool bChecked, bool bEnabled, bool bIsWindow )
2525 {
2526     // draws toolbar button background either native or using a coloured selection
2527     // if bIsWindow is true, the corresponding item is a control and only a selection border will be drawn
2528 
2529     bool bNativeOk = false;
2530     if( !bIsWindow && rRenderContext.IsNativeControlSupported( ControlType::Toolbar, ControlPart::Button ) )
2531     {
2532         ImplControlValue    aControlValue;
2533         ControlState        nState = ControlState::NONE;
2534 
2535         if ( highlight == 1 )   nState |= ControlState::PRESSED;
2536         if ( highlight == 2 )     nState |= ControlState::ROLLOVER;
2537         if ( bEnabled )         nState |= ControlState::ENABLED;
2538 
2539         aControlValue.setTristateVal( bChecked ? ButtonValue::On : ButtonValue::Off );
2540 
2541         bNativeOk = rRenderContext.DrawNativeControl( ControlType::Toolbar, ControlPart::Button,
2542                                               rRect, nState, aControlValue, OUString() );
2543     }
2544 
2545     if (!bNativeOk)
2546         vcl::RenderTools::DrawSelectionBackground(rRenderContext, *this, rRect, bIsWindow ? 3 : highlight,
2547                                                   bChecked, true, bIsWindow, nullptr, 2);
2548 }
2549 
ImplDrawItem(vcl::RenderContext & rRenderContext,ImplToolItems::size_type nPos,sal_uInt16 nHighlight)2550 void ToolBox::ImplDrawItem(vcl::RenderContext& rRenderContext, ImplToolItems::size_type nPos, sal_uInt16 nHighlight)
2551 {
2552     if (nPos >= mpData->m_aItems.size())
2553         return;
2554 
2555     // execute pending paint requests
2556     ImplCheckUpdate();
2557 
2558     rRenderContext.SetFillColor();
2559 
2560     ImplToolItem* pItem = &mpData->m_aItems[nPos];
2561 
2562     if (!pItem->mbEnabled)
2563         nHighlight = 0;
2564 
2565     // if the rectangle is outside visible area
2566     if (pItem->maRect.IsEmpty())
2567         return;
2568 
2569     const StyleSettings& rStyleSettings = rRenderContext.GetSettings().GetStyleSettings();
2570 
2571     // no gradient background for items that have a popup open
2572     bool bHasOpenPopup = mpFloatWin && (mnDownItemId==pItem->mnId);
2573 
2574     bool bHighContrastWhite = false;
2575     // check the face color as highcontrast indicator
2576     // because the toolbox itself might have a gradient
2577     if (rStyleSettings.GetFaceColor() == COL_WHITE)
2578         bHighContrastWhite = true;
2579 
2580     // Compute buttons area.
2581     Size    aBtnSize    = pItem->maRect.GetSize();
2582 
2583     /* Compute the button/separator rectangle here, we'll need it for
2584      * both the buttons and the separators. */
2585     tools::Rectangle aButtonRect( pItem->maRect.TopLeft(), aBtnSize );
2586     tools::Long    nOffX       = SMALLBUTTON_OFF_NORMAL_X;
2587     tools::Long    nOffY       = SMALLBUTTON_OFF_NORMAL_Y;
2588     tools::Long    nImageOffX  = 0;
2589     tools::Long    nImageOffY  = 0;
2590     DrawButtonFlags nStyle      = DrawButtonFlags::NONE;
2591 
2592     // draw separators
2593     if ( (pItem->meType == ToolBoxItemType::SEPARATOR) && nPos > 0 )
2594     {
2595         ImplDrawSeparator(rRenderContext, nPos, aButtonRect);
2596     }
2597 
2598     // do nothing if item is no button or will be displayed as window
2599     if ( (pItem->meType != ToolBoxItemType::BUTTON) || pItem->mbShowWindow )
2600         return;
2601 
2602     if ( pItem->meState == TRISTATE_TRUE )
2603     {
2604         nStyle |= DrawButtonFlags::Checked;
2605     }
2606     else if ( pItem->meState == TRISTATE_INDET )
2607     {
2608         nStyle |= DrawButtonFlags::DontKnow;
2609     }
2610     if ( nHighlight == 1 )
2611     {
2612         nStyle |= DrawButtonFlags::Pressed;
2613     }
2614 
2615     ImplErase(rRenderContext, pItem->maRect, nHighlight != 0, bHasOpenPopup );
2616 
2617     nOffX += pItem->maRect.Left();
2618     nOffY += pItem->maRect.Top();
2619 
2620     // determine what has to be drawn on the button: image, text or both
2621     bool bImage;
2622     bool bText;
2623     ButtonType tmpButtonType = determineButtonType( pItem, meButtonType ); // default to toolbox setting
2624     pItem->DetermineButtonDrawStyle( tmpButtonType, bImage, bText );
2625 
2626     // compute output values
2627     tools::Long    nBtnWidth = aBtnSize.Width()-SMALLBUTTON_HSIZE;
2628     tools::Long    nBtnHeight = aBtnSize.Height()-SMALLBUTTON_VSIZE;
2629     Size    aImageSize;
2630 
2631     const bool bDropDown = (pItem->mnBits & ToolBoxItemBits::DROPDOWN) == ToolBoxItemBits::DROPDOWN;
2632     tools::Rectangle aDropDownRect;
2633     if (bDropDown)
2634         aDropDownRect = pItem->GetDropDownRect(mbHorz);
2635 
2636     if ( bImage )
2637     {
2638         const Image* pImage = &(pItem->maImage);
2639         aImageSize = pImage->GetSizePixel();
2640 
2641         // determine drawing flags
2642         DrawImageFlags nImageStyle = DrawImageFlags::NONE;
2643 
2644         if ( !pItem->mbEnabled || !IsEnabled() )
2645             nImageStyle |= DrawImageFlags::Disable;
2646 
2647         // #i35563# the dontknow state indicates different states at the same time
2648         // which should not be rendered disabled but normal
2649 
2650         // draw the image
2651         nImageOffX = nOffX;
2652         nImageOffY = nOffY;
2653         if ( ( (pItem->mnBits & (ToolBoxItemBits::LEFT|ToolBoxItemBits::DROPDOWN)) || bText )
2654             && ( meTextPosition == ToolBoxTextPosition::Right ) )
2655         {
2656             // left align also to leave space for drop down arrow
2657             // and when drawing text+image
2658             // just center in y, except for vertical (ie rotated text)
2659             if( mbHorz || !bText )
2660                 nImageOffY += (nBtnHeight-aImageSize.Height())/2;
2661         }
2662         else
2663         {
2664             nImageOffX += (nBtnWidth-(bDropDown ? aDropDownRect.getOpenWidth() : 0)+SMALLBUTTON_OFF_NORMAL_X-aImageSize.Width())/2;
2665             if ( meTextPosition == ToolBoxTextPosition::Right )
2666                 nImageOffY += (nBtnHeight-aImageSize.Height())/2;
2667         }
2668         if ( nHighlight != 0 || (pItem->meState == TRISTATE_TRUE) )
2669         {
2670             if( bHasOpenPopup )
2671                 ImplDrawFloatwinBorder(rRenderContext, pItem);
2672             else
2673                 ImplDrawButton(rRenderContext, aButtonRect, nHighlight, pItem->meState == TRISTATE_TRUE,
2674                                pItem->mbEnabled && IsEnabled(), pItem->mbShowWindow);
2675 
2676             if( nHighlight != 0 )
2677             {
2678                 if( bHighContrastWhite )
2679                     nImageStyle |= DrawImageFlags::ColorTransform;
2680             }
2681         }
2682         rRenderContext.DrawImage(Point( nImageOffX, nImageOffY ), *pImage, nImageStyle);
2683     }
2684 
2685     // draw the text
2686     bool bRotate = false;
2687     if ( bText )
2688     {
2689         const Size aTxtSize(GetOutDev()->GetCtrlTextWidth(pItem->maText), GetTextHeight());
2690         tools::Long nTextOffX = nOffX;
2691         tools::Long nTextOffY = nOffY;
2692 
2693         // rotate text when vertically docked
2694         vcl::Font aOldFont = rRenderContext.GetFont();
2695         if( pItem->mbVisibleText && !ImplIsFloatingMode() &&
2696             ((meAlign == WindowAlign::Left) || (meAlign == WindowAlign::Right)) )
2697         {
2698             bRotate = true;
2699 
2700             vcl::Font aRotateFont = aOldFont;
2701             aRotateFont.SetOrientation( 2700_deg10 );
2702 
2703             // center horizontally
2704             nTextOffX += aTxtSize.Height();
2705             nTextOffX += (nBtnWidth-aTxtSize.Height())/2;
2706 
2707             // add in image offset
2708             if( bImage )
2709                 nTextOffY = nImageOffY + aImageSize.Height() + TB_IMAGETEXTOFFSET;
2710 
2711             rRenderContext.SetFont(aRotateFont);
2712         }
2713         else
2714         {
2715             if ( meTextPosition == ToolBoxTextPosition::Right )
2716             {
2717                 // center vertically
2718                 nTextOffY += (nBtnHeight-aTxtSize.Height())/2;
2719 
2720                 // add in image offset
2721                 if( bImage )
2722                     nTextOffX = nImageOffX + aImageSize.Width() + TB_IMAGETEXTOFFSET;
2723             }
2724             else
2725             {
2726                 // center horizontally
2727                 nTextOffX += (nBtnWidth-(bDropDown ? aDropDownRect.getOpenWidth() : 0)+SMALLBUTTON_OFF_NORMAL_X-aTxtSize.Width() - TB_IMAGETEXTOFFSET)/2;
2728                 // set vertical position
2729                 nTextOffY += nBtnHeight - aTxtSize.Height();
2730             }
2731         }
2732 
2733         // draw selection only if not already drawn during image output (see above)
2734         if ( !bImage && (nHighlight != 0 || (pItem->meState == TRISTATE_TRUE) ) )
2735         {
2736             if( bHasOpenPopup )
2737                 ImplDrawFloatwinBorder(rRenderContext, pItem);
2738             else
2739                 ImplDrawButton(rRenderContext, pItem->maRect, nHighlight, pItem->meState == TRISTATE_TRUE,
2740                                pItem->mbEnabled && IsEnabled(), pItem->mbShowWindow );
2741         }
2742 
2743         DrawTextFlags nTextStyle = DrawTextFlags::NONE;
2744         if ( !pItem->mbEnabled )
2745             nTextStyle |= DrawTextFlags::Disable;
2746         rRenderContext.DrawCtrlText( Point( nTextOffX, nTextOffY ), pItem->maText,
2747                       0, pItem->maText.getLength(), nTextStyle );
2748         if ( bRotate )
2749             SetFont( aOldFont );
2750     }
2751 
2752     // paint optional drop down arrow
2753     if (!bDropDown)
2754         return;
2755 
2756     bool bSetColor = true;
2757     if ( !pItem->mbEnabled || !IsEnabled() )
2758     {
2759         bSetColor = false;
2760         rRenderContext.SetFillColor(rStyleSettings.GetShadowColor());
2761     }
2762 
2763     // dropdown only will be painted without inner border
2764     if( (pItem->mnBits & ToolBoxItemBits::DROPDOWNONLY) != ToolBoxItemBits::DROPDOWNONLY )
2765     {
2766         ImplErase(rRenderContext, aDropDownRect, nHighlight != 0, bHasOpenPopup);
2767 
2768         if( nHighlight != 0 || (pItem->meState == TRISTATE_TRUE) )
2769         {
2770             if( bHasOpenPopup )
2771                 ImplDrawFloatwinBorder(rRenderContext, pItem);
2772             else
2773                 ImplDrawButton(rRenderContext, aDropDownRect, nHighlight, pItem->meState == TRISTATE_TRUE,
2774                                pItem->mbEnabled && IsEnabled(), false);
2775         }
2776     }
2777     ImplDrawDropdownArrow(rRenderContext, aDropDownRect, bSetColor, bRotate);
2778 }
2779 
ImplDrawFloatwinBorder(vcl::RenderContext & rRenderContext,ImplToolItem const * pItem)2780 void ToolBox::ImplDrawFloatwinBorder(vcl::RenderContext& rRenderContext, ImplToolItem const * pItem)
2781 {
2782     if ( pItem->maRect.IsEmpty() )
2783         return;
2784 
2785     rRenderContext.SetLineColor(rRenderContext.GetSettings().GetStyleSettings().GetShadowColor());
2786     Point p1, p2;
2787 
2788     p1 = pItem->maRect.TopLeft();
2789     p1.AdjustX( 1 );
2790     p2 = pItem->maRect.TopRight();
2791     p2.AdjustX( -1 );
2792     rRenderContext.DrawLine( p1, p2);
2793     p1 = pItem->maRect.BottomLeft();
2794     p1.AdjustX( 1 );
2795     p2 = pItem->maRect.BottomRight();
2796     p2.AdjustX( -1 );
2797     rRenderContext.DrawLine( p1, p2);
2798 
2799     p1 = pItem->maRect.TopLeft();
2800     p1.AdjustY( 1 );
2801     p2 = pItem->maRect.BottomLeft();
2802     p2.AdjustY( -1 );
2803     rRenderContext.DrawLine( p1, p2);
2804     p1 = pItem->maRect.TopRight();
2805     p1.AdjustY( 1 );
2806     p2 = pItem->maRect.BottomRight();
2807     p2.AdjustY( -1 );
2808     rRenderContext.DrawLine( p1, p2);
2809 
2810 }
2811 
ImplFloatControl(bool bStart,FloatingWindow * pFloatWindow)2812 void ToolBox::ImplFloatControl( bool bStart, FloatingWindow* pFloatWindow )
2813 {
2814 
2815     if ( bStart )
2816     {
2817         mpFloatWin = pFloatWindow;
2818 
2819         // redraw item, to trigger drawing of a special border
2820         InvalidateItem(mnCurPos);
2821 
2822         mbDrag = false;
2823         EndTracking();
2824         if (IsMouseCaptured())
2825             ReleaseMouse();
2826     }
2827     else
2828     {
2829         mpFloatWin = nullptr;
2830 
2831         // if focus is still in this toolbox, then the floater was opened by keyboard
2832         // draw current item with highlight and keep old state
2833         bool bWasKeyboardActivate = mpData->mbDropDownByKeyboard;
2834 
2835         if ( mnCurPos != ITEM_NOTFOUND )
2836             InvalidateItem(mnCurPos);
2837         Deactivate();
2838 
2839         if( !bWasKeyboardActivate )
2840         {
2841             mnCurPos = ITEM_NOTFOUND;
2842             mnCurItemId = ToolBoxItemId(0);
2843             mnHighItemId = ToolBoxItemId(0);
2844         }
2845         mnDownItemId = ToolBoxItemId(0);
2846 
2847     }
2848 }
2849 
ShowLine(bool bNext)2850 void ToolBox::ShowLine( bool bNext )
2851 {
2852     mbFormat = true;
2853 
2854     if ( bNext )
2855         mnCurLine++;
2856     else
2857         mnCurLine--;
2858 
2859     ImplFormat();
2860 }
2861 
ImplHandleMouseMove(const MouseEvent & rMEvt,bool bRepeat)2862 bool ToolBox::ImplHandleMouseMove( const MouseEvent& rMEvt, bool bRepeat )
2863 {
2864     Point aMousePos = rMEvt.GetPosPixel();
2865 
2866     if ( !mpData )
2867         return false;
2868 
2869     // ToolBox active?
2870     if ( mbDrag && mnCurPos != ITEM_NOTFOUND )
2871     {
2872         // is the cursor over the item?
2873         ImplToolItem* pItem = &mpData->m_aItems[mnCurPos];
2874         if ( pItem->maRect.Contains( aMousePos ) )
2875         {
2876             if ( !mnCurItemId )
2877             {
2878                 InvalidateItem(mnCurPos);
2879                 mnCurItemId = pItem->mnId;
2880                 Highlight();
2881             }
2882 
2883             if ( (pItem->mnBits & ToolBoxItemBits::REPEAT) && bRepeat )
2884                 Select();
2885         }
2886         else
2887         {
2888             if ( mnCurItemId )
2889             {
2890                 InvalidateItem(mnCurPos);
2891                 mnCurItemId = ToolBoxItemId(0);
2892                 InvalidateItem(mnCurPos);
2893                 Highlight();
2894             }
2895         }
2896 
2897         return true;
2898     }
2899 
2900     if ( mbUpper )
2901     {
2902         bool bNewIn = maUpperRect.Contains( aMousePos );
2903         if ( bNewIn != mbIn )
2904         {
2905             mbIn = bNewIn;
2906             InvalidateSpin(true, false);
2907         }
2908         return true;
2909     }
2910 
2911     if ( mbLower )
2912     {
2913         bool bNewIn = maLowerRect.Contains( aMousePos );
2914         if ( bNewIn != mbIn )
2915         {
2916             mbIn = bNewIn;
2917             InvalidateSpin(false);
2918         }
2919         return true;
2920     }
2921 
2922     return false;
2923 }
2924 
ImplHandleMouseButtonUp(const MouseEvent & rMEvt,bool bCancel)2925 bool ToolBox::ImplHandleMouseButtonUp( const MouseEvent& rMEvt, bool bCancel )
2926 {
2927     if ( !mpData )
2928         return false;
2929 
2930     // stop eventual running dropdown timer
2931     if( mnCurPos < mpData->m_aItems.size() &&
2932         (mpData->m_aItems[mnCurPos].mnBits & ToolBoxItemBits::DROPDOWN ) )
2933     {
2934         mpData->maDropdownTimer.Stop();
2935     }
2936 
2937     if ( mbDrag )
2938     {
2939         Deactivate();
2940 
2941         if ( mbDrag )
2942             mbDrag = false;
2943         else
2944         {
2945             if ( mnCurPos == ITEM_NOTFOUND )
2946                 return true;
2947         }
2948 
2949         // has mouse been released on top of item?
2950         if( mnCurPos < mpData->m_aItems.size() )
2951         {
2952             ImplToolItem* pItem = &mpData->m_aItems[mnCurPos];
2953             if ( pItem->maRect.Contains( rMEvt.GetPosPixel() ) )
2954             {
2955                 mnCurItemId = pItem->mnId;
2956                 if ( !bCancel )
2957                 {
2958                     // execute AutoCheck if required
2959                     if ( pItem->mnBits & ToolBoxItemBits::AUTOCHECK )
2960                     {
2961                         if ( pItem->mnBits & ToolBoxItemBits::RADIOCHECK )
2962                         {
2963                             if ( pItem->meState != TRISTATE_TRUE )
2964                                 SetItemState( pItem->mnId, TRISTATE_TRUE );
2965                         }
2966                         else
2967                         {
2968                             if ( pItem->meState != TRISTATE_TRUE )
2969                                 pItem->meState = TRISTATE_TRUE;
2970                             else
2971                                 pItem->meState = TRISTATE_FALSE;
2972                         }
2973                     }
2974 
2975                     // do not call Select when Repeat is active, as in this
2976                     // case that was triggered already in MouseButtonDown
2977                     if ( !(pItem->mnBits & ToolBoxItemBits::REPEAT) )
2978                     {
2979                         // prevent from being destroyed in the select handler
2980                         VclPtr<vcl::Window> xWindow = this;
2981                         Select();
2982                         if ( xWindow->isDisposed() )
2983                             return true;
2984                     }
2985                 }
2986 
2987                 {
2988                 }
2989 
2990                 // Items not destroyed, in Select handler
2991                 if ( mnCurItemId )
2992                 {
2993                     // Get current pos for the case that items are inserted/removed
2994                     // in the toolBox
2995                     mnCurPos = GetItemPos( mnCurItemId );
2996                     if ( mnCurPos != ITEM_NOTFOUND )
2997                     {
2998                         InvalidateItem(mnCurPos);
2999                         GetOutDev()->Flush();
3000                     }
3001                 }
3002             }
3003         }
3004 
3005         mnCurPos         = ITEM_NOTFOUND;
3006         mnCurItemId      = ToolBoxItemId(0);
3007         mnDownItemId     = ToolBoxItemId(0);
3008         mnMouseModifier  = 0;
3009         return true;
3010     }
3011     else if ( mbUpper || mbLower )
3012     {
3013         if ( mbIn )
3014             ShowLine( !mbUpper );
3015         mbUpper = false;
3016         mbLower = false;
3017         mbIn    = false;
3018         InvalidateSpin();
3019         return true;
3020     }
3021 
3022     return false;
3023 }
3024 
MouseMove(const MouseEvent & rMEvt)3025 void ToolBox::MouseMove( const MouseEvent& rMEvt )
3026 {
3027     // pressing a modifier generates synthetic mouse moves
3028     // ignore it if keyboard selection is active
3029     if( HasFocus() && ( rMEvt.GetMode() & MouseEventModifiers::MODIFIERCHANGED ) )
3030         return;
3031 
3032     if ( ImplHandleMouseMove( rMEvt ) )
3033         return;
3034 
3035     Point aMousePos = rMEvt.GetPosPixel();
3036 
3037     // only highlight when the focus is not inside a child window of a toolbox
3038     // eg, in an edit control
3039     // and do not highlight when focus is in a different toolbox
3040     bool bDrawHotSpot = true;
3041     vcl::Window *pFocusWin = Application::GetFocusWindow();
3042 
3043     bool bFocusWindowIsAToolBoxChild = false;
3044     if (pFocusWin)
3045     {
3046         vcl::Window *pWin = pFocusWin->GetParent();
3047         while (pWin)
3048         {
3049             if(pWin->ImplGetWindowImpl() && pWin->ImplGetWindowImpl()->mbToolBox)
3050             {
3051                 bFocusWindowIsAToolBoxChild = true;
3052                 break;
3053             }
3054             pWin = pWin->GetParent();
3055         }
3056     }
3057 
3058     if( bFocusWindowIsAToolBoxChild || (pFocusWin && pFocusWin->ImplGetWindowImpl() && pFocusWin->ImplGetWindowImpl()->mbToolBox && pFocusWin != this) )
3059         bDrawHotSpot = false;
3060 
3061     if ( mbDragging )
3062     {
3063         ImplTBDragMgr* pMgr = ImplGetTBDragMgr();
3064         pMgr->Dragging( aMousePos );
3065         return;
3066     }
3067 
3068     PointerStyle eStyle = PointerStyle::Arrow;
3069 
3070     // change mouse cursor over drag area
3071     ImplDockingWindowWrapper *pWrapper = ImplGetDockingManager()->GetDockingWindowWrapper( this );
3072     if( pWrapper && pWrapper->GetDragArea().Contains( rMEvt.GetPosPixel() ) )
3073         eStyle = PointerStyle::Move;
3074 
3075     if ( (mnWinStyle & TB_WBLINESIZING) == TB_WBLINESIZING )
3076     {
3077         if ( rMEvt.GetMode() & MouseEventModifiers::SIMPLEMOVE )
3078         {
3079             sal_uInt16 nLinePtr = ImplTestLineSize( rMEvt.GetPosPixel() );
3080             if ( nLinePtr & DOCK_LINEHSIZE )
3081             {
3082                 if ( meAlign == WindowAlign::Left )
3083                     eStyle = PointerStyle::WindowESize;
3084                 else
3085                     eStyle = PointerStyle::WindowWSize;
3086             }
3087             else if ( nLinePtr & DOCK_LINEVSIZE )
3088             {
3089                 if ( meAlign == WindowAlign::Top )
3090                     eStyle = PointerStyle::WindowSSize;
3091                 else
3092                     eStyle = PointerStyle::WindowNSize;
3093             }
3094         }
3095     }
3096 
3097     if ( bDrawHotSpot )
3098     {
3099         bool bClearHigh = true;
3100         if ( !rMEvt.IsLeaveWindow() && (mnCurPos == ITEM_NOTFOUND) )
3101         {
3102             ImplToolItems::size_type nTempPos = 0;
3103             for (auto const& item : mpData->m_aItems)
3104             {
3105                 if ( item.maRect.Contains( aMousePos ) )
3106                 {
3107                     if ( (item.meType == ToolBoxItemType::BUTTON) && item.mbEnabled )
3108                     {
3109                         bClearHigh = false;
3110                         if ( mnHighItemId != item.mnId )
3111                         {
3112                             if ( mnHighItemId )
3113                             {
3114                                 ImplHideFocus();
3115                                 ImplToolItems::size_type nPos = GetItemPos( mnHighItemId );
3116                                 InvalidateItem(nPos);
3117                                 CallEventListeners( VclEventId::ToolboxHighlightOff, reinterpret_cast< void* >( nPos ) );
3118                             }
3119                             if ( mpData->mbMenubuttonSelected )
3120                             {
3121                                 // remove highlight from menubutton
3122                                 InvalidateMenuButton();
3123                             }
3124                             mnHighItemId = item.mnId;
3125                             InvalidateItem(nTempPos);
3126                             ImplShowFocus();
3127                             CallEventListeners( VclEventId::ToolboxHighlight );
3128                         }
3129                     }
3130                     break;
3131                 }
3132                 ++nTempPos;
3133             }
3134         }
3135 
3136         // only clear highlight when focus is not in toolbar
3137         bool bMenuButtonHit = mpData->maMenubuttonItem.maRect.Contains( aMousePos ) && ImplHasClippedItems();
3138         if ( !HasFocus() && (bClearHigh || bMenuButtonHit) )
3139         {
3140             if ( !bMenuButtonHit && mpData->mbMenubuttonSelected )
3141             {
3142                 // remove highlight from menubutton
3143                 InvalidateMenuButton();
3144             }
3145 
3146             if( mnHighItemId )
3147             {
3148                 ImplToolItems::size_type nClearPos = GetItemPos( mnHighItemId );
3149                 if ( nClearPos != ITEM_NOTFOUND )
3150                 {
3151                     InvalidateItem(nClearPos);
3152                     if( nClearPos != mnCurPos )
3153                         CallEventListeners( VclEventId::ToolboxHighlightOff, reinterpret_cast< void* >( nClearPos ) );
3154                 }
3155                 ImplHideFocus();
3156                 mnHighItemId = ToolBoxItemId(0);
3157             }
3158 
3159             if( bMenuButtonHit )
3160             {
3161                 InvalidateMenuButton();
3162             }
3163         }
3164     }
3165 
3166     if ( meLastStyle != eStyle )
3167     {
3168         meLastStyle = eStyle;
3169         SetPointer( eStyle );
3170     }
3171 
3172     DockingWindow::MouseMove( rMEvt );
3173 }
3174 
MouseButtonDown(const MouseEvent & rMEvt)3175 void ToolBox::MouseButtonDown( const MouseEvent& rMEvt )
3176 {
3177     // only trigger toolbox for left mouse button and when
3178     // we're not in normal operation
3179     if ( rMEvt.IsLeft() && !mbDrag && (mnCurPos == ITEM_NOTFOUND) )
3180     {
3181         // call activate already here, as items could
3182         // be exchanged
3183         Activate();
3184 
3185         // update ToolBox here, such that user knows it
3186         if ( mbFormat )
3187         {
3188             ImplFormat();
3189             PaintImmediately();
3190         }
3191 
3192         Point  aMousePos = rMEvt.GetPosPixel();
3193         ImplToolItems::size_type i = 0;
3194         ImplToolItems::size_type nNewPos = ITEM_NOTFOUND;
3195 
3196         // search for item that was clicked
3197         for (auto const& item : mpData->m_aItems)
3198         {
3199             // is this the item?
3200             if ( item.maRect.Contains( aMousePos ) )
3201             {
3202                 // do nothing if it is a separator or
3203                 // if the item has been disabled
3204                 if ( (item.meType == ToolBoxItemType::BUTTON) &&
3205                      !item.mbShowWindow )
3206                     nNewPos = i;
3207 
3208                 break;
3209             }
3210 
3211             i++;
3212         }
3213 
3214         // item found
3215         if ( nNewPos != ITEM_NOTFOUND )
3216         {
3217             if ( !mpData->m_aItems[nNewPos].mbEnabled )
3218             {
3219                 Deactivate();
3220                 return;
3221             }
3222 
3223             // update actual data
3224             StartTrackingFlags nTrackFlags = StartTrackingFlags::NONE;
3225             mnCurPos         = i;
3226             mnCurItemId      = mpData->m_aItems[nNewPos].mnId;
3227             mnDownItemId     = mnCurItemId;
3228             mnMouseModifier  = rMEvt.GetModifier();
3229             if ( mpData->m_aItems[nNewPos].mnBits & ToolBoxItemBits::REPEAT )
3230                 nTrackFlags |= StartTrackingFlags::ButtonRepeat;
3231 
3232             // update bDrag here, as it is evaluated in the EndSelection
3233             mbDrag = true;
3234 
3235             // on double-click: only call the handler, but do so before the button
3236             // is hit, as in the handler dragging
3237             // can be terminated
3238             if ( rMEvt.GetClicks() == 2 )
3239                 DoubleClick();
3240 
3241             if ( mbDrag )
3242             {
3243                 InvalidateItem(mnCurPos);
3244                 Highlight();
3245             }
3246 
3247             // was dropdown arrow pressed
3248             if( mpData->m_aItems[nNewPos].mnBits & ToolBoxItemBits::DROPDOWN )
3249             {
3250                 if( ( (mpData->m_aItems[nNewPos].mnBits & ToolBoxItemBits::DROPDOWNONLY) == ToolBoxItemBits::DROPDOWNONLY)
3251                     || mpData->m_aItems[nNewPos].GetDropDownRect( mbHorz ).Contains( aMousePos ))
3252                 {
3253                     // dropdownonly always triggers the dropdown handler, over the whole button area
3254 
3255                     // the drop down arrow should not trigger the item action
3256                     mpData->mbDropDownByKeyboard = false;
3257                     mpData->maDropdownClickHdl.Call( this );
3258 
3259                     // do not reset data if the dropdown handler opened a floating window
3260                     // see ImplFloatControl()
3261                     if( !mpFloatWin )
3262                     {
3263                         // no floater was opened
3264                         Deactivate();
3265                         InvalidateItem(mnCurPos);
3266 
3267                         mnCurPos         = ITEM_NOTFOUND;
3268                         mnCurItemId      = ToolBoxItemId(0);
3269                         mnDownItemId     = ToolBoxItemId(0);
3270                         mnMouseModifier  = 0;
3271                         mnHighItemId     = ToolBoxItemId(0);
3272                     }
3273                     return;
3274                 }
3275                 else // activate long click timer
3276                     mpData->maDropdownTimer.Start();
3277             }
3278 
3279             // call Click handler
3280             if ( rMEvt.GetClicks() != 2 )
3281                 Click();
3282 
3283             // also call Select handler at repeat
3284             if ( nTrackFlags & StartTrackingFlags::ButtonRepeat )
3285                 Select();
3286 
3287             // if the actions was not aborted in Click handler
3288             if ( mbDrag )
3289                 StartTracking( nTrackFlags );
3290 
3291             // if mouse was clicked over an item we
3292             // can abort here
3293             return;
3294         }
3295 
3296         Deactivate();
3297 
3298         // menu button hit ?
3299         if( mpData->maMenubuttonItem.maRect.Contains( aMousePos ) && ImplHasClippedItems() )
3300         {
3301             if ( maMenuButtonHdl.IsSet() )
3302                 maMenuButtonHdl.Call( this );
3303             else
3304                 ExecuteCustomMenu( mpData->maMenubuttonItem.maRect );
3305             return;
3306         }
3307 
3308         // check scroll- and next-buttons here
3309         if ( maUpperRect.Contains( aMousePos ) )
3310         {
3311             if ( mnCurLine > 1 )
3312             {
3313                 StartTracking();
3314                 mbUpper = true;
3315                 mbIn    = true;
3316                 InvalidateSpin(true, false);
3317             }
3318             return;
3319         }
3320         if ( maLowerRect.Contains( aMousePos ) )
3321         {
3322             if ( mnCurLine+mnVisLines-1 < mnCurLines )
3323             {
3324                 StartTracking();
3325                 mbLower = true;
3326                 mbIn    = true;
3327                 InvalidateSpin(false);
3328             }
3329             return;
3330         }
3331 
3332         // Linesizing testen
3333         if ( (mnWinStyle & TB_WBLINESIZING) == TB_WBLINESIZING )
3334         {
3335             sal_uInt16 nLineMode = ImplTestLineSize( aMousePos );
3336             if ( nLineMode )
3337             {
3338                 ImplTBDragMgr* pMgr = ImplGetTBDragMgr();
3339 
3340                 // call handler, such that we can set the
3341                 // dock rectangles
3342                 StartDocking();
3343 
3344                 Point aPos  = GetParent()->OutputToScreenPixel( GetPosPixel() );
3345                 Size  aSize = GetSizePixel();
3346                 aPos = ScreenToOutputPixel( aPos );
3347 
3348                 // start dragging
3349                 pMgr->StartDragging( this, aMousePos, tools::Rectangle( aPos, aSize ),
3350                                      nLineMode );
3351                 return;
3352             }
3353         }
3354 
3355         // no item, then only click or double click
3356         if ( rMEvt.GetClicks() == 2 )
3357             DoubleClick();
3358         else
3359             Click();
3360     }
3361 
3362     if ( !mbDrag && (mnCurPos == ITEM_NOTFOUND) )
3363         DockingWindow::MouseButtonDown( rMEvt );
3364 }
3365 
MouseButtonUp(const MouseEvent & rMEvt)3366 void ToolBox::MouseButtonUp( const MouseEvent& rMEvt )
3367 {
3368     if ( ImplHandleMouseButtonUp( rMEvt ) )
3369         return;
3370 
3371     if ( mbDragging && rMEvt.IsLeft() )
3372     {
3373         ImplTBDragMgr* pMgr = ImplGetTBDragMgr();
3374         pMgr->EndDragging();
3375         return;
3376     }
3377 
3378     DockingWindow::MouseButtonUp( rMEvt );
3379 }
3380 
Tracking(const TrackingEvent & rTEvt)3381 void ToolBox::Tracking( const TrackingEvent& rTEvt )
3382 {
3383     VclPtr<vcl::Window> xWindow = this;
3384 
3385     if ( rTEvt.IsTrackingEnded() )
3386         ImplHandleMouseButtonUp( rTEvt.GetMouseEvent(), rTEvt.IsTrackingCanceled() );
3387     else
3388         ImplHandleMouseMove( rTEvt.GetMouseEvent(), rTEvt.IsTrackingRepeat() );
3389 
3390     if ( xWindow->isDisposed() )
3391         // toolbox was deleted
3392         return;
3393     DockingWindow::Tracking( rTEvt );
3394 }
3395 
InvalidateItem(ImplToolItems::size_type nPosition)3396 void ToolBox::InvalidateItem(ImplToolItems::size_type nPosition)
3397 {
3398     if (mpData && nPosition < mpData->m_aItems.size())
3399     {
3400         ImplToolItem* pItem = &mpData->m_aItems[nPosition];
3401         Invalidate(pItem->maRect);
3402     }
3403 }
3404 
InvalidateMenuButton()3405 void ToolBox::InvalidateMenuButton()
3406 {
3407     if (!mpData->maMenubuttonItem.maRect.IsEmpty())
3408         Invalidate(mpData->maMenubuttonItem.maRect);
3409 }
3410 
InvalidateSpin(bool bUpperIn,bool bLowerIn)3411 void ToolBox::InvalidateSpin(bool bUpperIn, bool bLowerIn)
3412 {
3413     if (bUpperIn && !maUpperRect.IsEmpty())
3414         Invalidate(maUpperRect);
3415 
3416     if (bLowerIn && !maLowerRect.IsEmpty())
3417         Invalidate(maLowerRect);
3418 }
3419 
Paint(vcl::RenderContext & rRenderContext,const tools::Rectangle & rPaintRect)3420 void ToolBox::Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle& rPaintRect)
3421 {
3422     if( mpData->mbIsPaintLocked )
3423         return;
3424 
3425     if (rPaintRect == tools::Rectangle(0, 0, mnDX-1, mnDY-1))
3426         mbFullPaint = true;
3427     ImplFormat();
3428     mbFullPaint = false;
3429 
3430     ImplDrawBackground(rRenderContext, rPaintRect);
3431 
3432     if ( (mnWinStyle & WB_BORDER) && !ImplIsFloatingMode() )
3433         ImplDrawBorder(rRenderContext);
3434 
3435     if( !ImplIsFloatingMode() )
3436         ImplDrawGrip(rRenderContext);
3437 
3438     ImplDrawMenuButton(rRenderContext, mpData->mbMenubuttonSelected);
3439 
3440     // draw SpinButtons
3441     if (mnWinStyle & WB_SCROLL)
3442     {
3443         if (mnCurLines > mnLines)
3444             ImplDrawSpin(rRenderContext);
3445     }
3446 
3447     // draw buttons
3448     ImplToolItems::size_type nHighPos;
3449     if ( mnHighItemId )
3450         nHighPos = GetItemPos( mnHighItemId );
3451     else
3452         nHighPos = ITEM_NOTFOUND;
3453 
3454     ImplToolItems::size_type nCount = mpData->m_aItems.size();
3455     for( ImplToolItems::size_type i = 0; i < nCount; i++ )
3456     {
3457         ImplToolItem* pItem = &mpData->m_aItems[i];
3458 
3459         // only draw when the rectangle is in the draw rectangle
3460         if ( !pItem->maRect.IsEmpty() && rPaintRect.Overlaps( pItem->maRect ) )
3461         {
3462             sal_uInt16 nHighlight = 0;
3463             if ( i == mnCurPos )
3464                 nHighlight = 1;
3465             else if ( i == nHighPos )
3466                 nHighlight = 2;
3467             ImplDrawItem(rRenderContext, i, nHighlight);
3468         }
3469     }
3470     ImplShowFocus();
3471 }
3472 
Resize()3473 void ToolBox::Resize()
3474 {
3475     Size aSize = GetOutputSizePixel();
3476     // #i31422# some WindowManagers send (0,0) sizes when
3477     // switching virtual desktops - ignore this and avoid reformatting
3478     if( !aSize.Width() && !aSize.Height() )
3479         return;
3480 
3481     tools::Long nOldDX = mnDX;
3482     tools::Long nOldDY = mnDY;
3483     mnDX = aSize.Width();
3484     mnDY = aSize.Height();
3485 
3486     mnLastResizeDY = 0;
3487 
3488     // invalidate everything to have gradient backgrounds properly drawn
3489     Invalidate();
3490 
3491     // If we have any expandable entries, then force a reformat first using
3492     // their optimal sizes, then share out the excess space evenly across those
3493     // expandables and reformat again
3494     std::vector<size_t> aExpandables;
3495     for (size_t i = 0; i < mpData->m_aItems.size(); ++i)
3496     {
3497         if (mpData->m_aItems[i].mbExpand)
3498         {
3499             vcl::Window *pWindow = mpData->m_aItems[i].mpWindow;
3500             SAL_INFO_IF(!pWindow, "vcl.layout", "only tabitems with window supported at the moment");
3501             if (!pWindow)
3502                 continue;
3503             Size aWinSize(pWindow->GetSizePixel());
3504             Size aPrefSize(pWindow->get_preferred_size());
3505             aWinSize.setWidth( aPrefSize.Width() );
3506             pWindow->SetSizePixel(aWinSize);
3507             aExpandables.push_back(i);
3508         }
3509     }
3510 
3511     // re-format or re-draw
3512     if ( mbScroll || !aExpandables.empty() )
3513     {
3514         if ( !mbFormat || !aExpandables.empty() )
3515         {
3516             mbFormat = true;
3517             if( IsReallyVisible() || !aExpandables.empty() )
3518             {
3519                 ImplFormat(true);
3520 
3521                 if (!aExpandables.empty())
3522                 {
3523                     //Get how big the optimal size is
3524                     tools::Rectangle aBounds;
3525                     for (const ImplToolItem & rItem : mpData->m_aItems)
3526                     {
3527                         aBounds.Union( rItem.maRect );
3528                     }
3529 
3530                     auto nOptimalWidth = aBounds.GetWidth();
3531                     auto nDiff = aSize.Width() - nOptimalWidth;
3532                     decltype(nDiff) nExpandablesSize = aExpandables.size();
3533                     nDiff /= nExpandablesSize;
3534 
3535                     //share out the diff from optimal to real across
3536                     //expandable entries
3537                     for (size_t nIndex : aExpandables)
3538                     {
3539                         vcl::Window *pWindow = mpData->m_aItems[nIndex].mpWindow;
3540                         Size aWinSize(pWindow->GetSizePixel());
3541                         Size aPrefSize(pWindow->get_preferred_size());
3542                         aWinSize.setWidth( aPrefSize.Width() + nDiff );
3543                         pWindow->SetSizePixel(aWinSize);
3544                     }
3545 
3546                     //now reformat with final sizes
3547                     mbFormat = true;
3548                     ImplFormat(true);
3549                 }
3550             }
3551         }
3552     }
3553 
3554     // redraw border
3555     if ( !(mnWinStyle & WB_BORDER) )
3556         return;
3557 
3558     // as otherwise, when painting we might think we have to re-draw everything
3559     if ( mbFormat && IsReallyVisible() )
3560         Invalidate();
3561     else
3562     {
3563         if ( mnRightBorder )
3564         {
3565             if ( nOldDX > mnDX )
3566                 Invalidate( tools::Rectangle( mnDX-mnRightBorder-1, 0, mnDX, mnDY ) );
3567             else
3568                 Invalidate( tools::Rectangle( nOldDX-mnRightBorder-1, 0, nOldDX, nOldDY ) );
3569         }
3570 
3571         if ( mnBottomBorder )
3572         {
3573             if ( nOldDY > mnDY )
3574                 Invalidate( tools::Rectangle( 0, mnDY-mnBottomBorder-1, mnDX, mnDY ) );
3575             else
3576                 Invalidate( tools::Rectangle( 0, nOldDY-mnBottomBorder-1, nOldDX, nOldDY ) );
3577         }
3578     }
3579 }
3580 
3581 namespace
3582 {
DispatchableCommand(std::u16string_view rName)3583     bool DispatchableCommand(std::u16string_view rName)
3584     {
3585         return o3tl::starts_with(rName, u".uno")  ||
3586                o3tl::starts_with(rName, u"slot:")  ||
3587                o3tl::starts_with(rName, u"macro:")  ||
3588                o3tl::starts_with(rName, u"vnd.sun.star.script");
3589     }
3590 }
3591 
ImplGetHelpText(ToolBoxItemId nItemId) const3592 const OUString& ToolBox::ImplGetHelpText( ToolBoxItemId nItemId ) const
3593 {
3594     ImplToolItem* pItem = ImplGetItem( nItemId );
3595 
3596     assert( pItem );
3597 
3598     if ( pItem->maHelpText.isEmpty() && ( !pItem->maHelpId.isEmpty() || pItem->maCommandStr.getLength() ))
3599     {
3600         Help* pHelp = Application::GetHelp();
3601         if ( pHelp )
3602         {
3603             if (DispatchableCommand(pItem->maCommandStr))
3604                 pItem->maHelpText = pHelp->GetHelpText( pItem->maCommandStr );
3605             if ( pItem->maHelpText.isEmpty() && !pItem->maHelpId.isEmpty() )
3606                 pItem->maHelpText = pHelp->GetHelpText( pItem->maHelpId );
3607         }
3608     }
3609 
3610     return pItem->maHelpText;
3611 }
3612 
RequestHelp(const HelpEvent & rHEvt)3613 void ToolBox::RequestHelp( const HelpEvent& rHEvt )
3614 {
3615     ToolBoxItemId nItemId;
3616     Point aHelpPos;
3617 
3618     if( !rHEvt.KeyboardActivated() )
3619     {
3620         nItemId = GetItemId( ScreenToOutputPixel( rHEvt.GetMousePosPixel() ) );
3621         aHelpPos = rHEvt.GetMousePosPixel();
3622     }
3623     else
3624     {
3625         if( !mnHighItemId )
3626             return;
3627         else
3628             nItemId = mnHighItemId;
3629         tools::Rectangle aRect( GetItemRect( nItemId ) );
3630         if( aRect.IsEmpty() )
3631             return;
3632         else
3633             aHelpPos = OutputToScreenPixel( aRect.Center() );
3634     }
3635 
3636     if ( nItemId )
3637     {
3638         if ( rHEvt.GetMode() & (HelpEventMode::BALLOON | HelpEventMode::QUICK) )
3639         {
3640             // get rectangle
3641             tools::Rectangle aTempRect = GetItemRect( nItemId );
3642             Point aPt = OutputToScreenPixel( aTempRect.TopLeft() );
3643             aTempRect.SetLeft( aPt.X() );
3644             aTempRect.SetTop( aPt.Y() );
3645             aPt = OutputToScreenPixel( aTempRect.BottomRight() );
3646             aTempRect.SetRight( aPt.X() );
3647             aTempRect.SetBottom( aPt.Y() );
3648 
3649             // get text and display it
3650             OUString aStr = GetQuickHelpText( nItemId );
3651             if (aStr.isEmpty())
3652                 aStr = MnemonicGenerator::EraseAllMnemonicChars( GetItemText( nItemId ) );
3653             if ( rHEvt.GetMode() & HelpEventMode::BALLOON )
3654             {
3655                 const OUString& rHelpStr = GetHelpText( nItemId );
3656                 if (!rHelpStr.isEmpty())
3657                     aStr = rHelpStr;
3658                 Help::ShowBalloon( this, aHelpPos, aTempRect, aStr );
3659             }
3660             else
3661                 Help::ShowQuickHelp( this, aTempRect, aStr, QuickHelpFlags::CtrlText );
3662             return;
3663         }
3664     }
3665 
3666     DockingWindow::RequestHelp( rHEvt );
3667 }
3668 
EventNotify(NotifyEvent & rNEvt)3669 bool ToolBox::EventNotify( NotifyEvent& rNEvt )
3670 {
3671     if ( rNEvt.GetType() == NotifyEventType::KEYINPUT )
3672     {
3673         KeyEvent aKEvt = *rNEvt.GetKeyEvent();
3674         vcl::KeyCode aKeyCode = aKEvt.GetKeyCode();
3675         sal_uInt16  nKeyCode = aKeyCode.GetCode();
3676         switch( nKeyCode )
3677         {
3678             case KEY_TAB:
3679             {
3680                 // internal TAB cycling only if parent is not a dialog or if we are the only child
3681                 // otherwise the dialog control will take over
3682                 vcl::Window *pParent = ImplGetParent();
3683                 bool bOldSchoolContainer =
3684                     ((pParent->GetStyle() & (WB_DIALOGCONTROL | WB_NODIALOGCONTROL)) == WB_DIALOGCONTROL &&
3685                     pParent->GetChildCount() != 1);
3686                 bool bNoTabCycling = bOldSchoolContainer || isContainerWindow(pParent);
3687 
3688                 if( bNoTabCycling )
3689                     return DockingWindow::EventNotify( rNEvt );
3690                 else if( ImplChangeHighlightUpDn( aKeyCode.IsShift() , bNoTabCycling ) )
3691                     return true;
3692                 else
3693                     return DockingWindow::EventNotify( rNEvt );
3694             }
3695             default:
3696                 break;
3697         }
3698     }
3699     else if( rNEvt.GetType() == NotifyEventType::GETFOCUS )
3700     {
3701         if( rNEvt.GetWindow() == this )
3702         {
3703             // the toolbar itself got the focus
3704             if( mnLastFocusItemId != ToolBoxItemId(0) || mpData->mbMenubuttonWasLastSelected )
3705             {
3706                 // restore last item
3707                 if( mpData->mbMenubuttonWasLastSelected )
3708                 {
3709                     ImplChangeHighlight( nullptr );
3710                     mpData->mbMenubuttonSelected = true;
3711                     InvalidateMenuButton();
3712                 }
3713                 else
3714                 {
3715                     ImplChangeHighlight( ImplGetItem( mnLastFocusItemId ) );
3716                     mnLastFocusItemId = ToolBoxItemId(0);
3717                 }
3718             }
3719             else if( (GetGetFocusFlags() & (GetFocusFlags::Backward|GetFocusFlags::Tab) ) == (GetFocusFlags::Backward|GetFocusFlags::Tab))
3720                 // Shift-TAB was pressed in the parent
3721                 ImplChangeHighlightUpDn( false );
3722             else
3723                 ImplChangeHighlightUpDn( true );
3724 
3725             mnLastFocusItemId = ToolBoxItemId(0);
3726 
3727             return true;
3728         }
3729         else
3730         {
3731             // a child window got the focus so update current item to
3732             // allow for proper lose focus handling in keyboard navigation
3733             for (auto const& item : mpData->m_aItems)
3734             {
3735                 if ( item.mbVisible )
3736                 {
3737                     if ( item.mpWindow && item.mpWindow->ImplIsWindowOrChild( rNEvt.GetWindow() ) )
3738                     {
3739                         mnHighItemId = item.mnId;
3740                         break;
3741                     }
3742                 }
3743             }
3744             return DockingWindow::EventNotify( rNEvt );
3745         }
3746     }
3747     else if( rNEvt.GetType() == NotifyEventType::LOSEFOCUS )
3748     {
3749         // deselect
3750         ImplHideFocus();
3751         mpData->mbMenubuttonWasLastSelected = false;
3752         mnHighItemId = ToolBoxItemId(0);
3753         mnCurPos = ITEM_NOTFOUND;
3754     }
3755 
3756     return DockingWindow::EventNotify( rNEvt );
3757 }
3758 
Command(const CommandEvent & rCEvt)3759 void ToolBox::Command( const CommandEvent& rCEvt )
3760 {
3761     if ( rCEvt.GetCommand() == CommandEventId::Wheel )
3762     {
3763         if ( (mnCurLine > 1) || (mnCurLine+mnVisLines-1 < mnCurLines) )
3764         {
3765             const CommandWheelData* pData = rCEvt.GetWheelData();
3766             if ( pData->GetMode() == CommandWheelMode::SCROLL )
3767             {
3768                 if ( (mnCurLine > 1) && (pData->GetDelta() > 0) )
3769                     ShowLine( false );
3770                 else if ( (mnCurLine+mnVisLines-1 < mnCurLines) && (pData->GetDelta() < 0) )
3771                     ShowLine( true );
3772                 InvalidateSpin();
3773                 return;
3774             }
3775         }
3776     }
3777     else if ( rCEvt.GetCommand() == CommandEventId::ContextMenu )
3778     {
3779         ExecuteCustomMenu( tools::Rectangle( rCEvt.GetMousePosPixel(), rCEvt.GetMousePosPixel() ) );
3780         return;
3781     }
3782 
3783     DockingWindow::Command( rCEvt );
3784 }
3785 
StateChanged(StateChangedType nType)3786 void ToolBox::StateChanged( StateChangedType nType )
3787 {
3788     DockingWindow::StateChanged( nType );
3789 
3790     if ( nType == StateChangedType::InitShow )
3791         ImplFormat();
3792     else if ( nType == StateChangedType::Enable )
3793         ImplUpdateItem();
3794     else if ( nType == StateChangedType::UpdateMode )
3795     {
3796         if ( IsUpdateMode() )
3797             Invalidate();
3798     }
3799     else if ( (nType == StateChangedType::Zoom) ||
3800               (nType == StateChangedType::ControlFont) )
3801     {
3802         mbCalc = true;
3803         mbFormat = true;
3804         ImplInitSettings( true, false, false );
3805         Invalidate();
3806     }
3807     else if ( nType == StateChangedType::ControlForeground )
3808     {
3809         ImplInitSettings( false, true, false );
3810         Invalidate();
3811     }
3812     else if ( nType == StateChangedType::ControlBackground )
3813     {
3814         ImplInitSettings( false, false, true ); // font, foreground, background
3815         Invalidate();
3816     }
3817 
3818     maStateChangedHandler.Call( &nType );
3819 }
3820 
DataChanged(const DataChangedEvent & rDCEvt)3821 void ToolBox::DataChanged( const DataChangedEvent& rDCEvt )
3822 {
3823     DockingWindow::DataChanged( rDCEvt );
3824 
3825     if ( (rDCEvt.GetType() == DataChangedEventType::DISPLAY) ||
3826          (rDCEvt.GetType() == DataChangedEventType::FONTS) ||
3827          (rDCEvt.GetType() == DataChangedEventType::FONTSUBSTITUTION) ||
3828          ((rDCEvt.GetType() == DataChangedEventType::SETTINGS) &&
3829           (rDCEvt.GetFlags() & AllSettingsFlags::STYLE)) )
3830     {
3831         mbCalc = true;
3832         mbFormat = true;
3833         ImplInitSettings( true, true, true );
3834         Invalidate();
3835     }
3836 
3837     maDataChangedHandler.Call( &rDCEvt );
3838 }
3839 
SetStyle(WinBits nNewStyle)3840 void ToolBox::SetStyle(WinBits nNewStyle)
3841 {
3842     mnWinStyle = nNewStyle;
3843     if (!ImplIsFloatingMode())
3844     {
3845         bool bOldScroll = mbScroll;
3846         mbScroll = (mnWinStyle & WB_SCROLL) != 0;
3847         if (mbScroll != bOldScroll)
3848         {
3849             mbFormat = true;
3850             ImplFormat();
3851         }
3852     }
3853 }
3854 
ToggleFloatingMode()3855 void ToolBox::ToggleFloatingMode()
3856 {
3857     DockingWindow::ToggleFloatingMode();
3858 
3859     if (!mpData)
3860         return;
3861 
3862     bool bOldHorz = mbHorz;
3863 
3864     if ( ImplIsFloatingMode() )
3865     {
3866         mbHorz   = true;
3867         meAlign  = WindowAlign::Top;
3868         mbScroll = true;
3869 
3870         if( bOldHorz != mbHorz )
3871             mbCalc = true;  // orientation was changed !
3872 
3873         ImplSetMinMaxFloatSize();
3874         SetOutputSizePixel( ImplCalcFloatSize( mnFloatLines ) );
3875     }
3876     else
3877     {
3878         mbScroll = (mnWinStyle & WB_SCROLL) != 0;
3879         if ( (meAlign == WindowAlign::Top) || (meAlign == WindowAlign::Bottom) )
3880             mbHorz = true;
3881         else
3882             mbHorz = false;
3883 
3884         // set focus back to document
3885         ImplGetFrameWindow()->GetWindow( GetWindowType::Client )->GrabFocus();
3886     }
3887 
3888     if( bOldHorz != mbHorz )
3889     {
3890         // if orientation changes, the toolbox has to be initialized again
3891         // to update the direction of the gradient
3892         mbCalc = true;
3893         ImplInitSettings( true, true, true );
3894     }
3895 
3896     mbFormat = true;
3897     ImplFormat();
3898 }
3899 
StartDocking()3900 void ToolBox::StartDocking()
3901 {
3902     meDockAlign = meAlign;
3903     mnDockLines = mnLines;
3904     mbLastFloatMode = ImplIsFloatingMode();
3905     DockingWindow::StartDocking();
3906 }
3907 
Docking(const Point & rPos,tools::Rectangle & rRect)3908 bool ToolBox::Docking( const Point& rPos, tools::Rectangle& rRect )
3909 {
3910     // do nothing during dragging, it was calculated before
3911     if ( mbDragging )
3912         return false;
3913 
3914     bool bFloatMode = false;
3915 
3916     DockingWindow::Docking( rPos, rRect );
3917 
3918     // if the mouse is outside the area, it can only become a floating window
3919     tools::Rectangle aDockingRect( rRect );
3920     if ( !ImplIsFloatingMode() )
3921     {
3922         // don't use tracking rectangle for alignment check, because it will be too large
3923         // to get a floating mode as result - switch to floating size
3924         // so the calculation only depends on the position of the rectangle, not the current
3925         // docking state of the window
3926         ImplToolItems::size_type nTemp = 0;
3927         aDockingRect.SetSize( ImplCalcFloatSize( nTemp ) );
3928 
3929         // in this mode docking is never done by keyboard, so it's OK to use the mouse position
3930         aDockingRect.SetPos( ImplGetFrameWindow()->GetPointerPosPixel() );
3931     }
3932 
3933     bFloatMode = true;
3934 
3935     meDockAlign = meAlign;
3936     if ( !mbLastFloatMode )
3937     {
3938         ImplToolItems::size_type nTemp = 0;
3939         aDockingRect.SetSize( ImplCalcFloatSize( nTemp ) );
3940     }
3941 
3942     rRect = aDockingRect;
3943     mbLastFloatMode = bFloatMode;
3944 
3945     return bFloatMode;
3946 }
3947 
EndDocking(const tools::Rectangle & rRect,bool bFloatMode)3948 void ToolBox::EndDocking( const tools::Rectangle& rRect, bool bFloatMode )
3949 {
3950     if ( !IsDockingCanceled() )
3951     {
3952         if ( mnLines != mnDockLines )
3953             SetLineCount( mnDockLines );
3954         if ( meAlign != meDockAlign )
3955             SetAlign( meDockAlign );
3956     }
3957     if ( bFloatMode || (bFloatMode != ImplIsFloatingMode()) )
3958         DockingWindow::EndDocking( rRect, bFloatMode );
3959 }
3960 
Resizing(Size & rSize)3961 void ToolBox::Resizing( Size& rSize )
3962 {
3963     ImplToolItems::size_type nCalcLines;
3964     ImplToolItems::size_type nTemp;
3965 
3966     // calculate all floating sizes
3967     ImplCalcFloatSizes();
3968 
3969     if ( !mnLastResizeDY )
3970         mnLastResizeDY = mnDY;
3971 
3972     // is vertical resizing needed
3973     if ( (mnLastResizeDY != rSize.Height()) && (mnDY != rSize.Height()) )
3974     {
3975         nCalcLines = ImplCalcLines( rSize.Height() );
3976         if ( nCalcLines < 1 )
3977             nCalcLines = 1;
3978         rSize = ImplCalcFloatSize( nCalcLines );
3979     }
3980     else
3981     {
3982         nCalcLines = 1;
3983         nTemp = nCalcLines;
3984         Size aTempSize = ImplCalcFloatSize( nTemp );
3985         while ( (aTempSize.Width() > rSize.Width()) &&
3986                 (nCalcLines <= maFloatSizes[0].mnLines) )
3987         {
3988             nCalcLines++;
3989             nTemp = nCalcLines;
3990             aTempSize = ImplCalcFloatSize( nTemp );
3991         }
3992         rSize = aTempSize;
3993     }
3994 
3995     mnLastResizeDY = rSize.Height();
3996 }
3997 
GetOptimalSize() const3998 Size ToolBox::GetOptimalSize() const
3999 {
4000     // If we have any expandable entries, then force them to their
4001     // optimal sizes, then reset them afterwards
4002     std::map<vcl::Window*, Size> aExpandables;
4003     for (const ImplToolItem & rItem : mpData->m_aItems)
4004     {
4005         if (rItem.mbExpand)
4006         {
4007             vcl::Window *pWindow = rItem.mpWindow;
4008             SAL_INFO_IF(!pWindow, "vcl.layout", "only tabitems with window supported at the moment");
4009             if (!pWindow)
4010                 continue;
4011             Size aWinSize(pWindow->GetSizePixel());
4012             aExpandables[pWindow] = aWinSize;
4013             Size aPrefSize(pWindow->get_preferred_size());
4014             aWinSize.setWidth( aPrefSize.Width() );
4015             pWindow->SetSizePixel(aWinSize);
4016         }
4017     }
4018 
4019     Size aSize(const_cast<ToolBox *>(this)->ImplCalcSize( mnLines ));
4020 
4021     for (auto const& [pWindow, aWinSize] : aExpandables)
4022         pWindow->SetSizePixel(aWinSize);
4023 
4024     return aSize;
4025 }
4026 
CalcWindowSizePixel(ImplToolItems::size_type nCalcLines)4027 Size ToolBox::CalcWindowSizePixel( ImplToolItems::size_type nCalcLines )
4028 {
4029     return ImplCalcSize( nCalcLines );
4030 }
4031 
CalcWindowSizePixel(ImplToolItems::size_type nCalcLines,WindowAlign eAlign)4032 Size ToolBox::CalcWindowSizePixel( ImplToolItems::size_type nCalcLines, WindowAlign eAlign )
4033 {
4034     return ImplCalcSize( nCalcLines,
4035         (eAlign == WindowAlign::Top || eAlign == WindowAlign::Bottom) ? TB_CALCMODE_HORZ : TB_CALCMODE_VERT );
4036 }
4037 
ImplCountLineBreaks() const4038 ToolBox::ImplToolItems::size_type ToolBox::ImplCountLineBreaks() const
4039 {
4040     ImplToolItems::size_type nLines = 0;
4041 
4042     for (auto const& item : mpData->m_aItems)
4043     {
4044         if( item.meType == ToolBoxItemType::BREAK )
4045             ++nLines;
4046     }
4047     return nLines;
4048 }
4049 
CalcPopupWindowSizePixel()4050 Size ToolBox::CalcPopupWindowSizePixel()
4051 {
4052     // count number of breaks and calc corresponding floating window size
4053     ImplToolItems::size_type nLines = ImplCountLineBreaks();
4054 
4055     if( nLines )
4056         ++nLines;   // add the first line
4057     else
4058     {
4059         // no breaks found: use quadratic layout
4060         nLines = static_cast<ImplToolItems::size_type>(ceil( sqrt( static_cast<double>(GetItemCount()) ) ));
4061     }
4062 
4063     bool bPopup = mpData->mbAssumePopupMode;
4064     mpData->mbAssumePopupMode = true;
4065 
4066     Size aSize = CalcFloatingWindowSizePixel( nLines );
4067 
4068     mpData->mbAssumePopupMode = bPopup;
4069     return aSize;
4070 }
4071 
CalcFloatingWindowSizePixel()4072 Size ToolBox::CalcFloatingWindowSizePixel()
4073 {
4074     ImplToolItems::size_type nLines = ImplCountLineBreaks();
4075     ++nLines; // add the first line
4076     return CalcFloatingWindowSizePixel( nLines );
4077 }
4078 
CalcFloatingWindowSizePixel(ImplToolItems::size_type nCalcLines)4079 Size ToolBox::CalcFloatingWindowSizePixel( ImplToolItems::size_type nCalcLines )
4080 {
4081     bool bFloat = mpData->mbAssumeFloating;
4082     bool bDocking = mpData->mbAssumeDocked;
4083 
4084     // simulate floating mode and force reformat before calculating
4085     mpData->mbAssumeFloating = true;
4086     mpData->mbAssumeDocked = false;
4087 
4088     Size aSize = ImplCalcFloatSize( nCalcLines );
4089 
4090     mbFormat = true;
4091     mpData->mbAssumeFloating = bFloat;
4092     mpData->mbAssumeDocked = bDocking;
4093 
4094     return aSize;
4095 }
4096 
CalcMinimumWindowSizePixel()4097 Size ToolBox::CalcMinimumWindowSizePixel()
4098 {
4099     if( ImplIsFloatingMode() )
4100         return ImplCalcSize( mnFloatLines );
4101     else
4102     {
4103         // create dummy toolbox for measurements
4104         VclPtrInstance< ToolBox > pToolBox( GetParent(), GetStyle() );
4105 
4106         // copy until first useful item
4107         for (auto const& item : mpData->m_aItems)
4108         {
4109             pToolBox->CopyItem( *this, item.mnId );
4110             if( (item.meType == ToolBoxItemType::BUTTON) &&
4111                 item.mbVisible && !ImplIsFixedControl( &item ) )
4112                 break;
4113         }
4114 
4115         // add to docking manager if required to obtain a drag area
4116         // (which is accounted for in calcwindowsizepixel)
4117         if( ImplGetDockingManager()->GetDockingWindowWrapper( this ) )
4118             ImplGetDockingManager()->AddWindow( pToolBox );
4119 
4120         // account for menu
4121         if( IsMenuEnabled() )
4122             pToolBox->SetMenuType( GetMenuType() );
4123 
4124         pToolBox->SetAlign( GetAlign() );
4125         Size aSize = pToolBox->CalcWindowSizePixel( 1 );
4126 
4127         ImplGetDockingManager()->RemoveWindow( pToolBox );
4128         pToolBox->Clear();
4129 
4130         pToolBox.disposeAndClear();
4131 
4132         return aSize;
4133     }
4134 }
4135 
EnableCustomize(bool bEnable)4136 void ToolBox::EnableCustomize( bool bEnable )
4137 {
4138     mbCustomize = bEnable;
4139 }
4140 
LoseFocus()4141 void ToolBox::LoseFocus()
4142 {
4143     ImplChangeHighlight( nullptr, true );
4144 
4145     DockingWindow::LoseFocus();
4146 }
4147 
4148 // performs the action associated with an item, ie simulates clicking the item
TriggerItem(ToolBoxItemId nItemId)4149 void ToolBox::TriggerItem( ToolBoxItemId nItemId )
4150 {
4151     mnHighItemId = nItemId;
4152     vcl::KeyCode aKeyCode( 0, 0 );
4153     ImplActivateItem( aKeyCode );
4154 }
4155 
4156 // calls the button's action handler
4157 // returns true if action was called
ImplActivateItem(vcl::KeyCode aKeyCode)4158 bool ToolBox::ImplActivateItem( vcl::KeyCode aKeyCode )
4159 {
4160     bool bRet = true;
4161     if( mnHighItemId )
4162     {
4163         ImplToolItem *pToolItem = ImplGetItem( mnHighItemId );
4164 
4165         // #107712#, activate can also be called for disabled entries
4166         if( pToolItem && !pToolItem->mbEnabled )
4167             return true;
4168 
4169         if( pToolItem && pToolItem->mpWindow && HasFocus() )
4170         {
4171             ImplHideFocus();
4172             mbChangingHighlight = true;  // avoid focus change due to loss of focus
4173             pToolItem->mpWindow->ImplControlFocus( GetFocusFlags::Tab );
4174             mbChangingHighlight = false;
4175         }
4176         else
4177         {
4178             mnDownItemId = mnCurItemId = mnHighItemId;
4179             if (pToolItem && (pToolItem->mnBits & ToolBoxItemBits::AUTOCHECK))
4180             {
4181                 if ( pToolItem->mnBits & ToolBoxItemBits::RADIOCHECK )
4182                 {
4183                     if ( pToolItem->meState != TRISTATE_TRUE )
4184                         SetItemState( pToolItem->mnId, TRISTATE_TRUE );
4185                 }
4186                 else
4187                 {
4188                     if ( pToolItem->meState != TRISTATE_TRUE )
4189                         pToolItem->meState = TRISTATE_TRUE;
4190                     else
4191                         pToolItem->meState = TRISTATE_FALSE;
4192                 }
4193             }
4194             mnMouseModifier = aKeyCode.GetModifier();
4195             mbIsKeyEvent = true;
4196             Activate();
4197             Click();
4198 
4199             // #107776# we might be destroyed in the selecthandler
4200             VclPtr<vcl::Window> xWindow = this;
4201             Select();
4202             if ( xWindow->isDisposed() )
4203                 return bRet;
4204 
4205             Deactivate();
4206             mbIsKeyEvent = false;
4207             mnMouseModifier = 0;
4208         }
4209     }
4210     else
4211         bRet = false;
4212     return bRet;
4213 }
4214 
ImplCloseLastPopup(vcl::Window const * pParent)4215 static bool ImplCloseLastPopup( vcl::Window const *pParent )
4216 {
4217     // close last popup toolbox (see also:
4218     // ImplHandleMouseFloatMode(...) in winproc.cxx )
4219 
4220     if (ImplGetSVData()->mpWinData->mpFirstFloat)
4221     {
4222         FloatingWindow* pLastLevelFloat = ImplGetSVData()->mpWinData->mpFirstFloat->ImplFindLastLevelFloat();
4223         // only close the floater if it is not our direct parent, which would kill ourself
4224         if( pLastLevelFloat && pLastLevelFloat != pParent )
4225         {
4226             pLastLevelFloat->EndPopupMode( FloatWinPopupEndFlags::Cancel | FloatWinPopupEndFlags::CloseAll );
4227             return true;
4228         }
4229     }
4230     return false;
4231 }
4232 
4233 // opens a drop down toolbox item
4234 // returns true if item was opened
ImplOpenItem(vcl::KeyCode aKeyCode)4235 bool ToolBox::ImplOpenItem( vcl::KeyCode aKeyCode )
4236 {
4237     sal_uInt16 nCode = aKeyCode.GetCode();
4238     bool bRet = true;
4239 
4240     // arrow keys should work only in the opposite direction of alignment (to not break cursor travelling)
4241     if ( ((nCode == KEY_LEFT || nCode == KEY_RIGHT) && IsHorizontal())
4242       || ((nCode == KEY_UP   || nCode == KEY_DOWN)  && !IsHorizontal()) )
4243         return false;
4244 
4245     if( mpData->mbMenubuttonSelected )
4246     {
4247         if( ImplCloseLastPopup( GetParent() ) )
4248             return bRet;
4249         mbIsKeyEvent = true;
4250         if ( maMenuButtonHdl.IsSet() )
4251             maMenuButtonHdl.Call( this );
4252         else
4253             ExecuteCustomMenu( mpData->maMenubuttonItem.maRect );
4254         mpData->mbMenubuttonWasLastSelected = true;
4255         mbIsKeyEvent = false;
4256     }
4257     else if( mnHighItemId &&  ImplGetItem( mnHighItemId ) &&
4258         (ImplGetItem( mnHighItemId )->mnBits & ToolBoxItemBits::DROPDOWN) )
4259     {
4260         mnDownItemId = mnCurItemId = mnHighItemId;
4261         mnCurPos = GetItemPos( mnCurItemId );
4262         mnLastFocusItemId = mnCurItemId; // save item id for possible later focus restore
4263         mnMouseModifier = aKeyCode.GetModifier();
4264         mbIsKeyEvent = true;
4265         Activate();
4266 
4267         mpData->mbDropDownByKeyboard = true;
4268         mpData->maDropdownClickHdl.Call( this );
4269 
4270         mbIsKeyEvent = false;
4271         mnMouseModifier = 0;
4272     }
4273     else
4274         bRet = false;
4275 
4276     return bRet;
4277 }
4278 
KeyInput(const KeyEvent & rKEvt)4279 void ToolBox::KeyInput( const KeyEvent& rKEvt )
4280 {
4281     vcl::KeyCode aKeyCode = rKEvt.GetKeyCode();
4282     sal_uInt16 nCode = aKeyCode.GetCode();
4283 
4284     vcl::Window *pParent = ImplGetParent();
4285     bool bOldSchoolContainer = ((pParent->GetStyle() & (WB_DIALOGCONTROL | WB_NODIALOGCONTROL)) == WB_DIALOGCONTROL);
4286     bool bParentIsContainer = bOldSchoolContainer || isContainerWindow(pParent);
4287 
4288     bool bForwardKey = false;
4289     bool bGrabFocusToDocument = false;
4290 
4291     // #107776# we might be destroyed in the keyhandler
4292     VclPtr<vcl::Window> xWindow = this;
4293 
4294     switch ( nCode )
4295     {
4296         case KEY_UP:
4297         {
4298             // Ctrl-Cursor activates next toolbox, indicated by a blue arrow pointing to the left/up
4299             if( aKeyCode.GetModifier() )    // allow only pure cursor keys
4300                 break;
4301             if( !IsHorizontal() )
4302                 ImplChangeHighlightUpDn( true );
4303             else
4304                 ImplOpenItem( aKeyCode );
4305         }
4306         break;
4307         case KEY_LEFT:
4308         {
4309             if( aKeyCode.GetModifier() )    // allow only pure cursor keys
4310                 break;
4311             if( IsHorizontal() )
4312                 ImplChangeHighlightUpDn( true );
4313             else
4314                 ImplOpenItem( aKeyCode );
4315         }
4316         break;
4317         case KEY_DOWN:
4318         {
4319             if( aKeyCode.GetModifier() )    // allow only pure cursor keys
4320                 break;
4321             if( !IsHorizontal() )
4322                 ImplChangeHighlightUpDn( false );
4323             else
4324                 ImplOpenItem( aKeyCode );
4325         }
4326         break;
4327         case KEY_RIGHT:
4328         {
4329             if( aKeyCode.GetModifier() )    // allow only pure cursor keys
4330                 break;
4331             if( IsHorizontal() )
4332                 ImplChangeHighlightUpDn( false );
4333             else
4334                 ImplOpenItem( aKeyCode );
4335         }
4336         break;
4337         case KEY_PAGEUP:
4338             if ( mnCurLine > 1 )
4339             {
4340                 if( mnCurLine > mnVisLines )
4341                     mnCurLine = mnCurLine - mnVisLines;
4342                 else
4343                     mnCurLine = 1;
4344                 mbFormat = true;
4345                 ImplFormat();
4346                 InvalidateSpin();
4347                 ImplChangeHighlight( ImplGetFirstValidItem( mnCurLine ) );
4348             }
4349         break;
4350         case KEY_PAGEDOWN:
4351             if ( mnCurLine+mnVisLines-1 < mnCurLines )
4352             {
4353                 if( mnCurLine + 2*mnVisLines-1 < mnCurLines )
4354                     mnCurLine = mnCurLine + mnVisLines;
4355                 else
4356                     mnCurLine = mnCurLines;
4357                 mbFormat = true;
4358                 ImplFormat();
4359                 InvalidateSpin();
4360                 ImplChangeHighlight( ImplGetFirstValidItem( mnCurLine ) );
4361             }
4362         break;
4363         case KEY_END:
4364             {
4365                 ImplChangeHighlight( nullptr );
4366                 ImplChangeHighlightUpDn( false );
4367             }
4368             break;
4369         case KEY_HOME:
4370             {
4371                 ImplChangeHighlight( nullptr );
4372                 ImplChangeHighlightUpDn( true );
4373             }
4374             break;
4375         case KEY_ESCAPE:
4376         {
4377             if( !ImplIsFloatingMode() && bParentIsContainer )
4378                 DockingWindow::KeyInput( rKEvt );
4379             else
4380             {
4381                 // send focus to document pane
4382                 vcl::Window *pWin = this;
4383                 while( pWin )
4384                 {
4385                     if( !pWin->GetParent() )
4386                     {
4387                         pWin->ImplGetFrameWindow()->GetWindow( GetWindowType::Client )->GrabFocus();
4388                         break;
4389                     }
4390                     pWin = pWin->GetParent();
4391                 }
4392             }
4393         }
4394         break;
4395         case KEY_RETURN:
4396         {
4397             // #107712#, disabled entries are selectable now
4398             //  leave toolbox and move focus to document
4399             if( mnHighItemId )
4400             {
4401                 ImplToolItem *pItem = ImplGetItem(mnHighItemId);
4402                 if (!pItem || !pItem->mbEnabled)
4403                 {
4404                     bGrabFocusToDocument = true;
4405                 }
4406             }
4407             if( !bGrabFocusToDocument )
4408                 bForwardKey = !ImplActivateItem( aKeyCode );
4409         }
4410         break;
4411         case KEY_SPACE:
4412         {
4413             ImplOpenItem( aKeyCode );
4414         }
4415         break;
4416         default:
4417             {
4418             sal_uInt16 aKeyGroup = aKeyCode.GetGroup();
4419             ImplToolItem *pItem = nullptr;
4420             if( mnHighItemId )
4421                 pItem = ImplGetItem( mnHighItemId );
4422             // #i13931# forward alphanum keyinput into embedded control
4423             if( (aKeyGroup == KEYGROUP_NUM || aKeyGroup == KEYGROUP_ALPHA ) && pItem && pItem->mpWindow && pItem->mbEnabled )
4424             {
4425                 vcl::Window *pFocusWindow = Application::GetFocusWindow();
4426                 ImplHideFocus();
4427                 mbChangingHighlight = true;  // avoid focus change due to loss of focus
4428                 pItem->mpWindow->ImplControlFocus( GetFocusFlags::Tab );
4429                 mbChangingHighlight = false;
4430                 if( pFocusWindow != Application::GetFocusWindow() )
4431                     Application::GetFocusWindow()->KeyInput( rKEvt );
4432             }
4433             else
4434             {
4435                 // do nothing to avoid key presses going into the document
4436                 // while the toolbox has the focus
4437                 // just forward function and special keys and combinations with Alt-key
4438                 // and Ctrl-key
4439                 if( aKeyGroup == KEYGROUP_FKEYS || aKeyGroup == KEYGROUP_MISC || aKeyCode.IsMod2()
4440                         || aKeyCode.IsMod1() )
4441                     bForwardKey = true;
4442             }
4443         }
4444     }
4445 
4446     if ( xWindow->isDisposed() )
4447         return;
4448 
4449     // #107251# move focus away if this toolbox was disabled during keyinput
4450     if (HasFocus() && mpData->mbKeyInputDisabled && bParentIsContainer)
4451     {
4452         vcl::Window *pFocusControl = pParent->ImplGetDlgWindow( 0, GetDlgWindowType::First );
4453         if ( pFocusControl && pFocusControl != this )
4454             pFocusControl->ImplControlFocus( GetFocusFlags::Init );
4455     }
4456 
4457     // #107712#, leave toolbox
4458     if( bGrabFocusToDocument )
4459     {
4460         GrabFocusToDocument();
4461         return;
4462     }
4463 
4464     if( bForwardKey )
4465         DockingWindow::KeyInput( rKEvt );
4466 }
4467 
4468 // returns the current toolbox line of the item
ImplGetItemLine(ImplToolItem const * pCurrentItem)4469 ToolBox::ImplToolItems::size_type ToolBox::ImplGetItemLine( ImplToolItem const * pCurrentItem )
4470 {
4471     ImplToolItems::size_type nLine = 1;
4472     for (auto const& item : mpData->m_aItems)
4473     {
4474         if ( item.mbBreak )
4475             ++nLine;
4476         if( &item == pCurrentItem)
4477             break;
4478     }
4479     return nLine;
4480 }
4481 
4482 // returns the first displayable item in the given line
ImplGetFirstValidItem(ImplToolItems::size_type nLine)4483 ImplToolItem* ToolBox::ImplGetFirstValidItem( ImplToolItems::size_type nLine )
4484 {
4485     if( !nLine || nLine > mnCurLines )
4486         return nullptr;
4487 
4488     nLine--;
4489 
4490     ImplToolItems::iterator it = mpData->m_aItems.begin();
4491     while( it != mpData->m_aItems.end() )
4492     {
4493         // find correct line
4494         if ( it->mbBreak )
4495             nLine--;
4496         if( !nLine )
4497         {
4498             // find first useful item
4499             while( it != mpData->m_aItems.end() && ((it->meType != ToolBoxItemType::BUTTON) ||
4500                 /*!it->mbEnabled ||*/ !it->mbVisible || ImplIsFixedControl( &(*it) )) )
4501             {
4502                 ++it;
4503                 if( it == mpData->m_aItems.end() || it->mbBreak )
4504                     return nullptr;    // no valid items in this line
4505             }
4506             return &(*it);
4507         }
4508         ++it;
4509     }
4510 
4511     return (it == mpData->m_aItems.end()) ? nullptr : &(*it);
4512 }
4513 
ImplFindItemPos(const ImplToolItem * pItem,const ImplToolItems & rList)4514 ToolBox::ImplToolItems::size_type ToolBox::ImplFindItemPos( const ImplToolItem* pItem, const ImplToolItems& rList )
4515 {
4516     if( pItem )
4517     {
4518         for( ImplToolItems::size_type nPos = 0; nPos < rList.size(); ++nPos )
4519             if( &rList[ nPos ] == pItem )
4520                 return nPos;
4521     }
4522     return ITEM_NOTFOUND;
4523 }
4524 
ChangeHighlight(ImplToolItems::size_type nPos)4525 void ToolBox::ChangeHighlight( ImplToolItems::size_type nPos )
4526 {
4527     if ( nPos < GetItemCount() ) {
4528         ImplGrabFocus( GetFocusFlags::NONE );
4529         ImplChangeHighlight ( ImplGetItem ( GetItemId ( nPos ) ) );
4530     }
4531 }
4532 
ImplChangeHighlight(ImplToolItem const * pItem,bool bNoGrabFocus)4533 void ToolBox::ImplChangeHighlight( ImplToolItem const * pItem, bool bNoGrabFocus )
4534 {
4535     // avoid recursion due to focus change
4536     if( mbChangingHighlight )
4537         return;
4538 
4539     mbChangingHighlight = true;
4540 
4541     ImplToolItem* pOldItem = nullptr;
4542 
4543     if ( mnHighItemId )
4544     {
4545         ImplHideFocus();
4546         ImplToolItems::size_type nPos = GetItemPos( mnHighItemId );
4547         pOldItem = ImplGetItem( mnHighItemId );
4548         // #i89962# ImplDrawItem can cause Invalidate/Update
4549         // which will in turn ImplShowFocus again
4550         // set mnHighItemId to 0 already to prevent this hen/egg problem
4551         mnHighItemId = ToolBoxItemId(0);
4552         InvalidateItem(nPos);
4553         CallEventListeners( VclEventId::ToolboxHighlightOff, reinterpret_cast< void* >( nPos ) );
4554     }
4555 
4556     if( !bNoGrabFocus && pItem != pOldItem && pOldItem && pOldItem->mpWindow )
4557     {
4558         // move focus into toolbox
4559         GrabFocus();
4560     }
4561 
4562     if( pItem )
4563     {
4564         ImplToolItems::size_type aPos = ToolBox::ImplFindItemPos( pItem, mpData->m_aItems );
4565         if( aPos != ITEM_NOTFOUND)
4566         {
4567             // check for line breaks
4568             ImplToolItems::size_type nLine = ImplGetItemLine( pItem );
4569 
4570             if( nLine >= mnCurLine + mnVisLines )
4571             {
4572                 mnCurLine = nLine - mnVisLines + 1;
4573                 mbFormat = true;
4574             }
4575             else if ( nLine < mnCurLine )
4576             {
4577                 mnCurLine = nLine;
4578                 mbFormat = true;
4579             }
4580 
4581             if( mbFormat )
4582             {
4583                 ImplFormat();
4584             }
4585 
4586             mnHighItemId = pItem->mnId;
4587             InvalidateItem(aPos);
4588 
4589             ImplShowFocus();
4590 
4591             if( pItem->mpWindow )
4592                 pItem->mpWindow->GrabFocus();
4593             if( pItem != pOldItem )
4594                 CallEventListeners( VclEventId::ToolboxHighlight );
4595         }
4596     }
4597     else
4598     {
4599         ImplHideFocus();
4600         mnHighItemId = ToolBoxItemId(0);
4601         mnCurPos = ITEM_NOTFOUND;
4602     }
4603 
4604     mbChangingHighlight = false;
4605 }
4606 
4607 // check for keyboard accessible items
ImplIsValidItem(const ImplToolItem * pItem,bool bNotClipped)4608 static bool ImplIsValidItem( const ImplToolItem* pItem, bool bNotClipped )
4609 {
4610     bool bValid = (pItem && pItem->meType == ToolBoxItemType::BUTTON && pItem->mbVisible && !ImplIsFixedControl( pItem )
4611                    && pItem->mbEnabled);
4612     if( bValid && bNotClipped && pItem->IsClipped() )
4613         bValid = false;
4614     return bValid;
4615 }
4616 
ImplChangeHighlightUpDn(bool bUp,bool bNoCycle)4617 bool ToolBox::ImplChangeHighlightUpDn( bool bUp, bool bNoCycle )
4618 {
4619     ImplToolItem* pToolItem = ImplGetItem( mnHighItemId );
4620 
4621     if( !pToolItem || !mnHighItemId )
4622     {
4623         // menubutton highlighted ?
4624         if( mpData->mbMenubuttonSelected )
4625         {
4626             mpData->mbMenubuttonSelected = false;
4627             if( bUp )
4628             {
4629                 // select last valid non-clipped item
4630                 ImplToolItem* pItem = nullptr;
4631                 auto it = std::find_if(mpData->m_aItems.rbegin(), mpData->m_aItems.rend(),
4632                     [](const ImplToolItem& rItem) { return ImplIsValidItem( &rItem, true ); });
4633                 if( it != mpData->m_aItems.rend() )
4634                     pItem = &(*it);
4635 
4636                 InvalidateMenuButton();
4637                 ImplChangeHighlight( pItem );
4638             }
4639             else
4640             {
4641                 // select first valid non-clipped item
4642                 ImplToolItems::iterator it = std::find_if(mpData->m_aItems.begin(), mpData->m_aItems.end(),
4643                     [](const ImplToolItem& rItem) { return ImplIsValidItem( &rItem, true ); });
4644                 if( it != mpData->m_aItems.end() )
4645                 {
4646                     InvalidateMenuButton();
4647                     ImplChangeHighlight( &(*it) );
4648                 }
4649             }
4650             return true;
4651         }
4652 
4653         if( bUp )
4654         {
4655             // Select first valid item
4656             ImplToolItems::iterator it = std::find_if(mpData->m_aItems.begin(), mpData->m_aItems.end(),
4657                 [](const ImplToolItem& rItem) { return ImplIsValidItem( &rItem, false ); });
4658 
4659             // select the menu button if a clipped item would be selected
4660             if( (it != mpData->m_aItems.end() && &(*it) == ImplGetFirstClippedItem()) && IsMenuEnabled() )
4661             {
4662                 ImplChangeHighlight( nullptr );
4663                 mpData->mbMenubuttonSelected = true;
4664                 InvalidateMenuButton();
4665             }
4666             else
4667                 ImplChangeHighlight( (it != mpData->m_aItems.end()) ? &(*it) : nullptr );
4668             return true;
4669         }
4670         else
4671         {
4672             // Select last valid item
4673 
4674             // docked toolbars have the menubutton as last item - if this button is enabled
4675             if( ImplHasClippedItems() && IsMenuEnabled() && !ImplIsFloatingMode() )
4676             {
4677                 ImplChangeHighlight( nullptr );
4678                 mpData->mbMenubuttonSelected = true;
4679                 InvalidateMenuButton();
4680             }
4681             else
4682             {
4683                 ImplToolItem* pItem = nullptr;
4684                 auto it = std::find_if(mpData->m_aItems.rbegin(), mpData->m_aItems.rend(),
4685                     [](const ImplToolItem& rItem) { return ImplIsValidItem( &rItem, false ); });
4686                 if( it != mpData->m_aItems.rend() )
4687                     pItem = &(*it);
4688 
4689                 ImplChangeHighlight( pItem );
4690             }
4691             return true;
4692         }
4693     }
4694 
4695     assert(pToolItem);
4696 
4697     ImplToolItems::size_type pos = ToolBox::ImplFindItemPos( pToolItem, mpData->m_aItems );
4698     ImplToolItems::size_type nCount = mpData->m_aItems.size();
4699 
4700     ImplToolItems::size_type i=0;
4701     do
4702     {
4703         if( bUp )
4704         {
4705             if( !pos-- )
4706             {
4707                 if( bNoCycle )
4708                     return false;
4709 
4710                 // highlight the menu button if it is the last item
4711                 if( ImplHasClippedItems() && IsMenuEnabled() && !ImplIsFloatingMode() )
4712                 {
4713                     ImplChangeHighlight( nullptr );
4714                     mpData->mbMenubuttonSelected = true;
4715                     InvalidateMenuButton();
4716                     return true;
4717                 }
4718                 else
4719                     pos = nCount-1;
4720             }
4721         }
4722         else
4723         {
4724             if( ++pos >= nCount )
4725             {
4726                 if( bNoCycle )
4727                     return false;
4728 
4729                 // highlight the menu button if it is the last item
4730                 if( ImplHasClippedItems() && IsMenuEnabled() && !ImplIsFloatingMode() )
4731                 {
4732                     ImplChangeHighlight( nullptr );
4733                     mpData->mbMenubuttonSelected = true;
4734                     InvalidateMenuButton();
4735                     return true;
4736                 }
4737                 else
4738                     pos = 0;
4739             }
4740         }
4741 
4742         pToolItem = &mpData->m_aItems[pos];
4743 
4744         if ( ImplIsValidItem( pToolItem, false ) )
4745             break;
4746 
4747     } while( ++i < nCount);
4748 
4749     if( pToolItem->IsClipped() && IsMenuEnabled() )
4750     {
4751         // select the menu button if a clipped item would be selected
4752         ImplChangeHighlight( nullptr );
4753         mpData->mbMenubuttonSelected = true;
4754         InvalidateMenuButton();
4755     }
4756     else if( i != nCount )
4757         ImplChangeHighlight( pToolItem );
4758     else
4759         return false;
4760 
4761     return true;
4762 }
4763 
ImplShowFocus()4764 void ToolBox::ImplShowFocus()
4765 {
4766     if( mnHighItemId && HasFocus() )
4767     {
4768         ImplToolItem* pItem = ImplGetItem( mnHighItemId );
4769         if (pItem && pItem->mpWindow && !pItem->mpWindow->isDisposed())
4770         {
4771             vcl::Window *pWin = pItem->mpWindow->ImplGetWindowImpl()->mpBorderWindow ? pItem->mpWindow->ImplGetWindowImpl()->mpBorderWindow.get() : pItem->mpWindow.get();
4772             pWin->ImplGetWindowImpl()->mbDrawSelectionBackground = true;
4773             pWin->Invalidate();
4774         }
4775     }
4776 }
4777 
ImplHideFocus()4778 void ToolBox::ImplHideFocus()
4779 {
4780     if( mnHighItemId )
4781     {
4782         mpData->mbMenubuttonWasLastSelected = false;
4783         ImplToolItem* pItem = ImplGetItem( mnHighItemId );
4784         if( pItem && pItem->mpWindow )
4785         {
4786             vcl::Window *pWin = pItem->mpWindow->ImplGetWindowImpl()->mpBorderWindow ? pItem->mpWindow->ImplGetWindowImpl()->mpBorderWindow.get() : pItem->mpWindow.get();
4787             pWin->ImplGetWindowImpl()->mbDrawSelectionBackground = false;
4788             pWin->Invalidate();
4789         }
4790     }
4791 
4792     if ( mpData && mpData->mbMenubuttonSelected )
4793     {
4794         mpData->mbMenubuttonWasLastSelected = true;
4795         // remove highlight from menubutton
4796         mpData->mbMenubuttonSelected = false;
4797         InvalidateMenuButton();
4798     }
4799 }
4800 
SetToolbarLayoutMode(ToolBoxLayoutMode eLayout)4801 void ToolBox::SetToolbarLayoutMode( ToolBoxLayoutMode eLayout )
4802 {
4803     if ( meLayoutMode != eLayout )
4804        meLayoutMode  = eLayout;
4805 }
4806 
SetToolBoxTextPosition(ToolBoxTextPosition ePosition)4807 void ToolBox::SetToolBoxTextPosition( ToolBoxTextPosition ePosition )
4808 {
4809     meTextPosition = ePosition;
4810 }
4811 
4812 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
4813