From 6b36d143043257b4937c3b41b4754ff78d5d2f39 Mon Sep 17 00:00:00 2001
From: Leo Franchi <lfranchi@kde.org>
Date: Fri, 10 Aug 2012 15:42:15 -0400
Subject: [PATCH] Update to Qocoa fixes

---
 src/libtomahawk/thirdparty/Qocoa/qocoa_mac.h  |  4 +--
 .../thirdparty/Qocoa/qtoolbartabdialog.h      | 22 +++++++++++++
 .../thirdparty/Qocoa/qtoolbartabdialog_mac.mm | 33 +++++++++++++------
 3 files changed, 47 insertions(+), 12 deletions(-)

diff --git a/src/libtomahawk/thirdparty/Qocoa/qocoa_mac.h b/src/libtomahawk/thirdparty/Qocoa/qocoa_mac.h
index ee620a3a5..053f57a06 100644
--- a/src/libtomahawk/thirdparty/Qocoa/qocoa_mac.h
+++ b/src/libtomahawk/thirdparty/Qocoa/qocoa_mac.h
@@ -32,7 +32,7 @@ static inline NSString* fromQString(const QString &string)
 {
     const QByteArray utf8 = string.toUtf8();
     const char* cString = utf8.constData();
-    return [[NSString alloc] initWithUTF8String:cString];
+    return [[[NSString alloc] initWithUTF8String:cString] autorelease];
 }
 
 static inline QString toQString(NSString *string)
@@ -45,7 +45,7 @@ static inline QString toQString(NSString *string)
 static inline NSImage* fromQPixmap(const QPixmap &pixmap)
 {
     CGImageRef cgImage = pixmap.toMacCGImageRef();
-    return [[NSImage alloc] initWithCGImage:cgImage size:NSZeroSize];
+    return [[[NSImage alloc] initWithCGImage:cgImage size:NSZeroSize] autorelease];
 }
 
 static inline void setupLayout(void *cocoaView, QWidget *parent)
diff --git a/src/libtomahawk/thirdparty/Qocoa/qtoolbartabdialog.h b/src/libtomahawk/thirdparty/Qocoa/qtoolbartabdialog.h
index 3949bd6f9..d1e9aee74 100644
--- a/src/libtomahawk/thirdparty/Qocoa/qtoolbartabdialog.h
+++ b/src/libtomahawk/thirdparty/Qocoa/qtoolbartabdialog.h
@@ -1,3 +1,25 @@
+/*
+ Copyright (C) 2012 by Leo Franchi <lfranchi@kde.org>
+
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to deal
+ in the Software without restriction, including without limitation the rights
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ */
+
 #ifndef QTOOLBARTABWIDGET_H
 #define QTOOLBARTABWIDGET_H
 
diff --git a/src/libtomahawk/thirdparty/Qocoa/qtoolbartabdialog_mac.mm b/src/libtomahawk/thirdparty/Qocoa/qtoolbartabdialog_mac.mm
index e36a1f15e..7c5fbced2 100644
--- a/src/libtomahawk/thirdparty/Qocoa/qtoolbartabdialog_mac.mm
+++ b/src/libtomahawk/thirdparty/Qocoa/qtoolbartabdialog_mac.mm
@@ -21,7 +21,6 @@
  */
 
 #include "qtoolbartabdialog.h"
-#include "moc_qtoolbartabdialog.cpp"
 
 #include "qocoa_mac.h"
 
@@ -90,7 +89,7 @@ class QToolbarTabDialogPrivate {
 public:
     QToolbarTabDialogPrivate(QToolbarTabDialog* dialog) : q(dialog),
                                                           currentPane(NULL),
-                                                          minimumWidthForToolbar(0)
+                                                          minimumWidth(0)
     {
     }
 
@@ -116,7 +115,7 @@ public:
             [prefsWindow setFrame:windowFrame display:NO];
             [prefsWindow setMinSize: windowFrame.size];
         }
-        minimumWidthForToolbar = windowFrame.size.width;
+        minimumWidth = windowFrame.size.width;
     }
 
     void showPaneWithIdentifier(NSString* ident) {
@@ -136,6 +135,8 @@ public:
         Q_ASSERT(newPage);
         if (!newPage)
             return;
+        
+        const NSRect oldFrame = [[prefsWindow contentView] frame];
 
         // Clear first responder on window and set a temporary NSView on the window
         // while we change the widget out underneath
@@ -146,6 +147,8 @@ public:
         [tempView release];
 
         QSize sizeToUse = newPage->sizeHint().isNull() ? newPage->size() : newPage->sizeHint();
+        sizeToUse.setWidth(qMax(sizeToUse.width(), newPage->minimumWidth()));
+        sizeToUse.setHeight(qMax(sizeToUse.height(), newPage->minimumHeight()));
 
         static const int spacing = 4;
 
@@ -156,8 +159,10 @@ public:
         newFrame.size.height = sizeToUse.height() + ([prefsWindow frame].size.height - [[prefsWindow contentView] frame].size.height) + spacing;
         newFrame.size.width = sizeToUse.width() + spacing;
 
-        //Ensure the full toolbar still fits
-        if (newFrame.size.width < minimumWidthForToolbar) newFrame.size.width =  minimumWidthForToolbar;
+        // Don't resize the width---only the height, so use the maximum width for any page
+        // or the same width as before, if the user already resized it to be larger.
+        newFrame.size.width < minimumWidth ? newFrame.size.width =  minimumWidth
+                                           : newFrame.size.width = qMax(newFrame.size.width, oldFrame.size.width);
 
         // Preserve upper left point of window during resize.
         newFrame.origin.y += ([[prefsWindow contentView] frame].size.height - sizeToUse.height()) - spacing;
@@ -180,11 +185,11 @@ public:
 
         if (newPage->sizePolicy().horizontalPolicy() == QSizePolicy::Fixed) {
             canResize = NO;
-            maxSize.width = minSize.width;
+            maxSize.width = sizeToUse.width();
         }
         if (newPage->sizePolicy().verticalPolicy() == QSizePolicy::Fixed) {
             canResize = NO;
-            maxSize.height = minSize.height;
+            maxSize.height = sizeToUse.height();
         }
 
 
@@ -192,6 +197,7 @@ public:
         [prefsWindow setShowsResizeIndicator:canResize];
 
         [prefsWindow setTitle:ident];
+        [prefsWindow makeFirstResponder:[panes objectForKey:ident]];
 
         [pool drain];
     }
@@ -220,7 +226,7 @@ public:
     NSToolbar *toolBar;
     NSString* currentPane;
 
-    int minimumWidthForToolbar;
+    int minimumWidth;
 };
 
 
@@ -320,11 +326,10 @@ public:
 
 -(NSSize)windowWillResize:(NSWindow *)sender toSize:(NSSize)frameSize
 {
-    Q_UNUSED(sender);
     if (!pimpl)
         return frameSize;
 
-    pimpl->resizeCurrentPageToSize(frameSize);
+    pimpl->resizeCurrentPageToSize([[sender contentView] frame].size);
 
     return frameSize;
 }
@@ -398,6 +403,8 @@ void QToolbarTabDialog::addTab(QWidget* page, const QPixmap& icon, const QString
     [nativeView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
     [nativeView setAutoresizesSubviews:YES];
 
+    pimpl->minimumWidth = qMax(pimpl->minimumWidth, page->sizeHint().width());
+    
     nativeWidget->show();
 
     ItemData data;
@@ -433,6 +440,11 @@ void QToolbarTabDialog::setCurrentIndex(int index)
 
 void QToolbarTabDialog::show()
 {
+    Q_ASSERT(pimpl);
+    if (!pimpl)
+        return;
+
+    [pimpl->prefsWindow center];
     [pimpl->prefsWindow makeKeyAndOrderFront:nil];
 }
 
@@ -446,3 +458,4 @@ void QToolbarTabDialog::hide()
     emit accepted();
 }
 
+#include "moc_qtoolbartabdialog.cpp"