From 9ae09ba2676b5b2e26877bac91324ad2822b633a Mon Sep 17 00:00:00 2001 From: "andre@woesten.com" Date: Wed, 3 Sep 2008 09:51:49 +0000 Subject: [PATCH] - Added minimize to tray feature - Moved win32cpp-specific WM's into Types.hpp --- src/cube/MainWindowController.cpp | 2 ++ src/win32cpp/Application.cpp | 2 -- src/win32cpp/ApplicationThread.cpp | 6 ++-- src/win32cpp/SysTray.cpp | 50 +++++++++++++++++++++++++++--- src/win32cpp/SysTray.hpp | 11 ++++++- src/win32cpp/TopLevelWindow.cpp | 8 ++--- src/win32cpp/Types.hpp | 9 ++++++ src/win32cpp/Window.hpp | 3 +- 8 files changed, 74 insertions(+), 17 deletions(-) diff --git a/src/cube/MainWindowController.cpp b/src/cube/MainWindowController.cpp index 92eda8b99..8ac50a8c5 100644 --- a/src/cube/MainWindowController.cpp +++ b/src/cube/MainWindowController.cpp @@ -109,6 +109,8 @@ void MainWindowController::OnMainWindowCreated(Window* window) Application::Instance().SysTrayManager()->SetTooltip(uidTrayIcon, _T("And another test...")); Application::Instance().SysTrayManager()->SetPopupMenu(uidTrayIcon, myMenu); Application::Instance().SysTrayManager()->ShowBalloon(uidTrayIcon, _T("musikCube 2"), _T("Welcome to musikCube!"), 2); + Application::Instance().SysTrayManager()->EnableMinimizeToTray(uidTrayIcon); + static const int TransportViewHeight = 54; diff --git a/src/win32cpp/Application.cpp b/src/win32cpp/Application.cpp index 9914b10f5..4f0736f43 100644 --- a/src/win32cpp/Application.cpp +++ b/src/win32cpp/Application.cpp @@ -139,8 +139,6 @@ void Application::Run(TopLevelWindow& mainWindow) MSG msg; while (::GetMessage(&msg, NULL, 0, 0) > 0) { - Application::Instance().SysTrayManager()->WindowProc(msg.message, msg.wParam, msg.lParam); - ::TranslateMessage(&msg); ::DispatchMessage(&msg); } diff --git a/src/win32cpp/ApplicationThread.cpp b/src/win32cpp/ApplicationThread.cpp index f67a2d800..9a105114a 100644 --- a/src/win32cpp/ApplicationThread.cpp +++ b/src/win32cpp/ApplicationThread.cpp @@ -41,8 +41,6 @@ using namespace win32cpp; -#define CALL_WAITING (WM_USER + 1000) - ////////////////////////////////////////// ///\brief ///Constructor @@ -117,7 +115,7 @@ void ApplicationThread::NotifyMainThread() { if(this->helperWindow) { - ::PostMessage(this->helperWindow->Handle(), CALL_WAITING, NULL, NULL); + ::PostMessage(this->helperWindow->Handle(), WM_W32CPP_APPLICATIONTHREAD_CALL_WAITING, NULL, NULL); } } @@ -179,7 +177,7 @@ LRESULT ApplicationThread::HelperWindow::WindowProc(UINT message, WPARAM wParam, { switch (message) { - case CALL_WAITING: + case WM_W32CPP_APPLICATIONTHREAD_CALL_WAITING: // This is a ApplicationTread message ApplicationThread *thread = Application::Instance().Thread(); if(thread) diff --git a/src/win32cpp/SysTray.cpp b/src/win32cpp/SysTray.cpp index c137a417b..82993da88 100644 --- a/src/win32cpp/SysTray.cpp +++ b/src/win32cpp/SysTray.cpp @@ -40,12 +40,12 @@ #include ////////////////////////////////////////////////////////////////////////////// -#define WM_MC2_SYSTRAY (WM_USER + 100) using namespace win32cpp; IconList SysTray::iconList; MenuList SysTray::menuList; +OptionsList SysTray::optionsList; int SysTray::uidCounter = 100; SysTray::SysTray() @@ -117,13 +117,14 @@ bool SysTray::SetPopupMenu(UINT uid, MenuRef menu) return false; } -LRESULT SysTray::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) +LRESULT SysTray::WindowProc(HWND window, UINT message, WPARAM wParam, LPARAM lParam) { - if(SysTray::menuList.find(message - WM_MC2_SYSTRAY) != SysTray::menuList.end()) { + if(SysTray::menuList.find(message - WM_W32CPP_SYSTRAY) != SysTray::menuList.end()) { + UINT uid = message - WM_W32CPP_SYSTRAY; + switch(LOWORD(lParam)) { case WM_RBUTTONDOWN: { - UINT uid = message - WM_MC2_SYSTRAY; if(SysTray::menuList.find(uid) != SysTray::menuList.end()) { POINT mousePos = { 0 }; ::GetCursorPos(&mousePos); @@ -139,12 +140,48 @@ LRESULT SysTray::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) } } return 0; + case WM_LBUTTONDOWN: + { + if(SysTray::optionsList[uid] & SysTray::MINIMIZE_TO_TRAY) { + // get window object + Window* wnd = Window::SubclassedWindowFromHWND(SysTray::iconList[uid].hWnd); + + // restore window + wnd->Show(SW_SHOW); + wnd->Show(SW_RESTORE); + + } + } + return 0; + } + } + + // handle minimize to tray + if(message == WM_SIZE && wParam == SIZE_MINIMIZED) { + // iterate through list with icon options and look, if the assigned icon/window pair wants that + for(IconList::iterator i = SysTray::iconList.begin(); i != SysTray::iconList.end(); ++i) { + // look if there is a corresponding window + if(i->second.hWnd == window) { + if(SysTray::optionsList[i->second.uID] & SysTray::MINIMIZE_TO_TRAY) { + // get window object + Window* wnd = Window::SubclassedWindowFromHWND(window); + + // and finally minimize it to tray + wnd->Show(SW_SHOWMINIMIZED); + wnd->Show(SW_HIDE); + } + } } } return 0; } +void SysTray::EnableMinimizeToTray(UINT uid) +{ + SysTray::optionsList[uid] |= SysTray::MINIMIZE_TO_TRAY; +} + int SysTray::AddIcon(Window* window, HICON icon, const uistring& tooltip) { UINT uid = SysTray::uidCounter++; @@ -162,7 +199,7 @@ int SysTray::AddIcon(Window* window, HICON icon, const uistring& tooltip) } nid.hIcon = icon; - nid.uCallbackMessage = WM_MC2_SYSTRAY + uid; + nid.uCallbackMessage = WM_W32CPP_SYSTRAY + uid; // create icon if(!::Shell_NotifyIcon(NIM_ADD, &nid)) { @@ -177,5 +214,8 @@ int SysTray::AddIcon(Window* window, HICON icon, const uistring& tooltip) // add to icon list SysTray::iconList[uid] = nid; + // add to options list + SysTray::optionsList[uid] = 0; + return uid; } \ No newline at end of file diff --git a/src/win32cpp/SysTray.hpp b/src/win32cpp/SysTray.hpp index a90c9a868..8afefa966 100644 --- a/src/win32cpp/SysTray.hpp +++ b/src/win32cpp/SysTray.hpp @@ -51,27 +51,36 @@ namespace win32cpp { typedef std::map IconList; typedef std::map MenuList; +typedef std::map OptionsList; class SysTray { private: + enum Options { + MINIMIZE_TO_TRAY = 1 + }; + // Contains the list of notify icons static IconList iconList; // Contains a list of menus for each icon static MenuList menuList; + // Contains a list of options for each icon + static OptionsList optionsList; + // Each notify icon has its own UID. This counter increments // when an icon is created. static int uidCounter; public: - LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam); + LRESULT WindowProc(HWND window, UINT message, WPARAM wParam, LPARAM lParam); bool DeleteIcon(UINT uid); int AddIcon(Window* window, HICON icon, const uistring& tooltip = _T("")); bool SetIcon(UINT uid, HICON icon); bool SetTooltip(UINT uid, const uistring& tooltip); bool SetPopupMenu(UINT uid, MenuRef menu); bool ShowBalloon(UINT uid, const uistring& title, const uistring& text, UINT timeout, UINT icon = NIIF_INFO); + void EnableMinimizeToTray(UINT uid); /* ctor */ SysTray(); /* dtor */ ~SysTray(); diff --git a/src/win32cpp/TopLevelWindow.cpp b/src/win32cpp/TopLevelWindow.cpp index f7c86bafc..c04e4c5dc 100644 --- a/src/win32cpp/TopLevelWindow.cpp +++ b/src/win32cpp/TopLevelWindow.cpp @@ -155,6 +155,10 @@ void TopLevelWindow::Close() LRESULT TopLevelWindow::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) { + if(this == Application::Instance().MainWindow()) { + Application::Instance().SysTrayManager()->WindowProc(this->Handle(), message, wParam, lParam); + } + switch (message) { case WM_SIZE: @@ -189,10 +193,6 @@ LRESULT TopLevelWindow::WindowProc(UINT message, WPARAM wParam, LPARAM lPara } return 0; } - - if(this == Application::Instance().MainWindow()) { - Application::Instance().SysTrayManager()->WindowProc(message, wParam, lParam); - } return base::WindowProc(message, wParam, lParam); } diff --git a/src/win32cpp/Types.hpp b/src/win32cpp/Types.hpp index 3319ac8be..a044fd884 100644 --- a/src/win32cpp/Types.hpp +++ b/src/win32cpp/Types.hpp @@ -84,4 +84,13 @@ typedef sigslot::has_slots<> EventHandler; ////////////////////////////////////////////////////////////////////////////// +///\brief +///User-defined application-wide messages +#define WM_W32CPP (WM_USER + 1000) +#define WM_W32CPP_SYSTRAY (WM_W32CPP + 1) +#define WM_W32CPP_APPLICATIONTHREAD_CALL_WAITING (WM_W32CPP + 2) + +////////////////////////////////////////////////////////////////////////////// + + } // namespace win32cpp diff --git a/src/win32cpp/Window.hpp b/src/win32cpp/Window.hpp index d35d6bf78..6454df27d 100644 --- a/src/win32cpp/Window.hpp +++ b/src/win32cpp/Window.hpp @@ -231,6 +231,8 @@ public: // methods bool TabStop(); void SetTabStop(bool enabled); + static Window* SubclassedWindowFromHWND(HWND hwnd); + public: // operators operator bool() { return (this->windowHandle != NULL); } @@ -242,7 +244,6 @@ protected: // methods static void SubclassWindowProc(Window* window); static void UnSubclassWindowProc(Window* window); static bool IsWindowSubclassed(Window* window); - static Window* SubclassedWindowFromHWND(HWND hwnd); static Window* WindowUnderCursor(HWND* targetHwnd = NULL); static void BeginCapture(Window* window); static void EndCapture(Window* window);