src/gui/widgets/qcocoatoolbardelegate_mac.mm
changeset 0 1918ee327afb
child 4 3b1da2848fc7
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 QtGui module 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 #import <private/qcocoatoolbardelegate_mac_p.h>
       
    43 #ifdef QT_MAC_USE_COCOA
       
    44 #include <private/qmainwindowlayout_p.h>
       
    45 #include <private/qt_mac_p.h>
       
    46 #include <private/qt_cocoa_helpers_mac_p.h>
       
    47 #include <private/qcocoaview_mac_p.h>
       
    48 #include <private/qwidget_p.h>
       
    49 #include <qtoolbar.h>
       
    50 #include <qlayout.h>
       
    51 #include <qdebug.h>
       
    52 
       
    53 QT_BEGIN_NAMESPACE
       
    54 extern QWidgetPrivate *qt_widget_private(QWidget *widget);
       
    55 QT_END_NAMESPACE
       
    56 
       
    57 QT_FORWARD_DECLARE_CLASS(QMainWindowLayout);
       
    58 QT_FORWARD_DECLARE_CLASS(QToolBar);
       
    59 QT_FORWARD_DECLARE_CLASS(QCFString);
       
    60 
       
    61 @implementation QCocoaToolBarDelegate
       
    62 
       
    63 - (id)initWithMainWindowLayout:(QMainWindowLayout *)layout
       
    64 {
       
    65     self = [super init];
       
    66     if (self) {
       
    67         mainWindowLayout = layout;
       
    68     }
       
    69     return self;
       
    70 }
       
    71 
       
    72 - (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar *)toolbar
       
    73 {
       
    74     Q_UNUSED(toolbar);
       
    75     return [NSArray arrayWithObject:@"com.trolltech.qt.nstoolbar-qtoolbar"];
       
    76 }
       
    77 
       
    78 - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar
       
    79 {
       
    80     return [self toolbarAllowedItemIdentifiers:toolbar];
       
    81 }
       
    82 
       
    83 - (void)toolbarDidRemoveItem:(NSNotification *)notification
       
    84 {
       
    85     NSToolbarItem *item = [[notification userInfo] valueForKey:@"item"];
       
    86     mainWindowLayout->unifiedToolbarHash.remove(item);
       
    87     for (int i = 0; i < mainWindowLayout->toolbarItemsCopy.size(); ++i) {
       
    88         if (mainWindowLayout->toolbarItemsCopy.at(i) == item) {
       
    89             // I know about it, so release it.
       
    90             mainWindowLayout->toolbarItemsCopy.removeAt(i);
       
    91             mainWindowLayout->qtoolbarsInUnifiedToolbarList.removeAt(i);
       
    92             [item release];
       
    93             break;
       
    94         }
       
    95     }
       
    96 }
       
    97 
       
    98 - (NSToolbarItem *)toolbar:(NSToolbar *)nstoolbar itemForItemIdentifier:(NSString *)itemIdentifier
       
    99     willBeInsertedIntoToolbar:(BOOL)flag
       
   100 {
       
   101     Q_UNUSED(flag);
       
   102     Q_UNUSED(nstoolbar);
       
   103     QToolBar *tb = mainWindowLayout->cocoaItemIDToToolbarHash.value(
       
   104             QT_PREPEND_NAMESPACE(qt_mac_NSStringToQString)(itemIdentifier));
       
   105     NSToolbarItem *item = nil;
       
   106     if (tb) {
       
   107         item = [[NSToolbarItem alloc] initWithItemIdentifier:itemIdentifier];
       
   108         mainWindowLayout->unifiedToolbarHash.insert(item, tb);
       
   109     }
       
   110     return item;
       
   111 }
       
   112 
       
   113 - (void)toolbarWillAddItem:(NSNotification *)notification
       
   114 {
       
   115     NSToolbarItem *item = [[notification userInfo] valueForKey:@"item"];
       
   116     QToolBar *tb = mainWindowLayout->cocoaItemIDToToolbarHash.value(
       
   117             QT_PREPEND_NAMESPACE(qt_mac_NSStringToQString)([item itemIdentifier]));
       
   118     if (!tb)
       
   119         return; // I can't really do anything about this.
       
   120     [item retain];
       
   121     [item setView:QT_PREPEND_NAMESPACE(qt_mac_nativeview_for)(tb)];
       
   122 
       
   123     NSArray *items = [[qt_mac_window_for(mainWindowLayout->layoutState.mainWindow->window()) toolbar] items];
       
   124     int someIndex = 0;
       
   125     for (NSToolbarItem *i in items) {
       
   126         if (i == item)
       
   127             break;
       
   128         ++someIndex;
       
   129     }
       
   130     mainWindowLayout->toolbarItemsCopy.insert(someIndex, item);
       
   131 
       
   132     // This is synchronization code that was needed in Carbon, but may not be needed anymore here.
       
   133     QToolBar *toolbar = mainWindowLayout->unifiedToolbarHash.value(item);
       
   134     if (toolbar) {
       
   135         int toolbarIndex = mainWindowLayout->qtoolbarsInUnifiedToolbarList.indexOf(toolbar);
       
   136         if (someIndex != toolbarIndex) {
       
   137             // Dang, we must be out of sync, rebuild it from the "toolbarItemsCopy"
       
   138             mainWindowLayout->qtoolbarsInUnifiedToolbarList.clear();
       
   139             for (int i = 0; i < mainWindowLayout->toolbarItemsCopy.size(); ++i) {
       
   140                 // This will either append the correct toolbar or an
       
   141                 // null toolbar. This is fine because this list
       
   142                 // is really only kept to make sure that things are but in the right order.
       
   143                 mainWindowLayout->qtoolbarsInUnifiedToolbarList.append(
       
   144                                   mainWindowLayout->unifiedToolbarHash.value(mainWindowLayout->
       
   145                                                                         toolbarItemsCopy.at(i)));
       
   146             }
       
   147         }
       
   148         toolbar->update();
       
   149     }
       
   150 }
       
   151 
       
   152 @end
       
   153 #endif // QT_MAC_USE_COCOA