icon alignment algorithms
[reactos.git] / reactos / subsys / system / explorer / desktop / desktop.cpp
1 /*
2 * Copyright 2003 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 "../utility/utility.h"
30 #include "../utility/shellclasses.h"
31 #include "../utility/shellbrowserimpl.h"
32 #include "../utility/dragdropimpl.h"
33 #include "../utility/window.h"
34
35 #include "../globals.h"
36 #include "../externals.h"
37 #include "../explorer_intres.h"
38
39 #include "desktop.h"
40 #include "../taskbar/desktopbar.h"
41 #include "../shell/mainframe.h" // for MainFrame::Create()
42
43
44 static BOOL (WINAPI*SetShellWindow)(HWND);
45 static BOOL (WINAPI*SetShellWindowEx)(HWND, HWND);
46
47
48 BOOL IsAnyDesktopRunning()
49 {
50 HINSTANCE hUser32 = GetModuleHandle(TEXT("user32"));
51
52 SetShellWindow = (BOOL(WINAPI*)(HWND)) GetProcAddress(hUser32, "SetShellWindow");
53 SetShellWindowEx = (BOOL(WINAPI*)(HWND,HWND)) GetProcAddress(hUser32, "SetShellWindowEx");
54
55 return GetShellWindow() != 0;
56 }
57
58
59 static void draw_desktop_background(HWND hwnd, HDC hdc)
60 {
61 ClientRect rect(hwnd);
62
63 PaintDesktop(hdc);
64 /*
65 HBRUSH bkgndBrush = CreateSolidBrush(RGB(0,32,160)); // dark blue
66 FillRect(hdc, &rect, bkgndBrush);
67 DeleteBrush(bkgndBrush);
68 */
69
70 rect.left = rect.right - 280;
71 rect.top = rect.bottom - 56 - DESKTOPBARBAR_HEIGHT;
72 rect.right = rect.left + 250;
73 rect.bottom = rect.top + 40;
74
75 #include "../buildno.h"
76 static const LPCTSTR BkgndText = TEXT("ReactOS ")TEXT(KERNEL_VERSION_STR)TEXT(" Explorer\nby Martin Fuchs");
77
78 BkMode bkMode(hdc, TRANSPARENT);
79
80 TextColor textColor(hdc, RGB(128,128,192));
81 DrawText(hdc, BkgndText, -1, &rect, DT_RIGHT);
82
83 SetTextColor(hdc, RGB(255,255,255));
84 --rect.right;
85 ++rect.top;
86 DrawText(hdc, BkgndText, -1, &rect, DT_RIGHT);
87 }
88
89
90 LRESULT BackgroundWindow::WndProc(UINT nmsg, WPARAM wparam, LPARAM lparam)
91 {
92 switch(nmsg) {
93 case WM_ERASEBKGND:
94 PaintDesktop((HDC)wparam);
95 return TRUE;
96
97 case WM_MBUTTONDBLCLK:
98 explorer_show_frame(_hwnd, SW_SHOWNORMAL);
99 break;
100
101 default:
102 return super::WndProc(nmsg, wparam, lparam);
103 }
104
105 return 0;
106 }
107
108
109 DesktopWindow::DesktopWindow(HWND hwnd)
110 : super(hwnd)
111 {
112 _pShellView = NULL;
113 }
114
115 DesktopWindow::~DesktopWindow()
116 {
117 if (_pShellView)
118 _pShellView->Release();
119 }
120
121
122 HWND DesktopWindow::Create()
123 {
124 IconWindowClass wcDesktop(TEXT("Progman"), IDI_REACTOS, CS_DBLCLKS);
125 wcDesktop.hbrBackground = (HBRUSH)(COLOR_BACKGROUND+1);
126
127 int width = GetSystemMetrics(SM_CXSCREEN);
128 int height = GetSystemMetrics(SM_CYSCREEN);
129
130 HWND hwndDesktop = Window::Create(WINDOW_CREATOR(DesktopWindow),
131 WS_EX_TOOLWINDOW, wcDesktop, TEXT("Program Manager"), WS_POPUP|WS_VISIBLE|WS_CLIPCHILDREN,
132 0, 0, width, height, 0);
133
134 // work around to display desktop bar in Wine
135 ShowWindow(GET_WINDOW(DesktopWindow, hwndDesktop)->_desktopBar, SW_SHOW);
136
137 // work around for Windows NT, Win 98, ...
138 // Without this the desktop has mysteriously only a size of 800x600 pixels.
139 MoveWindow(hwndDesktop, 0, 0, width, height, TRUE);
140
141 return hwndDesktop;
142 }
143
144
145 LRESULT DesktopWindow::Init(LPCREATESTRUCT pcs)
146 {
147 if (super::Init(pcs))
148 return 1;
149
150 HRESULT hr = Desktop()->CreateViewObject(_hwnd, IID_IShellView, (void**)&_pShellView);
151 /* also possible:
152 SFV_CREATE sfv_create;
153
154 sfv_create.cbSize = sizeof(SFV_CREATE);
155 sfv_create.pshf = Desktop();
156 sfv_create.psvOuter = NULL;
157 sfv_create.psfvcb = NULL;
158
159 HRESULT hr = SHCreateShellFolderView(&sfv_create, &_pShellView);
160 */
161 HWND hWndView = 0;
162
163 if (SUCCEEDED(hr)) {
164 FOLDERSETTINGS fs;
165
166 fs.ViewMode = FVM_ICON;
167 fs.fFlags = FWF_DESKTOP|FWF_NOCLIENTEDGE|FWF_NOSCROLL|FWF_BESTFITWINDOW|FWF_SNAPTOGRID; //|FWF_AUTOARRANGE;
168
169 ClientRect rect(_hwnd);
170
171 hr = _pShellView->CreateViewWindow(NULL, &fs, this, &rect, &hWndView);
172
173 ///@todo use IShellBrowser::GetViewStateStream() to restore previous view state -> see SHOpenRegStream()
174
175 if (SUCCEEDED(hr)) {
176 g_Globals._hwndShellView = hWndView;
177
178 // subclass shellview window
179 new DesktopShellView(hWndView, _pShellView);
180
181 _pShellView->UIActivate(SVUIA_ACTIVATE_FOCUS);
182
183 /*
184 IShellView2* pShellView2;
185
186 hr = _pShellView->QueryInterface(IID_IShellView2, (void**)&pShellView2);
187
188 SV2CVW2_PARAMS params;
189 params.cbSize = sizeof(SV2CVW2_PARAMS);
190 params.psvPrev = _pShellView;
191 params.pfs = &fs;
192 params.psbOwner = this;
193 params.prcView = ▭
194 params.pvid = params.pvid;//@@
195
196 hr = pShellView2->CreateViewWindow2(&params);
197 params.pvid;
198 */
199
200 /*
201 IFolderView* pFolderView;
202
203 hr = _pShellView->QueryInterface(IID_IFolderView, (void**)&pFolderView);
204
205 if (SUCCEEDED(hr)) {
206 hr = pFolderView->GetAutoArrange();
207 hr = pFolderView->SetCurrentViewMode(FVM_DETAILS);
208 }
209 */
210 }
211 }
212
213 if (hWndView && SetShellWindowEx)
214 SetShellWindowEx(_hwnd, hWndView);
215 else if (SetShellWindow)
216 SetShellWindow(_hwnd);
217
218 // create the explorer bar
219 _desktopBar = DesktopBar::Create();
220 g_Globals._hwndDesktopBar = _desktopBar;
221
222 return 0;
223 }
224
225
226 LRESULT DesktopWindow::WndProc(UINT nmsg, WPARAM wparam, LPARAM lparam)
227 {
228 switch(nmsg) {
229 case WM_PAINT:
230 draw_desktop_background(_hwnd, PaintCanvas(_hwnd));
231 break;
232
233 case WM_LBUTTONDBLCLK:
234 case WM_RBUTTONDBLCLK:
235 case WM_MBUTTONDBLCLK:
236 explorer_show_frame(_hwnd, SW_SHOWNORMAL);
237 break;
238
239 case WM_GETISHELLBROWSER:
240 return (LRESULT)static_cast<IShellBrowser*>(this);
241
242 case WM_DESTROY:
243
244 ///@todo use IShellBrowser::GetViewStateStream() and _pShellView->SaveViewState() to store view state
245
246 if (SetShellWindow)
247 SetShellWindow(0);
248 break;
249
250 case WM_CLOSE:
251 ShowExitWindowsDialog(_hwnd);
252 break;
253
254 case WM_SYSCOMMAND:
255 if (wparam == SC_TASKLIST) {
256 if (_desktopBar)
257 SendMessage(_desktopBar, nmsg, wparam, lparam);
258 }
259 goto def;
260
261 default: def:
262 return super::WndProc(nmsg, wparam, lparam);
263 }
264
265 return 0;
266 }
267
268
269 HRESULT DesktopWindow::OnDefaultCommand(LPIDA pida)
270 {
271 if (MainFrame::OpenShellFolders(pida, 0))
272 return S_OK;
273
274 return E_NOTIMPL;
275 }
276
277
278 DesktopShellView::DesktopShellView(HWND hwnd, IShellView* pShellView)
279 : super(hwnd),
280 _pShellView(pShellView)
281 {
282 _hwndListView = ::GetNextWindow(hwnd, GW_CHILD);
283
284 SetWindowStyle(_hwndListView, GetWindowStyle(_hwndListView)&~LVS_ALIGNMASK);//|LVS_ALIGNTOP|LVS_AUTOARRANGE);
285
286 // work around for Windows NT, Win 98, ...
287 // Without this the desktop has mysteriously only a size of 800x600 pixels.
288 ClientRect rect(hwnd);
289 MoveWindow(_hwndListView, 0, 0, rect.right, rect.bottom, TRUE);
290
291 // subclass background window
292 new BackgroundWindow(_hwndListView);
293
294 _alignment = 0;
295
296 PositionIcons(_alignment);
297 InitDragDrop();
298 }
299
300 bool DesktopShellView::InitDragDrop()
301 {
302 CONTEXT("DesktopShellView::InitDragDrop()");
303
304 _pDropTarget = new DesktopDropTarget(_hwnd);
305
306 if (!_pDropTarget)
307 return false;
308
309 _pDropTarget->AddRef();
310
311 if (FAILED(RegisterDragDrop(_hwnd, _pDropTarget))) {
312 _pDropTarget->Release();
313 _pDropTarget = NULL;
314 return false;
315 }
316 else
317 _pDropTarget->Release();
318
319 FORMATETC ftetc;
320
321 ftetc.dwAspect = DVASPECT_CONTENT;
322 ftetc.lindex = -1;
323 ftetc.tymed = TYMED_HGLOBAL;
324 ftetc.cfFormat = CF_HDROP;
325
326 _pDropTarget->AddSuportedFormat(ftetc);
327
328 return true;
329 }
330
331 LRESULT DesktopShellView::WndProc(UINT nmsg, WPARAM wparam, LPARAM lparam)
332 {
333 switch(nmsg) {
334 case WM_CONTEXTMENU:
335 if (!DoContextMenu(GET_X_LPARAM(lparam), GET_Y_LPARAM(lparam)))
336 DoDesktopContextMenu(GET_X_LPARAM(lparam), GET_Y_LPARAM(lparam));
337 break;
338
339 case PM_POSITION_ICONS:
340 PositionIcons(wparam);
341 _alignment = wparam;
342 break;
343
344 default:
345 return super::WndProc(nmsg, wparam, lparam);
346 }
347
348 return 0;
349 }
350
351 int DesktopShellView::Command(int id, int code)
352 {
353 return super::Command(id, code);
354 }
355
356 int DesktopShellView::Notify(int id, NMHDR* pnmh)
357 {
358 return super::Notify(id, pnmh);
359 }
360
361 bool DesktopShellView::DoContextMenu(int x, int y)
362 {
363 IDataObject* selection;
364
365 HRESULT hr = _pShellView->GetItemObject(SVGIO_SELECTION, IID_IDataObject, (void**)&selection);
366 if (FAILED(hr))
367 return false;
368
369 PIDList pidList;
370
371 hr = pidList.GetData(selection);
372 if (FAILED(hr)) {
373 selection->Release();
374 //CHECKERROR(hr);
375 return false;
376 }
377
378 LPIDA pida = pidList;
379 if (!pida->cidl) {
380 selection->Release();
381 return false;
382 }
383
384 LPCITEMIDLIST parent_pidl = (LPCITEMIDLIST) ((LPBYTE)pida+pida->aoffset[0]);
385
386 LPCITEMIDLIST* apidl = (LPCITEMIDLIST*) alloca(pida->cidl*sizeof(LPCITEMIDLIST));
387
388 for(int i=pida->cidl; i>0; --i)
389 apidl[i-1] = (LPCITEMIDLIST) ((LPBYTE)pida+pida->aoffset[i]);
390
391 hr = ShellFolderContextMenu(ShellFolder(parent_pidl), _hwnd, pida->cidl, apidl, x, y);
392
393 selection->Release();
394
395 CHECKERROR(hr);
396
397 return true;
398 }
399
400 HRESULT DesktopShellView::DoDesktopContextMenu(int x, int y)
401 {
402 IContextMenu* pcm;
403
404 HRESULT hr = DesktopFolder()->GetUIObjectOf(_hwnd, 0, NULL, IID_IContextMenu, NULL, (LPVOID*)&pcm);
405
406 if (SUCCEEDED(hr)) {
407 HMENU hmenu = CreatePopupMenu();
408
409 if (hmenu) {
410 hr = pcm->QueryContextMenu(hmenu, 0, FCIDM_SHVIEWFIRST, FCIDM_SHVIEWLAST-1, CMF_NORMAL|CMF_EXPLORE);
411
412 if (SUCCEEDED(hr)) {
413 AppendMenu(hmenu, MF_SEPARATOR, 0, NULL);
414 AppendMenu(hmenu, 0, FCIDM_SHVIEWLAST-1, ResString(IDS_ABOUT_EXPLORER));
415
416 UINT idCmd = TrackPopupMenu(hmenu, TPM_LEFTALIGN|TPM_RETURNCMD|TPM_RIGHTBUTTON, x, y, 0, _hwnd, NULL);
417
418 if (idCmd == FCIDM_SHVIEWLAST-1) {
419 explorer_about(_hwnd);
420 } else if (idCmd) {
421 CMINVOKECOMMANDINFO cmi;
422
423 cmi.cbSize = sizeof(CMINVOKECOMMANDINFO);
424 cmi.fMask = 0;
425 cmi.hwnd = _hwnd;
426 cmi.lpVerb = (LPCSTR)(INT_PTR)(idCmd - FCIDM_SHVIEWFIRST);
427 cmi.lpParameters = NULL;
428 cmi.lpDirectory = NULL;
429 cmi.nShow = SW_SHOWNORMAL;
430 cmi.dwHotKey = 0;
431 cmi.hIcon = 0;
432
433 hr = pcm->InvokeCommand(&cmi);
434 }
435 }
436 }
437
438 pcm->Release();
439 }
440
441 return hr;
442 }
443
444
445 static const POINTS s_align_start[8] = {
446 {0, 0}, // left/top
447 {0, 0},
448 {1, 0}, // right/top
449 {1, 0},
450 {0, 1}, // left/bottom
451 {0, 1},
452 {1, 1}, // right/bottom
453 {1, 1}
454 };
455
456 static const POINTS s_align_dir1[8] = {
457 { 0, +1}, // down
458 {+1, 0}, // right
459 {-1, 0}, // left
460 { 0, +1}, // down
461 { 0, -1}, // up
462 {+1, 0}, // right
463 {-1, 0}, // left
464 { 0, -1} // up
465 };
466
467 static const POINTS s_align_dir2[8] = {
468 {+1, 0}, // right
469 { 0, +1}, // down
470 { 0, +1}, // down
471 {-1, 0}, // left
472 {+1, 0}, // right
473 { 0, -1}, // up
474 { 0, -1}, // up
475 {-1, 0} // left
476 };
477
478 typedef pair<int,int> IconPos;
479 typedef map<IconPos, int> IconMap;
480
481 void DesktopShellView::PositionIcons(int alignment, int dir)
482 {
483 DWORD spacing = ListView_GetItemSpacing(_hwndListView, FALSE);
484
485 RECT work_area;
486 SystemParametersInfo(SPI_GETWORKAREA, 0, &work_area, 0);
487
488 const POINTS& dir1 = s_align_dir1[alignment];
489 const POINTS& dir2 = s_align_dir2[alignment];
490 const POINTS& start_pos = s_align_start[alignment];
491
492 int dir_x1 = dir1.x;
493 int dir_y1 = dir1.y;
494 int dir_x2 = dir2.x;
495 int dir_y2 = dir2.y;
496
497 int cx = LOWORD(spacing);
498 int cy = HIWORD(spacing);
499
500 int dx1 = dir_x1 * cx;
501 int dy1 = dir_y1 * cy;
502 int dx2 = dir_x2 * cx;
503 int dy2 = dir_y2 * cy;
504
505 int start_x = start_pos.x * work_area.right + (cx-32)/2;
506 int start_y = start_pos.y * work_area.bottom + 4/*(cy-32)/2*/;
507
508 if (start_x >= work_area.right)
509 start_x -= cx;
510
511 if (start_y >= work_area.bottom)
512 start_y -= cy;
513
514 int x = start_x;
515 int y = start_y;
516
517 int cnt = ListView_GetItemCount(_hwndListView);
518 int i1, i2;
519
520 if (dir > 0) {
521 i1 = 0;
522 i2 = cnt;
523 } else {
524 i1 = cnt-1;
525 i2 = -1;
526 }
527
528 IconMap pos_idx;
529
530 for(int idx=i1; idx!=i2; idx+=dir) {
531 pos_idx[IconPos(y, x)] = idx;
532
533 x += dx1;
534 y += dy1;
535
536 if (x<0 || x>=work_area.right) {
537 x = start_x;
538 y += dy2;
539 } else if (y<0 || y>=work_area.bottom) {
540 y = start_y;
541 x += dx2;
542 }
543 }
544
545 for(IconMap::const_iterator it=pos_idx.end(); --it!=pos_idx.begin(); ) {
546 const IconPos& pos = it->first;
547
548 ListView_SetItemPosition32(_hwndListView, it->second, pos.second, pos.first);
549 }
550
551 for(IconMap::const_iterator it=pos_idx.begin(); it!=pos_idx.end(); ++it) {
552 const IconPos& pos = it->first;
553
554 ListView_SetItemPosition32(_hwndListView, it->second, pos.second, pos.first);
555 }
556 }