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

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

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

File size: 21.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** No Commercial Usage
10** This file contains pre-release code and may not be distributed.
11** You may use this file in accordance with the terms and conditions
12** contained in the either Technology Preview License Agreement or the
13** Beta Release License Agreement.
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** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
41** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
42**
43****************************************************************************/
44
45#include <private/qmainwindowlayout_p.h>
46#include <qtoolbar.h>
47#include <private/qtoolbarlayout_p.h>
48#include <private/qt_cocoa_helpers_mac_p.h>
49
50#ifndef QT_MAC_USE_COCOA
51#include <Carbon/Carbon.h>
52#else
53#include <private/qcocoatoolbardelegate_mac_p.h>
54#endif
55
56QT_BEGIN_NAMESPACE
57#ifdef QT_NAMESPACE
58
59// namespace up the stuff
60#define SS(x) #x
61#define S0(x) SS(x)
62#define S "com.trolltech.qt-" S0(QT_NAMESPACE) ".qmainwindow.qtoolbarInHIToolbar"
63#define SToolbar "com.trolltech.qt-" S0(QT_NAMESPACE) ".hitoolbar-qtoolbar"
64#define SNSToolbar "com.trolltech.qt-" S0(QT_NAMESPACE) ".qtoolbarInNSToolbar"
65#define MacToolbar "com.trolltech.qt-" S0(QT_NAMESPACE) ".qmainwindow.mactoolbar"
66
67#ifndef QT_MAC_USE_COCOA
68static CFStringRef kQToolBarHIToolbarItemClassID = CFSTR(S);
69static CFStringRef kQToolBarHIToolbarIdentifier = CFSTR(SToolbar);
70#else
71static NSString *kQToolBarNSToolbarIdentifier = @SNSToolbar;
72#endif
73static CFStringRef kQMainWindowMacToolbarID = CFSTR(MacToolbar);
74#undef SS
75#undef S0
76#undef S
77#undef SToolbar
78#undef SNSToolbar
79#undef MacToolbar
80
81#else
82#ifndef QT_MAC_USE_COCOA
83static CFStringRef kQToolBarHIToolbarItemClassID = CFSTR("com.trolltech.qt.qmainwindow.qtoolbarInHIToolbar");
84static CFStringRef kQToolBarHIToolbarIdentifier = CFSTR("com.trolltech.qt.hitoolbar-qtoolbar");
85#else
86static NSString *kQToolBarNSToolbarIdentifier = @"com.trolltech.qt.qmainwindow.qtoolbarInNSToolbar";
87#endif
88static CFStringRef kQMainWindowMacToolbarID = CFSTR("com.trolltech.qt.qmainwindow.mactoolbar");
89#endif // QT_NAMESPACE
90
91#ifndef QT_MAC_USE_COCOA
92
93static const int kEventParamQToolBar = 'QTBR';
94static const int kEventParamQMainWindowLayout = 'QMWL';
95
96const EventTypeSpec qtoolbarEvents[] =
97{
98 { kEventClassHIObject, kEventHIObjectConstruct },
99 { kEventClassHIObject, kEventHIObjectDestruct },
100 { kEventClassHIObject, kEventHIObjectInitialize },
101 { kEventClassToolbarItem, kEventToolbarItemCreateCustomView }
102};
103
104struct QToolBarInHIToolbarInfo
105{
106 QToolBarInHIToolbarInfo(HIToolbarItemRef item)
107 : toolbarItem(item), mainWindowLayout(0)
108 {}
109 HIToolbarItemRef toolbarItem;
110 QMainWindowLayout *mainWindowLayout;
111};
112
113OSStatus QMainWindowLayout::qtoolbarInHIToolbarHandler(EventHandlerCallRef inCallRef,
114 EventRef event, void *data)
115{
116 OSStatus result = eventNotHandledErr;
117 QToolBarInHIToolbarInfo *object = static_cast<QToolBarInHIToolbarInfo *>(data);
118
119 switch (GetEventClass(event)) {
120 case kEventClassHIObject:
121 switch (GetEventKind(event)) {
122 case kEventHIObjectConstruct:
123 {
124 HIObjectRef toolbarItem;
125 GetEventParameter(event, kEventParamHIObjectInstance, typeHIObjectRef,
126 0, sizeof( HIObjectRef ), 0, &toolbarItem);
127
128 QToolBarInHIToolbarInfo *item = new QToolBarInHIToolbarInfo(toolbarItem);
129 SetEventParameter(event, kEventParamHIObjectInstance, typeVoidPtr,
130 sizeof(void *), &item);
131 result = noErr;
132 }
133 break;
134 case kEventHIObjectInitialize:
135 result = CallNextEventHandler(inCallRef, event);
136 if (result == noErr) {
137 QToolBar *toolbar = 0;
138 QMainWindowLayout *layout = 0;
139 GetEventParameter(event, kEventParamQToolBar, typeVoidPtr,
140 0, sizeof(void *), 0, &toolbar);
141 GetEventParameter(event, kEventParamQMainWindowLayout, typeVoidPtr,
142 0, sizeof(void *), 0, &layout);
143 object->mainWindowLayout = layout;
144 object->mainWindowLayout->unifiedToolbarHash.insert(object->toolbarItem, toolbar);
145 HIToolbarItemChangeAttributes(object->toolbarItem,
146 kHIToolbarItemLabelDisabled, 0);
147 }
148 break;
149
150 case kEventHIObjectDestruct:
151 delete object;
152 result = noErr;
153 break;
154 }
155 break;
156
157 case kEventClassToolbarItem:
158 switch (GetEventKind(event))
159 {
160 case kEventToolbarItemCreateCustomView:
161 {
162 QToolBar *toolbar
163 = object->mainWindowLayout->unifiedToolbarHash.value(object->toolbarItem);
164 if (toolbar) {
165 HIViewRef hiview = HIViewRef(toolbar->winId());
166 SetEventParameter(event, kEventParamControlRef, typeControlRef,
167 sizeof(HIViewRef), &hiview);
168 result = noErr;
169 }
170 }
171 break;
172 }
173 break;
174 }
175 return result;
176}
177
178void QMainWindowLayout::qtMacHIToolbarRegisterQToolBarInHIToolborItemClass()
179{
180 static bool registered = false;
181
182 if (!registered) {
183 HIObjectRegisterSubclass( kQToolBarHIToolbarItemClassID,
184 kHIToolbarItemClassID, 0, QMainWindowLayout::qtoolbarInHIToolbarHandler,
185 GetEventTypeCount(qtoolbarEvents), qtoolbarEvents, 0, 0 );
186 registered = true;
187 }
188}
189
190static void GetToolbarAllowedItems(CFMutableArrayRef array)
191{
192 CFArrayAppendValue(array, kQToolBarHIToolbarIdentifier);
193}
194
195HIToolbarItemRef QMainWindowLayout::createQToolBarInHIToolbarItem(QToolBar *toolbar,
196 QMainWindowLayout *layout)
197{
198 QMainWindowLayout::qtMacHIToolbarRegisterQToolBarInHIToolborItemClass();
199
200 EventRef event;
201 HIToolbarItemRef result = 0;
202
203 CFStringRef identifier = kQToolBarHIToolbarIdentifier;
204 UInt32 options = kHIToolbarItemAllowDuplicates;
205
206 CreateEvent(0, kEventClassHIObject, kEventHIObjectInitialize,
207 GetCurrentEventTime(), 0, &event);
208 SetEventParameter(event, kEventParamToolbarItemIdentifier, typeCFStringRef,
209 sizeof(CFStringRef), &identifier);
210 SetEventParameter(event, kEventParamAttributes, typeUInt32, sizeof(UInt32), &options);
211 SetEventParameter(event, kEventParamQToolBar, typeVoidPtr, sizeof(void *), &toolbar);
212 SetEventParameter(event, kEventParamQMainWindowLayout, typeVoidPtr, sizeof(void *), &layout);
213
214 HIObjectCreate(kQToolBarHIToolbarItemClassID, event,
215 static_cast<HIObjectRef *>(&result));
216
217 ReleaseEvent(event);
218 return result;
219
220}
221
222HIToolbarItemRef QMainWindowLayout::CreateToolbarItemForIdentifier(CFStringRef identifier,
223 CFTypeRef data)
224{
225 HIToolbarItemRef item = 0;
226 if (CFStringCompare(kQToolBarHIToolbarIdentifier, identifier,
227 kCFCompareBackwards) == kCFCompareEqualTo) {
228 if (data && CFGetTypeID(data) == CFArrayGetTypeID()) {
229 CFArrayRef array = static_cast<CFArrayRef>(data);
230 QToolBar *toolbar = static_cast<QToolBar *>(const_cast<void *>(CFArrayGetValueAtIndex(array, 0)));
231 QMainWindowLayout *layout = static_cast<QMainWindowLayout *>(const_cast<void *>(CFArrayGetValueAtIndex(array, 1)));
232 item = createQToolBarInHIToolbarItem(toolbar, layout);
233 }
234 }
235 return item;
236}
237
238static const EventTypeSpec kToolbarEvents[] = {
239{ kEventClassToolbar, kEventToolbarGetDefaultIdentifiers },
240{ kEventClassToolbar, kEventToolbarGetAllowedIdentifiers },
241{ kEventClassToolbar, kEventToolbarCreateItemWithIdentifier },
242{ kEventClassToolbar, kEventToolbarItemAdded },
243{ kEventClassToolbar, kEventToolbarItemRemoved }
244};
245
246OSStatus QMainWindowLayout::qtmacToolbarDelegate(EventHandlerCallRef, EventRef event, void *data)
247{
248 QMainWindowLayout *mainWindowLayout = static_cast<QMainWindowLayout *>(data);
249 OSStatus result = eventNotHandledErr;