bump product version to 7.6.3.2-android
[LibreOffice.git] / vcl / source / control / menubtn.cxx
blob64aec098db0fd34ce477fc330cd35a9ed93b5afa
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <vcl/dockwin.hxx>
21 #include <vcl/event.hxx>
22 #include <vcl/toolkit/floatwin.hxx>
23 #include <vcl/menu.hxx>
24 #include <vcl/timer.hxx>
25 #include <vcl/toolkit/menubtn.hxx>
26 #include <vcl/settings.hxx>
27 #include <vcl/uitest/uiobject.hxx>
28 #include <vcl/uitest/logger.hxx>
29 #include <vcl/uitest/eventdescription.hxx>
30 #include <menutogglebutton.hxx>
31 #include <tools/json_writer.hxx>
33 namespace
35 void collectUIInformation( const OUString& aID, const OUString& aevent , const OUString& akey , const OUString& avalue)
37 EventDescription aDescription;
38 aDescription.aID = aID;
39 aDescription.aParameters = {{ akey , avalue}};
40 aDescription.aAction = aevent;
41 aDescription.aParent = "MainWindow";
42 aDescription.aKeyWord = "MenuButton";
43 UITestLogger::getInstance().logEvent(aDescription);
47 void MenuButton::ImplInit( vcl::Window* pParent, WinBits nStyle )
49 if ( !(nStyle & WB_NOTABSTOP) )
50 nStyle |= WB_TABSTOP;
52 PushButton::ImplInit( pParent, nStyle );
53 EnableRTL( AllSettings::GetLayoutRTL() );
56 void MenuButton::ExecuteMenu()
58 mbStartingMenu = true;
60 Activate();
62 if (!mpMenu && !mpFloatingWindow)
64 mbStartingMenu = false;
65 return;
68 Size aSize = GetSizePixel();
69 SetPressed( true );
70 EndSelection();
71 if (mpMenu)
73 Point aPos(0, 1);
74 tools::Rectangle aRect(aPos, aSize );
75 mpMenu->Execute(this, aRect, PopupMenuFlags::ExecuteDown);
77 if (isDisposed())
78 return;
80 mnCurItemId = mpMenu->GetCurItemId();
81 msCurItemIdent = mpMenu->GetCurItemIdent();
83 else
85 Point aPos(GetParent()->OutputToScreenPixel(GetPosPixel()));
86 tools::Rectangle aRect(aPos, aSize );
87 FloatWinPopupFlags nFlags = FloatWinPopupFlags::Down | FloatWinPopupFlags::GrabFocus;
88 if (mpFloatingWindow->GetType() == WindowType::FLOATINGWINDOW)
89 static_cast<FloatingWindow*>(mpFloatingWindow.get())->StartPopupMode(aRect, nFlags);
90 else
92 mpFloatingWindow->EnableDocking();
93 vcl::Window::GetDockingManager()->StartPopupMode(mpFloatingWindow, aRect, nFlags);
97 mbStartingMenu = false;
99 SetPressed(false);
100 OUString aID = get_id(); // tdf#136678 take a copy if we are destroyed by Select callback
101 if (mnCurItemId)
103 Select();
104 mnCurItemId = 0;
105 msCurItemIdent.clear();
107 collectUIInformation(aID,"OPENLIST","","");
110 void MenuButton::CancelMenu()
112 if (!mpMenu && !mpFloatingWindow)
113 return;
115 if (mpMenu)
117 mpMenu->EndExecute();
119 else
121 if (mpFloatingWindow->GetType() == WindowType::FLOATINGWINDOW)
122 static_cast<FloatingWindow*>(mpFloatingWindow.get())->EndPopupMode();
123 else
124 vcl::Window::GetDockingManager()->EndPopupMode(mpFloatingWindow);
126 collectUIInformation(get_id(),"CLOSELIST","","");
129 bool MenuButton::InPopupMode() const
131 if (mbStartingMenu)
132 return true;
134 if (!mpMenu && !mpFloatingWindow)
135 return false;
137 if (mpMenu)
138 return PopupMenu::GetActivePopupMenu() == mpMenu;
139 else
141 if (mpFloatingWindow->GetType() == WindowType::FLOATINGWINDOW)
142 return static_cast<const FloatingWindow*>(mpFloatingWindow.get())->IsInPopupMode();
143 else
144 return vcl::Window::GetDockingManager()->IsInPopupMode(mpFloatingWindow);
148 MenuButton::MenuButton( vcl::Window* pParent, WinBits nWinBits )
149 : PushButton(WindowType::MENUBUTTON)
150 , mnCurItemId(0)
151 , mbDelayMenu(false)
152 , mbStartingMenu(false)
154 mnDDStyle = PushButtonDropdownStyle::MenuButton;
155 ImplInit(pParent, nWinBits);
158 MenuButton::~MenuButton()
160 disposeOnce();
163 void MenuButton::dispose()
165 mpMenuTimer.reset();
166 mpFloatingWindow.clear();
167 mpMenu.clear();
168 PushButton::dispose();
171 IMPL_LINK_NOARG(MenuButton, ImplMenuTimeoutHdl, Timer *, void)
173 // See if Button Tracking is still active, as it could've been cancelled earlier
174 if ( IsTracking() )
176 if ( !(GetStyle() & WB_NOPOINTERFOCUS) )
177 GrabFocus();
178 ExecuteMenu();
182 void MenuButton::MouseButtonDown( const MouseEvent& rMEvt )
184 bool bExecute = true;
185 if (mbDelayMenu)
187 // If the separated dropdown symbol is not hit, delay the popup execution
188 if( rMEvt.GetPosPixel().X() <= ImplGetSeparatorX() )
190 if ( !mpMenuTimer )
192 mpMenuTimer.reset(new Timer("MenuTimer"));
193 mpMenuTimer->SetInvokeHandler( LINK( this, MenuButton, ImplMenuTimeoutHdl ) );
196 mpMenuTimer->SetTimeout( MouseSettings::GetActionDelay() );
197 mpMenuTimer->Start();
199 PushButton::MouseButtonDown( rMEvt );
200 bExecute = false;
203 if( bExecute )
205 if ( PushButton::ImplHitTestPushButton( this, rMEvt.GetPosPixel() ) )
207 if ( !(GetStyle() & WB_NOPOINTERFOCUS) )
208 GrabFocus();
209 ExecuteMenu();
214 void MenuButton::KeyInput( const KeyEvent& rKEvt )
216 vcl::KeyCode aKeyCode = rKEvt.GetKeyCode();
217 sal_uInt16 nCode = aKeyCode.GetCode();
218 if ( (nCode == KEY_DOWN) && aKeyCode.IsMod2() )
219 ExecuteMenu();
220 else if ( !mbDelayMenu &&
221 !aKeyCode.GetModifier() &&
222 ((nCode == KEY_RETURN) || (nCode == KEY_SPACE)) )
223 ExecuteMenu();
224 else
225 PushButton::KeyInput( rKEvt );
228 void MenuButton::Activate()
230 maActivateHdl.Call( this );
233 void MenuButton::Select()
235 if (mnCurItemId)
236 collectUIInformation(get_id(),"OPENFROMLIST","POS",OUString::number(mnCurItemId));
238 maSelectHdl.Call( this );
241 void MenuButton::SetPopupMenu(PopupMenu* pNewMenu)
243 if (pNewMenu == mpMenu)
244 return;
246 mpMenu = pNewMenu;
249 void MenuButton::SetPopover(Window* pWindow)
251 if (pWindow == mpFloatingWindow)
252 return;
254 mpFloatingWindow = pWindow;
258 FactoryFunction MenuButton::GetUITestFactory() const
260 return MenuButtonUIObject::create;
263 void MenuButton::SetCurItemId(){
264 mnCurItemId = mpMenu->GetCurItemId();
265 msCurItemIdent = mpMenu->GetCurItemIdent();
268 void MenuButton::DumpAsPropertyTree(tools::JsonWriter& rJsonWriter)
270 PushButton::DumpAsPropertyTree(rJsonWriter);
272 if (mpMenu)
274 auto aMenuNode = rJsonWriter.startArray("menu");
275 for (int i = 0; i < mpMenu->GetItemCount(); i++)
277 auto aEntryNode = rJsonWriter.startStruct();
278 auto sId = mpMenu->GetItemId(i);
279 rJsonWriter.put("id", mpMenu->GetItemIdent(sId));
280 rJsonWriter.put("text", mpMenu->GetItemText(sId));
285 //class MenuToggleButton ----------------------------------------------------
287 MenuToggleButton::MenuToggleButton( vcl::Window* pParent, WinBits nWinBits )
288 : MenuButton( pParent, nWinBits )
292 MenuToggleButton::~MenuToggleButton()
294 disposeOnce();
297 void MenuToggleButton::SetActive( bool bSel )
299 mbIsActive = bSel;
302 bool MenuToggleButton::GetActive() const
304 return mbIsActive;
307 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */