LibreOffice Module vcl (master)  1
menubtn.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * 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/dockwin.hxx>
21 #include <vcl/event.hxx>
22 #include <vcl/floatwin.hxx>
23 #include <vcl/menu.hxx>
24 #include <vcl/timer.hxx>
25 #include <vcl/menubtn.hxx>
26 #include <vcl/settings.hxx>
27 
28 void MenuButton::ImplInit( vcl::Window* pParent, WinBits nStyle )
29 {
30  if ( !(nStyle & WB_NOTABSTOP) )
31  nStyle |= WB_TABSTOP;
32 
33  PushButton::ImplInit( pParent, nStyle );
35 }
36 
38 {
39  Activate();
40 
41  if (!mpMenu && !mpFloatingWindow)
42  return;
43 
44  Size aSize = GetSizePixel();
45  SetPressed( true );
46  EndSelection();
47  if (mpMenu)
48  {
49  Point aPos(0, 1);
50  tools::Rectangle aRect(aPos, aSize );
52 
53  if (IsDisposed())
54  return;
55 
58  }
59  else
60  {
62  tools::Rectangle aRect(aPos, aSize );
64  if (mpFloatingWindow->GetType() == WindowType::FLOATINGWINDOW)
65  static_cast<FloatingWindow*>(mpFloatingWindow.get())->StartPopupMode(aRect, nFlags);
66  else
67  {
68  mpFloatingWindow->EnableDocking();
70  }
71  }
72  SetPressed(false);
73  if (mnCurItemId)
74  {
75  Select();
76  mnCurItemId = 0;
77  msCurItemIdent.clear();
78  }
79 }
80 
82 {
83  if (!mpMenu && !mpFloatingWindow)
84  return;
85 
86  if (mpMenu)
87  {
88  mpMenu->EndExecute();
89  }
90  else
91  {
92  if (mpFloatingWindow->GetType() == WindowType::FLOATINGWINDOW)
93  static_cast<FloatingWindow*>(mpFloatingWindow.get())->EndPopupMode();
94  else
96  }
97 }
98 
100 {
101  if (!mpMenu && !mpFloatingWindow)
102  return false;
103 
104  if (mpMenu)
106  else
107  {
108  if (mpFloatingWindow->GetType() == WindowType::FLOATINGWINDOW)
109  return static_cast<const FloatingWindow*>(mpFloatingWindow.get())->IsInPopupMode();
110  else
112  }
113 }
114 
117  , mnCurItemId(0)
118  , mbDelayMenu(false)
119 {
121  ImplInit(pParent, nWinBits);
122 }
123 
125 {
126  disposeOnce();
127 }
128 
130 {
131  mpMenuTimer.reset();
133  mpMenu.clear();
135 }
136 
137 IMPL_LINK_NOARG(MenuButton, ImplMenuTimeoutHdl, Timer *, void)
138 {
139  // See if Button Tracking is still active, as it could've been cancelled earlier
140  if ( IsTracking() )
141  {
142  if ( !(GetStyle() & WB_NOPOINTERFOCUS) )
143  GrabFocus();
144  ExecuteMenu();
145  }
146 }
147 
149 {
150  bool bExecute = true;
151  if (mbDelayMenu)
152  {
153  // If the separated dropdown symbol is not hit, delay the popup execution
154  if( mnDDStyle == PushButtonDropdownStyle::Toolbox || // no separator at all
155  rMEvt.GetPosPixel().X() <= ImplGetSeparatorX() )
156  {
157  if ( !mpMenuTimer )
158  {
159  mpMenuTimer.reset(new Timer("MenuTimer"));
160  mpMenuTimer->SetInvokeHandler( LINK( this, MenuButton, ImplMenuTimeoutHdl ) );
161  }
162 
164  mpMenuTimer->Start();
165 
167  bExecute = false;
168  }
169  }
170  if( bExecute )
171  {
172  if ( PushButton::ImplHitTestPushButton( this, rMEvt.GetPosPixel() ) )
173  {
174  if ( !(GetStyle() & WB_NOPOINTERFOCUS) )
175  GrabFocus();
176  ExecuteMenu();
177  }
178  }
179 }
180 
181 void MenuButton::KeyInput( const KeyEvent& rKEvt )
182 {
183  vcl::KeyCode aKeyCode = rKEvt.GetKeyCode();
184  sal_uInt16 nCode = aKeyCode.GetCode();
185  if ( (nCode == KEY_DOWN) && aKeyCode.IsMod2() )
186  ExecuteMenu();
187  else if ( !mbDelayMenu &&
188  !aKeyCode.GetModifier() &&
189  ((nCode == KEY_RETURN) || (nCode == KEY_SPACE)) )
190  ExecuteMenu();
191  else
192  PushButton::KeyInput( rKEvt );
193 }
194 
196 {
197  maActivateHdl.Call( this );
198 }
199 
201 {
202  maSelectHdl.Call( this );
203 }
204 
206 {
207  if (pNewMenu == mpMenu)
208  return;
209 
210  mpMenu = pNewMenu;
211 }
212 
213 void MenuButton::SetPopover(Window* pWindow)
214 {
215  if (pWindow == mpFloatingWindow)
216  return;
217 
218  mpFloatingWindow = pWindow;
219 }
220 
221 //class MenuToggleButton ----------------------------------------------------
222 
224  : MenuButton( pParent, nWinBits )
225 {
226 }
227 
229 {
230  disposeOnce();
231 }
232 
234 {
235  mbIsActive = bSel;
236 }
237 
238 
239 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual Point GetPosPixel() const
Definition: window.cxx:2806
virtual void EnableRTL(bool bEnable=true) override
Definition: ctrl.cxx:65
bool mbDelayMenu
Definition: menubtn.hxx:40
void EndSelection()
Definition: button.cxx:1571
bool mbIsActive
Definition: button.hxx:171
#define KEY_SPACE
Definition: keycodes.hxx:123
virtual void KeyInput(const KeyEvent &rKEvt) override
Definition: button.cxx:1247
virtual void dispose() override
This is intended to be used to clear any locally held references to other Window-subclass objects...
Definition: menubtn.cxx:129
MenuToggleButton(vcl::Window *pParent, WinBits nStyle)
Definition: menubtn.cxx:223
virtual Size GetSizePixel() const
Definition: window.cxx:2364
sal_uInt16 GetCode() const
Definition: keycod.hxx:53
sal_uInt16 Execute(vcl::Window *pWindow, const Point &rPopupPos)
Definition: menu.cxx:2767
virtual void KeyInput(const KeyEvent &rKEvt) override
Definition: menubtn.cxx:181
void SetPopover(Window *pWindow)
Definition: menubtn.cxx:213
void ExecuteMenu()
Definition: menubtn.cxx:37
Link< MenuButton *, void > maSelectHdl
Definition: menubtn.hxx:42
virtual void dispose() override
This is intended to be used to clear any locally held references to other Window-subclass objects...
Definition: button.cxx:105
SAL_DLLPRIVATE void ImplInit(vcl::Window *pParent, WinBits nStyle)
Definition: button.cxx:585
OString const & GetCurItemIdent() const
Definition: menu.hxx:266
WinBits const WB_NOPOINTERFOCUS
sal_Int64 WinBits
sal_uInt16 GetModifier() const
Definition: keycod.hxx:56
PushButtonDropdownStyle mnDDStyle
Definition: button.hxx:170
sal_uInt16 mnCurItemId
Definition: menubtn.hxx:39
VclPtr< PopupMenu > mpMenu
Definition: menubtn.hxx:36
void clear()
Definition: vclptr.hxx:190
MenuButton(const MenuButton &)=delete
sal_uInt16 GetCurItemId() const
Definition: menu.hxx:265
virtual void Activate() override
Definition: menubtn.cxx:195
static PopupMenu * GetActivePopupMenu()
Definition: menu.cxx:2718
SAL_DLLPRIVATE void ImplInit(vcl::Window *pParent, WinBits nStyle)
Definition: menubtn.cxx:28
void CancelMenu()
Definition: menubtn.cxx:81
std::unique_ptr< Timer > mpMenuTimer
Definition: menubtn.hxx:35
static bool GetLayoutRTL()
virtual ~MenuButton() override
Definition: menubtn.cxx:124
VclPtr< Window > mpFloatingWindow
Definition: menubtn.hxx:37
FloatWinPopupFlags
Definition: floatwin.hxx:31
void GrabFocus()
Definition: window.cxx:2980
vcl::Window * GetParent() const
Definition: window2.cxx:1086
long X() const
Link< MenuButton *, void > maActivateHdl
Definition: menubtn.hxx:41
bool IsInPopupMode(const vcl::Window *pWin)
Definition: dockmgr.cxx:352
void SetActive(bool bSel)
Definition: menubtn.cxx:233
static sal_uLong GetActionDelay()
#define KEY_RETURN
Definition: keycodes.hxx:119
OString msCurItemIdent
Definition: menubtn.hxx:38
const vcl::KeyCode & GetKeyCode() const
Definition: event.hxx:53
void SetPressed(bool bPressed)
Definition: button.cxx:1562
virtual void MouseButtonDown(const MouseEvent &rMEvt) override
Definition: menubtn.cxx:148
#define KEY_DOWN
Definition: keycodes.hxx:110
void EndExecute()
Definition: menu.cxx:2724
static SAL_DLLPRIVATE bool ImplHitTestPushButton(vcl::Window const *pDev, const Point &rPos)
Definition: button.cxx:688
WindowType
bool MenuShown() const
Definition: menubtn.cxx:99
virtual void Select()
Definition: menubtn.cxx:200
void SetPopupMenu(PopupMenu *pNewMenu)
Definition: menubtn.cxx:205
const Point & GetPosPixel() const
Definition: event.hxx:117
virtual ~MenuToggleButton() override
Definition: menubtn.cxx:228
void EndPopupMode(const vcl::Window *pWin)
Definition: dockmgr.cxx:358
bool IsDisposed() const
Definition: window.cxx:136
WinBits const WB_TABSTOP
reference_type * get() const
Get the body.
Definition: vclptr.hxx:143
void StartPopupMode(const vcl::Window *pWin, const tools::Rectangle &rRect, FloatWinPopupFlags nPopupModeFlags)
Definition: dockmgr.cxx:331
WinBits GetStyle() const
Definition: window2.cxx:942
Definition: timer.hxx:26
Point OutputToScreenPixel(const Point &rPos) const
Definition: window.cxx:2818
SAL_DLLPRIVATE long ImplGetSeparatorX() const
The x-coordinate of the vertical separator line, use in MenuButton subclass only. ...
Definition: button.cxx:160
virtual void MouseButtonDown(const MouseEvent &rMEvt) override
Definition: button.cxx:1163
WinBits const WB_NOTABSTOP
static DockingManager * GetDockingManager()
Definition: window2.cxx:803
bool IsMod2() const
Definition: keycod.hxx:62