1 | #include <QtGui/QtGui>
|
---|
2 | #include <QtGui/qmacnativewidget_mac.h>
|
---|
3 | #ifdef QT_MAC_USE_COCOA
|
---|
4 | #import <Cocoa/Cocoa.h>
|
---|
5 | #else
|
---|
6 | #include <Carbon/Carbon.h>
|
---|
7 | #endif
|
---|
8 |
|
---|
9 | int main(int argc, char **argv)
|
---|
10 | {
|
---|
11 | QApplication app(argc, argv);
|
---|
12 | #ifdef QT_MAC_USE_COCOA
|
---|
13 | //![0]
|
---|
14 | NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
---|
15 | NSWindow *window = [[NSWindow alloc] initWithContentRect:NSMakeRect(200, app.desktop()->height() - 200, 239, 200)
|
---|
16 | styleMask:NSTitledWindowMask | NSClosableWindowMask
|
---|
17 | | NSMiniaturizableWindowMask | NSResizableWindowMask
|
---|
18 | backing:NSBackingStoreBuffered defer:NO];
|
---|
19 |
|
---|
20 | QMacNativeWidget *nativeWidget = new QMacNativeWidget();
|
---|
21 | nativeWidget->move(0, 0);
|
---|
22 | nativeWidget->setPalette(QPalette(Qt::red));
|
---|
23 | nativeWidget->setAutoFillBackground(true);
|
---|
24 | QVBoxLayout *layout = new QVBoxLayout();
|
---|
25 | QPushButton *pushButton = new QPushButton("An Embedded Qt Button!", nativeWidget);
|
---|
26 | pushButton->setAttribute(Qt::WA_LayoutUsesWidgetRect); // Don't use the layout rect calculated from QMacStyle.
|
---|
27 | layout->addWidget(pushButton);
|
---|
28 | nativeWidget->setLayout(layout);
|
---|
29 |
|
---|
30 | // Adjust Cocoa layouts
|
---|
31 | NSView *nativeWidgetView = reinterpret_cast<NSView *>(nativeWidget->winId());
|
---|
32 | NSView *contentView = [window contentView];
|
---|
33 | [contentView setAutoresizesSubviews:YES];
|
---|
34 | [nativeWidgetView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
|
---|
35 | [nativeWidgetView setAutoresizesSubviews:YES];
|
---|
36 | NSView *pushButtonView = reinterpret_cast<NSView *>(pushButton->winId());
|
---|
37 | [pushButtonView setAutoresizingMask:NSViewWidthSizable];
|
---|
38 |
|
---|
39 | // Add the nativeWidget to the window.
|
---|
40 | [contentView addSubview:nativeWidgetView positioned:NSWindowAbove relativeTo:nil];
|
---|
41 | nativeWidget->show();
|
---|
42 | pushButton->show();
|
---|
43 |
|
---|
44 | // Show the window.
|
---|
45 | [window makeKeyAndOrderFront:window];
|
---|
46 | [pool release];
|
---|
47 | //![0]
|
---|
48 | #else
|
---|
49 | //![1]
|
---|
50 | Rect contentRect;
|
---|
51 | SetRect(&contentRect, 200, 200, 400, 400);
|
---|
52 | HIWindowRef windowRef;
|
---|
53 | CreateNewWindow(kDocumentWindowClass, kWindowStandardDocumentAttributes | kWindowCompositingAttribute | kWindowStandardHandlerAttribute | kWindowLiveResizeAttribute, &contentRect, &windowRef);
|
---|
54 | HIViewRef contentView = 0;
|
---|
55 | GetRootControl(windowRef, &contentView);
|
---|
56 |
|
---|
57 | QMacNativeWidget *nativeWidget = new QMacNativeWidget();
|
---|
58 | nativeWidget->move(0, 0);
|
---|
59 | nativeWidget->setPalette(QPalette(Qt::red));
|
---|
60 | nativeWidget->setAutoFillBackground(true);
|
---|
61 | QVBoxLayout *layout = new QVBoxLayout();
|
---|
62 | QPushButton *pushButton = new QPushButton("An Embedded Qt Button!", nativeWidget);
|
---|
63 | pushButton->setAttribute(Qt::WA_LayoutUsesWidgetRect); // Don't use the layout rect calculated from QMacStyle.
|
---|
64 | layout->addWidget(pushButton);
|
---|
65 | nativeWidget->setLayout(layout);
|
---|
66 | HIViewRef nativeWidgetView = reinterpret_cast<HIViewRef>(nativeWidget->winId());
|
---|
67 | // Add the nativeWidget to the window.
|
---|
68 | HIViewAddSubview(contentView, nativeWidgetView);
|
---|
|
---|