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

Last change on this file since 859 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 5.8 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 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:BSD$
10** You may use this file under the terms of the BSD license as follows:
11**
12** "Redistribution and use in source and binary forms, with or without
13** modification, are permitted provided that the following conditions are
14** met:
15** * Redistributions of source code must retain the above copyright
16** notice, this list of conditions and the following disclaimer.
17** * Redistributions in binary form must reproduce the above copyright
18** notice, this list of conditions and the following disclaimer in
19** the documentation and/or other materials provided with the
20** distribution.
21** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22** the names of its contributors may be used to endorse or promote
23** products derived from this software without specific prior written
24** permission.
25**
26** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41#include <QtGui/QtGui>
42#include <QtGui/qmacnativewidget_mac.h>
43#ifdef QT_MAC_USE_COCOA
44#import <Cocoa/Cocoa.h>
45#else
46#include <Carbon/Carbon.h>
47#endif
48
49int main(int argc, char **argv)
50{
51 QApplication app(argc, argv);
52#ifdef QT_MAC_USE_COCOA
53//![0]
54 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
55 NSWindow *window = [[NSWindow alloc] initWithContentRect:NSMakeRect(200, app.desktop()->height() - 200, 239, 200)
56 styleMask:NSTitledWindowMask | NSClosableWindowMask
57 | NSMiniaturizableWindowMask | NSResizableWindowMask
58 backing:NSBackingStoreBuffered defer:NO];
59
60 QMacNativeWidget *nativeWidget = new QMacNativeWidget();
61 nativeWidget->move(0, 0);
62 nativeWidget->setPalette(QPalette(Qt::red));
63 nativeWidget->setAutoFillBackground(true);
64 QVBoxLayout *layout = new QVBoxLayout();
65 QPushButton *pushButton = new QPushButton("An Embedded Qt Button!", nativeWidget);
66 pushButton->setAttribute(Qt::WA_LayoutUsesWidgetRect); // Don't use the layout rect calculated from QMacStyle.
67 layout->addWidget(pushButton);
68 nativeWidget->setLayout(layout);
69
70 // Adjust Cocoa layouts
71 NSView *nativeWidgetView = reinterpret_cast<NSView *>(nativeWidget->winId());
72 NSView *contentView = [window contentView];
73 [contentView setAutoresizesSubviews:YES];
74 [nativeWidgetView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
75 [nativeWidgetView setAutoresizesSubviews:YES];
76 NSView *pushButtonView = reinterpret_cast<NSView *>(pushButton->winId());
77 [pushButtonView setAutoresizingMask:NSViewWidthSizable];
78
79 // Add the nativeWidget to the window.
80 [contentView addSubview:nativeWidgetView positioned:NSWindowAbove relativeTo:nil];
81 nativeWidget->show();
82 pushButton->show();
83
84 // Show the window.
85 [window makeKeyAndOrderFront:window];
86 [pool release];
87//![0]
88#else
89//![1]
90 Rect contentRect;
91 SetRect(&contentRect, 200, 200, 400, 400);
92 HIWindowRef windowRef;
93 CreateNewWindow(kDocumentWindowClass, kWindowStandardDocumentAttributes | kWindowCompositingAttribute | kWindowStandardHandlerAttribute | kWindowLiveResizeAttribute, &contentRect, &windowRef);
94 HIViewRef contentView = 0;
95 GetRootControl(windowRef, &contentView);
96
97 QMacNativeWidget *nativeWidget = new QMacNativeWidget();
98 nativeWidget->move(0, 0);
99 nativeWidget->setPalette(QPalette(Qt::red));
100 nativeWidget->setAutoFillBackground(true);
101 QVBoxLayout *layout = new QVBoxLayout();
102 QPushButton *pushButton = new QPushButton("An Embedded Qt Button!", nativeWidget);
103 pushButton->setAttribute(Qt::WA_LayoutUsesWidgetRect); // Don't use the layout rect calculated from QMacStyle.
104 layout->addWidget(pushButton);
105 nativeWidget->setLayout(layout);
106 HIViewRef nativeWidgetView = reinterpret_cast<HIViewRef>(nativeWidget->winId());
107 // Add the nativeWidget to the window.
108 HIViewAddSubview(contentView, nativeWidgetView);
109
110 // Adjust Carbon layouts
111 HILayoutInfo layoutInfo;
112 layoutInfo.version = kHILayoutInfoVersionZero;
113 HIViewGetLayoutInfo(nativeWidgetView, &layoutInfo);
114
115 layoutInfo.binding.top.toView = contentView;
116 layoutInfo.binding.top.kind = kHILayoutBindTop;
117 layoutInfo.binding.left.toView = contentView;
118 layoutInfo.binding.left.kind = kHILayoutBindLeft;
119 layoutInfo.binding.right.toView = contentView;
120 layoutInfo.binding.right.kind = kHILayoutBindRight;
121 layoutInfo.binding.bottom.toView = contentView;
122 layoutInfo.binding.bottom.kind = kHILayoutBindBottom;
123
124 HIViewSetLayoutInfo(nativeWidgetView, &layoutInfo);
125 HIViewApplyLayout(nativeWidgetView);
126
127 pushButton->show();
128 nativeWidget->show();
129 // Show the window.
130 ShowWindow(windowRef);
131//![1]
132#endif
133 return app.exec(); // gives us the same behavior in both
134}
Note: See TracBrowser for help on using the repository browser.