[CMAKE]
[reactos.git] / base / shell / explorer / desktop / desktop.cpp
1 /*
2 * Copyright 2003, 2004, 2005 Martin Fuchs
3 *
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.
8 *
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.
13 *
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19
20 //
21 // Explorer clone
22 //
23 // desktop.cpp
24 //
25 // Martin Fuchs, 09.08.2003
26 //
27
28
29 #include <precomp.h>
30
31 #include "../resource.h"
32
33 #include "../taskbar/desktopbar.h"
34 #include "../taskbar/taskbar.h" // for PM_GET_LAST_ACTIVE
35
36
37 static BOOL (WINAPI*SetShellWindow)(HWND);
38 static BOOL (WINAPI*SetShellWindowEx)(HWND, HWND);
39
40
41 #ifdef _USE_HDESK
42
43 Desktop::Desktop(HDESK hdesktop/*, HWINSTA hwinsta*/)
44 : _hdesktop(hdesktop)
45 // _hwinsta(hwinsta)
46 {
47 }
48
49 Desktop::~Desktop()
50 {
51 if (_hdesktop)
52 CloseDesktop(_hdesktop);
53
54 // if (_hwinsta)
55 // CloseWindowStation(_hwinsta);
56
57 if (_pThread.get()) {
58 _pThread->Stop();
59 _pThread.release();
60 }
61 }
62
63 #endif
64
65
66 Desktops::Desktops()
67 : _current_desktop(0)
68 {
69 }
70
71 Desktops::~Desktops()
72 {
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);
77 }
78
79 void Desktops::init()
80 {
81 resize(DESKTOP_COUNT);
82
83 #ifdef _USE_HDESK
84 DesktopPtr& desktop = (*this)[0];
85
86 desktop = DesktopPtr(new Desktop(OpenInputDesktop(0, FALSE, DESKTOP_SWITCHDESKTOP)));
87 #endif
88 }
89
90 #ifdef _USE_HDESK
91
92 void Desktops::SwitchToDesktop(int idx)
93 {
94 if (_current_desktop == idx)
95 return;
96
97 DesktopPtr& desktop = (*this)[idx];
98
99 DesktopThread* pThread = NULL;
100
101 if (desktop.get()) {
102 if (desktop->_hdesktop)
103 if (!SwitchDesktop(desktop->_hdesktop))
104 return;
105 } else {
106 FmtString desktop_name(TEXT("Desktop %d"), idx);
107
108 SECURITY_ATTRIBUTES saAttr = {sizeof(SECURITY_ATTRIBUTES), 0, TRUE};
109 /*
110 HWINSTA hwinsta = CreateWindowStation(TEXT("ExplorerWinStation"), 0, GENERIC_ALL, &saAttr);
111
112 if (!SetProcessWindowStation(hwinsta))
113 return;
114 */
115 HDESK hdesktop = CreateDesktop(desktop_name, NULL, NULL, 0, GENERIC_ALL, &saAttr);
116 if (!hdesktop)
117 return;
118
119 desktop = DesktopPtr(new Desktop(hdesktop/*, hwinsta*/));
120
121 pThread = new DesktopThread(*desktop);
122 }
123
124 _current_desktop = idx;
125
126 if (pThread) {
127 desktop->_pThread = DesktopThreadPtr(pThread);
128 pThread->Start();
129 }
130 }
131
132 int DesktopThread::Run()
133 {
134 if (!SetThreadDesktop(_desktop._hdesktop))
135 return -1;
136
137 HDESK hDesk_old = OpenInputDesktop(0, FALSE, DESKTOP_SWITCHDESKTOP);
138
139 if (!SwitchDesktop(_desktop._hdesktop))
140 return -1;
141
142 if (!_desktop._hwndDesktop)
143 _desktop._hwndDesktop = DesktopWindow::Create();
144
145 int ret = Window::MessageLoop();
146
147 SwitchDesktop(hDesk_old);
148
149 return ret;
150 }
151
152 #else // _USE_HDESK
153
154 static BOOL CALLBACK SwitchDesktopEnumFct(HWND hwnd, LPARAM lparam)
155 {
156 WindowSet& windows = *(WindowSet*)lparam;
157
158 if (hwnd!=g_Globals._hwndDesktopBar && hwnd!=g_Globals._hwndDesktop)
159 if (IsWindowVisible(hwnd))
160 windows.insert(hwnd);
161
162 return TRUE;
163 }
164
165 void Desktops::SwitchToDesktop(int idx)
166 {
167 if (_current_desktop == idx)
168 return;
169
170 Desktop& old_desktop = (*this)[_current_desktop];
171 WindowSet& windows = old_desktop._windows;
172 Desktop& desktop = (*this)[idx];
173
174 windows.clear();
175
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);
181
182 // save currently visible application windows
183 EnumWindows(SwitchDesktopEnumFct, (LPARAM)&windows);
184
185 old_desktop._hwndForeground = (HWND)SendMessage(g_Globals._hwndDesktopBar, PM_GET_LAST_ACTIVE, 0, 0);
186
187 // hide all windows of the previous desktop
188 for(WindowSet::iterator it=windows.begin(); it!=windows.end(); ++it)
189 ShowWindowAsync(*it, SW_HIDE);
190
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);
194
195 if (desktop._hwndForeground)
196 SetForegroundWindow(desktop._hwndForeground);
197
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)
200 windows.erase(*it);
201
202 // We don't need to store the window handles of what's now visible the now current desktop.
203 desktop._windows.clear();
204
205 _current_desktop = idx;
206 }
207
208 #endif // _USE_HDESK
209
210
211 static BOOL CALLBACK MinimizeDesktopEnumFct(HWND hwnd, LPARAM lparam)
212 {
213 list<MinimizeStruct>& minimized = *(list<MinimizeStruct>*)lparam;
214
215 if (hwnd!=g_Globals._hwndDesktopBar && hwnd!=g_Globals._hwndDesktop)
216 if (IsWindowVisible(hwnd) && !IsIconic(hwnd)) {
217 RECT rect;
218
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);
224 }
225 }
226
227 return TRUE;
228 }
229
230 /// minimize/restore all windows on the desktop
231 void Desktops::ToggleMinimize()
232 {
233 list<MinimizeStruct>& minimized = (*this)[_current_desktop]._minimized;
234
235 if (minimized.empty()) {
236 EnumWindows(MinimizeDesktopEnumFct, (LPARAM)&minimized);
237 } else {
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);
240
241 minimized.clear();
242 }
243 }
244
245
246 BOOL IsAnyDesktopRunning()
247 {
248 HINSTANCE hUser32 = GetModuleHandle(TEXT("user32"));
249
250 SetShellWindow = (BOOL(WINAPI*)(HWND)) GetProcAddress(hUser32, "SetShellWindow");
251 SetShellWindowEx = (BOOL(WINAPI*)(HWND,HWND)) GetProcAddress(hUser32, "SetShellWindowEx");
252
253 return GetShellWindow() != 0;
254 }
255
256
257 BackgroundWindow::BackgroundWindow(HWND hwnd)
258 : super(hwnd)
259 {
260 // set background brush for the short moment of displaying the
261 // background color while moving foreground windows
262 SetClassLongPtr(hwnd, GCL_HBRBACKGROUND, COLOR_BACKGROUND+1);
263
264 _display_version = RegGetDWORDValue(HKEY_CURRENT_USER, TEXT("Control Panel\\Desktop"), TEXT("PaintDesktopVersion"), 1);
265 }
266
267 LRESULT BackgroundWindow::WndProc(UINT nmsg, WPARAM wparam, LPARAM lparam)
268 {
269 switch(nmsg) {
270 case WM_ERASEBKGND:
271 DrawDesktopBkgnd((HDC)wparam);
272 return TRUE;
273
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);
278 break;
279
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);
289 }
290 return _display_version;
291
292 default:
293 return super::WndProc(nmsg, wparam, lparam);
294 }
295
296 return 0;
297 }
298
299 void BackgroundWindow::DrawDesktopBkgnd(HDC hdc)
300 {
301 PaintDesktop(hdc);
302
303 /* special solid background
304 HBRUSH bkgndBrush = CreateSolidBrush(RGB(0,32,160)); // dark blue
305 FillRect(hdc, &rect, bkgndBrush);
306 DeleteBrush(bkgndBrush);
307 */
308 }
309
310
311 DesktopWindow::DesktopWindow(HWND hwnd)
312 : super(hwnd)
313 {
314 _pShellView = NULL;
315 }
316
317 DesktopWindow::~DesktopWindow()
318 {
319 if (_pShellView)
320 _pShellView->Release();
321 }
322
323
324 HWND DesktopWindow::Create()
325 {
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); */
329
330 int width = GetSystemMetrics(SM_CXSCREEN);
331 int height = GetSystemMetrics(SM_CYSCREEN);
332
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);
336
337 // work around to display desktop bar in Wine
338 ShowWindow(GET_WINDOW(DesktopWindow, hwndDesktop)->_desktopBar, SW_SHOW);
339
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);
343
344 return hwndDesktop;
345 }
346
347
348 LRESULT DesktopWindow::Init(LPCREATESTRUCT pcs)
349 {
350 if (super::Init(pcs))
351 return 1;
352
353 HRESULT hr = GetDesktopFolder()->CreateViewObject(_hwnd, IID_IShellView, (void**)&_pShellView);
354 /* also possible:
355 SFV_CREATE sfv_create;
356
357 sfv_create.cbSize = sizeof(SFV_CREATE);
358 sfv_create.pshf = GetDesktopFolder();
359 sfv_create.psvOuter = NULL;
360 sfv_create.psfvcb = NULL;
361
362 HRESULT hr = SHCreateShellFolderView(&sfv_create, &_pShellView);
363 */
364 HWND hWndView = 0;
365
366 if (SUCCEEDED(hr)) {
367 FOLDERSETTINGS fs;
368
369 fs.ViewMode = FVM_ICON;
370 fs.fFlags = FWF_DESKTOP|FWF_NOCLIENTEDGE|FWF_NOSCROLL|FWF_BESTFITWINDOW|FWF_SNAPTOGRID; //|FWF_AUTOARRANGE;
371
372 ClientRect rect(_hwnd);
373
374 hr = _pShellView->CreateViewWindow(NULL, &fs, this, &rect, &hWndView);
375
376 ///@todo use IShellBrowser::GetViewStateStream() to restore previous view state -> see SHOpenRegStream()
377
378 if (SUCCEEDED(hr)) {
379 g_Globals._hwndShellView = hWndView;
380
381 // subclass shellview window
382 new DesktopShellView(hWndView, _pShellView);
383
384 _pShellView->UIActivate(SVUIA_ACTIVATE_FOCUS);
385
386 /*
387 IShellView2* pShellView2;
388
389 hr = _pShellView->QueryInterface(IID_IShellView2, (void**)&pShellView2);
390
391 SV2CVW2_PARAMS params;
392 params.cbSize = sizeof(SV2CVW2_PARAMS);
393 params.psvPrev = _pShellView;
394 params.pfs = &fs;
395 params.psbOwner = this;
396 params.prcView = &rect;
397 params.pvid = params.pvid;//@@
398
399 hr = pShellView2->CreateViewWindow2(&params);
400 params.pvid;
401 */
402
403 /*
404 IFolderView* pFolderView;
405
406 hr = _pShellView->QueryInterface(IID_IFolderView, (void**)&pFolderView);
407
408 if (SUCCEEDED(hr)) {
409 hr = pFolderView->GetAutoArrange();
410 hr = pFolderView->SetCurrentViewMode(FVM_DETAILS);
411 }
412 */
413 }
414 }
415
416 if (hWndView && SetShellWindowEx)
417 SetShellWindowEx(_hwnd, hWndView);
418 else if (SetShellWindow)
419 SetShellWindow(_hwnd);
420
421 // create the explorer bar
422 _desktopBar = DesktopBar::Create();
423 g_Globals._hwndDesktopBar = _desktopBar;
424
425 return 0;
426 }
427
428
429 LRESULT DesktopWindow::WndProc(UINT nmsg, WPARAM wparam, LPARAM lparam)
430 {
431 switch(nmsg) {
432 case WM_LBUTTONDBLCLK:
433 case WM_RBUTTONDBLCLK:
434 case WM_MBUTTONDBLCLK:
435 explorer_show_frame(SW_SHOWNORMAL);
436 break;
437
438 case WM_GETISHELLBROWSER:
439 return (LRESULT)static_cast<IShellBrowser*>(this);
440
441 case WM_DESTROY:
442
443 ///@todo use IShellBrowser::GetViewStateStream() and _pShellView->SaveViewState() to store view state
444
445 if (SetShellWindow)
446 SetShellWindow(0);
447 break;
448
449 case WM_CLOSE:
450 ShowExitWindowsDialog(_hwnd);
451 break;
452
453 case WM_SYSCOMMAND:
454 if (wparam == SC_TASKLIST) {
455 if (_desktopBar)
456 SendMessage(_desktopBar, nmsg, wparam, lparam);
457 }
458 goto def;
459
460 case WM_SYSCOLORCHANGE:
461 // redraw background window
462 InvalidateRect(g_Globals._hwndShellView, NULL, TRUE);
463
464 // forward message to shell view window to redraw icon backgrounds
465 SendMessage(g_Globals._hwndShellView, WM_SYSCOLORCHANGE, wparam, lparam);
466 break;
467
468 default: def:
469 return super::WndProc(nmsg, wparam, lparam);
470 }
471
472 return 0;
473 }
474
475
476 HRESULT DesktopWindow::OnDefaultCommand(LPIDA pida)
477 {
478 #ifndef ROSSHELL // in shell-only-mode fall through and let shell32 handle the command
479 if (MainFrameBase::OpenShellFolders(pida, 0))
480 return S_OK;
481 #endif
482
483 return E_NOTIMPL;
484 }
485
486
487 DesktopShellView::DesktopShellView(HWND hwnd, IShellView* pShellView)
488 : super(hwnd),
489 _pShellView(pShellView)
490 {
491 _hwndListView = GetNextWindow(hwnd, GW_CHILD);
492
493 SetWindowStyle(_hwndListView, GetWindowStyle(_hwndListView)&~LVS_ALIGNMASK);//|LVS_ALIGNTOP|LVS_AUTOARRANGE);
494
495 // work around for Windows NT, Win 98, ...
496 // Without this the desktop has mysteriously only a size of 800x600 pixels.
497 ClientRect rect(hwnd);
498 MoveWindow(_hwndListView, 0, 0, rect.right, rect.bottom, TRUE);
499
500 // subclass background window
501 new BackgroundWindow(_hwndListView);
502
503 _icon_algo = 1; // default icon arrangement
504
505 PositionIcons();
506 InitDragDrop();
507 }
508
509
510 DesktopShellView::~DesktopShellView()
511 {
512 if (FAILED(RevokeDragDrop(_hwnd)))
513 assert(0);
514 }
515
516
517 bool DesktopShellView::InitDragDrop()
518 {
519 CONTEXT("DesktopShellView::InitDragDrop()");
520
521 DesktopDropTarget * pDropTarget = new DesktopDropTarget(_hwnd);
522
523 if (!pDropTarget)
524 return false;
525
526 pDropTarget->AddRef();
527
528 if (FAILED(RegisterDragDrop(_hwnd, pDropTarget))) {
529 pDropTarget->Release();
530 return false;
531 }
532
533 FORMATETC ftetc;
534
535 ftetc.dwAspect = DVASPECT_CONTENT;
536 ftetc.lindex = -1;
537 ftetc.tymed = TYMED_HGLOBAL;
538 ftetc.cfFormat = CF_HDROP;
539
540 pDropTarget->AddSuportedFormat(ftetc);
541 pDropTarget->Release();
542
543 return true;
544 }
545
546 LRESULT DesktopShellView::WndProc(UINT nmsg, WPARAM wparam, LPARAM lparam)
547 {
548 switch(nmsg) {
549 case WM_CONTEXTMENU:
550 if (!DoContextMenu(GET_X_LPARAM(lparam), GET_Y_LPARAM(lparam)))
551 DoDesktopContextMenu(GET_X_LPARAM(lparam), GET_Y_LPARAM(lparam));
552 break;
553
554 case PM_SET_ICON_ALGORITHM:
555 _icon_algo = wparam;
556 PositionIcons();
557 break;
558
559 case PM_GET_ICON_ALGORITHM:
560 return _icon_algo;
561
562 case PM_DISPLAY_VERSION:
563 return SendMessage(_hwndListView, nmsg, wparam, lparam);
564
565 default:
566 return super::WndProc(nmsg, wparam, lparam);
567 }
568
569 return 0;
570 }
571
572 int DesktopShellView::Command(int id, int code)
573 {
574 return super::Command(id, code);
575 }
576
577 int DesktopShellView::Notify(int id, NMHDR* pnmh)
578 {
579 return super::Notify(id, pnmh);
580 }
581
582 bool DesktopShellView::DoContextMenu(int x, int y)
583 {
584 IDataObject* selection;
585
586 HRESULT hr = _pShellView->GetItemObject(SVGIO_SELECTION, IID_IDataObject, (void**)&selection);
587 if (FAILED(hr))
588 return false;
589
590 PIDList pidList;
591
592 hr = pidList.GetData(selection);
593 if (FAILED(hr)) {
594 selection->Release();
595 //CHECKERROR(hr);
596 return false;
597 }
598
599 LPIDA pida = pidList;
600 if (!pida->cidl) {
601 selection->Release();
602 return false;
603 }
604
605 LPCITEMIDLIST parent_pidl = (LPCITEMIDLIST) ((LPBYTE)pida+pida->aoffset[0]);
606
607 LPCITEMIDLIST* apidl = (LPCITEMIDLIST*) alloca(pida->cidl*sizeof(LPCITEMIDLIST));
608
609 for(int i=pida->cidl; i>0; --i)
610 apidl[i-1] = (LPCITEMIDLIST) ((LPBYTE)pida+pida->aoffset[i]);
611
612 hr = ShellFolderContextMenu(ShellFolder(parent_pidl), _hwnd, pida->cidl, apidl, x, y, _cm_ifs);
613
614 selection->Release();
615
616 if (SUCCEEDED(hr))
617 refresh();
618 else
619 CHECKERROR(hr);
620
621 return true;
622 }
623
624 HRESULT DesktopShellView::DoDesktopContextMenu(int x, int y)
625 {
626 IContextMenu* pcm;
627
628 HRESULT hr = DesktopFolder()->GetUIObjectOf(_hwnd, 0, NULL, IID_IContextMenu, NULL, (LPVOID*)&pcm);
629
630 if (SUCCEEDED(hr)) {
631 pcm = _cm_ifs.query_interfaces(pcm);
632
633 HMENU hmenu = CreatePopupMenu();
634
635 if (hmenu) {
636 hr = pcm->QueryContextMenu(hmenu, 0, FCIDM_SHVIEWFIRST, FCIDM_SHVIEWLAST-1, CMF_NORMAL|CMF_EXPLORE);
637
638 if (SUCCEEDED(hr)) {
639 AppendMenu(hmenu, MF_SEPARATOR, 0, NULL);
640 AppendMenu(hmenu, 0, FCIDM_SHVIEWLAST-1, ResString(IDS_ABOUT_EXPLORER));
641
642 UINT idCmd = TrackPopupMenu(hmenu, TPM_LEFTALIGN|TPM_RETURNCMD|TPM_RIGHTBUTTON, x, y, 0, _hwnd, NULL);
643
644 _cm_ifs.reset();
645
646 if (idCmd == FCIDM_SHVIEWLAST-1) {
647 explorer_about(_hwnd);
648 } else if (idCmd) {
649 CMINVOKECOMMANDINFO cmi;
650
651 cmi.cbSize = sizeof(CMINVOKECOMMANDINFO);
652 cmi.fMask = 0;
653 cmi.hwnd = _hwnd;
654 cmi.lpVerb = (LPCSTR)(INT_PTR)(idCmd - FCIDM_SHVIEWFIRST);
655 cmi.lpParameters = NULL;
656 cmi.lpDirectory = NULL;
657 cmi.nShow = SW_SHOWNORMAL;
658 cmi.dwHotKey = 0;
659 cmi.hIcon = 0;
660
661 hr = pcm->InvokeCommand(&cmi);
662 }
663 } else
664 _cm_ifs.reset();
665 DestroyMenu(hmenu);
666 }
667
668 pcm->Release();
669 }
670
671 return hr;
672 }
673
674
675 #define ARRANGE_BORDER_DOWN 8
676 #define ARRANGE_BORDER_HV 9
677 #define ARRANGE_ROUNDABOUT 10
678
679 static const POINTS s_align_start[] = {
680 {0, 0}, // left/top
681 {0, 0},
682 {1, 0}, // right/top
683 {1, 0},
684 {0, 1}, // left/bottom
685 {0, 1},
686 {1, 1}, // right/bottom
687 {1, 1},
688
689 {0, 0}, // left/top
690 {0, 0},
691 {0, 0}
692 };
693
694 static const POINTS s_align_dir1[] = {
695 { 0, +1}, // down
696 {+1, 0}, // right
697 {-1, 0}, // left
698 { 0, +1}, // down
699 { 0, -1}, // up
700 {+1, 0}, // right
701 {-1, 0}, // left
702 { 0, -1}, // up
703
704 { 0, +1}, // down
705 {+1, 0}, // right
706 {+1, 0} // right
707 };
708
709 static const POINTS s_align_dir2[] = {
710 {+1, 0}, // right
711 { 0, +1}, // down
712 { 0, +1}, // down
713 {-1, 0}, // left
714 {+1, 0}, // right
715 { 0, -1}, // up
716 { 0, -1}, // up
717 {-1, 0}, // left
718
719 {+1, 0}, // right
720 { 0, +1}, // down
721 { 0, +1} // down
722 };
723
724 typedef pair<int,int> IconPos;
725 typedef map<IconPos, int> IconMap;
726
727 void DesktopShellView::PositionIcons(int dir)
728 {
729 DWORD spacing = ListView_GetItemSpacing(_hwndListView, FALSE);
730
731 RECT work_area;
732 SystemParametersInfo(SPI_GETWORKAREA, 0, &work_area, 0);
733
734 const POINTS& dir1 = s_align_dir1[_icon_algo];
735 const POINTS& dir2 = s_align_dir2[_icon_algo];
736 const POINTS& start_pos = s_align_start[_icon_algo];
737
738 int dir_x1 = dir1.x;
739 int dir_y1 = dir1.y;
740 int dir_x2 = dir2.x;
741 int dir_y2 = dir2.y;
742
743 int cx = LOWORD(spacing);
744 int cy = HIWORD(spacing);
745
746 int dx1 = dir_x1 * cx;
747 int dy1 = dir_y1 * cy;
748 int dx2 = dir_x2 * cx;
749 int dy2 = dir_y2 * cy;
750
751 int xoffset = (cx-32)/2;
752 int yoffset = 4/*(cy-32)/2*/;
753
754 int start_x = start_pos.x * (work_area.right - cx) + xoffset;
755 int start_y = start_pos.y * (work_area.bottom - cy) + yoffset;
756
757 int x = start_x;
758 int y = start_y;
759
760 int all = ListView_GetItemCount(_hwndListView);
761 int i1, i2;
762
763 if (dir > 0) {
764 i1 = 0;
765 i2 = all;
766 } else {
767 i1 = all-1;
768 i2 = -1;
769 }
770
771 IconMap pos_idx;
772 int cnt = 0;
773 int xhv = start_x;
774 int yhv = start_y;
775
776 for(int idx=i1; idx!=i2; idx+=dir) {
777 pos_idx[IconPos(y, x)] = idx;
778
779 if (_icon_algo == ARRANGE_BORDER_DOWN) {
780 if (++cnt & 1)
781 x = work_area.right - x - cx + 2*xoffset;
782 else {
783 y += dy1;
784
785 if (y + cy - 2 * yoffset > work_area.bottom) {
786 y = start_y;
787 start_x += dx2;
788 x = start_x;
789 }
790 }
791
792 continue;
793 }
794 else if (_icon_algo == ARRANGE_BORDER_HV) {
795 if (++cnt & 1)
796 x = work_area.right - x - cx + 2*xoffset;
797 else if (cnt & 2) {
798 yhv += cy;
799 y = yhv;
800 x = start_x;
801
802 if (y + cy - 2 * yoffset > work_area.bottom) {
803 start_x += cx;
804 xhv = start_x;
805 x = xhv;
806 start_y += cy;
807 yhv = start_y;
808 y = yhv;
809 }
810 } else {
811 xhv += cx;
812 x = xhv;
813 y = start_y;
814
815 if (x + cx - 2 * xoffset > work_area.right) {
816 start_x += cx;
817 xhv = start_x;
818 x = xhv;
819 start_y += cy;
820 yhv = start_y;
821 y = yhv;
822 }
823 }
824
825 continue;
826 }
827 else if (_icon_algo == ARRANGE_ROUNDABOUT) {
828
829 ///@todo
830
831 }
832
833 x += dx1;
834 y += dy1;
835
836 if (x<0 || x+cx-2*xoffset>work_area.right) {
837 x = start_x;
838 y += dy2;
839 } else if (y<0 || y+cy-2*yoffset>work_area.bottom) {
840 y = start_y;
841 x += dx2;
842 }
843 }
844
845 // use a little trick to get the icons where we want them to be...
846
847 //for(IconMap::const_iterator it=pos_idx.end(); --it!=pos_idx.begin(); ) {
848 // const IconPos& pos = it->first;
849
850 // ListView_SetItemPosition32(_hwndListView, it->second, pos.second, pos.first);
851 //}
852
853 for(IconMap::const_iterator it=pos_idx.begin(); it!=pos_idx.end(); ++it) {
854 const IconPos& pos = it->first;
855
856 ListView_SetItemPosition32(_hwndListView, it->second, pos.second, pos.first);
857 }
858 }
859
860
861 void DesktopShellView::refresh()
862 {
863 ///@todo
864 }