From 7054714114a76a34fdd8748d2d06948cb2756fe0 Mon Sep 17 00:00:00 2001
From: Roman Telezhynskyi <kroluku@gmail.com>
Date: Sat, 5 Dec 2015 11:50:32 +0200
Subject: [PATCH] Fixed dock menu on Mac OS.

--HG--
branch : develop
---
 src/app/tape/tmainwindow.cpp     | 41 +++++++++++++++++++++-----------
 src/app/tape/tmainwindow.h       |  1 +
 src/app/valentina/mainwindow.cpp |  4 +++-
 3 files changed, 31 insertions(+), 15 deletions(-)

diff --git a/src/app/tape/tmainwindow.cpp b/src/app/tape/tmainwindow.cpp
index 9f8a8d1cb..bbe2441e9 100644
--- a/src/app/tape/tmainwindow.cpp
+++ b/src/app/tape/tmainwindow.cpp
@@ -120,18 +120,8 @@ TMainWindow::TMainWindow(QWidget *parent)
 #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 2)
     // Mac OS Dock Menu
     QMenu *menu = new QMenu(this);
-
-    CreateWindowMenu(ui->menuWindow);
-
-    menu->addSeparator();
-
-    menu->addAction(ui->actionOpenIndividual);
-    menu->addAction(ui->actionOpenStandard);
-    menu->addAction(ui->actionOpenTemplate);
-
-    menu->addSeparator();
-
-    menu->addAction(ui->actionPreferences);
+    connect(menu, &QMenu::aboutToShow, this, &TMainWindow::AboutToShowDockMenu);
+    AboutToShowDockMenu();
 
     extern void qt_mac_set_dock_menu(QMenu *);
     qt_mac_set_dock_menu(menu);
@@ -765,6 +755,7 @@ void TMainWindow::ShowWindow()
         {
             const int offset = qvariant_cast<int>(v);
             QList<TMainWindow*> windows = qApp->MainWindows();
+            windows.at(offset)->raise();
             windows.at(offset)->activateWindow();
         }
     }
@@ -786,6 +777,28 @@ void TMainWindow::AboutQt()
 
 //---------------------------------------------------------------------------------------------------------------------
 #if defined(Q_OS_MAC)
+void TMainWindow::AboutToShowDockMenu()
+{
+    if (QMenu *menu = qobject_cast<QMenu *>(sender()))
+    {
+        menu->clear();
+        CreateWindowMenu(menu);
+
+        menu->addSeparator();
+
+        menu->addAction(ui->actionOpenIndividual);
+        menu->addAction(ui->actionOpenStandard);
+        menu->addAction(ui->actionOpenTemplate);
+
+        menu->addSeparator();
+
+        QAction *actionPreferences = menu->addAction(tr("Preferences"));
+        actionPreferences->setMenuRole(QAction::NoRole);
+        connect(actionPreferences, &QAction::triggered, this, &TMainWindow::Preferences);
+    }
+}
+
+//---------------------------------------------------------------------------------------------------------------------
 void TMainWindow::OpenAt(QAction *where)
 {
     const QString path = curFile.left(curFile.indexOf(where->text())) + where->text();
@@ -2758,7 +2771,7 @@ void TMainWindow::CreateWindowMenu(QMenu *menu)
     {
         TMainWindow *window = windows.at(i);
 
-        QString title = window->windowTitle();
+        QString title = QString("%1. %2").arg(i+1).arg(window->windowTitle());
         const int index = title.lastIndexOf("[*]");
         if (index != -1)
         {
@@ -2769,7 +2782,7 @@ void TMainWindow::CreateWindowMenu(QMenu *menu)
         action->setData(i);
         action->setCheckable(true);
         action->setMenuRole(QAction::NoRole);
-        if (window == this)
+        if (window->isActiveWindow())
         {
             action->setChecked(true);
         }
diff --git a/src/app/tape/tmainwindow.h b/src/app/tape/tmainwindow.h
index 13831d054..2b4f6b967 100644
--- a/src/app/tape/tmainwindow.h
+++ b/src/app/tape/tmainwindow.h
@@ -87,6 +87,7 @@ private slots:
     void AboutQt();
 
 #if defined(Q_OS_MAC)
+    void AboutToShowDockMenu();
     void OpenAt(QAction *where);
 #endif //defined(Q_OS_MAC)
 
diff --git a/src/app/valentina/mainwindow.cpp b/src/app/valentina/mainwindow.cpp
index f895c0516..314931425 100644
--- a/src/app/valentina/mainwindow.cpp
+++ b/src/app/valentina/mainwindow.cpp
@@ -172,7 +172,9 @@ MainWindow::MainWindow(QWidget *parent)
     actionOpenTape->setMenuRole(QAction::NoRole);
     connect(actionOpenTape, &QAction::triggered, this, &MainWindow::CreateMeasurements);
 
-    menu->addAction(ui->actionPreferences);
+    QAction *actionPreferences = menu->addAction(tr("Preferences"));
+    actionPreferences->setMenuRole(QAction::NoRole);
+    connect(actionPreferences, &QAction::triggered, this, &MainWindow::Preferences);
 
     extern void qt_mac_set_dock_menu(QMenu *);
     qt_mac_set_dock_menu(menu);