403c3edc24000ce5bed44acd6c57c61c9050d70d
[profile/ivi/qtbase.git] / src / widgets / dialogs / qnspanelproxy_mac.mm
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <qdialogbuttonbox.h>
43 #if defined(Q_WS_MAC)
44 #include <private/qt_mac_p.h>
45 #include <private/qcocoaintrospection_p.h>
46 #import <AppKit/AppKit.h>
47 #import <Foundation/Foundation.h>
48 #import <objc/objc-class.h>
49
50 QT_BEGIN_NAMESPACE
51 static QWidget *currentWindow = 0;
52 QT_END_NAMESPACE
53
54 QT_USE_NAMESPACE
55
56 @class QT_MANGLE_NAMESPACE(QNSPanelProxy);
57
58 @interface QT_MANGLE_NAMESPACE(QNSPanelProxy) : NSWindow {
59 }
60 - (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)windowStyle
61     backing:(NSBackingStoreType)bufferingType defer:(BOOL)deferCreation;
62 - (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)windowStyle
63     backing:(NSBackingStoreType)bufferingType defer:(BOOL)deferCreation screen:(NSScreen *)screen;
64 - (id)qt_fakeInitWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)windowStyle
65     backing:(NSBackingStoreType)bufferingType defer:(BOOL)deferCreation;
66 - (id)qt_fakeInitWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)windowStyle
67     backing:(NSBackingStoreType)bufferingType defer:(BOOL)deferCreation screen:(NSScreen *)screen;
68 @end
69
70 @implementation QT_MANGLE_NAMESPACE(QNSPanelProxy)
71 - (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)windowStyle
72       backing:(NSBackingStoreType)bufferingType defer:(BOOL)deferCreation
73 {
74     // remove evil flag
75     windowStyle &= ~NSUtilityWindowMask;
76         self = [self qt_fakeInitWithContentRect:contentRect styleMask:windowStyle
77                                         backing:bufferingType defer:deferCreation];
78     return self;
79 }
80
81 - (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)windowStyle
82       backing:(NSBackingStoreType)bufferingType defer:(BOOL)deferCreation screen:(NSScreen *)screen
83 {
84     // remove evil flag
85     windowStyle &= ~NSUtilityWindowMask;
86         return [self qt_fakeInitWithContentRect:contentRect styleMask:windowStyle
87                                         backing:bufferingType defer:deferCreation screen:screen];
88 }
89
90 - (id)qt_fakeInitWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)windowStyle
91     backing:(NSBackingStoreType)bufferingType defer:(BOOL)deferCreation
92 {
93     Q_UNUSED(contentRect);
94     Q_UNUSED(windowStyle);
95     Q_UNUSED(bufferingType);
96     Q_UNUSED(deferCreation);
97     return nil;
98 }
99
100 - (id)qt_fakeInitWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)windowStyle
101     backing:(NSBackingStoreType)bufferingType defer:(BOOL)deferCreation screen:(NSScreen *)screen
102 {
103     Q_UNUSED(contentRect);
104     Q_UNUSED(windowStyle);
105     Q_UNUSED(bufferingType);
106     Q_UNUSED(deferCreation);
107     Q_UNUSED(screen);
108     return nil;
109 }
110 @end
111
112 @class QT_MANGLE_NAMESPACE(QNSWindowProxy);
113
114 @interface QT_MANGLE_NAMESPACE(QNSWindowProxy) : NSWindow {
115 }
116 - (void)setTitle:(NSString *)title;
117 - (void)qt_fakeSetTitle:(NSString *)title;
118 @end
119
120 @implementation QT_MANGLE_NAMESPACE(QNSWindowProxy)
121 - (void)setTitle:(NSString *)title
122 {
123     QCFString cftitle(currentWindow->windowTitle());
124
125     // evil reverse engineering
126     if ([title isEqualToString:@"Print"]
127             || [title isEqualToString:@"Page Setup"]
128             || [[self className] isEqualToString:@"PMPrintingWindow"])
129         title = (NSString *)(static_cast<CFStringRef>(cftitle));
130     return [self qt_fakeSetTitle:title];
131 }
132
133 - (void)qt_fakeSetTitle:(NSString *)title
134 {
135     Q_UNUSED(title);
136 }
137 @end
138
139 QT_BEGIN_NAMESPACE
140
141 /*
142     Intercept the NSColorPanel constructor if the shared
143     color panel doesn't exist yet. What's going on here is
144     quite wacky, because we want to override the NSPanel
145     constructor and at the same time call the old NSPanel
146     constructor. So what we do is we effectively rename the
147     old NSPanel constructor qt_fakeInitWithContentRect:...
148     and have the new one call the old one.
149 */
150 void macStartInterceptNSPanelCtor()
151 {
152     qt_cocoa_change_implementation(
153             [NSPanel class],
154             @selector(initWithContentRect:styleMask:backing:defer:),
155             [QT_MANGLE_NAMESPACE(QNSPanelProxy) class],
156             @selector(initWithContentRect:styleMask:backing:defer:),
157             @selector(qt_fakeInitWithContentRect:styleMask:backing:defer:));
158     qt_cocoa_change_implementation(
159             [NSPanel class],
160             @selector(initWithContentRect:styleMask:backing:defer:screen:),
161             [QT_MANGLE_NAMESPACE(QNSPanelProxy) class],
162             @selector(initWithContentRect:styleMask:backing:defer:screen:),
163             @selector(qt_fakeInitWithContentRect:styleMask:backing:defer:screen:));
164 }
165
166 /*
167     Restore things as they were.
168 */
169 void macStopInterceptNSPanelCtor()
170 {
171     qt_cocoa_change_back_implementation(
172             [NSPanel class],
173             @selector(initWithContentRect:styleMask:backing:defer:screen:),
174             @selector(qt_fakeInitWithContentRect:styleMask:backing:defer:screen:));
175     qt_cocoa_change_back_implementation(
176             [NSPanel class],
177             @selector(initWithContentRect:styleMask:backing:defer:),
178             @selector(qt_fakeInitWithContentRect:styleMask:backing:defer:));
179 }
180
181 /*
182     Intercept the NSPrintPanel and NSPageLayout setTitle: calls. The
183     hack is similar as for NSColorPanel above.
184 */
185 void macStartInterceptWindowTitle(QWidget *window)
186 {
187     currentWindow = window;
188     qt_cocoa_change_implementation(
189             [NSWindow class],
190             @selector(setTitle:),
191             [QT_MANGLE_NAMESPACE(QNSWindowProxy) class],
192             @selector(setTitle:),
193             @selector(qt_fakeSetTitle:));
194 }
195
196 /*
197     Restore things as they were.
198 */
199 void macStopInterceptWindowTitle()
200 {
201     currentWindow = 0;
202     qt_cocoa_change_back_implementation(
203             [NSWindow class],
204             @selector(setTitle:),
205             @selector(qt_fakeSetTitle:));
206 }
207
208 /*
209     Doesn't really belong in here.
210 */
211 NSButton *macCreateButton(const char *text, NSView *superview)
212 {
213     static const NSRect buttonFrameRect = { { 0.0, 0.0 }, { 0.0, 0.0 } };
214
215     NSButton *button = [[NSButton alloc] initWithFrame:buttonFrameRect];
216     [button setButtonType:NSMomentaryLightButton];
217     [button setBezelStyle:NSRoundedBezelStyle];
218     [button setTitle:(NSString*)(CFStringRef)QCFString(QDialogButtonBox::tr(text)
219                                                        .remove(QLatin1Char('&')))];
220     [[button cell] setFont:[NSFont systemFontOfSize:
221             [NSFont systemFontSizeForControlSize:NSRegularControlSize]]];
222     [superview addSubview:button];
223     return button;
224 }
225
226 QT_END_NAMESPACE
227
228 #endif