*** empty log message ***
[desktopswitcher.git] / switcherwindow.cpp
blob1be24700f1e34f962e428f177e27b4e07bb830f0
1 // $Id: switcherwindow.cpp,v 1.2 2002/06/29 15:03:33 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;
37 m_hBackground = NULL;
38 m_hdcMemory = NULL;
41 CSwitcherWindow::~CSwitcherWindow()
43 if (m_hBackground)
44 VERIFY(DeleteObject(m_hBackground));
46 if (m_hdcMemory)
47 VERIFY(DeleteDC(m_hdcMemory));
50 BOOL CSwitcherWindow::Create()
52 RECT rect = {0,0,g_Image.GetWidth(),g_Image.GetHeight()};
53 if (!CWindowImpl<CSwitcherWindow>::Create(NULL, // Desktop is our parent
54 rect, // Window rectangle
55 "rDesktop Switcher", // Window name
56 WS_POPUP, // Style
57 WS_EX_TOPMOST|WS_EX_TOOLWINDOW|WS_EX_LAYERED, // Extended style
58 0))
60 DWORD dwError = GetLastError();
61 return FALSE;
64 m_pDesktopSwitcher->RegisterHotKeys(m_hWnd);
66 DrawWindow();
68 return TRUE;
71 LRESULT CSwitcherWindow::OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
73 m_pDesktopSwitcher->Exit();
74 return 0;
77 LRESULT CSwitcherWindow::OnHotKey(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
79 WORD wVKey = HIWORD(lParam);
80 WORD wModifiers = LOWORD(lParam);
81 m_pDesktopSwitcher->HotKeyPressed(wVKey,wModifiers);
83 return 0;
86 void CSwitcherWindow::ToggleVisible()
88 ShowWindow(IsWindowVisible()?SW_HIDE:SW_SHOW);
91 LRESULT CSwitcherWindow::OnRButtonDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
93 unsigned int n = 0;
94 HMENU hMenu = CreatePopupMenu();
95 MENUITEMINFO mi;
96 mi.cbSize = sizeof(MENUITEMINFO);
97 mi.fMask = MIIM_TYPE|MIIM_ID;
98 mi.fType = MFT_STRING;
100 mi.dwTypeData = "Run...";
101 mi.wID = ID_RUN;
102 InsertMenuItem(hMenu,n++,TRUE,&mi);
104 mi.dwTypeData = "Exit";
105 mi.wID = ID_EXIT;
106 InsertMenuItem(hMenu,n++,TRUE,&mi);
108 POINT pt;
109 pt.x = LOWORD(lParam);
110 pt.y = HIWORD(lParam);
112 TrackPopupMenu(hMenu,TPM_LEFTALIGN,pt.x,pt.y,0,m_hWnd,NULL);
113 return 0;
116 void CSwitcherWindow::DrawWindow()
118 const char *pszDesktopName = m_pDesktop->GetDesktopName();
120 if (pszDesktopName == NULL)
122 Beep(10000,10000);
123 return;
126 HDC hdcScreen = ::GetDC(m_hWnd);
128 if (m_hdcMemory == NULL)
130 ASSERT(hdcScreen);
131 m_hdcMemory = ::CreateCompatibleDC(hdcScreen);
132 ASSERT(m_hdcMemory);
134 if (!g_Image.SelectBitmap(m_hdcMemory))
136 return;
139 const char *pszImage = "C:\\topka.png";
141 BITMAPV4HEADER BIH;
142 ZeroMemory(&BIH,sizeof(BIH));
143 BIH.bV4Size = sizeof(BIH);
145 CPNGImage img;
146 if (img.LoadPNGImage(pszImage))
148 unsigned char *pInternalData = new unsigned char[327*45*4];
149 for (int y = 0 ; y < 45; y++)
151 unsigned char *pPixel= pInternalData + 327 * 4 * y;
153 for (int x = 0 ; x < 327 ; x++)
155 pPixel[0]= 0xFF; // blue
156 pPixel[1]= 0xFF; // green
157 pPixel[2]= 0xFF; // red
158 pPixel[3]= 180; // alpha
159 pPixel+= 4;
163 m_hBackground = CreateBitmap(327,45,1,32,pInternalData);
164 ASSERT(m_hBackground);
166 VERIFY(GetDIBits(m_hdcMemory, m_hBackground, 0, 0, NULL, (BITMAPINFO *)&BIH, DIB_RGB_COLORS));
168 BIH.bV4RedMask = 0x00FF0000;
169 BIH.bV4GreenMask = 0x0000FF00;
170 BIH.bV4BlueMask = 0x000000FF;
171 BIH.bV4AlphaMask = 0xFF000000;
173 else
175 m_hBackground = LoadBitmap(_Module.GetModuleInstance(),MAKEINTRESOURCE(IDB_BACKGROUND));
176 ASSERT(m_hBackground);
178 VERIFY(GetDIBits(m_hdcMemory, m_hBackground, 0, 0, NULL, (BITMAPINFO *)&BIH, DIB_RGB_COLORS));
181 unsigned char *pData = new unsigned char[BIH.bV4SizeImage];
182 VERIFY(GetDIBits(m_hdcMemory, m_hBackground, 0, BIH.bV4Height, pData, (BITMAPINFO *)&BIH, DIB_RGB_COLORS));
184 for (int y = 0 ; y < BIH.bV4Height; y++)
186 unsigned char *pPixel= pData + BIH.bV4Width * 4 * y;
188 for (int x = 0 ; x < BIH.bV4Width ; x++)
190 pPixel[0]= (unsigned __int8)(((unsigned int)pPixel[0])*pPixel[3]/255);
191 pPixel[1]= (unsigned __int8)(((unsigned int)pPixel[1])*pPixel[3]/255);
192 pPixel[2]= (unsigned __int8)(((unsigned int)pPixel[2])*pPixel[3]/255);
193 pPixel+= 4;
197 if (!SetDIBits(m_hdcMemory, m_hBackground, 0, BIH.bV4Height, pData, (BITMAPINFO *)&BIH, DIB_RGB_COLORS))
199 char Buffer[256];
200 sprintf(Buffer,"SetDIBits() failed. GetLastError() returns %u",GetLastError());
201 ::MessageBox(NULL, Buffer, "DesktopSwitcher", MB_OK | MB_ICONINFORMATION);
204 delete pData;
206 SelectObject(m_hdcMemory,m_hBackground);
210 RECT r;
211 GetWindowRect(&r);
213 // setup the blend function
214 BLENDFUNCTION blendPixelFunction = { AC_SRC_OVER, 0, 0xFF,AC_SRC_ALPHA };
215 POINT ptSrc = {0,0}; // start point of the copy from dcMemory to dcScreen
217 POINT ptWindowScreenPosition = {r.left,r.top};
218 SIZE szWindow = {g_Image.GetWidth(), g_Image.GetHeight()};
220 // perform the alpha blend
221 if (!UpdateLayeredWindow(m_hWnd,
222 hdcScreen,
223 &ptWindowScreenPosition,
224 &szWindow,
225 m_hdcMemory,
226 &ptSrc,
228 &blendPixelFunction,
229 ULW_ALPHA))
231 char Buffer[256];
232 sprintf(Buffer,"UpdateLayeredWindow() failed. GetLastError() returns %u",GetLastError());
233 ::MessageBox(NULL, Buffer, "DesktopSwitcher", MB_OK | MB_ICONINFORMATION);
234 // ::MessageBox(NULL, strMsg, "DesktopSwitcher", MB_OK | MB_ICONINFORMATION);
237 VERIFY(ReleaseDC(hdcScreen));
240 LRESULT CSwitcherWindow::OnNCHitText(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
242 return HTCAPTION;
245 LRESULT CSwitcherWindow::OnRun(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
247 CRunDialog dlg;
249 const char *szExec = dlg.DoModal(m_hWnd);
250 if (szExec)
251 m_pDesktop->Run(szExec);
252 return 0;
255 LRESULT CSwitcherWindow::OnExit(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
257 m_pDesktopSwitcher->Exit();
258 return 0;
261 void CSwitcherWindow::Destroy()
263 VERIFY(DestroyWindow());
266 void CSwitcherWindow::OnRun()
268 PostMessage(WM_COMMAND,MAKEWPARAM(ID_RUN,1),NULL);
271 LRESULT CSwitcherWindow::OnExitSizeMove(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
273 RECT rect;
274 VERIFY(GetWindowRect(&rect));
275 if (rect.left > -DOCK && rect.left < DOCK)
277 rect.right -= rect.left;
278 rect.left = 0;
281 if (rect.top+11 > -DOCK && rect.top+11 < DOCK)
283 rect.bottom -= rect.top+11;
284 rect.top = -11;
287 VERIFY(SetWindowPos(NULL,rect.left,rect.top,10,10,SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOSIZE|SWP_NOZORDER|SWP_SHOWWINDOW));*/
288 return 0;