source: trunk/src/gui/widgets/qmainwindowlayout_mac.mm@ 668

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

trunk: Merged in qt 4.6.2 sources.

File size: 22.3 KB
RevLine 
[2]1/****************************************************************************
2**
[651]3** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
[561]4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
[2]6**
7** This file is part of the QtGui module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
[561]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.
[2]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**
[561]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.
[2]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**
[561]36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
[2]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#endif
52
53QT_BEGIN_NAMESPACE
54#ifdef QT_NAMESPACE
55
56// namespace up the stuff
57#define SS(x) #x
58#define S0(x) SS(x)
59#define S "com.trolltech.qt-" S0(QT_NAMESPACE) ".qmainwindow.qtoolbarInHIToolbar"
60#define SToolbar "com.trolltech.qt-" S0(QT_NAMESPACE) ".hitoolbar-qtoolbar"
61#define SNSToolbar "com.trolltech.qt-" S0(QT_NAMESPACE) ".qtoolbarInNSToolbar"
62#define MacToolbar "com.trolltech.qt-" S0(QT_NAMESPACE) ".qmainwindow.mactoolbar"
63
64#ifndef QT_MAC_USE_COCOA
65static CFStringRef kQToolBarHIToolbarItemClassID = CFSTR(S);
66static CFStringRef kQToolBarHIToolbarIdentifier = CFSTR(SToolbar);
67#else
68static NSString *kQToolBarNSToolbarIdentifier = @SNSToolbar;
69#endif
70static CFStringRef kQMainWindowMacToolbarID = CFSTR(MacToolbar);
71#undef SS
72#undef S0
73#undef S
74#undef SToolbar
75#undef SNSToolbar
76#undef MacToolbar
77
78#else
79#ifndef QT_MAC_USE_COCOA
80static CFStringRef kQToolBarHIToolbarItemClassID = CFSTR("com.trolltech.qt.qmainwindow.qtoolbarInHIToolbar");
81static CFStringRef kQToolBarHIToolbarIdentifier = CFSTR("com.trolltech.qt.hitoolbar-qtoolbar");
82#else
83static NSString *kQToolBarNSToolbarIdentifier = @"com.trolltech.qt.qmainwindow.qtoolbarInNSToolbar";
84#endif
85static CFStringRef kQMainWindowMacToolbarID = CFSTR("com.trolltech.qt.qmainwindow.mactoolbar");
86#endif // QT_NAMESPACE
87
88#ifndef QT_MAC_USE_COCOA
89
90static const int kEventParamQToolBar = 'QTBR';
91static const int kEventParamQMainWindowLayout = 'QMWL';
92
93const EventTypeSpec qtoolbarEvents[] =
94{
95 { kEventClassHIObject, kEventHIObjectConstruct },
96 { kEventClassHIObject, kEventHIObjectDestruct },
97 { kEventClassHIObject, kEventHIObjectInitialize },
98 { kEventClassToolbarItem, kEventToolbarItemCreateCustomView }
99};
100
101struct QToolBarInHIToolbarInfo
102{
103 QToolBarInHIToolbarInfo(HIToolbarItemRef item)
104 : toolbarItem(item), mainWindowLayout(0)
105 {}
106 HIToolbarItemRef toolbarItem;
107 QMainWindowLayout *mainWindowLayout;
108};
109
110OSStatus QMainWindowLayout::qtoolbarInHIToolbarHandler(EventHandlerCallRef inCallRef,
111 EventRef event, void *data)
112{
113 OSStatus result = eventNotHandledErr;
114 QToolBarInHIToolbarInfo *object = static_cast<QToolBarInHIToolbarInfo *>(data);
115
116 switch (GetEventClass(event)) {
117 case kEventClassHIObject:
118 switch (GetEventKind(event)) {
119 case kEventHIObjectConstruct:
120 {
121 HIObjectRef toolbarItem;
122 GetEventParameter(event, kEventParamHIObjectInstance, typeHIObjectRef,
123 0, sizeof( HIObjectRef ), 0, &toolbarItem);
124
125 QToolBarInHIToolbarInfo *item = new QToolBarInHIToolbarInfo(toolbarItem);
126 SetEventParameter(event, kEventParamHIObjectInstance, typeVoidPtr,
127 sizeof(void *), &item);
128 result = noErr;
129 }
130 break;
131 case kEventHIObjectInitialize:
132 result = CallNextEventHandler(inCallRef, event);
133 if (result == noErr) {
134 QToolBar *toolbar = 0;
135 QMainWindowLayout *layout = 0;
136 GetEventParameter(event, kEventParamQToolBar, typeVoidPtr,
137 0, sizeof(void *), 0, &toolbar);
138 GetEventParameter(event, kEventParamQMainWindowLayout, typeVoidPtr,
139 0, sizeof(void *), 0, &layout);
140 object->mainWindowLayout = layout;
141 object->mainWindowLayout->unifiedToolbarHash.insert(object->toolbarItem, toolbar);
142 HIToolbarItemChangeAttributes(object->toolbarItem,
143 kHIToolbarItemLabelDisabled, 0);
144 }
145 break;
146
147 case kEventHIObjectDestruct:
148 delete object;
149 result = noErr;
150 break;
151 }
152 break;
153
154 case kEventClassToolbarItem:
155 switch (GetEventKind(event))
156 {
157 case kEventToolbarItemCreateCustomView:
158 {
159 QToolBar *toolbar
160 = object->mainWindowLayout->unifiedToolbarHash.value(object->toolbarItem);
161 if (toolbar) {
162 HIViewRef hiview = HIViewRef(toolbar->winId());
163 SetEventParameter(event, kEventParamControlRef, typeControlRef,
164 sizeof(HIViewRef), &hiview);
165 result = noErr;
166 }
167 }
168 break;
169 }
170 break;
171 }
172 return result;
173}
174
175void QMainWindowLayout::qtMacHIToolbarRegisterQToolBarInHIToolborItemClass()
176{
177 static bool registered = false;
178
179 if (!registered) {
180 HIObjectRegisterSubclass( kQToolBarHIToolbarItemClassID,
181 kHIToolbarItemClassID, 0, QMainWindowLayout::qtoolbarInHIToolbarHandler,
182 GetEventTypeCount(qtoolbarEvents), qtoolbarEvents, 0, 0 );
183 registered = true;
184 }
185}
186
187static void GetToolbarAllowedItems(CFMutableArrayRef array)
188{
189 CFArrayAppendValue(array, kQToolBarHIToolbarIdentifier);
190}
191
192HIToolbarItemRef QMainWindowLayout::createQToolBarInHIToolbarItem(QToolBar *toolbar,
193 QMainWindowLayout *layout)
194{
195 QMainWindowLayout::qtMacHIToolbarRegisterQToolBarInHIToolborItemClass();
196
197 EventRef event;
198 HIToolbarItemRef result = 0;
199
200 CFStringRef identifier = kQToolBarHIToolbarIdentifier;
201 UInt32 options = kHIToolbarItemAllowDuplicates;
202
203 CreateEvent(0, kEventClassHIObject, kEventHIObjectInitialize,
204 GetCurrentEventTime(), 0, &event);
205 SetEventParameter(event, kEventParamToolbarItemIdentifier, typeCFStringRef,
206 sizeof(CFStringRef), &identifier);
207 SetEventParameter(event, kEventParamAttributes, typeUInt32, sizeof(UInt32), &options);
208 SetEventParameter(event, kEventParamQToolBar, typeVoidPtr, sizeof(void *), &toolbar);
209 SetEventParameter(event, kEventParamQMainWindowLayout, typeVoidPtr, sizeof(void *), &layout);
210
211 HIObjectCreate(kQToolBarHIToolbarItemClassID, event,
212 static_cast<HIObjectRef *>(&result));
213
214 ReleaseEvent(event);
215 return result;
216
217}
218
219HIToolbarItemRef QMainWindowLayout::CreateToolbarItemForIdentifier(CFStringRef identifier,
220 CFTypeRef data)
221{
222 HIToolbarItemRef item = 0;
223 if (CFStringCompare(kQToolBarHIToolbarIdentifier, identifier,
224 kCFCompareBackwards) == kCFCompareEqualTo) {
225 if (data && CFGetTypeID(data) == CFArrayGetTypeID()) {
226 CFArrayRef array = static_cast<CFArrayRef>(data);
227 QToolBar *toolbar = static_cast<QToolBar *>(const_cast<void *>(CFArrayGetValueAtIndex(array, 0)));
228 QMainWindowLayout *layout = static_cast<QMainWindowLayout *>(const_cast<void *>(CFArrayGetValueAtIndex(array, 1)));
229 item = createQToolBarInHIToolbarItem(toolbar, layout);
230 }
231 }
232 return item;