source: trunk/doc/src/snippets/qmacnativewidget/main.mm@ 561

Last change on this file since 561 was 561, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.1 sources.

File size: 5.7 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the documentation of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
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.
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**
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.
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 have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include <QtGui/QtGui>
43#include <QtGui/qmacnativewidget_mac.h>
44#ifdef QT_MAC_USE_COCOA
45#import <Cocoa/Cocoa.h>
46#else
47#include <Carbon/Carbon.h>
48#endif
49
50int main(int argc, char **argv)
51{
52 QApplication app(argc, argv);
53#ifdef QT_MAC_USE_COCOA
54//![0]
55 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
56 NSWindow *window = [[NSWindow alloc] initWithContentRect:NSMakeRect(200, app.desktop()->height() - 200, 239, 200)
57 styleMask:NSTitledWindowMask | NSClosableWindowMask
58 | NSMiniaturizableWindowMask | NSResizableWindowMask
59 backing:NSBackingStoreBuffered defer:NO];
60
61 QMacNativeWidget *nativeWidget = new QMacNativeWidget();
62 nativeWidget->move(0, 0);
63 nativeWidget->setPalette(QPalette(Qt::red));
64 nativeWidget->setAutoFillBackground(true);
65 QVBoxLayout *layout = new QVBoxLayout();
66 QPushButton *pushButton = new QPushButton("An Embedded Qt Button!", nativeWidget);
67 pushButton->setAttribute(Qt::WA_LayoutUsesWidgetRect); // Don't use the layout rect calculated from QMacStyle.
68 layout->addWidget(pushButton);
69 nativeWidget->setLayout(layout);
70
71 // Adjust Cocoa layouts
72 NSView *nativeWidgetView = reinterpret_cast<NSView *>(nativeWidget->winId());
73 NSView *contentView = [window contentView];
74 [contentView setAutoresizesSubviews:YES];
75 [nativeWidgetView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
76 [nativeWidgetView setAutoresizesSubviews:YES];
77 NSView *pushButtonView = reinterpret_cast<NSView *>(pushButton->winId());
78 [pushButtonView setAutoresizingMask:NSViewWidthSizable];
79
80 // Add the nativeWidget to the window.
81 [contentView addSubview:nativeWidgetView positioned:NSWindowAbove relativeTo:nil];
82 nativeWidget->show();
83 pushButton->show();
84
85 // Show the window.
86 [window makeKeyAndOrderFront:window];
87 [pool release];
88//![0]
89#else
90//![1]
91 Rect contentRect;
92 SetRect(&contentRect, 200, 200, 400, 400);
93 HIWindowRef windowRef;
94 CreateNewWindow(kDocumentWindowClass, kWindowStandardDocumentAttributes | kWindowCompositingAttribute | kWindowStandardHandlerAttribute | kWindowLiveResizeAttribute, &contentRect, &windowRef);
95 HIViewRef contentView = 0;
96 GetRootControl(windowRef, &contentView);
97
98 QMacNativeWidget *nativeWidget = new QMacNativeWidget();
99 nativeWidget->move(0, 0);
100 nativeWidget->setPalette(QPalette(Qt::red));
101 nativeWidget->setAutoFillBackground(true);
102 QVBoxLayout *layout = new QVBoxLayout();
103 QPushButton *pushButton = new QPushButton("An Embedded Qt Button!", nativeWidget);
104 pushButton->setAttribute(Qt::WA_LayoutUsesWidgetRect); // Don't use the layout rect calculated from QMacStyle.
105 layout->addWidget(pushButton);
106 nativeWidget->setLayout(layout);
107 HIViewRef nativeWidgetView = reinterpret_cast<HIViewRef>(nativeWidget->winId());
108 // Add the nativeWidget to the window.
109 HIViewAddSubview(contentView, nativeWidgetView);
110
111 // Adjust Carbon layouts
112 HILayoutInfo layoutInfo;
113 layoutInfo.version = kHILayoutInfoVersionZero;
114 HIViewGetLayoutInfo(nativeWidgetView, &layoutInfo);
115
116 layoutInfo.binding.top.toView = contentView;
117 layoutInfo.binding.top.kind = kHILayoutBindTop;
118 layoutInfo.binding.left.toView = contentView;
119 layoutInfo.binding.left.kind = kHILayoutBindLeft;
120 layoutInfo.binding.right.toView = contentView;
121 layoutInfo.binding.right.kind = kHILayoutBindRight;
122 layoutInfo.binding.bottom.toView = contentView;
123 layoutInfo.binding.bottom.kind = kHILayoutBindBottom;
124
125 HIViewSetLayoutInfo(nativeWidgetView, &layoutInfo);
126 HIViewApplyLayout(nativeWidgetView);
127
128 pushButton->show();
129 nativeWidget->show();
130 // Show the window.
131 ShowWindow(windowRef);
132//![1]
133#endif
134 return app.exec(); // gives us the same behavior in both
135}
Note: See TracBrowser for help on using the repository browser.