1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the QtGui module of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
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 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include <private/qmainwindowlayout_p.h>
|
---|
43 | #include <qtoolbar.h>
|
---|
44 | #include <private/qtoolbarlayout_p.h>
|
---|
45 | #include <private/qt_cocoa_helpers_mac_p.h>
|
---|
46 |
|
---|
47 | #ifndef QT_MAC_USE_COCOA
|
---|
48 | #include <Carbon/Carbon.h>
|
---|
49 | #else
|
---|
50 | #include <private/qcocoatoolbardelegate_mac_p.h>
|
---|
51 | #import <private/qcocoawindowdelegate_mac_p.h>
|
---|
52 | #endif
|
---|
53 |
|
---|
54 | QT_BEGIN_NAMESPACE
|
---|
55 | #ifdef QT_NAMESPACE
|
---|
56 |
|
---|
57 | // namespace up the stuff
|
---|
58 | #define SS(x) #x
|
---|
59 | #define S0(x) SS(x)
|
---|
60 | #define S "com.trolltech.qt-" S0(QT_NAMESPACE) ".qmainwindow.qtoolbarInHIToolbar"
|
---|
61 | #define SToolbar "com.trolltech.qt-" S0(QT_NAMESPACE) ".hitoolbar-qtoolbar"
|
---|
62 | #define SNSToolbar "com.trolltech.qt-" S0(QT_NAMESPACE) ".qtoolbarInNSToolbar"
|
---|
63 | #define MacToolbar "com.trolltech.qt-" S0(QT_NAMESPACE) ".qmainwindow.mactoolbar"
|
---|
64 |
|
---|
65 | #ifndef QT_MAC_USE_COCOA
|
---|
66 | static CFStringRef kQToolBarHIToolbarItemClassID = CFSTR(S);
|
---|
67 | static CFStringRef kQToolBarHIToolbarIdentifier = CFSTR(SToolbar);
|
---|
68 | #else
|
---|
69 | static NSString *kQToolBarNSToolbarIdentifier = @SNSToolbar;
|
---|
70 | #endif
|
---|
71 | static CFStringRef kQMainWindowMacToolbarID = CFSTR(MacToolbar);
|
---|
72 | #undef SS
|
---|
73 | #undef S0
|
---|
74 | #undef S
|
---|
75 | #undef SToolbar
|
---|
76 | #undef SNSToolbar
|
---|
77 | #undef MacToolbar
|
---|
78 |
|
---|
79 | #else
|
---|
80 | #ifndef QT_MAC_USE_COCOA
|
---|
81 | static CFStringRef kQToolBarHIToolbarItemClassID = CFSTR("com.trolltech.qt.qmainwindow.qtoolbarInHIToolbar");
|
---|
82 | static CFStringRef kQToolBarHIToolbarIdentifier = CFSTR("com.trolltech.qt.hitoolbar-qtoolbar");
|
---|
83 | #else
|
---|
84 | static NSString *kQToolBarNSToolbarIdentifier = @"com.trolltech.qt.qmainwindow.qtoolbarInNSToolbar";
|
---|
85 | #endif
|
---|
86 | static CFStringRef kQMainWindowMacToolbarID = CFSTR("com.trolltech.qt.qmainwindow.mactoolbar");
|
---|
87 | #endif // QT_NAMESPACE
|
---|
88 |
|
---|
89 | #ifndef QT_MAC_USE_COCOA
|
---|
90 |
|
---|
91 | static const int kEventParamQToolBar = 'QTBR';
|
---|
92 | static const int kEventParamQMainWindowLayout = 'QMWL';
|
---|
93 |
|
---|
94 | const EventTypeSpec qtoolbarEvents[] =
|
---|
95 | {
|
---|
96 | { kEventClassHIObject, kEventHIObjectConstruct },
|
---|
97 | { kEventClassHIObject, kEventHIObjectDestruct },
|
---|
98 | { kEventClassHIObject, kEventHIObjectInitialize },
|
---|
99 | { kEventClassToolbarItem, kEventToolbarItemCreateCustomView }
|
---|
100 | };
|
---|
101 |
|
---|
102 | struct QToolBarInHIToolbarInfo
|
---|
103 | {
|
---|
104 | QToolBarInHIToolbarInfo(HIToolbarItemRef item)
|
---|
105 | : toolbarItem(item), mainWindowLayout(0)
|
---|
106 | {}
|
---|
107 | HIToolbarItemRef toolbarItem;
|
---|
108 | QMainWindowLayout *mainWindowLayout;
|
---|
109 | };
|
---|
110 |
|
---|
111 | OSStatus QMainWindowLayout::qtoolbarInHIToolbarHandler(EventHandlerCallRef inCallRef,
|
---|
112 | EventRef event, void *data)
|
---|
113 | {
|
---|
114 | OSStatus result = eventNotHandledErr;
|
---|
115 | QToolBarInHIToolbarInfo *object = static_cast<QToolBarInHIToolbarInfo *>(data);
|
---|
116 |
|
---|
117 | switch (GetEventClass(event)) {
|
---|
118 | case kEventClassHIObject:
|
---|
119 | switch (GetEventKind(event)) {
|
---|
120 | case kEventHIObjectConstruct:
|
---|
121 | {
|
---|
122 | HIObjectRef toolbarItem;
|
---|
123 | GetEventParameter(event, kEventParamHIObjectInstance, typeHIObjectRef,
|
---|
124 | 0, sizeof( HIObjectRef ), 0, &toolbarItem);
|
---|
125 |
|
---|
126 | QToolBarInHIToolbarInfo *item = new QToolBarInHIToolbarInfo(toolbarItem);
|
---|
127 | SetEventParameter(event, kEventParamHIObjectInstance, typeVoidPtr,
|
---|
128 | sizeof(void *), &item);
|
---|
129 | result = noErr;
|
---|
130 | }
|
---|
131 | break;
|
---|
132 | case kEventHIObjectInitialize:
|
---|
133 | result = CallNextEventHandler(inCallRef, event);
|
---|
134 | if (result == noErr) {
|
---|
135 | QToolBar *toolbar = 0;
|
---|
136 | QMainWindowLayout *layout = 0;
|
---|
137 | GetEventParameter(event, kEventParamQToolBar, typeVoidPtr,
|
---|
138 | 0, sizeof(void *), 0, &toolbar);
|
---|
139 | GetEventParameter(event, kEventParamQMainWindowLayout, typeVoidPtr,
|
---|
140 | 0, sizeof(void *), 0, &layout);
|
---|
141 | object->mainWindowLayout = layout;
|
---|
142 | object->mainWindowLayout->unifiedToolbarHash.insert(object->toolbarItem, toolbar);
|
---|
143 | HIToolbarItemChangeAttributes(object->toolbarItem,
|
---|
144 | kHIToolbarItemLabelDisabled, 0);
|
---|
145 | }
|
---|
146 | break;
|
---|
147 |
|
---|
148 | case kEventHIObjectDestruct:
|
---|
149 | delete object;
|
---|
150 | result = noErr;
|
---|
151 | break;
|
---|
152 | }
|
---|
153 | break;
|
---|
154 |
|
---|
155 | case kEventClassToolbarItem:
|
---|
156 | switch (GetEventKind(event))
|
---|
157 | {
|
---|
158 | case kEventToolbarItemCreateCustomView:
|
---|
159 | {
|
---|
160 | QToolBar *toolbar
|
---|
161 | = object->mainWindowLayout->unifiedToolbarHash.value(object->toolbarItem);
|
---|
162 | if (toolbar) {
|
---|
163 | HIViewRef hiview = HIViewRef(toolbar->winId());
|
---|
164 | SetEventParameter(event, kEventParamControlRef, typeControlRef,
|
---|
165 | sizeof(HIViewRef), &hiview);
|
---|
166 | result = noErr;
|
---|
167 | }
|
---|
168 | }
|
---|
169 | break;
|
---|
170 | }
|
---|
171 | break;
|
---|
172 | }
|
---|
173 | return result;
|
---|
174 | }
|
---|
175 |
|
---|
176 | void QMainWindowLayout::qtMacHIToolbarRegisterQToolBarInHIToolborItemClass()
|
---|
177 | {
|
---|
178 | static bool registered = false;
|
---|
179 |
|
---|
180 | if (!registered) {
|
---|
181 | HIObjectRegisterSubclass( kQToolBarHIToolbarItemClassID,
|
---|
182 | kHIToolbarItemClassID, 0, QMainWindowLayout::qtoolbarInHIToolbarHandler,
|
---|
183 | GetEventTypeCount(qtoolbarEvents), qtoolbarEvents, 0, 0 );
|
---|
184 | registered = true;
|
---|
185 | }
|
---|
186 | }
|
---|
187 |
|
---|
188 | static void GetToolbarAllowedItems(CFMutableArrayRef array)
|
---|
189 | {
|
---|
190 | CFArrayAppendValue(array, kQToolBarHIToolbarIdentifier);
|
---|
191 | }
|
---|
192 |
|
---|
193 | HIToolbarItemRef QMainWindowLayout::createQToolBarInHIToolbarItem(QToolBar *toolbar,
|
---|
194 | QMainWindowLayout *layout)
|
---|
195 | {
|
---|
196 | QMainWindowLayout::qtMacHIToolbarRegisterQToolBarInHIToolborItemClass();
|
---|
197 |
|
---|
198 | EventRef event;
|
---|
199 | HIToolbarItemRef result = 0;
|
---|
200 |
|
---|
201 | CFStringRef identifier = kQToolBarHIToolbarIdentifier;
|
---|
202 | UInt32 options = kHIToolbarItemAllowDuplicates;
|
---|
203 |
|
---|
204 | CreateEvent(0, kEventClassHIObject, kEventHIObjectInitialize,
|
---|
205 | GetCurrentEventTime(), 0, &event);
|
---|
206 | SetEventParameter(event, kEventParamToolbarItemIdentifier, typeCFStringRef,
|
---|
207 | sizeof(CFStringRef), &identifier);
|
---|
208 | SetEventParameter(event, kEventParamAttributes, typeUInt32, sizeof(UInt32), &options);
|
---|
209 | SetEventParameter(event, kEventParamQToolBar, typeVoidPtr, sizeof(void *), &toolbar);
|
---|
210 | SetEventParameter(event, kEventParamQMainWindowLayout, typeVoidPtr, sizeof(void *), &layout);
|
---|
211 |
|
---|
212 | HIObjectCreate(kQToolBarHIToolbarItemClassID, event,
|
---|
213 | static_cast<HIObjectRef *>(&result));
|
---|
214 |
|
---|
215 | ReleaseEvent(event);
|
---|
216 | return result;
|
---|
217 |
|
---|
218 | }
|
---|
219 |
|
---|
220 | HIToolbarItemRef QMainWindowLayout::CreateToolbarItemForIdentifier(CFStringRef identifier,
|
---|
221 | CFTypeRef data)
|
---|
222 | {
|
---|
223 | HIToolbarItemRef item = 0;
|
---|
224 | if (CFStringCompare(kQToolBarHIToolbarIdentifier, identifier,
|
---|
225 | kCFCompareBackwards) == kCFCompareEqualTo) {
|
---|
226 | if (data && CFGetTypeID(data) == CFArrayGetTypeID()) {
|
---|
227 | CFArrayRef array = static_cast<CFArrayRef>(data);
|
---|
228 | QToolBar *toolbar = static_cast<QToolBar *>(const_cast<void *>(CFArrayGetValueAtIndex(array, 0)));
|
---|
229 | QMainWindowLayout *layout = static_cast<QMainWindowLayout *>(const_cast<void *>(CFArrayGetValueAtIndex(array, 1)));
|
---|
230 | item = createQToolBarInHIToolbarItem(toolbar, layout);
|
---|
231 | }
|
---|
232 | }
|
---|
233 | return item;
|
---|
234 | }
|
---|
235 |
|
---|
236 | static const EventTypeSpec kToolbarEvents[] = {
|
---|
237 | { kEventClassToolbar, kEventToolbarGetDefaultIdentifiers },
|
---|
238 | { kEventClassToolbar, kEventToolbarGetAllowedIdentifiers },
|
---|
239 | { kEventClassToolbar, kEventToolbarCreateItemWithIdentifier },
|
---|
240 | { kEventClassToolbar, kEventToolbarItemAdded },
|
---|
241 | { kEventClassToolbar, kEventToolbarItemRemoved }
|
---|
242 | };
|
---|
243 |
|
---|
244 | OSStatus QMainWindowLayout::qtmacToolbarDelegate(EventHandlerCallRef, EventRef event, void *data)
|
---|
245 | {
|
---|
246 | QMainWindowLayout *mainWindowLayout = static_cast<QMainWindowLayout *>(data);
|
---|
247 | OSStatus result = eventNotHandledErr;
|
---|
248 | CFMutableArrayRef array;
|
---|
249 | CFStringRef identifier;
|
---|
250 | switch (GetEventKind(event)) {
|
---|
251 | case kEventToolbarGetDefaultIdentifiers:
|
---|
252 | case kEventToolbarGetAllowedIdentifiers:
|
---|
253 | GetEventParameter(event, kEventParamMutableArray, typeCFMutableArrayRef, 0,
|
---|
254 | sizeof(CFMutableArrayRef), 0, &array);
|
---|
255 | GetToolbarAllowedItems(array);
|
---|
256 | result = noErr;
|
---|
257 | break;
|
---|
258 | case kEventToolbarCreateItemWithIdentifier: {
|
---|
259 | HIToolbarItemRef item;
|
---|
260 | CFTypeRef data = 0;
|
---|
261 | OSStatus err = GetEventParameter(event, kEventParamToolbarItemIdentifier, typeCFStringRef,
|
---|
262 | 0, sizeof(CFStringRef), 0, &identifier);
|
---|
263 | err = GetEventParameter(event, kEventParamToolbarItemConfigData, typeCFTypeRef,
|
---|
264 | 0, sizeof(CFTypeRef), 0, &data);
|
---|
265 | item = CreateToolbarItemForIdentifier(identifier, data);
|
---|
266 | if (item) {
|
---|
267 | result = SetEventParameter(event, kEventParamToolbarItem, typeHIToolbarItemRef,
|
---|
268 | sizeof(HIToolbarItemRef), &item );
|
---|
269 | }
|
---|
270 | break;
|
---|
271 | }
|
---|
272 | case kEventToolbarItemAdded: {
|
---|
273 | // Double check that our "view" of the toolbar is similar.
|
---|
274 | HIToolbarItemRef item;
|
---|
275 | CFIndex index;
|
---|
276 | if (GetEventParameter(event, kEventParamToolbarItem, typeHIToolbarItemRef,
|
---|
277 | 0, sizeof(HIToolbarItemRef), 0, &item) == noErr
|
---|
278 | && GetEventParameter(event, kEventParamIndex, typeCFIndex, 0,
|
---|
279 | sizeof(CFIndex), 0, &index) == noErr) {
|
---|
280 | CFRetain(item); // We will watch this until it's removed from the list (or bust).
|
---|
281 | mainWindowLayout->toolbarItemsCopy.insert(index, item);
|
---|
282 | QToolBar *toolbar = mainWindowLayout->unifiedToolbarHash.value(item);
|
---|
283 | if (toolbar) {
|
---|
284 | int toolbarIndex = mainWindowLayout->qtoolbarsInUnifiedToolbarList.indexOf(toolbar);
|
---|
285 | if (index != toolbarIndex) {
|
---|
286 | // Dang, we must be out of sync, rebuild it from the "toolbarItemsCopy"
|
---|
287 | mainWindowLayout->qtoolbarsInUnifiedToolbarList.clear();
|
---|
288 | for (int i = 0; i < mainWindowLayout->toolbarItemsCopy.size(); ++i) {
|
---|
289 | // This will either append the correct toolbar or an
|
---|
290 | // null toolbar. This is fine because this list
|
---|
291 | // is really only kept to make sure that things are but in the right order.
|
---|
292 | mainWindowLayout->qtoolbarsInUnifiedToolbarList.append(
|
---|
293 | mainWindowLayout->unifiedToolbarHash.value(mainWindowLayout->
|
---|
294 | toolbarItemsCopy.at(i)));
|
---|
295 | }
|
---|
296 | }
|
---|
297 | }
|
---|
298 | }
|
---|
299 | break;
|
---|
300 | }
|
---|
301 | case kEventToolbarItemRemoved: {
|
---|
302 | HIToolbarItemRef item;
|
---|
303 | if (GetEventParameter(event, kEventParamToolbarItem, typeHIToolbarItemRef,
|
---|
304 | 0, sizeof(HIToolbarItemRef), 0, &item) == noErr) {
|
---|
305 | mainWindowLayout->unifiedToolbarHash.remove(item);
|
---|
306 | for (int i = 0; i < mainWindowLayout->toolbarItemsCopy.size(); ++i) {
|
---|
307 | if (mainWindowLayout->toolbarItemsCopy.at(i) == item) {
|
---|
308 | // I know about it, so release it.
|
---|
309 | mainWindowLayout->toolbarItemsCopy.removeAt(i);
|
---|
310 | mainWindowLayout->qtoolbarsInUnifiedToolbarList.removeAt(i);
|
---|
311 | CFRelease(item);
|
---|
312 | break;
|
---|
313 | }
|
---|
314 | }
|
---|
315 | }
|
---|
316 | break;
|
---|
317 | }
|
---|
318 | }
|
---|
319 | return result;
|
---|
320 | }
|
---|
321 | #endif // ! QT_MAC_USE_COCOA
|
---|
322 |
|
---|
323 | #ifndef kWindowUnifiedTitleAndToolbarAttribute
|
---|
324 | #define kWindowUnifiedTitleAndToolbarAttribute (1 << 7)
|
---|
325 | #endif
|
---|
326 |
|
---|
327 | void QMainWindowLayout::updateHIToolBarStatus()
|
---|
328 | {
|
---|
329 | bool useMacToolbar = layoutState.mainWindow->unifiedTitleAndToolBarOnMac();
|
---|
330 | #ifndef QT_MAC_USE_COCOA
|
---|
331 | if (useMacToolbar) {
|
---|
332 | ChangeWindowAttributes(qt_mac_window_for(layoutState.mainWindow),
|
---|
333 | kWindowUnifiedTitleAndToolbarAttribute, 0);
|
---|
334 | } else {
|
---|
335 | ChangeWindowAttributes(qt_mac_window_for(layoutState.mainWindow),
|
---|
336 | 0, kWindowUnifiedTitleAndToolbarAttribute);
|
---|
337 | }
|
---|
338 | #endif
|
---|
339 |
|
---|
340 | layoutState.mainWindow->setUpdatesEnabled(false); // reduces a little bit of flicker, not all though
|
---|
341 | #if defined(QT_MAC_USE_COCOA)
|
---|
342 | QMacCocoaAutoReleasePool pool;
|
---|
343 | NSView *cView = [qt_mac_window_for(layoutState.mainWindow) contentView];
|
---|
344 | if (useMacToolbar) {
|
---|
345 | [cView setPostsFrameChangedNotifications:YES];
|
---|
346 | [[NSNotificationCenter defaultCenter] addObserver: [QT_MANGLE_NAMESPACE(QCocoaWindowDelegate) sharedDelegate]
|
---|
347 | selector: @selector(syncContentViewFrame:)
|
---|
348 | name: NSViewFrameDidChangeNotification
|
---|
349 | object: cView];
|
---|
350 | }
|
---|
351 | #endif
|
---|
352 | if (!useMacToolbar) {
|
---|
353 | macWindowToolbarShow(layoutState.mainWindow, false);
|
---|
354 | // Move everything out of the HIToolbar into the main toolbar.
|
---|
355 | while (!qtoolbarsInUnifiedToolbarList.isEmpty()) {
|
---|
356 | // Should shrink the list by one every time.
|
---|
357 | layoutState.mainWindow->addToolBar(Qt::TopToolBarArea, qtoolbarsInUnifiedToolbarList.first());
|
---|
358 | }
|
---|
359 | macWindowToolbarSet(qt_mac_window_for(layoutState.mainWindow), 0);
|
---|
360 | } else {
|
---|
361 | QList<QToolBar *> toolbars = layoutState.mainWindow->findChildren<QToolBar *>();
|
---|
362 | for (int i = 0; i < toolbars.size(); ++i) {
|
---|
363 | QToolBar *toolbar = toolbars.at(i);
|
---|
364 | if (toolBarArea(toolbar) == Qt::TopToolBarArea) {
|
---|
365 | removeWidget(toolbar); // Do this here, because we are in an in-between state.
|
---|
366 | layoutState.mainWindow->addToolBar(Qt::TopToolBarArea, toolbar);
|
---|
367 | }
|
---|
368 | }
|
---|
369 | syncUnifiedToolbarVisibility();
|
---|
370 | }
|
---|
371 | #if defined(QT_MAC_USE_COCOA)
|
---|
372 | if (!useMacToolbar) {
|
---|
373 | [cView setPostsFrameChangedNotifications:NO];
|
---|
374 | [[NSNotificationCenter defaultCenter] removeObserver: [QT_MANGLE_NAMESPACE(QCocoaWindowDelegate) sharedDelegate]
|
---|
375 | name: NSViewFrameDidChangeNotification
|
---|
376 | object: cView];
|
---|
377 | }
|
---|
378 | #endif
|
---|
379 | layoutState.mainWindow->setUpdatesEnabled(true);
|
---|
380 | }
|
---|
381 |
|
---|
382 | void QMainWindowLayout::insertIntoMacToolbar(QToolBar *before, QToolBar *toolbar)
|
---|
383 | {
|
---|
384 | // This layering could go on to one more level, but I decided to stop here.
|
---|
385 | // The HIToolbar and NSToolbar APIs are fairly similar as you will see.
|
---|
386 | if (toolbar == 0)
|
---|
387 | return;
|
---|
388 |
|
---|
389 |
|
---|
390 | QToolBarLayout *toolbarLayout = static_cast<QToolBarLayout *>(toolbar->layout());
|
---|
391 | toolbarSaveState.insert(toolbar, ToolBarSaveState(toolbar->isMovable(),
|
---|
392 | toolbar->maximumSize()));
|
---|
393 |
|
---|
394 | if (toolbarLayout->hasExpandFlag() == false)
|
---|
395 | toolbar->setMaximumSize(toolbar->sizeHint());
|
---|
396 |
|
---|
397 | toolbar->setMovable(false);
|
---|
398 | toolbarLayout->setUsePopupMenu(true);
|
---|
399 | // Make the toolbar a child of the mainwindow to avoid creating a window.
|
---|
400 | toolbar->setParent(layoutState.mainWindow);
|
---|
401 | toolbar->createWinId(); // Now create the OSViewRef.
|
---|
402 |
|
---|
403 | layoutState.mainWindow->createWinId();
|
---|
404 |
|
---|
405 | OSWindowRef window = qt_mac_window_for(layoutState.mainWindow);
|
---|
406 | int beforeIndex = qtoolbarsInUnifiedToolbarList.indexOf(before);
|
---|
407 | if (beforeIndex == -1)
|
---|
408 | beforeIndex = qtoolbarsInUnifiedToolbarList.size();
|
---|
409 |
|
---|
410 | int toolbarIndex = qtoolbarsInUnifiedToolbarList.indexOf(toolbar);
|
---|
411 | #ifndef QT_MAC_USE_COCOA
|
---|
412 | HIToolbarRef macToolbar = NULL;
|
---|
413 | if ((GetWindowToolbar(window, &macToolbar) == noErr) && !macToolbar) {
|
---|
414 | HIToolbarCreate(kQMainWindowMacToolbarID,
|
---|
415 | kHIToolbarItemAllowDuplicates, &macToolbar);
|
---|
416 | InstallEventHandler(HIObjectGetEventTarget(static_cast<HIToolbarRef>(macToolbar)),
|
---|
417 | QMainWindowLayout::qtmacToolbarDelegate, GetEventTypeCount(kToolbarEvents),
|
---|
418 | kToolbarEvents, this, 0);
|
---|
419 | HIToolbarSetDisplaySize(macToolbar, kHIToolbarDisplaySizeNormal);
|
---|
420 | HIToolbarSetDisplayMode(macToolbar, kHIToolbarDisplayModeIconOnly);
|
---|
421 | macWindowToolbarSet(window, macToolbar);
|
---|
422 | if (layoutState.mainWindow->isVisible())
|
---|
423 | macWindowToolbarShow(layoutState.mainWindow, true);
|
---|
424 | CFRelease(macToolbar);
|
---|
425 | }
|
---|
426 | #else
|
---|
427 | QMacCocoaAutoReleasePool pool;
|
---|
428 | NSToolbar *macToolbar = [window toolbar];
|
---|
429 | if (macToolbar == nil) {
|
---|
430 | macToolbar = [[NSToolbar alloc] initWithIdentifier:(NSString *)kQMainWindowMacToolbarID];
|
---|
431 | [macToolbar setDisplayMode:NSToolbarDisplayModeIconOnly];
|
---|
432 | [macToolbar setSizeMode:NSToolbarSizeModeRegular];
|
---|
433 | [macToolbar setDelegate:[[QT_MANGLE_NAMESPACE(QCocoaToolBarDelegate) alloc] initWithMainWindowLayout:this]];
|
---|
434 | [window setToolbar:macToolbar];
|
---|
435 | [macToolbar release];
|
---|
436 | }
|
---|
437 | #endif
|
---|
438 | if (toolbarIndex != -1) {
|
---|
439 | qtoolbarsInUnifiedToolbarList.removeAt(toolbarIndex);
|
---|
440 | #ifndef QT_MAC_USE_COCOA
|
---|
441 | HIToolbarRemoveItemAtIndex(macToolbar, toolbarIndex);
|
---|
442 | #else
|
---|
443 | [macToolbar removeItemAtIndex:toolbarIndex];
|
---|
444 | #endif
|
---|
445 | }
|
---|
446 | qtoolbarsInUnifiedToolbarList.insert(beforeIndex, toolbar);
|
---|
447 | #ifndef QT_MAC_USE_COCOA
|
---|
448 | QCFType<HIToolbarItemRef> outItem;
|
---|
449 | const QObject *stupidArray[] = { toolbar, this };
|
---|
450 | QCFType<CFArrayRef> array = CFArrayCreate(0, reinterpret_cast<const void **>(&stupidArray),
|
---|
451 | 2, 0);
|
---|
452 | HIToolbarCreateItemWithIdentifier(macToolbar, kQToolBarHIToolbarIdentifier,
|
---|
453 | array, &outItem);
|
---|
454 | HIToolbarInsertItemAtIndex(macToolbar, outItem, beforeIndex);
|
---|
455 | #else
|
---|
456 | NSString *toolbarID = kQToolBarNSToolbarIdentifier;
|
---|
457 | toolbarID = [toolbarID stringByAppendingFormat:@"%p", toolbar];
|
---|
458 | cocoaItemIDToToolbarHash.insert(qt_mac_NSStringToQString(toolbarID), toolbar);
|
---|
459 | [macToolbar insertItemWithItemIdentifier:toolbarID atIndex:beforeIndex];
|
---|
460 | #endif
|
---|
461 | }
|
---|
462 |
|
---|
463 | void QMainWindowLayout::removeFromMacToolbar(QToolBar *toolbar)
|
---|
464 | {
|
---|
465 | QHash<void *, QToolBar *>::iterator it = unifiedToolbarHash.begin();
|
---|
466 | while (it != unifiedToolbarHash.end()) {
|
---|
467 | if (it.value() == toolbar) {
|
---|
468 | // Rescue our HIView and set it on the mainWindow again.
|
---|
469 | bool saveVisible = !toolbar->isHidden();
|
---|
470 | toolbar->setParent(0);
|
---|
471 | toolbar->setParent(parentWidget());
|
---|
472 | toolbar->setVisible(saveVisible);
|
---|
473 | ToolBarSaveState saveState = toolbarSaveState.value(toolbar);
|
---|
474 | static_cast<QToolBarLayout *>(toolbar->layout())->setUsePopupMenu(false);
|
---|
475 | toolbar->setMovable(saveState.movable);
|
---|
476 | toolbar->setMaximumSize(saveState.maximumSize);
|
---|
477 | toolbarSaveState.remove(toolbar);
|
---|
478 | #ifndef QT_MAC_USE_COCOA
|
---|
479 | HIToolbarItemRef item = static_cast<HIToolbarItemRef>(it.key());
|
---|
480 | HIToolbarRemoveItemAtIndex(HIToolbarItemGetToolbar(item),
|
---|
481 | toolbarItemsCopy.indexOf(item));
|
---|
482 | #else
|
---|
483 | NSToolbarItem *item = static_cast<NSToolbarItem *>(it.key());
|
---|
484 | [[qt_mac_window_for(layoutState.mainWindow->window()) toolbar]
|
---|
485 | removeItemAtIndex:toolbarItemsCopy.indexOf(item)];
|
---|
486 | unifiedToolbarHash.remove(item);
|
---|
487 | qtoolbarsInUnifiedToolbarList.removeAll(toolbar);
|
---|
488 | #endif
|
---|
489 | break;
|
---|
490 | }
|
---|
491 | ++it;
|
---|
492 | }
|
---|
493 | }
|
---|
494 |
|
---|
495 | void QMainWindowLayout::cleanUpMacToolbarItems()
|
---|
496 | {
|
---|
497 | #ifdef QT_MAC_USE_COCOA
|
---|
498 | QMacCocoaAutoReleasePool pool;
|
---|
499 | #endif
|
---|
500 | for (int i = 0; i < toolbarItemsCopy.size(); ++i) {
|
---|
501 | #ifdef QT_MAC_USE_COCOA
|
---|
502 | NSToolbarItem *item = static_cast<NSToolbarItem *>(toolbarItemsCopy.at(i));
|
---|
503 | [item setView:0];
|
---|
504 | #endif
|
---|
505 | CFRelease(toolbarItemsCopy.at(i));
|
---|
506 | }
|
---|
507 | toolbarItemsCopy.clear();
|
---|
508 | unifiedToolbarHash.clear();
|
---|
509 |
|
---|
510 | #ifdef QT_MAC_USE_COCOA
|
---|
511 | OSWindowRef window = qt_mac_window_for(layoutState.mainWindow);
|
---|
512 | NSToolbar *macToolbar = [window toolbar];
|
---|
513 | if (macToolbar) {
|
---|
514 | [[macToolbar delegate] release];
|
---|
515 | [macToolbar setDelegate:nil];
|
---|
516 | }
|
---|
517 | #endif
|
---|
518 | }
|
---|
519 |
|
---|
520 | void QMainWindowLayout::fixSizeInUnifiedToolbar(QToolBar *tb) const
|
---|
521 | {
|
---|
522 | #ifdef QT_MAC_USE_COCOA
|
---|
523 | QHash<void *, QToolBar *>::const_iterator it = unifiedToolbarHash.constBegin();
|
---|
524 | NSToolbarItem *item = nil;
|
---|
525 | while (it != unifiedToolbarHash.constEnd()) {
|
---|
526 | if (tb == it.value()) {
|
---|
527 | item = static_cast<NSToolbarItem *>(it.key());
|
---|
528 | break;
|
---|
529 | }
|
---|
530 | ++it;
|
---|
531 | }
|
---|
532 | if (item) {
|
---|
533 | QMacCocoaAutoReleasePool pool;
|
---|
534 | QWidgetItem layoutItem(tb);
|
---|
535 | QSize size = layoutItem.maximumSize();
|
---|
536 | NSSize nssize = NSMakeSize(size.width(), size.height() - 2);
|
---|
537 | [item setMaxSize:nssize];
|
---|
538 | size = layoutItem.minimumSize();
|
---|
539 | nssize.width = size.width();
|
---|
540 | nssize.height = size.height() - 2;
|
---|
541 | [item setMinSize:nssize];
|
---|
542 | }
|
---|
543 | #else
|
---|
544 | Q_UNUSED(tb);
|
---|
545 | #endif
|
---|
546 | }
|
---|
547 |
|
---|
548 | void QMainWindowLayout::syncUnifiedToolbarVisibility()
|
---|
549 | {
|
---|
550 | if (blockVisiblityCheck)
|
---|
551 | return;
|
---|
552 |
|
---|
553 | Q_ASSERT(layoutState.mainWindow->unifiedTitleAndToolBarOnMac());
|
---|
554 | bool show = false;
|
---|
555 | const int ToolBarCount = qtoolbarsInUnifiedToolbarList.count();
|
---|
556 | for (int i = 0; i < ToolBarCount; ++i) {
|
---|
557 | if (qtoolbarsInUnifiedToolbarList.at(i)->isVisible()) {
|
---|
558 | show = true;
|
---|
559 | break;
|
---|
560 | }
|
---|
561 | }
|
---|
562 | macWindowToolbarShow(layoutState.mainWindow, show);
|
---|
563 | }
|
---|
564 |
|
---|
565 | QT_END_NAMESPACE
|
---|