display available command line options when started with "-?"
[reactos.git] / reactos / 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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._last_error != XML_ERROR_NO_ELEMENTS)
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 XMLPos explorer_options = g_Globals.get_cfg("general/explorer");
727 XS_String mdiStr = XMLString(explorer_options, "mdi");
728
729 // If there isn't yet the "mdi" setting in the configuration, display the MDI/SDI dialog.
730 if (mdiStr.empty())
731 Dialog::DoModal(IDD_MDI_SDI, WINDOW_CREATOR(MdiSdiDlg), g_Globals._hwndDesktop);
732
733 // Now read the MDI attribute again and interpret it as boolean value.
734 cmd._mdi = XMLBool(explorer_options, "mdi", true);
735
736 cmd._cmdShow = cmdShow;
737
738 // parse command line options, which may overwrite the MDI flag
739 if (lpCmdLine)
740 cmd.ParseCmdLine(lpCmdLine);
741
742 // create main window
743 MainFrameBase::Create(cmd);
744 }
745
746 bool ExplorerCmd::ParseCmdLine(LPCTSTR lpCmdLine)
747 {
748 bool ok = true;
749
750 LPCTSTR b = lpCmdLine;
751 LPCTSTR p = b;
752
753 while(*b) {
754 // remove leading space
755 while(_istspace((unsigned)*b))
756 ++b;
757
758 p = b;
759
760 bool quote = false;
761
762 // options are separated by ','
763 for(; *p; ++p) {
764 if (*p == '"') // Quote characters may appear at any position in the command line.
765 quote = !quote;
766 else if (*p==',' && !quote)
767 break;
768 }
769
770 if (p > b) {
771 int l = p - b;
772
773 // remove trailing space
774 while(l>0 && _istspace((unsigned)b[l-1]))
775 --l;
776
777 if (!EvaluateOption(String(b, l)))
778 ok = false;
779
780 if (*p)
781 ++p;
782
783 b = p;
784 }
785 }
786
787 return ok;
788 }
789
790 bool ExplorerCmd::EvaluateOption(LPCTSTR option)
791 {
792 String opt_str;
793
794 // Remove quote characters, as they are evaluated at this point.
795 for(; *option; ++option)
796 if (*option != '"')
797 opt_str += *option;
798
799 option = opt_str;
800
801 if (option[0] == '/') {
802 ++option;
803
804 // option /e for windows in explorer mode
805 if (!_tcsicmp(option, TEXT("e")))
806 _flags |= OWM_EXPLORE;
807 // option /root for rooted explorer windows
808 else if (!_tcsicmp(option, TEXT("root")))
809 _flags |= OWM_ROOTED;
810 // non-standard options: /mdi, /sdi
811 else if (!_tcsicmp(option, TEXT("mdi")))
812 _mdi = true;
813 else if (!_tcsicmp(option, TEXT("sdi")))
814 _mdi = false;
815 else
816 return false;
817 } else {
818 if (!_path.empty())
819 return false;
820
821 _path = opt_str;
822 }
823
824 return true;
825 }
826
827 bool ExplorerCmd::IsValidPath() const
828 {
829 if (!_path.empty()) {
830 DWORD attribs = GetFileAttributes(_path);
831
832 if (attribs!=INVALID_FILE_ATTRIBUTES && (attribs&FILE_ATTRIBUTE_DIRECTORY))
833 return true; // file system path
834 else if (*_path==':' && _path.at(1)==':')
835 return true; // text encoded IDL
836 }
837
838 return false;
839 }
840
841 #else
842
843 void explorer_show_frame(int cmdShow, LPTSTR lpCmdLine)
844 {
845 if (!lpCmdLine)
846 lpCmdLine = TEXT("explorer.exe");
847
848 launch_file(GetDesktopWindow(), lpCmdLine, cmdShow);
849 }
850
851 #endif
852
853
854 PopupMenu::PopupMenu(UINT nid)
855 {
856 HMENU hMenu = LoadMenu(g_Globals._hInstance, MAKEINTRESOURCE(nid));
857 _hmenu = GetSubMenu(hMenu, 0);
858 }
859
860
861 /// "About Explorer" Dialog
862 struct ExplorerAboutDlg : public
863 CtlColorParent<
864 OwnerDrawParent<Dialog>
865 >
866 {
867 typedef CtlColorParent<
868 OwnerDrawParent<Dialog>
869 > super;
870
871 ExplorerAboutDlg(HWND hwnd)
872 : super(hwnd)
873 {
874 SetWindowIcon(hwnd, IDI_REACTOS);
875
876 new FlatButton(hwnd, IDOK);
877
878 _hfont = CreateFont(20, 0, 0, 0, FW_BOLD, TRUE, 0, 0, 0, 0, 0, 0, 0, TEXT("Sans Serif"));
879 new ColorStatic(hwnd, IDC_ROS_EXPLORER, RGB(32,32,128), 0, _hfont);
880
881 new HyperlinkCtrl(hwnd, IDC_WWW);
882
883 FmtString ver_txt(ResString(IDS_EXPLORER_VERSION_STR), (LPCTSTR)ResString(IDS_VERSION_STR));
884 SetWindowText(GetDlgItem(hwnd, IDC_VERSION_TXT), ver_txt);
885
886 HWND hwnd_winver = GetDlgItem(hwnd, IDC_WIN_VERSION);
887 SetWindowText(hwnd_winver, get_windows_version_str());
888 SetWindowFont(hwnd_winver, GetStockFont(DEFAULT_GUI_FONT), FALSE);
889
890 CenterWindow(hwnd);
891 }
892
893 ~ExplorerAboutDlg()
894 {
895 DeleteObject(_hfont);
896 }
897
898 LRESULT WndProc(UINT nmsg, WPARAM wparam, LPARAM lparam)
899 {
900 switch(nmsg) {
901 case WM_PAINT:
902 Paint();
903 break;
904
905 default:
906 return super::WndProc(nmsg, wparam, lparam);
907 }
908
909 return 0;
910 }
911
912 void Paint()
913 {
914 PaintCanvas canvas(_hwnd);
915
916 HICON hicon = (HICON) LoadImage(g_Globals._hInstance, MAKEINTRESOURCE(IDI_REACTOS_BIG), IMAGE_ICON, 0, 0, LR_SHARED);
917
918 DrawIconEx(canvas, 20, 10, hicon, 0, 0, 0, 0, DI_NORMAL);
919 }
920
921 protected:
922 HFONT _hfont;
923 };
924
925 void explorer_about(HWND hwndParent)
926 {
927 Dialog::DoModal(IDD_ABOUT_EXPLORER, WINDOW_CREATOR(ExplorerAboutDlg), hwndParent);
928 }
929
930
931 static void InitInstance(HINSTANCE hInstance)
932 {
933 CONTEXT("InitInstance");
934
935 setlocale(LC_COLLATE, ""); // set collating rules to local settings for compareName
936
937 #ifndef ROSSHELL
938 // register frame window class
939 g_Globals._hframeClass = IconWindowClass(CLASSNAME_FRAME,IDI_EXPLORER);
940
941 // register child window class
942 WindowClass(CLASSNAME_CHILDWND, CS_CLASSDC|CS_DBLCLKS|CS_VREDRAW).Register();
943
944 // register tree window class
945 WindowClass(CLASSNAME_WINEFILETREE, CS_CLASSDC|CS_DBLCLKS|CS_VREDRAW).Register();
946 #endif
947
948 g_Globals._cfStrFName = RegisterClipboardFormat(CFSTR_FILENAME);
949 }
950
951
952 int explorer_main(HINSTANCE hInstance, LPTSTR lpCmdLine, int cmdShow)
953 {
954 CONTEXT("explorer_main");
955
956 // initialize Common Controls library
957 CommonControlInit usingCmnCtrl;
958
959 try {
960 InitInstance(hInstance);
961 } catch(COMException& e) {
962 HandleException(e, GetDesktopWindow());
963 return -1;
964 }
965
966 #ifndef ROSSHELL
967 if (cmdShow != SW_HIDE) {
968 /* // don't maximize if being called from the ROS desktop
969 if (cmdShow == SW_SHOWNORMAL)
970 ///@todo read window placement from registry
971 cmdShow = SW_MAXIMIZE;
972 */
973
974 explorer_show_frame(cmdShow, lpCmdLine);
975 }
976 #endif
977
978 return Window::MessageLoop();
979 }
980
981
982 // MinGW does not provide a Unicode startup routine, so we have to implement an own.
983 #if defined(__MINGW32__) && defined(UNICODE)
984
985 #define _tWinMain wWinMain
986 int WINAPI wWinMain(HINSTANCE, HINSTANCE, LPWSTR, int);
987
988 int main(int argc, char* argv[])
989 {
990 CONTEXT("main");
991
992 STARTUPINFO startupinfo;
993 int nShowCmd = SW_SHOWNORMAL;
994
995 GetStartupInfo(&startupinfo);
996
997 if (startupinfo.dwFlags & STARTF_USESHOWWINDOW)
998 nShowCmd = startupinfo.wShowWindow;
999
1000 LPWSTR cmdline = GetCommandLineW();
1001
1002 while(*cmdline && !_istspace((unsigned)*cmdline))
1003 ++cmdline;
1004
1005 while(_istspace((unsigned)*cmdline))
1006 ++cmdline;
1007
1008 return wWinMain(GetModuleHandle(NULL), 0, cmdline, nShowCmd);
1009 }
1010
1011 #endif // __MINGW && UNICODE
1012
1013
1014 static bool SetShellReadyEvent(LPCTSTR evtName)
1015 {
1016 HANDLE hEvent = OpenEvent(EVENT_MODIFY_STATE, FALSE, evtName);
1017 if (!hEvent)
1018 return false;
1019
1020 SetEvent(hEvent);
1021 CloseHandle(hEvent);
1022
1023 return true;
1024 }
1025
1026
1027 int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nShowCmd)
1028 {
1029 CONTEXT("WinMain()");
1030
1031 BOOL any_desktop_running = IsAnyDesktopRunning();
1032
1033 BOOL startup_desktop;
1034
1035 // strip extended options from the front of the command line
1036 String ext_options;
1037
1038 while(*lpCmdLine == '-') {
1039 while(*lpCmdLine && !_istspace((unsigned)*lpCmdLine))
1040 ext_options += *lpCmdLine++;
1041
1042 while(_istspace((unsigned)*lpCmdLine))
1043 ++lpCmdLine;
1044 }
1045
1046 // command line option "-install" to replace previous shell application with ROS Explorer
1047 if (_tcsstr(ext_options,TEXT("-install"))) {
1048 // install ROS Explorer into the registry
1049 TCHAR path[MAX_PATH];
1050
1051 int l = GetModuleFileName(0, path, COUNTOF(path));
1052 if (l) {
1053 HKEY hkey;
1054
1055 if (!RegOpenKey(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon"), &hkey)) {
1056
1057 ///@todo save previous shell application in config file
1058
1059 RegSetValueEx(hkey, TEXT("Shell"), 0, REG_SZ, (LPBYTE)path, l*sizeof(TCHAR));
1060 RegCloseKey(hkey);
1061 }
1062
1063 if (!RegOpenKey(HKEY_CURRENT_USER, TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon"), &hkey)) {
1064
1065 ///@todo save previous shell application in config file
1066
1067 RegSetValueEx(hkey, TEXT("Shell"), 0, REG_SZ, (LPBYTE)TEXT(""), l*sizeof(TCHAR));
1068 RegCloseKey(hkey);
1069 }
1070 }
1071
1072 HWND shellWindow = GetShellWindow();
1073
1074 if (shellWindow) {
1075 DWORD pid;
1076
1077 // terminate shell process for NT like systems
1078 GetWindowThreadProcessId(shellWindow, &pid);
1079 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
1080
1081 // On Win 9x it's sufficient to destroy the shell window.
1082 DestroyWindow(shellWindow);
1083
1084 if (TerminateProcess(hProcess, 0))
1085 WaitForSingleObject(hProcess, INFINITE);
1086
1087 CloseHandle(hProcess);
1088 }
1089
1090 startup_desktop = TRUE;
1091 } else {
1092 // create desktop window and task bar only, if there is no other shell and we are
1093 // the first explorer instance
1094 // MS Explorer looks additionally into the registry entry HKCU\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\shell,
1095 // to decide wether it is currently configured as shell application.
1096 startup_desktop = !any_desktop_running;
1097 }
1098
1099
1100 bool autostart = !any_desktop_running;
1101
1102 // disable autostart if the SHIFT key is pressed
1103 if (GetAsyncKeyState(VK_SHIFT) < 0)
1104 autostart = false;
1105
1106 #ifdef _DEBUG //MF: disabled for debugging
1107 autostart = false;
1108 #endif
1109
1110 // If there is given the command line option "-desktop", create desktop window anyways
1111 if (_tcsstr(ext_options,TEXT("-desktop")))
1112 startup_desktop = TRUE;
1113 #ifndef ROSSHELL
1114 else if (_tcsstr(ext_options,TEXT("-nodesktop")))
1115 startup_desktop = FALSE;
1116
1117 // Don't display cabinet window in desktop mode
1118 if (startup_desktop && !_tcsstr(ext_options,TEXT("-explorer")))
1119 nShowCmd = SW_HIDE;
1120 #endif
1121
1122 if (_tcsstr(ext_options,TEXT("-noautostart")))
1123 autostart = false;
1124 else if (_tcsstr(ext_options,TEXT("-autostart")))
1125 autostart = true;
1126
1127 #ifndef __WINE__
1128 if (_tcsstr(ext_options,TEXT("-console"))) {
1129 AllocConsole();
1130
1131 _dup2(_open_osfhandle((long)GetStdHandle(STD_INPUT_HANDLE), _O_RDONLY), 0);
1132 _dup2(_open_osfhandle((long)GetStdHandle(STD_OUTPUT_HANDLE), 0), 1);
1133 _dup2(_open_osfhandle((long)GetStdHandle(STD_ERROR_HANDLE), 0), 2);
1134
1135 g_Globals._log = _fdopen(1, "w");
1136 setvbuf(g_Globals._log, 0, _IONBF, 0);
1137
1138 LOG(TEXT("starting explorer debug log\n"));
1139 }
1140 #endif
1141
1142
1143 if (startup_desktop) {
1144 // hide the XP login screen (Credit to Nicolas Escuder)
1145 // another undocumented event: "Global\\msgina: ReturnToWelcome"
1146 if (!SetShellReadyEvent(TEXT("msgina: ShellReadyEvent")))
1147 SetShellReadyEvent(TEXT("Global\\msgina: ShellReadyEvent"));
1148 }
1149 #ifdef ROSSHELL
1150 else
1151 return 0; // no shell to launch, so exit immediatelly
1152 #endif
1153
1154
1155 if (!any_desktop_running) {
1156 // launch the shell DDE server
1157 if (g_SHDOCVW_ShellDDEInit)
1158 (*g_SHDOCVW_ShellDDEInit)(TRUE);
1159 }
1160
1161
1162 bool use_gdb_stub = false; // !IsDebuggerPresent();
1163
1164 if (_tcsstr(ext_options,TEXT("-debug")))
1165 use_gdb_stub = true;
1166
1167 if (_tcsstr(ext_options,TEXT("-break"))) {
1168 LOG(TEXT("debugger breakpoint"));
1169 #ifdef _MSC_VER
1170 __asm int 3
1171 #else
1172 asm("int3");
1173 #endif
1174 }
1175
1176 // activate GDB remote debugging stub if no other debugger is running
1177 if (use_gdb_stub) {
1178 LOG(TEXT("waiting for debugger connection...\n"));
1179
1180 initialize_gdb_stub();
1181 }
1182
1183 g_Globals.init(hInstance);
1184
1185 // initialize COM and OLE before creating the desktop window
1186 OleInit usingCOM;
1187
1188 // init common controls library
1189 CommonControlInit usingCmnCtrl;
1190
1191 g_Globals.read_persistent();
1192
1193 if (startup_desktop) {
1194 WaitCursor wait;
1195
1196 g_Globals._desktops.init();
1197
1198 g_Globals._hwndDesktop = DesktopWindow::Create();
1199 #ifdef _USE_HDESK
1200 g_Globals._desktops.get_current_Desktop()->_hwndDesktop = g_Globals._hwndDesktop;
1201 #endif
1202 }
1203
1204 if (_tcsstr(ext_options,TEXT("-?"))) {
1205 MessageBoxA(g_Globals._hwndDesktop,
1206 "/e open cabinet window in explorer mode\r\n"
1207 "/root open cabinet window in rooted mode\r\n"
1208 "/mdi open cabinet window in MDI mode\r\n"
1209 "/sdi open cabinet window in SDI mode\r\n"
1210 "\r\n"
1211 "-? display command line options\r\n"
1212 "\r\n"
1213 "-desktop start in desktop mode regardless of an already running shell\r\n"
1214 "-nodesktop disable desktop mode\r\n"
1215 "-explorer display cabinet window regardless of enabled desktop mode\r\n"
1216 "\r\n"
1217 "-install replace previous shell application with ROS Explorer\r\n"
1218 "\r\n"
1219 "-noautostart disable autostarts\r\n"
1220 "-autostart enable autostarts regardless of debug build\r\n"
1221 "\r\n"
1222 "-console open debug console\r\n"
1223 "\r\n"
1224 "-debug activate GDB remote debugging stub\r\n"
1225 "-break activate debugger breakpoint\r\n",
1226 "ROS Explorer - command line options", MB_OK);
1227 }
1228
1229 Thread* pSSOThread = NULL;
1230
1231 if (startup_desktop) {
1232 // launch SSO thread to allow message processing independent from the explorer main thread
1233 pSSOThread = new SSOThread;
1234 pSSOThread->Start();
1235 }
1236
1237 /**TODO launching autostart programs can be moved into a background thread. */
1238 if (autostart) {
1239 const char* argv[] = {"", "s"}; // call startup routine in SESSION_START mode
1240 startup(2, argv);
1241 }
1242
1243 #ifndef ROSSHELL
1244 if (g_Globals._hwndDesktop)
1245 g_Globals._desktop_mode = true;
1246 #endif
1247
1248
1249 int ret = explorer_main(hInstance, lpCmdLine, nShowCmd);
1250
1251
1252 // write configuration file
1253 g_Globals.write_persistent();
1254
1255 if (pSSOThread) {
1256 pSSOThread->Stop();
1257 delete pSSOThread;
1258 }
1259
1260 if (!any_desktop_running) {
1261 // shutdown the shell DDE server
1262 if (g_SHDOCVW_ShellDDEInit)
1263 (*g_SHDOCVW_ShellDDEInit)(FALSE);
1264 }
1265
1266 return ret;
1267 }