Qt 4.8
qcocoatoolbardelegate_mac.mm
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #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/qt_cocoa_helpers_mac_p.h>
47 #include <private/qcocoaview_mac_p.h>
48 #include <private/qwidget_p.h>
49 #include <qtoolbar.h>
50 #include <qlayout.h>
51 #include <qdebug.h>
52 
56 
60 
61 @implementation QT_MANGLE_NAMESPACE(QCocoaToolBarDelegate)
62 
63 - (id)initWithMainWindowLayout:(QMainWindowLayout *)layout
64 {
65  self = [super init];
66  if (self) {
67  mainWindowLayout = layout;
68  }
69  return self;
70 }
71 
72 - (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar *)toolbar
73 {
75  return [NSArray arrayWithObject:@"com.trolltech.qt.nstoolbar-qtoolbar"];
76 }
77 
78 - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar
79 {
80  return [self toolbarAllowedItemIdentifiers:toolbar];
81 }
82 
83 - (void)toolbarDidRemoveItem:(NSNotification *)notification
84 {
85  NSToolbarItem *item = [[notification userInfo] valueForKey:@"item"];
86  mainWindowLayout->unifiedToolbarHash.remove(item);
87  for (int i = 0; i < mainWindowLayout->toolbarItemsCopy.size(); ++i) {
88  if (mainWindowLayout->toolbarItemsCopy.at(i) == item) {
89  // I know about it, so release it.
90  mainWindowLayout->toolbarItemsCopy.removeAt(i);
91  mainWindowLayout->qtoolbarsInUnifiedToolbarList.removeAt(i);
92  [item release];
93  break;
94  }
95  }
96 }
97 
98 - (NSToolbarItem *)toolbar:(NSToolbar *)nstoolbar itemForItemIdentifier:(NSString *)itemIdentifier
99  willBeInsertedIntoToolbar:(BOOL)flag
100 {
101  Q_UNUSED(flag);
102  Q_UNUSED(nstoolbar);
103  QToolBar *tb = mainWindowLayout->cocoaItemIDToToolbarHash.value(
104  QT_PREPEND_NAMESPACE(qt_mac_NSStringToQString)(itemIdentifier));
105  NSToolbarItem *item = nil;
106  if (tb) {
107  item = [[NSToolbarItem alloc] initWithItemIdentifier:itemIdentifier];
108  mainWindowLayout->unifiedToolbarHash.insert(item, tb);
109  }
110  return item;
111 }
112 
113 - (void)toolbarWillAddItem:(NSNotification *)notification
114 {
115  NSToolbarItem *item = [[notification userInfo] valueForKey:@"item"];
116  QToolBar *tb = mainWindowLayout->cocoaItemIDToToolbarHash.value(
117  QT_PREPEND_NAMESPACE(qt_mac_NSStringToQString)([item itemIdentifier]));
118  if (!tb)
119  return; // I can't really do anything about this.
120  [item retain];
121  [item setView:QT_PREPEND_NAMESPACE(qt_mac_nativeview_for)(tb)];
122 
123  NSArray *items = [[qt_mac_window_for(mainWindowLayout->layoutState.mainWindow->window()) toolbar] items];
124  int someIndex = 0;
125  for (NSToolbarItem *i in items) {
126  if (i == item)
127  break;
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
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QPointer< QWidget > widget
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
NSToolbar * toolbar
void update()
Updates the widget unless updates are disabled or the widget is hidden.
Definition: qwidget.cpp:10883
#define QT_FORWARD_DECLARE_CLASS(name)
Definition: qglobal.h:95
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
The QToolBar class provides a movable panel that contains a set of controls.
Definition: qtoolbar.h:62
const char * layout
#define QT_PREPEND_NAMESPACE(name)
This macro qualifies identifier with the full namespace.
Definition: qglobal.h:87
Q_GUI_EXPORT QWidgetPrivate * qt_widget_private(QWidget *widget)
Definition: qwidget.cpp:12920
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729