Line data Source code
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 <tools/rc.h>
21 : #include <vcl/decoview.hxx>
22 : #include <vcl/event.hxx>
23 : #include <vcl/menu.hxx>
24 : #include <vcl/timer.hxx>
25 : #include <vcl/menubtn.hxx>
26 : #include <vcl/svapp.hxx>
27 : #include <vcl/settings.hxx>
28 :
29 353 : void MenuButton::ImplInitMenuButtonData()
30 : {
31 353 : mnDDStyle = PushButtonDropdownStyle::MenuButton;
32 :
33 353 : mpMenuTimer = NULL;
34 353 : mpMenu = NULL;
35 353 : mpOwnMenu = NULL;
36 353 : mnCurItemId = 0;
37 353 : mnMenuMode = 0;
38 353 : }
39 :
40 353 : void MenuButton::ImplInit( vcl::Window* pParent, WinBits nStyle )
41 : {
42 353 : if ( !(nStyle & WB_NOTABSTOP) )
43 353 : nStyle |= WB_TABSTOP;
44 :
45 353 : PushButton::ImplInit( pParent, nStyle );
46 353 : EnableRTL( AllSettings::GetLayoutRTL() );
47 353 : }
48 :
49 0 : void MenuButton::ExecuteMenu()
50 : {
51 0 : Activate();
52 :
53 0 : if ( mpMenu )
54 : {
55 0 : Point aPos( 0, 1 );
56 0 : Size aSize = GetSizePixel();
57 0 : Rectangle aRect( aPos, aSize );
58 0 : SetPressed( true );
59 0 : EndSelection();
60 0 : mnCurItemId = mpMenu->Execute( this, aRect, PopupMenuFlags::ExecuteDown );
61 0 : SetPressed( false );
62 0 : if ( mnCurItemId )
63 : {
64 0 : Select();
65 0 : mnCurItemId = 0;
66 : }
67 : }
68 0 : }
69 :
70 0 : OString MenuButton::GetCurItemIdent() const
71 : {
72 0 : return (mnCurItemId && mpMenu) ?
73 0 : mpMenu->GetItemIdent(mnCurItemId) : OString();
74 : }
75 :
76 353 : MenuButton::MenuButton( vcl::Window* pParent, WinBits nWinBits )
77 353 : : PushButton( WINDOW_MENUBUTTON )
78 : {
79 353 : ImplInitMenuButtonData();
80 353 : ImplInit( pParent, nWinBits );
81 353 : }
82 :
83 708 : MenuButton::~MenuButton()
84 : {
85 353 : disposeOnce();
86 355 : }
87 :
88 353 : void MenuButton::dispose()
89 : {
90 353 : delete mpMenuTimer;
91 353 : delete mpOwnMenu;
92 353 : PushButton::dispose();
93 353 : }
94 :
95 0 : IMPL_LINK_NOARG_TYPED(MenuButton, ImplMenuTimeoutHdl, Timer *, void)
96 : {
97 : // See if Button Tracking is still active, as it could've been cancelled earler
98 0 : if ( IsTracking() )
99 : {
100 0 : if ( !(GetStyle() & WB_NOPOINTERFOCUS) )
101 0 : GrabFocus();
102 0 : ExecuteMenu();
103 : }
104 0 : }
105 :
106 0 : void MenuButton::MouseButtonDown( const MouseEvent& rMEvt )
107 : {
108 0 : bool bExecute = true;
109 0 : if ( mnMenuMode & MENUBUTTON_MENUMODE_TIMED )
110 : {
111 : // If the separated dropdown symbol is not hit, delay the popup execution
112 0 : if( mnDDStyle != PushButtonDropdownStyle::MenuButton || // no separator at all
113 0 : rMEvt.GetPosPixel().X() <= ImplGetSeparatorX() )
114 : {
115 0 : if ( !mpMenuTimer )
116 : {
117 0 : mpMenuTimer = new Timer;
118 0 : mpMenuTimer->SetTimeoutHdl( LINK( this, MenuButton, ImplMenuTimeoutHdl ) );
119 : }
120 :
121 0 : mpMenuTimer->SetTimeout( GetSettings().GetMouseSettings().GetActionDelay() );
122 0 : mpMenuTimer->Start();
123 :
124 0 : PushButton::MouseButtonDown( rMEvt );
125 0 : bExecute = false;
126 : }
127 : }
128 0 : if( bExecute )
129 : {
130 0 : if ( PushButton::ImplHitTestPushButton( this, rMEvt.GetPosPixel() ) )
131 : {
132 0 : if ( !(GetStyle() & WB_NOPOINTERFOCUS) )
133 0 : GrabFocus();
134 0 : ExecuteMenu();
135 : }
136 : }
137 0 : }
138 :
139 0 : void MenuButton::KeyInput( const KeyEvent& rKEvt )
140 : {
141 0 : vcl::KeyCode aKeyCode = rKEvt.GetKeyCode();
142 0 : sal_uInt16 nCode = aKeyCode.GetCode();
143 0 : if ( (nCode == KEY_DOWN) && aKeyCode.IsMod2() )
144 0 : ExecuteMenu();
145 0 : else if ( !(mnMenuMode & MENUBUTTON_MENUMODE_TIMED) &&
146 0 : !aKeyCode.GetModifier() &&
147 0 : ((nCode == KEY_RETURN) || (nCode == KEY_SPACE)) )
148 0 : ExecuteMenu();
149 : else
150 0 : PushButton::KeyInput( rKEvt );
151 0 : }
152 :
153 0 : void MenuButton::Activate()
154 : {
155 0 : maActivateHdl.Call( this );
156 0 : }
157 :
158 0 : void MenuButton::Select()
159 : {
160 0 : maSelectHdl.Call( this );
161 0 : }
162 :
163 1 : void MenuButton::SetMenuMode( sal_uInt16 nMode )
164 : {
165 : // FIXME: It's better to not inline this for 5.1; in 6.0 we can make it inline, however
166 1 : mnMenuMode = nMode;
167 1 : }
168 :
169 353 : void MenuButton::SetPopupMenu( PopupMenu* pNewMenu )
170 : {
171 353 : if (pNewMenu == mpMenu)
172 353 : return;
173 :
174 353 : mpMenu = pNewMenu;
175 : }
176 :
177 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|