1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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
) )
52 PushButton::ImplInit( pParent
, nStyle
);
53 EnableRTL( AllSettings::GetLayoutRTL() );
56 void MenuButton::ExecuteMenu()
58 mbStartingMenu
= true;
62 if (!mpMenu
&& !mpFloatingWindow
)
64 mbStartingMenu
= false;
68 Size aSize
= GetSizePixel();
74 tools::Rectangle
aRect(aPos
, aSize
);
75 mpMenu
->Execute(this, aRect
, PopupMenuFlags::ExecuteDown
);
80 mnCurItemId
= mpMenu
->GetCurItemId();
81 msCurItemIdent
= mpMenu
->GetCurItemIdent();
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
);
92 mpFloatingWindow
->EnableDocking();
93 vcl::Window::GetDockingManager()->StartPopupMode(mpFloatingWindow
, aRect
, nFlags
);
97 mbStartingMenu
= false;
100 OUString aID
= get_id(); // tdf#136678 take a copy if we are destroyed by Select callback
105 msCurItemIdent
.clear();
107 collectUIInformation(aID
,"OPENLIST","","");
110 void MenuButton::CancelMenu()
112 if (!mpMenu
&& !mpFloatingWindow
)
117 mpMenu
->EndExecute();
121 if (mpFloatingWindow
->GetType() == WindowType::FLOATINGWINDOW
)
122 static_cast<FloatingWindow
*>(mpFloatingWindow
.get())->EndPopupMode();
124 vcl::Window::GetDockingManager()->EndPopupMode(mpFloatingWindow
);
126 collectUIInformation(get_id(),"CLOSELIST","","");
129 bool MenuButton::InPopupMode() const
134 if (!mpMenu
&& !mpFloatingWindow
)
138 return PopupMenu::GetActivePopupMenu() == mpMenu
;
141 if (mpFloatingWindow
->GetType() == WindowType::FLOATINGWINDOW
)
142 return static_cast<const FloatingWindow
*>(mpFloatingWindow
.get())->IsInPopupMode();
144 return vcl::Window::GetDockingManager()->IsInPopupMode(mpFloatingWindow
);
148 MenuButton::MenuButton( vcl::Window
* pParent
, WinBits nWinBits
)
149 : PushButton(WindowType::MENUBUTTON
)
152 , mbStartingMenu(false)
154 mnDDStyle
= PushButtonDropdownStyle::MenuButton
;
155 ImplInit(pParent
, nWinBits
);
158 MenuButton::~MenuButton()
163 void MenuButton::dispose()
166 mpFloatingWindow
.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
176 if ( !(GetStyle() & WB_NOPOINTERFOCUS
) )
182 void MenuButton::MouseButtonDown( const MouseEvent
& rMEvt
)
184 bool bExecute
= true;
187 // If the separated dropdown symbol is not hit, delay the popup execution
188 if( rMEvt
.GetPosPixel().X() <= ImplGetSeparatorX() )
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
);
205 if ( PushButton::ImplHitTestPushButton( this, rMEvt
.GetPosPixel() ) )
207 if ( !(GetStyle() & WB_NOPOINTERFOCUS
) )
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() )
220 else if ( !mbDelayMenu
&&
221 !aKeyCode
.GetModifier() &&
222 ((nCode
== KEY_RETURN
) || (nCode
== KEY_SPACE
)) )
225 PushButton::KeyInput( rKEvt
);
228 void MenuButton::Activate()
230 maActivateHdl
.Call( this );
233 void MenuButton::Select()
236 collectUIInformation(get_id(),"OPENFROMLIST","POS",OUString::number(mnCurItemId
));
238 maSelectHdl
.Call( this );
241 void MenuButton::SetPopupMenu(PopupMenu
* pNewMenu
)
243 if (pNewMenu
== mpMenu
)
249 void MenuButton::SetPopover(Window
* pWindow
)
251 if (pWindow
== mpFloatingWindow
)
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
);
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()
297 void MenuToggleButton::SetActive( bool bSel
)
302 bool MenuToggleButton::GetActive() const
307 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */