[CMAKE]
[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 Window::MessageLoop();
972
973 return 1;
974 }
975
976
977 // MinGW does not provide a Unicode startup routine, so we have to implement an own.
978 #if defined(__MINGW32__) && defined(UNICODE)
979
980 #define _tWinMain wWinMain
981 int WINAPI wWinMain(HINSTANCE, HINSTANCE, LPWSTR, int);
982
983 int main(int argc, char* argv[])
984 {
985 CONTEXT("main");
986
987 STARTUPINFO startupinfo;
988 int nShowCmd = SW_SHOWNORMAL;
989
990 GetStartupInfo(&startupinfo);
991
992 if (startupinfo.dwFlags & STARTF_USESHOWWINDOW)
993 nShowCmd = startupinfo.wShowWindow;
994
995 LPWSTR cmdline = GetCommandLineW();
996
997 while(*cmdline && !_istspace((unsigned)*cmdline))
998 ++cmdline;
999
1000 while(_istspace((unsigned)*cmdline))
1001 ++cmdline;
1002
1003 return wWinMain(GetModuleHandle(NULL), 0, cmdline, nShowCmd);
1004 }
1005
1006 #endif // __MINGW && UNICODE
1007
1008
1009 static bool SetShellReadyEvent(LPCTSTR evtName)
1010 {
1011 HANDLE hEvent = OpenEvent(EVENT_MODIFY_STATE, FALSE, evtName);
1012 if (!hEvent)
1013 return false;
1014
1015 SetEvent(hEvent);
1016 CloseHandle(hEvent);
1017
1018 return true;
1019 }
1020
1021
1022 int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nShowCmd)
1023 {
1024 CONTEXT("WinMain()");
1025
1026 BOOL any_desktop_running = IsAnyDesktopRunning();
1027
1028 BOOL startup_desktop;
1029
1030 // strip extended options from the front of the command line
1031 String ext_options;
1032
1033 while(*lpCmdLine == '-') {
1034 while(*lpCmdLine && !_istspace((unsigned)*lpCmdLine))
1035 ext_options += *lpCmdLine++;
1036
1037 while(_istspace((unsigned)*lpCmdLine))
1038 ++lpCmdLine;
1039 }
1040
1041 // command line option "-install" to replace previous shell application with ROS Explorer
1042 if (_tcsstr(ext_options,TEXT("-install"))) {
1043 // install ROS Explorer into the registry
1044 TCHAR path[MAX_PATH];
1045
1046 int l = GetModuleFileName(0, path, COUNTOF(path));
1047 if (l) {
1048 HKEY hkey;
1049
1050 if (!RegOpenKey(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon"), &hkey)) {
1051
1052 ///@todo save previous shell application in config file
1053
1054 RegSetValueEx(hkey, TEXT("Shell"), 0, REG_SZ, (LPBYTE)path, l*sizeof(TCHAR));
1055 RegCloseKey(hkey);
1056 }
1057
1058 if (!RegOpenKey(HKEY_CURRENT_USER, TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon"), &hkey)) {
1059
1060 ///@todo save previous shell application in config file
1061
1062 RegSetValueEx(hkey, TEXT("Shell"), 0, REG_SZ, (LPBYTE)TEXT(""), l*sizeof(TCHAR));
1063 RegCloseKey(hkey);
1064 }
1065 }
1066
1067 HWND shellWindow = GetShellWindow();
1068
1069 if (shellWindow) {
1070 DWORD pid;
1071
1072 // terminate shell process for NT like systems
1073 GetWindowThreadProcessId(shellWindow, &pid);
1074 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
1075
1076 // On Win 9x it's sufficient to destroy the shell window.
1077 DestroyWindow(shellWindow);
1078
1079 if (TerminateProcess(hProcess, 0))
1080 WaitForSingleObject(hProcess, INFINITE);
1081
1082 CloseHandle(hProcess);
1083 }
1084
1085 startup_desktop = TRUE;
1086 } else {
1087 // create desktop window and task bar only, if there is no other shell and we are
1088 // the first explorer instance
1089 // MS Explorer looks additionally into the registry entry HKCU\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\shell,
1090 // to decide wether it is currently configured as shell application.
1091 startup_desktop = !any_desktop_running;
1092 }
1093
1094
1095 bool autostart = !any_desktop_running;
1096
1097 // disable autostart if the SHIFT key is pressed
1098 if (GetAsyncKeyState(VK_SHIFT) < 0)
1099 autostart = false;
1100
1101 #ifdef _DEBUG //MF: disabled for debugging
1102 autostart = false;
1103 #endif
1104
1105 // If there is given the command line option "-desktop", create desktop window anyways
1106 if (_tcsstr(ext_options,TEXT("-desktop")))
1107 startup_desktop = TRUE;
1108 #ifndef ROSSHELL
1109 else if (_tcsstr(ext_options,TEXT("-nodesktop")))
1110 startup_desktop = FALSE;
1111
1112 // Don't display cabinet window in desktop mode
1113 if (startup_desktop && !_tcsstr(ext_options,TEXT("-explorer")))
1114 nShowCmd = SW_HIDE;
1115 #endif
1116
1117 if (_tcsstr(ext_options,TEXT("-noautostart")))
1118 autostart = false;
1119 else if (_tcsstr(ext_options,TEXT("-autostart")))
1120 autostart = true;
1121
1122 #ifndef __WINE__
1123 if (_tcsstr(ext_options,TEXT("-console"))) {
1124 AllocConsole();
1125
1126 _dup2(_open_osfhandle((long)GetStdHandle(STD_INPUT_HANDLE), _O_RDONLY), 0);
1127 _dup2(_open_osfhandle((long)GetStdHandle(STD_OUTPUT_HANDLE), 0), 1);
1128 _dup2(_open_osfhandle((long)GetStdHandle(STD_ERROR_HANDLE), 0), 2);
1129
1130 g_Globals._log = _fdopen(1, "w");
1131 setvbuf(g_Globals._log, 0, _IONBF, 0);
1132
1133 LOG(TEXT("starting explorer debug log\n"));
1134 }
1135 #endif
1136
1137
1138 if (startup_desktop) {
1139 // hide the XP login screen (Credit to Nicolas Escuder)
1140 // another undocumented event: "Global\\msgina: ReturnToWelcome"
1141 if (!SetShellReadyEvent(TEXT("msgina: ShellReadyEvent")))
1142 SetShellReadyEvent(TEXT("Global\\msgina: ShellReadyEvent"));
1143 }
1144 #ifdef ROSSHELL
1145 else
1146 return 0; // no shell to launch, so exit immediatelly
1147 #endif
1148
1149
1150 if (!any_desktop_running) {
1151 // launch the shell DDE server
1152 if (g_SHDOCVW_ShellDDEInit)
1153 (*g_SHDOCVW_ShellDDEInit)(TRUE);
1154 }
1155
1156
1157 bool use_gdb_stub = false; // !IsDebuggerPresent();
1158
1159 if (_tcsstr(ext_options,TEXT("-debug")))
1160 use_gdb_stub = true;
1161
1162 if (_tcsstr(ext_options,TEXT("-break"))) {
1163 LOG(TEXT("debugger breakpoint"));
1164 #ifdef _MSC_VER
1165 __asm int 3
1166 #else
1167 asm("int3");
1168 #endif
1169 }
1170
1171 // activate GDB remote debugging stub if no other debugger is running
1172 if (use_gdb_stub) {
1173 LOG(TEXT("waiting for debugger connection...\n"));
1174
1175 initialize_gdb_stub();
1176 }
1177
1178 g_Globals.init(hInstance);
1179
1180 // initialize COM and OLE before creating the desktop window
1181 OleInit usingCOM;
1182
1183 // init common controls library
1184 CommonControlInit usingCmnCtrl;
1185
1186 g_Globals.read_persistent();
1187
1188 if (startup_desktop) {
1189 WaitCursor wait;
1190
1191 g_Globals._desktops.init();
1192
1193 g_Globals._hwndDesktop = DesktopWindow::Create();
1194 #ifdef _USE_HDESK
1195 g_Globals._desktops.get_current_Desktop()->_hwndDesktop = g_Globals._hwndDesktop;
1196 #endif
1197 }
1198
1199 if (_tcsstr(ext_options,TEXT("-?"))) {
1200 MessageBoxA(g_Globals._hwndDesktop,
1201 "/e open cabinet window in explorer mode\r\n"
1202 "/root open cabinet window in rooted mode\r\n"
1203 "/mdi open cabinet window in MDI mode\r\n"
1204 "/sdi open cabinet window in SDI mode\r\n"
1205 "\r\n"
1206 "-? display command line options\r\n"
1207 "\r\n"
1208 "-desktop start in desktop mode regardless of an already running shell\r\n"
1209 "-nodesktop disable desktop mode\r\n"
1210 "-explorer display cabinet window regardless of enabled desktop mode\r\n"
1211 "\r\n"
1212 "-install replace previous shell application with ROS Explorer\r\n"
1213 "\r\n"
1214 "-noautostart disable autostarts\r\n"
1215 "-autostart enable autostarts regardless of debug build\r\n"
1216 "\r\n"
1217 "-console open debug console\r\n"
1218 "\r\n"
1219 "-debug activate GDB remote debugging stub\r\n"
1220 "-break activate debugger breakpoint\r\n",
1221 "ROS Explorer - command line options", MB_OK);
1222 }
1223
1224 Thread* pSSOThread = NULL;
1225
1226 if (startup_desktop) {
1227 // launch SSO thread to allow message processing independent from the explorer main thread
1228 pSSOThread = new SSOThread;
1229 pSSOThread->Start();
1230 }
1231
1232 /**TODO launching autostart programs can be moved into a background thread. */
1233 if (autostart) {
1234 const char* argv[] = {"", "s"}; // call startup routine in SESSION_START mode
1235 startup(2, argv);
1236 }
1237
1238 #ifndef ROSSHELL
1239 if (g_Globals._hwndDesktop)
1240 g_Globals._desktop_mode = true;
1241 #endif
1242
1243
1244 int ret = explorer_main(hInstance, lpCmdLine, nShowCmd);
1245
1246
1247 // write configuration file
1248 g_Globals.write_persistent();
1249
1250 if (pSSOThread) {
1251 pSSOThread->Stop();
1252 delete pSSOThread;
1253 }
1254
1255 if (!any_desktop_running) {
1256 // shutdown the shell DDE server
1257 if (g_SHDOCVW_ShellDDEInit)
1258 (*g_SHDOCVW_ShellDDEInit)(FALSE);
1259 }
1260
1261 return ret;
1262 }