Debian Codegraph @

tests/manual/cocoa/qt_on_cocoa/main.mm

raw
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
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 The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28
29 #include "rasterwindow.h"
30
31 #include <QtGui>
32 #include <QtWidgets/QtWidgets>
33
34 #include <AppKit/AppKit.h>
35
36
37 @interface ContentView : NSView
38 @end
39
40 @implementation ContentView
41 - (void)drawRect:(NSRect)dirtyRect {
42 [[NSColor whiteColor] setFill];
43 NSRectFill(dirtyRect);
44 }
45
46 - (void)cursorUpdate:(NSEvent *)theEvent
47 {
48 Q_UNUSED(theEvent);
49 [[NSCursor pointingHandCursor] set];
50 }
51 @end
52
53 @interface AppDelegate : NSObject <NSApplicationDelegate>
54 @end
55
56 @implementation AppDelegate {
57 QGuiApplication *m_app;
58 QWindow *m_window;
59 }
60
61 - (instancetype)initWithArgc:(int)argc argv:(const char **)argv
62 {
63 if ((self = [self init])) {
64 m_app = new QGuiApplication(argc, const_cast<char **>(argv));
65 }
66 return self;
67 }
68
69 - (void)applicationWillFinishLaunching:(NSNotification *)notification
70 {
71 Q_UNUSED(notification);
72
73 // Create the NSWindow
74 NSRect frame = NSMakeRect(500, 500, 500, 500);
75 NSWindow *window = [[NSWindow alloc] initWithContentRect:frame
76 styleMask:NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask
77 backing:NSBackingStoreBuffered
78 defer:NO];
79
80 NSString *title = @"This the NSWindow window";
81 [window setTitle:title];
82 [window setBackgroundColor:[NSColor blueColor]];
83
84 NSView *contentView = [[[ContentView alloc] initWithFrame:frame] autorelease];
85 [contentView addTrackingArea:[[NSTrackingArea alloc] initWithRect:[contentView frame]
86 options:NSTrackingActiveInActiveApp | NSTrackingInVisibleRect | NSTrackingCursorUpdate
87 owner:contentView userInfo:nil]];
88
89 // Create the QWindow, add its NSView to the content view
90 m_window = new RasterWindow;
91 m_window->setObjectName("RasterWindow");
92 m_window->setCursor(Qt::CrossCursor);
93 m_window->setGeometry(QRect(0, 0, 300, 300));
94
95 QWindow *childWindow = new RasterWindow;
96 childWindow->setObjectName("RasterWindowChild");
97 childWindow->setParent(m_window);
98 childWindow->setCursor(Qt::BusyCursor);
99 childWindow->setGeometry(50, 50, 100, 100);
100
101 NSTextField *textField = [[NSTextField alloc] initWithFrame:NSMakeRect(10, 10, 80, 25)];
102 [reinterpret_cast<NSView *>(childWindow->winId()) addSubview:textField];
103
104 [contentView addSubview:reinterpret_cast<NSView *>(m_window->winId())];
105
106 window.contentView = contentView;
107
108 // Show the NSWindow delayed, so that we can verify that Qt picks up the right
109 // notifications to expose the window when it does become visible.
110 dispatch_async(dispatch_get_main_queue(), ^{
111 [window makeKeyAndOrderFront:NSApp];
112 });
113 }
114
115 - (void)applicationWillTerminate:(NSNotification *)notification
116 {
117 Q_UNUSED(notification);
118 delete m_window;
119 delete m_app;
120 }
121
122 @end
123
124 int main(int argc, const char *argv[])
125 {
126 // Create NSApplicaiton with delgate
127 NSApplication *app = [NSApplication sharedApplication];
128 app.delegate = [[AppDelegate alloc] initWithArgc:argc argv:argv];
129 return NSApplicationMain(argc, argv);
130 }