2 * Copyright 2003, 2004, 2005 Martin Fuchs
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 // Martin Fuchs, 09.08.2003
31 #include "../explorer_intres.h"
33 #include "../taskbar/desktopbar.h"
34 #include "../taskbar/taskbar.h" // for PM_GET_LAST_ACTIVE
37 static BOOL (WINAPI
*SetShellWindow
)(HWND
);
38 static BOOL (WINAPI
*SetShellWindowEx
)(HWND
, HWND
);
43 Desktop::Desktop(HDESK hdesktop
/*, HWINSTA hwinsta*/)
52 CloseDesktop(_hdesktop
);
55 // CloseWindowStation(_hwinsta);
73 // show all hidden windows
74 for(iterator it_dsk
=begin(); it_dsk
!=end(); ++it_dsk
)
75 for(WindowSet::iterator it
=it_dsk
->_windows
.begin(); it
!=it_dsk
->_windows
.end(); ++it
)
76 ShowWindowAsync(*it
, SW_SHOW
);
81 resize(DESKTOP_COUNT
);
84 DesktopPtr
& desktop
= (*this)[0];
86 desktop
= DesktopPtr(new Desktop(OpenInputDesktop(0, FALSE
, DESKTOP_SWITCHDESKTOP
)));
92 void Desktops::SwitchToDesktop(int idx
)
94 if (_current_desktop
== idx
)
97 DesktopPtr
& desktop
= (*this)[idx
];
99 DesktopThread
* pThread
= NULL
;
102 if (desktop
->_hdesktop
)
103 if (!SwitchDesktop(desktop
->_hdesktop
))
106 FmtString
desktop_name(TEXT("Desktop %d"), idx
);
108 SECURITY_ATTRIBUTES saAttr
= {sizeof(SECURITY_ATTRIBUTES
), 0, TRUE
};
110 HWINSTA hwinsta = CreateWindowStation(TEXT("ExplorerWinStation"), 0, GENERIC_ALL, &saAttr);
112 if (!SetProcessWindowStation(hwinsta))
115 HDESK hdesktop
= CreateDesktop(desktop_name
, NULL
, NULL
, 0, GENERIC_ALL
, &saAttr
);
119 desktop
= DesktopPtr(new Desktop(hdesktop
/*, hwinsta*/));
121 pThread
= new DesktopThread(*desktop
);
124 _current_desktop
= idx
;
127 desktop
->_pThread
= DesktopThreadPtr(pThread
);
132 int DesktopThread::Run()
134 if (!SetThreadDesktop(_desktop
._hdesktop
))
137 HDESK hDesk_old
= OpenInputDesktop(0, FALSE
, DESKTOP_SWITCHDESKTOP
);
139 if (!SwitchDesktop(_desktop
._hdesktop
))
142 if (!_desktop
._hwndDesktop
)
143 _desktop
._hwndDesktop
= DesktopWindow::Create();
145 int ret
= Window::MessageLoop();
147 SwitchDesktop(hDesk_old
);
154 static BOOL CALLBACK
SwitchDesktopEnumFct(HWND hwnd
, LPARAM lparam
)
156 WindowSet
& windows
= *(WindowSet
*)lparam
;
158 if (hwnd
!=g_Globals
._hwndDesktopBar
&& hwnd
!=g_Globals
._hwndDesktop
)
159 if (IsWindowVisible(hwnd
))
160 windows
.insert(hwnd
);
165 void Desktops::SwitchToDesktop(int idx
)
167 if (_current_desktop
== idx
)
170 Desktop
& old_desktop
= (*this)[_current_desktop
];
171 WindowSet
& windows
= old_desktop
._windows
;
172 Desktop
& desktop
= (*this)[idx
];
176 // collect window handles of all other desktops
177 WindowSet other_wnds
;
178 for(const_iterator it1
=begin(); it1
!=end(); ++it1
)
179 for(WindowSet::const_iterator it2
=it1
->_windows
.begin(); it2
!=it1
->_windows
.end(); ++it2
)
180 other_wnds
.insert(*it2
);
182 // save currently visible application windows
183 EnumWindows(SwitchDesktopEnumFct
, (LPARAM
)&windows
);
185 old_desktop
._hwndForeground
= (HWND
)SendMessage(g_Globals
._hwndDesktopBar
, PM_GET_LAST_ACTIVE
, 0, 0);
187 // hide all windows of the previous desktop
188 for(WindowSet::iterator it
=windows
.begin(); it
!=windows
.end(); ++it
)
189 ShowWindowAsync(*it
, SW_HIDE
);
191 // show all windows of the new desktop
192 for(WindowSet::iterator it
=desktop
._windows
.begin(); it
!=desktop
._windows
.end(); ++it
)
193 ShowWindowAsync(*it
, SW_SHOW
);
195 if (desktop
._hwndForeground
)
196 SetForegroundWindow(desktop
._hwndForeground
);
198 // remove the window handles of the other desktops from what we found on the previous desktop
199 for(WindowSet::const_iterator it
=other_wnds
.begin(); it
!=other_wnds
.end(); ++it
)
202 // We don't need to store the window handles of what's now visible the now current desktop.
203 desktop
._windows
.clear();
205 _current_desktop
= idx
;
211 static BOOL CALLBACK
MinimizeDesktopEnumFct(HWND hwnd
, LPARAM lparam
)
213 list
<MinimizeStruct
>& minimized
= *(list
<MinimizeStruct
>*)lparam
;
215 if (hwnd
!=g_Globals
._hwndDesktopBar
&& hwnd
!=g_Globals
._hwndDesktop
)
216 if (IsWindowVisible(hwnd
) && !IsIconic(hwnd
)) {
219 if (GetWindowRect(hwnd
,&rect
))
220 if (rect
.right
>0 && rect
.bottom
>0 &&
221 rect
.right
>rect
.left
&& rect
.bottom
>rect
.top
) {
222 minimized
.push_back(MinimizeStruct(hwnd
, GetWindowStyle(hwnd
)));
223 ShowWindowAsync(hwnd
, SW_MINIMIZE
);
230 /// minimize/restore all windows on the desktop
231 void Desktops::ToggleMinimize()
233 list
<MinimizeStruct
>& minimized
= (*this)[_current_desktop
]._minimized
;
235 if (minimized
.empty()) {
236 EnumWindows(MinimizeDesktopEnumFct
, (LPARAM
)&minimized
);
238 for(list
<MinimizeStruct
>::const_iterator it
=minimized
.begin(); it
!=minimized
.end(); ++it
)
239 ShowWindowAsync(it
->first
, it
->second
&WS_MAXIMIZE
? SW_MAXIMIZE
: SW_RESTORE
);
246 BOOL
IsAnyDesktopRunning()
248 HINSTANCE hUser32
= GetModuleHandle(TEXT("user32"));
250 SetShellWindow
= (BOOL(WINAPI
*)(HWND
)) GetProcAddress(hUser32
, "SetShellWindow");
251 SetShellWindowEx
= (BOOL(WINAPI
*)(HWND
,HWND
)) GetProcAddress(hUser32
, "SetShellWindowEx");
253 return GetShellWindow() != 0;
257 BackgroundWindow::BackgroundWindow(HWND hwnd
)
260 // set background brush for the short moment of displaying the
261 // background color while moving foreground windows
262 SetClassLong(hwnd
, GCL_HBRBACKGROUND
, COLOR_BACKGROUND
+1);
264 _display_version
= RegGetDWORDValue(HKEY_CURRENT_USER
, TEXT("Control Panel\\Desktop"), TEXT("PaintDesktopVersion"), 1);
267 LRESULT
BackgroundWindow::WndProc(UINT nmsg
, WPARAM wparam
, LPARAM lparam
)
271 DrawDesktopBkgnd((HDC
)wparam
);
274 case WM_MBUTTONDBLCLK
:
275 /* Imagelist icons are missing if MainFrame::Create() is called directly from here!
276 explorer_show_frame(SW_SHOWNORMAL); */
277 PostMessage(g_Globals
._hwndDesktop
, nmsg
, wparam
, lparam
);
280 case PM_DISPLAY_VERSION
:
281 if (lparam
|| wparam
) {
282 DWORD or_mask
= wparam
;
283 DWORD reset_mask
= LOWORD(lparam
);
284 DWORD xor_mask
= HIWORD(lparam
);
285 _display_version
= ((_display_version
&~reset_mask
) | or_mask
) ^ xor_mask
;
286 RegSetDWORDValue(HKEY_CURRENT_USER
, TEXT("Control Panel\\Desktop"), TEXT("PaintDesktopVersion"), _display_version
);
287 ///@todo Changing the PaintDesktopVersion-Flag needs a restart of the shell -> display a message box
288 InvalidateRect(_hwnd
, NULL
, TRUE
);
290 return _display_version
;
293 return super::WndProc(nmsg
, wparam
, lparam
);
299 void BackgroundWindow::DrawDesktopBkgnd(HDC hdc
)
303 /* special solid background
304 HBRUSH bkgndBrush = CreateSolidBrush(RGB(0,32,160)); // dark blue
305 FillRect(hdc, &rect, bkgndBrush);
306 DeleteBrush(bkgndBrush);
311 DesktopWindow::DesktopWindow(HWND hwnd
)
317 DesktopWindow::~DesktopWindow()
320 _pShellView
->Release();
324 HWND
DesktopWindow::Create()
326 static IconWindowClass
wcDesktop(TEXT("Progman"), IDI_REACTOS
, CS_DBLCLKS
);
327 /* (disabled because of small ugly temporary artefacts when hiding start menu)
328 wcDesktop.hbrBackground = (HBRUSH)(COLOR_BACKGROUND+1); */
330 int width
= GetSystemMetrics(SM_CXSCREEN
);
331 int height
= GetSystemMetrics(SM_CYSCREEN
);
333 HWND hwndDesktop
= Window::Create(WINDOW_CREATOR(DesktopWindow
),
334 WS_EX_TOOLWINDOW
, wcDesktop
, TEXT("Program Manager"), WS_POPUP
|WS_VISIBLE
, //|WS_CLIPCHILDREN for SDI frames
335 0, 0, width
, height
, 0);
337 // work around to display desktop bar in Wine
338 ShowWindow(GET_WINDOW(DesktopWindow
, hwndDesktop
)->_desktopBar
, SW_SHOW
);
340 // work around for Windows NT, Win 98, ...
341 // Without this the desktop has mysteriously only a size of 800x600 pixels.
342 MoveWindow(hwndDesktop
, 0, 0, width
, height
, TRUE
);
348 LRESULT
DesktopWindow::Init(LPCREATESTRUCT pcs
)
350 if (super::Init(pcs
))
353 HRESULT hr
= GetDesktopFolder()->CreateViewObject(_hwnd
, IID_IShellView
, (void**)&_pShellView
);
355 SFV_CREATE sfv_create;
357 sfv_create.cbSize = sizeof(SFV_CREATE);
358 sfv_create.pshf = GetDesktopFolder();
359 sfv_create.psvOuter = NULL;
360 sfv_create.psfvcb = NULL;
362 HRESULT hr = SHCreateShellFolderView(&sfv_create, &_pShellView);
369 fs
.ViewMode
= FVM_ICON
;
370 fs
.fFlags
= FWF_DESKTOP
|FWF_NOCLIENTEDGE
|FWF_NOSCROLL
|FWF_BESTFITWINDOW
|FWF_SNAPTOGRID
; //|FWF_AUTOARRANGE;
372 ClientRect
rect(_hwnd
);
374 hr
= _pShellView
->CreateViewWindow(NULL
, &fs
, this, &rect
, &hWndView
);
376 ///@todo use IShellBrowser::GetViewStateStream() to restore previous view state -> see SHOpenRegStream()
379 g_Globals
._hwndShellView
= hWndView
;
381 // subclass shellview window
382 new DesktopShellView(hWndView
, _pShellView
);
384 _pShellView
->UIActivate(SVUIA_ACTIVATE_FOCUS
);
387 IShellView2* pShellView2;
389 hr = _pShellView->QueryInterface(IID_IShellView2, (void**)&pShellView2);
391 SV2CVW2_PARAMS params;
392 params.cbSize = sizeof(SV2CVW2_PARAMS);
393 params.psvPrev = _pShellView;
395 params.psbOwner = this;
396 params.prcView = ▭
397 params.pvid = params.pvid;//@@
399 hr = pShellView2->CreateViewWindow2(¶ms);
404 IFolderView* pFolderView;
406 hr = _pShellView->QueryInterface(IID_IFolderView, (void**)&pFolderView);
409 hr = pFolderView->GetAutoArrange();
410 hr = pFolderView->SetCurrentViewMode(FVM_DETAILS);
416 if (hWndView
&& SetShellWindowEx
)
417 SetShellWindowEx(_hwnd
, hWndView
);
418 else if (SetShellWindow
)
419 SetShellWindow(_hwnd
);
421 // create the explorer bar
422 _desktopBar
= DesktopBar::Create();
423 g_Globals
._hwndDesktopBar
= _desktopBar
;
429 LRESULT
DesktopWindow::WndProc(UINT nmsg
, WPARAM wparam
, LPARAM lparam
)
432 case WM_LBUTTONDBLCLK
:
433 case WM_RBUTTONDBLCLK
:
434 case WM_MBUTTONDBLCLK
:
435 explorer_show_frame(SW_SHOWNORMAL
);
438 case WM_GETISHELLBROWSER
:
439 return (LRESULT
)static_cast<IShellBrowser
*>(this);
443 ///@todo use IShellBrowser::GetViewStateStream() and _pShellView->SaveViewState() to store view state
450 ShowExitWindowsDialog(_hwnd
);
454 if (wparam
== SC_TASKLIST
) {
456 SendMessage(_desktopBar
, nmsg
, wparam
, lparam
);
461 return super::WndProc(nmsg
, wparam
, lparam
);
468 HRESULT
DesktopWindow::OnDefaultCommand(LPIDA pida
)
470 if (MainFrameBase::OpenShellFolders(pida
, 0))
477 DesktopShellView::DesktopShellView(HWND hwnd
, IShellView
* pShellView
)
479 _pShellView(pShellView
)
481 _hwndListView
= ::GetNextWindow(hwnd
, GW_CHILD
);
483 SetWindowStyle(_hwndListView
, GetWindowStyle(_hwndListView
)&~LVS_ALIGNMASK
);//|LVS_ALIGNTOP|LVS_AUTOARRANGE);
485 // work around for Windows NT, Win 98, ...
486 // Without this the desktop has mysteriously only a size of 800x600 pixels.
487 ClientRect
rect(hwnd
);
488 MoveWindow(_hwndListView
, 0, 0, rect
.right
, rect
.bottom
, TRUE
);
490 // subclass background window
491 new BackgroundWindow(_hwndListView
);
493 _icon_algo
= 1; // default icon arrangement
499 bool DesktopShellView::InitDragDrop()
501 CONTEXT("DesktopShellView::InitDragDrop()");
503 _pDropTarget
= new DesktopDropTarget(_hwnd
);
508 _pDropTarget
->AddRef();
510 if (FAILED(RegisterDragDrop(_hwnd
, _pDropTarget
))) {
511 _pDropTarget
->Release();
516 _pDropTarget
->Release();
520 ftetc
.dwAspect
= DVASPECT_CONTENT
;
522 ftetc
.tymed
= TYMED_HGLOBAL
;
523 ftetc
.cfFormat
= CF_HDROP
;
525 _pDropTarget
->AddSuportedFormat(ftetc
);
530 LRESULT
DesktopShellView::WndProc(UINT nmsg
, WPARAM wparam
, LPARAM lparam
)
534 if (!DoContextMenu(GET_X_LPARAM(lparam
), GET_Y_LPARAM(lparam
)))
535 DoDesktopContextMenu(GET_X_LPARAM(lparam
), GET_Y_LPARAM(lparam
));
538 case PM_SET_ICON_ALGORITHM
:
543 case PM_GET_ICON_ALGORITHM
:
546 case PM_DISPLAY_VERSION
:
547 return SendMessage(_hwndListView
, nmsg
, wparam
, lparam
);
550 return super::WndProc(nmsg
, wparam
, lparam
);
556 int DesktopShellView::Command(int id
, int code
)
558 return super::Command(id
, code
);
561 int DesktopShellView::Notify(int id
, NMHDR
* pnmh
)
563 return super::Notify(id
, pnmh
);
566 bool DesktopShellView::DoContextMenu(int x
, int y
)
568 IDataObject
* selection
;
570 HRESULT hr
= _pShellView
->GetItemObject(SVGIO_SELECTION
, IID_IDataObject
, (void**)&selection
);
576 hr
= pidList
.GetData(selection
);
578 selection
->Release();
583 LPIDA pida
= pidList
;
585 selection
->Release();
589 LPCITEMIDLIST parent_pidl
= (LPCITEMIDLIST
) ((LPBYTE
)pida
+pida
->aoffset
[0]);
591 LPCITEMIDLIST
* apidl
= (LPCITEMIDLIST
*) alloca(pida
->cidl
*sizeof(LPCITEMIDLIST
));
593 for(int i
=pida
->cidl
; i
>0; --i
)
594 apidl
[i
-1] = (LPCITEMIDLIST
) ((LPBYTE
)pida
+pida
->aoffset
[i
]);
596 hr
= ShellFolderContextMenu(ShellFolder(parent_pidl
), _hwnd
, pida
->cidl
, apidl
, x
, y
, _cm_ifs
);
598 selection
->Release();
605 HRESULT
DesktopShellView::DoDesktopContextMenu(int x
, int y
)
609 HRESULT hr
= DesktopFolder()->GetUIObjectOf(_hwnd
, 0, NULL
, IID_IContextMenu
, NULL
, (LPVOID
*)&pcm
);
612 pcm
= _cm_ifs
.query_interfaces(pcm
);
614 HMENU hmenu
= CreatePopupMenu();
617 hr
= pcm
->QueryContextMenu(hmenu
, 0, FCIDM_SHVIEWFIRST
, FCIDM_SHVIEWLAST
-1, CMF_NORMAL
|CMF_EXPLORE
);
620 AppendMenu(hmenu
, MF_SEPARATOR
, 0, NULL
);
621 AppendMenu(hmenu
, 0, FCIDM_SHVIEWLAST
-1, ResString(IDS_ABOUT_EXPLORER
));
623 UINT idCmd
= TrackPopupMenu(hmenu
, TPM_LEFTALIGN
|TPM_RETURNCMD
|TPM_RIGHTBUTTON
, x
, y
, 0, _hwnd
, NULL
);
627 if (idCmd
== FCIDM_SHVIEWLAST
-1) {
628 explorer_about(_hwnd
);
630 CMINVOKECOMMANDINFO cmi
;
632 cmi
.cbSize
= sizeof(CMINVOKECOMMANDINFO
);
635 cmi
.lpVerb
= (LPCSTR
)(INT_PTR
)(idCmd
- FCIDM_SHVIEWFIRST
);
636 cmi
.lpParameters
= NULL
;
637 cmi
.lpDirectory
= NULL
;
638 cmi
.nShow
= SW_SHOWNORMAL
;
642 hr
= pcm
->InvokeCommand(&cmi
);
654 #define ARRANGE_BORDER_DOWN 8
655 #define ARRANGE_BORDER_HV 9
656 #define ARRANGE_ROUNDABOUT 10
658 static const POINTS s_align_start
[] = {
663 {0, 1}, // left/bottom
665 {1, 1}, // right/bottom
673 static const POINTS s_align_dir1
[] = {
688 static const POINTS s_align_dir2
[] = {
703 typedef pair
<int,int> IconPos
;
704 typedef map
<IconPos
, int> IconMap
;
706 void DesktopShellView::PositionIcons(int dir
)
708 DWORD spacing
= ListView_GetItemSpacing(_hwndListView
, FALSE
);
711 SystemParametersInfo(SPI_GETWORKAREA
, 0, &work_area
, 0);
713 const POINTS
& dir1
= s_align_dir1
[_icon_algo
];
714 const POINTS
& dir2
= s_align_dir2
[_icon_algo
];
715 const POINTS
& start_pos
= s_align_start
[_icon_algo
];
722 int cx
= LOWORD(spacing
);
723 int cy
= HIWORD(spacing
);
725 int dx1
= dir_x1
* cx
;
726 int dy1
= dir_y1
* cy
;
727 int dx2
= dir_x2
* cx
;
728 int dy2
= dir_y2
* cy
;
730 int start_x
= (start_pos
.x
* work_area
.right
)/cx
*cx
+ (cx
-32)/2;
731 int start_y
= (start_pos
.y
* work_area
.bottom
)/cy
*cy
+ 4/*(cy-32)/2*/;
733 if (start_x
>= work_area
.right
)
736 if (start_y
>= work_area
.bottom
)
742 int all
= ListView_GetItemCount(_hwndListView
);
756 for(int idx
=i1
; idx
!=i2
; idx
+=dir
) {
757 pos_idx
[IconPos(y
, x
)] = idx
;
759 if (_icon_algo
== ARRANGE_BORDER_DOWN
) {
761 x
= work_area
.right
- x
;
765 if (y
>= work_area
.bottom
) {
773 else if (_icon_algo
== ARRANGE_BORDER_HV
) {
775 x
= work_area
.right
- x
;
779 if (y
>= work_area
.bottom
) {
786 if (x
>= work_area
.right
) {
794 else if (_icon_algo
== ARRANGE_ROUNDABOUT
) {
803 if (x
<0 || x
>=work_area
.right
) {
806 } else if (y
<0 || y
>=work_area
.bottom
) {
812 // use a little trick to get the icons where we want them to be...
814 for(IconMap::const_iterator it
=pos_idx
.end(); --it
!=pos_idx
.begin(); ) {
815 const IconPos
& pos
= it
->first
;
817 ListView_SetItemPosition32(_hwndListView
, it
->second
, pos
.second
, pos
.first
);
820 for(IconMap::const_iterator it
=pos_idx
.begin(); it
!=pos_idx
.end(); ++it
) {
821 const IconPos
& pos
= it
->first
;
823 ListView_SetItemPosition32(_hwndListView
, it
->second
, pos
.second
, pos
.first
);