source: trunk/src/gui/widgets/qcocoatoolbardelegate_mac.mm@ 500

Last change on this file since 500 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 5.8 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtGui module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** 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 are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#import <private/qcocoatoolbardelegate_mac_p.h>
43#ifdef QT_MAC_USE_COCOA
44#include <private/qmainwindowlayout_p.h>
45#include <private/qt_mac_p.h>
46#include <private/qcocoaview_mac_p.h>
47#include <private/qwidget_p.h>
48#include <qtoolbar.h>
49#include <qlayout.h>
50#include <qdebug.h>
51
52QT_BEGIN_NAMESPACE
53extern QWidgetPrivate *qt_widget_private(QWidget *widget);
54QT_END_NAMESPACE
55
56QT_FORWARD_DECLARE_CLASS(QMainWindowLayout);
57QT_FORWARD_DECLARE_CLASS(QToolBar);
58QT_FORWARD_DECLARE_CLASS(QCFString);
59
60@implementation QCocoaToolBarDelegate
61
62- (id)initWithMainWindowLayout:(QMainWindowLayout *)layout
63{
64 self = [super init];
65 if (self) {
66 mainWindowLayout = layout;
67 }
68 return self;
69}
70
71- (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar *)toolbar
72{
73 Q_UNUSED(toolbar);
74 return [NSArray arrayWithObject:@"com.trolltech.qt.nstoolbar-qtoolbar"];
75}
76
77- (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar
78{
79 return [self toolbarAllowedItemIdentifiers:toolbar];
80}
81
82- (void)toolbarDidRemoveItem:(NSNotification *)notification
83{
84 NSToolbarItem *item = [[notification userInfo] valueForKey:@"item"];
85 mainWindowLayout->unifiedToolbarHash.remove(item);
86 for (int i = 0; i < mainWindowLayout->toolbarItemsCopy.size(); ++i) {
87 if (mainWindowLayout->toolbarItemsCopy.at(i) == item) {
88 // I know about it, so release it.
89 mainWindowLayout->toolbarItemsCopy.removeAt(i);
90 mainWindowLayout->qtoolbarsInUnifiedToolbarList.removeAt(i);
91 [item release];
92 break;
93 }
94 }
95}
96
97- (NSToolbarItem *)toolbar:(NSToolbar *)nstoolbar itemForItemIdentifier:(NSString *)itemIdentifier
98 willBeInsertedIntoToolbar:(BOOL)flag
99{
100 Q_UNUSED(flag);
101 Q_UNUSED(nstoolbar);
102 QToolBar *tb = mainWindowLayout->cocoaItemIDToToolbarHash.value(QCFString::toQString(CFStringRef(itemIdentifier)));
103 NSToolbarItem *item = nil;
104 if (tb) {
105 item = [[NSToolbarItem alloc] initWithItemIdentifier:itemIdentifier];
106 mainWindowLayout->unifiedToolbarHash.insert(item, tb);
107 }
108 return item;
109}
110
111- (void)toolbarWillAddItem:(NSNotification *)notification
112{
113 NSToolbarItem *item = [[notification userInfo] valueForKey:@"item"];
114 QToolBar *tb = mainWindowLayout->cocoaItemIDToToolbarHash.value(QCFString::toQString(CFStringRef([item itemIdentifier])));
115 if (!tb)
116 return; // I can't really do anything about this.
117 [item retain];
118 [item setView:QT_PREPEND_NAMESPACE(qt_mac_nativeview_for)(tb)];
119
120 NSArray *items = [[qt_mac_window_for(mainWindowLayout->layoutState.mainWindow->window()) toolbar] items];
121 int someIndex = 0;
122 bool foundItem = false;
123 for (NSToolbarItem *i in items) {
124 if (i == item) {
125 foundItem = true;
126 break;
127 }
128 ++someIndex;
129 }
130 mainWindowLayout->toolbarItemsCopy.insert(someIndex, item);
131
132 // This is synchronization code that was needed in Carbon, but may not be needed anymore here.
133 QToolBar *toolbar = mainWindowLayout->unifiedToolbarHash.value(item);
134 if (toolbar) {
135 int toolbarIndex = mainWindowLayout->qtoolbarsInUnifiedToolbarList.indexOf(toolbar);
136 if (someIndex != toolbarIndex) {
137 // Dang, we must be out of sync, rebuild it from the "toolbarItemsCopy"
138 mainWindowLayout->qtoolbarsInUnifiedToolbarList.clear();
139 for (int i = 0; i < mainWindowLayout->toolbarItemsCopy.size(); ++i) {
140 // This will either append the correct toolbar or an
141 // null toolbar. This is fine because this list
142 // is really only kept to make sure that things are but in the right order.
143 mainWindowLayout->qtoolbarsInUnifiedToolbarList.append(
144 mainWindowLayout->unifiedToolbarHash.value(mainWindowLayout->
145 toolbarItemsCopy.at(i)));
146 }
147 }
148 toolbar->update();
149 }
150}
151
152@end
153#endif // QT_MAC_USE_COCOA
Note: See TracBrowser for help on using the repository browser.