1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
 * Copyright 2003, 2004, 2005 Martin Fuchs
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */


 //
 // Explorer clone
 //
 // quicklaunch.cpp
 //
 // Martin Fuchs, 22.08.2003
 //


#include <precomp.h>

#include "quicklaunch.h"


QuickLaunchEntry::QuickLaunchEntry()<--- Member variable 'QuickLaunchEntry::_entry' is not initialized in the constructor.
{
	_hbmp = 0;
}

QuickLaunchMap::~QuickLaunchMap()
{
	while(!empty()) {
		iterator it = begin();
		DeleteBitmap(it->second._hbmp);
		erase(it);
	}
}


QuickLaunchBar::QuickLaunchBar(HWND hwnd)
 :	super(hwnd)
{
	CONTEXT("QuickLaunchBar::QuickLaunchBar()");

	_dir = NULL;
	_next_id = IDC_FIRST_QUICK_ID;
	_btn_dist = 20;
	_size = 0;

	HWND hwndToolTip = (HWND) SendMessage(hwnd, TB_GETTOOLTIPS, 0, 0);

	SetWindowStyle(hwndToolTip, GetWindowStyle(hwndToolTip)|TTS_ALWAYSTIP);

	 // delay refresh to some time later
	PostMessage(hwnd, PM_REFRESH, 0, 0);
}

QuickLaunchBar::~QuickLaunchBar()
{
	delete _dir;
}

HWND QuickLaunchBar::Create(HWND hwndParent)
{
	CONTEXT("QuickLaunchBar::Create()");

	ClientRect clnt(hwndParent);

	HWND hwnd = CreateToolbarEx(hwndParent,
								WS_CHILD|WS_VISIBLE|WS_CLIPSIBLINGS|WS_CLIPCHILDREN|
								CCS_TOP|CCS_NODIVIDER|CCS_NOPARENTALIGN|CCS_NORESIZE|
								TBSTYLE_TOOLTIPS|TBSTYLE_WRAPABLE|TBSTYLE_FLAT,
								IDW_QUICKLAUNCHBAR, 0, 0, 0, NULL, 0, 0, 0, 16, 16, sizeof(TBBUTTON));

	if (hwnd)
		new QuickLaunchBar(hwnd);

	return hwnd;
}

void QuickLaunchBar::AddShortcuts()
{
	CONTEXT("QuickLaunchBar::AddShortcuts()");

	WaitCursor wait;

	try {
		TCHAR path[MAX_PATH];

		SpecialFolderFSPath app_data(CSIDL_APPDATA, _hwnd);	///@todo perhaps also look into CSIDL_COMMON_APPDATA ?

		_stprintf(path, TEXT("%s\\")QUICKLAUNCH_FOLDER, (LPCTSTR)app_data);

		RecursiveCreateDirectory(path);
		_dir = new ShellDirectory(GetDesktopFolder(), path, _hwnd);

		_dir->smart_scan(SORT_NAME);

		 // immediatelly extract the shortcut icons
		for(Entry*entry=_dir->_down; entry; entry=entry->_next)
			entry->_icon_id = entry->safe_extract_icon(ICF_NORMAL);
	} catch(COMException&) {
		return;
	}


	ShellFolder desktop_folder;
	WindowCanvas canvas(_hwnd);

	COLORREF bk_color = GetSysColor(COLOR_BTNFACE);
	HBRUSH bk_brush = GetSysColorBrush(COLOR_BTNFACE);

	AddButton(ID_MINIMIZE_ALL, g_Globals._icon_cache.get_icon(ICID_MINIMIZE).create_bitmap(bk_color, bk_brush, canvas), ResString(IDS_MINIMIZE_ALL), NULL);
	AddButton(ID_EXPLORE, g_Globals._icon_cache.get_icon(ICID_EXPLORER).create_bitmap(bk_color, bk_brush, canvas), ResString(IDS_TITLE), NULL);

	TBBUTTON sep = {0, -1, TBSTATE_ENABLED, BTNS_SEP, {0, 0}, 0, 0};
	SendMessage(_hwnd, TB_INSERTBUTTON, INT_MAX, (LPARAM)&sep);

	int cur_desktop = g_Globals._desktops._current_desktop;
	ResString desktop_fmt(IDS_DESKTOP_NUM);

	HDC hdc = CreateCompatibleDC(canvas);
	DWORD size = SendMessage(_hwnd, TB_GETBUTTONSIZE, 0, 0);
	int cx = LOWORD(size);
	int cy = HIWORD(size);
	RECT rect = {0, 0, cx, cy};
	RECT textRect = {0, 0, cx-7, cy-7};

	for(int i=0; i<DESKTOP_COUNT; ++i) {
		HBITMAP hbmp = CreateCompatibleBitmap(canvas, cx, cy);
		HBITMAP hbmp_old = SelectBitmap(hdc, hbmp);

		FontSelection font(hdc, GetStockFont(ANSI_VAR_FONT));
		FmtString num_txt(TEXT("%d"), i+1);
		TextColor color(hdc, RGB(64,64,64));
		BkMode mode(hdc, TRANSPARENT);
		FillRect(hdc, &rect, GetSysColorBrush(COLOR_BTNFACE));
		DrawText(hdc, num_txt, num_txt.length(), &textRect, DT_CENTER|DT_VCENTER|DT_SINGLELINE);

		SelectBitmap(hdc, hbmp_old);

		AddButton(ID_SWITCH_DESKTOP_1+i, hbmp, FmtString(desktop_fmt, i+1), NULL, cur_desktop==i?TBSTATE_ENABLED|TBSTATE_PRESSED:TBSTATE_ENABLED);
	}
	DeleteDC(hdc);

	for(Entry*entry=_dir->_down; entry; entry=entry->_next) {
		 // hide files like "desktop.ini"
		if (entry->_data.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
			continue;

		 // hide subfolders
		if (!(entry->_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
			HBITMAP hbmp = g_Globals._icon_cache.get_icon(entry->_icon_id).create_bitmap(bk_color, bk_brush, canvas);

			AddButton(_next_id++, hbmp, entry->_display_name, entry);	//entry->_etype==ET_SHELL? desktop_folder.get_name(static_cast<ShellEntry*>(entry)->_pidl): entry->_display_name);
		}
	}

	_btn_dist = LOWORD(SendMessage(_hwnd, TB_GETBUTTONSIZE, 0, 0));
	_size = _entries.size() * _btn_dist;

	SendMessage(GetParent(_hwnd), PM_RESIZE_CHILDREN, 0, 0);
}

void QuickLaunchBar::AddButton(int id, HBITMAP hbmp, LPCTSTR name, Entry* entry, int flags)
{
	TBADDBITMAP ab = {0, (UINT_PTR)hbmp};
	int bmp_idx = SendMessage(_hwnd, TB_ADDBITMAP, 1, (LPARAM)&ab);

	QuickLaunchEntry qle;

	qle._hbmp = hbmp;
	qle._title = name;
	qle._entry = entry;

	_entries[id] = qle;

	TBBUTTON btn = {0, 0, (BYTE)flags, BTNS_BUTTON|BTNS_NOPREFIX, {0, 0}, 0, 0};

	btn.idCommand = id;
	btn.iBitmap = bmp_idx;

	SendMessage(_hwnd, TB_INSERTBUTTON, INT_MAX, (LPARAM)&btn);
}

void QuickLaunchBar::UpdateDesktopButtons(int desktop_idx)
{
	for(int i=0; i<DESKTOP_COUNT; ++i) {
		TBBUTTONINFO tbi = {sizeof(TBBUTTONINFO), TBIF_STATE, 0, 0, (BYTE)(desktop_idx==i? TBSTATE_ENABLED|TBSTATE_PRESSED: TBSTATE_ENABLED)};

		SendMessage(_hwnd, TB_SETBUTTONINFO, ID_SWITCH_DESKTOP_1+i, (LPARAM)&tbi);
	}
}

LRESULT QuickLaunchBar::WndProc(UINT nmsg, WPARAM wparam, LPARAM lparam)
{
	switch(nmsg) {
	  case PM_REFRESH:
		AddShortcuts();
		break;

	  case PM_GET_WIDTH: {
		 // take line wrapping into account
		int btns = SendMessage(_hwnd, TB_BUTTONCOUNT, 0, 0);
		int rows = SendMessage(_hwnd, TB_GETROWS, 0, 0);

		if (rows<2 || rows==btns)
			return _size;

		RECT rect;
		int max_cx = 0;

		for(QuickLaunchMap::const_iterator it=_entries.begin(); it!=_entries.end(); ++it) {
			SendMessage(_hwnd, TB_GETRECT, it->first, (LPARAM)&rect);

			if (rect.right > max_cx)
				max_cx = rect.right;
		}

		return max_cx;}

	  case PM_UPDATE_DESKTOP:
		UpdateDesktopButtons(wparam);
		break;

	  case WM_CONTEXTMENU: {
		TBBUTTON btn;
		QuickLaunchMap::iterator it;
		Point screen_pt(lparam), clnt_pt=screen_pt;
		ScreenToClient(_hwnd, &clnt_pt);

		Entry* entry = NULL;
		int idx = SendMessage(_hwnd, TB_HITTEST, 0, (LPARAM)&clnt_pt);

		if (idx>=0 &&
			SendMessage(_hwnd, TB_GETBUTTON, idx, (LPARAM)&btn)!=-1 &&
			(it=_entries.find(btn.idCommand))!=_entries.end()) {
			entry = it->second._entry;
		}

		if (entry) {	// entry is NULL for desktop switch buttons
			HRESULT hr = entry->do_context_menu(_hwnd, screen_pt, _cm_ifs);

			if (!SUCCEEDED(hr))
				CHECKERROR(hr);
		} else
			goto def;
		break;}

	  default: def:
		return super::WndProc(nmsg, wparam, lparam);
	}

	return 0;
}

int QuickLaunchBar::Command(int id, int code)
{
	CONTEXT("QuickLaunchBar::Command()");

	if ((id&~0xFF) == IDC_FIRST_QUICK_ID) {
		QuickLaunchEntry& qle = _entries[id];

		if (qle._entry) {
			qle._entry->launch_entry(_hwnd);
			return 0;
		}
	}

	return 0; // Don't return 1 to avoid recursion with DesktopBar::Command()
}

int QuickLaunchBar::Notify(int id, NMHDR* pnmh)
{
	switch(pnmh->code) {
	  case TTN_GETDISPINFO: {
		NMTTDISPINFO* ttdi = (NMTTDISPINFO*) pnmh;

		int id = ttdi->hdr.idFrom;
		ttdi->lpszText = _entries[id]._title.str();
#ifdef TTF_DI_SETITEM
		ttdi->uFlags |= TTF_DI_SETITEM;
#endif

		 // enable multiline tooltips (break at CR/LF and for very long one-line strings)
		SendMessage(pnmh->hwndFrom, TTM_SETMAXTIPWIDTH, 0, 400);

		break;}

		return super::Notify(id, pnmh);
	}

	return 0;
}