- Added minimize to tray feature

- Moved win32cpp-specific WM's into Types.hpp
This commit is contained in:
andre@woesten.com 2008-09-03 09:51:49 +00:00
parent 3aa5ded910
commit 9ae09ba267
8 changed files with 74 additions and 17 deletions

View File

@ -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;

View File

@ -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);
}

View File

@ -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)

View File

@ -40,12 +40,12 @@
#include <win32cpp/SysTray.hpp>
//////////////////////////////////////////////////////////////////////////////
#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;
}

View File

@ -51,27 +51,36 @@ namespace win32cpp {
typedef std::map<UINT, NOTIFYICONDATA> IconList;
typedef std::map<UINT, MenuRef> MenuList;
typedef std::map<UINT, UINT> 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();

View File

@ -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);
}

View File

@ -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

View File

@ -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);