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  mbStartingMenu = true;
40 
41  Activate();
42 
43  if (!mpMenu && !mpFloatingWindow)
44  {
45  mbStartingMenu = false;
46  return;
47  }
48 
49  Size aSize = GetSizePixel();
50  SetPressed( true );
51  EndSelection();
52  if (mpMenu)
53  {
54  Point aPos(0, 1);
55  tools::Rectangle aRect(aPos, aSize );
57 
58  if (IsDisposed())
59  return;
60 
63  }
64  else
65  {
67  tools::Rectangle aRect(aPos, aSize );
69  if (mpFloatingWindow->GetType() == WindowType::FLOATINGWINDOW)
70  static_cast<FloatingWindow*>(mpFloatingWindow.get())->StartPopupMode(aRect, nFlags);
71  else
72  {
73  mpFloatingWindow->EnableDocking();
75  }
76  }
77 
78  mbStartingMenu = false;
79 
80  SetPressed(false);
81  if (mnCurItemId)
82  {
83  Select();
84  mnCurItemId = 0;
85  msCurItemIdent.clear();
86  }
87 }
88 
90 {
91  if (!mpMenu && !mpFloatingWindow)
92  return;
93 
94  if (mpMenu)
95  {
96  mpMenu->EndExecute();
97  }
98  else
99  {
100  if (mpFloatingWindow->GetType() == WindowType::FLOATINGWINDOW)
101  static_cast<FloatingWindow*>(mpFloatingWindow.get())->EndPopupMode();
102  else
104  }
105 }
106 
108 {
109  if (mbStartingMenu)
110  return true;
111 
112  if (!mpMenu && !mpFloatingWindow)
113  return false;
114 
115  if (mpMenu)
117  else
118  {
119  if (mpFloatingWindow->GetType() == WindowType::FLOATINGWINDOW)
120  return static_cast<const FloatingWindow*>(mpFloatingWindow.get())->IsInPopupMode();
121  else
123  }
124 }
125 
128  , mnCurItemId(0)
129  , mbDelayMenu(false)
130  , mbStartingMenu(false)
131 {
133  ImplInit(pParent, nWinBits);
134 }
135 
137 {
138  disposeOnce();
139 }
140 
142 {
143  mpMenuTimer.reset();
145  mpMenu.clear();
147 }
148 
149 IMPL_LINK_NOARG(MenuButton, ImplMenuTimeoutHdl, Timer *, void)
150 {
151  // See if Button Tracking is still active, as it could've been cancelled earlier
152  if ( IsTracking() )
153  {
154  if ( !(GetStyle() & WB_NOPOINTERFOCUS) )
155  GrabFocus();
156  ExecuteMenu();
157  }
158 }
159 
161 {
162  bool bExecute = true;
163  if (mbDelayMenu)
164  {
165  // If the separated dropdown symbol is not hit, delay the popup execution
166  if( rMEvt.GetPosPixel().X() <= ImplGetSeparatorX() )
167  {
168  if ( !mpMenuTimer )
169  {
170  mpMenuTimer.reset(new Timer("MenuTimer"));
171  mpMenuTimer->SetInvokeHandler( LINK( this, MenuButton, ImplMenuTimeoutHdl ) );
172  }
173 
175  mpMenuTimer->Start();
176 
178  bExecute = false;
179  }
180  }
181  if( bExecute )
182  {
183  if ( PushButton::ImplHitTestPushButton( this, rMEvt.GetPosPixel() ) )
184  {
185  if ( !(GetStyle() & WB_NOPOINTERFOCUS) )
186  GrabFocus();
187  ExecuteMenu();
188  }
189  }
190 }
191 
192 void MenuButton::KeyInput( const KeyEvent& rKEvt )
193 {
194  vcl::KeyCode aKeyCode = rKEvt.GetKeyCode();
195  sal_uInt16 nCode = aKeyCode.GetCode();
196  if ( (nCode == KEY_DOWN) && aKeyCode.IsMod2() )
197  ExecuteMenu();
198  else if ( !mbDelayMenu &&
199  !aKeyCode.GetModifier() &&
200  ((nCode == KEY_RETURN) || (nCode == KEY_SPACE)) )
201  ExecuteMenu();
202  else
203  PushButton::KeyInput( rKEvt );
204 }
205 
207 {
208  maActivateHdl.Call( this );
209 }
210 
212 {
213  maSelectHdl.Call( this );
214 }
215 
217 {
218  if (pNewMenu == mpMenu)
219  return;
220 
221  mpMenu = pNewMenu;
222 }
223 
224 void MenuButton::SetPopover(Window* pWindow)
225 {
226  if (pWindow == mpFloatingWindow)
227  return;
228 
229  mpFloatingWindow = pWindow;
230 }
231 
232 //class MenuToggleButton ----------------------------------------------------
233 
235  : MenuButton( pParent, nWinBits )
236 {
237 }
238 
240 {
241  disposeOnce();
242 }
243 
245 {
246  mbIsActive = bSel;
247 }
248 
249 
250 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual Point GetPosPixel() const
Definition: window.cxx:2803
virtual void EnableRTL(bool bEnable=true) override
Definition: ctrl.cxx:65
bool mbDelayMenu
Definition: menubtn.hxx:40
void EndSelection()
Definition: button.cxx:1525
bool mbIsActive
Definition: button.hxx:171
#define KEY_SPACE
Definition: keycodes.hxx:123
virtual void KeyInput(const KeyEvent &rKEvt) override
Definition: button.cxx:1201
bool InPopupMode() const
Definition: menubtn.cxx:107
virtual void dispose() override
This is intended to be used to clear any locally held references to other Window-subclass objects...
Definition: menubtn.cxx:141
MenuToggleButton(vcl::Window *pParent, WinBits nStyle)
Definition: menubtn.cxx:234
virtual Size GetSizePixel() const
Definition: window.cxx:2365
sal_uInt16 GetCode() const
Definition: keycod.hxx:53
sal_uInt16 Execute(vcl::Window *pWindow, const Point &rPopupPos)
Definition: menu.cxx:2763
virtual void KeyInput(const KeyEvent &rKEvt) override
Definition: menubtn.cxx:192
void SetPopover(Window *pWindow)
Definition: menubtn.cxx:224
void ExecuteMenu()
Definition: menubtn.cxx:37
Link< MenuButton *, void > maSelectHdl
Definition: menubtn.hxx:43
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:265
WinBits const WB_NOPOINTERFOCUS
sal_Int64 WinBits
sal_uInt16 GetModifier() const
Definition: keycod.hxx:56
PushButtonDropdownStyle mnDDStyle
Definition: button.hxx:170
sal_uInt16 nCode
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:264
virtual void Activate() override
Definition: menubtn.cxx:206
static PopupMenu * GetActivePopupMenu()
Definition: menu.cxx:2714
SAL_DLLPRIVATE void ImplInit(vcl::Window *pParent, WinBits nStyle)
Definition: menubtn.cxx:28
void CancelMenu()
Definition: menubtn.cxx:89
std::unique_ptr< Timer > mpMenuTimer
Definition: menubtn.hxx:35
static bool GetLayoutRTL()
virtual ~MenuButton() override
Definition: menubtn.cxx:136
VclPtr< Window > mpFloatingWindow
Definition: menubtn.hxx:37
bool mbStartingMenu
Definition: menubtn.hxx:41
FloatWinPopupFlags
Definition: floatwin.hxx:31
void GrabFocus()
Definition: window.cxx:2977
vcl::Window * GetParent() const
Definition: window2.cxx:1092
long X() const
Link< MenuButton *, void > maActivateHdl
Definition: menubtn.hxx:42
bool IsInPopupMode(const vcl::Window *pWin)
Definition: dockmgr.cxx:356
void SetActive(bool bSel)
Definition: menubtn.cxx:244
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:1516
virtual void MouseButtonDown(const MouseEvent &rMEvt) override
Definition: menubtn.cxx:160
#define KEY_DOWN
Definition: keycodes.hxx:110
void EndExecute()
Definition: menu.cxx:2720
static SAL_DLLPRIVATE bool ImplHitTestPushButton(vcl::Window const *pDev, const Point &rPos)
Definition: button.cxx:688
WindowType
virtual void Select()
Definition: menubtn.cxx:211
void SetPopupMenu(PopupMenu *pNewMenu)
Definition: menubtn.cxx:216
const Point & GetPosPixel() const
Definition: event.hxx:117
virtual ~MenuToggleButton() override
Definition: menubtn.cxx:239
void EndPopupMode(const vcl::Window *pWin)
Definition: dockmgr.cxx:362
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:335
WinBits GetStyle() const
Definition: window2.cxx:948
Definition: timer.hxx:26
Point OutputToScreenPixel(const Point &rPos) const
Definition: window.cxx:2815
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:1117
WinBits const WB_NOTABSTOP
static DockingManager * GetDockingManager()
Definition: window2.cxx:803
bool IsMod2() const
Definition: keycod.hxx:62