merge ROS Shell without integrated explorer part into trunk
[reactos.git] / reactos / subsys / system / explorer / taskbar / quicklaunch.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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19
20 //
21 // Explorer clone
22 //
23 // quicklaunch.cpp
24 //
25 // Martin Fuchs, 22.08.2003
26 //
27
28
29 #include "precomp.h"
30
31 #include "../explorer_intres.h"
32
33 #include "quicklaunch.h"
34
35
36 QuickLaunchEntry::QuickLaunchEntry()
37 {
38 _hbmp = 0;
39 }
40
41 QuickLaunchMap::~QuickLaunchMap()
42 {
43 while(!empty()) {
44 iterator it = begin();
45 DeleteBitmap(it->second._hbmp);
46 erase(it);
47 }
48 }
49
50
51 QuickLaunchBar::QuickLaunchBar(HWND hwnd)
52 : super(hwnd)
53 {
54 CONTEXT("QuickLaunchBar::QuickLaunchBar()");
55
56 _dir = NULL;
57 _next_id = IDC_FIRST_QUICK_ID;
58 _btn_dist = 20;
59 _size = 0;
60
61 HWND hwndToolTip = (HWND) SendMessage(hwnd, TB_GETTOOLTIPS, 0, 0);
62
63 SetWindowStyle(hwndToolTip, GetWindowStyle(hwndToolTip)|TTS_ALWAYSTIP);
64
65 // delay refresh to some time later
66 PostMessage(hwnd, PM_REFRESH, 0, 0);
67 }
68
69 QuickLaunchBar::~QuickLaunchBar()
70 {
71 delete _dir;
72 }
73
74 HWND QuickLaunchBar::Create(HWND hwndParent)
75 {
76 CONTEXT("QuickLaunchBar::Create()");
77
78 ClientRect clnt(hwndParent);
79
80 HWND hwnd = CreateToolbarEx(hwndParent,
81 WS_CHILD|WS_VISIBLE|WS_CLIPSIBLINGS|WS_CLIPCHILDREN|
82 CCS_TOP|CCS_NODIVIDER|CCS_NOPARENTALIGN|CCS_NORESIZE|
83 TBSTYLE_TOOLTIPS|TBSTYLE_WRAPABLE|TBSTYLE_FLAT,
84 IDW_QUICKLAUNCHBAR, 0, 0, 0, NULL, 0, 0, 0, 16, 16, sizeof(TBBUTTON));
85
86 if (hwnd)
87 new QuickLaunchBar(hwnd);
88
89 return hwnd;
90 }
91
92 void QuickLaunchBar::AddShortcuts()
93 {
94 CONTEXT("QuickLaunchBar::AddShortcuts()");
95
96 WaitCursor wait;
97
98 try {
99 TCHAR path[MAX_PATH];
100
101 SpecialFolderFSPath app_data(CSIDL_APPDATA, _hwnd); ///@todo perhaps also look into CSIDL_COMMON_APPDATA ?
102
103 _stprintf(path, TEXT("%s\\")QUICKLAUNCH_FOLDER, (LPCTSTR)app_data);
104
105 RecursiveCreateDirectory(path);
106 _dir = new ShellDirectory(GetDesktopFolder(), path, _hwnd);
107
108 _dir->smart_scan(SORT_NAME, SCAN_EXTRACT_ICONS|SCAN_FILESYSTEM);
109 } catch(COMException&) {
110 return;
111 }
112
113
114 ShellFolder desktop_folder;
115 WindowCanvas canvas(_hwnd);
116
117 COLORREF bk_color = GetSysColor(COLOR_BTNFACE);
118 HBRUSH bk_brush = GetSysColorBrush(COLOR_BTNFACE);
119
120 AddButton(ID_MINIMIZE_ALL, g_Globals._icon_cache.get_icon(ICID_LOGOFF/*@@*/).create_bitmap(bk_color, bk_brush, canvas), ResString(IDS_MINIMIZE_ALL), NULL);
121 AddButton(ID_EXPLORE, g_Globals._icon_cache.get_icon(ICID_EXPLORER).create_bitmap(bk_color, bk_brush, canvas), ResString(IDS_TITLE), NULL);
122
123 TBBUTTON sep = {0, -1, TBSTATE_ENABLED, BTNS_SEP, {0, 0}, 0, 0};
124 SendMessage(_hwnd, TB_INSERTBUTTON, INT_MAX, (LPARAM)&sep);
125
126 int cur_desktop = g_Globals._desktops._current_desktop;
127 ResString desktop_fmt(IDS_DESKTOP_NUM);
128
129 HDC hdc = CreateCompatibleDC(canvas);
130 DWORD size = SendMessage(_hwnd, TB_GETBUTTONSIZE, 0, 0);
131 int cx = LOWORD(size);
132 int cy = HIWORD(size);
133 RECT rect = {0, 0, cx, cy};
134 RECT textRect = {0, 0, cx-7, cy-7};
135
136 for(int i=0; i<DESKTOP_COUNT; ++i) {
137 HBITMAP hbmp = CreateCompatibleBitmap(canvas, cx, cy);
138 HBITMAP hbmp_old = SelectBitmap(hdc, hbmp);
139
140 FmtString num_txt(TEXT("%d"), i+1);
141 TextColor color(hdc, RGB(64,64,64));
142 BkMode mode(hdc, TRANSPARENT);
143 FillRect(hdc, &rect, GetSysColorBrush(COLOR_BTNFACE));
144 DrawText(hdc, num_txt, num_txt.length(), &textRect, DT_CENTER|DT_VCENTER|DT_SINGLELINE);
145
146 SelectBitmap(hdc, hbmp_old);
147
148 AddButton(ID_SWITCH_DESKTOP_1+i, hbmp, FmtString(desktop_fmt, i+1), NULL, cur_desktop==i?TBSTATE_ENABLED|TBSTATE_CHECKED:TBSTATE_ENABLED);
149 }
150 DeleteDC(hdc);
151
152 SendMessage(_hwnd, TB_INSERTBUTTON, INT_MAX, (LPARAM)&sep);
153
154 for(Entry*entry=_dir->_down; entry; entry=entry->_next) {
155 // hide files like "desktop.ini"
156 if (entry->_data.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
157 continue;
158
159 // hide subfolders
160 if (!(entry->_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
161 HBITMAP hbmp = g_Globals._icon_cache.get_icon(entry->_icon_id).create_bitmap(bk_color, bk_brush, canvas);
162
163 AddButton(_next_id++, hbmp, entry->_display_name, entry); //entry->_etype==ET_SHELL? desktop_folder.get_name(static_cast<ShellEntry*>(entry)->_pidl): entry->_display_name);
164 }
165 }
166
167 _btn_dist = LOWORD(SendMessage(_hwnd, TB_GETBUTTONSIZE, 0, 0));
168 _size = _entries.size() * _btn_dist;
169
170 SendMessage(GetParent(_hwnd), PM_RESIZE_CHILDREN, 0, 0);
171 }
172
173 void QuickLaunchBar::AddButton(int id, HBITMAP hbmp, LPCTSTR name, Entry* entry, int flags)
174 {
175 TBADDBITMAP ab = {0, (UINT_PTR)hbmp};
176 int bmp_idx = SendMessage(_hwnd, TB_ADDBITMAP, 1, (LPARAM)&ab);
177
178 QuickLaunchEntry qle;
179
180 qle._hbmp = hbmp;
181 qle._title = name;
182 qle._entry = entry;
183
184 _entries[id] = qle;
185
186 TBBUTTON btn = {0, 0, flags, BTNS_BUTTON|BTNS_NOPREFIX, {0, 0}, 0, 0};
187
188 btn.idCommand = id;
189 btn.iBitmap = bmp_idx;
190
191 SendMessage(_hwnd, TB_INSERTBUTTON, INT_MAX, (LPARAM)&btn);
192 }
193
194 void QuickLaunchBar::UpdateDesktopButtons(int desktop_idx)
195 {
196 for(int i=0; i<DESKTOP_COUNT; ++i) {
197 TBBUTTONINFO tbi = {sizeof(TBBUTTONINFO), TBIF_STATE, 0, 0, desktop_idx==i? TBSTATE_ENABLED|TBSTATE_CHECKED: TBSTATE_ENABLED};
198
199 SendMessage(_hwnd, TB_SETBUTTONINFO, ID_SWITCH_DESKTOP_1+i, (LPARAM)&tbi);
200 }
201 }
202
203 LRESULT QuickLaunchBar::WndProc(UINT nmsg, WPARAM wparam, LPARAM lparam)
204 {
205 switch(nmsg) {
206 case PM_REFRESH:
207 AddShortcuts();
208 break;
209
210 case PM_GET_WIDTH: {
211 // take line wrapping into account
212 int btns = SendMessage(_hwnd, TB_BUTTONCOUNT, 0, 0);
213 int rows = SendMessage(_hwnd, TB_GETROWS, 0, 0);
214
215 if (rows<2 || rows==btns)
216 return _size;
217
218 RECT rect;
219 int max_cx = 0;
220
221 for(QuickLaunchMap::const_iterator it=_entries.begin(); it!=_entries.end(); ++it) {
222 SendMessage(_hwnd, TB_GETRECT, it->first, (LPARAM)&rect);
223
224 if (rect.right > max_cx)
225 max_cx = rect.right;
226 }
227
228 return max_cx;}
229
230 case PM_UPDATE_DESKTOP:
231 UpdateDesktopButtons(wparam);
232 break;
233
234 case WM_CONTEXTMENU: {
235 TBBUTTON btn;
236 QuickLaunchMap::iterator it;
237 Point screen_pt(lparam), clnt_pt=screen_pt;
238 ScreenToClient(_hwnd, &clnt_pt);
239
240 Entry* entry = NULL;
241 int idx = SendMessage(_hwnd, TB_HITTEST, 0, (LPARAM)&clnt_pt);
242
243 if (idx>=0 &&
244 SendMessage(_hwnd, TB_GETBUTTON, idx, (LPARAM)&btn)!=-1 &&
245 (it=_entries.find(btn.idCommand))!=_entries.end()) {
246 entry = it->second._entry;
247 }
248
249 if (entry) // entry is NULL for desktop switch buttons
250 CHECKERROR(entry->do_context_menu(_hwnd, screen_pt, _cm_ifs));
251 else
252 goto def;
253 break;}
254
255 default: def:
256 return super::WndProc(nmsg, wparam, lparam);
257 }
258
259 return 0;
260 }
261
262 int QuickLaunchBar::Command(int id, int code)
263 {
264 CONTEXT("QuickLaunchBar::Command()");
265
266 if ((id&~0xFF) == IDC_FIRST_QUICK_ID) {
267 QuickLaunchEntry& qle = _entries[id];
268
269 if (qle._entry) {
270 qle._entry->launch_entry(_hwnd);
271 return 0;
272 }
273 }
274
275 return 0; // Don't return 1 to avoid recursion with DesktopBar::Command()
276 }
277
278 int QuickLaunchBar::Notify(int id, NMHDR* pnmh)
279 {
280 switch(pnmh->code) {
281 case TTN_GETDISPINFO: {
282 NMTTDISPINFO* ttdi = (NMTTDISPINFO*) pnmh;
283
284 int id = ttdi->hdr.idFrom;
285 ttdi->lpszText = (LPTSTR)_entries[id]._title.c_str();
286 #ifdef TTF_DI_SETITEM
287 ttdi->uFlags |= TTF_DI_SETITEM;
288 #endif
289
290 // enable multiline tooltips (break at CR/LF and for very long one-line strings)
291 SendMessage(pnmh->hwndFrom, TTM_SETMAXTIPWIDTH, 0, 400);
292
293 break;}
294
295 return super::Notify(id, pnmh);
296 }
297
298 return 0;
299 }