- Use a separate icon for minimizing windows.
[reactos.git] / reactos / subsys / system / explorer / explorer.cpp
1 /*
2 * Copyright 2003, 2004, 2005 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(_cfg_path)) {
97 if (_cfg._last_error != XML_ERROR_NO_ELEMENTS)
98 MessageBox(g_Globals._hwndDesktop, String(_cfg._last_error_msg.c_str()),
99 TEXT("ROS Explorer - reading user settings"), MB_OK);
100
101 _cfg.read(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(_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 entry->_display_name = (LPTSTR) malloc((len+1)*sizeof(TCHAR));
231 lstrcpyn(entry->_display_name, entry->_data.cFileName, len + 1);
232 }
233
234 if (is_exe_file(ext))
235 entry->_data.dwFileAttributes |= ATTRIBUTE_EXECUTABLE;
236 }
237
238 return ext;
239 }
240
241
242 Icon::Icon()
243 : _id(ICID_UNKNOWN),
244 _itype(IT_STATIC),
245 _hicon(0)
246 {
247 }
248
249 Icon::Icon(ICON_ID id, UINT nid) //, int cx, int cy
250 : _id(id),
251 _itype(IT_STATIC),
252 _hicon(ResIcon(nid)) // ResIconEx(nid, cx, cy)
253 {
254 }
255
256 Icon::Icon(ICON_TYPE itype, int id, HICON hIcon)
257 : _id((ICON_ID)id),
258 _itype(itype),
259 _hicon(hIcon)
260 {
261 }
262
263 Icon::Icon(ICON_TYPE itype, int id, int sys_idx)
264 : _id((ICON_ID)id),
265 _itype(itype),
266 _sys_idx(sys_idx)
267 {
268 }
269
270 void Icon::draw(HDC hdc, int x, int y, int cx, int cy, COLORREF bk_color, HBRUSH bk_brush) const
271 {
272 if (_itype == IT_SYSCACHE)
273 ImageList_DrawEx(g_Globals._icon_cache.get_sys_imagelist(), _sys_idx, hdc, x, y, cx, cy, bk_color, CLR_DEFAULT, ILD_NORMAL);
274 else
275 DrawIconEx(hdc, x, y, _hicon, cx, cy, 0, bk_brush, DI_NORMAL);
276 }
277
278 HBITMAP Icon::create_bitmap(COLORREF bk_color, HBRUSH hbrBkgnd, HDC hdc_wnd) const
279 {
280 if (_itype == IT_SYSCACHE) {
281 HIMAGELIST himl = g_Globals._icon_cache.get_sys_imagelist();
282
283 int cx, cy;
284 ImageList_GetIconSize(himl, &cx, &cy);
285
286 HBITMAP hbmp = CreateCompatibleBitmap(hdc_wnd, cx, cy);
287 HDC hdc = CreateCompatibleDC(hdc_wnd);
288 HBITMAP hbmp_old = SelectBitmap(hdc, hbmp);
289 ImageList_DrawEx(himl, _sys_idx, hdc, 0, 0, cx, cy, bk_color, CLR_DEFAULT, ILD_NORMAL);
290 SelectBitmap(hdc, hbmp_old);
291 DeleteDC(hdc);
292
293 return hbmp;
294 } else
295 return create_bitmap_from_icon(_hicon, hbrBkgnd, hdc_wnd);
296 }
297
298
299 int Icon::add_to_imagelist(HIMAGELIST himl, HDC hdc_wnd, COLORREF bk_color, HBRUSH bk_brush) const
300 {
301 int ret;
302
303 if (_itype == IT_SYSCACHE) {
304 HIMAGELIST himl = g_Globals._icon_cache.get_sys_imagelist();
305
306 int cx, cy;
307 ImageList_GetIconSize(himl, &cx, &cy);
308
309 HBITMAP hbmp = CreateCompatibleBitmap(hdc_wnd, cx, cy);
310 HDC hdc = CreateCompatibleDC(hdc_wnd);
311 HBITMAP hbmp_old = SelectBitmap(hdc, hbmp);
312 ImageList_DrawEx(himl, _sys_idx, hdc, 0, 0, cx, cy, bk_color, CLR_DEFAULT, ILD_NORMAL);
313 SelectBitmap(hdc, hbmp_old);
314 DeleteDC(hdc);
315
316 ret = ImageList_Add(himl, hbmp, 0);
317
318 DeleteObject(hbmp);
319 } else
320 ret = ImageList_AddAlphaIcon(himl, _hicon, bk_brush, hdc_wnd);
321
322 return ret;
323 }
324
325 HBITMAP create_bitmap_from_icon(HICON hIcon, HBRUSH hbrush_bkgnd, HDC hdc_wnd/*, bool large_icons*/)
326 {
327 int cx = GetSystemMetrics(SM_CXSMICON); //ICON_SIZE_X;
328 int cy = GetSystemMetrics(SM_CYSMICON); //ICON_SIZE_Y;
329 HBITMAP hbmp = CreateCompatibleBitmap(hdc_wnd, cx, cy);
330
331 MemCanvas canvas;
332 BitmapSelection sel(canvas, hbmp);
333
334 RECT rect = {0, 0, cx, cy};
335 FillRect(canvas, &rect, hbrush_bkgnd);
336
337 DrawIconEx(canvas, 0, 0, hIcon, cx, cy, 0, hbrush_bkgnd, DI_NORMAL);
338
339 return hbmp;
340 }
341
342 HBITMAP create_small_bitmap_from_icon(HICON hIcon, HBRUSH hbrush_bkgnd, HDC hdc_wnd)
343 {
344 int cx = GetSystemMetrics(SM_CXSMICON);
345 int cy = GetSystemMetrics(SM_CYSMICON);
346 HBITMAP hbmp = CreateCompatibleBitmap(hdc_wnd, cx, cy);
347
348 MemCanvas canvas;
349 BitmapSelection sel(canvas, hbmp);
350
351 RECT rect = {0, 0, cx, cy};
352 FillRect(canvas, &rect, hbrush_bkgnd);
353
354 DrawIconEx(canvas, 0, 0, hIcon, cx, cy, 0, hbrush_bkgnd, DI_NORMAL);
355
356 return hbmp;
357 }
358
359 int ImageList_AddAlphaIcon(HIMAGELIST himl, HICON hIcon, HBRUSH hbrush_bkgnd, HDC hdc_wnd)
360 {
361 HBITMAP hbmp = create_bitmap_from_icon(hIcon, hbrush_bkgnd, hdc_wnd);
362
363 int ret = ImageList_Add(himl, hbmp, 0);
364
365 DeleteObject(hbmp);
366
367 return ret;
368 }
369
370
371 int IconCache::s_next_id = ICID_DYNAMIC;
372
373
374 void IconCache::init()
375 {
376 _icons[ICID_NONE] = Icon(IT_STATIC, ICID_NONE, (HICON)0);
377
378 _icons[ICID_FOLDER] = Icon(ICID_FOLDER, IDI_FOLDER);
379 //_icons[ICID_DOCUMENT] = Icon(ICID_DOCUMENT, IDI_DOCUMENT);
380 _icons[ICID_EXPLORER] = Icon(ICID_EXPLORER, IDI_EXPLORER);
381 _icons[ICID_APP] = Icon(ICID_APP, IDI_APPICON);
382
383 _icons[ICID_CONFIG] = Icon(ICID_CONFIG, IDI_CONFIG);
384 _icons[ICID_DOCUMENTS] = Icon(ICID_DOCUMENTS, IDI_DOCUMENTS);
385 _icons[ICID_FAVORITES] = Icon(ICID_FAVORITES, IDI_FAVORITES);
386 _icons[ICID_INFO] = Icon(ICID_INFO, IDI_INFO);
387 _icons[ICID_APPS] = Icon(ICID_APPS, IDI_APPS);
388 _icons[ICID_SEARCH] = Icon(ICID_SEARCH, IDI_SEARCH);
389 _icons[ICID_ACTION] = Icon(ICID_ACTION, IDI_ACTION);
390 _icons[ICID_SEARCH_DOC] = Icon(ICID_SEARCH_DOC, IDI_SEARCH_DOC);
391 _icons[ICID_PRINTER] = Icon(ICID_PRINTER, IDI_PRINTER);
392 _icons[ICID_NETWORK] = Icon(ICID_NETWORK, IDI_NETWORK);
393 _icons[ICID_COMPUTER] = Icon(ICID_COMPUTER, IDI_COMPUTER);
394 _icons[ICID_LOGOFF] = Icon(ICID_LOGOFF, IDI_LOGOFF);
395 _icons[ICID_BOOKMARK] = Icon(ICID_BOOKMARK, IDI_DOT_TRANS);
396 _icons[ICID_MINIMIZE] = Icon(ICID_MINIMIZE, IDI_MINIMIZE);
397 }
398
399
400 const Icon& IconCache::extract(LPCTSTR path, ICONCACHE_FLAGS flags)
401 {
402 // search for matching icon with unchanged flags in the cache
403 CacheKey mapkey(path, flags);
404 PathCacheMap::iterator found = _pathCache.find(mapkey);
405
406 if (found != _pathCache.end())
407 return _icons[found->second];
408
409 // search for matching icon with handle
410 CacheKey mapkey_hicon(path, flags|ICF_HICON);
411 if (flags != mapkey_hicon.second) {
412 found = _pathCache.find(mapkey_hicon);
413
414 if (found != _pathCache.end())
415 return _icons[found->second];
416 }
417
418 // search for matching icon in the system image list cache
419 CacheKey mapkey_syscache(path, flags|ICF_SYSCACHE);
420 if (flags != mapkey_syscache.second) {
421 found = _pathCache.find(mapkey_syscache);
422
423 if (found != _pathCache.end())
424 return _icons[found->second];
425 }
426
427 SHFILEINFO sfi;
428
429 int shgfi_flags = 0;
430
431 if (flags & ICF_OPEN)
432 shgfi_flags |= SHGFI_OPENICON;
433
434 if ((flags&(ICF_LARGE|ICF_OVERLAYS|ICF_HICON)) && !(flags&ICF_SYSCACHE)) {
435 shgfi_flags |= SHGFI_ICON;
436
437 if (!(flags & ICF_LARGE))
438 shgfi_flags |= SHGFI_SMALLICON;
439
440 if (flags & ICF_OVERLAYS)
441 shgfi_flags |= SHGFI_ADDOVERLAYS;
442
443 // get small/big icons with/without overlays
444 if (SHGetFileInfo(path, 0, &sfi, sizeof(sfi), shgfi_flags)) {
445 const Icon& icon = add(sfi.hIcon, IT_CACHED);
446
447 ///@todo limit cache size
448 _pathCache[mapkey_hicon] = icon;
449
450 return icon;
451 }
452 } else {
453 assert(!(flags&ICF_OVERLAYS));
454
455 shgfi_flags |= SHGFI_SYSICONINDEX|SHGFI_SMALLICON;
456
457 // use system image list - the "search program dialog" needs it
458 HIMAGELIST himlSys_small = (HIMAGELIST) SHGetFileInfo(path, 0, &sfi, sizeof(sfi), shgfi_flags);
459
460 if (himlSys_small) {
461 _himlSys_small = himlSys_small;
462
463 const Icon& icon = add(sfi.iIcon/*, IT_SYSCACHE*/);
464
465 ///@todo limit cache size
466 _pathCache[mapkey_syscache] = icon;
467
468 return icon;
469 }
470 }
471
472 return _icons[ICID_NONE];
473 }
474
475 const Icon& IconCache::extract(LPCTSTR path, int idx, ICONCACHE_FLAGS flags)
476 {
477 IdxCacheKey key(path, make_pair(idx, (flags|ICF_HICON)&~ICF_SYSCACHE));
478
479 key.first.toLower();
480
481 IdxCacheMap::iterator found = _idxCache.find(key);
482
483 if (found != _idxCache.end())
484 return _icons[found->second];
485
486 HICON hIcon;
487
488 if ((int)ExtractIconEx(path, idx, NULL, &hIcon, 1) > 0) {
489 const Icon& icon = add(hIcon, IT_CACHED);
490
491 _idxCache[key] = icon;
492
493 return icon;
494 } else {
495
496 ///@todo retreive "http://.../favicon.ico" format icons
497
498 return _icons[ICID_NONE];
499 }
500 }
501
502 const Icon& IconCache::extract(IExtractIcon* pExtract, LPCTSTR path, int idx, ICONCACHE_FLAGS flags)
503 {
504 HICON hIconLarge = 0;
505 HICON hIcon;
506
507 bool large_icons = flags & ICF_LARGE;
508 HRESULT hr = pExtract->Extract(path, idx, &hIconLarge, &hIcon, MAKELONG(GetSystemMetrics(SM_CXICON), ICON_SIZE_X));
509
510 if (hr == NOERROR) { //@@ oder SUCCEEDED(hr) ?
511 if (large_icons) { //@@ OK?
512 if (hIcon)
513 DestroyIcon(hIcon);
514
515 hIcon = hIconLarge;
516 } else {
517 if (hIconLarge)
518 DestroyIcon(hIconLarge);
519 }
520
521 if (hIcon)
522 return add(hIcon); //@@ When do we want not to free this icons?
523 }
524
525 return _icons[ICID_NONE];
526 }
527
528 const Icon& IconCache::add(HICON hIcon, ICON_TYPE type)
529 {
530 int id = ++s_next_id;
531
532 return _icons[id] = Icon(type, id, hIcon);
533 }
534
535 const Icon& IconCache::add(int sys_idx/*, ICON_TYPE type=IT_SYSCACHE*/)
536 {
537 int id = ++s_next_id;
538
539 return _icons[id] = SysCacheIcon(id, sys_idx);
540 }
541
542 const Icon& IconCache::get_icon(int id)
543 {
544 return _icons[id];
545 }
546
547 void IconCache::free_icon(int icon_id)
548 {
549 IconMap::iterator found = _icons.find(icon_id);
550
551 if (found != _icons.end()) {
552 Icon& icon = found->second;
553
554 if (icon.destroy())
555 _icons.erase(found);
556 }
557 }
558
559
560 ResString::ResString(UINT nid)
561 {
562 TCHAR buffer[BUFFER_LEN];
563
564 int len = LoadString(g_Globals._hInstance, nid, buffer, sizeof(buffer)/sizeof(TCHAR));
565
566 super::assign(buffer, len);
567 }
568
569
570 ResIcon::ResIcon(UINT nid)
571 {
572 _hicon = LoadIcon(g_Globals._hInstance, MAKEINTRESOURCE(nid));
573 }
574
575 SmallIcon::SmallIcon(UINT nid)
576 {
577 _hicon = (HICON)LoadImage(g_Globals._hInstance, MAKEINTRESOURCE(nid), IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED);
578 }
579
580 ResIconEx::ResIconEx(UINT nid, int w, int h)
581 {
582 _hicon = (HICON)LoadImage(g_Globals._hInstance, MAKEINTRESOURCE(nid), IMAGE_ICON, w, h, LR_SHARED);
583 }
584
585
586 void SetWindowIcon(HWND hwnd, UINT nid)
587 {
588 HICON hIcon = ResIcon(nid);
589 (void)Window_SetIcon(hwnd, ICON_BIG, hIcon);
590
591 HICON hIconSmall = SmallIcon(nid);
592 (void)Window_SetIcon(hwnd, ICON_SMALL, hIconSmall);
593 }
594
595
596 ResBitmap::ResBitmap(UINT nid)
597 {
598 _hBmp = LoadBitmap(g_Globals._hInstance, MAKEINTRESOURCE(nid));
599 }
600
601
602 #ifndef ROSSHELL
603
604 void explorer_show_frame(int cmdShow, LPTSTR lpCmdLine)
605 {
606 ExplorerCmd cmd;
607
608 if (g_Globals._hMainWnd) {
609 if (IsIconic(g_Globals._hMainWnd))
610 ShowWindow(g_Globals._hMainWnd, SW_RESTORE);
611 else
612 SetForegroundWindow(g_Globals._hMainWnd);
613
614 return;
615 }
616
617 g_Globals._prescan_nodes = false;
618
619 XMLPos explorer_options = g_Globals.get_cfg("general/explorer");
620 XS_String mdiStr = XMLString(explorer_options, "mdi");
621
622 // If there isn't yet the "mdi" setting in the configuration, display MDI/SDI dialog.
623 if (mdiStr.empty())
624 Dialog::DoModal(IDD_MDI_SDI, WINDOW_CREATOR(MdiSdiDlg), g_Globals._hwndDesktop);
625
626 // Now read the MDI attribute again and interpret it as boolean value.
627 cmd._mdi = XMLBool(explorer_options, "mdi", true);
628
629 cmd._cmdShow = cmdShow;
630
631 // parse command line options, which may overwrite the MDI flag
632 if (lpCmdLine)
633 cmd.ParseCmdLine(lpCmdLine);
634
635 // create main window
636 MainFrameBase::Create(cmd);
637 }
638
639 bool ExplorerCmd::ParseCmdLine(LPCTSTR lpCmdLine)
640 {
641 bool ok = true;
642
643 LPCTSTR b = lpCmdLine;
644 LPCTSTR p = b;
645
646 while(*b) {
647 // remove leading space
648 while(_istspace((unsigned)*b))
649 ++b;
650
651 p = b;
652
653 bool quote = false;
654
655 // options are separated by ','
656 for(; *p; ++p) {
657 if (*p == '"') // Quote characters may appear at any position in the command line.
658 quote = !quote;
659 else if (*p==',' && !quote)
660 break;
661 }
662
663 if (p > b) {
664 int l = p - b;
665
666 // remove trailing space
667 while(l>0 && _istspace((unsigned)b[l-1]))
668 --l;
669
670 if (!EvaluateOption(String(b, l)))
671 ok = false;
672
673 if (*p)
674 ++p;
675
676 b = p;
677 }
678 }
679
680 return ok;
681 }
682
683 bool ExplorerCmd::EvaluateOption(LPCTSTR option)
684 {
685 String opt_str;
686
687 // Remove quote characters, as they are evaluated at this point.
688 for(; *option; ++option)
689 if (*option != '"')
690 opt_str += *option;
691
692 option = opt_str;
693
694 if (option[0] == '/') {
695 ++option;
696
697 // option /e for windows in explorer mode
698 if (!_tcsicmp(option, TEXT("e")))
699 _flags |= OWM_EXPLORE;
700 // option /root for rooted explorer windows
701 else if (!_tcsicmp(option, TEXT("root")))
702 _flags |= OWM_ROOTED;
703 // non-standard options: /mdi, /sdi
704 else if (!_tcsicmp(option, TEXT("mdi")))
705 _mdi = true;
706 else if (!_tcsicmp(option, TEXT("sdi")))
707 _mdi = false;
708 else
709 return false;
710 } else {
711 if (!_path.empty())
712 return false;
713
714 _path = opt_str;
715 }
716
717 return true;
718 }
719
720 bool ExplorerCmd::IsValidPath() const
721 {
722 if (!_path.empty()) {
723 DWORD attribs = GetFileAttributes(_path);
724
725 if (attribs!=INVALID_FILE_ATTRIBUTES && (attribs&FILE_ATTRIBUTE_DIRECTORY))
726 return true; // file system path
727 else if (*_path==':' && _path.at(1)==':')
728 return true; // text encoded IDL
729 }
730
731 return false;
732 }
733
734 #else
735
736 void explorer_show_frame(int cmdShow, LPTSTR lpCmdLine)
737 {
738 if (!lpCmdLine)
739 lpCmdLine = TEXT("explorer.exe");
740
741 launch_file(GetDesktopWindow(), lpCmdLine, cmdShow);
742 }
743
744 #endif
745
746
747 PopupMenu::PopupMenu(UINT nid)
748 {
749 HMENU hMenu = LoadMenu(g_Globals._hInstance, MAKEINTRESOURCE(nid));
750 _hmenu = GetSubMenu(hMenu, 0);
751 }
752
753
754 /// "About Explorer" Dialog
755 struct ExplorerAboutDlg : public
756 CtlColorParent<
757 OwnerDrawParent<Dialog>
758 >
759 {
760 typedef CtlColorParent<
761 OwnerDrawParent<Dialog>
762 > super;
763
764 ExplorerAboutDlg(HWND hwnd)
765 : super(hwnd)
766 {
767 SetWindowIcon(hwnd, IDI_REACTOS);
768
769 new FlatButton(hwnd, IDOK);
770
771 _hfont = CreateFont(20, 0, 0, 0, FW_BOLD, TRUE, 0, 0, 0, 0, 0, 0, 0, TEXT("Sans Serif"));
772 new ColorStatic(hwnd, IDC_ROS_EXPLORER, RGB(32,32,128), 0, _hfont);
773
774 new HyperlinkCtrl(hwnd, IDC_WWW);
775
776 FmtString ver_txt(ResString(IDS_EXPLORER_VERSION_STR), (LPCTSTR)ResString(IDS_VERSION_STR));
777 SetWindowText(GetDlgItem(hwnd, IDC_VERSION_TXT), ver_txt);
778
779 HWND hwnd_winver = GetDlgItem(hwnd, IDC_WIN_VERSION);
780 SetWindowText(hwnd_winver, get_windows_version_str());
781 SetWindowFont(hwnd_winver, GetStockFont(DEFAULT_GUI_FONT), FALSE);
782
783 CenterWindow(hwnd);
784 }
785
786 ~ExplorerAboutDlg()
787 {
788 DeleteObject(_hfont);
789 }
790
791 LRESULT WndProc(UINT nmsg, WPARAM wparam, LPARAM lparam)
792 {
793 switch(nmsg) {
794 case WM_PAINT:
795 Paint();
796 break;
797
798 default:
799 return super::WndProc(nmsg, wparam, lparam);
800 }
801
802 return 0;
803 }
804
805 void Paint()
806 {
807 PaintCanvas canvas(_hwnd);
808
809 HICON hicon = (HICON) LoadImage(g_Globals._hInstance, MAKEINTRESOURCE(IDI_REACTOS_BIG), IMAGE_ICON, 0, 0, LR_SHARED);
810
811 DrawIconEx(canvas, 20, 10, hicon, 0, 0, 0, 0, DI_NORMAL);
812 }
813
814 protected:
815 HFONT _hfont;
816 };
817
818 void explorer_about(HWND hwndParent)
819 {
820 Dialog::DoModal(IDD_ABOUT_EXPLORER, WINDOW_CREATOR(ExplorerAboutDlg), hwndParent);
821 }
822
823
824 static void InitInstance(HINSTANCE hInstance)
825 {
826 CONTEXT("InitInstance");
827
828 setlocale(LC_COLLATE, ""); // set collating rules to local settings for compareName
829
830 #ifndef ROSSHELL
831 // register frame window class
832 g_Globals._hframeClass = IconWindowClass(CLASSNAME_FRAME,IDI_EXPLORER);
833
834 // register child window class
835 WindowClass(CLASSNAME_CHILDWND, CS_CLASSDC|CS_DBLCLKS|CS_VREDRAW).Register();
836
837 // register tree window class
838 WindowClass(CLASSNAME_WINEFILETREE, CS_CLASSDC|CS_DBLCLKS|CS_VREDRAW).Register();
839 #endif
840
841 g_Globals._cfStrFName = RegisterClipboardFormat(CFSTR_FILENAME);
842 }
843
844
845 int explorer_main(HINSTANCE hInstance, LPTSTR lpCmdLine, int cmdShow)
846 {
847 CONTEXT("explorer_main");
848
849 // initialize Common Controls library
850 CommonControlInit usingCmnCtrl;
851
852 try {
853 InitInstance(hInstance);
854 } catch(COMException& e) {
855 HandleException(e, GetDesktopWindow());
856 return -1;
857 }
858
859 #ifndef ROSSHELL
860 if (cmdShow != SW_HIDE) {
861 /* // don't maximize if being called from the ROS desktop
862 if (cmdShow == SW_SHOWNORMAL)
863 ///@todo read window placement from registry
864 cmdShow = SW_MAXIMIZE;
865 */
866
867 explorer_show_frame(cmdShow, lpCmdLine);
868 }
869 #endif
870
871 return Window::MessageLoop();
872 }
873
874
875 // MinGW does not provide a Unicode startup routine, so we have to implement an own.
876 #if defined(__MINGW32__) && defined(UNICODE)
877
878 #define _tWinMain wWinMain
879 int WINAPI wWinMain(HINSTANCE, HINSTANCE, LPWSTR, int);
880
881 int main(int argc, char* argv[])
882 {
883 CONTEXT("main");
884
885 STARTUPINFO startupinfo;
886 int nShowCmd = SW_SHOWNORMAL;
887
888 GetStartupInfo(&startupinfo);
889
890 if (startupinfo.dwFlags & STARTF_USESHOWWINDOW)
891 nShowCmd = startupinfo.wShowWindow;
892
893 LPWSTR cmdline = GetCommandLineW();
894
895 while(*cmdline && !_istspace((unsigned)*cmdline))
896 ++cmdline;
897
898 while(_istspace((unsigned)*cmdline))
899 ++cmdline;
900
901 return wWinMain(GetModuleHandle(NULL), 0, cmdline, nShowCmd);
902 }
903
904 #endif // __MINGW && UNICODE
905
906
907 static bool SetShellReadyEvent(LPCTSTR evtName)
908 {
909 HANDLE hEvent = OpenEvent(EVENT_MODIFY_STATE, FALSE, evtName);
910 if (!hEvent)
911 return false;
912
913 SetEvent(hEvent);
914 CloseHandle(hEvent);
915
916 return true;
917 }
918
919
920 int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nShowCmd)
921 {
922 CONTEXT("WinMain()");
923
924 BOOL any_desktop_running = IsAnyDesktopRunning();
925
926 BOOL startup_desktop;
927
928 // strip extended options from the front of the command line
929 String ext_options;
930
931 while(*lpCmdLine == '-') {
932 while(*lpCmdLine && !_istspace((unsigned)*lpCmdLine))
933 ext_options += *lpCmdLine++;
934
935 while(_istspace((unsigned)*lpCmdLine))
936 ++lpCmdLine;
937 }
938
939 // command line option "-install" to replace previous shell application with ROS Explorer
940 if (_tcsstr(ext_options,TEXT("-install"))) {
941 // install ROS Explorer into the registry
942 TCHAR path[MAX_PATH];
943
944 int l = GetModuleFileName(0, path, COUNTOF(path));
945 if (l) {
946 HKEY hkey;
947
948 if (!RegOpenKey(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon"), &hkey)) {
949
950 ///@todo save previous shell application in config file
951
952 RegSetValueEx(hkey, TEXT("Shell"), 0, REG_SZ, (LPBYTE)path, l*sizeof(TCHAR));
953 RegCloseKey(hkey);
954 }
955
956 if (!RegOpenKey(HKEY_CURRENT_USER, TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon"), &hkey)) {
957
958 ///@todo save previous shell application in config file
959
960 RegSetValueEx(hkey, TEXT("Shell"), 0, REG_SZ, (LPBYTE)TEXT(""), l*sizeof(TCHAR));
961 RegCloseKey(hkey);
962 }
963 }
964
965 HWND shellWindow = GetShellWindow();
966
967 if (shellWindow) {
968 DWORD pid;
969
970 // terminate shell process for NT like systems
971 GetWindowThreadProcessId(shellWindow, &pid);
972 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
973
974 // On Win 9x it's sufficient to destroy the shell window.
975 DestroyWindow(shellWindow);
976
977 if (TerminateProcess(hProcess, 0))
978 WaitForSingleObject(hProcess, INFINITE);
979
980 CloseHandle(hProcess);
981 }
982
983 startup_desktop = TRUE;
984 } else {
985 // create desktop window and task bar only, if there is no other shell and we are
986 // the first explorer instance
987 // MS Explorer looks additionally into the registry entry HKCU\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\shell,
988 // to decide wether it is currently configured as shell application.
989 startup_desktop = !any_desktop_running;
990 }
991
992
993 bool autostart = !any_desktop_running;
994
995 // disable autostart if the SHIFT key is pressed
996 if (GetAsyncKeyState(VK_SHIFT) < 0)
997 autostart = false;
998
999 #ifdef _DEBUG //MF: disabled for debugging
1000 autostart = false;
1001 #endif
1002
1003 // If there is given the command line option "-desktop", create desktop window anyways
1004 if (_tcsstr(ext_options,TEXT("-desktop")))
1005 startup_desktop = TRUE;
1006 #ifndef ROSSHELL
1007 else if (_tcsstr(ext_options,TEXT("-nodesktop")))
1008 startup_desktop = FALSE;
1009
1010 // Don't display cabinet window in desktop mode
1011 if (startup_desktop && !_tcsstr(ext_options,TEXT("-explorer")))
1012 nShowCmd = SW_HIDE;
1013 #endif
1014
1015 if (_tcsstr(ext_options,TEXT("-noautostart")))
1016 autostart = false;
1017 else if (_tcsstr(ext_options,TEXT("-autostart")))
1018 autostart = true;
1019
1020 #ifndef __WINE__
1021 if (_tcsstr(ext_options,TEXT("-console"))) {
1022 AllocConsole();
1023
1024 _dup2(_open_osfhandle((long)GetStdHandle(STD_INPUT_HANDLE), _O_RDONLY), 0);
1025 _dup2(_open_osfhandle((long)GetStdHandle(STD_OUTPUT_HANDLE), 0), 1);
1026 _dup2(_open_osfhandle((long)GetStdHandle(STD_ERROR_HANDLE), 0), 2);
1027
1028 g_Globals._log = _fdopen(1, "w");
1029 setvbuf(g_Globals._log, 0, _IONBF, 0);
1030
1031 LOG(TEXT("starting explorer debug log\n"));
1032 }
1033 #endif
1034
1035
1036 if (startup_desktop) {
1037 // hide the XP login screen (Credit to Nicolas Escuder)
1038 // another undocumented event: "Global\\msgina: ReturnToWelcome"
1039 if (!SetShellReadyEvent(TEXT("msgina: ShellReadyEvent")))
1040 SetShellReadyEvent(TEXT("Global\\msgina: ShellReadyEvent"));
1041 }
1042 #ifdef ROSSHELL
1043 else
1044 return 0; // no shell to launch, so exit immediatelly
1045 #endif
1046
1047
1048 if (!any_desktop_running) {
1049 // launch the shell DDE server
1050 if (g_SHDOCVW_ShellDDEInit)
1051 (*g_SHDOCVW_ShellDDEInit)(TRUE);
1052 }
1053
1054
1055 bool use_gdb_stub = false; // !IsDebuggerPresent();
1056
1057 if (_tcsstr(ext_options,TEXT("-debug")))
1058 use_gdb_stub = true;
1059
1060 if (_tcsstr(ext_options,TEXT("-break"))) {
1061 LOG(TEXT("debugger breakpoint"));
1062 #ifdef _MSC_VER
1063 __asm int 3
1064 #else
1065 asm("int3");
1066 #endif
1067 }
1068
1069 // activate GDB remote debugging stub if no other debugger is running
1070 if (use_gdb_stub) {
1071 LOG(TEXT("waiting for debugger connection...\n"));
1072
1073 initialize_gdb_stub();
1074 }
1075
1076 g_Globals.init(hInstance);
1077
1078 // initialize COM and OLE before creating the desktop window
1079 OleInit usingCOM;
1080
1081 // init common controls library
1082 CommonControlInit usingCmnCtrl;
1083
1084 g_Globals.read_persistent();
1085
1086 if (startup_desktop) {
1087 WaitCursor wait;
1088
1089 g_Globals._desktops.init();
1090
1091 g_Globals._hwndDesktop = DesktopWindow::Create();
1092 #ifdef _USE_HDESK
1093 g_Globals._desktops.get_current_Desktop()->_hwndDesktop = g_Globals._hwndDesktop;
1094 #endif
1095 }
1096
1097 Thread* pSSOThread = NULL;
1098
1099 if (startup_desktop) {
1100 // launch SSO thread to allow message processing independent from the explorer main thread
1101 pSSOThread = new SSOThread;
1102 pSSOThread->Start();
1103 }
1104
1105 /**TODO launching autostart programs can be moved into a background thread. */
1106 if (autostart) {
1107 char* argv[] = {"", "s"}; // call startup routine in SESSION_START mode
1108 startup(2, argv);
1109 }
1110
1111 #ifndef ROSSHELL
1112 if (g_Globals._hwndDesktop)
1113 g_Globals._desktop_mode = true;
1114 #endif
1115
1116
1117 int ret = explorer_main(hInstance, lpCmdLine, nShowCmd);
1118
1119
1120 // write configuration file
1121 g_Globals.write_persistent();
1122
1123 if (pSSOThread) {
1124 pSSOThread->Stop();
1125 delete pSSOThread;
1126 }
1127
1128 if (!any_desktop_running) {
1129 // shutdown the shell DDE server
1130 if (g_SHDOCVW_ShellDDEInit)
1131 (*g_SHDOCVW_ShellDDEInit)(FALSE);
1132 }
1133
1134 return ret;
1135 }