Flush current work
[desktopswitcher.git] / switcherwindow.cpp
blob9ad94e5afb881a5b6237d3e17f6fe451d740d73a
1 // $Id: switcherwindow.cpp,v 1.3 2003/08/03 16:56:29 nedko Exp $
2 //
3 // Desktop Switcher
4 // Copyright (C) 2000,2001,2002 Nedko Arnaudov <nedko@users.sourceforge.net>
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "ph.h"
21 #include "SwitcherWindow.h"
22 #include "DesktopSwitcher.h"
23 #include "Desktop.h"
24 #include "RunDialog.h"
25 #include "resource.h"
26 #include "png.h"
28 #define BACKGROUD_COLOR RGB(96,96,96)
29 #define TEXT_COLOR RGB(220,220,220)
30 #define DOCK 10
32 CSwitcherWindow::CSwitcherWindow(CDesktopSwitcher *pDesktopSwitcher, CDesktop *pDesktop)
34 m_pDesktopSwitcher = pDesktopSwitcher;
35 m_pDesktop = pDesktop;
36 m_pMoveWnd = NULL;
39 CSwitcherWindow::~CSwitcherWindow()
43 BOOL CSwitcherWindow::Create()
45 RECT rect = {0,0,g_Image.GetWidth(),g_Image.GetHeight()};
46 if (!CWindowImpl<CSwitcherWindow>::Create(NULL, // Desktop is our parent
47 rect, // Window rectangle
48 "rDesktop Switcher", // Window name
49 WS_POPUP, // Style
50 WS_EX_TOPMOST|WS_EX_TOOLWINDOW|WS_EX_LAYERED, // Extended style
51 0))
53 DWORD dwError = GetLastError();
54 return FALSE;
57 m_pDesktopSwitcher->RegisterHotKeys(m_hWnd);
59 DrawWindow();
61 return TRUE;
64 LRESULT CSwitcherWindow::OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
66 m_pDesktopSwitcher->Exit();
67 return 0;
70 LRESULT CSwitcherWindow::OnHotKey(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
72 WORD wVKey = HIWORD(lParam);
73 WORD wModifiers = LOWORD(lParam);
74 m_pDesktopSwitcher->HotKeyPressed(wVKey,wModifiers);
76 return 0;
79 void CSwitcherWindow::ToggleVisible()
81 ShowWindow(IsWindowVisible()?SW_HIDE:SW_SHOW);
84 LRESULT CSwitcherWindow::OnRButtonDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
86 unsigned int n = 0;
87 HMENU hMenu = CreatePopupMenu();
88 MENUITEMINFO mi;
89 mi.cbSize = sizeof(MENUITEMINFO);
90 mi.fMask = MIIM_TYPE|MIIM_ID;
91 mi.fType = MFT_STRING;
93 mi.dwTypeData = "Run...";
94 mi.wID = ID_RUN;
95 InsertMenuItem(hMenu,n++,TRUE,&mi);
97 mi.dwTypeData = "Exit";
98 mi.wID = ID_EXIT;
99 InsertMenuItem(hMenu,n++,TRUE,&mi);
101 POINT pt;
102 pt.x = LOWORD(lParam);
103 pt.y = HIWORD(lParam);
105 TrackPopupMenu(hMenu,TPM_LEFTALIGN,pt.x,pt.y,0,m_hWnd,NULL);
106 return 0;
109 void CSwitcherWindow::DrawWindow()
111 const char *pszDesktopName = m_pDesktop->GetDesktopName();
112 HDC hScreenDC = NULL;
113 HDC hMemoryDC = NULL;
114 HBITMAP hBitmap = NULL;
115 RECT r;
117 GetWindowRect(&r);
119 // setup the blend function
120 BLENDFUNCTION blendPixelFunction = { AC_SRC_OVER, 0, 0xFF,AC_SRC_ALPHA };
122 POINT ptSrc = {0,0}; // start point of the copy from hMemoryDC to hScreenDC
123 POINT ptWindowScreenPosition = {r.left,r.top};
124 SIZE szWindow = {g_Image.GetWidth(), g_Image.GetHeight()};
126 if (pszDesktopName == NULL)
127 goto Exit;
129 hScreenDC = ::GetDC(m_hWnd);
130 if (hScreenDC == NULL)
131 goto Exit;
133 hMemoryDC = ::CreateCompatibleDC(hScreenDC);
134 if (hMemoryDC == NULL)
135 goto Exit;
137 hBitmap = g_Image.CreateBitmap(hMemoryDC);
138 if (hBitmap == NULL)
139 goto Exit;
141 if (SelectObject(hMemoryDC,hBitmap) == NULL)
142 goto Exit;
144 // perform the alpha blend
145 UpdateLayeredWindow(m_hWnd,
146 hScreenDC,
147 &ptWindowScreenPosition,
148 &szWindow,
149 hMemoryDC,
150 &ptSrc,
152 &blendPixelFunction,
153 ULW_ALPHA);
155 Exit:
156 if (hMemoryDC)
157 VERIFY(DeleteDC(hMemoryDC));
159 if (hScreenDC)
160 VERIFY(ReleaseDC(hScreenDC));
162 if (hBitmap)
163 VERIFY(DeleteObject(hBitmap));
166 LRESULT CSwitcherWindow::OnNCHitText(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
168 return HTCAPTION;
171 LRESULT CSwitcherWindow::OnRun(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
173 CRunDialog dlg;
175 const char *szExec = dlg.DoModal(m_hWnd);
176 if (szExec)
177 m_pDesktop->Run(szExec);
178 return 0;
181 LRESULT CSwitcherWindow::OnExit(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
183 m_pDesktopSwitcher->Exit();
184 return 0;
187 void CSwitcherWindow::Destroy()
189 VERIFY(DestroyWindow());
192 void CSwitcherWindow::OnRun()
194 PostMessage(WM_COMMAND,MAKEWPARAM(ID_RUN,1),NULL);
197 LRESULT CSwitcherWindow::OnExitSizeMove(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
199 RECT rect;
200 VERIFY(GetWindowRect(&rect));
201 if (rect.left > -DOCK && rect.left < DOCK)
203 rect.right -= rect.left;
204 rect.left = 0;
207 if (rect.top+11 > -DOCK && rect.top+11 < DOCK)
209 rect.bottom -= rect.top+11;
210 rect.top = -11;
213 VERIFY(SetWindowPos(NULL,rect.left,rect.top,10,10,SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOSIZE|SWP_NOZORDER|SWP_SHOWWINDOW));*/
214 return 0;