author | Eckhart Koeppen <eckhart.koppen@nokia.com> |
Wed, 21 Apr 2010 20:15:53 +0300 | |
branch | RCL_3 |
changeset 14 | c0432d11811c |
parent 5 | d3bac044e0f0 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
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 "qmenu.h" |
|
43 |
#include "qhash.h" |
|
44 |
#include <qdebug.h> |
|
45 |
#include "qapplication.h" |
|
46 |
#include <private/qt_mac_p.h> |
|
47 |
#include "qregexp.h" |
|
48 |
#include "qmainwindow.h" |
|
49 |
#include "qdockwidget.h" |
|
50 |
#include "qtoolbar.h" |
|
51 |
#include "qevent.h" |
|
52 |
#include "qstyle.h" |
|
53 |
#include "qwidgetaction.h" |
|
54 |
#include "qmacnativewidget_mac.h" |
|
55 |
||
56 |
#include <private/qapplication_p.h> |
|
57 |
#include <private/qcocoaapplication_mac_p.h> |
|
58 |
#include <private/qmenu_p.h> |
|
59 |
#include <private/qmenubar_p.h> |
|
60 |
#include <private/qcocoamenuloader_mac_p.h> |
|
61 |
#include <private/qcocoamenu_mac_p.h> |
|
62 |
#include <private/qt_cocoa_helpers_mac_p.h> |
|
63 |
#include <Cocoa/Cocoa.h> |
|
64 |
||
65 |
QT_BEGIN_NAMESPACE |
|
66 |
||
67 |
/***************************************************************************** |
|
68 |
QMenu debug facilities |
|
69 |
*****************************************************************************/ |
|
70 |
||
71 |
/***************************************************************************** |
|
72 |
QMenu globals |
|
73 |
*****************************************************************************/ |
|
74 |
bool qt_mac_no_menubar_merge = false; |
|
75 |
bool qt_mac_quit_menu_item_enabled = true; |
|
76 |
int qt_mac_menus_open_count = 0; |
|
77 |
||
78 |
static OSMenuRef qt_mac_create_menu(QWidget *w); |
|
79 |
||
80 |
#ifndef QT_MAC_USE_COCOA |
|
81 |
static uint qt_mac_menu_static_cmd_id = 'QT00'; |
|
82 |
const UInt32 kMenuCreatorQt = 'cute'; |
|
83 |
enum { |
|
84 |
kMenuPropertyQAction = 'QAcT', |
|
85 |
kMenuPropertyQWidget = 'QWId', |
|
86 |
kMenuPropertyCausedQWidget = 'QCAU', |
|
87 |
kMenuPropertyMergeMenu = 'QApP', |
|
88 |
kMenuPropertyMergeList = 'QAmL', |
|
89 |
kMenuPropertyWidgetActionWidget = 'QWid', |
|
90 |
kMenuPropertyWidgetMenu = 'QWMe', |
|
91 |
||
92 |
kHICommandAboutQt = 'AOQT', |
|
93 |
kHICommandCustomMerge = 'AQt0' |
|
94 |
}; |
|
95 |
#endif |
|
96 |
||
97 |
static struct { |
|
98 |
QPointer<QMenuBar> qmenubar; |
|
99 |
bool modal; |
|
100 |
} qt_mac_current_menubar = { 0, false }; |
|
101 |
||
102 |
||
103 |
||
104 |
||
105 |
/***************************************************************************** |
|
106 |
Externals |
|
107 |
*****************************************************************************/ |
|
108 |
extern OSViewRef qt_mac_hiview_for(const QWidget *w); //qwidget_mac.cpp |
|
109 |
extern HIViewRef qt_mac_hiview_for(OSWindowRef w); //qwidget_mac.cpp |
|
110 |
extern IconRef qt_mac_create_iconref(const QPixmap &px); //qpixmap_mac.cpp |
|
111 |
extern QWidget * mac_keyboard_grabber; //qwidget_mac.cpp |
|
112 |
extern bool qt_sendSpontaneousEvent(QObject*, QEvent*); //qapplication_xxx.cpp |
|
113 |
RgnHandle qt_mac_get_rgn(); //qregion_mac.cpp |
|
114 |
void qt_mac_dispose_rgn(RgnHandle r); //qregion_mac.cpp |
|
115 |
||
116 |
/***************************************************************************** |
|
117 |
QMenu utility functions |
|
118 |
*****************************************************************************/ |
|
119 |
bool qt_mac_watchingAboutToShow(QMenu *menu) |
|
120 |
{ |
|
121 |
return menu && menu->receivers(SIGNAL(aboutToShow())); |
|
122 |
} |
|
123 |
||
124 |
static int qt_mac_CountMenuItems(OSMenuRef menu) |
|
125 |
{ |
|
126 |
if (menu) { |
|
127 |
#ifndef QT_MAC_USE_COCOA |
|
128 |
int ret = 0; |
|
129 |
const int items = CountMenuItems(menu); |
|
130 |
for(int i = 0; i < items; i++) { |
|
131 |
MenuItemAttributes attr; |
|
132 |
if (GetMenuItemAttributes(menu, i+1, &attr) == noErr && |
|
133 |
attr & kMenuItemAttrHidden) |
|
134 |
continue; |
|
135 |
++ret; |
|
136 |
} |
|
137 |
return ret; |
|
138 |
#else |
|
139 |
return [menu numberOfItems]; |
|
140 |
#endif |
|
141 |
} |
|
142 |
return 0; |
|
143 |
} |
|
144 |
||
145 |
static quint32 constructModifierMask(quint32 accel_key) |
|
146 |
{ |
|
147 |
quint32 ret = 0; |
|
148 |
const bool dontSwap = qApp->testAttribute(Qt::AA_MacDontSwapCtrlAndMeta); |
|
149 |
#ifndef QT_MAC_USE_COCOA |
|
150 |
if ((accel_key & Qt::ALT) == Qt::ALT) |
|
151 |
ret |= kMenuOptionModifier; |
|
152 |
if ((accel_key & Qt::SHIFT) == Qt::SHIFT) |
|
153 |
ret |= kMenuShiftModifier; |
|
154 |
if (dontSwap) { |
|
155 |
if ((accel_key & Qt::META) != Qt::META) |
|
156 |
ret |= kMenuNoCommandModifier; |
|
157 |
if ((accel_key & Qt::CTRL) == Qt::CTRL) |
|
158 |
ret |= kMenuControlModifier; |
|
159 |
} else { |
|
160 |
if ((accel_key & Qt::CTRL) != Qt::CTRL) |
|
161 |
ret |= kMenuNoCommandModifier; |
|
162 |
if ((accel_key & Qt::META) == Qt::META) |
|
163 |
ret |= kMenuControlModifier; |
|
164 |
} |
|
165 |
#else |
|
166 |
if ((accel_key & Qt::CTRL) == Qt::CTRL) |
|
167 |
ret |= (dontSwap ? NSControlKeyMask : NSCommandKeyMask); |
|
168 |
if ((accel_key & Qt::META) == Qt::META) |
|
169 |
ret |= (dontSwap ? NSCommandKeyMask : NSControlKeyMask); |
|
170 |
if ((accel_key & Qt::ALT) == Qt::ALT) |
|
171 |
ret |= NSAlternateKeyMask; |
|
172 |
if ((accel_key & Qt::SHIFT) == Qt::SHIFT) |
|
173 |
ret |= NSShiftKeyMask; |
|
174 |
#endif |
|
175 |
return ret; |
|
176 |
} |
|
177 |
||
5
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
178 |
static void cancelAllMenuTracking() |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
179 |
{ |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
180 |
#ifdef QT_MAC_USE_COCOA |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
181 |
QMacCocoaAutoReleasePool pool; |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
182 |
NSMenu *mainMenu = [NSApp mainMenu]; |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
183 |
[mainMenu cancelTracking]; |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
184 |
for (NSMenuItem *item in [mainMenu itemArray]) { |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
185 |
if ([item submenu]) { |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
186 |
[[item submenu] cancelTracking]; |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
187 |
} |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
188 |
} |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
189 |
#else |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
190 |
CancelMenuTracking(AcquireRootMenu(), true, 0); |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
191 |
#endif |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
192 |
} |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
193 |
|
0 | 194 |
static bool actualMenuItemVisibility(const QMenuBarPrivate::QMacMenuBarPrivate *mbp, |
195 |
const QMacMenuAction *action) |
|
196 |
{ |
|
197 |
bool visible = action->action->isVisible(); |
|
198 |
if (visible && action->action->text() == QString(QChar(0x14))) |
|
199 |
return false; |
|
200 |
if (visible && action->action->menu() && !action->action->menu()->actions().isEmpty() && |
|
201 |
!qt_mac_CountMenuItems(action->action->menu()->macMenu(mbp->apple_menu)) && |
|
202 |
!qt_mac_watchingAboutToShow(action->action->menu())) { |
|
203 |
return false; |
|
204 |
} |
|
205 |
return visible; |
|
206 |
} |
|
207 |
||
208 |
#ifndef QT_MAC_USE_COCOA |
|
209 |
bool qt_mac_activate_action(MenuRef menu, uint command, QAction::ActionEvent action_e, bool by_accel) |
|
210 |
{ |
|
211 |
//fire event |
|
212 |
QMacMenuAction *action = 0; |
|
213 |
if (GetMenuCommandProperty(menu, command, kMenuCreatorQt, kMenuPropertyQAction, sizeof(action), 0, &action) != noErr) { |
|
214 |
QMenuMergeList *list = 0; |
|
215 |
GetMenuItemProperty(menu, 0, kMenuCreatorQt, kMenuPropertyMergeList, |
|
216 |
sizeof(list), 0, &list); |
|
217 |
if (!list && qt_mac_current_menubar.qmenubar && qt_mac_current_menubar.qmenubar->isNativeMenuBar()) { |
|
218 |
MenuRef apple_menu = qt_mac_current_menubar.qmenubar->d_func()->mac_menubar->apple_menu; |
|
219 |
GetMenuItemProperty(apple_menu, 0, kMenuCreatorQt, kMenuPropertyMergeList, sizeof(list), 0, &list); |
|
220 |
if (list) |
|
221 |
menu = apple_menu; |
|
222 |
} |
|
223 |
if (list) { |
|
224 |
for(int i = 0; i < list->size(); ++i) { |
|
225 |
QMenuMergeItem item = list->at(i); |
|
226 |
if (item.command == command && item.action) { |
|
227 |
action = item.action; |
|
228 |
break; |
|
229 |
} |
|
230 |
} |
|
231 |
} |
|
232 |
if (!action) |
|
233 |
return false; |
|
234 |
} |
|
235 |
||
236 |
if (action_e == QAction::Trigger && by_accel && action->ignore_accel) //no, not a real accel (ie tab) |
|
237 |
return false; |
|
238 |
||
239 |
// Unhighlight the highlighted menu item before triggering the action to |
|
240 |
// prevent items from staying highlighted while a modal dialog is shown. |
|
241 |
// This also fixed the problem that parentless modal dialogs leave |
|
242 |
// the menu item highlighted (since the menu bar is cleared for these types of dialogs). |
|
243 |
if (action_e == QAction::Trigger) |
|
244 |
HiliteMenu(0); |
|
245 |
||
246 |
action->action->activate(action_e); |
|
247 |
||
248 |
//now walk up firing for each "caused" widget (like in the platform independent menu) |
|
249 |
QWidget *caused = 0; |
|
250 |
if (GetMenuItemProperty(menu, 0, kMenuCreatorQt, kMenuPropertyCausedQWidget, sizeof(caused), 0, &caused) == noErr) { |
|
251 |
MenuRef caused_menu = 0; |
|
252 |
if (QMenu *qmenu2 = qobject_cast<QMenu*>(caused)) |
|
253 |
caused_menu = qmenu2->macMenu(); |
|
254 |
else if (QMenuBar *qmenubar2 = qobject_cast<QMenuBar*>(caused)) |
|
255 |
caused_menu = qmenubar2->macMenu(); |
|
256 |
else |
|
257 |
caused_menu = 0; |
|
258 |
while(caused_menu) { |
|
259 |
//fire |
|
260 |
QWidget *widget = 0; |
|
261 |
GetMenuItemProperty(caused_menu, 0, kMenuCreatorQt, kMenuPropertyQWidget, sizeof(widget), 0, &widget); |
|
262 |
if (QMenu *qmenu = qobject_cast<QMenu*>(widget)) { |
|
263 |
if (action_e == QAction::Trigger) { |
|
264 |
emit qmenu->triggered(action->action); |
|
265 |
} else if (action_e == QAction::Hover) { |
|
266 |
action->action->showStatusText(widget); |
|
267 |
emit qmenu->hovered(action->action); |
|
268 |
} |
|
269 |
} else if (QMenuBar *qmenubar = qobject_cast<QMenuBar*>(widget)) { |
|
270 |
if (action_e == QAction::Trigger) { |
|
271 |
emit qmenubar->triggered(action->action); |
|
272 |
} else if (action_e == QAction::Hover) { |
|
273 |
action->action->showStatusText(widget); |
|
274 |
emit qmenubar->hovered(action->action); |
|
275 |
} |
|
276 |
break; //nothing more.. |
|
277 |
} |
|
278 |
||
279 |
//walk up |
|
280 |
if (GetMenuItemProperty(caused_menu, 0, kMenuCreatorQt, kMenuPropertyCausedQWidget, |
|
281 |
sizeof(caused), 0, &caused) != noErr) |
|
282 |
break; |
|
283 |
if (QMenu *qmenu2 = qobject_cast<QMenu*>(caused)) |
|
284 |
caused_menu = qmenu2->macMenu(); |
|
285 |
else if (QMenuBar *qmenubar2 = qobject_cast<QMenuBar*>(caused)) |
|
286 |
caused_menu = qmenubar2->macMenu(); |
|
287 |
else |
|
288 |
caused_menu = 0; |
|
289 |
} |
|
290 |
} |
|
291 |
return true; |
|
292 |
} |
|
293 |
||
294 |
//lookup a QMacMenuAction in a menu |
|
295 |
static int qt_mac_menu_find_action(MenuRef menu, MenuCommand cmd) |
|
296 |
{ |
|
297 |
MenuItemIndex ret_idx; |
|
298 |
MenuRef ret_menu; |
|
299 |
if (GetIndMenuItemWithCommandID(menu, cmd, 1, &ret_menu, &ret_idx) == noErr) { |
|
300 |
if (ret_menu == menu) |
|
301 |
return (int)ret_idx; |
|
302 |
} |
|
303 |
return -1; |
|
304 |
} |
|
305 |
static int qt_mac_menu_find_action(MenuRef menu, QMacMenuAction *action) |
|
306 |
{ |
|
307 |
return qt_mac_menu_find_action(menu, action->command); |
|
308 |
} |
|
309 |
||
310 |
typedef QMultiHash<OSMenuRef, EventHandlerRef> EventHandlerHash; |
|
311 |
Q_GLOBAL_STATIC(EventHandlerHash, menu_eventHandlers_hash) |
|
312 |
||
313 |
static EventTypeSpec widget_in_menu_events[] = { |
|
314 |
{ kEventClassMenu, kEventMenuMeasureItemWidth }, |
|
315 |
{ kEventClassMenu, kEventMenuMeasureItemHeight }, |
|
316 |
{ kEventClassMenu, kEventMenuDrawItem }, |
|
317 |
{ kEventClassMenu, kEventMenuCalculateSize } |
|
318 |
}; |
|
319 |
||
320 |
static OSStatus qt_mac_widget_in_menu_eventHandler(EventHandlerCallRef er, EventRef event, void *) |
|
321 |
{ |
|
322 |
UInt32 ekind = GetEventKind(event); |
|
323 |
UInt32 eclass = GetEventClass(event); |
|
324 |
OSStatus result = eventNotHandledErr; |
|
325 |
switch (eclass) { |
|
326 |
case kEventClassMenu: |
|
327 |
switch (ekind) { |
|
328 |
default: |
|
329 |
break; |
|
330 |
case kEventMenuMeasureItemWidth: { |
|
331 |
MenuItemIndex item; |
|
332 |
GetEventParameter(event, kEventParamMenuItemIndex, typeMenuItemIndex, |
|
333 |
0, sizeof(item), 0, &item); |
|
334 |
OSMenuRef menu; |
|
335 |
GetEventParameter(event, kEventParamDirectObject, typeMenuRef, 0, sizeof(menu), 0, &menu); |
|
336 |
QWidget *widget; |
|
337 |
if (GetMenuItemProperty(menu, item, kMenuCreatorQt, kMenuPropertyWidgetActionWidget, |
|
338 |
sizeof(widget), 0, &widget) == noErr) { |
|
339 |
short width = short(widget->sizeHint().width()); |
|
340 |
SetEventParameter(event, kEventParamMenuItemWidth, typeSInt16, |
|
341 |
sizeof(short), &width); |
|
342 |
result = noErr; |
|
343 |
} |
|
344 |
break; } |
|
345 |
case kEventMenuMeasureItemHeight: { |
|
346 |
MenuItemIndex item; |
|
347 |
GetEventParameter(event, kEventParamMenuItemIndex, typeMenuItemIndex, |
|
348 |
0, sizeof(item), 0, &item); |
|
349 |
OSMenuRef menu; |
|
350 |
GetEventParameter(event, kEventParamDirectObject, typeMenuRef, 0, sizeof(menu), 0, &menu); |
|
351 |
QWidget *widget; |
|
352 |
if (GetMenuItemProperty(menu, item, kMenuCreatorQt, kMenuPropertyWidgetActionWidget, |
|
353 |
sizeof(widget), 0, &widget) == noErr && widget) { |
|
354 |
short height = short(widget->sizeHint().height()); |
|
355 |
SetEventParameter(event, kEventParamMenuItemHeight, typeSInt16, |
|
356 |
sizeof(short), &height); |
|
357 |
result = noErr; |
|
358 |
} |
|
359 |
break; } |
|
360 |
case kEventMenuDrawItem: |
|
361 |
result = noErr; |
|
362 |
break; |
|
363 |
case kEventMenuCalculateSize: { |
|
364 |
result = CallNextEventHandler(er, event); |
|
365 |
if (result == noErr) { |
|
366 |
OSMenuRef menu; |
|
367 |
GetEventParameter(event, kEventParamDirectObject, typeMenuRef, 0, sizeof(menu), 0, &menu); |
|
368 |
HIViewRef content; |
|
369 |
HIMenuGetContentView(menu, kThemeMenuTypePullDown, &content); |
|
370 |
UInt16 count = CountMenuItems(menu); |
|
371 |
for (MenuItemIndex i = 1; i <= count; ++i) { |
|
372 |
QWidget *widget; |
|
373 |
if (GetMenuItemProperty(menu, i, kMenuCreatorQt, kMenuPropertyWidgetActionWidget, |
|
374 |
sizeof(widget), 0, &widget) == noErr && widget) { |
|
375 |
RgnHandle itemRgn = qt_mac_get_rgn(); |
|
376 |
GetControlRegion(content, i, itemRgn); |
|
377 |
||
378 |
Rect bounds; |
|
379 |
GetRegionBounds( itemRgn, &bounds ); |
|
380 |
qt_mac_dispose_rgn(itemRgn); |
|
381 |
widget->setGeometry(bounds.left, bounds.top, |
|
382 |
bounds.right - bounds.left, bounds.bottom - bounds.top); |
|
383 |
} |
|
384 |
} |
|
385 |
} |
|
386 |
break; } |
|
387 |
} |
|
388 |
} |
|
389 |
return result; |
|
390 |
} |
|
391 |
||
392 |
//handling of events for menurefs created by Qt.. |
|
393 |
static EventTypeSpec menu_events[] = { |
|
394 |
{ kEventClassCommand, kEventCommandProcess }, |
|
395 |
{ kEventClassMenu, kEventMenuTargetItem }, |
|
396 |
{ kEventClassMenu, kEventMenuOpening }, |
|
397 |
{ kEventClassMenu, kEventMenuClosed } |
|
398 |
}; |
|
399 |
||
400 |
// Special case for kEventMenuMatchKey, see qt_mac_create_menu below. |
|
401 |
static EventTypeSpec menu_menu_events[] = { |
|
402 |
{ kEventClassMenu, kEventMenuMatchKey } |
|
403 |
}; |
|
404 |
||
405 |
OSStatus qt_mac_menu_event(EventHandlerCallRef er, EventRef event, void *) |
|
406 |
{ |
|
407 |
QScopedLoopLevelCounter loopLevelCounter(QApplicationPrivate::instance()->threadData); |
|
408 |
||
409 |
bool handled_event = true; |
|
410 |
UInt32 ekind = GetEventKind(event), eclass = GetEventClass(event); |
|
411 |
switch(eclass) { |
|
412 |
case kEventClassCommand: |
|
413 |
if (ekind == kEventCommandProcess) { |
|
414 |
UInt32 context; |
|
415 |
GetEventParameter(event, kEventParamMenuContext, typeUInt32, |
|
416 |
0, sizeof(context), 0, &context); |
|
417 |
HICommand cmd; |
|
418 |
GetEventParameter(event, kEventParamDirectObject, typeHICommand, |
|
419 |
0, sizeof(cmd), 0, &cmd); |
|
420 |
if (!mac_keyboard_grabber && (context & kMenuContextKeyMatching)) { |
|
421 |
QMacMenuAction *action = 0; |
|
422 |
if (GetMenuCommandProperty(cmd.menu.menuRef, cmd.commandID, kMenuCreatorQt, |
|
423 |
kMenuPropertyQAction, sizeof(action), 0, &action) == noErr) { |
|
424 |
QWidget *widget = 0; |
|
425 |
if (qApp->activePopupWidget()) |
|
426 |
widget = (qApp->activePopupWidget()->focusWidget() ? |
|
427 |
qApp->activePopupWidget()->focusWidget() : qApp->activePopupWidget()); |
|
428 |
else if (QApplicationPrivate::focus_widget) |
|
429 |
widget = QApplicationPrivate::focus_widget; |
|
430 |
if (widget) { |
|
431 |
int key = action->action->shortcut(); |
|
432 |
QKeyEvent accel_ev(QEvent::ShortcutOverride, (key & (~Qt::KeyboardModifierMask)), |
|
433 |
Qt::KeyboardModifiers(key & Qt::KeyboardModifierMask)); |
|
434 |
accel_ev.ignore(); |
|
435 |
qt_sendSpontaneousEvent(widget, &accel_ev); |
|
436 |
if (accel_ev.isAccepted()) { |
|
437 |
handled_event = false; |
|
438 |
break; |
|
439 |
} |
|
440 |
} |
|
441 |
} |
|
442 |
} |
|
443 |
handled_event = qt_mac_activate_action(cmd.menu.menuRef, cmd.commandID, |
|
444 |
QAction::Trigger, context & kMenuContextKeyMatching); |
|
445 |
} |
|
446 |
break; |
|
447 |
case kEventClassMenu: { |
|
448 |
MenuRef menu; |
|
449 |
GetEventParameter(event, kEventParamDirectObject, typeMenuRef, NULL, sizeof(menu), NULL, &menu); |
|
450 |
if (ekind == kEventMenuMatchKey) { |
|
451 |
// Don't activate any actions if we are showing a native modal dialog, |
|
452 |
// the key events should go to the dialog in this case. |
|
453 |
if (QApplicationPrivate::native_modal_dialog_active) |
|
454 |
return menuItemNotFoundErr; |
|
455 |
||
456 |
handled_event = false; |
|
457 |
} else if (ekind == kEventMenuTargetItem) { |
|
458 |
MenuCommand command; |
|
459 |
GetEventParameter(event, kEventParamMenuCommand, typeMenuCommand, |
|
460 |
0, sizeof(command), 0, &command); |
|
461 |
handled_event = qt_mac_activate_action(menu, command, QAction::Hover, false); |
|
462 |
} else if (ekind == kEventMenuOpening || ekind == kEventMenuClosed) { |
|
463 |
qt_mac_menus_open_count += (ekind == kEventMenuOpening) ? 1 : -1; |
|
464 |
MenuRef mr; |
|
465 |
GetEventParameter(event, kEventParamDirectObject, typeMenuRef, |
|
466 |
0, sizeof(mr), 0, &mr); |
|
467 |
||
468 |
QWidget *widget = 0; |
|
469 |
if (GetMenuItemProperty(mr, 0, kMenuCreatorQt, kMenuPropertyQWidget, sizeof(widget), 0, &widget) == noErr) { |
|
470 |
if (QMenu *qmenu = qobject_cast<QMenu*>(widget)) { |
|
471 |
handled_event = true; |
|
472 |
if (ekind == kEventMenuOpening) { |
|
473 |
emit qmenu->aboutToShow(); |
|
474 |
||
475 |
int merged = 0; |
|
476 |
const QMenuPrivate::QMacMenuPrivate *mac_menu = qmenu->d_func()->mac_menu; |
|
477 |
const int ActionItemsCount = mac_menu->actionItems.size(); |
|
478 |
for(int i = 0; i < ActionItemsCount; ++i) { |
|
479 |
QMacMenuAction *action = mac_menu->actionItems.at(i); |
|
480 |
if (action->action->isSeparator()) { |
|
481 |
bool hide = false; |
|
482 |
if(!action->action->isVisible()) { |
|
483 |
hide = true; |
|
484 |
} else if (merged && merged == i) { |
|
485 |
hide = true; |
|
486 |
} else { |
|
487 |
for(int l = i+1; l < mac_menu->actionItems.size(); ++l) { |
|
488 |
QMacMenuAction *action = mac_menu->actionItems.at(l); |
|
489 |
if (action->merged) { |
|
490 |
hide = true; |
|
491 |
} else if (action->action->isSeparator()) { |
|
492 |
if (hide) |
|
493 |
break; |
|
494 |
} else if (!action->merged) { |
|
495 |
hide = false; |
|
496 |
break; |
|
497 |
} |
|
498 |
} |
|
499 |
} |
|
500 |
||
501 |
const int index = qt_mac_menu_find_action(mr, action); |
|
502 |
if (hide) { |
|
503 |
++merged; |
|
504 |
ChangeMenuItemAttributes(mr, index, kMenuItemAttrHidden, 0); |
|
505 |
} else { |
|
506 |
ChangeMenuItemAttributes(mr, index, 0, kMenuItemAttrHidden); |
|
507 |
} |
|
508 |
} else if (action->merged) { |
|
509 |
++merged; |
|
510 |
} |
|
511 |
} |
|
512 |
} else { |
|
513 |
emit qmenu->aboutToHide(); |
|
514 |
} |
|
515 |
} |
|
516 |
} |
|
517 |
} else { |
|
518 |
handled_event = false; |
|
519 |
} |
|
520 |
break; } |
|
521 |
default: |
|
522 |
handled_event = false; |
|
523 |
break; |
|
524 |
} |
|
525 |
if (!handled_event) //let the event go through |
|
526 |
return CallNextEventHandler(er, event); |
|
527 |
return noErr; //we eat the event |
|
528 |
} |
|
529 |
static EventHandlerRef mac_menu_event_handler = 0; |
|
530 |
static EventHandlerUPP mac_menu_eventUPP = 0; |
|
531 |
static void qt_mac_cleanup_menu_event() |
|
532 |
{ |
|
533 |
if (mac_menu_event_handler) { |
|
534 |
RemoveEventHandler(mac_menu_event_handler); |
|
535 |
mac_menu_event_handler = 0; |
|
536 |
} |
|
537 |
if (mac_menu_eventUPP) { |
|
538 |
DisposeEventHandlerUPP(mac_menu_eventUPP); |
|
539 |
mac_menu_eventUPP = 0; |
|
540 |
} |
|
541 |
} |
|
542 |
static inline void qt_mac_create_menu_event_handler() |
|
543 |
{ |
|
544 |
if (!mac_menu_event_handler) { |
|
545 |
mac_menu_eventUPP = NewEventHandlerUPP(qt_mac_menu_event); |
|
546 |
InstallEventHandler(GetApplicationEventTarget(), mac_menu_eventUPP, |
|
547 |
GetEventTypeCount(menu_events), menu_events, 0, |
|
548 |
&mac_menu_event_handler); |
|
549 |
qAddPostRoutine(qt_mac_cleanup_menu_event); |
|
550 |
} |
|
551 |
} |
|
552 |
||
553 |
||
554 |
//enabling of commands |
|
555 |
static void qt_mac_command_set_enabled(MenuRef menu, UInt32 cmd, bool b) |
|
556 |
{ |
|
557 |
if (cmd == kHICommandQuit) |
|
558 |
qt_mac_quit_menu_item_enabled = b; |
|
559 |
||
560 |
if (b) { |
|
561 |
EnableMenuCommand(menu, cmd); |
|
562 |
if (MenuRef dock_menu = GetApplicationDockTileMenu()) |
|
563 |
EnableMenuCommand(dock_menu, cmd); |
|
564 |
} else { |
|
565 |
DisableMenuCommand(menu, cmd); |
|
566 |
if (MenuRef dock_menu = GetApplicationDockTileMenu()) |
|
567 |
DisableMenuCommand(dock_menu, cmd); |
|
568 |
} |
|
569 |
} |
|
570 |
||
571 |
static bool qt_mac_auto_apple_menu(MenuCommand cmd) |
|
572 |
{ |
|
573 |
return (cmd == kHICommandPreferences || cmd == kHICommandQuit); |
|
574 |
} |
|
575 |
||
576 |
static void qt_mac_get_accel(quint32 accel_key, quint32 *modif, quint32 *key) { |
|
577 |
if (modif) { |
|
578 |
*modif = constructModifierMask(accel_key); |
|
579 |
} |
|
580 |
||
581 |
accel_key &= ~(Qt::MODIFIER_MASK | Qt::UNICODE_ACCEL); |
|
582 |
if (key) { |
|
583 |
*key = 0; |
|
584 |
if (accel_key == Qt::Key_Return) |
|
585 |
*key = kMenuReturnGlyph; |
|
586 |
else if (accel_key == Qt::Key_Enter) |
|
587 |
*key = kMenuEnterGlyph; |
|
588 |
else if (accel_key == Qt::Key_Tab) |
|
589 |
*key = kMenuTabRightGlyph; |
|
590 |
else if (accel_key == Qt::Key_Backspace) |
|
591 |
*key = kMenuDeleteLeftGlyph; |
|
592 |
else if (accel_key == Qt::Key_Delete) |
|
593 |
*key = kMenuDeleteRightGlyph; |
|
594 |
else if (accel_key == Qt::Key_Escape) |
|
595 |
*key = kMenuEscapeGlyph; |
|
596 |
else if (accel_key == Qt::Key_PageUp) |
|
597 |
*key = kMenuPageUpGlyph; |
|
598 |
else if (accel_key == Qt::Key_PageDown) |
|
599 |
*key = kMenuPageDownGlyph; |
|
600 |
else if (accel_key == Qt::Key_Up) |
|
601 |
*key = kMenuUpArrowGlyph; |
|
602 |
else if (accel_key == Qt::Key_Down) |
|
603 |
*key = kMenuDownArrowGlyph; |
|
604 |
else if (accel_key == Qt::Key_Left) |
|
605 |
*key = kMenuLeftArrowGlyph; |
|
606 |
else if (accel_key == Qt::Key_Right) |
|
607 |
*key = kMenuRightArrowGlyph; |
|
608 |
else if (accel_key == Qt::Key_CapsLock) |
|
609 |
*key = kMenuCapsLockGlyph; |
|
610 |
else if (accel_key >= Qt::Key_F1 && accel_key <= Qt::Key_F15) |
|
611 |
*key = (accel_key - Qt::Key_F1) + kMenuF1Glyph; |
|
612 |
else if (accel_key == Qt::Key_Home) |
|
613 |
*key = kMenuNorthwestArrowGlyph; |
|
614 |
else if (accel_key == Qt::Key_End) |
|
615 |
*key = kMenuSoutheastArrowGlyph; |
|
616 |
} |
|
617 |
} |
|
618 |
#else // Cocoa |
|
619 |
static inline void syncNSMenuItemVisiblity(NSMenuItem *menuItem, bool actionVisibility) |
|
620 |
{ |
|
621 |
[menuItem setHidden:NO]; |
|
622 |
[menuItem setHidden:YES]; |
|
623 |
[menuItem setHidden:!actionVisibility]; |
|
624 |
} |
|
625 |
||
626 |
static inline void syncNSMenuItemEnabled(NSMenuItem *menuItem, bool enabled) |
|
627 |
{ |
|
628 |
[menuItem setEnabled:NO]; |
|
629 |
[menuItem setEnabled:YES]; |
|
630 |
[menuItem setEnabled:enabled]; |
|
631 |
} |
|
632 |
||
633 |
static inline void syncMenuBarItemsVisiblity(const QMenuBarPrivate::QMacMenuBarPrivate *mac_menubar) |
|
634 |
{ |
|
635 |
const QList<QMacMenuAction *> &menubarActions = mac_menubar->actionItems; |
|
636 |
for (int i = 0; i < menubarActions.size(); ++i) { |
|
637 |
const QMacMenuAction *action = menubarActions.at(i); |
|
638 |
syncNSMenuItemVisiblity(action->menuItem, actualMenuItemVisibility(mac_menubar, action)); |
|
639 |
} |
|
640 |
} |
|
641 |
||
642 |
static inline QT_MANGLE_NAMESPACE(QCocoaMenuLoader) *getMenuLoader() |
|
643 |
{ |
|
644 |
return [NSApp QT_MANGLE_NAMESPACE(qt_qcocoamenuLoader)]; |
|
645 |
} |
|
646 |
||
647 |
static NSMenuItem *createNSMenuItem(const QString &title) |
|
648 |
{ |
|
649 |
NSMenuItem *item = [[NSMenuItem alloc] |
|
650 |
initWithTitle:qt_mac_QStringToNSString(title) |
|
651 |
action:@selector(qtDispatcherToQAction:) keyEquivalent:@""]; |
|
652 |
[item setTarget:getMenuLoader()]; |
|
653 |
return item; |
|
654 |
} |
|
655 |
#endif |
|
656 |
||
657 |
||
658 |
||
659 |
// helper that recurses into a menu structure and en/dis-ables them |
|
660 |
void qt_mac_set_modal_state_helper_recursive(OSMenuRef menu, OSMenuRef merge, bool on) |
|
661 |
{ |
|
662 |
#ifndef QT_MAC_USE_COCOA |
|
663 |
for (int i = 0; i < CountMenuItems(menu); i++) { |
|
664 |
OSMenuRef submenu; |
|
665 |
GetMenuItemHierarchicalMenu(menu, i+1, &submenu); |
|
666 |
if (submenu != merge) { |
|
667 |
if (submenu) |
|
668 |
qt_mac_set_modal_state_helper_recursive(submenu, merge, on); |
|
669 |
if (on) |
|
670 |
DisableMenuItem(submenu, 0); |
|
671 |
else |
|
672 |
EnableMenuItem(submenu, 0); |
|
673 |
} |
|
674 |
} |
|
675 |
#else |
|
676 |
for (NSMenuItem *item in [menu itemArray]) { |
|
677 |
OSMenuRef submenu = [item submenu]; |
|
678 |
if (submenu != merge) { |
|
679 |
if (submenu) |
|
680 |
qt_mac_set_modal_state_helper_recursive(submenu, merge, on); |
|
681 |
if (!on) { |
|
682 |
// The item should follow what the QAction has. |
|
683 |
if ([item tag]) { |
|
684 |
QAction *action = reinterpret_cast<QAction *>([item tag]); |
|
685 |
syncNSMenuItemEnabled(item, action->isEnabled()); |
|
686 |
} else { |
|
687 |
syncNSMenuItemEnabled(item, YES); |
|
688 |
} |
|
689 |
} else { |
|
690 |
syncNSMenuItemEnabled(item, NO); |
|
691 |
} |
|
692 |
} |
|
693 |
} |
|
694 |
#endif |
|
695 |
} |
|
696 |
||
697 |
//toggling of modal state |
|
698 |
static void qt_mac_set_modal_state(OSMenuRef menu, bool on) |
|
699 |
{ |
|
700 |
#ifndef QT_MAC_USE_COCOA |
|
701 |
OSMenuRef merge = 0; |
|
702 |
GetMenuItemProperty(menu, 0, kMenuCreatorQt, kMenuPropertyMergeMenu, |
|
703 |
sizeof(merge), 0, &merge); |
|
704 |
||
705 |
qt_mac_set_modal_state_helper_recursive(menu, merge, on); |
|
706 |
||
707 |
UInt32 commands[] = { kHICommandQuit, kHICommandPreferences, kHICommandAbout, kHICommandAboutQt, 0 }; |
|
708 |
for(int c = 0; commands[c]; c++) { |
|
709 |
bool enabled = !on; |
|
710 |
if (enabled) { |
|
711 |
QMacMenuAction *action = 0; |
|
712 |
GetMenuCommandProperty(menu, commands[c], kMenuCreatorQt, kMenuPropertyQAction, |
|
713 |
sizeof(action), 0, &action); |
|
714 |
if (!action && merge) { |
|
715 |
GetMenuCommandProperty(merge, commands[c], kMenuCreatorQt, kMenuPropertyQAction, |
|
716 |
sizeof(action), 0, &action); |
|
717 |
if (!action) { |
|
718 |
QMenuMergeList *list = 0; |
|
719 |
GetMenuItemProperty(merge, 0, kMenuCreatorQt, kMenuPropertyMergeList, |
|
720 |
sizeof(list), 0, &list); |
|
721 |
for(int i = 0; list && i < list->size(); ++i) { |
|
722 |
QMenuMergeItem item = list->at(i); |
|
723 |
if (item.command == commands[c] && item.action) { |
|
724 |
action = item.action; |
|
725 |
break; |
|
726 |
} |
|
727 |
} |
|
728 |
} |
|
729 |
} |
|
730 |
||
731 |
if (!action) { |
|
732 |
if (commands[c] != kHICommandQuit) |
|
733 |
enabled = false; |
|
734 |
} else { |
|
735 |
enabled = action->action ? action->action->isEnabled() : 0; |
|
736 |
} |
|
737 |
} |
|
738 |
qt_mac_command_set_enabled(menu, commands[c], enabled); |
|
739 |
} |
|
740 |
#else |
|
741 |
OSMenuRef merge = QMenuPrivate::mergeMenuHash.value(menu); |
|
742 |
qt_mac_set_modal_state_helper_recursive(menu, merge, on); |
|
743 |
// I'm ignoring the special items now, since they should get handled via a syncAction() |
|
744 |
#endif |
|
745 |
} |
|
746 |
||
747 |
bool qt_mac_menubar_is_open() |
|
748 |
{ |
|
749 |
return qt_mac_menus_open_count > 0; |
|
750 |
} |
|
751 |
||
752 |
void qt_mac_clear_menubar() |
|
753 |
{ |
|
754 |
if (QApplication::testAttribute(Qt::AA_MacPluginApplication)) |
|
755 |
return; |
|
756 |
||
757 |
#ifndef QT_MAC_USE_COCOA |
|
758 |
MenuRef clear_menu = 0; |
|
759 |
if (CreateNewMenu(0, 0, &clear_menu) == noErr) { |
|
760 |
SetRootMenu(clear_menu); |
|
761 |
ReleaseMenu(clear_menu); |
|
762 |
} else { |
|
763 |
qWarning("QMenu: Internal error at %s:%d", __FILE__, __LINE__); |
|
764 |
} |
|
765 |
ClearMenuBar(); |
|
766 |
qt_mac_command_set_enabled(0, kHICommandPreferences, false); |
|
767 |
InvalMenuBar(); |
|
768 |
#else |
|
769 |
QMacCocoaAutoReleasePool pool; |
|
770 |
QT_MANGLE_NAMESPACE(QCocoaMenuLoader) *loader = getMenuLoader(); |
|
771 |
NSMenu *menu = [loader menu]; |
|
772 |
[loader ensureAppMenuInMenu:menu]; |
|
773 |
[NSApp setMainMenu:menu]; |
|
774 |
#endif |
|
775 |
} |
|
776 |
||
777 |
||
778 |
QMacMenuAction::~QMacMenuAction() |
|
779 |
{ |
|
780 |
#ifdef QT_MAC_USE_COCOA |
|
781 |
[menu release]; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
782 |
// Update the menu item if this action still owns it. For some items |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
783 |
// (like 'Quit') ownership will be transferred between all menu bars... |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
784 |
if (action && action.data() == reinterpret_cast<QAction *>([menuItem tag])) { |
0 | 785 |
QAction::MenuRole role = action->menuRole(); |
786 |
// Check if the item is owned by Qt, and should be hidden to keep it from causing |
|
787 |
// problems. Do it for everything but the quit menu item since that should always |
|
788 |
// be visible. |
|
789 |
if (role > QAction::ApplicationSpecificRole && role < QAction::QuitRole) { |
|
790 |
[menuItem setHidden:YES]; |
|
791 |
} else if (role == QAction::TextHeuristicRole |
|
792 |
&& menuItem != [getMenuLoader() quitMenuItem]) { |
|
793 |
[menuItem setHidden:YES]; |
|
794 |
} |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
795 |
[menuItem setTag:nil]; |
0 | 796 |
} |
797 |
[menuItem release]; |
|
798 |
#endif |
|
799 |
} |
|
800 |
||
801 |
#ifndef QT_MAC_USE_COCOA |
|
802 |
static MenuCommand qt_mac_menu_merge_action(MenuRef merge, QMacMenuAction *action) |
|
803 |
#else |
|
804 |
static NSMenuItem *qt_mac_menu_merge_action(OSMenuRef merge, QMacMenuAction *action) |
|
805 |
#endif |
|
806 |
{ |
|
807 |
if (qt_mac_no_menubar_merge || action->action->menu() || action->action->isSeparator() |
|
808 |
|| action->action->menuRole() == QAction::NoRole) |
|
809 |
return 0; |
|
810 |
||
811 |
QString t = qt_mac_removeMnemonics(action->action->text().toLower()); |
|
812 |
int st = t.lastIndexOf(QLatin1Char('\t')); |
|
813 |
if (st != -1) |
|
814 |
t.remove(st, t.length()-st); |
|
815 |
t.replace(QRegExp(QString::fromLatin1("\\.*$")), QLatin1String("")); //no ellipses |
|
816 |
//now the fun part |
|
817 |
#ifndef QT_MAC_USE_COCOA |
|
818 |
MenuCommand ret = 0; |
|
819 |
#else |
|
820 |
NSMenuItem *ret = 0; |
|
821 |
QT_MANGLE_NAMESPACE(QCocoaMenuLoader) *loader = getMenuLoader(); |
|
822 |
#endif |
|
823 |
switch (action->action->menuRole()) { |
|
824 |
case QAction::NoRole: |
|
825 |
ret = 0; |
|
826 |
break; |
|
827 |
case QAction::ApplicationSpecificRole: |
|
828 |
#ifndef QT_MAC_USE_COCOA |
|
829 |
{ |
|
830 |
QMenuMergeList *list = 0; |
|
831 |
if (GetMenuItemProperty(merge, 0, kMenuCreatorQt, kMenuPropertyMergeList, |
|
832 |
sizeof(list), 0, &list) == noErr && list) { |
|
833 |
MenuCommand lastCustom = kHICommandCustomMerge; |
|
834 |
for(int i = 0; i < list->size(); ++i) { |
|
835 |
QMenuMergeItem item = list->at(i); |
|
836 |
if (item.command == lastCustom) |
|
837 |
++lastCustom; |
|
838 |
} |
|
839 |
ret = lastCustom; |
|
840 |
} else { |
|
841 |
// The list hasn't been created, so, must be the first one. |
|
842 |
ret = kHICommandCustomMerge; |
|
843 |
} |
|
844 |
} |
|
845 |
#else |
|
846 |
ret = [loader appSpecificMenuItem]; |
|
847 |
#endif |
|
848 |
break; |
|
849 |
case QAction::AboutRole: |
|
850 |
#ifndef QT_MAC_USE_COCOA |
|
851 |
ret = kHICommandAbout; |
|
852 |
#else |
|
853 |
ret = [loader aboutMenuItem]; |
|
854 |
#endif |
|
855 |
break; |
|
856 |
case QAction::AboutQtRole: |
|
857 |
#ifndef QT_MAC_USE_COCOA |
|
858 |
ret = kHICommandAboutQt; |
|
859 |
#else |
|
860 |
ret = [loader aboutQtMenuItem]; |
|
861 |
#endif |
|
862 |
break; |
|
863 |
case QAction::QuitRole: |
|
864 |
#ifndef QT_MAC_USE_COCOA |
|
865 |
ret = kHICommandQuit; |
|
866 |
#else |
|
867 |
ret = [loader quitMenuItem]; |
|
868 |
#endif |
|
869 |
break; |
|
870 |
case QAction::PreferencesRole: |
|
871 |
#ifndef QT_MAC_USE_COCOA |
|
872 |
ret = kHICommandPreferences; |
|
873 |
#else |
|
874 |
ret = [loader preferencesMenuItem]; |
|
875 |
#endif |
|
876 |
break; |
|
877 |
case QAction::TextHeuristicRole: { |
|
878 |
QString aboutString = QMenuBar::tr("About").toLower(); |
|
879 |
if (t.startsWith(aboutString) || t.endsWith(aboutString)) { |
|
880 |
if (t.indexOf(QRegExp(QString::fromLatin1("qt$"), Qt::CaseInsensitive)) == -1) { |
|
881 |
#ifndef QT_MAC_USE_COCOA |
|
882 |
ret = kHICommandAbout; |
|
883 |
#else |
|
884 |
ret = [loader aboutMenuItem]; |
|
885 |
#endif |
|
886 |
} else { |
|
887 |
#ifndef QT_MAC_USE_COCOA |
|
888 |
ret = kHICommandAboutQt; |
|
889 |
#else |
|
890 |
ret = [loader aboutQtMenuItem]; |
|
891 |
#endif |
|
892 |
} |
|
893 |
} else if (t.startsWith(QMenuBar::tr("Config").toLower()) |
|
894 |
|| t.startsWith(QMenuBar::tr("Preference").toLower()) |
|
895 |
|| t.startsWith(QMenuBar::tr("Options").toLower()) |
|
896 |
|| t.startsWith(QMenuBar::tr("Setting").toLower()) |
|
897 |
|| t.startsWith(QMenuBar::tr("Setup").toLower())) { |
|
898 |
#ifndef QT_MAC_USE_COCOA |
|
899 |
ret = kHICommandPreferences; |
|
900 |
#else |
|
901 |
ret = [loader preferencesMenuItem]; |
|
902 |
#endif |
|
903 |
} else if (t.startsWith(QMenuBar::tr("Quit").toLower()) |
|
904 |
|| t.startsWith(QMenuBar::tr("Exit").toLower())) { |
|
905 |
#ifndef QT_MAC_USE_COCOA |
|
906 |
ret = kHICommandQuit; |
|
907 |
#else |
|
908 |
ret = [loader quitMenuItem]; |
|
909 |
#endif |
|
910 |
} |
|
911 |
} |
|
912 |
break; |
|
913 |
} |
|
914 |
||
915 |
#ifndef QT_MAC_USE_COCOA |
|
916 |
QMenuMergeList *list = 0; |
|
917 |
if (GetMenuItemProperty(merge, 0, kMenuCreatorQt, kMenuPropertyMergeList, |
|
918 |
sizeof(list), 0, &list) == noErr && list) { |
|
919 |
for(int i = 0; i < list->size(); ++i) { |
|
920 |
QMenuMergeItem item = list->at(i); |
|
921 |
if (item.command == ret && item.action) |
|
922 |
return 0; |
|
923 |
} |
|
924 |
} |
|
925 |
||
926 |
QAction *cmd_action = 0; |
|
927 |
if (GetMenuCommandProperty(merge, ret, kMenuCreatorQt, kMenuPropertyQAction, |
|
928 |
sizeof(cmd_action), 0, &cmd_action) == noErr && cmd_action) |
|
929 |
return 0; //already taken |
|
930 |
#else |
|
931 |
if (QMenuMergeList *list = QMenuPrivate::mergeMenuItemsHash.value(merge)) { |
|
932 |
for(int i = 0; i < list->size(); ++i) { |
|
933 |
const QMenuMergeItem &item = list->at(i); |
|
934 |
if (item.menuItem == ret && item.action) |
|
935 |
return 0; |
|
936 |
} |
|
937 |
} |
|
938 |
||
939 |
#endif |
|
940 |
return ret; |
|
941 |
} |
|
942 |
||
943 |
static QString qt_mac_menu_merge_text(QMacMenuAction *action) |
|
944 |
{ |
|
945 |
QString ret; |
|
946 |
#ifdef QT_MAC_USE_COCOA |
|
947 |
QT_MANGLE_NAMESPACE(QCocoaMenuLoader) *loader = getMenuLoader(); |
|
948 |
#endif |
|
949 |
if (action->action->menuRole() == QAction::ApplicationSpecificRole) |
|
950 |
ret = action->action->text(); |
|
951 |
#ifndef QT_MAC_USE_COCOA |
|
952 |
else if (action->command == kHICommandAbout) |
|
953 |
ret = QMenuBar::tr("About %1").arg(qAppName()); |
|
954 |
else if (action->command == kHICommandAboutQt) |
|
955 |
ret = QMenuBar::tr("About Qt"); |
|
956 |
else if (action->command == kHICommandPreferences) |
|
957 |
ret = QMenuBar::tr("Preferences"); |
|
958 |
else if (action->command == kHICommandQuit) |
|
959 |
ret = QMenuBar::tr("Quit %1").arg(qAppName()); |
|
960 |
#else |
|
961 |
else if (action->menuItem == [loader aboutMenuItem]) |
|
962 |
ret = QMenuBar::tr("About %1").arg(qAppName()); |
|
963 |
else if (action->menuItem == [loader aboutQtMenuItem]) |
|
964 |
ret = QMenuBar::tr("About Qt"); |
|
965 |
else if (action->menuItem == [loader preferencesMenuItem]) |
|
966 |
ret = QMenuBar::tr("Preferences"); |
|
967 |
else if (action->menuItem == [loader quitMenuItem]) |
|
968 |
ret = QMenuBar::tr("Quit %1").arg(qAppName()); |
|
969 |
#endif |
|
970 |
return ret; |
|
971 |
} |
|
972 |
||
973 |
static QKeySequence qt_mac_menu_merge_accel(QMacMenuAction *action) |
|
974 |
{ |
|
975 |
QKeySequence ret; |
|
976 |
#ifdef QT_MAC_USE_COCOA |
|
977 |
QT_MANGLE_NAMESPACE(QCocoaMenuLoader) *loader = getMenuLoader(); |
|
978 |
#endif |
|
979 |
if (action->action->menuRole() == QAction::ApplicationSpecificRole) |
|
980 |
ret = action->action->shortcut(); |
|
981 |
#ifndef QT_MAC_USE_COCOA |
|
982 |
else if (action->command == kHICommandPreferences) |
|
983 |
ret = QKeySequence(QKeySequence::Preferences); |
|
984 |
else if (action->command == kHICommandQuit) |
|
985 |
ret = QKeySequence(QKeySequence::Quit); |
|
986 |
#else |
|
987 |
else if (action->menuItem == [loader preferencesMenuItem]) |
|
988 |
ret = QKeySequence(QKeySequence::Preferences); |
|
989 |
else if (action->menuItem == [loader quitMenuItem]) |
|
990 |
ret = QKeySequence(QKeySequence::Quit); |
|
991 |
#endif |
|
992 |
return ret; |
|
993 |
} |
|
994 |
||
995 |
void Q_GUI_EXPORT qt_mac_set_menubar_icons(bool b) |
|
996 |
{ QApplication::instance()->setAttribute(Qt::AA_DontShowIconsInMenus, !b); } |
|
997 |
void Q_GUI_EXPORT qt_mac_set_native_menubar(bool b) |
|
998 |
{ QApplication::instance()->setAttribute(Qt::AA_DontUseNativeMenuBar, !b); } |
|
999 |
void Q_GUI_EXPORT qt_mac_set_menubar_merge(bool b) { qt_mac_no_menubar_merge = !b; } |
|
1000 |
||
1001 |
/***************************************************************************** |
|
1002 |
QMenu bindings |
|
1003 |
*****************************************************************************/ |
|
1004 |
QMenuPrivate::QMacMenuPrivate::QMacMenuPrivate() : menu(0) |
|
1005 |
{ |
|
1006 |
} |
|
1007 |
||
1008 |
QMenuPrivate::QMacMenuPrivate::~QMacMenuPrivate() |
|
1009 |
{ |
|
1010 |
#ifndef QT_MAC_USE_COCOA |
|
1011 |
for(QList<QMacMenuAction*>::Iterator it = actionItems.begin(); it != actionItems.end(); ++it) { |
|
1012 |
QMacMenuAction *action = (*it); |
|
1013 |
RemoveMenuCommandProperty(action->menu, action->command, kMenuCreatorQt, kMenuPropertyQAction); |
|
1014 |
if (action->merged) { |
|
1015 |
QMenuMergeList *list = 0; |
|
1016 |
GetMenuItemProperty(action->menu, 0, kMenuCreatorQt, kMenuPropertyMergeList, |
|
1017 |
sizeof(list), 0, &list); |
|
1018 |
for(int i = 0; list && i < list->size(); ) { |
|
1019 |
QMenuMergeItem item = list->at(i); |
|
1020 |
if (item.action == action) |
|
1021 |
list->removeAt(i); |
|
1022 |
else |
|
1023 |
++i; |
|
1024 |
} |
|
1025 |
} |
|
1026 |
delete action; |
|
1027 |
} |
|
1028 |
if (menu) { |
|
1029 |
EventHandlerHash::iterator it = menu_eventHandlers_hash()->find(menu); |
|
1030 |
while (it != menu_eventHandlers_hash()->end() && it.key() == menu) { |
|
1031 |
RemoveEventHandler(it.value()); |
|
1032 |
++it; |
|
1033 |
} |
|
1034 |
menu_eventHandlers_hash()->remove(menu); |
|
1035 |
ReleaseMenu(menu); |
|
1036 |
} |
|
1037 |
#else |
|
1038 |
QMacCocoaAutoReleasePool pool; |
|
1039 |
while (actionItems.size()) { |
|
1040 |
QMacMenuAction *action = actionItems.takeFirst(); |
|
1041 |
if (QMenuMergeList *list = mergeMenuItemsHash.value(action->menu)) { |
|
1042 |
int i = 0; |
|
1043 |
while (i < list->size()) { |
|
1044 |
const QMenuMergeItem &item = list->at(i); |
|
1045 |
if (item.action == action) |
|
1046 |
list->removeAt(i); |
|
1047 |
else |
|
1048 |
++i; |
|
1049 |
} |
|
1050 |
} |
|
1051 |
delete action; |
|
1052 |
} |
|
1053 |
mergeMenuHash.remove(menu); |
|
1054 |
mergeMenuItemsHash.remove(menu); |
|
1055 |
[menu release]; |
|
1056 |
#endif |
|
1057 |
} |
|
1058 |
||
1059 |
void |
|
1060 |
QMenuPrivate::QMacMenuPrivate::addAction(QAction *a, QMacMenuAction *before, QMenuPrivate *qmenu) |
|
1061 |
{ |
|
1062 |
QMacMenuAction *action = new QMacMenuAction; |
|
1063 |
action->action = a; |
|
1064 |
action->ignore_accel = 0; |
|
1065 |
action->merged = 0; |
|
1066 |
action->menu = 0; |
|
1067 |
#ifndef QT_MAC_USE_COCOA |
|
1068 |
action->command = qt_mac_menu_static_cmd_id++; |
|
1069 |
#endif |
|
1070 |
addAction(action, before, qmenu); |
|
1071 |
} |
|
1072 |
||
1073 |
void |
|
1074 |
QMenuPrivate::QMacMenuPrivate::addAction(QMacMenuAction *action, QMacMenuAction *before, QMenuPrivate *qmenu) |
|
1075 |
{ |
|
1076 |
#ifdef QT_MAC_USE_COCOA |
|
1077 |
QMacCocoaAutoReleasePool pool; |
|
1078 |
Q_UNUSED(qmenu); |
|
1079 |
#endif |
|
1080 |
if (!action) |
|
1081 |
return; |
|
1082 |
int before_index = actionItems.indexOf(before); |
|
1083 |
if (before_index < 0) { |
|
1084 |
before = 0; |
|
1085 |
before_index = actionItems.size(); |
|
1086 |
} |
|
1087 |
actionItems.insert(before_index, action); |
|
1088 |
||
1089 |
#ifndef QT_MAC_USE_COCOA |
|
1090 |
int index = qt_mac_menu_find_action(menu, action); |
|
1091 |
#else |
|
1092 |
[menu retain]; |
|
1093 |
[action->menu release]; |
|
1094 |
#endif |
|
1095 |
action->menu = menu; |
|
1096 |
||
1097 |
/* When the action is considered a mergable action it |
|
1098 |
will stay that way, until removed.. */ |
|
1099 |
if (!qt_mac_no_menubar_merge) { |
|
1100 |
#ifndef QT_MAC_USE_COCOA |
|
1101 |
MenuRef merge = 0; |
|
1102 |
GetMenuItemProperty(menu, 0, kMenuCreatorQt, kMenuPropertyMergeMenu, |
|
1103 |
sizeof(merge), 0, &merge); |
|
1104 |
#else |
|
1105 |
OSMenuRef merge = QMenuPrivate::mergeMenuHash.value(menu); |
|
1106 |
#endif |
|
1107 |
if (merge) { |
|
1108 |
#ifndef QT_MAC_USE_COCOA |
|
1109 |
if (MenuCommand cmd = qt_mac_menu_merge_action(merge, action)) { |
|
1110 |
action->merged = 1; |
|
1111 |
action->menu = merge; |
|
1112 |
action->command = cmd; |
|
1113 |
if (qt_mac_auto_apple_menu(cmd)) |
|
1114 |
index = 0; //no need |
|
1115 |
||
1116 |
QMenuMergeList *list = 0; |
|
1117 |
if (GetMenuItemProperty(merge, 0, kMenuCreatorQt, kMenuPropertyMergeList, |
|
1118 |
sizeof(list), 0, &list) != noErr || !list) { |
|
1119 |
list = new QMenuMergeList; |
|
1120 |
SetMenuItemProperty(merge, 0, kMenuCreatorQt, kMenuPropertyMergeList, |
|
1121 |
sizeof(list), &list); |
|
1122 |
} |
|
1123 |
list->append(QMenuMergeItem(cmd, action)); |
|
1124 |
} |
|
1125 |
#else |
|
1126 |
if (NSMenuItem *cmd = qt_mac_menu_merge_action(merge, action)) { |
|
1127 |
action->merged = 1; |
|
1128 |
[merge retain]; |
|
1129 |
[action->menu release]; |
|
1130 |
action->menu = merge; |
|
1131 |
[cmd retain]; |
|
1132 |
[cmd setAction:@selector(qtDispatcherToQAction:)]; |
|
1133 |
[cmd setTarget:getMenuLoader()]; |
|
1134 |
[action->menuItem release]; |
|
1135 |
action->menuItem = cmd; |
|
1136 |
QMenuMergeList *list = QMenuPrivate::mergeMenuItemsHash.value(merge); |
|
1137 |
if (!list) { |
|
1138 |
list = new QMenuMergeList; |
|
1139 |
QMenuPrivate::mergeMenuItemsHash.insert(merge, list); |
|
1140 |
} |
|
1141 |
list->append(QMenuMergeItem(cmd, action)); |
|
1142 |
} |
|
1143 |
#endif |
|
1144 |
} |
|
1145 |
} |
|
1146 |
||
1147 |
#ifdef QT_MAC_USE_COCOA |
|
1148 |
NSMenuItem *newItem = action->menuItem; |
|
1149 |
#endif |
|
1150 |
if ( |
|
1151 |
#ifndef QT_MAC_USE_COCOA |
|
1152 |
index == -1 |
|
1153 |
#else |
|
1154 |
newItem == 0 |
|
1155 |
#endif |
|
1156 |
) { |
|
1157 |
#ifndef QT_MAC_USE_COCOA |
|
1158 |
index = before_index; |
|
1159 |
MenuItemAttributes attr = kMenuItemAttrAutoRepeat; |
|
1160 |
#else |
|
1161 |
newItem = createNSMenuItem(action->action->text()); |
|
1162 |
action->menuItem = newItem; |
|
1163 |
#endif |
|
1164 |
if (before) { |
|
1165 |
#ifndef QT_MAC_USE_COCOA |
|
1166 |
InsertMenuItemTextWithCFString(action->menu, 0, qMax(before_index, 0), attr, action->command); |
|
1167 |
#else |
|
1168 |
[menu insertItem:newItem atIndex:qMax(before_index, 0)]; |
|
1169 |
#endif |
|
1170 |
} else { |
|
1171 |
#ifndef QT_MAC_USE_COCOA |
|
1172 |
// Append the menu item to the menu. If it is a kHICommandAbout or a kHICommandAboutQt append |
|
1173 |
// a separator also (to get a separator above "Preferences"), but make sure that we don't |
|
1174 |
// add separators between two "about" items. |
|
1175 |
||
1176 |
// Build a set of all commands that could possibly be before the separator. |
|
1177 |
QSet<MenuCommand> mergedItems; |
|
1178 |
mergedItems.insert(kHICommandAbout); |
|
1179 |
mergedItems.insert(kHICommandAboutQt); |
|
1180 |
mergedItems.insert(kHICommandCustomMerge); |
|
1181 |
||
1182 |
QMenuMergeList *list = 0; |
|
1183 |
if (GetMenuItemProperty(action->menu, 0, kMenuCreatorQt, kMenuPropertyMergeList, |
|
1184 |
sizeof(list), 0, &list) == noErr && list) { |
|
1185 |
for (int i = 0; i < list->size(); ++i) { |
|
1186 |
MenuCommand command = list->at(i).command; |
|
1187 |
if (command > kHICommandCustomMerge) { |
|
1188 |
mergedItems.insert(command); |
|
1189 |
} |
|
1190 |
} |
|
1191 |
} |
|
1192 |
||
1193 |
const int itemCount = CountMenuItems(action->menu); |
|
1194 |
MenuItemAttributes testattr; |
|
1195 |
GetMenuItemAttributes(action->menu, itemCount , &testattr); |
|
1196 |
if (mergedItems.contains(action->command) |
|
1197 |
&& (testattr & kMenuItemAttrSeparator)) { |
|
1198 |
InsertMenuItemTextWithCFString(action->menu, 0, qMax(itemCount - 1, 0), attr, action->command); |
|
1199 |
index = itemCount; |
|
1200 |
} else { |
|
1201 |
MenuItemIndex tmpIndex; |
|
1202 |
AppendMenuItemTextWithCFString(action->menu, 0, attr, action->command, &tmpIndex); |
|
1203 |
index = tmpIndex; |
|
1204 |
if (mergedItems.contains(action->command)) |
|
1205 |
AppendMenuItemTextWithCFString(action->menu, 0, kMenuItemAttrSeparator, 0, &tmpIndex); |
|
1206 |
} |
|
1207 |
#else |
|
1208 |
[menu addItem:newItem]; |
|
1209 |
#endif |
|
1210 |
} |
|
1211 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1212 |
QWidget *widget = qmenu ? qmenu->widgetItems.value(action->action) : 0; |
0 | 1213 |
if (widget) { |
1214 |
#ifndef QT_MAC_USE_COCOA |
|
1215 |
ChangeMenuAttributes(action->menu, kMenuAttrDoNotCacheImage, 0); |
|
1216 |
attr = kMenuItemAttrCustomDraw; |
|
1217 |
SetMenuItemProperty(action->menu, index, kMenuCreatorQt, kMenuPropertyWidgetActionWidget, |
|
1218 |
sizeof(QWidget *), &widget); |
|
1219 |
HIViewRef content; |
|
1220 |
HIMenuGetContentView(action->menu, kThemeMenuTypePullDown, &content); |
|
1221 |
||
1222 |
EventHandlerRef eventHandlerRef; |
|
1223 |
InstallMenuEventHandler(action->menu, qt_mac_widget_in_menu_eventHandler, |
|
1224 |
GetEventTypeCount(widget_in_menu_events), |
|
1225 |
widget_in_menu_events, 0, &eventHandlerRef); |
|
1226 |
menu_eventHandlers_hash()->insert(action->menu, eventHandlerRef); |
|
1227 |
||
1228 |
QWidget *menuWidget = 0; |
|
1229 |
GetMenuItemProperty(action->menu, 0, kMenuCreatorQt, kMenuPropertyWidgetMenu, |
|
1230 |
sizeof(menuWidget), 0, &menuWidget); |
|
1231 |
if(!menuWidget) { |
|
1232 |
menuWidget = new QMacNativeWidget(content); |
|
1233 |
SetMenuItemProperty(menu, 0, kMenuCreatorQt, kMenuPropertyWidgetMenu, |
|
1234 |
sizeof(menuWidget), &menuWidget); |
|
1235 |
menuWidget->show(); |
|
1236 |
} |
|
1237 |
widget->setParent(menuWidget); |
|
1238 |
#else |
|
1239 |
QMacNativeWidget *container = new QMacNativeWidget(0); |
|
1240 |
container->resize(widget->sizeHint()); |
|
1241 |
widget->setAttribute(Qt::WA_LayoutUsesWidgetRect); |
|
1242 |
widget->setParent(container); |
|
1243 |
||
1244 |
NSView *containerView = qt_mac_nativeview_for(container); |
|
1245 |
[containerView setAutoresizesSubviews:YES]; |
|
1246 |
[containerView setAutoresizingMask:NSViewWidthSizable]; |
|
1247 |
[qt_mac_nativeview_for(widget) setAutoresizingMask:NSViewWidthSizable]; |
|
1248 |
||
1249 |
[newItem setView:containerView]; |
|
1250 |
container->show(); |
|
1251 |
#endif |
|
1252 |
widget->show(); |
|
1253 |
} |
|
1254 |
||
1255 |
} else { |
|
1256 |
#ifndef QT_MAC_USE_COCOA |
|
1257 |
qt_mac_command_set_enabled(action->menu, action->command, !QApplicationPrivate::modalState()); |
|
1258 |
#else |
|
1259 |
[newItem setEnabled:!QApplicationPrivate::modalState()]; |
|
1260 |
#endif |
|
1261 |
} |
|
1262 |
#ifndef QT_MAC_USE_COCOA |
|
1263 |
SetMenuCommandProperty(action->menu, action->command, kMenuCreatorQt, kMenuPropertyQAction, |
|
1264 |
sizeof(action), &action); |
|
1265 |
#else |
|
1266 |
[newItem setTag:long(static_cast<QAction *>(action->action))]; |
|
1267 |
#endif |
|
1268 |
syncAction(action); |
|
1269 |
} |
|
1270 |
||
1271 |
// return an autoreleased string given a QKeySequence (currently only looks at the first one). |
|
1272 |
NSString *keySequenceToKeyEqivalent(const QKeySequence &accel) |
|
1273 |
{ |
|
1274 |
quint32 accel_key = (accel[0] & ~(Qt::MODIFIER_MASK | Qt::UNICODE_ACCEL)); |
|
1275 |
extern QChar qt_macSymbolForQtKey(int key); // qkeysequence.cpp |
|
1276 |
QChar keyEquiv = qt_macSymbolForQtKey(accel_key); |
|
1277 |
if (keyEquiv.isNull()) { |
|
1278 |
if (accel_key >= Qt::Key_F1 && accel_key <= Qt::Key_F15) |
|
1279 |
keyEquiv = (accel_key - Qt::Key_F1) + NSF1FunctionKey; |
|
1280 |
else |
|
1281 |
keyEquiv = unichar(QChar(accel_key).toLower().unicode()); |
|
1282 |
} |
|
1283 |
return [NSString stringWithCharacters:&keyEquiv.unicode() length:1]; |
|
1284 |
} |
|
1285 |
||
1286 |
// return the cocoa modifier mask for the QKeySequence (currently only looks at the first one). |
|
1287 |
NSUInteger keySequenceModifierMask(const QKeySequence &accel) |
|
1288 |
{ |
|
1289 |
return constructModifierMask(accel[0]); |
|
1290 |
} |
|
1291 |
||
1292 |
void |
|
1293 |
QMenuPrivate::QMacMenuPrivate::syncAction(QMacMenuAction *action) |
|
1294 |
{ |
|
1295 |
if (!action) |
|
1296 |
return; |
|
1297 |
||
1298 |
#ifndef QT_MAC_USE_COCOA |
|
1299 |
const int index = qt_mac_menu_find_action(action->menu, action); |
|
1300 |
if (index == -1) |
|
1301 |
return; |
|
1302 |
#else |
|
1303 |
NSMenuItem *item = action->menuItem; |
|
1304 |
if (!item) |
|
1305 |
return; |
|
1306 |
#endif |
|
1307 |
||
1308 |
#ifndef QT_MAC_USE_COCOA |
|
1309 |
if (!action->action->isVisible()) { |
|
1310 |
ChangeMenuItemAttributes(action->menu, index, kMenuItemAttrHidden, 0); |
|
1311 |
return; |
|
1312 |
} |
|
1313 |
ChangeMenuItemAttributes(action->menu, index, 0, kMenuItemAttrHidden); |
|
1314 |
#else |
|
1315 |
QMacCocoaAutoReleasePool pool; |
|
1316 |
NSMenu *menu = [item menu]; |
|
1317 |
bool actionVisible = action->action->isVisible(); |
|
1318 |
[item setHidden:!actionVisible]; |
|
1319 |
if (!actionVisible) |
|
1320 |
return; |
|
1321 |
#endif |
|
1322 |
||
1323 |
#ifndef QT_MAC_USE_COCOA |
|
1324 |
if (action->action->isSeparator()) { |
|
1325 |
ChangeMenuItemAttributes(action->menu, index, kMenuItemAttrSeparator, 0); |
|
1326 |
return; |
|
1327 |
} |
|
1328 |
ChangeMenuItemAttributes(action->menu, index, 0, kMenuItemAttrSeparator); |
|
1329 |
#else |
|
1330 |
int itemIndex = [menu indexOfItem:item]; |
|
1331 |
Q_ASSERT(itemIndex != -1); |
|
1332 |
if (action->action->isSeparator()) { |
|
1333 |
action->menuItem = [NSMenuItem separatorItem]; |
|
1334 |
[action->menuItem retain]; |
|
1335 |
[menu insertItem: action->menuItem atIndex:itemIndex]; |
|
1336 |
[menu removeItem:item]; |
|
1337 |
[item release]; |
|
1338 |
item = action->menuItem; |
|
1339 |
return; |
|
1340 |
} else if ([item isSeparatorItem]) { |
|
1341 |
// I'm no longer a separator... |
|
1342 |
action->menuItem = createNSMenuItem(action->action->text()); |
|
1343 |
[menu insertItem:action->menuItem atIndex:itemIndex]; |
|
1344 |
[menu removeItem:item]; |
|
1345 |
[item release]; |
|
1346 |
item = action->menuItem; |
|
1347 |
} |
|
1348 |
#endif |
|
1349 |
||
1350 |
//find text (and accel) |
|
1351 |
action->ignore_accel = 0; |
|
1352 |
QString text = action->action->text(); |
|
1353 |
QKeySequence accel = action->action->shortcut(); |
|
1354 |
{ |
|
1355 |
int st = text.lastIndexOf(QLatin1Char('\t')); |
|
1356 |
if (st != -1) { |
|
1357 |
action->ignore_accel = 1; |
|
1358 |
accel = QKeySequence(text.right(text.length()-(st+1))); |
|
1359 |
text.remove(st, text.length()-st); |
|
1360 |
} |
|
1361 |
} |
|
1362 |
{ |
|
1363 |
QString cmd_text = qt_mac_menu_merge_text(action); |
|
1364 |
if (!cmd_text.isEmpty()) { |
|
1365 |
text = cmd_text; |
|
1366 |
accel = qt_mac_menu_merge_accel(action); |
|
1367 |
} |
|
1368 |
} |
|
1369 |
// Show multiple key sequences as part of the menu text. |
|
1370 |
if (accel.count() > 1) |
|
1371 |
text += QLatin1String(" (") + accel.toString(QKeySequence::NativeText) + QLatin1String(")"); |
|
1372 |
||
1373 |
QString finalString = qt_mac_removeMnemonics(text); |
|
1374 |
||
1375 |
#ifndef QT_MAC_USE_COCOA |
|
1376 |
MenuItemDataRec data; |
|
1377 |
memset(&data, '\0', sizeof(data)); |
|
1378 |
||
1379 |
//Carbon text |
|
1380 |
data.whichData |= kMenuItemDataCFString; |
|
1381 |
QCFString cfstring(finalString); // Hold the reference to the end of the function. |
|
1382 |
data.cfText = cfstring; |
|
1383 |
||
1384 |
// Carbon enabled |
|
1385 |
data.whichData |= kMenuItemDataEnabled; |
|
1386 |
data.enabled = action->action->isEnabled(); |
|
1387 |
// Carbon icon |
|
1388 |
data.whichData |= kMenuItemDataIconHandle; |
|
1389 |
if (!action->action->icon().isNull() |
|
1390 |
&& action->action->isIconVisibleInMenu()) { |
|
1391 |
data.iconType = kMenuIconRefType; |
|
1392 |
data.iconHandle = (Handle)qt_mac_create_iconref(action->action->icon().pixmap(16, QIcon::Normal)); |
|
1393 |
} else { |
|
1394 |
data.iconType = kMenuNoIcon; |
|
1395 |
} |
|
1396 |
if (action->action->font().resolve()) { // Carbon font |
|
1397 |
if (action->action->font().bold()) |
|
1398 |
data.style |= bold; |
|
1399 |
if (action->action->font().underline()) |
|
1400 |
data.style |= underline; |
|
1401 |
if (action->action->font().italic()) |
|
1402 |
data.style |= italic; |
|
1403 |
if (data.style) |
|
1404 |
data.whichData |= kMenuItemDataStyle; |
|
1405 |
data.whichData |= kMenuItemDataFontID; |
|
1406 |
data.fontID = action->action->font().macFontID(); |
|
1407 |
} |
|
1408 |
#else |
|
1409 |
// Cocoa Font and title |
|
1410 |
if (action->action->font().resolve()) { |
|
1411 |
const QFont &actionFont = action->action->font(); |
|
1412 |
NSFont *customMenuFont = [NSFont fontWithName:qt_mac_QStringToNSString(actionFont.family()) |
|
1413 |
size:actionFont.pointSize()]; |
|
1414 |
NSArray *keys = [NSArray arrayWithObjects:NSFontAttributeName, nil]; |
|
1415 |
NSArray *objects = [NSArray arrayWithObjects:customMenuFont, nil]; |
|
1416 |
NSDictionary *attributes = [NSDictionary dictionaryWithObjects:objects forKeys:keys]; |
|
1417 |
NSAttributedString *str = [[[NSAttributedString alloc] initWithString:qt_mac_QStringToNSString(finalString) |
|
1418 |
attributes:attributes] autorelease]; |
|
1419 |
[item setAttributedTitle: str]; |
|
1420 |
} else { |
|
1421 |
[item setTitle: qt_mac_QStringToNSString(finalString)]; |
|
1422 |
} |
|
1423 |
[item setTitle:qt_mac_QStringToNSString(qt_mac_removeMnemonics(text))]; |
|
1424 |
||
1425 |
// Cocoa Enabled |
|
1426 |
[item setEnabled: action->action->isEnabled()]; |
|
1427 |
||
1428 |
// Cocoa icon |
|
1429 |
NSImage *nsimage = 0; |
|
1430 |
if (!action->action->icon().isNull() && action->action->isIconVisibleInMenu()) { |
|
1431 |
nsimage = static_cast<NSImage *>(qt_mac_create_nsimage(action->action->icon().pixmap(16, QIcon::Normal))); |
|
1432 |
} |
|
1433 |
[item setImage:nsimage]; |
|
1434 |
[nsimage release]; |
|
1435 |
#endif |
|
1436 |
||
1437 |
if (action->action->menu()) { //submenu |
|
1438 |
#ifndef QT_MAC_USE_COCOA |
|
1439 |
data.whichData |= kMenuItemDataSubmenuHandle; |
|
1440 |
data.submenuHandle = action->action->menu()->macMenu(); |
|
1441 |
QWidget *caused = 0; |
|
1442 |
GetMenuItemProperty(action->menu, 0, kMenuCreatorQt, kMenuPropertyQWidget, sizeof(caused), 0, &caused); |
|
1443 |
SetMenuItemProperty(data.submenuHandle, 0, kMenuCreatorQt, kMenuPropertyCausedQWidget, sizeof(caused), &caused); |
|
1444 |
#else |
|
1445 |
NSMenu *subMenu = static_cast<NSMenu *>(action->action->menu()->macMenu()); |
|
1446 |
if ([subMenu supermenu] && [subMenu supermenu] != [item menu]) { |
|
1447 |
// The menu is already a sub-menu of another one. Cocoa will throw an exception, |
|
1448 |
// in such cases. For the time being, a new QMenu with same set of actions is the |
|
1449 |
// only workaround. |
|
1450 |
action->action->setEnabled(false); |
|
1451 |
} else { |
|
1452 |
[item setSubmenu:subMenu]; |
|
1453 |
} |
|
1454 |
#endif |
|
1455 |
} else { //respect some other items |
|
1456 |
#ifndef QT_MAC_USE_COCOA |
|
1457 |
//shortcuts (say we are setting them all so that we can also clear them). |
|
1458 |
data.whichData |= kMenuItemDataCmdKey; |
|
1459 |
data.whichData |= kMenuItemDataCmdKeyModifiers; |
|
1460 |
data.whichData |= kMenuItemDataCmdKeyGlyph; |
|
1461 |
if (accel.count() == 1) { |
|
1462 |
qt_mac_get_accel(accel[0], (quint32*)&data.cmdKeyModifiers, (quint32*)&data.cmdKeyGlyph); |
|
1463 |
if (data.cmdKeyGlyph == 0) |
|
1464 |
data.cmdKey = (UniChar)accel[0]; |
|
1465 |
} |
|
1466 |
#else |
|
1467 |
[item setSubmenu:0]; |
|
1468 |
// No key equivalent set for multiple key QKeySequence. |
|
1469 |
if (accel.count() == 1) { |
|
1470 |
[item setKeyEquivalent:keySequenceToKeyEqivalent(accel)]; |
|
1471 |
[item setKeyEquivalentModifierMask:keySequenceModifierMask(accel)]; |
|
1472 |
} else { |
|
1473 |
[item setKeyEquivalent:@""]; |
|
1474 |
[item setKeyEquivalentModifierMask:NSCommandKeyMask]; |
|
1475 |
} |
|
1476 |
#endif |
|
1477 |
} |
|
1478 |
#ifndef QT_MAC_USE_COCOA |
|
1479 |
//mark glyph |
|
1480 |
data.whichData |= kMenuItemDataMark; |
|
1481 |
if (action->action->isChecked()) { |
|
1482 |
#if 0 |
|
1483 |
if (action->action->actionGroup() && |
|
1484 |
action->action->actionGroup()->isExclusive()) |
|
1485 |
data.mark = diamondMark; |
|
1486 |
else |
|
1487 |
#endif |
|
1488 |
data.mark = checkMark; |
|
1489 |
} else { |
|
1490 |
data.mark = noMark; |
|
1491 |
} |
|
1492 |
||
1493 |
//actually set it |
|
1494 |
SetMenuItemData(action->menu, action->command, true, &data); |
|
1495 |
||
1496 |
// Free up memory |
|
1497 |
if (data.iconHandle) |
|
1498 |
ReleaseIconRef(IconRef(data.iconHandle)); |
|
1499 |
#else |
|
1500 |
//mark glyph |
|
1501 |
[item setState:action->action->isChecked() ? NSOnState : NSOffState]; |
|
1502 |
#endif |
|
1503 |
} |
|
1504 |
||
1505 |
void |
|
1506 |
QMenuPrivate::QMacMenuPrivate::removeAction(QMacMenuAction *action) |
|
1507 |
{ |
|
1508 |
if (!action) |
|
1509 |
return; |
|
1510 |
#ifndef QT_MAC_USE_COCOA |
|
1511 |
if (action->command == kHICommandQuit || action->command == kHICommandPreferences) |
|
1512 |
qt_mac_command_set_enabled(action->menu, action->command, false); |
|
1513 |
else |
|
1514 |
DeleteMenuItem(action->menu, qt_mac_menu_find_action(action->menu, action)); |
|
1515 |
#else |
|
1516 |
QMacCocoaAutoReleasePool pool; |
|
1517 |
if (action->merged) { |
|
1518 |
if (reinterpret_cast<QAction *>([action->menuItem tag]) == action->action) { |
|
1519 |
QT_MANGLE_NAMESPACE(QCocoaMenuLoader) *loader = getMenuLoader(); |
|
1520 |
[action->menuItem setEnabled:false]; |
|
1521 |
if (action->menuItem != [loader quitMenuItem] |
|
1522 |
&& action->menuItem != [loader preferencesMenuItem]) { |
|
1523 |
[[action->menuItem menu] removeItem:action->menuItem]; |
|
1524 |
} |
|
1525 |
} |
|
1526 |
} else { |
|
1527 |
[[action->menuItem menu] removeItem:action->menuItem]; |
|
1528 |
} |
|
1529 |
#endif |
|
1530 |
actionItems.removeAll(action); |
|
1531 |
} |
|
1532 |
||
1533 |
OSMenuRef |
|
1534 |
QMenuPrivate::macMenu(OSMenuRef merge) |
|
1535 |
{ |
|
1536 |
Q_UNUSED(merge); |
|
1537 |
Q_Q(QMenu); |
|
1538 |
if (mac_menu && mac_menu->menu) |
|
1539 |
return mac_menu->menu; |
|
1540 |
if (!mac_menu) |
|
1541 |
mac_menu = new QMacMenuPrivate; |
|
1542 |
mac_menu->menu = qt_mac_create_menu(q); |
|
1543 |
if (merge) { |
|
1544 |
#ifndef QT_MAC_USE_COCOA |
|
1545 |
SetMenuItemProperty(mac_menu->menu, 0, kMenuCreatorQt, kMenuPropertyMergeMenu, sizeof(merge), &merge); |
|
1546 |
#else |
|
1547 |
mergeMenuHash.insert(mac_menu->menu, merge); |
|
1548 |
#endif |
|
1549 |
} |
|
1550 |
QList<QAction*> items = q->actions(); |
|
1551 |
for(int i = 0; i < items.count(); i++) |
|
1552 |
mac_menu->addAction(items[i], 0, this); |
|
1553 |
syncSeparatorsCollapsible(collapsibleSeparators); |
|
1554 |
return mac_menu->menu; |
|
1555 |
} |
|
1556 |
||
1557 |
/*! |
|
1558 |
\internal |
|
1559 |
*/ |
|
1560 |
void |
|
1561 |
QMenuPrivate::syncSeparatorsCollapsible(bool collapse) |
|
1562 |
{ |
|
1563 |
#ifndef QT_MAC_USE_COCOA |
|
1564 |
if (collapse) |
|
1565 |
ChangeMenuAttributes(mac_menu->menu, kMenuAttrCondenseSeparators, 0); |
|
1566 |
else |
|
1567 |
ChangeMenuAttributes(mac_menu->menu, 0, kMenuAttrCondenseSeparators); |
|
1568 |
#else |
|
1569 |
qt_mac_menu_collapseSeparators(mac_menu->menu, collapse); |
|
1570 |
#endif |
|
1571 |
} |
|
1572 |
||
1573 |
||
1574 |
||
1575 |
/*! |
|
1576 |
\internal |
|
1577 |
*/ |
|
1578 |
void QMenuPrivate::setMacMenuEnabled(bool enable) |
|
1579 |
{ |
|
1580 |
if (!macMenu(0)) |
|
1581 |
return; |
|
1582 |
||
1583 |
QMacCocoaAutoReleasePool pool; |
|
1584 |
if (enable) { |
|
1585 |
for (int i = 0; i < mac_menu->actionItems.count(); ++i) { |
|
1586 |
QMacMenuAction *menuItem = mac_menu->actionItems.at(i); |
|
1587 |
if (menuItem && menuItem->action && menuItem->action->isEnabled()) { |
|
1588 |
#ifndef QT_MAC_USE_COCOA |
|
1589 |
// Only enable those items which contains an enabled QAction. |
|
1590 |
// i == 0 -> the menu itself, hence i + 1 for items. |
|
1591 |
EnableMenuItem(mac_menu->menu, i + 1); |
|
1592 |
#else |
|
1593 |
[menuItem->menuItem setEnabled:true]; |
|
1594 |
#endif |
|
1595 |
} |
|
1596 |
} |
|
1597 |
} else { |
|
1598 |
#ifndef QT_MAC_USE_COCOA |
|
1599 |
DisableAllMenuItems(mac_menu->menu); |
|
1600 |
#else |
|
1601 |
NSMenu *menu = mac_menu->menu; |
|
1602 |
for (NSMenuItem *item in [menu itemArray]) { |
|
1603 |
[item setEnabled:false]; |
|
1604 |
} |
|
1605 |
#endif |
|
1606 |
} |
|
1607 |
} |
|
1608 |
||
1609 |
/*! |
|
1610 |
\internal |
|
1611 |
||
1612 |
This function will return the OSMenuRef used to create the native menu bar |
|
1613 |
bindings. |
|
1614 |
||
1615 |
If Qt is built against Carbon, the OSMenuRef is a MenuRef that can be used |
|
1616 |
with Carbon's Menu Manager API. |
|
1617 |
||
1618 |
If Qt is built against Cocoa, the OSMenuRef is a NSMenu pointer. |
|
1619 |
||
1620 |
\warning This function is not portable. |
|
1621 |
||
1622 |
\sa QMenuBar::macMenu() |
|
1623 |
*/ |
|
1624 |
OSMenuRef QMenu::macMenu(OSMenuRef merge) { return d_func()->macMenu(merge); } |
|
1625 |
||
1626 |
/***************************************************************************** |
|
1627 |
QMenuBar bindings |
|
1628 |
*****************************************************************************/ |
|
1629 |
typedef QHash<QWidget *, QMenuBar *> MenuBarHash; |
|
1630 |
Q_GLOBAL_STATIC(MenuBarHash, menubars) |
|
1631 |
static QMenuBar *fallback = 0; |
|
1632 |
QMenuBarPrivate::QMacMenuBarPrivate::QMacMenuBarPrivate() : menu(0), apple_menu(0) |
|
1633 |
{ |
|
1634 |
} |
|
1635 |
||
1636 |
QMenuBarPrivate::QMacMenuBarPrivate::~QMacMenuBarPrivate() |
|
1637 |
{ |
|
1638 |
for(QList<QMacMenuAction*>::Iterator it = actionItems.begin(); it != actionItems.end(); ++it) |
|
1639 |
delete (*it); |
|
1640 |
#ifndef QT_MAC_USE_COCOA |
|
1641 |
if (apple_menu) { |
|
1642 |
QMenuMergeList *list = 0; |
|
1643 |
GetMenuItemProperty(apple_menu, 0, kMenuCreatorQt, kMenuPropertyMergeList, |
|
1644 |
sizeof(list), 0, &list); |
|
1645 |
if (list) { |
|
1646 |
RemoveMenuItemProperty(apple_menu, 0, kMenuCreatorQt, kMenuPropertyMergeList); |
|
1647 |
delete list; |
|
1648 |
} |
|
1649 |
ReleaseMenu(apple_menu); |
|
1650 |
} |
|
1651 |
if (menu) |
|
1652 |
ReleaseMenu(menu); |
|
1653 |
#else |
|
1654 |
[apple_menu release]; |
|
1655 |
[menu release]; |
|
1656 |
#endif |
|
1657 |
} |
|
1658 |
||
1659 |
void |
|
1660 |
QMenuBarPrivate::QMacMenuBarPrivate::addAction(QAction *a, QMacMenuAction *before) |
|
1661 |
{ |
|
1662 |
if (a->isSeparator() || !menu) |
|
1663 |
return; |
|
1664 |
QMacMenuAction *action = new QMacMenuAction; |
|
1665 |
action->action = a; |
|
1666 |
action->ignore_accel = 1; |
|
1667 |
#ifndef QT_MAC_USE_COCOA |
|
1668 |
action->command = qt_mac_menu_static_cmd_id++; |
|
1669 |
#endif |
|
1670 |
addAction(action, before); |
|
1671 |
} |
|
1672 |
||
1673 |
void |
|
1674 |
QMenuBarPrivate::QMacMenuBarPrivate::addAction(QMacMenuAction *action, QMacMenuAction *before) |
|
1675 |
{ |
|
1676 |
if (!action || !menu) |
|
1677 |
return; |
|
1678 |
||
1679 |
int before_index = actionItems.indexOf(before); |
|
1680 |
if (before_index < 0) { |
|
1681 |
before = 0; |
|
1682 |
before_index = actionItems.size(); |
|
1683 |
} |
|
1684 |
actionItems.insert(before_index, action); |
|
1685 |
||
1686 |
MenuItemIndex index = actionItems.size()-1; |
|
1687 |
||
1688 |
action->menu = menu; |
|
1689 |
#ifdef QT_MAC_USE_COCOA |
|
1690 |
QMacCocoaAutoReleasePool pool; |
|
1691 |
[action->menu retain]; |
|
1692 |
NSMenuItem *newItem = createNSMenuItem(action->action->text()); |
|
1693 |
action->menuItem = newItem; |
|
1694 |
#endif |
|
1695 |
if (before) { |
|
1696 |
#ifndef QT_MAC_USE_COCOA |
|
1697 |
InsertMenuItemTextWithCFString(action->menu, 0, qMax(1, before_index+1), 0, action->command); |
|
1698 |
#else |
|
1699 |
[menu insertItem:newItem atIndex:qMax(1, before_index + 1)]; |
|
1700 |
#endif |
|
1701 |
index = before_index; |
|
1702 |
} else { |
|
1703 |
#ifndef QT_MAC_USE_COCOA |
|
1704 |
AppendMenuItemTextWithCFString(action->menu, 0, 0, action->command, &index); |
|
1705 |
#else |
|
1706 |
[menu addItem:newItem]; |
|
1707 |
#endif |
|
1708 |
} |
|
1709 |
#ifndef QT_MAC_USE_COCOA |
|
1710 |
SetMenuItemProperty(action->menu, index, kMenuCreatorQt, kMenuPropertyQAction, sizeof(action), |
|
1711 |
&action); |
|
1712 |
#else |
|
1713 |
[newItem setTag:long(static_cast<QAction *>(action->action))]; |
|
1714 |
#endif |
|
1715 |
syncAction(action); |
|
1716 |
} |
|
1717 |
||
1718 |
void |
|
1719 |
QMenuBarPrivate::QMacMenuBarPrivate::syncAction(QMacMenuAction *action) |
|
1720 |
{ |
|
1721 |
if (!action || !menu) |
|
1722 |
return; |
|
1723 |
#ifndef QT_MAC_USE_COCOA |
|
1724 |
const int index = qt_mac_menu_find_action(action->menu, action); |
|
1725 |
#else |
|
1726 |
QMacCocoaAutoReleasePool pool; |
|
1727 |
NSMenuItem *item = action->menuItem; |
|
1728 |
#endif |
|
1729 |
||
1730 |
OSMenuRef submenu = 0; |
|
1731 |
bool release_submenu = false; |
|
1732 |
if (action->action->menu()) { |
|
1733 |
if ((submenu = action->action->menu()->macMenu(apple_menu))) { |
|
1734 |
#ifndef QT_MAC_USE_COCOA |
|
1735 |
QWidget *caused = 0; |
|
1736 |
GetMenuItemProperty(action->menu, 0, kMenuCreatorQt, kMenuPropertyQWidget, sizeof(caused), 0, &caused); |
|
1737 |
SetMenuItemProperty(submenu, 0, kMenuCreatorQt, kMenuPropertyCausedQWidget, sizeof(caused), &caused); |
|
1738 |
#else |
|
1739 |
if ([submenu supermenu] && [submenu supermenu] != [item menu]) |
|
1740 |
return; |
|
1741 |
else |
|
1742 |
[item setSubmenu:submenu]; |
|
1743 |
#endif |
|
1744 |
} |
|
1745 |
#ifndef QT_MAC_USE_COCOA |
|
1746 |
} else { // create a submenu to act as menu |
|
1747 |
release_submenu = true; |
|
1748 |
CreateNewMenu(0, 0, &submenu); |
|
1749 |
#endif |
|
1750 |
} |
|
1751 |
||
1752 |
if (submenu) { |
|
1753 |
bool visible = actualMenuItemVisibility(this, action); |
|
1754 |
#ifndef QT_MAC_USE_COCOA |
|
1755 |
SetMenuItemHierarchicalMenu(action->menu, index, submenu); |
|
1756 |
SetMenuTitleWithCFString(submenu, QCFString(qt_mac_removeMnemonics(action->action->text()))); |
|
1757 |
if (visible) |
|
1758 |
ChangeMenuAttributes(submenu, 0, kMenuAttrHidden); |
|
1759 |
else |
|
1760 |
ChangeMenuAttributes(submenu, kMenuAttrHidden, 0); |
|
1761 |
#else |
|
1762 |
[item setSubmenu: submenu]; |
|
1763 |
[submenu setTitle:qt_mac_QStringToNSString(qt_mac_removeMnemonics(action->action->text()))]; |
|
1764 |
syncNSMenuItemVisiblity(item, visible); |
|
1765 |
#endif |
|
1766 |
if (release_submenu) { //no pointers to it |
|
1767 |
#ifndef QT_MAC_USE_COCOA |
|
1768 |
ReleaseMenu(submenu); |
|
1769 |
#else |
|
1770 |
[submenu release]; |
|
1771 |
#endif |
|
1772 |
} |
|
1773 |
} else { |
|
1774 |
qWarning("QMenu: No OSMenuRef created for popup menu"); |
|
1775 |
} |
|
1776 |
} |
|
1777 |
||
1778 |
void |
|
1779 |
QMenuBarPrivate::QMacMenuBarPrivate::removeAction(QMacMenuAction *action) |
|
1780 |
{ |
|
1781 |
if (!action || !menu) |
|
1782 |
return; |
|
1783 |
#ifndef QT_MAC_USE_COCOA |
|
1784 |
DeleteMenuItem(action->menu, qt_mac_menu_find_action(action->menu, action)); |
|
1785 |
#else |
|
1786 |
QMacCocoaAutoReleasePool pool; |
|
1787 |
[action->menu removeItem:action->menuItem]; |
|
1788 |
#endif |
|
1789 |
actionItems.removeAll(action); |
|
1790 |
} |
|
1791 |
||
1792 |
bool QMenuBarPrivate::macWidgetHasNativeMenubar(QWidget *widget) |
|
1793 |
{ |
|
1794 |
// This function is different from q->isNativeMenuBar(), as |
|
1795 |
// it returns true only if a native menu bar is actually |
|
1796 |
// _created_. |
|
1797 |
if (!widget) |
|
1798 |
return false; |
|
1799 |
return menubars()->contains(widget->window()); |
|
1800 |
} |
|
1801 |
||
1802 |
void |
|
1803 |
QMenuBarPrivate::macCreateMenuBar(QWidget *parent) |
|
1804 |
{ |
|
1805 |
Q_Q(QMenuBar); |
|
1806 |
static int dontUseNativeMenuBar = -1; |
|
1807 |
// We call the isNativeMenuBar function here |
|
1808 |
// because that will make sure that local overrides |
|
1809 |
// are dealt with correctly. q->isNativeMenuBar() will, if not |
|
1810 |
// overridden, depend on the attribute Qt::AA_DontUseNativeMenuBar: |
|
1811 |
bool qt_mac_no_native_menubar = !q->isNativeMenuBar(); |
|
1812 |
if (qt_mac_no_native_menubar == false && dontUseNativeMenuBar < 0) { |
|
1813 |
// The menubar is set to be native. Let's check (one time only |
|
1814 |
// for all menubars) if this is OK with the rest of the environment. |
|
1815 |
// As a result, Qt::AA_DontUseNativeMenuBar is set. NB: the application |
|
1816 |
// might still choose to not respect, or change, this flag. |
|
1817 |
bool isPlugin = QApplication::testAttribute(Qt::AA_MacPluginApplication); |
|
1818 |
bool environmentSaysNo = !qgetenv("QT_MAC_NO_NATIVE_MENUBAR").isEmpty(); |
|
1819 |
dontUseNativeMenuBar = isPlugin || environmentSaysNo; |
|
1820 |
QApplication::instance()->setAttribute(Qt::AA_DontUseNativeMenuBar, dontUseNativeMenuBar); |
|
1821 |
qt_mac_no_native_menubar = !q->isNativeMenuBar(); |
|
1822 |
} |
|
1823 |
if (qt_mac_no_native_menubar == false) { |
|
1824 |
// INVARIANT: Use native menubar. |
|
1825 |
extern void qt_event_request_menubarupdate(); //qapplication_mac.cpp |
|
1826 |
qt_event_request_menubarupdate(); |
|
1827 |
if (!parent && !fallback) { |
|
1828 |
fallback = q; |
|
1829 |
mac_menubar = new QMacMenuBarPrivate; |
|
1830 |
} else if (parent && parent->isWindow()) { |
|
1831 |
menubars()->insert(q->window(), q); |
|
1832 |
mac_menubar = new QMacMenuBarPrivate; |
|
1833 |
} |
|
1834 |
} |
|
1835 |
} |
|
1836 |
||
1837 |
void QMenuBarPrivate::macDestroyMenuBar() |
|
1838 |
{ |
|
1839 |
Q_Q(QMenuBar); |
|
1840 |
QMacCocoaAutoReleasePool pool; |
|
1841 |
if (fallback == q) |
|
1842 |
fallback = 0; |
|
1843 |
delete mac_menubar; |
|
1844 |
QWidget *tlw = q->window(); |
|
1845 |
menubars()->remove(tlw); |
|
1846 |
mac_menubar = 0; |
|
1847 |
||
1848 |
if (qt_mac_current_menubar.qmenubar == q) { |
|
5
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
1849 |
#ifdef QT_MAC_USE_COCOA |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
1850 |
QT_MANGLE_NAMESPACE(QCocoaMenuLoader) *loader = getMenuLoader(); |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
1851 |
[loader removeActionsFromAppMenu]; |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
1852 |
#else |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
1853 |
cancelAllMenuTracking(); |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
1854 |
#endif |
0 | 1855 |
extern void qt_event_request_menubarupdate(); //qapplication_mac.cpp |
1856 |
qt_event_request_menubarupdate(); |
|
1857 |
} |
|
1858 |
} |
|
1859 |
||
1860 |
OSMenuRef QMenuBarPrivate::macMenu() |
|
1861 |
{ |
|
1862 |
Q_Q(QMenuBar); |
|
1863 |
if (!q->isNativeMenuBar() || !mac_menubar) { |
|
1864 |
return 0; |
|
1865 |
} else if (!mac_menubar->menu) { |
|
1866 |
mac_menubar->menu = qt_mac_create_menu(q); |
|
1867 |
ProcessSerialNumber mine, front; |
|
1868 |
if (GetCurrentProcess(&mine) == noErr && GetFrontProcess(&front) == noErr) { |
|
1869 |
if (!qt_mac_no_menubar_merge && !mac_menubar->apple_menu) { |
|
1870 |
mac_menubar->apple_menu = qt_mac_create_menu(q); |
|
1871 |
#ifndef QT_MAC_USE_COCOA |
|
1872 |
MenuItemIndex index; |
|
1873 |
AppendMenuItemTextWithCFString(mac_menubar->menu, 0, 0, 0, &index); |
|
1874 |
||
1875 |
SetMenuTitleWithCFString(mac_menubar->apple_menu, QCFString(QString(QChar(0x14)))); |
|
1876 |
SetMenuItemHierarchicalMenu(mac_menubar->menu, index, mac_menubar->apple_menu); |
|
1877 |
SetMenuItemProperty(mac_menubar->apple_menu, 0, kMenuCreatorQt, kMenuPropertyQWidget, sizeof(q), &q); |
|
1878 |
#else |
|
1879 |
[mac_menubar->apple_menu setTitle:qt_mac_QStringToNSString(QString(QChar(0x14)))]; |
|
1880 |
NSMenuItem *apple_menuItem = [[NSMenuItem alloc] init]; |
|
1881 |
[apple_menuItem setSubmenu:mac_menubar->menu]; |
|
1882 |
[mac_menubar->apple_menu addItem:apple_menuItem]; |
|
1883 |
[apple_menuItem release]; |
|
1884 |
#endif |
|
1885 |
} |
|
1886 |
if (mac_menubar->apple_menu) { |
|
1887 |
#ifndef QT_MAC_USE_COCOA |
|
1888 |
SetMenuItemProperty(mac_menubar->menu, 0, kMenuCreatorQt, kMenuPropertyMergeMenu, |
|
1889 |
sizeof(mac_menubar->apple_menu), &mac_menubar->apple_menu); |
|
1890 |
#else |
|
1891 |
QMenuPrivate::mergeMenuHash.insert(mac_menubar->menu, mac_menubar->apple_menu); |
|
1892 |
#endif |
|
1893 |
} |
|
1894 |
QList<QAction*> items = q->actions(); |
|
1895 |
for(int i = 0; i < items.count(); i++) |
|
1896 |
mac_menubar->addAction(items[i]); |
|
1897 |
} |
|
1898 |
} |
|
1899 |
return mac_menubar->menu; |
|
1900 |
} |
|
1901 |
||
1902 |
/*! |
|
1903 |
\internal |
|
1904 |
||
1905 |
This function will return the OSMenuRef used to create the native menu bar |
|
1906 |
bindings. This OSMenuRef is then set as the root menu for the Menu |
|
1907 |
Manager. |
|
1908 |
||
1909 |
\warning This function is not portable. |
|
1910 |
||
1911 |
\sa QMenu::macMenu() |
|
1912 |
*/ |
|
1913 |
OSMenuRef QMenuBar::macMenu() { return d_func()->macMenu(); } |
|
1914 |
||
1915 |
/* ! |
|
1916 |
\internal |
|
1917 |
Ancestor function that crosses windows (QWidget::isAncestorOf |
|
1918 |
only considers widgets within the same window). |
|
1919 |
*/ |
|
1920 |
static bool qt_mac_is_ancestor(QWidget* possibleAncestor, QWidget *child) |
|
1921 |
{ |
|
1922 |
if (!possibleAncestor) |
|
1923 |
return false; |
|
1924 |
||
1925 |
QWidget * current = child->parentWidget(); |
|
1926 |
while (current != 0) { |
|
1927 |
if (current == possibleAncestor) |
|
1928 |
return true; |
|
1929 |
current = current->parentWidget(); |
|
1930 |
} |
|
1931 |
return false; |
|
1932 |
} |
|
1933 |
||
1934 |
/* ! |
|
1935 |
\internal |
|
1936 |
Returns true if the entries of menuBar should be disabled, |
|
1937 |
based on the modality type of modalWidget. |
|
1938 |
*/ |
|
1939 |
static bool qt_mac_should_disable_menu(QMenuBar *menuBar, QWidget *modalWidget) |
|
1940 |
{ |
|
1941 |
if (modalWidget == 0 || menuBar == 0) |
|
1942 |
return false; |
|
1943 |
||
1944 |
// If there is an application modal window on |
|
1945 |
// screen, the entries of the menubar should be disabled: |
|
1946 |
QWidget *w = modalWidget; |
|
1947 |
while (w) { |
|
1948 |
if (w->isVisible() && w->windowModality() == Qt::ApplicationModal) |
|
1949 |
return true; |
|
1950 |
w = w->parentWidget(); |
|
1951 |
} |
|
1952 |
||
1953 |
// INVARIANT: modalWidget is window modal. Disable menu entries |
|
1954 |
// if the menu bar belongs to an ancestor of modalWidget: |
|
1955 |
return qt_mac_is_ancestor(menuBar->parentWidget(), modalWidget); |
|
1956 |
} |
|
1957 |
||
1958 |
/*! |
|
1959 |
\internal |
|
1960 |
||
1961 |
This function will update the current menu bar and set it as the |
|
1962 |
active menu bar in the Menu Manager. |
|
1963 |
||
1964 |
\warning This function is not portable. |
|
1965 |
||
1966 |
\sa QMenu::macMenu(), QMenuBar::macMenu() |
|
1967 |
*/ |
|
1968 |
bool QMenuBar::macUpdateMenuBar() |
|
1969 |
{ |
|
1970 |
cancelAllMenuTracking(); |
|
1971 |
QMenuBar *mb = 0; |
|
1972 |
//find a menu bar |
|
1973 |
QWidget *w = qApp->activeWindow(); |
|
1974 |
||
1975 |
if (!w) { |
|
1976 |
QWidgetList tlws = QApplication::topLevelWidgets(); |
|
1977 |
for(int i = 0; i < tlws.size(); ++i) { |
|
1978 |
QWidget *tlw = tlws.at(i); |
|
1979 |
if ((tlw->isVisible() && tlw->windowType() != Qt::Tool && |
|
1980 |
tlw->windowType() != Qt::Popup)) { |
|
1981 |
w = tlw; |
|
1982 |
break; |
|
1983 |
} |
|
1984 |
} |
|
1985 |
} |
|
1986 |
if (w) { |
|
1987 |
mb = menubars()->value(w); |
|
1988 |
#ifndef QT_NO_MAINWINDOW |
|
1989 |
QDockWidget *dw = qobject_cast<QDockWidget *>(w); |
|
1990 |
if (!mb && dw) { |
|
1991 |
QMainWindow *mw = qobject_cast<QMainWindow *>(dw->parentWidget()); |
|
1992 |
if (mw && (mb = menubars()->value(mw))) |
|
1993 |
w = mw; |
|
1994 |
} |
|
1995 |
#endif |
|
1996 |
while(w && !mb) |
|
1997 |
mb = menubars()->value((w = w->parentWidget())); |
|
1998 |
} |
|
1999 |
if (!mb) |
|
2000 |
mb = fallback; |
|
2001 |
//now set it |
|
2002 |
bool ret = false; |
|
2003 |
if (mb && mb->isNativeMenuBar()) { |
|
2004 |
#ifdef QT_MAC_USE_COCOA |
|
2005 |
QMacCocoaAutoReleasePool pool; |
|
2006 |
#endif |
|
2007 |
if (OSMenuRef menu = mb->macMenu()) { |
|
2008 |
#ifndef QT_MAC_USE_COCOA |
|
2009 |
SetRootMenu(menu); |
|
2010 |
#else |
|
2011 |
QT_MANGLE_NAMESPACE(QCocoaMenuLoader) *loader = getMenuLoader(); |
|
2012 |
[loader ensureAppMenuInMenu:menu]; |
|
2013 |
[NSApp setMainMenu:menu]; |
|
2014 |
syncMenuBarItemsVisiblity(mb->d_func()->mac_menubar); |
|
2015 |
||
2016 |
if (OSMenuRef tmpMerge = QMenuPrivate::mergeMenuHash.value(menu)) { |
|
2017 |
if (QMenuMergeList *mergeList |
|
2018 |
= QMenuPrivate::mergeMenuItemsHash.value(tmpMerge)) { |
|
2019 |
const int mergeListSize = mergeList->size(); |
|
2020 |
||
2021 |
for (int i = 0; i < mergeListSize; ++i) { |
|
2022 |
const QMenuMergeItem &mergeItem = mergeList->at(i); |
|
2023 |
// Ideally we would call QMenuPrivate::syncAction, but that requires finding |
|
2024 |
// the original QMen and likely doing more work than we need. |
|
2025 |
// For example, enabled is handled below. |
|
2026 |
[mergeItem.menuItem setTag:reinterpret_cast<long>( |
|
2027 |
static_cast<QAction *>(mergeItem.action->action))]; |
|
2028 |
[mergeItem.menuItem setHidden:!(mergeItem.action->action->isVisible())]; |
|
2029 |
} |
|
2030 |
} |
|
2031 |
} |
|
2032 |
#endif |
|
2033 |
QWidget *modalWidget = qApp->activeModalWidget(); |
|
2034 |
if (mb != menubars()->value(modalWidget)) { |
|
2035 |
qt_mac_set_modal_state(menu, qt_mac_should_disable_menu(mb, modalWidget)); |
|
2036 |
} |
|
2037 |
} |
|
2038 |
qt_mac_current_menubar.qmenubar = mb; |
|
2039 |
qt_mac_current_menubar.modal = QApplicationPrivate::modalState(); |
|
2040 |
ret = true; |
|
2041 |
} else if (qt_mac_current_menubar.qmenubar && qt_mac_current_menubar.qmenubar->isNativeMenuBar()) { |
|
2042 |
const bool modal = QApplicationPrivate::modalState(); |
|
2043 |
if (modal != qt_mac_current_menubar.modal) { |
|
2044 |
ret = true; |
|
2045 |
if (OSMenuRef menu = qt_mac_current_menubar.qmenubar->macMenu()) { |
|
2046 |
#ifndef QT_MAC_USE_COCOA |
|
2047 |
SetRootMenu(menu); |
|
2048 |
#else |
|
2049 |
QT_MANGLE_NAMESPACE(QCocoaMenuLoader) *loader = getMenuLoader(); |
|
2050 |
[loader ensureAppMenuInMenu:menu]; |
|
2051 |
[NSApp setMainMenu:menu]; |
|
2052 |
syncMenuBarItemsVisiblity(qt_mac_current_menubar.qmenubar->d_func()->mac_menubar); |
|
2053 |
#endif |
|
2054 |
QWidget *modalWidget = qApp->activeModalWidget(); |
|
2055 |
if (qt_mac_current_menubar.qmenubar != menubars()->value(modalWidget)) { |
|
2056 |
qt_mac_set_modal_state(menu, qt_mac_should_disable_menu(mb, modalWidget)); |
|
2057 |
} |
|
2058 |
} |
|
2059 |
qt_mac_current_menubar.modal = modal; |
|
2060 |
} |
|
2061 |
} |
|
2062 |
if(!ret) |
|
2063 |
qt_mac_clear_menubar(); |
|
2064 |
return ret; |
|
2065 |
} |
|
2066 |
||
2067 |
QHash<OSMenuRef, OSMenuRef> QMenuPrivate::mergeMenuHash; |
|
2068 |
QHash<OSMenuRef, QMenuMergeList*> QMenuPrivate::mergeMenuItemsHash; |
|
2069 |
||
2070 |
bool QMenuPrivate::QMacMenuPrivate::merged(const QAction *action) const |
|
2071 |
{ |
|
2072 |
#ifndef QT_MAC_USE_COCOA |
|
2073 |
MenuRef merge = 0; |
|
2074 |
GetMenuItemProperty(menu, 0, kMenuCreatorQt, kMenuPropertyMergeMenu, |
|
2075 |
sizeof(merge), 0, &merge); |
|
2076 |
if (merge) { |
|
2077 |
QMenuMergeList *list = 0; |
|
2078 |
if (GetMenuItemProperty(merge, 0, kMenuCreatorQt, kMenuPropertyMergeList, |
|
2079 |
sizeof(list), 0, &list) == noErr && list) { |
|
2080 |
for(int i = 0; i < list->size(); ++i) { |
|
2081 |
QMenuMergeItem item = list->at(i); |
|
2082 |
if (item.action->action == action) |
|
2083 |
return true; |
|
2084 |
} |
|
2085 |
} |
|
2086 |
} |
|
2087 |
#else |
|
2088 |
if (OSMenuRef merge = mergeMenuHash.value(menu)) { |
|
2089 |
if (QMenuMergeList *list = mergeMenuItemsHash.value(merge)) { |
|
2090 |
for(int i = 0; i < list->size(); ++i) { |
|
2091 |
const QMenuMergeItem &item = list->at(i); |
|
2092 |
if (item.action->action == action) |
|
2093 |
return true; |
|
2094 |
} |
|
2095 |
} |
|
2096 |
} |
|
2097 |
#endif |
|
2098 |
return false; |
|
2099 |
} |
|
2100 |
||
2101 |
//creation of the OSMenuRef |
|
2102 |
static OSMenuRef qt_mac_create_menu(QWidget *w) |
|
2103 |
{ |
|
2104 |
OSMenuRef ret; |
|
2105 |
#ifndef QT_MAC_USE_COCOA |
|
2106 |
ret = 0; |
|
2107 |
if (CreateNewMenu(0, 0, &ret) == noErr) { |
|
2108 |
qt_mac_create_menu_event_handler(); |
|
2109 |
SetMenuItemProperty(ret, 0, kMenuCreatorQt, kMenuPropertyQWidget, sizeof(w), &w); |
|
2110 |
||
2111 |
// kEventMenuMatchKey is only sent to the menu itself and not to |
|
2112 |
// the application, install a separate handler for that event. |
|
2113 |
EventHandlerRef eventHandlerRef; |
|
2114 |
InstallMenuEventHandler(ret, qt_mac_menu_event, |
|
2115 |
GetEventTypeCount(menu_menu_events), |
|
2116 |
menu_menu_events, 0, &eventHandlerRef); |
|
2117 |
menu_eventHandlers_hash()->insert(ret, eventHandlerRef); |
|
2118 |
} else { |
|
2119 |
qWarning("QMenu: Internal error"); |
|
2120 |
} |
|
2121 |
#else |
|
2122 |
if (QMenu *qmenu = qobject_cast<QMenu *>(w)){ |
|
2123 |
ret = [[QT_MANGLE_NAMESPACE(QCocoaMenu) alloc] initWithQMenu:qmenu]; |
|
2124 |
} else { |
|
2125 |
ret = [[NSMenu alloc] init]; |
|
2126 |
} |
|
2127 |
#endif |
|
2128 |
return ret; |
|
2129 |
} |
|
2130 |
||
2131 |
||
2132 |
||
2133 |
QT_END_NAMESPACE |