The real, definitive, Visual C++ support branch. Accept no substitutes
[reactos.git] / base / shell / explorer / taskbar / desktopbar.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 // desktopbar.cpp
24 //
25 // Martin Fuchs, 22.08.2003
26 //
27
28
29 #include <precomp.h>
30
31 #include "../resource.h"
32
33 #include "desktopbar.h"
34 #include "taskbar.h"
35 #include "startmenu.h"
36 #include "traynotify.h"
37 #include "quicklaunch.h"
38
39 #include "../dialogs/settings.h"
40
41
42 DesktopBar::DesktopBar(HWND hwnd)
43 : super(hwnd),
44 #ifdef __REACTOS__
45 _trayIcon(hwnd, ID_TRAY_VOLUME)
46 #else
47 WM_TASKBARCREATED(RegisterWindowMessage(WINMSG_TASKBARCREATED))
48 #endif
49 {
50 SetWindowIcon(hwnd, IDI_REACTOS);
51
52 SystemParametersInfo(SPI_GETWORKAREA, 0, &_work_area_org, 0);
53 }
54
55 DesktopBar::~DesktopBar()
56 {
57 // restore work area to the previous size
58 SystemParametersInfo(SPI_SETWORKAREA, 0, &_work_area_org, 0);
59 PostMessage(HWND_BROADCAST, WM_SETTINGCHANGE, SPI_SETWORKAREA, 0);
60
61 // exit application after destroying desktop window
62 PostQuitMessage(0);
63 }
64
65
66 HWND DesktopBar::Create()
67 {
68 static BtnWindowClass wcDesktopBar(CLASSNAME_EXPLORERBAR);
69
70 RECT rect;
71
72 rect.left = -2; // hide left border
73 #ifdef TASKBAR_AT_TOP
74 rect.top = -2; // hide top border
75 #else
76 rect.top = GetSystemMetrics(SM_CYSCREEN) - DESKTOPBARBAR_HEIGHT;
77 #endif
78 rect.right = GetSystemMetrics(SM_CXSCREEN) + 2;
79 rect.bottom = rect.top + DESKTOPBARBAR_HEIGHT + 2;
80
81 return Window::Create(WINDOW_CREATOR(DesktopBar), WS_EX_PALETTEWINDOW,
82 wcDesktopBar, TITLE_EXPLORERBAR,
83 WS_POPUP|WS_THICKFRAME|WS_CLIPCHILDREN|WS_VISIBLE,
84 rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top, 0);
85 }
86
87
88 LRESULT DesktopBar::Init(LPCREATESTRUCT pcs)
89 {
90 if (super::Init(pcs))
91 return 1;
92
93 // create start button
94 ResString start_str(IDS_START);
95 WindowCanvas canvas(_hwnd);
96 FontSelection font(canvas, GetStockFont(ANSI_VAR_FONT));
97 RECT rect = {0, 0};
98 DrawText(canvas, start_str, -1, &rect, DT_SINGLELINE|DT_CALCRECT);
99 int start_btn_width = rect.right+16+8;
100
101 _taskbar_pos = start_btn_width + 6;
102
103 // create "Start" button
104 HWND hwndStart = Button(_hwnd, start_str, 1, 1, start_btn_width, REBARBAND_HEIGHT, IDC_START, WS_VISIBLE|WS_CHILD|BS_OWNERDRAW);
105 SetWindowFont(hwndStart, GetStockFont(ANSI_VAR_FONT), FALSE);
106 new StartButton(hwndStart);
107
108 /* Save the handle to the window, needed for push-state handling */
109 _hwndStartButton = hwndStart;
110
111 // disable double clicks
112 SetClassLong(hwndStart, GCL_STYLE, GetClassLong(hwndStart, GCL_STYLE) & ~CS_DBLCLKS);
113
114 // create task bar
115 _hwndTaskBar = TaskBar::Create(_hwnd);
116
117 #ifndef __MINGW32__ // SHRestricted() missing in MinGW (as of 29.10.2003)
118 if (!g_Globals._SHRestricted || !SHRestricted(REST_NOTRAYITEMSDISPLAY))
119 #endif
120 // create tray notification area
121 _hwndNotify = NotifyArea::Create(_hwnd);
122
123
124 // notify all top level windows about the successfully created desktop bar
125 //@@ Use SendMessage() instead of PostMessage() to avoid problems with delayed created shell service objects?
126 PostMessage(HWND_BROADCAST, WM_TASKBARCREATED, 0, 0);
127
128
129 _hwndQuickLaunch = QuickLaunchBar::Create(_hwnd);
130
131 // create rebar window to manage task and quick launch bar
132 #ifndef _NO_REBAR
133 _hwndrebar = CreateWindowEx(WS_EX_TOOLWINDOW, REBARCLASSNAME, NULL,
134 WS_CHILD|WS_VISIBLE|WS_CLIPSIBLINGS|WS_CLIPCHILDREN|
135 RBS_VARHEIGHT|RBS_AUTOSIZE|RBS_DBLCLKTOGGLE| //|RBS_REGISTERDROP
136 CCS_NODIVIDER|CCS_NOPARENTALIGN|CCS_TOP,
137 0, 0, 0, 0, _hwnd, 0, g_Globals._hInstance, 0);
138
139 REBARBANDINFO rbBand;
140
141 rbBand.cbSize = sizeof(REBARBANDINFO);
142 rbBand.fMask = RBBIM_TEXT|RBBIM_STYLE|RBBIM_CHILD|RBBIM_CHILDSIZE|RBBIM_SIZE|RBBIM_ID|RBBIM_IDEALSIZE;
143 #ifndef RBBS_HIDETITLE // missing in MinGW headers as of 25.02.2004
144 #define RBBS_HIDETITLE 0x400
145 #endif
146 rbBand.cyChild = REBARBAND_HEIGHT;
147 rbBand.cyMaxChild = (ULONG)-1;
148 rbBand.cyMinChild = REBARBAND_HEIGHT;
149 rbBand.cyIntegral = REBARBAND_HEIGHT + 3; //@@ OK?
150 rbBand.cxMinChild = rbBand.cyIntegral * 3;
151 rbBand.fStyle = RBBS_VARIABLEHEIGHT|RBBS_GRIPPERALWAYS|RBBS_HIDETITLE;
152
153 TCHAR QuickLaunchBand[] = _T("Quicklaunch");
154 rbBand.lpText = QuickLaunchBand;
155 rbBand.hwndChild = _hwndQuickLaunch;
156 rbBand.cx = 120;
157 rbBand.wID = IDW_QUICKLAUNCHBAR;
158 SendMessage(_hwndrebar, RB_INSERTBAND, (WPARAM)-1, (LPARAM)&rbBand);
159
160 TCHAR TaskbarBand[] = _T("Taskbar");
161 rbBand.lpText = TaskbarBand;
162 rbBand.hwndChild = _hwndTaskBar;
163 rbBand.cx = 200; //pcs->cx-_taskbar_pos-quicklaunch_width-(notifyarea_width+1);
164 rbBand.wID = IDW_TASKTOOLBAR;
165 SendMessage(_hwndrebar, RB_INSERTBAND, (WPARAM)-1, (LPARAM)&rbBand);
166 #endif
167
168
169 RegisterHotkeys();
170
171 // prepare Startmenu, but hide it for now
172 _startMenuRoot = GET_WINDOW(StartMenuRoot, StartMenuRoot::Create(_hwndStartButton, STARTMENUROOT_ICON_SIZE));
173 _startMenuRoot->_hwndParent = _hwndStartButton;
174
175 return 0;
176 }
177
178
179 StartButton::StartButton(HWND hwnd)
180 : PictureButton(hwnd, SmallIcon(IDI_STARTMENU), GetSysColorBrush(COLOR_BTNFACE))
181 {
182 }
183
184 LRESULT StartButton::WndProc(UINT nmsg, WPARAM wparam, LPARAM lparam)
185 {
186 switch(nmsg) {
187 // one click activation: handle button-down message, don't wait for button-up
188 case WM_LBUTTONDOWN:
189 if (!Button_GetState(_hwnd)) {
190 Button_SetState(_hwnd, TRUE);
191
192 SetCapture(_hwnd);
193
194 SendMessage(GetParent(_hwnd), WM_COMMAND, MAKEWPARAM(GetDlgCtrlID(_hwnd),0), 0);
195 }
196
197 Button_SetState(_hwnd, FALSE);
198 break;
199
200 // re-target mouse move messages while moving the mouse cursor through the start menu
201 case WM_MOUSEMOVE:
202 if (GetCapture() == _hwnd) {
203 POINT pt = {GET_X_LPARAM(lparam), GET_Y_LPARAM(lparam)};
204
205 ClientToScreen(_hwnd, &pt);
206 HWND hwnd = WindowFromPoint(pt);
207
208 if (hwnd && hwnd!=_hwnd) {
209 ScreenToClient(hwnd, &pt);
210 SendMessage(hwnd, WM_MOUSEMOVE, 0, MAKELPARAM(pt.x, pt.y));
211 }
212 }
213 break;
214
215 case WM_LBUTTONUP:
216 if (GetCapture() == _hwnd) {
217 ReleaseCapture();
218
219 POINT pt = {GET_X_LPARAM(lparam), GET_Y_LPARAM(lparam)};
220
221 ClientToScreen(_hwnd, &pt);
222 HWND hwnd = WindowFromPoint(pt);
223
224 if (hwnd && hwnd!=_hwnd) {
225 ScreenToClient(hwnd, &pt);
226 PostMessage(hwnd, WM_LBUTTONDOWN, 0, MAKELPARAM(pt.x, pt.y));
227 PostMessage(hwnd, WM_LBUTTONUP, 0, MAKELPARAM(pt.x, pt.y));
228 }
229 }
230 break;
231
232 case WM_CANCELMODE:
233 ReleaseCapture();
234 break;
235
236 default:
237 return super::WndProc(nmsg, wparam, lparam);
238 }
239
240 return 0;
241 }
242
243
244 void DesktopBar::RegisterHotkeys()
245 {
246 // register hotkey WIN+E opening explorer
247 RegisterHotKey(_hwnd, 0, MOD_WIN, 'E');
248
249 ///@todo register all common hotkeys
250 }
251
252 void DesktopBar::ProcessHotKey(int id_hotkey)
253 {
254 switch(id_hotkey) {
255 case 0: explorer_show_frame(SW_SHOWNORMAL);
256 break;
257
258 ///@todo implement all common hotkeys
259 }
260 }
261
262
263 LRESULT DesktopBar::WndProc(UINT nmsg, WPARAM wparam, LPARAM lparam)
264 {
265 switch(nmsg) {
266 case WM_NCHITTEST: {
267 LRESULT res = super::WndProc(nmsg, wparam, lparam);
268
269 if (res>=HTSIZEFIRST && res<=HTSIZELAST) {
270 #ifdef TASKBAR_AT_TOP
271 if (res == HTBOTTOM) // enable vertical resizing at the lower border
272 #else
273 if (res == HTTOP) // enable vertical resizing at the upper border
274 #endif
275 return res;
276 else
277 return HTCLIENT; // disable any other resizing
278 }
279 return res;}
280
281 case WM_SYSCOMMAND:
282 if ((wparam&0xFFF0) == SC_SIZE) {
283 #ifdef TASKBAR_AT_TOP
284 if (wparam == SC_SIZE+6)// enable vertical resizing at the lower border
285 #else
286 if (wparam == SC_SIZE+3)// enable vertical resizing at the upper border
287 #endif
288 goto def;
289 else
290 return 0; // disable any other resizing
291 } else if (wparam == SC_TASKLIST)
292 ShowStartMenu();
293 goto def;
294
295 case WM_SIZE:
296 Resize(LOWORD(lparam), HIWORD(lparam));
297 break;
298
299 case WM_SIZING:
300 ControlResize(wparam, lparam);
301 break;
302
303 case PM_RESIZE_CHILDREN: {
304 ClientRect size(_hwnd);
305 Resize(size.right, size.bottom);
306 break;}
307
308 case WM_CLOSE:
309 ShowExitWindowsDialog(_hwnd);
310 break;
311
312 case WM_HOTKEY:
313 ProcessHotKey(wparam);
314 break;
315
316 case WM_COPYDATA:
317 return ProcessCopyData((COPYDATASTRUCT*)lparam);
318
319 case WM_CONTEXTMENU: {
320 POINTS p;
321 p.x = LOWORD(lparam);
322 p.y = HIWORD(lparam);
323 PopupMenu menu(IDM_DESKTOPBAR);
324 SetMenuDefaultItem(menu, 0, MF_BYPOSITION);
325 menu.TrackPopupMenu(_hwnd, p);
326 break;}
327
328 case PM_GET_LAST_ACTIVE:
329 if (_hwndTaskBar)
330 return SendMessage(_hwndTaskBar, nmsg, wparam, lparam);
331 break;
332
333 case PM_REFRESH_CONFIG: ///@todo read desktop bar settings
334 SendMessage(_hwndNotify, PM_REFRESH_CONFIG, 0, 0);
335 break;
336
337 case WM_TIMER:
338 if (wparam == ID_TRAY_VOLUME) {
339 KillTimer(_hwnd, wparam);
340 launch_file(_hwnd, TEXT("sndvol32.exe"), SW_SHOWNORMAL, TEXT("-t")); // launch volume control in small mode
341 }
342 break;
343
344 case PM_GET_NOTIFYAREA:
345 return (LRESULT)(HWND)_hwndNotify;
346
347 default: def:
348 return super::WndProc(nmsg, wparam, lparam);
349 }
350
351 return 0;
352 }
353
354 int DesktopBar::Notify(int id, NMHDR* pnmh)
355 {
356 if (pnmh->code == RBN_CHILDSIZE) {
357 /* align the task bands to the top, so it's in row with the Start button */
358 NMREBARCHILDSIZE* childSize = (NMREBARCHILDSIZE*)pnmh;
359
360 if (childSize->wID == IDW_TASKTOOLBAR) {
361 int cy = childSize->rcChild.top - childSize->rcBand.top;
362
363 if (cy) {
364 childSize->rcChild.bottom -= cy;
365 childSize->rcChild.top -= cy;
366 }
367 }
368 }
369
370 return 0;
371 }
372
373 void DesktopBar::Resize(int cx, int cy)
374 {
375 ///@todo general children resizing algorithm
376 int quicklaunch_width = SendMessage(_hwndQuickLaunch, PM_GET_WIDTH, 0, 0);
377 int notifyarea_width = SendMessage(_hwndNotify, PM_GET_WIDTH, 0, 0);
378
379 HDWP hdwp = BeginDeferWindowPos(3);
380
381 if (_hwndrebar)
382 DeferWindowPos(hdwp, _hwndrebar, 0, _taskbar_pos, 1, cx-_taskbar_pos-(notifyarea_width+1), cy-2, SWP_NOZORDER|SWP_NOACTIVATE);
383 else {
384 if (_hwndQuickLaunch)
385 DeferWindowPos(hdwp, _hwndQuickLaunch, 0, _taskbar_pos, 1, quicklaunch_width, cy-2, SWP_NOZORDER|SWP_NOACTIVATE);
386
387 if (_hwndTaskBar)
388 DeferWindowPos(hdwp, _hwndTaskBar, 0, _taskbar_pos+quicklaunch_width, 0, cx-_taskbar_pos-quicklaunch_width-(notifyarea_width+1), cy, SWP_NOZORDER|SWP_NOACTIVATE);
389 }
390
391 if (_hwndNotify)
392 DeferWindowPos(hdwp, _hwndNotify, 0, cx-(notifyarea_width+1), 1, notifyarea_width, cy-2, SWP_NOZORDER|SWP_NOACTIVATE);
393
394 EndDeferWindowPos(hdwp);
395
396 WindowRect rect(_hwnd);
397 RECT work_area = {0, 0, GetSystemMetrics(SM_CXSCREEN), rect.top};
398 SystemParametersInfo(SPI_SETWORKAREA, 0, &work_area, 0); // don't use SPIF_SENDCHANGE because then we have to wait for any message being delivered
399 PostMessage(HWND_BROADCAST, WM_SETTINGCHANGE, SPI_SETWORKAREA, 0);
400 }
401
402
403 int DesktopBar::Command(int id, int code)
404 {
405 switch(id) {
406 case IDC_START:
407 ShowStartMenu();
408 break;
409
410 case ID_ABOUT_EXPLORER:
411 explorer_about(g_Globals._hwndDesktop);
412 break;
413
414 case ID_DESKTOPBAR_SETTINGS:
415 ExplorerPropertySheet(g_Globals._hwndDesktop);
416 break;
417
418 case ID_MINIMIZE_ALL:
419 g_Globals._desktops.ToggleMinimize();
420 break;
421
422 case ID_EXPLORE:
423 explorer_show_frame(SW_SHOWNORMAL);
424 break;
425
426 case ID_TASKMGR:
427 launch_file(_hwnd, TEXT("taskmgr.exe"), SW_SHOWNORMAL);
428 break;
429
430 case ID_SWITCH_DESKTOP_1:
431 case ID_SWITCH_DESKTOP_1+1: {
432 int desktop_idx = id - ID_SWITCH_DESKTOP_1;
433
434 g_Globals._desktops.SwitchToDesktop(desktop_idx);
435
436 if (_hwndQuickLaunch)
437 PostMessage(_hwndQuickLaunch, PM_UPDATE_DESKTOP, desktop_idx, 0);
438 break;}
439
440 #ifdef __REACTOS__
441 case ID_TRAY_VOLUME:
442 launch_file(_hwnd, TEXT("sndvol32.exe"), SW_SHOWNORMAL); // launch volume control application
443 break;
444
445 case ID_VOLUME_PROPERTIES:
446 launch_cpanel(_hwnd, TEXT("mmsys.cpl"));
447 break;
448 #endif
449
450 default:
451 if (_hwndQuickLaunch)
452 return SendMessage(_hwndQuickLaunch, WM_COMMAND, MAKEWPARAM(id,code), 0);
453 else
454 return 1;
455 }
456
457 return 0;
458 }
459
460
461 void DesktopBar::ShowStartMenu()
462 {
463 if (_startMenuRoot)
464 {
465 // set the Button, if not set
466 if (!Button_GetState(_hwndStartButton))
467 Button_SetState(_hwndStartButton, TRUE);
468
469 _startMenuRoot->TrackStartmenu();
470
471 // StartMenu was closed, release button state
472 Button_SetState(_hwndStartButton, false);
473 }
474 }
475
476
477 /// copy data structure for tray notifications
478 struct TrayNotifyCDS {
479 DWORD cookie;
480 DWORD notify_code;
481 NOTIFYICONDATA nicon_data;
482 };
483
484 LRESULT DesktopBar::ProcessCopyData(COPYDATASTRUCT* pcd)
485 {
486 // Is this a tray notification message?
487 if (pcd->dwData == 1) {
488 TrayNotifyCDS* ptr = (TrayNotifyCDS*) pcd->lpData;
489
490 NotifyArea* notify_area = GET_WINDOW(NotifyArea, _hwndNotify);
491
492 if (notify_area)
493 return notify_area->ProcessTrayNotification(ptr->notify_code, &ptr->nicon_data);
494 }
495
496 return FALSE;
497 }
498
499
500 void DesktopBar::ControlResize(WPARAM wparam, LPARAM lparam)
501 {
502 PRECT dragRect = (PRECT) lparam;
503 //int screenWidth = GetSystemMetrics(SM_CXSCREEN);
504 int screenHeight = GetSystemMetrics(SM_CYSCREEN);
505
506 ///@todo write code for taskbar being at sides or top.
507
508 switch(wparam) {
509 case WMSZ_BOTTOM: ///@todo Taskbar is at the top of the screen
510 break;
511
512 case WMSZ_TOP: // Taskbar is at the bottom of the screen
513 dragRect->top = screenHeight - (((screenHeight - dragRect->top) + DESKTOPBARBAR_HEIGHT/2) / DESKTOPBARBAR_HEIGHT) * DESKTOPBARBAR_HEIGHT;
514 if (dragRect->top < screenHeight / 2)
515 dragRect->top = screenHeight - (screenHeight/2 / DESKTOPBARBAR_HEIGHT * DESKTOPBARBAR_HEIGHT);
516 else if (dragRect->top > screenHeight - 5)
517 dragRect->top = screenHeight - 5;
518 break;
519
520 case WMSZ_RIGHT: ///@todo Taskbar is at the left of the screen
521 break;
522
523 case WMSZ_LEFT: ///@todo Taskbar is at the right of the screen
524 break;
525 }
526 }
527
528
529 #ifdef __REACTOS__
530
531 void DesktopBar::AddTrayIcons()
532 {
533 _trayIcon.Add(SmallIcon(IDI_SPEAKER), ResString(IDS_VOLUME));
534 }
535
536 void DesktopBar::TrayClick(UINT id, int btn)
537 {
538 switch(id) {
539 case ID_TRAY_VOLUME:
540 if (btn == TRAYBUTTON_LEFT)
541 SetTimer(_hwnd, ID_TRAY_VOLUME, 500, NULL); // wait a bit to correctly handle double clicks
542 else {
543 PopupMenu menu(IDM_VOLUME);
544 SetMenuDefaultItem(menu, 0, MF_BYPOSITION);
545 menu.TrackPopupMenuAtPos(_hwnd, GetMessagePos());
546 }
547 break;
548 }
549 }
550
551 void DesktopBar::TrayDblClick(UINT id, int btn)
552 {
553 switch(id) {
554 case ID_TRAY_VOLUME:
555 KillTimer(_hwnd, ID_TRAY_VOLUME); // finish one-click timer
556 launch_file(_hwnd, TEXT("sndvol32.exe"), SW_SHOWNORMAL); // launch volume control application
557 break;
558 }
559 }
560
561 #endif