2 * Copyright 2003 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
29 #include "../utility/utility.h"
30 #include "../utility/shellclasses.h"
31 #include "../utility/shellbrowserimpl.h"
32 #include "../utility/dragdropimpl.h"
33 #include "../utility/window.h"
35 #include "../globals.h"
36 #include "../externals.h"
37 #include "../explorer_intres.h"
40 #include "../taskbar/desktopbar.h"
41 #include "../shell/mainframe.h" // for MainFrame::Create()
44 static BOOL (WINAPI
*SetShellWindow
)(HWND
);
45 static BOOL (WINAPI
*SetShellWindowEx
)(HWND
, HWND
);
50 Desktop::Desktop(HDESK hdesktop
/*, HWINSTA hwinsta*/)
59 CloseDesktop(_hdesktop
);
62 // CloseWindowStation(_hwinsta);
80 // show all hidden windows
81 for(iterator it_dsk
=begin(); it_dsk
!=end(); ++it_dsk
)
82 for(WindowSet::iterator it
=it_dsk
->_windows
.begin(); it
!=it_dsk
->_windows
.end(); ++it
)
83 ShowWindowAsync(*it
, SW_SHOW
);
88 resize(DESKTOP_COUNT
);
91 DesktopPtr
& desktop
= (*this)[0];
93 desktop
= DesktopPtr(new Desktop(OpenInputDesktop(0, FALSE
, DESKTOP_SWITCHDESKTOP
)));
99 void Desktops::SwitchToDesktop(int idx
)
101 if (_current_desktop
== idx
)
104 DesktopPtr
& desktop
= (*this)[idx
];
106 DesktopThread
* pThread
= NULL
;
109 if (desktop
->_hdesktop
)
110 if (!SwitchDesktop(desktop
->_hdesktop
))
113 FmtString
desktop_name(TEXT("Desktop %d"), idx
);
115 SECURITY_ATTRIBUTES saAttr
= {sizeof(SECURITY_ATTRIBUTES
), 0, TRUE
};
117 HWINSTA hwinsta = CreateWindowStation(TEXT("ExplorerWinStation"), 0, GENERIC_ALL, &saAttr);
119 if (!SetProcessWindowStation(hwinsta))
122 HDESK hdesktop
= CreateDesktop(desktop_name
, NULL
, NULL
, 0, GENERIC_ALL
, &saAttr
);
126 desktop
= DesktopPtr(new Desktop(hdesktop
/*, hwinsta*/));
128 pThread
= new DesktopThread(*desktop
);
131 _current_desktop
= idx
;
134 desktop
->_pThread
= DesktopThreadPtr(pThread
);
139 int DesktopThread::Run()
141 if (!SetThreadDesktop(_desktop
._hdesktop
))
144 HDESK hDesk_old
= OpenInputDesktop(0, FALSE
, DESKTOP_SWITCHDESKTOP
);
146 if (!SwitchDesktop(_desktop
._hdesktop
))
149 if (!_desktop
._hwndDesktop
)
150 _desktop
._hwndDesktop
= DesktopWindow::Create();
152 int ret
= Window::MessageLoop();
154 SwitchDesktop(hDesk_old
);
161 static BOOL CALLBACK
DesktopEnumFct(HWND hwnd
, LPARAM lparam
)
163 WindowSet
& windows
= *(WindowSet
*)lparam
;
165 if (IsWindowVisible(hwnd
)) {
168 GetWindowThreadProcessId(hwnd
, &pid
);
170 if (pid
!= GetCurrentProcessId())
171 windows
.insert(hwnd
);
177 void Desktops::SwitchToDesktop(int idx
)
179 if (_current_desktop
== idx
)
182 Desktop
& desktop
= (*this)[idx
];
184 // save currently visible application windows
185 Desktop
& old_desktop
= (*this)[_current_desktop
];
186 WindowSet
& windows
= old_desktop
._windows
;
189 EnumWindows(DesktopEnumFct
, (LPARAM
)&windows
);
191 // hide all windows we found
192 for(WindowSet::iterator it
=windows
.begin(); it
!=windows
.end(); ++it
)
193 ShowWindowAsync(*it
, SW_HIDE
);
195 // show all windows of the new desktop
196 for(WindowSet::iterator it
=desktop
._windows
.begin(); it
!=desktop
._windows
.end(); ++it
)
197 ShowWindowAsync(*it
, SW_SHOW
);
199 desktop
._windows
.clear();
201 _current_desktop
= idx
;
207 BOOL
IsAnyDesktopRunning()
209 HINSTANCE hUser32
= GetModuleHandle(TEXT("user32"));
211 SetShellWindow
= (BOOL(WINAPI
*)(HWND
)) GetProcAddress(hUser32
, "SetShellWindow");
212 SetShellWindowEx
= (BOOL(WINAPI
*)(HWND
,HWND
)) GetProcAddress(hUser32
, "SetShellWindowEx");
214 return GetShellWindow() != 0;
218 static void draw_desktop_background(HWND hwnd
, HDC hdc
)
220 ClientRect
rect(hwnd
);
224 HBRUSH bkgndBrush = CreateSolidBrush(RGB(0,32,160)); // dark blue
225 FillRect(hdc, &rect, bkgndBrush);
226 DeleteBrush(bkgndBrush);
229 rect
.left
= rect
.right
- 280;
230 rect
.top
= rect
.bottom
- 56 - DESKTOPBARBAR_HEIGHT
;
231 rect
.right
= rect
.left
+ 250;
232 rect
.bottom
= rect
.top
+ 40;
234 #include "../buildno.h"
235 static const LPCTSTR BkgndText
= TEXT("ReactOS ")TEXT(KERNEL_VERSION_STR
)TEXT(" Explorer\nby Martin Fuchs");
237 BkMode
bkMode(hdc
, TRANSPARENT
);
239 TextColor
textColor(hdc
, RGB(128,128,192));
240 DrawText(hdc
, BkgndText
, -1, &rect
, DT_RIGHT
);
242 SetTextColor(hdc
, RGB(255,255,255));
245 DrawText(hdc
, BkgndText
, -1, &rect
, DT_RIGHT
);
249 LRESULT
BackgroundWindow::WndProc(UINT nmsg
, WPARAM wparam
, LPARAM lparam
)
253 PaintDesktop((HDC
)wparam
);
256 case WM_MBUTTONDBLCLK
:
257 explorer_show_frame(SW_SHOWNORMAL
);
261 return super::WndProc(nmsg
, wparam
, lparam
);
268 DesktopWindow::DesktopWindow(HWND hwnd
)
274 DesktopWindow::~DesktopWindow()
277 _pShellView
->Release();
281 HWND
DesktopWindow::Create()
283 static IconWindowClass
wcDesktop(TEXT("Progman"), IDI_REACTOS
, CS_DBLCLKS
);
284 wcDesktop
.hbrBackground
= (HBRUSH
)(COLOR_BACKGROUND
+1);
286 int width
= GetSystemMetrics(SM_CXSCREEN
);
287 int height
= GetSystemMetrics(SM_CYSCREEN
);
289 HWND hwndDesktop
= Window::Create(WINDOW_CREATOR(DesktopWindow
),
290 WS_EX_TOOLWINDOW
, wcDesktop
, TEXT("Program Manager"), WS_POPUP
|WS_VISIBLE
|WS_CLIPCHILDREN
,
291 0, 0, width
, height
, 0);
293 // work around to display desktop bar in Wine
294 ShowWindow(GET_WINDOW(DesktopWindow
, hwndDesktop
)->_desktopBar
, SW_SHOW
);
296 // work around for Windows NT, Win 98, ...
297 // Without this the desktop has mysteriously only a size of 800x600 pixels.
298 MoveWindow(hwndDesktop
, 0, 0, width
, height
, TRUE
);
304 LRESULT
DesktopWindow::Init(LPCREATESTRUCT pcs
)
306 if (super::Init(pcs
))
309 HRESULT hr
= GetDesktopFolder()->CreateViewObject(_hwnd
, IID_IShellView
, (void**)&_pShellView
);
311 SFV_CREATE sfv_create;
313 sfv_create.cbSize = sizeof(SFV_CREATE);
314 sfv_create.pshf = GetDesktopFolder();
315 sfv_create.psvOuter = NULL;
316 sfv_create.psfvcb = NULL;
318 HRESULT hr = SHCreateShellFolderView(&sfv_create, &_pShellView);
325 fs
.ViewMode
= FVM_ICON
;
326 fs
.fFlags
= FWF_DESKTOP
|FWF_NOCLIENTEDGE
|FWF_NOSCROLL
|FWF_BESTFITWINDOW
|FWF_SNAPTOGRID
; //|FWF_AUTOARRANGE;
328 ClientRect
rect(_hwnd
);
330 hr
= _pShellView
->CreateViewWindow(NULL
, &fs
, this, &rect
, &hWndView
);
332 ///@todo use IShellBrowser::GetViewStateStream() to restore previous view state -> see SHOpenRegStream()
335 g_Globals
._hwndShellView
= hWndView
;
337 // subclass shellview window
338 new DesktopShellView(hWndView
, _pShellView
);
340 _pShellView
->UIActivate(SVUIA_ACTIVATE_FOCUS
);
343 IShellView2* pShellView2;
345 hr = _pShellView->QueryInterface(IID_IShellView2, (void**)&pShellView2);
347 SV2CVW2_PARAMS params;
348 params.cbSize = sizeof(SV2CVW2_PARAMS);
349 params.psvPrev = _pShellView;
351 params.psbOwner = this;
352 params.prcView = ▭
353 params.pvid = params.pvid;//@@
355 hr = pShellView2->CreateViewWindow2(¶ms);
360 IFolderView* pFolderView;
362 hr = _pShellView->QueryInterface(IID_IFolderView, (void**)&pFolderView);
365 hr = pFolderView->GetAutoArrange();
366 hr = pFolderView->SetCurrentViewMode(FVM_DETAILS);
372 if (hWndView
&& SetShellWindowEx
)
373 SetShellWindowEx(_hwnd
, hWndView
);
374 else if (SetShellWindow
)
375 SetShellWindow(_hwnd
);
377 // create the explorer bar
378 _desktopBar
= DesktopBar::Create();
379 g_Globals
._hwndDesktopBar
= _desktopBar
;
385 LRESULT
DesktopWindow::WndProc(UINT nmsg
, WPARAM wparam
, LPARAM lparam
)
389 draw_desktop_background(_hwnd
, PaintCanvas(_hwnd
));
392 case WM_LBUTTONDBLCLK
:
393 case WM_RBUTTONDBLCLK
:
394 case WM_MBUTTONDBLCLK
:
395 explorer_show_frame(SW_SHOWNORMAL
);
398 case WM_GETISHELLBROWSER
:
399 return (LRESULT
)static_cast<IShellBrowser
*>(this);
403 ///@todo use IShellBrowser::GetViewStateStream() and _pShellView->SaveViewState() to store view state
410 ShowExitWindowsDialog(_hwnd
);
414 if (wparam
== SC_TASKLIST
) {
416 SendMessage(_desktopBar
, nmsg
, wparam
, lparam
);
421 return super::WndProc(nmsg
, wparam
, lparam
);
428 HRESULT
DesktopWindow::OnDefaultCommand(LPIDA pida
)
430 if (MainFrame::OpenShellFolders(pida
, 0))
437 DesktopShellView::DesktopShellView(HWND hwnd
, IShellView
* pShellView
)
439 _pShellView(pShellView
)
441 _hwndListView
= ::GetNextWindow(hwnd
, GW_CHILD
);
443 SetWindowStyle(_hwndListView
, GetWindowStyle(_hwndListView
)&~LVS_ALIGNMASK
);//|LVS_ALIGNTOP|LVS_AUTOARRANGE);
445 // work around for Windows NT, Win 98, ...
446 // Without this the desktop has mysteriously only a size of 800x600 pixels.
447 ClientRect
rect(hwnd
);
448 MoveWindow(_hwndListView
, 0, 0, rect
.right
, rect
.bottom
, TRUE
);
450 // subclass background window
451 new BackgroundWindow(_hwndListView
);
453 _icon_algo
= 1; // default icon arrangement
459 bool DesktopShellView::InitDragDrop()
461 CONTEXT("DesktopShellView::InitDragDrop()");
463 _pDropTarget
= new DesktopDropTarget(_hwnd
);
468 _pDropTarget
->AddRef();
470 if (FAILED(RegisterDragDrop(_hwnd
, _pDropTarget
))) {
471 _pDropTarget
->Release();
476 _pDropTarget
->Release();
480 ftetc
.dwAspect
= DVASPECT_CONTENT
;
482 ftetc
.tymed
= TYMED_HGLOBAL
;
483 ftetc
.cfFormat
= CF_HDROP
;
485 _pDropTarget
->AddSuportedFormat(ftetc
);
490 LRESULT
DesktopShellView::WndProc(UINT nmsg
, WPARAM wparam
, LPARAM lparam
)
494 if (!DoContextMenu(GET_X_LPARAM(lparam
), GET_Y_LPARAM(lparam
)))
495 DoDesktopContextMenu(GET_X_LPARAM(lparam
), GET_Y_LPARAM(lparam
));
498 case PM_SET_ICON_ALGORITHM
:
503 case PM_GET_ICON_ALGORITHM
:
507 return super::WndProc(nmsg
, wparam
, lparam
);
513 int DesktopShellView::Command(int id
, int code
)
515 return super::Command(id
, code
);
518 int DesktopShellView::Notify(int id
, NMHDR
* pnmh
)
520 return super::Notify(id
, pnmh
);
523 bool DesktopShellView::DoContextMenu(int x
, int y
)
525 IDataObject
* selection
;
527 HRESULT hr
= _pShellView
->GetItemObject(SVGIO_SELECTION
, IID_IDataObject
, (void**)&selection
);
533 hr
= pidList
.GetData(selection
);
535 selection
->Release();
540 LPIDA pida
= pidList
;
542 selection
->Release();
546 LPCITEMIDLIST parent_pidl
= (LPCITEMIDLIST
) ((LPBYTE
)pida
+pida
->aoffset
[0]);
548 LPCITEMIDLIST
* apidl
= (LPCITEMIDLIST
*) alloca(pida
->cidl
*sizeof(LPCITEMIDLIST
));
550 for(int i
=pida
->cidl
; i
>0; --i
)
551 apidl
[i
-1] = (LPCITEMIDLIST
) ((LPBYTE
)pida
+pida
->aoffset
[i
]);
553 hr
= ShellFolderContextMenu(ShellFolder(parent_pidl
), _hwnd
, pida
->cidl
, apidl
, x
, y
);
555 selection
->Release();
562 HRESULT
DesktopShellView::DoDesktopContextMenu(int x
, int y
)
566 HRESULT hr
= DesktopFolder()->GetUIObjectOf(_hwnd
, 0, NULL
, IID_IContextMenu
, NULL
, (LPVOID
*)&pcm
);
569 HMENU hmenu
= CreatePopupMenu();
572 hr
= pcm
->QueryContextMenu(hmenu
, 0, FCIDM_SHVIEWFIRST
, FCIDM_SHVIEWLAST
-1, CMF_NORMAL
|CMF_EXPLORE
);
575 AppendMenu(hmenu
, MF_SEPARATOR
, 0, NULL
);
576 AppendMenu(hmenu
, 0, FCIDM_SHVIEWLAST
-1, ResString(IDS_ABOUT_EXPLORER
));
578 UINT idCmd
= TrackPopupMenu(hmenu
, TPM_LEFTALIGN
|TPM_RETURNCMD
|TPM_RIGHTBUTTON
, x
, y
, 0, _hwnd
, NULL
);
580 if (idCmd
== FCIDM_SHVIEWLAST
-1) {
581 explorer_about(_hwnd
);
583 CMINVOKECOMMANDINFO cmi
;
585 cmi
.cbSize
= sizeof(CMINVOKECOMMANDINFO
);
588 cmi
.lpVerb
= (LPCSTR
)(INT_PTR
)(idCmd
- FCIDM_SHVIEWFIRST
);
589 cmi
.lpParameters
= NULL
;
590 cmi
.lpDirectory
= NULL
;
591 cmi
.nShow
= SW_SHOWNORMAL
;
595 hr
= pcm
->InvokeCommand(&cmi
);
607 #define ARRANGE_BORDER_DOWN 8
608 #define ARRANGE_BORDER_HV 9
609 #define ARRANGE_ROUNDABOUT 10
611 static const POINTS s_align_start
[] = {
616 {0, 1}, // left/bottom
618 {1, 1}, // right/bottom
626 static const POINTS s_align_dir1
[] = {
641 static const POINTS s_align_dir2
[] = {
656 typedef pair
<int,int> IconPos
;
657 typedef map
<IconPos
, int> IconMap
;
659 void DesktopShellView::PositionIcons(int dir
)
661 DWORD spacing
= ListView_GetItemSpacing(_hwndListView
, FALSE
);
664 SystemParametersInfo(SPI_GETWORKAREA
, 0, &work_area
, 0);
666 const POINTS
& dir1
= s_align_dir1
[_icon_algo
];
667 const POINTS
& dir2
= s_align_dir2
[_icon_algo
];
668 const POINTS
& start_pos
= s_align_start
[_icon_algo
];
675 int cx
= LOWORD(spacing
);
676 int cy
= HIWORD(spacing
);
678 int dx1
= dir_x1
* cx
;
679 int dy1
= dir_y1
* cy
;
680 int dx2
= dir_x2
* cx
;
681 int dy2
= dir_y2
* cy
;
683 int start_x
= (start_pos
.x
* work_area
.right
)/cx
*cx
+ (cx
-32)/2;
684 int start_y
= (start_pos
.y
* work_area
.bottom
)/cy
*cy
+ 4/*(cy-32)/2*/;
686 if (start_x
>= work_area
.right
)
689 if (start_y
>= work_area
.bottom
)
695 int all
= ListView_GetItemCount(_hwndListView
);
709 for(int idx
=i1
; idx
!=i2
; idx
+=dir
) {
710 pos_idx
[IconPos(y
, x
)] = idx
;
712 if (_icon_algo
== ARRANGE_BORDER_DOWN
) {
714 x
= work_area
.right
- x
;
718 if (y
>= work_area
.bottom
) {
726 else if (_icon_algo
== ARRANGE_BORDER_HV
) {
728 x
= work_area
.right
- x
;
732 if (y
>= work_area
.bottom
) {
739 if (x
>= work_area
.right
) {
747 else if (_icon_algo
== ARRANGE_ROUNDABOUT
) {
756 if (x
<0 || x
>=work_area
.right
) {
759 } else if (y
<0 || y
>=work_area
.bottom
) {
765 // use a little trick to get the icons where we want them to be...
767 for(IconMap::const_iterator it
=pos_idx
.end(); --it
!=pos_idx
.begin(); ) {
768 const IconPos
& pos
= it
->first
;
770 ListView_SetItemPosition32(_hwndListView
, it
->second
, pos
.second
, pos
.first
);
773 for(IconMap::const_iterator it
=pos_idx
.begin(); it
!=pos_idx
.end(); ++it
) {
774 const IconPos
& pos
= it
->first
;
776 ListView_SetItemPosition32(_hwndListView
, it
->second
, pos
.second
, pos
.first
);