Flush current work
[desktopswitcher.git] / desktopswitcher.cpp
blobff562e7d924280d353d790f6e800f67bbe8fa39a
1 // $Id: desktopswitcher.cpp,v 1.2 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 "DesktopSwitcher.h"
22 #include "Desktop.h"
24 CDesktopSwitcher::CDesktopSwitcher()
26 m_ppDesktops = NULL;
27 m_hExitEvent = NULL;
30 CDesktopSwitcher::~CDesktopSwitcher()
32 ASSERT(!m_ppDesktops);
33 if (m_ppDesktops)
34 delete m_ppDesktops;
36 ASSERT(!m_hExitEvent);
37 if (m_hExitEvent)
38 VERIFY(CloseHandle(m_hExitEvent));
41 HRESULT CDesktopSwitcher::Go(unsigned int nDesktops)
43 DWORD dwError;
45 if (m_hExitEvent)
46 return E_UNEXPECTED; // call this method only once
48 if (nDesktops <= 1 || nDesktops > 12)
49 return E_INVALIDARG;
51 m_nDesktops = nDesktops;
53 m_hExitEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
54 if (!m_hExitEvent)
56 dwError = GetLastError();
57 return HRESULT_FROM_WIN32(dwError);
60 m_ppDesktops = new CDesktop * [nDesktops];
61 if (!m_ppDesktops)
62 return E_OUTOFMEMORY;
64 m_nCurrentDesktopIndex = 0;
66 HRESULT hr;
67 char pszDesktopName[256] = "";
69 HKEY hKey;
70 // open/create the key
71 RegCreateKeyEx(HKEY_CURRENT_USER, DESKTOP_NAMES_KEY, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_QUERY_VALUE|KEY_SET_VALUE,NULL,&hKey,NULL);
72 char NameBuffer[256];
74 for (unsigned int i = 0 ; i < nDesktops ; i++)
76 // try to get desktop name from registry
77 _ultot(i+1,NameBuffer,10);
78 DWORD dwSize = 256;
79 DWORD dwType;
80 if (hKey == NULL || RegQueryValueEx(hKey,NameBuffer,NULL,&dwType,(BYTE *)pszDesktopName,&dwSize) != ERROR_SUCCESS && dwType != REG_SZ)
81 sprintf(pszDesktopName,"Desktop %u",i+1);
83 m_ppDesktops[i] = new CDesktop(this,i?pszDesktopName:"Default",pszDesktopName,hr);
84 if (!m_ppDesktops[i])
86 Exit();
87 return E_OUTOFMEMORY;
90 if (FAILED(hr))
92 Exit();
93 return E_OUTOFMEMORY;
97 switch(WaitForSingleObject(m_hExitEvent,INFINITE))
99 case WAIT_OBJECT_0:
100 break;
101 case WAIT_FAILED:
103 dwError = GetLastError();
104 return HRESULT_FROM_WIN32(dwError);
106 default:
107 ASSERT(FALSE);
108 return E_UNEXPECTED;
111 m_ppDesktops[0]->SwitchTo();
113 for (i = 0 ; i < nDesktops ; i ++)
114 m_ppDesktops[i]->Exit();
116 delete m_ppDesktops;
117 m_ppDesktops = NULL;
119 VERIFY(CloseHandle(m_hExitEvent));
120 m_hExitEvent= NULL;
122 return S_OK;
125 CDesktop * CDesktopSwitcher::GetDefaultDesktop()
127 if (!m_ppDesktops)
128 return NULL;
130 return m_ppDesktops[0];
133 void CDesktopSwitcher::Exit()
135 ASSERT(m_hExitEvent);
136 VERIFY(SetEvent(m_hExitEvent));
139 HRESULT CDesktopSwitcher::HotKeyPressed(WORD wVKey, WORD wModifiers)
141 HRESULT hr;
142 if (wVKey >= VK_F1 && wVKey < VK_F1 + m_nDesktops && wModifiers == (MOD_ALT|MOD_CONTROL))
144 m_nCurrentDesktopIndex = wVKey - VK_F1;
145 hr = m_ppDesktops[m_nCurrentDesktopIndex]->SwitchTo();
146 if (FAILED(hr))
147 return hr;
149 else if (wVKey == VK_RETURN && wModifiers == MOD_WIN)
151 for (unsigned int i = 0 ; i < m_nDesktops ; i ++)
152 m_ppDesktops[i]->WindowToggleVisible();
154 return S_OK;
156 else if (wVKey == VK_ADD && wModifiers == MOD_WIN)
158 m_nCurrentDesktopIndex = (m_nCurrentDesktopIndex+1) % m_nDesktops;
159 hr = m_ppDesktops[m_nCurrentDesktopIndex]->SwitchTo();
160 if (FAILED(hr))
161 return hr;
163 return S_OK;
165 else if (wVKey == VK_SUBTRACT && wModifiers == MOD_WIN)
167 if (m_nCurrentDesktopIndex)
168 m_nCurrentDesktopIndex--;
169 else
170 m_nCurrentDesktopIndex = m_nDesktops-1;
172 hr = m_ppDesktops[m_nCurrentDesktopIndex]->SwitchTo();
173 if (FAILED(hr))
174 return hr;
176 return S_OK;
178 else if (wVKey == 'R' && wModifiers == (MOD_WIN|MOD_SHIFT))
180 m_ppDesktops[m_nCurrentDesktopIndex]->RunPrompt();
181 return S_OK;
183 else if (wVKey == VK_ESCAPE && wModifiers == (MOD_WIN))
185 Exit();
186 return S_OK;
189 return S_FALSE;
192 void CDesktopSwitcher::RegisterHotKeys(HWND hWnd)
194 for (unsigned int i=0 ; i < m_nDesktops ; i++)
195 RegisterHotKey (hWnd, VK_F1+i, MOD_ALT|MOD_CONTROL, VK_F1+i);
197 RegisterHotKey(hWnd, VK_RETURN, MOD_WIN, VK_RETURN);
198 RegisterHotKey(hWnd, VK_ADD, MOD_WIN, VK_ADD);
199 RegisterHotKey(hWnd, VK_SUBTRACT, MOD_WIN, VK_SUBTRACT);
200 RegisterHotKey(hWnd, 'R', MOD_WIN|MOD_SHIFT, 'R');
201 RegisterHotKey(hWnd, VK_ESCAPE, MOD_WIN, VK_ESCAPE);