doc/src/snippets/qmacnativewidget/main.mm
changeset 0 1918ee327afb
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the documentation of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    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 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    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 
       
    50 int 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 }