Qt 4.8
qcocoamenu_mac.mm
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
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 #import <private/qcocoaapplication_mac_p.h>
48 #include <private/qt_cocoa_helpers_mac_p.h>
49 #include <private/qapplication_p.h>
50 #include <private/qaction_p.h>
51 #include <private/qcocoaapplication_mac_p.h>
52 
53 #include <QtGui/QMenu>
54 
62 
64 extern bool qt_sendSpontaneousEvent(QObject*, QEvent*); //qapplication.cpp
65 extern NSString *qt_mac_removePrivateUnicode(NSString* string);
67 
69 
70 @implementation QT_MANGLE_NAMESPACE(QCocoaMenu)
71 
72 - (id)initWithQMenu:(QMenu*)menu
73 {
74  self = [super init];
75  if (self) {
76  qmenu = menu;
77  previousAction = 0;
78  [self setAutoenablesItems:NO];
79  [self setDelegate:self];
80  }
81  return self;
82 }
83 
84 - (void)menu:(NSMenu*)menu willHighlightItem:(NSMenuItem*)item
85 {
86  Q_UNUSED(menu);
87 
88  if (!item) {
89  if (previousAction) {
90  qt_mac_clear_status_text(previousAction);
91  previousAction = 0;
92  }
93  return;
94  }
95 
96  if (QAction *action = reinterpret_cast<QAction *>([item tag])) {
97  QMenu *qtmenu = static_cast<QT_MANGLE_NAMESPACE(QCocoaMenu) *>(menu)->qmenu;
98  previousAction = action;
99  action->activate(QAction::Hover);
100  qt_mac_menu_emit_hovered(qtmenu, action);
101  action->showStatusText(0); // 0 widget -> action's parent
102  }
103 }
104 
105 - (void)menuWillOpen:(NSMenu*)menu
106 {
107  while (QWidget *popup
109  popup->close();
110  QMenu *qtmenu = static_cast<QT_MANGLE_NAMESPACE(QCocoaMenu) *>(menu)->qmenu;
111  qt_mac_emit_menuSignals(qtmenu, true);
112  qt_mac_menu_collapseSeparators(menu, qtmenu->separatorsCollapsible());
113 }
114 
115 - (void)menuDidClose:(NSMenu*)menu
116 {
117  qt_mac_emit_menuSignals(((QT_MANGLE_NAMESPACE(QCocoaMenu) *)menu)->qmenu, false);
118  if (previousAction) {
119  qt_mac_clear_status_text(previousAction);
120  previousAction = 0;
121  }
122 }
123 
124 - (BOOL)hasShortcut:(NSMenu *)menu forKey:(NSString *)key forModifiers:(NSUInteger)modifier
125  whichItem:(NSMenuItem**)outItem
126 {
127  for (NSMenuItem *item in [menu itemArray]) {
128  if (![item isEnabled] || [item isHidden] || [item isSeparatorItem])
129  continue;
130  if ([item hasSubmenu]) {
131  if ([self hasShortcut:[item submenu]
132  forKey:key
133  forModifiers:modifier whichItem:outItem]) {
134  if (outItem)
135  *outItem = item;
136  return YES;
137  }
138  }
139  NSString *menuKey = [item keyEquivalent];
140  if (menuKey && NSOrderedSame == [menuKey compare:key]
141  && (modifier == [item keyEquivalentModifierMask])) {
142  if (outItem)
143  *outItem = item;
144  return YES;
145  }
146  }
147  if (outItem)
148  *outItem = 0;
149  return NO;
150 }
151 
152 - (BOOL)menuHasKeyEquivalent:(NSMenu *)menu forEvent:(NSEvent *)event target:(id *)target action:(SEL *)action
153 {
154  // Check if the menu actually has a keysequence defined for this key event.
155  // If it does, then we will first send the key sequence to the QWidget that has focus
156  // since (in Qt's eyes) it needs to a chance at the key event first. If the widget
157  // accepts the key event, we then return YES, but set the target and action to be nil,
158  // which means that the action should not be triggered, and instead dispatch the event ourselves.
159  // In every other case we return NO, which means that Cocoa can do as it pleases
160  // (i.e., fire the menu action).
161  NSMenuItem *whichItem;
162  // Change the private unicode keys to the ones used in setting the "Key Equivalents"
163  NSString *characters = qt_mac_removePrivateUnicode([event characters]);
164  if ([self hasShortcut:menu
165  forKey:characters
166  // Interested only in Shift, Cmd, Ctrl & Alt Keys, so ignoring masks like, Caps lock, Num Lock ...
167  forModifiers:([event modifierFlags] & (NSShiftKeyMask | NSControlKeyMask | NSCommandKeyMask | NSAlternateKeyMask))
168  whichItem:&whichItem]) {
169  QWidget *widget = 0;
170  QAction *qaction = 0;
171  if (whichItem && [whichItem tag]) {
172  qaction = reinterpret_cast<QAction *>([whichItem tag]);
173  }
174  if (qApp->activePopupWidget())
175  widget = (qApp->activePopupWidget()->focusWidget() ?
176  qApp->activePopupWidget()->focusWidget() : qApp->activePopupWidget());
179  // If we could not find any receivers, pass it to the active window
180  if (!widget)
181  widget = qApp->activeWindow();
182  if (qaction && widget) {
183  int key = qaction->shortcut();
185  Qt::KeyboardModifiers(key & Qt::KeyboardModifierMask));
186  accel_ev.ignore();
187  qt_sendSpontaneousEvent(widget, &accel_ev);
188  if (accel_ev.isAccepted()) {
189  qt_dispatchKeyEvent(event, widget);
190  *target = nil;
191  *action = nil;
192  return YES;
193  }
194  }
195  }
196  return NO;
197 }
198 
199 - (NSInteger)indexOfItemWithTarget:(id)anObject andAction:(SEL)actionSelector
200 {
201  NSInteger index = [super indexOfItemWithTarget:anObject andAction:actionSelector];
202  static SEL selForOFCP = NSSelectorFromString(@"orderFrontCharacterPalette:");
203  if (index == -1 && selForOFCP == actionSelector) {
204  // Check if the 'orderFrontCharacterPalette' SEL exists for QCocoaMenuLoader object
205  QT_MANGLE_NAMESPACE(QCocoaMenuLoader) *loader = [NSApp QT_MANGLE_NAMESPACE(qt_qcocoamenuLoader)];
206  return [super indexOfItemWithTarget:loader andAction:actionSelector];
207  }
208  return index;
209 }
210 
211 @end
212 
214 extern int qt_mac_menus_open_count; // qmenu_mac.mm
215 
216 void qt_mac_emit_menuSignals(QMenu *menu, bool show)
217 {
218  if (!menu)
219  return;
220  int delta;
221  if (show) {
222  emit menu->aboutToShow();
223  delta = 1;
224  } else {
225  emit menu->aboutToHide();
226  delta = -1;
227  }
228  qt_mac_menus_open_count += delta;
229 }
230 
231 void qt_mac_clear_status_text(QAction *action)
232 {
233  action->d_func()->showStatusText(0, QString());
234 }
235 
236 void qt_mac_menu_emit_hovered(QMenu *menu, QAction *action)
237 {
238  emit menu->hovered(action);
239 }
240 
241 
243 
244 #endif
The QApplication class manages the GUI application&#39;s control flow and main settings.
Definition: qapplication.h:99
The QKeyEvent class describes a key event.
Definition: qevent.h:224
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
bool separatorsCollapsible
whether consecutive separators should be collapsed
Definition: qmenu.h:81
QPointer< QWidget > widget
QKeySequence shortcut
the action&#39;s primary shortcut key
Definition: qaction.h:83
bool showStatusText(QWidget *widget=0)
Updates the relevant status bar for the widget specified by sending a QStatusTipEvent to its parent w...
Definition: qaction.cpp:1315
void hovered(QAction *action)
This signal is emitted when a menu action is highlighted; action is the action that caused the signal...
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
void aboutToHide()
This signal is emitted just before the menu is hidden from the user.
static QWidget * focus_widget
void NSMenuItem
Definition: qmenu_p.h:80
#define QT_FORWARD_DECLARE_CLASS(name)
Definition: qglobal.h:95
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
static bool compare(const QVariant::Private *a, const QVariant::Private *b)
Compares a to b.
Definition: qvariant.cpp:383
#define qApp
#define emit
Definition: qobjectdefs.h:76
bool qt_dispatchKeyEvent(void *keyEvent, QWidget *widgetToGetEvent)
The QCoreApplication class provides an event loop for console Qt applications.
void aboutToShow()
This signal is emitted just before the menu is shown to the user.
static QWidget * activePopupWidget()
Returns the active popup widget.
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus...
Definition: qmenu.h:72
#define QT_MANGLE_NAMESPACE(name)
Definition: qglobal.h:106
bool qt_sendSpontaneousEvent(QObject *, QEvent *)
#define QT_USE_NAMESPACE
This macro expands to using QT_NAMESPACE if QT_NAMESPACE is defined and nothing otherwise.
Definition: qglobal.h:88
quint16 index
The QEvent class is the base class of all event classes.
Definition: qcoreevent.h:56
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
The QAction class provides an abstract user interface action that can be inserted into widgets...
Definition: qaction.h:64
int qt_mac_menus_open_count
Definition: qmenu_mac.mm:76