improved exception handling in start menu, especially for non-NT systems
[reactos.git] / reactos / subsys / system / explorer / taskbar / quicklaunch.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 // quicklaunch.cpp
24 //
25 // Martin Fuchs, 22.08.2003
26 //
27
28
29 #include "../utility/utility.h"
30
31 #include "../explorer.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 _dir = NULL;
55
56 _next_id = IDC_FIRST_QUICK_ID;
57
58 HWND hwndToolTip = (HWND) SendMessage(hwnd, TB_GETTOOLTIPS, 0, 0);
59
60 SetWindowStyle(hwndToolTip, GetWindowStyle(hwndToolTip)|TTS_ALWAYSTIP);
61
62 // delay refresh to some tome later
63 PostMessage(hwnd, PM_REFRESH, 0, 0);
64 }
65
66 QuickLaunchBar::~QuickLaunchBar()
67 {
68 delete _dir;
69 }
70
71 HWND QuickLaunchBar::Create(HWND hwndParent)
72 {
73 ClientRect clnt(hwndParent);
74
75 HWND hwnd = CreateToolbarEx(hwndParent,
76 WS_CHILD|WS_VISIBLE|CCS_NODIVIDER|CCS_NORESIZE|
77 TBSTYLE_TOOLTIPS|TBSTYLE_WRAPABLE|TBSTYLE_FLAT,
78 IDW_QUICKLAUNCHBAR, 0, 0, 0, NULL, 0, 0, 0, 16, 16, sizeof(TBBUTTON));
79
80 if (hwnd)
81 new QuickLaunchBar(hwnd);
82
83 return hwnd;
84 }
85
86 void QuickLaunchBar::AddShortcuts()
87 {
88 WaitCursor wait;
89
90 try {
91 TCHAR path[_MAX_PATH];
92
93 SpecialFolderFSPath app_data(CSIDL_APPDATA, _hwnd);
94
95 _stprintf(path, _T("%s\\")QUICKLAUNCH_FOLDER, (LPCTSTR)app_data);
96
97 _dir = new ShellDirectory(Desktop(), path, _hwnd);
98
99 _dir->smart_scan();
100 } catch(COMException&) {
101 return;
102 }
103
104
105 ShellFolder desktop_folder;
106 WindowCanvas canvas(_hwnd);
107
108 TBBUTTON btn = {0, 0, TBSTATE_ENABLED, BTNS_BUTTON|BTNS_NOPREFIX, {0, 0}, 0, 0};
109
110 for(Entry*entry=_dir->_down; entry; entry=entry->_next) {
111 // hide files like "desktop.ini"
112 if (entry->_data.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
113 continue;
114
115 // hide subfolders
116 if (!(entry->_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
117 ShellEntry* shell_entry = static_cast<ShellEntry*>(entry);
118
119 const String& entry_name = desktop_folder.get_name(shell_entry->_pidl);
120 HBITMAP hbmp = create_bitmap_from_icon(shell_entry->_hIcon, GetSysColorBrush(COLOR_BTNFACE), canvas);
121
122 TBADDBITMAP ab = {0, (UINT_PTR)hbmp};
123 int bmp_idx = SendMessage(_hwnd, TB_ADDBITMAP, 1, (LPARAM)&ab);
124
125 QuickLaunchEntry qle;
126
127 int id = ++_next_id;
128
129 qle._hbmp = hbmp;
130 qle._title = entry_name;
131 qle._entry = shell_entry;
132
133 _entries[id] = qle;
134
135 btn.idCommand = id;
136 btn.iBitmap = bmp_idx;
137 int idx = SendMessage(_hwnd, TB_BUTTONCOUNT, 0, 0);
138
139 SendMessage(_hwnd, TB_INSERTBUTTON, idx, (LPARAM)&btn);
140 }
141 }
142 }
143
144 LRESULT QuickLaunchBar::WndProc(UINT nmsg, WPARAM wparam, LPARAM lparam)
145 {
146 switch(nmsg) {
147 case PM_REFRESH:
148 AddShortcuts();
149 break;
150
151 default:
152 return super::WndProc(nmsg, wparam, lparam);
153 }
154
155 return 0;
156 }
157
158 int QuickLaunchBar::Command(int id, int code)
159 {
160 _entries[id]._entry->launch_entry(_hwnd);
161
162 return 0;
163 }
164
165 int QuickLaunchBar::Notify(int id, NMHDR* pnmh)
166 {
167 switch(pnmh->code) {
168 case TTN_GETDISPINFO: {
169 NMTTDISPINFO* ttdi = (NMTTDISPINFO*) pnmh;
170
171 int id = ttdi->hdr.idFrom;
172 ttdi->lpszText = (LPTSTR)_entries[id]._title.c_str();
173 #ifdef TTF_DI_SETITEM
174 ttdi->uFlags |= TTF_DI_SETITEM;
175 #endif
176 break;}
177
178 return super::Notify(id, pnmh);
179 }
180
181 return 0;
182 }