Sync with trunk head
[reactos.git] / base / shell / explorer / explorer.cpp
1 /*
2 * Copyright 2003, 2004, 2005, 2006 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19
20 //
21 // Explorer clone
22 //
23 // explorer.cpp
24 //
25 // Martin Fuchs, 23.07.2003
26 //
27 // Credits: Thanks to Leon Finker for his explorer cabinet window example
28 //
29
30
31 #include <precomp.h> // <precomp.h> instead of "precomp.h" because the ROS build system needs this to find the precompiled header file (*.gch) in the output directory tree
32
33 #include "resource.h"
34
35 #include <locale.h> // for setlocale()
36
37 #ifndef __WINE__
38 #include <io.h> // for dup2()
39 #include <fcntl.h> // for _O_RDONLY
40 #endif
41
42 #include "dialogs/settings.h" // for MdiSdiDlg
43
44 #include "services/shellservices.h"
45
46
47 extern "C" int initialize_gdb_stub(); // start up GDB stub
48
49
50 DynamicLoadLibFct<void(__stdcall*)(BOOL)> g_SHDOCVW_ShellDDEInit(TEXT("SHDOCVW"), 118);
51
52
53 ExplorerGlobals g_Globals;
54
55
56 ExplorerGlobals::ExplorerGlobals()
57 {
58 _hInstance = 0;
59 _cfStrFName = 0;
60
61 #ifndef ROSSHELL
62 _hframeClass = 0;
63 _hMainWnd = 0;
64 _desktop_mode = false;
65 _prescan_nodes = false;
66 #endif
67
68 _log = NULL;
69 #ifndef __MINGW32__ // SHRestricted() missing in MinGW (as of 29.10.2003)
70 _SHRestricted = 0;
71 #endif
72 _hwndDesktopBar = 0;
73 _hwndShellView = 0;
74 _hwndDesktop = 0;
75 }
76
77
78 void ExplorerGlobals::init(HINSTANCE hInstance)
79 {
80 _hInstance = hInstance;
81
82 #ifndef __MINGW32__ // SHRestricted() missing in MinGW (as of 29.10.2003)
83 _SHRestricted = (DWORD(STDAPICALLTYPE*)(RESTRICTIONS)) GetProcAddress(GetModuleHandle(TEXT("SHELL32")), "SHRestricted");
84 #endif
85
86 _icon_cache.init();
87 }
88
89
90 void ExplorerGlobals::read_persistent()
91 {
92 // read configuration file
93 _cfg_dir.printf(TEXT("%s\\ReactOS"), (LPCTSTR)SpecialFolderFSPath(CSIDL_APPDATA,0));
94 _cfg_path.printf(TEXT("%s\\ros-explorer-cfg.xml"), _cfg_dir.c_str());
95
96 if (!_cfg.read_file(_cfg_path)) {
97 if (!_cfg._errors.empty())
98 MessageBox(_hwndDesktop, _cfg._errors.str(),
99 TEXT("ROS Explorer - reading user settings"), MB_OK);
100
101 _cfg.read_file(TEXT("explorer-cfg-template.xml"));
102 }
103
104 // read bookmarks
105 _favorites_path.printf(TEXT("%s\\ros-explorer-bookmarks.xml"), _cfg_dir.c_str());
106
107 if (!_favorites.read(_favorites_path)) {
108 _favorites.import_IE_favorites(0);
109 _favorites.write(_favorites_path);
110 }
111 }
112
113 void ExplorerGlobals::write_persistent()
114 {
115 // write configuration file
116 RecursiveCreateDirectory(_cfg_dir);
117
118 _cfg.write_file(_cfg_path);
119 _favorites.write(_favorites_path);
120 }
121
122
123 XMLPos ExplorerGlobals::get_cfg()
124 {
125 XMLPos cfg_pos(&_cfg);
126
127 cfg_pos.smart_create("explorer-cfg");
128
129 return cfg_pos;
130 }
131
132 XMLPos ExplorerGlobals::get_cfg(const char* path)
133 {
134 XMLPos cfg_pos(&_cfg);
135
136 cfg_pos.smart_create("explorer-cfg");
137 cfg_pos.create_relative(path);
138
139 return cfg_pos;
140 }
141
142
143 void _log_(LPCTSTR txt)
144 {
145 FmtString msg(TEXT("%s\n"), txt);
146
147 if (g_Globals._log)
148 _fputts(msg, g_Globals._log);
149
150 OutputDebugString(msg);
151 }
152
153
154 bool FileTypeManager::is_exe_file(LPCTSTR ext)
155 {
156 static const LPCTSTR s_executable_extensions[] = {
157 TEXT("COM"),
158 TEXT("EXE"),
159 TEXT("BAT"),
160 TEXT("CMD"),
161 TEXT("CMM"),
162 TEXT("BTM"),
163 TEXT("AWK"),
164 0
165 };
166
167 TCHAR ext_buffer[_MAX_EXT];
168 const LPCTSTR* p;
169 LPCTSTR s;
170 LPTSTR d;
171
172 for(s=ext+1,d=ext_buffer; (*d=toupper(*s)); s++)
173 ++d;
174
175 for(p=s_executable_extensions; *p; p++)
176 if (!lstrcmp(ext_buffer, *p))
177 return true;
178
179 return false;
180 }
181
182
183 const FileTypeInfo& FileTypeManager::operator[](String ext)
184 {
185 ext.toLower();
186
187 iterator found = find(ext);
188 if (found != end())
189 return found->second;
190
191 FileTypeInfo& ftype = super::operator[](ext);
192
193 ftype._neverShowExt = false;
194
195 HKEY hkey;
196 TCHAR value[MAX_PATH], display_name[MAX_PATH];
197 LONG valuelen = sizeof(value);
198
199 if (!RegQueryValue(HKEY_CLASSES_ROOT, ext, value, &valuelen)) {
200 ftype._classname = value;
201
202 valuelen = sizeof(display_name);
203 if (!RegQueryValue(HKEY_CLASSES_ROOT, ftype._classname, display_name, &valuelen))
204 ftype._displayname = display_name;
205
206 if (!RegOpenKey(HKEY_CLASSES_ROOT, ftype._classname, &hkey)) {
207 if (!RegQueryValueEx(hkey, TEXT("NeverShowExt"), 0, NULL, NULL, NULL))
208 ftype._neverShowExt = true;
209
210 RegCloseKey(hkey);
211 }
212 }
213
214 return ftype;
215 }
216
217 LPCTSTR FileTypeManager::set_type(Entry* entry, bool dont_hide_ext)
218 {
219 LPCTSTR ext = _tcsrchr(entry->_data.cFileName, TEXT('.'));
220
221 if (ext) {
222 const FileTypeInfo& type = (*this)[ext];
223
224 if (!type._displayname.empty())
225 entry->_type_name = _tcsdup(type._displayname);
226
227 // hide some file extensions
228 if (type._neverShowExt && !dont_hide_ext) {
229 int len = ext - entry->_data.cFileName;
230
231 if (entry->_display_name != entry->_data.cFileName)
232 free(entry->_display_name);
233
234 entry->_display_name = (LPTSTR) malloc((len+1)*sizeof(TCHAR));
235 lstrcpyn(entry->_display_name, entry->_data.cFileName, len + 1);
236 }
237
238 if (is_exe_file(ext))
239 entry->_data.dwFileAttributes |= ATTRIBUTE_EXECUTABLE;
240 }
241
242 return ext;
243 }
244
245
246 Icon::Icon()
247 : _id(ICID_UNKNOWN),
248 _itype(IT_STATIC),
249 _hicon(0)
250 {
251 }
252
253 Icon::Icon(ICON_ID id, UINT nid) //, int cx, int cy
254 : _id(id),
255 _itype(IT_STATIC),
256 _hicon(ResIcon(nid)) // ResIconEx(nid, cx, cy)
257 {
258 }
259
260 Icon::Icon(ICON_ID id, UINT nid, int icon_size)
261 : _id(id),
262 _itype(IT_STATIC),
263 _hicon(ResIconEx(nid, icon_size, icon_size))
264 {
265 }
266
267 Icon::Icon(ICON_TYPE itype, int id, HICON hIcon)
268 : _id((ICON_ID)id),
269 _itype(itype),
270 _hicon(hIcon)
271 {
272 }
273
274 Icon::Icon(ICON_TYPE itype, int id, int sys_idx)
275 : _id((ICON_ID)id),
276 _itype(itype),
277 _sys_idx(sys_idx)
278 {
279 }
280
281 void Icon::draw(HDC hdc, int x, int y, int cx, int cy, COLORREF bk_color, HBRUSH bk_brush) const
282 {
283 if (_itype == IT_SYSCACHE)
284 ImageList_DrawEx(g_Globals._icon_cache.get_sys_imagelist(), _sys_idx, hdc, x, y, cx, cy, bk_color, CLR_DEFAULT, ILD_NORMAL);
285 else
286 DrawIconEx(hdc, x, y, _hicon, cx, cy, 0, bk_brush, DI_NORMAL);
287 }
288
289 HBITMAP Icon::create_bitmap(COLORREF bk_color, HBRUSH hbrBkgnd, HDC hdc_wnd) const
290 {
291 if (_itype == IT_SYSCACHE) {
292 HIMAGELIST himl = g_Globals._icon_cache.get_sys_imagelist();
293
294 int cx, cy;
295 ImageList_GetIconSize(himl, &cx, &cy);
296
297 HBITMAP hbmp = CreateCompatibleBitmap(hdc_wnd, cx, cy);
298 HDC hdc = CreateCompatibleDC(hdc_wnd);
299 HBITMAP hbmp_old = SelectBitmap(hdc, hbmp);
300 ImageList_DrawEx(himl, _sys_idx, hdc, 0, 0, cx, cy, bk_color, CLR_DEFAULT, ILD_NORMAL);
301 SelectBitmap(hdc, hbmp_old);
302 DeleteDC(hdc);
303
304 return hbmp;
305 } else
306 return create_bitmap_from_icon(_hicon, hbrBkgnd, hdc_wnd);
307 }
308
309
310 int Icon::add_to_imagelist(HIMAGELIST himl, HDC hdc_wnd, COLORREF bk_color, HBRUSH bk_brush) const
311 {
312 int ret;
313
314 if (_itype == IT_SYSCACHE) {
315 HIMAGELIST himl = g_Globals._icon_cache.get_sys_imagelist();
316
317 int cx, cy;
318 ImageList_GetIconSize(himl, &cx, &cy);
319
320 HBITMAP hbmp = CreateCompatibleBitmap(hdc_wnd, cx, cy);
321 HDC hdc = CreateCompatibleDC(hdc_wnd);
322 HBITMAP hbmp_old = SelectBitmap(hdc, hbmp);
323 ImageList_DrawEx(himl, _sys_idx, hdc, 0, 0, cx, cy, bk_color, CLR_DEFAULT, ILD_NORMAL);
324 SelectBitmap(hdc, hbmp_old);
325 DeleteDC(hdc);
326
327 ret = ImageList_Add(himl, hbmp, 0);
328
329 DeleteObject(hbmp);
330 } else
331 ret = ImageList_AddAlphaIcon(himl, _hicon, bk_brush, hdc_wnd);
332
333 return ret;
334 }
335
336 HBITMAP create_bitmap_from_icon(HICON hIcon, HBRUSH hbrush_bkgnd, HDC hdc_wnd/*, int icon_size*/)
337 {
338 int cx = ICON_SIZE_SMALL;
339 int cy = ICON_SIZE_SMALL;
340 HBITMAP hbmp = CreateCompatibleBitmap(hdc_wnd, cx, cy);
341
342 MemCanvas canvas;
343 BitmapSelection sel(canvas, hbmp);
344
345 RECT rect = {0, 0, cx, cy};
346 FillRect(canvas, &rect, hbrush_bkgnd);
347
348 DrawIconEx(canvas, 0, 0, hIcon, cx, cy, 0, hbrush_bkgnd, DI_NORMAL);
349
350 return hbmp;
351 }
352
353 HBITMAP create_small_bitmap_from_icon(HICON hIcon, HBRUSH hbrush_bkgnd, HDC hdc_wnd)
354 {
355 int cx = GetSystemMetrics(SM_CXSMICON);
356 int cy = GetSystemMetrics(SM_CYSMICON);
357 HBITMAP hbmp = CreateCompatibleBitmap(hdc_wnd, cx, cy);
358
359 MemCanvas canvas;
360 BitmapSelection sel(canvas, hbmp);
361
362 RECT rect = {0, 0, cx, cy};
363 FillRect(canvas, &rect, hbrush_bkgnd);
364
365 DrawIconEx(canvas, 0, 0, hIcon, cx, cy, 0, hbrush_bkgnd, DI_NORMAL);
366
367 return hbmp;
368 }
369
370 int ImageList_AddAlphaIcon(HIMAGELIST himl, HICON hIcon, HBRUSH hbrush_bkgnd, HDC hdc_wnd)
371 {
372 HBITMAP hbmp = create_bitmap_from_icon(hIcon, hbrush_bkgnd, hdc_wnd);
373
374 int ret = ImageList_Add(himl, hbmp, 0);
375
376 DeleteObject(hbmp);
377
378 return ret;
379 }
380
381
382 int IconCache::s_next_id = ICID_DYNAMIC;
383
384
385 void IconCache::init()
386 {
387 int icon_size = STARTMENUROOT_ICON_SIZE;
388
389 _icons[ICID_NONE] = Icon(IT_STATIC, ICID_NONE, (HICON)0);
390
391 _icons[ICID_FOLDER] = Icon(ICID_FOLDER, IDI_FOLDER);
392 //_icons[ICID_DOCUMENT] = Icon(ICID_DOCUMENT, IDI_DOCUMENT);
393 _icons[ICID_EXPLORER] = Icon(ICID_EXPLORER, IDI_EXPLORER);
394 // _icons[ICID_APP] = Icon(ICID_APP, IDI_APPICON);
395
396 _icons[ICID_CONFIG] = Icon(ICID_CONFIG, IDI_CONFIG, icon_size);
397 _icons[ICID_DOCUMENTS] = Icon(ICID_DOCUMENTS, IDI_DOCUMENTS, icon_size);
398 _icons[ICID_FAVORITES] = Icon(ICID_FAVORITES, IDI_FAVORITES, icon_size);
399 _icons[ICID_INFO] = Icon(ICID_INFO, IDI_INFO, icon_size);
400 _icons[ICID_APPS] = Icon(ICID_APPS, IDI_APPS, icon_size);
401 _icons[ICID_SEARCH] = Icon(ICID_SEARCH, IDI_SEARCH, icon_size);
402 _icons[ICID_ACTION] = Icon(ICID_ACTION, IDI_ACTION, icon_size);
403 _icons[ICID_SEARCH_DOC] = Icon(ICID_SEARCH_DOC, IDI_SEARCH_DOC, icon_size);
404 _icons[ICID_PRINTER] = Icon(ICID_PRINTER, IDI_PRINTER, icon_size);
405 _icons[ICID_NETWORK] = Icon(ICID_NETWORK, IDI_NETWORK, icon_size);
406 _icons[ICID_COMPUTER] = Icon(ICID_COMPUTER, IDI_COMPUTER, icon_size);
407 _icons[ICID_LOGOFF] = Icon(ICID_LOGOFF, IDI_LOGOFF, icon_size);
408 _icons[ICID_SHUTDOWN] = Icon(ICID_SHUTDOWN, IDI_SHUTDOWN, icon_size);
409 _icons[ICID_RESTART] = Icon(ICID_RESTART, IDI_RESTART, icon_size);
410 _icons[ICID_BOOKMARK] = Icon(ICID_BOOKMARK, IDI_DOT_TRANS, icon_size);
411 _icons[ICID_MINIMIZE] = Icon(ICID_MINIMIZE, IDI_MINIMIZE, icon_size);
412 _icons[ICID_CONTROLPAN] = Icon(ICID_CONTROLPAN, IDI_CONTROLPAN, icon_size);
413 _icons[ICID_DESKSETTING]= Icon(ICID_DESKSETTING,IDI_DESKSETTING,icon_size);
414 _icons[ICID_NETCONNS] = Icon(ICID_NETCONNS, IDI_NETCONNS, icon_size);
415 _icons[ICID_ADMIN] = Icon(ICID_ADMIN, IDI_ADMIN, icon_size);
416 _icons[ICID_RECENT] = Icon(ICID_RECENT, IDI_RECENT, icon_size);
417 }
418
419
420 const Icon& IconCache::extract(LPCTSTR path, ICONCACHE_FLAGS flags)
421 {
422 // search for matching icon with unchanged flags in the cache
423 CacheKey mapkey(path, flags);
424 PathCacheMap::iterator found = _pathCache.find(mapkey);
425
426 if (found != _pathCache.end())
427 return _icons[found->second];
428
429 // search for matching icon with handle
430 CacheKey mapkey_hicon(path, flags|ICF_HICON);
431 if (flags != mapkey_hicon.second) {
432 found = _pathCache.find(mapkey_hicon);
433
434 if (found != _pathCache.end())
435 return _icons[found->second];
436 }
437
438 // search for matching icon in the system image list cache
439 CacheKey mapkey_syscache(path, flags|ICF_SYSCACHE);
440 if (flags != mapkey_syscache.second) {
441 found = _pathCache.find(mapkey_syscache);
442
443 if (found != _pathCache.end())
444 return _icons[found->second];
445 }
446
447 SHFILEINFO sfi;
448
449 int shgfi_flags = 0;
450
451 if (flags & ICF_OPEN)
452 shgfi_flags |= SHGFI_OPENICON;
453
454 if ((flags&(ICF_LARGE|ICF_MIDDLE|ICF_OVERLAYS|ICF_HICON)) && !(flags&ICF_SYSCACHE)) {
455 shgfi_flags |= SHGFI_ICON;
456
457 if (!(flags & (ICF_LARGE|ICF_MIDDLE)))
458 shgfi_flags |= SHGFI_SMALLICON;
459
460 if (flags & ICF_OVERLAYS)
461 shgfi_flags |= SHGFI_ADDOVERLAYS;
462
463 // get small/big icons with/without overlays
464 if (SHGetFileInfo(path, 0, &sfi, sizeof(sfi), shgfi_flags)) {
465 const Icon& icon = add(sfi.hIcon, IT_CACHED);
466
467 ///@todo limit cache size
468 _pathCache[mapkey_hicon] = icon;
469
470 return icon;
471 }
472 } else {
473 assert(!(flags&ICF_OVERLAYS));
474
475 shgfi_flags |= SHGFI_SYSICONINDEX|SHGFI_SMALLICON;
476
477 // use system image list - the "search program dialog" needs it
478 HIMAGELIST himlSys_small = (HIMAGELIST) SHGetFileInfo(path, 0, &sfi, sizeof(sfi), shgfi_flags);
479
480 if (himlSys_small) {
481 _himlSys_small = himlSys_small;
482
483 const Icon& icon = add(sfi.iIcon/*, IT_SYSCACHE*/);
484
485 ///@todo limit cache size
486 _pathCache[mapkey_syscache] = icon;
487
488 return icon;
489 }
490 }
491
492 return _icons[ICID_NONE];
493 }
494
495 const Icon& IconCache::extract(LPCTSTR path, int icon_idx, ICONCACHE_FLAGS flags)
496 {
497 IdxCacheKey key(path, make_pair(icon_idx, (flags|ICF_HICON)&~ICF_SYSCACHE));
498
499 key.first.toLower();
500
501 IdxCacheMap::iterator found = _idxCache.find(key);
502
503 if (found != _idxCache.end())
504 return _icons[found->second];
505
506 HICON hIcon;
507
508 if ((int)ExtractIconEx(path, icon_idx, NULL, &hIcon, 1) > 0) {
509 const Icon& icon = add(hIcon, IT_CACHED);
510
511 _idxCache[key] = icon;
512
513 return icon;
514 } else {
515
516 ///@todo retreive "http://.../favicon.ico" format icons
517
518 return _icons[ICID_NONE];
519 }
520 }
521
522 const Icon& IconCache::extract(IExtractIcon* pExtract, LPCTSTR path, int icon_idx, ICONCACHE_FLAGS flags)
523 {
524 HICON hIconLarge = 0;
525 HICON hIcon;
526
527 int icon_size = ICON_SIZE_FROM_ICF(flags);
528 HRESULT hr = pExtract->Extract(path, icon_idx, &hIconLarge, &hIcon, MAKELONG(GetSystemMetrics(SM_CXICON), icon_size));
529
530 if (hr == NOERROR) { //@@ oder SUCCEEDED(hr) ?
531 if (icon_size > ICON_SIZE_SMALL) { //@@ OK?
532 if (hIcon)
533 DestroyIcon(hIcon);
534
535 hIcon = hIconLarge;
536 } else {
537 if (hIconLarge)
538 DestroyIcon(hIconLarge);
539 }
540
541 if (hIcon)
542 return add(hIcon); //@@ When do we want not to free this icons?
543 }
544
545 return _icons[ICID_NONE];
546 }
547
548 const Icon& IconCache::extract(LPCITEMIDLIST pidl, ICONCACHE_FLAGS flags)
549 {
550 // search for matching icon with unchanged flags in the cache
551 PidlCacheKey mapkey(pidl, flags);
552 PidlCacheMap::iterator found = _pidlcache.find(mapkey);
553
554 if (found != _pidlcache.end())
555 return _icons[found->second];
556
557 // search for matching icon with handle
558 PidlCacheKey mapkey_hicon(pidl, flags|ICF_HICON);
559 if (flags != mapkey_hicon.second) {
560 found = _pidlcache.find(mapkey_hicon);
561
562 if (found != _pidlcache.end())
563 return _icons[found->second];
564 }
565
566 // search for matching icon in the system image list cache
567 PidlCacheKey mapkey_syscache(pidl, flags|ICF_SYSCACHE);
568 if (flags != mapkey_syscache.second) {
569 found = _pidlcache.find(mapkey_syscache);
570
571 if (found != _pidlcache.end())
572 return _icons[found->second];
573 }
574
575 SHFILEINFO sfi;
576
577 int shgfi_flags = SHGFI_PIDL;
578
579 if (!(flags & (ICF_LARGE|ICF_MIDDLE)))
580 shgfi_flags |= SHGFI_SMALLICON;
581
582 if (flags & ICF_OPEN)
583 shgfi_flags |= SHGFI_OPENICON;
584
585 if (flags & ICF_SYSCACHE) {
586 assert(!(flags&ICF_OVERLAYS));
587
588 HIMAGELIST himlSys = (HIMAGELIST) SHGetFileInfo((LPCTSTR)pidl, 0, &sfi, sizeof(sfi), SHGFI_SYSICONINDEX|shgfi_flags);
589 if (himlSys) {
590 const Icon& icon = add(sfi.iIcon/*, IT_SYSCACHE*/);
591
592 ///@todo limit cache size
593 _pidlcache[mapkey_syscache] = icon;
594
595 return icon;
596 }
597 } else {
598 if (flags & ICF_OVERLAYS)
599 shgfi_flags |= SHGFI_ADDOVERLAYS;
600
601 if (SHGetFileInfo((LPCTSTR)pidl, 0, &sfi, sizeof(sfi), SHGFI_ICON|shgfi_flags)) {
602 const Icon& icon = add(sfi.hIcon, IT_CACHED);
603
604 ///@todo limit cache size
605 _pidlcache[mapkey_hicon] = icon;
606
607 return icon;
608 }
609 }
610
611 return _icons[ICID_NONE];
612 }
613
614
615 const Icon& IconCache::add(HICON hIcon, ICON_TYPE type)
616 {
617 int id = ++s_next_id;
618
619 return _icons[id] = Icon(type, id, hIcon);
620 }
621
622 const Icon& IconCache::add(int sys_idx/*, ICON_TYPE type=IT_SYSCACHE*/)
623 {
624 int id = ++s_next_id;
625
626 return _icons[id] = SysCacheIcon(id, sys_idx);
627 }
628
629 const Icon& IconCache::get_icon(int id)
630 {
631 return _icons[id];
632 }
633
634 IconCache::~IconCache()
635 {
636 /* We don't need to free cached resources - they are automatically freed at process termination
637 for (int index = s_next_id; index >= 0; index--) {
638 IconMap::iterator found = _icons.find(index);
639
640 if (found != _icons.end()) {
641 Icon& icon = found->second;
642
643 if ((icon.get_icontype() == IT_DYNAMIC) ||
644 (icon.get_icontype() == IT_CACHED))
645 {
646 DestroyIcon(icon.get_hicon());
647 _icons.erase(found);
648 }
649 }
650 }
651 */
652 }
653
654 void IconCache::free_icon(int icon_id)
655 {
656 IconMap::iterator found = _icons.find(icon_id);
657
658 if (found != _icons.end()) {
659 Icon& icon = found->second;
660
661 if (icon.destroy())
662 _icons.erase(found);
663 }
664 }
665
666
667 ResString::ResString(UINT nid)
668 {
669 TCHAR buffer[BUFFER_LEN];
670
671 int len = LoadString(g_Globals._hInstance, nid, buffer, sizeof(buffer)/sizeof(TCHAR));
672
673 super::assign(buffer, len);
674 }
675
676
677 ResIcon::ResIcon(UINT nid)
678 {
679 _hicon = LoadIcon(g_Globals._hInstance, MAKEINTRESOURCE(nid));
680 }
681
682 SmallIcon::SmallIcon(UINT nid)
683 {
684 _hicon = (HICON)LoadImage(g_Globals._hInstance, MAKEINTRESOURCE(nid), IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED);
685 }
686
687 ResIconEx::ResIconEx(UINT nid, int w, int h)
688 {
689 _hicon = (HICON)LoadImage(g_Globals._hInstance, MAKEINTRESOURCE(nid), IMAGE_ICON, w, h, LR_SHARED);
690 }
691
692
693 void SetWindowIcon(HWND hwnd, UINT nid)
694 {
695 HICON hIcon = ResIcon(nid);
696 (void)Window_SetIcon(hwnd, ICON_BIG, hIcon);
697
698 HICON hIconSmall = SmallIcon(nid);
699 (void)Window_SetIcon(hwnd, ICON_SMALL, hIconSmall);
700 }
701
702
703 ResBitmap::ResBitmap(UINT nid)
704 {
705 _hBmp = LoadBitmap(g_Globals._hInstance, MAKEINTRESOURCE(nid));
706 }
707
708
709 #ifndef ROSSHELL
710
711 void explorer_show_frame(int cmdShow, LPTSTR lpCmdLine)
712 {
713 ExplorerCmd cmd;
714
715 if (g_Globals._hMainWnd) {
716 if (IsIconic(g_Globals._hMainWnd))
717 ShowWindow(g_Globals._hMainWnd, SW_RESTORE);
718 else
719 SetForegroundWindow(g_Globals._hMainWnd);
720
721 return;
722 }
723
724 g_Globals._prescan_nodes = false;
725
726 cmd._mdi = true;
727 cmd._cmdShow = cmdShow;
728
729 // parse command line options, which may overwrite the MDI flag
730 if (lpCmdLine)
731 cmd.ParseCmdLine(lpCmdLine);
732
733 // create main window
734 MainFrameBase::Create(cmd);
735 }
736
737 bool ExplorerCmd::ParseCmdLine(LPCTSTR lpCmdLine)
738 {
739 bool ok = true;
740
741 LPCTSTR b = lpCmdLine;
742 LPCTSTR p = b;
743
744 while(*b) {
745 // remove leading space
746 while(_istspace((unsigned)*b))
747 ++b;
748
749 p = b;
750
751 bool quote = false;
752
753 // options are separated by ','
754 for(; *p; ++p) {
755 if (*p == '"') // Quote characters may appear at any position in the command line.
756 quote = !quote;
757 else if (*p==',' && !quote)
758 break;
759 }
760
761 if (p > b) {
762 int l = p - b;
763
764 // remove trailing space
765 while(l>0 && _istspace((unsigned)b[l-1]))
766 --l;
767
768 if (!EvaluateOption(String(b, l)))
769 ok = false;
770
771 if (*p)
772 ++p;
773
774 b = p;
775 }
776 }
777
778 return ok;
779 }
780
781 bool ExplorerCmd::EvaluateOption(LPCTSTR option)
782 {
783 String opt_str;
784
785 // Remove quote characters, as they are evaluated at this point.
786 for(; *option; ++option)
787 if (*option != '"')
788 opt_str += *option;
789
790 option = opt_str;
791
792 if (option[0] == '/') {
793 ++option;
794
795 // option /e for windows in explorer mode
796 if (!_tcsicmp(option, TEXT("e")))
797 _flags |= OWM_EXPLORE;
798 // option /root for rooted explorer windows
799 else if (!_tcsicmp(option, TEXT("root")))
800 _flags |= OWM_ROOTED;
801 // non-standard options: /mdi, /sdi
802 else if (!_tcsicmp(option, TEXT("mdi")))
803 _mdi = true;
804 else if (!_tcsicmp(option, TEXT("sdi")))
805 _mdi = false;
806 else
807 return false;
808 } else {
809 if (!_path.empty())
810 return false;
811
812 _path = opt_str;
813 }
814
815 return true;
816 }
817
818 bool ExplorerCmd::IsValidPath() const
819 {
820 if (!_path.empty()) {
821 DWORD attribs = GetFileAttributes(_path);
822
823 if (attribs!=INVALID_FILE_ATTRIBUTES && (attribs&FILE_ATTRIBUTE_DIRECTORY))
824 return true; // file system path
825 else if (*_path==':' && _path.at(1)==':')
826 return true; // text encoded IDL
827 }
828
829 return false;
830 }
831
832 #else
833
834 void explorer_show_frame(int cmdShow, LPTSTR lpCmdLine)
835 {
836 if (!lpCmdLine)
837 lpCmdLine = TEXT("explorer.exe");
838
839 launch_file(GetDesktopWindow(), lpCmdLine, cmdShow);
840 }
841
842 #endif
843
844
845 PopupMenu::PopupMenu(UINT nid)
846 {
847 HMENU hMenu = LoadMenu(g_Globals._hInstance, MAKEINTRESOURCE(nid));
848 _hmenu = GetSubMenu(hMenu, 0);
849 RemoveMenu(hMenu, 0, MF_BYPOSITION);
850 DestroyMenu(hMenu);
851 }
852
853
854 /// "About Explorer" Dialog
855 struct ExplorerAboutDlg : public
856 CtlColorParent<
857 OwnerDrawParent<Dialog>
858 >
859 {
860 typedef CtlColorParent<
861 OwnerDrawParent<Dialog>
862 > super;
863
864 ExplorerAboutDlg(HWND hwnd)
865 : super(hwnd)
866 {
867 SetWindowIcon(hwnd, IDI_REACTOS);
868
869 new FlatButton(hwnd, IDOK);
870
871 _hfont = CreateFont(20, 0, 0, 0, FW_BOLD, TRUE, 0, 0, 0, 0, 0, 0, 0, TEXT("Sans Serif"));
872 new ColorStatic(hwnd, IDC_ROS_EXPLORER, RGB(32,32,128), 0, _hfont);
873
874 new HyperlinkCtrl(hwnd, IDC_WWW);
875
876 FmtString ver_txt(ResString(IDS_EXPLORER_VERSION_STR), (LPCTSTR)ResString(IDS_VERSION_STR));
877 SetWindowText(GetDlgItem(hwnd, IDC_VERSION_TXT), ver_txt);
878
879 HWND hwnd_winver = GetDlgItem(hwnd, IDC_WIN_VERSION);
880 SetWindowText(hwnd_winver, get_windows_version_str());
881 SetWindowFont(hwnd_winver, GetStockFont(DEFAULT_GUI_FONT), FALSE);
882
883 CenterWindow(hwnd);
884 }
885
886 ~ExplorerAboutDlg()
887 {
888 DeleteObject(_hfont);
889 }
890
891 LRESULT WndProc(UINT nmsg, WPARAM wparam, LPARAM lparam)
892 {
893 switch(nmsg) {
894 case WM_PAINT:
895 Paint();
896 break;
897
898 default:
899 return super::WndProc(nmsg, wparam, lparam);
900 }
901
902 return 0;
903 }
904
905 void Paint()
906 {
907 PaintCanvas canvas(_hwnd);
908
909 HICON hicon = (HICON) LoadImage(g_Globals._hInstance, MAKEINTRESOURCE(IDI_REACTOS_BIG), IMAGE_ICON, 0, 0, LR_SHARED);
910
911 DrawIconEx(canvas, 20, 10, hicon, 0, 0, 0, 0, DI_NORMAL);
912 }
913
914 protected:
915 HFONT _hfont;
916 };
917
918 void explorer_about(HWND hwndParent)
919 {
920 Dialog::DoModal(IDD_ABOUT_EXPLORER, WINDOW_CREATOR(ExplorerAboutDlg), hwndParent);
921 }
922
923
924 static void InitInstance(HINSTANCE hInstance)
925 {
926 CONTEXT("InitInstance");
927
928 setlocale(LC_COLLATE, ""); // set collating rules to local settings for compareName
929
930 #ifndef ROSSHELL
931 // register frame window class
932 g_Globals._hframeClass = IconWindowClass(CLASSNAME_FRAME,IDI_EXPLORER);
933
934 // register child window class
935 WindowClass(CLASSNAME_CHILDWND, CS_CLASSDC|CS_DBLCLKS|CS_VREDRAW).Register();
936
937 // register tree window class
938 WindowClass(CLASSNAME_WINEFILETREE, CS_CLASSDC|CS_DBLCLKS|CS_VREDRAW).Register();
939 #endif
940
941 g_Globals._cfStrFName = RegisterClipboardFormat(CFSTR_FILENAME);
942 }
943
944
945 int explorer_main(HINSTANCE hInstance, LPTSTR lpCmdLine, int cmdShow)
946 {
947 CONTEXT("explorer_main");
948
949 // initialize Common Controls library
950 CommonControlInit usingCmnCtrl;
951
952 try {
953 InitInstance(hInstance);
954 } catch(COMException& e) {
955 HandleException(e, GetDesktopWindow());
956 return -1;
957 }
958
959 #ifndef ROSSHELL
960 if (cmdShow != SW_HIDE) {
961 /* // don't maximize if being called from the ROS desktop
962 if (cmdShow == SW_SHOWNORMAL)
963 ///@todo read window placement from registry
964 cmdShow = SW_MAXIMIZE;
965 */
966
967 explorer_show_frame(cmdShow, lpCmdLine);
968 }
969 #endif
970
971 return Window::MessageLoop();
972 }
973
974
975 // MinGW does not provide a Unicode startup routine, so we have to implement an own.
976 #if defined(__MINGW32__) && defined(UNICODE)
977
978 #define _tWinMain wWinMain
979 int WINAPI wWinMain(HINSTANCE, HINSTANCE, LPWSTR, int);
980
981 int main(int argc, char* argv[])
982 {
983 CONTEXT("main");
984
985 STARTUPINFO startupinfo;
986 int nShowCmd = SW_SHOWNORMAL;
987
988 GetStartupInfo(&startupinfo);
989
990 if (startupinfo.dwFlags & STARTF_USESHOWWINDOW)
991 nShowCmd = startupinfo.wShowWindow;
992
993 LPWSTR cmdline = GetCommandLineW();
994
995 while(*cmdline && !_istspace((unsigned)*cmdline))
996 ++cmdline;
997
998 while(_istspace((unsigned)*cmdline))
999 ++cmdline;
1000
1001 return wWinMain(GetModuleHandle(NULL), 0, cmdline, nShowCmd);
1002 }
1003
1004 #endif // __MINGW && UNICODE
1005
1006
1007 static bool SetShellReadyEvent(LPCTSTR evtName)
1008 {
1009 HANDLE hEvent = OpenEvent(EVENT_MODIFY_STATE, FALSE, evtName);
1010 if (!hEvent)
1011 return false;
1012
1013 SetEvent(hEvent);
1014 CloseHandle(hEvent);
1015
1016 return true;
1017 }
1018
1019
1020 int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nShowCmd)
1021 {
1022 CONTEXT("WinMain()");
1023
1024 BOOL any_desktop_running = IsAnyDesktopRunning();
1025
1026 BOOL startup_desktop;
1027
1028 // strip extended options from the front of the command line
1029 String ext_options;
1030
1031 while(*lpCmdLine == '-') {
1032 while(*lpCmdLine && !_istspace((unsigned)*lpCmdLine))
1033 ext_options += *lpCmdLine++;
1034
1035 while(_istspace((unsigned)*lpCmdLine))
1036 ++lpCmdLine;
1037 }
1038
1039 // command line option "-install" to replace previous shell application with ROS Explorer
1040 if (_tcsstr(ext_options,TEXT("-install"))) {
1041 // install ROS Explorer into the registry
1042 TCHAR path[MAX_PATH];
1043
1044 int l = GetModuleFileName(0, path, COUNTOF(path));
1045 if (l) {
1046 HKEY hkey;
1047
1048 if (!RegOpenKey(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon"), &hkey)) {
1049
1050 ///@todo save previous shell application in config file
1051
1052 RegSetValueEx(hkey, TEXT("Shell"), 0, REG_SZ, (LPBYTE)path, l*sizeof(TCHAR));
1053 RegCloseKey(hkey);
1054 }
1055
1056 if (!RegOpenKey(HKEY_CURRENT_USER, TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon"), &hkey)) {
1057
1058 ///@todo save previous shell application in config file
1059
1060 RegSetValueEx(hkey, TEXT("Shell"), 0, REG_SZ, (LPBYTE)TEXT(""), l*sizeof(TCHAR));
1061 RegCloseKey(hkey);
1062 }
1063 }
1064
1065 HWND shellWindow = GetShellWindow();
1066
1067 if (shellWindow) {
1068 DWORD pid;
1069
1070 // terminate shell process for NT like systems
1071 GetWindowThreadProcessId(shellWindow, &pid);
1072 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
1073
1074 // On Win 9x it's sufficient to destroy the shell window.
1075 DestroyWindow(shellWindow);
1076
1077 if (TerminateProcess(hProcess, 0))
1078 WaitForSingleObject(hProcess, INFINITE);
1079
1080 CloseHandle(hProcess);
1081 }
1082
1083 startup_desktop = TRUE;
1084 } else {
1085 // create desktop window and task bar only, if there is no other shell and we are
1086 // the first explorer instance
1087 // MS Explorer looks additionally into the registry entry HKCU\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\shell,
1088 // to decide wether it is currently configured as shell application.
1089 startup_desktop = !any_desktop_running;
1090 }
1091
1092
1093 bool autostart = !any_desktop_running;
1094
1095 // disable autostart if the SHIFT key is pressed
1096 if (GetAsyncKeyState(VK_SHIFT) < 0)
1097 autostart = false;
1098
1099 #ifdef _DEBUG //MF: disabled for debugging
1100 autostart = false;
1101 #endif
1102
1103 // If there is given the command line option "-desktop", create desktop window anyways
1104 if (_tcsstr(ext_options,TEXT("-desktop")))
1105 startup_desktop = TRUE;
1106 #ifndef ROSSHELL
1107 else if (_tcsstr(ext_options,TEXT("-nodesktop")))
1108 startup_desktop = FALSE;
1109
1110 // Don't display cabinet window in desktop mode
1111 if (startup_desktop && !_tcsstr(ext_options,TEXT("-explorer")))
1112 nShowCmd = SW_HIDE;
1113 #endif
1114
1115 if (_tcsstr(ext_options,TEXT("-noautostart")))
1116 autostart = false;
1117 else if (_tcsstr(ext_options,TEXT("-autostart")))
1118 autostart = true;
1119
1120 #ifndef __WINE__
1121 if (_tcsstr(ext_options,TEXT("-console"))) {
1122 AllocConsole();
1123
1124 _dup2(_open_osfhandle((long)GetStdHandle(STD_INPUT_HANDLE), _O_RDONLY), 0);
1125 _dup2(_open_osfhandle((long)GetStdHandle(STD_OUTPUT_HANDLE), 0), 1);
1126 _dup2(_open_osfhandle((long)GetStdHandle(STD_ERROR_HANDLE), 0), 2);
1127
1128 g_Globals._log = _fdopen(1, "w");
1129 setvbuf(g_Globals._log, 0, _IONBF, 0);
1130
1131 LOG(TEXT("starting explorer debug log\n"));
1132 }
1133 #endif
1134
1135
1136 if (startup_desktop) {
1137 // hide the XP login screen (Credit to Nicolas Escuder)
1138 // another undocumented event: "Global\\msgina: ReturnToWelcome"
1139 if (!SetShellReadyEvent(TEXT("msgina: ShellReadyEvent")))
1140 SetShellReadyEvent(TEXT("Global\\msgina: ShellReadyEvent"));
1141 }
1142 #ifdef ROSSHELL
1143 else
1144 return 0; // no shell to launch, so exit immediatelly
1145 #endif
1146
1147
1148 if (!any_desktop_running) {
1149 // launch the shell DDE server
1150 if (g_SHDOCVW_ShellDDEInit)
1151 (*g_SHDOCVW_ShellDDEInit)(TRUE);
1152 }
1153
1154
1155 bool use_gdb_stub = false; // !IsDebuggerPresent();
1156
1157 if (_tcsstr(ext_options,TEXT("-debug")))
1158 use_gdb_stub = true;
1159
1160 if (_tcsstr(ext_options,TEXT("-break"))) {
1161 LOG(TEXT("debugger breakpoint"));
1162 #ifdef _MSC_VER
1163 __asm int 3
1164 #else
1165 asm("int3");
1166 #endif
1167 }
1168
1169 // activate GDB remote debugging stub if no other debugger is running
1170 if (use_gdb_stub) {
1171 LOG(TEXT("waiting for debugger connection...\n"));
1172
1173 initialize_gdb_stub();
1174 }
1175
1176 g_Globals.init(hInstance);
1177
1178 // initialize COM and OLE before creating the desktop window
1179 OleInit usingCOM;
1180
1181 // init common controls library
1182 CommonControlInit usingCmnCtrl;
1183
1184 g_Globals.read_persistent();
1185
1186 if (startup_desktop) {
1187 WaitCursor wait;
1188
1189 g_Globals._desktops.init();
1190
1191 g_Globals._hwndDesktop = DesktopWindow::Create();
1192 #ifdef _USE_HDESK
1193 g_Globals._desktops.get_current_Desktop()->_hwndDesktop = g_Globals._hwndDesktop;
1194 #endif
1195 }
1196
1197 if (_tcsstr(ext_options,TEXT("-?"))) {
1198 MessageBoxA(g_Globals._hwndDesktop,
1199 "/e open cabinet window in explorer mode\r\n"
1200 "/root open cabinet window in rooted mode\r\n"
1201 "/mdi open cabinet window in MDI mode\r\n"
1202 "/sdi open cabinet window in SDI mode\r\n"
1203 "\r\n"
1204 "-? display command line options\r\n"
1205 "\r\n"
1206 "-desktop start in desktop mode regardless of an already running shell\r\n"
1207 "-nodesktop disable desktop mode\r\n"
1208 "-explorer display cabinet window regardless of enabled desktop mode\r\n"
1209 "\r\n"
1210 "-install replace previous shell application with ROS Explorer\r\n"
1211 "\r\n"
1212 "-noautostart disable autostarts\r\n"
1213 "-autostart enable autostarts regardless of debug build\r\n"
1214 "\r\n"
1215 "-console open debug console\r\n"
1216 "\r\n"
1217 "-debug activate GDB remote debugging stub\r\n"
1218 "-break activate debugger breakpoint\r\n",
1219 "ROS Explorer - command line options", MB_OK);
1220 }
1221
1222 Thread* pSSOThread = NULL;
1223
1224 if (startup_desktop) {
1225 // launch SSO thread to allow message processing independent from the explorer main thread
1226 pSSOThread = new SSOThread;
1227 pSSOThread->Start();
1228 }
1229
1230 /**TODO launching autostart programs can be moved into a background thread. */
1231 if (autostart) {
1232 const char* argv[] = {"", "s"}; // call startup routine in SESSION_START mode
1233 startup(2, argv);
1234 }
1235
1236 #ifndef ROSSHELL
1237 if (g_Globals._hwndDesktop)
1238 g_Globals._desktop_mode = true;
1239 #endif
1240
1241
1242 int ret = explorer_main(hInstance, lpCmdLine, nShowCmd);
1243
1244
1245 // write configuration file
1246 g_Globals.write_persistent();
1247
1248 if (pSSOThread) {
1249 pSSOThread->Stop();
1250 delete pSSOThread;
1251 }
1252
1253 if (!any_desktop_running) {
1254 // shutdown the shell DDE server
1255 if (g_SHDOCVW_ShellDDEInit)
1256 (*g_SHDOCVW_ShellDDEInit)(FALSE);
1257 }
1258
1259 return ret;
1260 }