test version of startmenu root with big icons
[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 big_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 }
397
398
399 const Icon& IconCache::extract(LPCTSTR path, bool big_icons)
400 {
401 PathMap::iterator found = _pathMap.find(path);
402
403 if (found != _pathMap.end())
404 return _icons[found->second];
405
406 SHFILEINFO sfi;
407
408 if (big_icons) {
409 if (SHGetFileInfo(path, 0, &sfi, sizeof(sfi), SHGFI_ICON)) {
410 const Icon& icon = add(sfi.hIcon, IT_CACHED);
411
412 ///@todo limit cache size
413 _pathMap[path] = icon;
414
415 return icon;
416 }
417 } else {
418 // use system image list - the "search program dialog" needs it
419 HIMAGELIST himlSys_small = (HIMAGELIST) SHGetFileInfo(path, 0, &sfi, sizeof(sfi), SHGFI_SYSICONINDEX|SHGFI_SMALLICON);
420
421 if (himlSys_small) {
422 _himlSys_small = himlSys_small;
423
424 const Icon& icon = add(sfi.iIcon/*, IT_SYSCACHE*/);
425
426 ///@todo limit cache size
427 _pathMap[path] = icon;
428
429 return icon;
430 }
431 }
432
433 return _icons[ICID_NONE];
434 }
435
436 const Icon& IconCache::extract(LPCTSTR path, int idx)
437 {
438 CachePair key(path, idx);
439
440 key.first.toLower();
441
442 PathIdxMap::iterator found = _pathIdxMap.find(key);
443
444 if (found != _pathIdxMap.end())
445 return _icons[found->second];
446
447 HICON hIcon;
448
449 if ((int)ExtractIconEx(path, idx, NULL, &hIcon, 1) > 0) {
450 const Icon& icon = add(hIcon, IT_CACHED);
451
452 _pathIdxMap[key] = icon;
453
454 return icon;
455 } else {
456
457 ///@todo retreive "http://.../favicon.ico" format icons
458
459 return _icons[ICID_NONE];
460 }
461 }
462
463 const Icon& IconCache::extract(IExtractIcon* pExtract, LPCTSTR path, int idx, bool big_icons)
464 {
465 HICON hIconLarge = 0;
466 HICON hIcon;
467
468 HRESULT hr = pExtract->Extract(path, idx, &hIconLarge, &hIcon, MAKELONG(GetSystemMetrics(SM_CXICON), ICON_SIZE_X));
469
470 if (hr == NOERROR) { //@@ oder SUCCEEDED(hr) ?
471 if (big_icons) { //@@ OK?
472 if (hIcon)
473 DestroyIcon(hIcon);
474
475 hIcon = hIconLarge;
476 } else {
477 if (hIconLarge)
478 DestroyIcon(hIconLarge);
479 }
480
481 if (hIcon)
482 return add(hIcon);
483 }
484
485 return _icons[ICID_NONE];
486 }
487
488 const Icon& IconCache::add(HICON hIcon, ICON_TYPE type)
489 {
490 int id = ++s_next_id;
491
492 return _icons[id] = Icon(type, id, hIcon);
493 }
494
495 const Icon& IconCache::add(int sys_idx/*, ICON_TYPE type=IT_SYSCACHE*/)
496 {
497 int id = ++s_next_id;
498
499 return _icons[id] = SysCacheIcon(id, sys_idx);
500 }
501
502 const Icon& IconCache::get_icon(int id)
503 {
504 return _icons[id];
505 }
506
507 void IconCache::free_icon(int icon_id)
508 {
509 IconMap::iterator found = _icons.find(icon_id);
510
511 if (found != _icons.end()) {
512 Icon& icon = found->second;
513
514 if (icon.destroy())
515 _icons.erase(found);
516 }
517 }
518
519
520 ResString::ResString(UINT nid)
521 {
522 TCHAR buffer[BUFFER_LEN];
523
524 int len = LoadString(g_Globals._hInstance, nid, buffer, sizeof(buffer)/sizeof(TCHAR));
525
526 super::assign(buffer, len);
527 }
528
529
530 ResIcon::ResIcon(UINT nid)
531 {
532 _hicon = LoadIcon(g_Globals._hInstance, MAKEINTRESOURCE(nid));
533 }
534
535 SmallIcon::SmallIcon(UINT nid)
536 {
537 _hicon = (HICON)LoadImage(g_Globals._hInstance, MAKEINTRESOURCE(nid), IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED);
538 }
539
540 ResIconEx::ResIconEx(UINT nid, int w, int h)
541 {
542 _hicon = (HICON)LoadImage(g_Globals._hInstance, MAKEINTRESOURCE(nid), IMAGE_ICON, w, h, LR_SHARED);
543 }
544
545
546 void SetWindowIcon(HWND hwnd, UINT nid)
547 {
548 HICON hIcon = ResIcon(nid);
549 (void)Window_SetIcon(hwnd, ICON_BIG, hIcon);
550
551 HICON hIconSmall = SmallIcon(nid);
552 (void)Window_SetIcon(hwnd, ICON_SMALL, hIconSmall);
553 }
554
555
556 ResBitmap::ResBitmap(UINT nid)
557 {
558 _hBmp = LoadBitmap(g_Globals._hInstance, MAKEINTRESOURCE(nid));
559 }
560
561
562 #ifndef ROSSHELL
563
564 void explorer_show_frame(int cmdShow, LPTSTR lpCmdLine)
565 {
566 ExplorerCmd cmd;
567
568 if (g_Globals._hMainWnd) {
569 if (IsIconic(g_Globals._hMainWnd))
570 ShowWindow(g_Globals._hMainWnd, SW_RESTORE);
571 else
572 SetForegroundWindow(g_Globals._hMainWnd);
573
574 return;
575 }
576
577 g_Globals._prescan_nodes = false;
578
579 XMLPos explorer_options = g_Globals.get_cfg("general/explorer");
580 XS_String mdiStr = XMLString(explorer_options, "mdi");
581
582 // If there isn't yet the "mdi" setting in the configuration, display MDI/SDI dialog.
583 if (mdiStr.empty())
584 Dialog::DoModal(IDD_MDI_SDI, WINDOW_CREATOR(MdiSdiDlg), g_Globals._hwndDesktop);
585
586 // Now read the MDI attribute again and interpret it as boolean value.
587 cmd._mdi = XMLBool(explorer_options, "mdi", true);
588
589 cmd._cmdShow = cmdShow;
590
591 // parse command line options, which may overwrite the MDI flag
592 if (lpCmdLine)
593 cmd.ParseCmdLine(lpCmdLine);
594
595 // create main window
596 MainFrameBase::Create(cmd);
597 }
598
599 bool ExplorerCmd::ParseCmdLine(LPCTSTR lpCmdLine)
600 {
601 bool ok = true;
602
603 LPCTSTR b = lpCmdLine;
604 LPCTSTR p = b;
605
606 while(*b) {
607 // remove leading space
608 while(_istspace((unsigned)*b))
609 ++b;
610
611 p = b;
612
613 bool quote = false;
614
615 // options are separated by ','
616 for(; *p; ++p) {
617 if (*p == '"') // Quote characters may appear at any position in the command line.
618 quote = !quote;
619 else if (*p==',' && !quote)
620 break;
621 }
622
623 if (p > b) {
624 int l = p - b;
625
626 // remove trailing space
627 while(l>0 && _istspace((unsigned)b[l-1]))
628 --l;
629
630 if (!EvaluateOption(String(b, l)))
631 ok = false;
632
633 if (*p)
634 ++p;
635
636 b = p;
637 }
638 }
639
640 return ok;
641 }
642
643 bool ExplorerCmd::EvaluateOption(LPCTSTR option)
644 {
645 String opt_str;
646
647 // Remove quote characters, as they are evaluated at this point.
648 for(; *option; ++option)
649 if (*option != '"')
650 opt_str += *option;
651
652 option = opt_str;
653
654 if (option[0] == '/') {
655 ++option;
656
657 // option /e for windows in explorer mode
658 if (!_tcsicmp(option, TEXT("e")))
659 _flags |= OWM_EXPLORE;
660 // option /root for rooted explorer windows
661 else if (!_tcsicmp(option, TEXT("root")))
662 _flags |= OWM_ROOTED;
663 // non-standard options: /mdi, /sdi
664 else if (!_tcsicmp(option, TEXT("mdi")))
665 _mdi = true;
666 else if (!_tcsicmp(option, TEXT("sdi")))
667 _mdi = false;
668 else
669 return false;
670 } else {
671 if (!_path.empty())
672 return false;
673
674 _path = opt_str;
675 }
676
677 return true;
678 }
679
680 bool ExplorerCmd::IsValidPath() const
681 {
682 if (!_path.empty()) {
683 DWORD attribs = GetFileAttributes(_path);
684
685 if (attribs!=INVALID_FILE_ATTRIBUTES && (attribs&FILE_ATTRIBUTE_DIRECTORY))
686 return true; // file system path
687 else if (*_path==':' && _path.at(1)==':')
688 return true; // text encoded IDL
689 }
690
691 return false;
692 }
693
694 #else
695
696 void explorer_show_frame(int cmdShow, LPTSTR lpCmdLine)
697 {
698 if (!lpCmdLine)
699 lpCmdLine = TEXT("explorer.exe");
700
701 launch_file(GetDesktopWindow(), lpCmdLine, cmdShow);
702 }
703
704 #endif
705
706
707 PopupMenu::PopupMenu(UINT nid)
708 {
709 HMENU hMenu = LoadMenu(g_Globals._hInstance, MAKEINTRESOURCE(nid));
710 _hmenu = GetSubMenu(hMenu, 0);
711 }
712
713
714 /// "About Explorer" Dialog
715 struct ExplorerAboutDlg : public
716 CtlColorParent<
717 OwnerDrawParent<Dialog>
718 >
719 {
720 typedef CtlColorParent<
721 OwnerDrawParent<Dialog>
722 > super;
723
724 ExplorerAboutDlg(HWND hwnd)
725 : super(hwnd)
726 {
727 SetWindowIcon(hwnd, IDI_REACTOS);
728
729 new FlatButton(hwnd, IDOK);
730
731 _hfont = CreateFont(20, 0, 0, 0, FW_BOLD, TRUE, 0, 0, 0, 0, 0, 0, 0, TEXT("Sans Serif"));
732 new ColorStatic(hwnd, IDC_ROS_EXPLORER, RGB(32,32,128), 0, _hfont);
733
734 new HyperlinkCtrl(hwnd, IDC_WWW);
735
736 FmtString ver_txt(ResString(IDS_EXPLORER_VERSION_STR), (LPCTSTR)ResString(IDS_VERSION_STR));
737 SetWindowText(GetDlgItem(hwnd, IDC_VERSION_TXT), ver_txt);
738
739 HWND hwnd_winver = GetDlgItem(hwnd, IDC_WIN_VERSION);
740 SetWindowText(hwnd_winver, get_windows_version_str());
741 SetWindowFont(hwnd_winver, GetStockFont(DEFAULT_GUI_FONT), FALSE);
742
743 CenterWindow(hwnd);
744 }
745
746 ~ExplorerAboutDlg()
747 {
748 DeleteObject(_hfont);
749 }
750
751 LRESULT WndProc(UINT nmsg, WPARAM wparam, LPARAM lparam)
752 {
753 switch(nmsg) {
754 case WM_PAINT:
755 Paint();
756 break;
757
758 default:
759 return super::WndProc(nmsg, wparam, lparam);
760 }
761
762 return 0;
763 }
764
765 void Paint()
766 {
767 PaintCanvas canvas(_hwnd);
768
769 HICON hicon = (HICON) LoadImage(g_Globals._hInstance, MAKEINTRESOURCE(IDI_REACTOS_BIG), IMAGE_ICON, 0, 0, LR_SHARED);
770
771 DrawIconEx(canvas, 20, 10, hicon, 0, 0, 0, 0, DI_NORMAL);
772 }
773
774 protected:
775 HFONT _hfont;
776 };
777
778 void explorer_about(HWND hwndParent)
779 {
780 Dialog::DoModal(IDD_ABOUT_EXPLORER, WINDOW_CREATOR(ExplorerAboutDlg), hwndParent);
781 }
782
783
784 static void InitInstance(HINSTANCE hInstance)
785 {
786 CONTEXT("InitInstance");
787
788 setlocale(LC_COLLATE, ""); // set collating rules to local settings for compareName
789
790 #ifndef ROSSHELL
791 // register frame window class
792 g_Globals._hframeClass = IconWindowClass(CLASSNAME_FRAME,IDI_EXPLORER);
793
794 // register child window class
795 WindowClass(CLASSNAME_CHILDWND, CS_CLASSDC|CS_DBLCLKS|CS_VREDRAW).Register();
796
797 // register tree window class
798 WindowClass(CLASSNAME_WINEFILETREE, CS_CLASSDC|CS_DBLCLKS|CS_VREDRAW).Register();
799 #endif
800
801 g_Globals._cfStrFName = RegisterClipboardFormat(CFSTR_FILENAME);
802 }
803
804
805 int explorer_main(HINSTANCE hInstance, LPTSTR lpCmdLine, int cmdShow)
806 {
807 CONTEXT("explorer_main");
808
809 // initialize Common Controls library
810 CommonControlInit usingCmnCtrl;
811
812 try {
813 InitInstance(hInstance);
814 } catch(COMException& e) {
815 HandleException(e, GetDesktopWindow());
816 return -1;
817 }
818
819 #ifndef ROSSHELL
820 if (cmdShow != SW_HIDE) {
821 /* // don't maximize if being called from the ROS desktop
822 if (cmdShow == SW_SHOWNORMAL)
823 ///@todo read window placement from registry
824 cmdShow = SW_MAXIMIZE;
825 */
826
827 explorer_show_frame(cmdShow, lpCmdLine);
828 }
829 #endif
830
831 return Window::MessageLoop();
832 }
833
834
835 // MinGW does not provide a Unicode startup routine, so we have to implement an own.
836 #if defined(__MINGW32__) && defined(UNICODE)
837
838 #define _tWinMain wWinMain
839 int WINAPI wWinMain(HINSTANCE, HINSTANCE, LPWSTR, int);
840
841 int main(int argc, char* argv[])
842 {
843 CONTEXT("main");
844
845 STARTUPINFO startupinfo;
846 int nShowCmd = SW_SHOWNORMAL;
847
848 GetStartupInfo(&startupinfo);
849
850 if (startupinfo.dwFlags & STARTF_USESHOWWINDOW)
851 nShowCmd = startupinfo.wShowWindow;
852
853 LPWSTR cmdline = GetCommandLineW();
854
855 while(*cmdline && !_istspace((unsigned)*cmdline))
856 ++cmdline;
857
858 while(_istspace((unsigned)*cmdline))
859 ++cmdline;
860
861 return wWinMain(GetModuleHandle(NULL), 0, cmdline, nShowCmd);
862 }
863
864 #endif // __MINGW && UNICODE
865
866
867 static bool SetShellReadyEvent(LPCTSTR evtName)
868 {
869 HANDLE hEvent = OpenEvent(EVENT_MODIFY_STATE, FALSE, evtName);
870 if (!hEvent)
871 return false;
872
873 SetEvent(hEvent);
874 CloseHandle(hEvent);
875
876 return true;
877 }
878
879
880 int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nShowCmd)
881 {
882 CONTEXT("WinMain()");
883
884 BOOL any_desktop_running = IsAnyDesktopRunning();
885
886 BOOL startup_desktop;
887
888 // strip extended options from the front of the command line
889 String ext_options;
890
891 while(*lpCmdLine == '-') {
892 while(*lpCmdLine && !_istspace((unsigned)*lpCmdLine))
893 ext_options += *lpCmdLine++;
894
895 while(_istspace((unsigned)*lpCmdLine))
896 ++lpCmdLine;
897 }
898
899 // command line option "-install" to replace previous shell application with ROS Explorer
900 if (_tcsstr(ext_options,TEXT("-install"))) {
901 // install ROS Explorer into the registry
902 TCHAR path[MAX_PATH];
903
904 int l = GetModuleFileName(0, path, MAX_PATH);
905 if (l) {
906 HKEY hkey;
907
908 if (!RegOpenKey(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon"), &hkey)) {
909
910 ///@todo save previous shell application in config file
911
912 RegSetValueEx(hkey, TEXT("Shell"), 0, REG_SZ, (LPBYTE)path, l*sizeof(TCHAR));
913 RegCloseKey(hkey);
914 }
915
916 if (!RegOpenKey(HKEY_CURRENT_USER, TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon"), &hkey)) {
917
918 ///@todo save previous shell application in config file
919
920 RegSetValueEx(hkey, TEXT("Shell"), 0, REG_SZ, (LPBYTE)TEXT(""), l*sizeof(TCHAR));
921 RegCloseKey(hkey);
922 }
923 }
924
925 HWND shellWindow = GetShellWindow();
926
927 if (shellWindow) {
928 DWORD pid;
929
930 // terminate shell process for NT like systems
931 GetWindowThreadProcessId(shellWindow, &pid);
932 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
933
934 // On Win 9x it's sufficient to destroy the shell window.
935 DestroyWindow(shellWindow);
936
937 if (TerminateProcess(hProcess, 0))
938 WaitForSingleObject(hProcess, INFINITE);
939
940 CloseHandle(hProcess);
941 }
942
943 startup_desktop = TRUE;
944 } else {
945 // create desktop window and task bar only, if there is no other shell and we are
946 // the first explorer instance
947 // MS Explorer looks additionally into the registry entry HKCU\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\shell,
948 // to decide wether it is currently configured as shell application.
949 startup_desktop = !any_desktop_running;
950 }
951
952
953 bool autostart = !any_desktop_running;
954
955 // disable autostart if the SHIFT key is pressed
956 if (GetAsyncKeyState(VK_SHIFT) < 0)
957 autostart = false;
958
959 #ifdef _DEBUG //MF: disabled for debugging
960 autostart = false;
961 #endif
962
963 // If there is given the command line option "-desktop", create desktop window anyways
964 if (_tcsstr(ext_options,TEXT("-desktop")))
965 startup_desktop = TRUE;
966 #ifndef ROSSHELL
967 else if (_tcsstr(ext_options,TEXT("-nodesktop")))
968 startup_desktop = FALSE;
969
970 // Don't display cabinet window in desktop mode
971 if (startup_desktop && !_tcsstr(ext_options,TEXT("-explorer")))
972 nShowCmd = SW_HIDE;
973 #endif
974
975 if (_tcsstr(ext_options,TEXT("-noautostart")))
976 autostart = false;
977 else if (_tcsstr(ext_options,TEXT("-autostart")))
978 autostart = true;
979
980 #ifndef __WINE__
981 if (_tcsstr(ext_options,TEXT("-console"))) {
982 AllocConsole();
983
984 _dup2(_open_osfhandle((long)GetStdHandle(STD_INPUT_HANDLE), _O_RDONLY), 0);
985 _dup2(_open_osfhandle((long)GetStdHandle(STD_OUTPUT_HANDLE), 0), 1);
986 _dup2(_open_osfhandle((long)GetStdHandle(STD_ERROR_HANDLE), 0), 2);
987
988 g_Globals._log = fdopen(1, "w");
989 setvbuf(g_Globals._log, 0, _IONBF, 0);
990
991 LOG(TEXT("starting explorer debug log\n"));
992 }
993 #endif
994
995
996 if (startup_desktop) {
997 // hide the XP login screen (Credit to Nicolas Escuder)
998 // another undocumented event: "Global\\msgina: ReturnToWelcome"
999 if (!SetShellReadyEvent(TEXT("msgina: ShellReadyEvent")))
1000 SetShellReadyEvent(TEXT("Global\\msgina: ShellReadyEvent"));
1001 }
1002 #ifdef ROSSHELL
1003 else
1004 return 0; // no shell to launch, so exit immediatelly
1005 #endif
1006
1007
1008 if (!any_desktop_running) {
1009 // launch the shell DDE server
1010 if (g_SHDOCVW_ShellDDEInit)
1011 (*g_SHDOCVW_ShellDDEInit)(TRUE);
1012 }
1013
1014
1015 bool use_gdb_stub = false; // !IsDebuggerPresent();
1016
1017 if (_tcsstr(ext_options,TEXT("-debug")))
1018 use_gdb_stub = true;
1019
1020 if (_tcsstr(ext_options,TEXT("-break"))) {
1021 LOG(TEXT("debugger breakpoint"));
1022 #ifdef _MSC_VER
1023 __asm int 3
1024 #else
1025 asm("int3");
1026 #endif
1027 }
1028
1029 // activate GDB remote debugging stub if no other debugger is running
1030 if (use_gdb_stub) {
1031 LOG(TEXT("waiting for debugger connection...\n"));
1032
1033 initialize_gdb_stub();
1034 }
1035
1036 g_Globals.init(hInstance);
1037
1038 // initialize COM and OLE before creating the desktop window
1039 OleInit usingCOM;
1040
1041 // init common controls library
1042 CommonControlInit usingCmnCtrl;
1043
1044 g_Globals.read_persistent();
1045
1046 if (startup_desktop) {
1047 WaitCursor wait;
1048
1049 g_Globals._desktops.init();
1050
1051 g_Globals._hwndDesktop = DesktopWindow::Create();
1052 #ifdef _USE_HDESK
1053 g_Globals._desktops.get_current_Desktop()->_hwndDesktop = g_Globals._hwndDesktop;
1054 #endif
1055 }
1056
1057 Thread* pSSOThread = NULL;
1058
1059 if (startup_desktop) {
1060 // launch SSO thread to allow message processing independent from the explorer main thread
1061 pSSOThread = new SSOThread;
1062 pSSOThread->Start();
1063 }
1064
1065 /**TODO launching autostart programs can be moved into a background thread. */
1066 if (autostart) {
1067 char* argv[] = {"", "s"}; // call startup routine in SESSION_START mode
1068 startup(2, argv);
1069 }
1070
1071 #ifndef ROSSHELL
1072 if (g_Globals._hwndDesktop)
1073 g_Globals._desktop_mode = true;
1074 #endif
1075
1076
1077 int ret = explorer_main(hInstance, lpCmdLine, nShowCmd);
1078
1079
1080 // write configuration file
1081 g_Globals.write_persistent();
1082
1083 if (pSSOThread) {
1084 pSSOThread->Stop();
1085 delete pSSOThread;
1086 }
1087
1088 if (!any_desktop_running) {
1089 // shutdown the shell DDE server
1090 if (g_SHDOCVW_ShellDDEInit)
1091 (*g_SHDOCVW_ShellDDEInit)(FALSE);
1092 }
1093
1094 return ret;
1095 }