author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Wed, 18 Aug 2010 10:37:55 +0300 | |
changeset 33 | 3e2da88830cd |
parent 30 | 5dc02b23752f |
child 37 | 758a864f9613 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the QtGui module of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#include "qmacdefines_mac.h" |
|
43 |
#include "qapplication.h" |
|
44 |
#ifdef QT_MAC_USE_COCOA |
|
45 |
#import <private/qcocoamenu_mac_p.h> |
|
46 |
#import <private/qcocoamenuloader_mac_p.h> |
|
47 |
#include <private/qt_cocoa_helpers_mac_p.h> |
|
48 |
#include <private/qapplication_p.h> |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
49 |
#include <private/qaction_p.h> |
0 | 50 |
|
51 |
#include <QtGui/QMenu> |
|
52 |
||
53 |
QT_FORWARD_DECLARE_CLASS(QAction) |
|
54 |
QT_FORWARD_DECLARE_CLASS(QWidget) |
|
55 |
QT_FORWARD_DECLARE_CLASS(QApplication) |
|
56 |
QT_FORWARD_DECLARE_CLASS(QCoreApplication) |
|
57 |
QT_FORWARD_DECLARE_CLASS(QApplicationPrivate) |
|
58 |
QT_FORWARD_DECLARE_CLASS(QKeyEvent) |
|
59 |
QT_FORWARD_DECLARE_CLASS(QEvent) |
|
60 |
||
61 |
QT_BEGIN_NAMESPACE |
|
62 |
extern bool qt_sendSpontaneousEvent(QObject*, QEvent*); //qapplication.cpp |
|
63 |
QT_END_NAMESPACE |
|
64 |
||
65 |
QT_USE_NAMESPACE |
|
66 |
||
67 |
@implementation QT_MANGLE_NAMESPACE(QCocoaMenu) |
|
68 |
||
69 |
- (id)initWithQMenu:(QMenu*)menu |
|
70 |
{ |
|
71 |
self = [super init]; |
|
72 |
if (self) { |
|
73 |
qmenu = menu; |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
74 |
previousAction = 0; |
0 | 75 |
[self setAutoenablesItems:NO]; |
76 |
[self setDelegate:self]; |
|
77 |
} |
|
78 |
return self; |
|
79 |
} |
|
80 |
||
81 |
- (void)menu:(NSMenu*)menu willHighlightItem:(NSMenuItem*)item; |
|
82 |
{ |
|
83 |
Q_UNUSED(menu); |
|
84 |
||
85 |
if (!item) { |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
86 |
if (previousAction) { |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
87 |
qt_mac_clear_status_text(previousAction); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
88 |
previousAction = 0; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
89 |
} |
0 | 90 |
return; |
91 |
} |
|
92 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
93 |
if (QAction *action = reinterpret_cast<QAction *>([item tag])) { |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
94 |
QMenu *qtmenu = static_cast<QT_MANGLE_NAMESPACE(QCocoaMenu) *>(menu)->qmenu; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
95 |
previousAction = action; |
0 | 96 |
action->activate(QAction::Hover); |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
97 |
qt_mac_menu_emit_hovered(qtmenu, action); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
98 |
action->showStatusText(0); // 0 widget -> action's parent |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
99 |
} |
0 | 100 |
} |
101 |
||
102 |
- (void)menuWillOpen:(NSMenu*)menu; |
|
103 |
{ |
|
104 |
while (QWidget *popup |
|
105 |
= QApplication::activePopupWidget()) |
|
106 |
popup->close(); |
|
107 |
QMenu *qtmenu = static_cast<QT_MANGLE_NAMESPACE(QCocoaMenu) *>(menu)->qmenu; |
|
108 |
qt_mac_emit_menuSignals(qtmenu, true); |
|
109 |
qt_mac_menu_collapseSeparators(menu, qtmenu->separatorsCollapsible()); |
|
110 |
} |
|
111 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
112 |
- (void)menuDidClose:(NSMenu*)menu; |
0 | 113 |
{ |
114 |
qt_mac_emit_menuSignals(((QT_MANGLE_NAMESPACE(QCocoaMenu) *)menu)->qmenu, false); |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
115 |
if (previousAction) { |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
116 |
qt_mac_clear_status_text(previousAction); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
117 |
previousAction = 0; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
118 |
} |
0 | 119 |
} |
120 |
||
121 |
- (BOOL)hasShortcut:(NSMenu *)menu forKey:(NSString *)key forModifiers:(NSUInteger)modifier |
|
122 |
whichItem:(NSMenuItem**)outItem |
|
123 |
{ |
|
124 |
for (NSMenuItem *item in [menu itemArray]) { |
|
125 |
if (![item isEnabled] || [item isHidden] || [item isSeparatorItem]) |
|
126 |
continue; |
|
127 |
if ([item hasSubmenu]) { |
|
128 |
if ([self hasShortcut:[item submenu] |
|
129 |
forKey:key |
|
130 |
forModifiers:modifier whichItem:outItem]) { |
|
131 |
if (outItem) |
|
132 |
*outItem = item; |
|
133 |
return YES; |
|
134 |
} |
|
135 |
} |
|
136 |
NSString *menuKey = [item keyEquivalent]; |
|
137 |
if (menuKey && NSOrderedSame == [menuKey compare:key] |
|
138 |
&& (modifier == [item keyEquivalentModifierMask])) { |
|
139 |
if (outItem) |
|
140 |
*outItem = item; |
|
141 |
return YES; |
|
142 |
} |
|
143 |
} |
|
144 |
if (outItem) |
|
145 |
*outItem = 0; |
|
146 |
return NO; |
|
147 |
} |
|
148 |
||
149 |
- (BOOL)menuHasKeyEquivalent:(NSMenu *)menu forEvent:(NSEvent *)event target:(id *)target action:(SEL *)action |
|
150 |
{ |
|
151 |
// Check if the menu actually has a keysequence defined for this key event. |
|
152 |
// If it does, then we will first send the key sequence to the QWidget that has focus |
|
153 |
// since (in Qt's eyes) it needs to a chance at the key event first. If the widget |
|
154 |
// accepts the key event, we then return YES, but set the target and action to be nil, |
|
155 |
// which means that the action should not be triggered, and instead dispatch the event ourselves. |
|
156 |
// In every other case we return NO, which means that Cocoa can do as it pleases |
|
157 |
// (i.e., fire the menu action). |
|
158 |
NSMenuItem *whichItem; |
|
159 |
if ([self hasShortcut:menu |
|
160 |
forKey:[event characters] |
|
161 |
forModifiers:([event modifierFlags] & NSDeviceIndependentModifierFlagsMask) |
|
162 |
whichItem:&whichItem]) { |
|
163 |
QWidget *widget = 0; |
|
164 |
QAction *qaction = 0; |
|
165 |
if (whichItem && [whichItem tag]) { |
|
166 |
qaction = reinterpret_cast<QAction *>([whichItem tag]); |
|
167 |
} |
|
168 |
if (qApp->activePopupWidget()) |
|
169 |
widget = (qApp->activePopupWidget()->focusWidget() ? |
|
170 |
qApp->activePopupWidget()->focusWidget() : qApp->activePopupWidget()); |
|
171 |
else if (QApplicationPrivate::focus_widget) |
|
172 |
widget = QApplicationPrivate::focus_widget; |
|
173 |
if (qaction && widget) { |
|
174 |
int key = qaction->shortcut(); |
|
175 |
QKeyEvent accel_ev(QEvent::ShortcutOverride, (key & (~Qt::KeyboardModifierMask)), |
|
176 |
Qt::KeyboardModifiers(key & Qt::KeyboardModifierMask)); |
|
177 |
accel_ev.ignore(); |
|
178 |
qt_sendSpontaneousEvent(widget, &accel_ev); |
|
179 |
if (accel_ev.isAccepted()) { |
|
180 |
if (qt_dispatchKeyEvent(event, widget)) { |
|
181 |
*target = nil; |
|
182 |
*action = nil; |
|
183 |
return YES; |
|
184 |
} |
|
185 |
} |
|
186 |
} |
|
187 |
} |
|
188 |
return NO; |
|
189 |
} |
|
190 |
||
191 |
@end |
|
192 |
||
193 |
QT_BEGIN_NAMESPACE |
|
194 |
extern int qt_mac_menus_open_count; // qmenu_mac.mm |
|
195 |
||
196 |
void qt_mac_emit_menuSignals(QMenu *menu, bool show) |
|
197 |
{ |
|
198 |
if (!menu) |
|
199 |
return; |
|
200 |
int delta; |
|
201 |
if (show) { |
|
202 |
emit menu->aboutToShow(); |
|
203 |
delta = 1; |
|
204 |
} else { |
|
205 |
emit menu->aboutToHide(); |
|
206 |
delta = -1; |
|
207 |
} |
|
208 |
qt_mac_menus_open_count += delta; |
|
209 |
} |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
210 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
211 |
void qt_mac_clear_status_text(QAction *action) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
212 |
{ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
213 |
action->d_func()->showStatusText(0, QString()); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
214 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
215 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
216 |
void qt_mac_menu_emit_hovered(QMenu *menu, QAction *action) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
217 |
{ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
218 |
emit menu->hovered(action); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
219 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
220 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
221 |
|
0 | 222 |
QT_END_NAMESPACE |
223 |
||
224 |
#endif |