1 // $Id: switcherwindow.cpp,v 1.2 2002/06/29 15:03:33 nedko Exp $
4 // Copyright (C) 2000,2001,2002 Nedko Arnaudov <nedko@users.sourceforge.net>
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.
21 #include "SwitcherWindow.h"
22 #include "DesktopSwitcher.h"
24 #include "RunDialog.h"
28 #define BACKGROUD_COLOR RGB(96,96,96)
29 #define TEXT_COLOR RGB(220,220,220)
32 CSwitcherWindow::CSwitcherWindow(CDesktopSwitcher
*pDesktopSwitcher
, CDesktop
*pDesktop
)
34 m_pDesktopSwitcher
= pDesktopSwitcher
;
35 m_pDesktop
= pDesktop
;
41 CSwitcherWindow::~CSwitcherWindow()
44 VERIFY(DeleteObject(m_hBackground
));
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
57 WS_EX_TOPMOST
|WS_EX_TOOLWINDOW
|WS_EX_LAYERED
, // Extended style
60 DWORD dwError
= GetLastError();
64 m_pDesktopSwitcher
->RegisterHotKeys(m_hWnd
);
71 LRESULT
CSwitcherWindow::OnClose(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
73 m_pDesktopSwitcher
->Exit();
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
);
86 void CSwitcherWindow::ToggleVisible()
88 ShowWindow(IsWindowVisible()?SW_HIDE
:SW_SHOW
);
91 LRESULT
CSwitcherWindow::OnRButtonDown(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
94 HMENU hMenu
= CreatePopupMenu();
96 mi
.cbSize
= sizeof(MENUITEMINFO
);
97 mi
.fMask
= MIIM_TYPE
|MIIM_ID
;
98 mi
.fType
= MFT_STRING
;
100 mi
.dwTypeData
= "Run...";
102 InsertMenuItem(hMenu
,n
++,TRUE
,&mi
);
104 mi
.dwTypeData
= "Exit";
106 InsertMenuItem(hMenu
,n
++,TRUE
,&mi
);
109 pt
.x
= LOWORD(lParam
);
110 pt
.y
= HIWORD(lParam
);
112 TrackPopupMenu(hMenu
,TPM_LEFTALIGN
,pt
.x
,pt
.y
,0,m_hWnd
,NULL
);
116 void CSwitcherWindow::DrawWindow()
118 const char *pszDesktopName
= m_pDesktop
->GetDesktopName();
120 if (pszDesktopName
== NULL
)
126 HDC hdcScreen
= ::GetDC(m_hWnd
);
128 if (m_hdcMemory
== NULL
)
131 m_hdcMemory
= ::CreateCompatibleDC(hdcScreen
);
134 if (!g_Image
.SelectBitmap(m_hdcMemory
))
139 const char *pszImage = "C:\\topka.png";
142 ZeroMemory(&BIH,sizeof(BIH));
143 BIH.bV4Size = sizeof(BIH);
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
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;
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);
197 if (!SetDIBits(m_hdcMemory, m_hBackground, 0, BIH.bV4Height, pData, (BITMAPINFO *)&BIH, DIB_RGB_COLORS))
200 sprintf(Buffer,"SetDIBits() failed. GetLastError() returns %u",GetLastError());
201 ::MessageBox(NULL, Buffer, "DesktopSwitcher", MB_OK | MB_ICONINFORMATION);
206 SelectObject(m_hdcMemory,m_hBackground);
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
,
223 &ptWindowScreenPosition
,
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
)
245 LRESULT
CSwitcherWindow::OnRun(WORD wNotifyCode
, WORD wID
, HWND hWndCtl
, BOOL
& bHandled
)
249 const char *szExec
= dlg
.DoModal(m_hWnd
);
251 m_pDesktop
->Run(szExec
);
255 LRESULT
CSwitcherWindow::OnExit(WORD wNotifyCode
, WORD wID
, HWND hWndCtl
, BOOL
& bHandled
)
257 m_pDesktopSwitcher
->Exit();
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
)
274 VERIFY(GetWindowRect(&rect));
275 if (rect.left > -DOCK && rect.left < DOCK)
277 rect.right -= rect.left;
281 if (rect.top+11 > -DOCK && rect.top+11 < DOCK)
283 rect.bottom -= rect.top+11;
287 VERIFY(SetWindowPos(NULL,rect.left,rect.top,10,10,SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOSIZE|SWP_NOZORDER|SWP_SHOWWINDOW));*/