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 #include <vcl/uitest/uiobject.hxx>
28 #include <vcl/uitest/logger.hxx>
30 
31 namespace
32 {
33 void collectUIInformation( const OUString& aID, const OUString& aevent , const OUString& akey , const OUString& avalue)
34 {
35  EventDescription aDescription;
36  aDescription.aID = aID;
37  aDescription.aParameters = {{ akey , avalue}};
38  aDescription.aAction = aevent;
39  aDescription.aParent = "MainWindow";
40  aDescription.aKeyWord = "MenuButton";
41  UITestLogger::getInstance().logEvent(aDescription);
42 }
43 }
44 
45 void MenuButton::ImplInit( vcl::Window* pParent, WinBits nStyle )
46 {
47  if ( !(nStyle & WB_NOTABSTOP) )
48  nStyle |= WB_TABSTOP;
49 
50  PushButton::ImplInit( pParent, nStyle );
52 }
53 
55 {
56  mbStartingMenu = true;
57 
58  Activate();
59 
60  if (!mpMenu && !mpFloatingWindow)
61  {
62  mbStartingMenu = false;
63  return;
64  }
65 
66  Size aSize = GetSizePixel();
67  SetPressed( true );
68  EndSelection();
69  if (mpMenu)
70  {
71  Point aPos(0, 1);
72  tools::Rectangle aRect(aPos, aSize );
74 
75  if (IsDisposed())
76  return;
77 
80  }
81  else
82  {
84  tools::Rectangle aRect(aPos, aSize );
86  if (mpFloatingWindow->GetType() == WindowType::FLOATINGWINDOW)
87  static_cast<FloatingWindow*>(mpFloatingWindow.get())->StartPopupMode(aRect, nFlags);
88  else
89  {
90  mpFloatingWindow->EnableDocking();
92  }
93  }
94 
95  mbStartingMenu = false;
96 
97  SetPressed(false);
98  if (mnCurItemId)
99  {
100  Select();
101  mnCurItemId = 0;
102  msCurItemIdent.clear();
103  }
104  collectUIInformation(get_id(),"OPENLIST","","");
105 }
106 
108 {
109  if (!mpMenu && !mpFloatingWindow)
110  return;
111 
112  if (mpMenu)
113  {
114  mpMenu->EndExecute();
115  }
116  else
117  {
118  if (mpFloatingWindow->GetType() == WindowType::FLOATINGWINDOW)
119  static_cast<FloatingWindow*>(mpFloatingWindow.get())->EndPopupMode();
120  else
122  }
123  collectUIInformation(get_id(),"CLOSELIST","","");
124 }
125 
127 {
128  if (mbStartingMenu)
129  return true;
130 
131  if (!mpMenu && !mpFloatingWindow)
132  return false;
133 
134  if (mpMenu)
136  else
137  {
138  if (mpFloatingWindow->GetType() == WindowType::FLOATINGWINDOW)
139  return static_cast<const FloatingWindow*>(mpFloatingWindow.get())->IsInPopupMode();
140  else
142  }
143 }
144 
147  , mnCurItemId(0)
148  , mbDelayMenu(false)
149  , mbStartingMenu(false)
150 {
152  ImplInit(pParent, nWinBits);
153 }
154 
156 {
157  disposeOnce();
158 }
159 
161 {
162  mpMenuTimer.reset();
164  mpMenu.clear();
166 }
167 
168 IMPL_LINK_NOARG(MenuButton, ImplMenuTimeoutHdl, Timer *, void)
169 {
170  // See if Button Tracking is still active, as it could've been cancelled earlier
171  if ( IsTracking() )
172  {
173  if ( !(GetStyle() & WB_NOPOINTERFOCUS) )
174  GrabFocus();
175  ExecuteMenu();
176  }
177 }
178 
180 {
181  bool bExecute = true;
182  if (mbDelayMenu)
183  {
184  // If the separated dropdown symbol is not hit, delay the popup execution
185  if( rMEvt.GetPosPixel().X() <= ImplGetSeparatorX() )
186  {
187  if ( !mpMenuTimer )
188  {
189  mpMenuTimer.reset(new Timer("MenuTimer"));
190  mpMenuTimer->SetInvokeHandler( LINK( this, MenuButton, ImplMenuTimeoutHdl ) );
191  }
192 
194  mpMenuTimer->Start();
195 
197  bExecute = false;
198  }
199  }
200  if( bExecute )
201  {
202  if ( PushButton::ImplHitTestPushButton( this, rMEvt.GetPosPixel() ) )
203  {
204  if ( !(GetStyle() & WB_NOPOINTERFOCUS) )
205  GrabFocus();
206  ExecuteMenu();
207  }
208  }
209 }
210 
211 void MenuButton::KeyInput( const KeyEvent& rKEvt )
212 {
213  vcl::KeyCode aKeyCode = rKEvt.GetKeyCode();
214  sal_uInt16 nCode = aKeyCode.GetCode();
215  if ( (nCode == KEY_DOWN) && aKeyCode.IsMod2() )
216  ExecuteMenu();
217  else if ( !mbDelayMenu &&
218  !aKeyCode.GetModifier() &&
219  ((nCode == KEY_RETURN) || (nCode == KEY_SPACE)) )
220  ExecuteMenu();
221  else
222  PushButton::KeyInput( rKEvt );
223 }
224 
226 {
227  maActivateHdl.Call( this );
228 }
229 
231 {
232  if (mnCurItemId)
233  collectUIInformation(get_id(),"OPENFROMLIST","POS",OUString::number(mnCurItemId));
234 
235  maSelectHdl.Call( this );
236 }
237 
239 {
240  if (pNewMenu == mpMenu)
241  return;
242 
243  mpMenu = pNewMenu;
244 }
245 
246 void MenuButton::SetPopover(Window* pWindow)
247 {
248  if (pWindow == mpFloatingWindow)
249  return;
250 
251  mpFloatingWindow = pWindow;
252 }
253 
254 
256 {
258 }
259 
263 }
264 
265 //class MenuToggleButton ----------------------------------------------------
266 
267 MenuToggleButton::MenuToggleButton( vcl::Window* pParent, WinBits nWinBits )
268  : MenuButton( pParent, nWinBits )
269 {
270 }
271 
272 MenuToggleButton::~MenuToggleButton()
273 {
274  disposeOnce();
275 }
276 
277 void MenuToggleButton::SetActive( bool bSel )
278 {
279  mbIsActive = bSel;
280 }
281 
282 
283 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual Point GetPosPixel() const
Definition: window.cxx:2792
virtual void EnableRTL(bool bEnable=true) override
Definition: ctrl.cxx:65
bool mbDelayMenu
Definition: menubtn.hxx:41
void EndSelection()
Definition: button.cxx:1549
bool mbIsActive
virtual void KeyInput(const KeyEvent &rKEvt) override
Definition: button.cxx:1228
bool InPopupMode() const
Definition: menubtn.cxx:126
static std::unique_ptr< UIObject > create(vcl::Window *pWindow)
virtual void dispose() override
This is intended to be used to clear any locally held references to other Window-subclass objects...
Definition: menubtn.cxx:160
std::function< std::unique_ptr< UIObject >vcl::Window *)> FactoryFunction
virtual Size GetSizePixel() const
Definition: window.cxx:2395
sal_uInt16 GetCode() const
Definition: keycod.hxx:51
sal_uInt16 Execute(vcl::Window *pWindow, const Point &rPopupPos)
Definition: menu.cxx:2794
constexpr sal_uInt16 KEY_SPACE
Definition: keycodes.hxx:123
virtual void KeyInput(const KeyEvent &rKEvt) override
Definition: menubtn.cxx:211
void SetPopover(Window *pWindow)
Definition: menubtn.cxx:246
void ExecuteMenu()
Definition: menubtn.cxx:54
Link< MenuButton *, void > maSelectHdl
Definition: menubtn.hxx:44
virtual void dispose() override
This is intended to be used to clear any locally held references to other Window-subclass objects...
Definition: button.cxx:106
SAL_DLLPRIVATE void ImplInit(vcl::Window *pParent, WinBits nStyle)
Definition: button.cxx:590
OString const & GetCurItemIdent() const
Definition: menu.hxx:262
WinBits const WB_NOPOINTERFOCUS
static UITestLogger & getInstance()
Definition: logger.cxx:603
void logEvent(const EventDescription &rDescription)
Definition: logger.cxx:377
sal_Int64 WinBits
const OUString & get_id() const
Get the ID of the window.
Definition: window.cxx:3859
sal_uInt16 GetModifier() const
Definition: keycod.hxx:54
PushButtonDropdownStyle mnDDStyle
Definition: button.hxx:170
sal_uInt16 nCode
virtual FactoryFunction GetUITestFactory() const override
Definition: menubtn.cxx:255
sal_uInt16 mnCurItemId
Definition: menubtn.hxx:40
VclPtr< PopupMenu > mpMenu
Definition: menubtn.hxx:37
constexpr sal_uInt16 KEY_DOWN
Definition: keycodes.hxx:110
void clear()
Definition: vclptr.hxx:190
MenuButton(const MenuButton &)=delete
sal_uInt16 GetCurItemId() const
Definition: menu.hxx:261
void SetCurItemId()
Definition: menubtn.cxx:260
virtual void Activate() override
Definition: menubtn.cxx:225
static PopupMenu * GetActivePopupMenu()
Definition: menu.cxx:2745
SAL_DLLPRIVATE void ImplInit(vcl::Window *pParent, WinBits nStyle)
Definition: menubtn.cxx:45
void CancelMenu()
Definition: menubtn.cxx:107
std::unique_ptr< Timer > mpMenuTimer
Definition: menubtn.hxx:36
static bool GetLayoutRTL()
virtual ~MenuButton() override
Definition: menubtn.cxx:155
VclPtr< Window > mpFloatingWindow
Definition: menubtn.hxx:38
bool mbStartingMenu
Definition: menubtn.hxx:42
FloatWinPopupFlags
Definition: floatwin.hxx:31
void GrabFocus()
Definition: window.cxx:2960
vcl::Window * GetParent() const
Definition: window2.cxx:1097
Link< MenuButton *, void > maActivateHdl
Definition: menubtn.hxx:43
constexpr sal_uInt16 KEY_RETURN
Definition: keycodes.hxx:119
bool IsInPopupMode(const vcl::Window *pWin)
Definition: dockmgr.cxx:356
static sal_uLong GetActionDelay()
OString msCurItemIdent
Definition: menubtn.hxx:39
const vcl::KeyCode & GetKeyCode() const
Definition: event.hxx:54
void SetPressed(bool bPressed)
Definition: button.cxx:1540
virtual void MouseButtonDown(const MouseEvent &rMEvt) override
Definition: menubtn.cxx:179
void EndExecute()
Definition: menu.cxx:2751
static SAL_DLLPRIVATE bool ImplHitTestPushButton(vcl::Window const *pDev, const Point &rPos)
Definition: button.cxx:693
WindowType
virtual void Select()
Definition: menubtn.cxx:230
void SetPopupMenu(PopupMenu *pNewMenu)
Definition: menubtn.cxx:238
const Point & GetPosPixel() const
Definition: event.hxx:120
void EndPopupMode(const vcl::Window *pWin)
Definition: dockmgr.cxx:362
bool IsDisposed() const
Definition: window.cxx:140
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:953
Definition: timer.hxx:26
Point OutputToScreenPixel(const Point &rPos) const
Definition: window.cxx:2804
SAL_DLLPRIVATE long ImplGetSeparatorX() const
The x-coordinate of the vertical separator line, use in MenuButton subclass only. ...
Definition: button.cxx:161
std::map< OUString, OUString > aParameters
virtual void MouseButtonDown(const MouseEvent &rMEvt) override
Definition: button.cxx:1145
WinBits const WB_NOTABSTOP
static DockingManager * GetDockingManager()
Definition: window2.cxx:803
bool IsMod2() const
Definition: keycod.hxx:60