display version information in about dialog and on the desktop
[reactos.git] / reactos / subsys / system / explorer / desktop / desktop.cpp
1 /*
2 * Copyright 2003, 2004 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 "../explorer_intres.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 (IsWindowVisible(hwnd))
159 if (hwnd!=g_Globals._hwndDesktopBar && hwnd!=g_Globals._hwndDesktop)
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 (IsWindowVisible(hwnd))
216 if (hwnd!=g_Globals._hwndDesktopBar && hwnd!=g_Globals._hwndDesktop)
217 if (!IsIconic(hwnd)) {
218 minimized.push_back(MinimizeStruct(hwnd, GetWindowStyle(hwnd)));
219 ShowWindowAsync(hwnd, SW_MINIMIZE);
220 }
221
222 return TRUE;
223 }
224
225 /// minimize/restore all windows on the desktop
226 void Desktops::ToggleMinimize()
227 {
228 list<MinimizeStruct>& minimized = (*this)[_current_desktop]._minimized;
229
230 if (minimized.empty()) {
231 EnumWindows(MinimizeDesktopEnumFct, (LPARAM)&minimized);
232 } else {
233 for(list<MinimizeStruct>::const_iterator it=minimized.begin(); it!=minimized.end(); ++it)
234 ShowWindowAsync(it->first, it->second&WS_MAXIMIZE? SW_MAXIMIZE: SW_RESTORE);
235
236 minimized.clear();
237 }
238 }
239
240
241 BOOL IsAnyDesktopRunning()
242 {
243 HINSTANCE hUser32 = GetModuleHandle(TEXT("user32"));
244
245 SetShellWindow = (BOOL(WINAPI*)(HWND)) GetProcAddress(hUser32, "SetShellWindow");
246 SetShellWindowEx = (BOOL(WINAPI*)(HWND,HWND)) GetProcAddress(hUser32, "SetShellWindowEx");
247
248 return GetShellWindow() != 0;
249 }
250
251
252 BackgroundWindow::BackgroundWindow(HWND hwnd)
253 : super(hwnd)
254 {
255 // set background brush for the short moment of displaying the
256 // background color while moving foreground windows
257 SetClassLong(hwnd, GCL_HBRBACKGROUND, COLOR_BACKGROUND+1);
258
259 _display_version = RegGetDWORDValue(HKEY_CURRENT_USER, TEXT("Control Panel\\Desktop"), TEXT("PaintDesktopVersion"), 1);
260 }
261
262 LRESULT BackgroundWindow::WndProc(UINT nmsg, WPARAM wparam, LPARAM lparam)
263 {
264 switch(nmsg) {
265 case WM_ERASEBKGND:
266 DrawDesktopBkgnd((HDC)wparam);
267 return TRUE;
268
269 case WM_MBUTTONDBLCLK:
270 /* Imagelist icons are missing if MainFrame::Create() is called directly from here!
271 explorer_show_frame(SW_SHOWNORMAL); */
272 PostMessage(g_Globals._hwndDesktop, nmsg, wparam, lparam);
273 break;
274
275 case PM_DISPLAY_VERSION:
276 if (lparam || wparam) {
277 DWORD or_mask = wparam;
278 DWORD reset_mask = LOWORD(lparam);
279 DWORD xor_mask = HIWORD(lparam);
280 _display_version = ((_display_version&~reset_mask) | or_mask) ^ xor_mask;
281 InvalidateRect(_hwnd, NULL, TRUE);
282 }
283 return _display_version;
284
285 default:
286 return super::WndProc(nmsg, wparam, lparam);
287 }
288
289 return 0;
290 }
291
292 void BackgroundWindow::DrawDesktopBkgnd(HDC hdc)
293 {
294 PaintDesktop(hdc);
295
296 /* special solid background
297 HBRUSH bkgndBrush = CreateSolidBrush(RGB(0,32,160)); // dark blue
298 FillRect(hdc, &rect, bkgndBrush);
299 DeleteBrush(bkgndBrush);
300 */
301 if (_display_version) {
302 static const String s_bkgnd_txt = ResString(IDS_EXPLORER_VERSION_STR) + TEXT("\nby Martin Fuchs\n%s");
303 static String s_version_str = get_windows_version_str();
304
305 FmtString txt(s_bkgnd_txt, (LPCTSTR)ResString(IDS_VERSION_STR), (LPCTSTR)s_version_str);
306 ClientRect rect(_hwnd);
307
308 rect.left = rect.right - 440;
309 rect.top = rect.bottom - 90 - DESKTOPBARBAR_HEIGHT;
310 rect.right = rect.left + 420;
311 rect.bottom = rect.top + 70;
312
313 BkMode bkMode(hdc, TRANSPARENT);
314
315 TextColor textColor(hdc, RGB(128,128,192));
316 DrawText(hdc, txt, -1, &rect, DT_RIGHT);
317
318 SetTextColor(hdc, RGB(255,255,255));
319 --rect.right;
320 ++rect.top;
321 DrawText(hdc, txt, -1, &rect, DT_RIGHT);
322 }
323 }
324
325
326 DesktopWindow::DesktopWindow(HWND hwnd)
327 : super(hwnd)
328 {
329 _pShellView = NULL;
330 }
331
332 DesktopWindow::~DesktopWindow()
333 {
334 if (_pShellView)
335 _pShellView->Release();
336 }
337
338
339 HWND DesktopWindow::Create()
340 {
341 static IconWindowClass wcDesktop(TEXT("Progman"), IDI_REACTOS, CS_DBLCLKS);
342 /* (disabled because of small ugly temporary artefacts when hiding start menu)
343 wcDesktop.hbrBackground = (HBRUSH)(COLOR_BACKGROUND+1); */
344
345 int width = GetSystemMetrics(SM_CXSCREEN);
346 int height = GetSystemMetrics(SM_CYSCREEN);
347
348 HWND hwndDesktop = Window::Create(WINDOW_CREATOR(DesktopWindow),
349 WS_EX_TOOLWINDOW, wcDesktop, TEXT("Program Manager"), WS_POPUP|WS_VISIBLE, //|WS_CLIPCHILDREN for SDI frames
350 0, 0, width, height, 0);
351
352 // work around to display desktop bar in Wine
353 ShowWindow(GET_WINDOW(DesktopWindow, hwndDesktop)->_desktopBar, SW_SHOW);
354
355 // work around for Windows NT, Win 98, ...
356 // Without this the desktop has mysteriously only a size of 800x600 pixels.
357 MoveWindow(hwndDesktop, 0, 0, width, height, TRUE);
358
359 return hwndDesktop;
360 }
361
362
363 LRESULT DesktopWindow::Init(LPCREATESTRUCT pcs)
364 {
365 if (super::Init(pcs))
366 return 1;
367
368 HRESULT hr = GetDesktopFolder()->CreateViewObject(_hwnd, IID_IShellView, (void**)&_pShellView);
369 /* also possible:
370 SFV_CREATE sfv_create;
371
372 sfv_create.cbSize = sizeof(SFV_CREATE);
373 sfv_create.pshf = GetDesktopFolder();
374 sfv_create.psvOuter = NULL;
375 sfv_create.psfvcb = NULL;
376
377 HRESULT hr = SHCreateShellFolderView(&sfv_create, &_pShellView);
378 */
379 HWND hWndView = 0;
380
381 if (SUCCEEDED(hr)) {
382 FOLDERSETTINGS fs;
383
384 fs.ViewMode = FVM_ICON;
385 fs.fFlags = FWF_DESKTOP|FWF_NOCLIENTEDGE|FWF_NOSCROLL|FWF_BESTFITWINDOW|FWF_SNAPTOGRID; //|FWF_AUTOARRANGE;
386
387 ClientRect rect(_hwnd);
388
389 hr = _pShellView->CreateViewWindow(NULL, &fs, this, &rect, &hWndView);
390
391 ///@todo use IShellBrowser::GetViewStateStream() to restore previous view state -> see SHOpenRegStream()
392
393 if (SUCCEEDED(hr)) {
394 g_Globals._hwndShellView = hWndView;
395
396 // subclass shellview window
397 new DesktopShellView(hWndView, _pShellView);
398
399 _pShellView->UIActivate(SVUIA_ACTIVATE_FOCUS);
400
401 /*
402 IShellView2* pShellView2;
403
404 hr = _pShellView->QueryInterface(IID_IShellView2, (void**)&pShellView2);
405
406 SV2CVW2_PARAMS params;
407 params.cbSize = sizeof(SV2CVW2_PARAMS);
408 params.psvPrev = _pShellView;
409 params.pfs = &fs;
410 params.psbOwner = this;
411 params.prcView = &rect;
412 params.pvid = params.pvid;//@@
413
414 hr = pShellView2->CreateViewWindow2(&params);
415 params.pvid;
416 */
417
418 /*
419 IFolderView* pFolderView;
420
421 hr = _pShellView->QueryInterface(IID_IFolderView, (void**)&pFolderView);
422
423 if (SUCCEEDED(hr)) {
424 hr = pFolderView->GetAutoArrange();
425 hr = pFolderView->SetCurrentViewMode(FVM_DETAILS);
426 }
427 */
428 }
429 }
430
431 if (hWndView && SetShellWindowEx)
432 SetShellWindowEx(_hwnd, hWndView);
433 else if (SetShellWindow)
434 SetShellWindow(_hwnd);
435
436 // create the explorer bar
437 _desktopBar = DesktopBar::Create();
438 g_Globals._hwndDesktopBar = _desktopBar;
439
440 return 0;
441 }
442
443
444 LRESULT DesktopWindow::WndProc(UINT nmsg, WPARAM wparam, LPARAM lparam)
445 {
446 switch(nmsg) {
447 case WM_LBUTTONDBLCLK:
448 case WM_RBUTTONDBLCLK:
449 case WM_MBUTTONDBLCLK:
450 explorer_show_frame(SW_SHOWNORMAL);
451 break;
452
453 case WM_GETISHELLBROWSER:
454 return (LRESULT)static_cast<IShellBrowser*>(this);
455
456 case WM_DESTROY:
457
458 ///@todo use IShellBrowser::GetViewStateStream() and _pShellView->SaveViewState() to store view state
459
460 if (SetShellWindow)
461 SetShellWindow(0);
462 break;
463
464 case WM_CLOSE:
465 ShowExitWindowsDialog(_hwnd);
466 break;
467
468 case WM_SYSCOMMAND:
469 if (wparam == SC_TASKLIST) {
470 if (_desktopBar)
471 SendMessage(_desktopBar, nmsg, wparam, lparam);
472 }
473 goto def;
474
475 default: def:
476 return super::WndProc(nmsg, wparam, lparam);
477 }
478
479 return 0;
480 }
481
482
483 HRESULT DesktopWindow::OnDefaultCommand(LPIDA pida)
484 {
485 if (MainFrameBase::OpenShellFolders(pida, 0))
486 return S_OK;
487
488 return E_NOTIMPL;
489 }
490
491
492 DesktopShellView::DesktopShellView(HWND hwnd, IShellView* pShellView)
493 : super(hwnd),
494 _pShellView(pShellView)
495 {
496 _hwndListView = ::GetNextWindow(hwnd, GW_CHILD);
497
498 SetWindowStyle(_hwndListView, GetWindowStyle(_hwndListView)&~LVS_ALIGNMASK);//|LVS_ALIGNTOP|LVS_AUTOARRANGE);
499
500 // work around for Windows NT, Win 98, ...
501 // Without this the desktop has mysteriously only a size of 800x600 pixels.
502 ClientRect rect(hwnd);
503 MoveWindow(_hwndListView, 0, 0, rect.right, rect.bottom, TRUE);
504
505 // subclass background window
506 new BackgroundWindow(_hwndListView);
507
508 _icon_algo = 1; // default icon arrangement
509
510 PositionIcons();
511 InitDragDrop();
512 }
513
514 bool DesktopShellView::InitDragDrop()
515 {
516 CONTEXT("DesktopShellView::InitDragDrop()");
517
518 _pDropTarget = new DesktopDropTarget(_hwnd);
519
520 if (!_pDropTarget)
521 return false;
522
523 _pDropTarget->AddRef();
524
525 if (FAILED(RegisterDragDrop(_hwnd, _pDropTarget))) {
526 _pDropTarget->Release();
527 _pDropTarget = NULL;
528 return false;
529 }
530 else
531 _pDropTarget->Release();
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
542 return true;
543 }
544
545 LRESULT DesktopShellView::WndProc(UINT nmsg, WPARAM wparam, LPARAM lparam)
546 {
547 switch(nmsg) {
548 case WM_CONTEXTMENU:
549 if (!DoContextMenu(GET_X_LPARAM(lparam), GET_Y_LPARAM(lparam)))
550 DoDesktopContextMenu(GET_X_LPARAM(lparam), GET_Y_LPARAM(lparam));
551 break;
552
553 case PM_SET_ICON_ALGORITHM:
554 _icon_algo = wparam;
555 PositionIcons();
556 break;
557
558 case PM_GET_ICON_ALGORITHM:
559 return _icon_algo;
560
561 case PM_DISPLAY_VERSION:
562 return SendMessage(_hwndListView, nmsg, wparam, lparam);
563
564 default:
565 return super::WndProc(nmsg, wparam, lparam);
566 }
567
568 return 0;
569 }
570
571 int DesktopShellView::Command(int id, int code)
572 {
573 return super::Command(id, code);
574 }
575
576 int DesktopShellView::Notify(int id, NMHDR* pnmh)
577 {
578 return super::Notify(id, pnmh);
579 }
580
581 bool DesktopShellView::DoContextMenu(int x, int y)
582 {
583 IDataObject* selection;
584
585 HRESULT hr = _pShellView->GetItemObject(SVGIO_SELECTION, IID_IDataObject, (void**)&selection);
586 if (FAILED(hr))
587 return false;
588
589 PIDList pidList;
590
591 hr = pidList.GetData(selection);
592 if (FAILED(hr)) {
593 selection->Release();
594 //CHECKERROR(hr);
595 return false;
596 }
597
598 LPIDA pida = pidList;
599 if (!pida->cidl) {
600 selection->Release();
601 return false;
602 }
603
604 LPCITEMIDLIST parent_pidl = (LPCITEMIDLIST) ((LPBYTE)pida+pida->aoffset[0]);
605
606 LPCITEMIDLIST* apidl = (LPCITEMIDLIST*) alloca(pida->cidl*sizeof(LPCITEMIDLIST));
607
608 for(int i=pida->cidl; i>0; --i)
609 apidl[i-1] = (LPCITEMIDLIST) ((LPBYTE)pida+pida->aoffset[i]);
610
611 hr = ShellFolderContextMenu(ShellFolder(parent_pidl), _hwnd, pida->cidl, apidl, x, y);
612
613 selection->Release();
614
615 CHECKERROR(hr);
616
617 return true;
618 }
619
620 HRESULT DesktopShellView::DoDesktopContextMenu(int x, int y)
621 {
622 IContextMenu* pcm;
623
624 HRESULT hr = DesktopFolder()->GetUIObjectOf(_hwnd, 0, NULL, IID_IContextMenu, NULL, (LPVOID*)&pcm);
625
626 if (SUCCEEDED(hr)) {
627 HMENU hmenu = CreatePopupMenu();
628
629 if (hmenu) {
630 hr = pcm->QueryContextMenu(hmenu, 0, FCIDM_SHVIEWFIRST, FCIDM_SHVIEWLAST-1, CMF_NORMAL|CMF_EXPLORE);
631
632 if (SUCCEEDED(hr)) {
633 AppendMenu(hmenu, MF_SEPARATOR, 0, NULL);
634 AppendMenu(hmenu, 0, FCIDM_SHVIEWLAST-1, ResString(IDS_ABOUT_EXPLORER));
635
636 UINT idCmd = TrackPopupMenu(hmenu, TPM_LEFTALIGN|TPM_RETURNCMD|TPM_RIGHTBUTTON, x, y, 0, _hwnd, NULL);
637
638 if (idCmd == FCIDM_SHVIEWLAST-1) {
639 explorer_about(_hwnd);
640 } else if (idCmd) {
641 CMINVOKECOMMANDINFO cmi;
642
643 cmi.cbSize = sizeof(CMINVOKECOMMANDINFO);
644 cmi.fMask = 0;
645 cmi.hwnd = _hwnd;
646 cmi.lpVerb = (LPCSTR)(INT_PTR)(idCmd - FCIDM_SHVIEWFIRST);
647 cmi.lpParameters = NULL;
648 cmi.lpDirectory = NULL;
649 cmi.nShow = SW_SHOWNORMAL;
650 cmi.dwHotKey = 0;
651 cmi.hIcon = 0;
652
653 hr = pcm->InvokeCommand(&cmi);
654 }
655 }
656 }
657
658 pcm->Release();
659 }
660
661 return hr;
662 }
663
664
665 #define ARRANGE_BORDER_DOWN 8
666 #define ARRANGE_BORDER_HV 9
667 #define ARRANGE_ROUNDABOUT 10
668
669 static const POINTS s_align_start[] = {
670 {0, 0}, // left/top
671 {0, 0},
672 {1, 0}, // right/top
673 {1, 0},
674 {0, 1}, // left/bottom
675 {0, 1},
676 {1, 1}, // right/bottom
677 {1, 1},
678
679 {0, 0}, // left/top
680 {0, 0},
681 {0, 0}
682 };
683
684 static const POINTS s_align_dir1[] = {
685 { 0, +1}, // down
686 {+1, 0}, // right
687 {-1, 0}, // left
688 { 0, +1}, // down
689 { 0, -1}, // up
690 {+1, 0}, // right
691 {-1, 0}, // left
692 { 0, -1}, // up
693
694 { 0, +1}, // down
695 {+1, 0}, // right
696 {+1, 0} // right
697 };
698
699 static const POINTS s_align_dir2[] = {
700 {+1, 0}, // right
701 { 0, +1}, // down
702 { 0, +1}, // down
703 {-1, 0}, // left
704 {+1, 0}, // right
705 { 0, -1}, // up
706 { 0, -1}, // up
707 {-1, 0}, // left
708
709 {+1, 0}, // right
710 { 0, +1}, // down
711 { 0, +1} // down
712 };
713
714 typedef pair<int,int> IconPos;
715 typedef map<IconPos, int> IconMap;
716
717 void DesktopShellView::PositionIcons(int dir)
718 {
719 DWORD spacing = ListView_GetItemSpacing(_hwndListView, FALSE);
720
721 RECT work_area;
722 SystemParametersInfo(SPI_GETWORKAREA, 0, &work_area, 0);
723
724 const POINTS& dir1 = s_align_dir1[_icon_algo];
725 const POINTS& dir2 = s_align_dir2[_icon_algo];
726 const POINTS& start_pos = s_align_start[_icon_algo];
727
728 int dir_x1 = dir1.x;
729 int dir_y1 = dir1.y;
730 int dir_x2 = dir2.x;
731 int dir_y2 = dir2.y;
732
733 int cx = LOWORD(spacing);
734 int cy = HIWORD(spacing);
735
736 int dx1 = dir_x1 * cx;
737 int dy1 = dir_y1 * cy;
738 int dx2 = dir_x2 * cx;
739 int dy2 = dir_y2 * cy;
740
741 int start_x = (start_pos.x * work_area.right)/cx*cx + (cx-32)/2;
742 int start_y = (start_pos.y * work_area.bottom)/cy*cy + 4/*(cy-32)/2*/;
743
744 if (start_x >= work_area.right)
745 start_x -= cx;
746
747 if (start_y >= work_area.bottom)
748 start_y -= cy;
749
750 int x = start_x;
751 int y = start_y;
752
753 int all = ListView_GetItemCount(_hwndListView);
754 int i1, i2;
755
756 if (dir > 0) {
757 i1 = 0;
758 i2 = all;
759 } else {
760 i1 = all-1;
761 i2 = -1;
762 }
763
764 IconMap pos_idx;
765 int cnt = 0;
766
767 for(int idx=i1; idx!=i2; idx+=dir) {
768 pos_idx[IconPos(y, x)] = idx;
769
770 if (_icon_algo == ARRANGE_BORDER_DOWN) {
771 if (++cnt & 1)
772 x = work_area.right - x;
773 else {
774 y += dy1;
775
776 if (y >= work_area.bottom) {
777 y = start_y;
778 x += dx2;
779 }
780 }
781
782 continue;
783 }
784 else if (_icon_algo == ARRANGE_BORDER_HV) {
785 if (++cnt & 1)
786 x = work_area.right - x;
787 else if (cnt & 2) {
788 y += dy1;
789
790 if (y >= work_area.bottom) {
791 y = start_y;
792 x += dx2;
793 }
794 } else {
795 x += dx1;
796
797 if (x >= work_area.right) {
798 x = start_x;
799 y += dy2;
800 }
801 }
802
803 continue;
804 }
805 else if (_icon_algo == ARRANGE_ROUNDABOUT) {
806
807 ///@todo
808
809 }
810
811 x += dx1;
812 y += dy1;
813
814 if (x<0 || x>=work_area.right) {
815 x = start_x;
816 y += dy2;
817 } else if (y<0 || y>=work_area.bottom) {
818 y = start_y;
819 x += dx2;
820 }
821 }
822
823 // use a little trick to get the icons where we want them to be...
824
825 for(IconMap::const_iterator it=pos_idx.end(); --it!=pos_idx.begin(); ) {
826 const IconPos& pos = it->first;
827
828 ListView_SetItemPosition32(_hwndListView, it->second, pos.second, pos.first);
829 }
830
831 for(IconMap::const_iterator it=pos_idx.begin(); it!=pos_idx.end(); ++it) {
832 const IconPos& pos = it->first;
833
834 ListView_SetItemPosition32(_hwndListView, it->second, pos.second, pos.first);
835 }
836 }