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 #include <menutogglebutton.hxx>
31 
32 namespace
33 {
34 void collectUIInformation( const OUString& aID, const OUString& aevent , const OUString& akey , const OUString& avalue)
35 {
36  EventDescription aDescription;
37  aDescription.aID = aID;
38  aDescription.aParameters = {{ akey , avalue}};
39  aDescription.aAction = aevent;
40  aDescription.aParent = "MainWindow";
41  aDescription.aKeyWord = "MenuButton";
42  UITestLogger::getInstance().logEvent(aDescription);
43 }
44 }
45 
46 void MenuButton::ImplInit( vcl::Window* pParent, WinBits nStyle )
47 {
48  if ( !(nStyle & WB_NOTABSTOP) )
49  nStyle |= WB_TABSTOP;
50 
51  PushButton::ImplInit( pParent, nStyle );
53 }
54 
56 {
57  mbStartingMenu = true;
58 
59  Activate();
60 
61  if (!mpMenu && !mpFloatingWindow)
62  {
63  mbStartingMenu = false;
64  return;
65  }
66 
67  Size aSize = GetSizePixel();
68  SetPressed( true );
69  EndSelection();
70  if (mpMenu)
71  {
72  Point aPos(0, 1);
73  tools::Rectangle aRect(aPos, aSize );
75 
76  if (IsDisposed())
77  return;
78 
81  }
82  else
83  {
85  tools::Rectangle aRect(aPos, aSize );
87  if (mpFloatingWindow->GetType() == WindowType::FLOATINGWINDOW)
88  static_cast<FloatingWindow*>(mpFloatingWindow.get())->StartPopupMode(aRect, nFlags);
89  else
90  {
91  mpFloatingWindow->EnableDocking();
93  }
94  }
95 
96  mbStartingMenu = false;
97 
98  SetPressed(false);
99  OUString aID = get_id(); // tdf#136678 take a copy if we are destroyed by Select callback
100  if (mnCurItemId)
101  {
102  Select();
103  mnCurItemId = 0;
104  msCurItemIdent.clear();
105  }
106  collectUIInformation(aID,"OPENLIST","","");
107 }
108 
110 {
111  if (!mpMenu && !mpFloatingWindow)
112  return;
113 
114  if (mpMenu)
115  {
116  mpMenu->EndExecute();
117  }
118  else
119  {
120  if (mpFloatingWindow->GetType() == WindowType::FLOATINGWINDOW)
121  static_cast<FloatingWindow*>(mpFloatingWindow.get())->EndPopupMode();
122  else
124  }
125  collectUIInformation(get_id(),"CLOSELIST","","");
126 }
127 
129 {
130  if (mbStartingMenu)
131  return true;
132 
133  if (!mpMenu && !mpFloatingWindow)
134  return false;
135 
136  if (mpMenu)
138  else
139  {
140  if (mpFloatingWindow->GetType() == WindowType::FLOATINGWINDOW)
141  return static_cast<const FloatingWindow*>(mpFloatingWindow.get())->IsInPopupMode();
142  else
144  }
145 }
146 
149  , mnCurItemId(0)
150  , mbDelayMenu(false)
151  , mbStartingMenu(false)
152 {
154  ImplInit(pParent, nWinBits);
155 }
156 
158 {
159  disposeOnce();
160 }
161 
163 {
164  mpMenuTimer.reset();
166  mpMenu.clear();
168 }
169 
170 IMPL_LINK_NOARG(MenuButton, ImplMenuTimeoutHdl, Timer *, void)
171 {
172  // See if Button Tracking is still active, as it could've been cancelled earlier
173  if ( IsTracking() )
174  {
175  if ( !(GetStyle() & WB_NOPOINTERFOCUS) )
176  GrabFocus();
177  ExecuteMenu();
178  }
179 }
180 
182 {
183  bool bExecute = true;
184  if (mbDelayMenu)
185  {
186  // If the separated dropdown symbol is not hit, delay the popup execution
187  if( rMEvt.GetPosPixel().X() <= ImplGetSeparatorX() )
188  {
189  if ( !mpMenuTimer )
190  {
191  mpMenuTimer.reset(new Timer("MenuTimer"));
192  mpMenuTimer->SetInvokeHandler( LINK( this, MenuButton, ImplMenuTimeoutHdl ) );
193  }
194 
196  mpMenuTimer->Start();
197 
199  bExecute = false;
200  }
201  }
202  if( bExecute )
203  {
204  if ( PushButton::ImplHitTestPushButton( this, rMEvt.GetPosPixel() ) )
205  {
206  if ( !(GetStyle() & WB_NOPOINTERFOCUS) )
207  GrabFocus();
208  ExecuteMenu();
209  }
210  }
211 }
212 
213 void MenuButton::KeyInput( const KeyEvent& rKEvt )
214 {
215  vcl::KeyCode aKeyCode = rKEvt.GetKeyCode();
216  sal_uInt16 nCode = aKeyCode.GetCode();
217  if ( (nCode == KEY_DOWN) && aKeyCode.IsMod2() )
218  ExecuteMenu();
219  else if ( !mbDelayMenu &&
220  !aKeyCode.GetModifier() &&
221  ((nCode == KEY_RETURN) || (nCode == KEY_SPACE)) )
222  ExecuteMenu();
223  else
224  PushButton::KeyInput( rKEvt );
225 }
226 
228 {
229  maActivateHdl.Call( this );
230 }
231 
233 {
234  if (mnCurItemId)
235  collectUIInformation(get_id(),"OPENFROMLIST","POS",OUString::number(mnCurItemId));
236 
237  maSelectHdl.Call( this );
238 }
239 
241 {
242  if (pNewMenu == mpMenu)
243  return;
244 
245  mpMenu = pNewMenu;
246 }
247 
248 void MenuButton::SetPopover(Window* pWindow)
249 {
250  if (pWindow == mpFloatingWindow)
251  return;
252 
253  mpFloatingWindow = pWindow;
254 }
255 
256 
258 {
260 }
261 
265 }
266 
267 //class MenuToggleButton ----------------------------------------------------
268 
270  : MenuButton( pParent, nWinBits )
271 {
272 }
273 
275 {
276  disposeOnce();
277 }
278 
280 {
281  mbIsActive = bSel;
282 }
283 
285 {
286  return mbIsActive;
287 }
288 
289 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual Point GetPosPixel() const
Definition: window.cxx:2786
virtual void EnableRTL(bool bEnable=true) override
Definition: ctrl.cxx:65
bool mbDelayMenu
Definition: menubtn.hxx:41
void EndSelection()
Definition: button.cxx:1553
bool mbIsActive
Definition: button.hxx:171
virtual void KeyInput(const KeyEvent &rKEvt) override
Definition: button.cxx:1232
bool InPopupMode() const
Definition: menubtn.cxx:128
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:162
MenuToggleButton(vcl::Window *pParent, WinBits nStyle)
Definition: menubtn.cxx:269
std::function< std::unique_ptr< UIObject >vcl::Window *)> FactoryFunction
virtual Size GetSizePixel() const
Definition: window.cxx:2397
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:213
void SetPopover(Window *pWindow)
Definition: menubtn.cxx:248
void ExecuteMenu()
Definition: menubtn.cxx:55
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:263
WinBits const WB_NOPOINTERFOCUS
static UITestLogger & getInstance()
Definition: logger.cxx:608
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:3910
SAL_DLLPRIVATE tools::Long ImplGetSeparatorX() const
The x-coordinate of the vertical separator line, use in MenuButton subclass only. ...
Definition: button.cxx:161
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:257
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:262
void SetCurItemId()
Definition: menubtn.cxx:262
virtual void Activate() override
Definition: menubtn.cxx:227
static PopupMenu * GetActivePopupMenu()
Definition: menu.cxx:2745
SAL_DLLPRIVATE void ImplInit(vcl::Window *pParent, WinBits nStyle)
Definition: menubtn.cxx:46
void CancelMenu()
Definition: menubtn.cxx:109
std::unique_ptr< Timer > mpMenuTimer
Definition: menubtn.hxx:36
static bool GetLayoutRTL()
virtual ~MenuButton() override
Definition: menubtn.cxx:157
VclPtr< Window > mpFloatingWindow
Definition: menubtn.hxx:38
bool mbStartingMenu
Definition: menubtn.hxx:42
FloatWinPopupFlags
Definition: floatwin.hxx:31
void GrabFocus()
Definition: window.cxx:2954
vcl::Window * GetParent() const
Definition: window2.cxx:1097
bool GetActive() const
Definition: menubtn.cxx:284
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
void SetActive(bool bSel)
Definition: menubtn.cxx:279
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:1544
virtual void MouseButtonDown(const MouseEvent &rMEvt) override
Definition: menubtn.cxx:181
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:232
void SetPopupMenu(PopupMenu *pNewMenu)
Definition: menubtn.cxx:240
const Point & GetPosPixel() const
Definition: event.hxx:120
virtual ~MenuToggleButton() override
Definition: menubtn.cxx:274
void EndPopupMode(const vcl::Window *pWin)
Definition: dockmgr.cxx:362
bool IsDisposed() const
Definition: window.cxx:142
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:2798
std::map< OUString, OUString > aParameters
virtual void MouseButtonDown(const MouseEvent &rMEvt) override
Definition: button.cxx:1149
WinBits const WB_NOTABSTOP
static DockingManager * GetDockingManager()
Definition: window2.cxx:803
bool IsMod2() const
Definition: keycod.hxx:60