1 // $Id: desktopswitcher.cpp,v 1.2 2003/08/03 16:56:29 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 "DesktopSwitcher.h"
24 CDesktopSwitcher::CDesktopSwitcher()
30 CDesktopSwitcher::~CDesktopSwitcher()
32 ASSERT(!m_ppDesktops
);
36 ASSERT(!m_hExitEvent
);
38 VERIFY(CloseHandle(m_hExitEvent
));
41 HRESULT
CDesktopSwitcher::Go(unsigned int nDesktops
)
46 return E_UNEXPECTED
; // call this method only once
48 if (nDesktops
<= 1 || nDesktops
> 12)
51 m_nDesktops
= nDesktops
;
53 m_hExitEvent
= CreateEvent(NULL
,TRUE
,FALSE
,NULL
);
56 dwError
= GetLastError();
57 return HRESULT_FROM_WIN32(dwError
);
60 m_ppDesktops
= new CDesktop
* [nDesktops
];
64 m_nCurrentDesktopIndex
= 0;
67 char pszDesktopName
[256] = "";
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
);
74 for (unsigned int i
= 0 ; i
< nDesktops
; i
++)
76 // try to get desktop name from registry
77 _ultot(i
+1,NameBuffer
,10);
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
);
97 switch(WaitForSingleObject(m_hExitEvent
,INFINITE
))
103 dwError
= GetLastError();
104 return HRESULT_FROM_WIN32(dwError
);
111 m_ppDesktops
[0]->SwitchTo();
113 for (i
= 0 ; i
< nDesktops
; i
++)
114 m_ppDesktops
[i
]->Exit();
119 VERIFY(CloseHandle(m_hExitEvent
));
125 CDesktop
* CDesktopSwitcher::GetDefaultDesktop()
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
)
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();
149 else if (wVKey
== VK_RETURN
&& wModifiers
== MOD_WIN
)
151 for (unsigned int i
= 0 ; i
< m_nDesktops
; i
++)
152 m_ppDesktops
[i
]->WindowToggleVisible();
156 else if (wVKey
== VK_ADD
&& wModifiers
== MOD_WIN
)
158 m_nCurrentDesktopIndex
= (m_nCurrentDesktopIndex
+1) % m_nDesktops
;
159 hr
= m_ppDesktops
[m_nCurrentDesktopIndex
]->SwitchTo();
165 else if (wVKey
== VK_SUBTRACT
&& wModifiers
== MOD_WIN
)
167 if (m_nCurrentDesktopIndex
)
168 m_nCurrentDesktopIndex
--;
170 m_nCurrentDesktopIndex
= m_nDesktops
-1;
172 hr
= m_ppDesktops
[m_nCurrentDesktopIndex
]->SwitchTo();
178 else if (wVKey
== 'R' && wModifiers
== (MOD_WIN
|MOD_SHIFT
))
180 m_ppDesktops
[m_nCurrentDesktopIndex
]->RunPrompt();
183 else if (wVKey
== VK_ESCAPE
&& wModifiers
== (MOD_WIN
))
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
);