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);
|
---|
69 |
|
---|
70 | // Adjust Carbon layouts
|
---|
71 | HILayoutInfo layoutInfo;
|
---|
72 | layoutInfo.version = kHILayoutInfoVersionZero;
|
---|
73 | HIViewGetLayoutInfo(nativeWidgetView, &layoutInfo);
|
---|
74 |
|
---|
75 | layoutInfo.binding.top.toView = contentView;
|
---|
76 | layoutInfo.binding.top.kind = kHILayoutBindTop;
|
---|
77 | layoutInfo.binding.left.toView = contentView;
|
---|
78 | layoutInfo.binding.left.kind = kHILayoutBindLeft;
|
---|
79 | layoutInfo.binding.right.toView = contentView;
|
---|
80 | layoutInfo.binding.right.kind = kHILayoutBindRight;
|
---|
81 | layoutInfo.binding.bottom.toView = contentView;
|
---|
82 | layoutInfo.binding.bottom.kind = kHILayoutBindBottom;
|
---|
83 |
|
---|
84 | HIViewSetLayoutInfo(nativeWidgetView, &layoutInfo);
|
---|
85 | HIViewApplyLayout(nativeWidgetView);
|
---|
86 |
|
---|
87 | pushButton->show();
|
---|
88 | nativeWidget->show();
|
---|
89 | // Show the window.
|
---|
90 | ShowWindow(windowRef);
|
---|
91 | //![1]
|
---|
92 | #endif
|
---|
93 | return app.exec(); // gives us the same behavior in both
|
---|
94 | }
|
---|