Bletch <npwoods@mess.org>:
[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 #ifndef __WINE__ ///@todo _tcslwr() for Wine
186 _tcslwr(&ext.at(0));
187 #endif
188
189 iterator found = find(ext);
190 if (found != end())
191 return found->second;
192
193 FileTypeInfo& ftype = super::operator[](ext);
194
195 ftype._neverShowExt = false;
196
197 HKEY hkey;
198 TCHAR value[MAX_PATH], display_name[MAX_PATH];
199 LONG valuelen = sizeof(value);
200
201 if (!RegQueryValue(HKEY_CLASSES_ROOT, ext, value, &valuelen)) {
202 ftype._classname = value;
203
204 valuelen = sizeof(display_name);
205 if (!RegQueryValue(HKEY_CLASSES_ROOT, ftype._classname, display_name, &valuelen))
206 ftype._displayname = display_name;
207
208 if (!RegOpenKey(HKEY_CLASSES_ROOT, ftype._classname, &hkey)) {
209 if (!RegQueryValueEx(hkey, TEXT("NeverShowExt"), 0, NULL, NULL, NULL))
210 ftype._neverShowExt = true;
211
212 RegCloseKey(hkey);
213 }
214 }
215
216 return ftype;
217 }
218
219 LPCTSTR FileTypeManager::set_type(Entry* entry, bool dont_hide_ext)
220 {
221 LPCTSTR ext = _tcsrchr(entry->_data.cFileName, TEXT('.'));
222
223 if (ext) {
224 const FileTypeInfo& type = (*this)[ext];
225
226 if (!type._displayname.empty())
227 entry->_type_name = _tcsdup(type._displayname);
228
229 // hide some file extensions
230 if (type._neverShowExt && !dont_hide_ext) {
231 int len = ext - entry->_data.cFileName;
232 entry->_display_name = (LPTSTR) malloc((len+1)*sizeof(TCHAR));
233 lstrcpyn(entry->_display_name, entry->_data.cFileName, len + 1);
234 }
235
236 if (is_exe_file(ext))
237 entry->_data.dwFileAttributes |= ATTRIBUTE_EXECUTABLE;
238 }
239
240 return ext;
241 }
242
243
244 Icon::Icon()
245 : _id(ICID_UNKNOWN),
246 _itype(IT_STATIC),
247 _hicon(0)
248 {
249 }
250
251 Icon::Icon(ICON_ID id, UINT nid)
252 : _id(id),
253 _itype(IT_STATIC),
254 _hicon(SmallIcon(nid))
255 {
256 }
257
258 Icon::Icon(ICON_TYPE itype, int id, HICON hIcon)
259 : _id((ICON_ID)id),
260 _itype(itype),
261 _hicon(hIcon)
262 {
263 }
264
265 Icon::Icon(ICON_TYPE itype, int id, int sys_idx)
266 : _id((ICON_ID)id),
267 _itype(itype),
268 _sys_idx(sys_idx)
269 {
270 }
271
272 void Icon::draw(HDC hdc, int x, int y, int cx, int cy, COLORREF bk_color, HBRUSH bk_brush) const
273 {
274 if (_itype == IT_SYSCACHE)
275 ImageList_DrawEx(g_Globals._icon_cache.get_sys_imagelist(), _sys_idx, hdc, x, y, cx, cy, bk_color, CLR_DEFAULT, ILD_NORMAL);
276 else
277 DrawIconEx(hdc, x, y, _hicon, cx, cy, 0, bk_brush, DI_NORMAL);
278 }
279
280 HBITMAP Icon::create_bitmap(COLORREF bk_color, HBRUSH hbrBkgnd, HDC hdc_wnd) const
281 {
282 if (_itype == IT_SYSCACHE) {
283 HIMAGELIST himl = g_Globals._icon_cache.get_sys_imagelist();
284
285 int cx, cy;
286 ImageList_GetIconSize(himl, &cx, &cy);
287
288 HBITMAP hbmp = CreateCompatibleBitmap(hdc_wnd, cx, cy);
289 HDC hdc = CreateCompatibleDC(hdc_wnd);
290 HBITMAP hbmp_old = SelectBitmap(hdc, hbmp);
291 ImageList_DrawEx(himl, _sys_idx, hdc, 0, 0, cx, cy, bk_color, CLR_DEFAULT, ILD_NORMAL);
292 SelectBitmap(hdc, hbmp_old);
293 DeleteDC(hdc);
294
295 return hbmp;
296 } else
297 return create_bitmap_from_icon(_hicon, hbrBkgnd, hdc_wnd);
298 }
299
300
301 int Icon::add_to_imagelist(HIMAGELIST himl, HDC hdc_wnd, COLORREF bk_color, HBRUSH bk_brush) const
302 {
303 int ret;
304
305 if (_itype == IT_SYSCACHE) {
306 HIMAGELIST himl = g_Globals._icon_cache.get_sys_imagelist();
307
308 int cx, cy;
309 ImageList_GetIconSize(himl, &cx, &cy);
310
311 HBITMAP hbmp = CreateCompatibleBitmap(hdc_wnd, cx, cy);
312 HDC hdc = CreateCompatibleDC(hdc_wnd);
313 HBITMAP hbmp_old = SelectBitmap(hdc, hbmp);
314 ImageList_DrawEx(himl, _sys_idx, hdc, 0, 0, cx, cy, bk_color, CLR_DEFAULT, ILD_NORMAL);
315 SelectBitmap(hdc, hbmp_old);
316 DeleteDC(hdc);
317
318 ret = ImageList_Add(himl, hbmp, 0);
319
320 DeleteObject(hbmp);
321 } else
322 ret = ImageList_AddAlphaIcon(himl, _hicon, bk_brush, hdc_wnd);
323
324 return ret;
325 }
326
327 HBITMAP create_bitmap_from_icon(HICON hIcon, HBRUSH hbrush_bkgnd, HDC hdc_wnd)
328 {
329 int cx = GetSystemMetrics(SM_CXSMICON);
330 int cy = GetSystemMetrics(SM_CYSMICON);
331 HBITMAP hbmp = CreateCompatibleBitmap(hdc_wnd, cx, cy);
332
333 MemCanvas canvas;
334 BitmapSelection sel(canvas, hbmp);
335
336 RECT rect = {0, 0, cx, cy};
337 FillRect(canvas, &rect, hbrush_bkgnd);
338
339 DrawIconEx(canvas, 0, 0, hIcon, cx, cy, 0, hbrush_bkgnd, DI_NORMAL);
340
341 return hbmp;
342 }
343
344 int ImageList_AddAlphaIcon(HIMAGELIST himl, HICON hIcon, HBRUSH hbrush_bkgnd, HDC hdc_wnd)
345 {
346 HBITMAP hbmp = create_bitmap_from_icon(hIcon, hbrush_bkgnd, hdc_wnd);
347
348 int ret = ImageList_Add(himl, hbmp, 0);
349
350 DeleteObject(hbmp);
351
352 return ret;
353 }
354
355
356 int IconCache::s_next_id = ICID_DYNAMIC;
357
358
359 void IconCache::init()
360 {
361 _icons[ICID_NONE] = Icon(IT_STATIC, ICID_NONE, (HICON)0);
362
363 _icons[ICID_FOLDER] = Icon(ICID_FOLDER, IDI_FOLDER);
364 //_icons[ICID_DOCUMENT] = Icon(ICID_DOCUMENT, IDI_DOCUMENT);
365 _icons[ICID_EXPLORER] = Icon(ICID_EXPLORER, IDI_EXPLORER);
366 _icons[ICID_APP] = Icon(ICID_APP, IDI_APPICON);
367
368 _icons[ICID_CONFIG] = Icon(ICID_CONFIG, IDI_CONFIG);
369 _icons[ICID_DOCUMENTS] = Icon(ICID_DOCUMENTS, IDI_DOCUMENTS);
370 _icons[ICID_FAVORITES] = Icon(ICID_FAVORITES, IDI_FAVORITES);
371 _icons[ICID_INFO] = Icon(ICID_INFO, IDI_INFO);
372 _icons[ICID_APPS] = Icon(ICID_APPS, IDI_APPS);
373 _icons[ICID_SEARCH] = Icon(ICID_SEARCH, IDI_SEARCH);
374 _icons[ICID_ACTION] = Icon(ICID_ACTION, IDI_ACTION);
375 _icons[ICID_SEARCH_DOC] = Icon(ICID_SEARCH_DOC, IDI_SEARCH_DOC);
376 _icons[ICID_PRINTER] = Icon(ICID_PRINTER, IDI_PRINTER);
377 _icons[ICID_NETWORK] = Icon(ICID_NETWORK, IDI_NETWORK);
378 _icons[ICID_COMPUTER] = Icon(ICID_COMPUTER, IDI_COMPUTER);
379 _icons[ICID_LOGOFF] = Icon(ICID_LOGOFF, IDI_LOGOFF);
380 _icons[ICID_BOOKMARK] = Icon(ICID_BOOKMARK, IDI_DOT_TRANS);
381 }
382
383
384 const Icon& IconCache::extract(const String& path)
385 {
386 PathMap::iterator found = _pathMap.find(path);
387
388 if (found != _pathMap.end())
389 return _icons[found->second];
390
391 SHFILEINFO sfi;
392
393 #if 1 // use system image list - the "search program dialog" needs it
394 HIMAGELIST himlSys = (HIMAGELIST) SHGetFileInfo(path, 0, &sfi, sizeof(sfi), SHGFI_SYSICONINDEX|SHGFI_SMALLICON);
395
396 if (himlSys) {
397 _himlSys = himlSys;
398
399 const Icon& icon = add(sfi.iIcon/*, IT_SYSCACHE*/);
400 #else
401 if (SHGetFileInfo(path, 0, &sfi, sizeof(sfi), SHGFI_ICON|SHGFI_SMALLICON)) {
402 const Icon& icon = add(sfi.hIcon, IT_CACHED);
403 #endif
404
405 ///@todo limit cache size
406 _pathMap[path] = icon;
407
408 return icon;
409 } else
410 return _icons[ICID_NONE];
411 }
412
413 const Icon& IconCache::extract(LPCTSTR path, int idx)
414 {
415 CachePair key(path, idx);
416
417 #ifndef __WINE__ ///@todo _tcslwr() for Wine
418 _tcslwr(&key.first.at(0));
419 #endif
420
421 PathIdxMap::iterator found = _pathIdxMap.find(key);
422
423 if (found != _pathIdxMap.end())
424 return _icons[found->second];
425
426 HICON hIcon;
427
428 if ((int)ExtractIconEx(path, idx, NULL, &hIcon, 1) > 0) {
429 const Icon& icon = add(hIcon, IT_CACHED);
430
431 _pathIdxMap[key] = icon;
432
433 return icon;
434 } else {
435
436 ///@todo retreive "http://.../favicon.ico" format icons
437
438 return _icons[ICID_NONE];
439 }
440 }
441
442 const Icon& IconCache::extract(IExtractIcon* pExtract, LPCTSTR path, int idx)
443 {
444 HICON hIconLarge = 0;
445 HICON hIcon;
446
447 HRESULT hr = pExtract->Extract(path, idx, &hIconLarge, &hIcon, MAKELONG(0/*GetSystemMetrics(SM_CXICON)*/,GetSystemMetrics(SM_CXSMICON)));
448
449 if (hr == NOERROR) { //@@ oder SUCCEEDED(hr) ?
450 if (hIconLarge)
451 DestroyIcon(hIconLarge);
452
453 if (hIcon)
454 return add(hIcon);
455 }
456
457 return _icons[ICID_NONE];
458 }
459
460 const Icon& IconCache::add(HICON hIcon, ICON_TYPE type)
461 {
462 int id = ++s_next_id;
463
464 return _icons[id] = Icon(type, id, hIcon);
465 }
466
467 const Icon& IconCache::add(int sys_idx/*, ICON_TYPE type=IT_SYSCACHE*/)
468 {
469 int id = ++s_next_id;
470
471 return _icons[id] = SysCacheIcon(id, sys_idx);
472 }
473
474 const Icon& IconCache::get_icon(int id)
475 {
476 return _icons[id];
477 }
478
479 void IconCache::free_icon(int icon_id)
480 {
481 IconMap::iterator found = _icons.find(icon_id);
482
483 if (found != _icons.end()) {
484 Icon& icon = found->second;
485
486 if (icon.destroy())
487 _icons.erase(found);
488 }
489 }
490
491
492 ResString::ResString(UINT nid)
493 {
494 TCHAR buffer[BUFFER_LEN];
495
496 int len = LoadString(g_Globals._hInstance, nid, buffer, sizeof(buffer)/sizeof(TCHAR));
497
498 super::assign(buffer, len);
499 }
500
501
502 ResIcon::ResIcon(UINT nid)
503 {
504 _hicon = LoadIcon(g_Globals._hInstance, MAKEINTRESOURCE(nid));
505 }
506
507 SmallIcon::SmallIcon(UINT nid)
508 {
509 _hicon = (HICON)LoadImage(g_Globals._hInstance, MAKEINTRESOURCE(nid), IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED);
510 }
511
512 ResIconEx::ResIconEx(UINT nid, int w, int h)
513 {
514 _hicon = (HICON)LoadImage(g_Globals._hInstance, MAKEINTRESOURCE(nid), IMAGE_ICON, w, h, LR_SHARED);
515 }
516
517
518 void SetWindowIcon(HWND hwnd, UINT nid)
519 {
520 HICON hIcon = ResIcon(nid);
521 (void)Window_SetIcon(hwnd, ICON_BIG, hIcon);
522
523 HICON hIconSmall = SmallIcon(nid);
524 (void)Window_SetIcon(hwnd, ICON_SMALL, hIconSmall);
525 }
526
527
528 ResBitmap::ResBitmap(UINT nid)
529 {
530 _hBmp = LoadBitmap(g_Globals._hInstance, MAKEINTRESOURCE(nid));
531 }
532
533
534 #ifndef ROSSHELL
535
536 void explorer_show_frame(int cmdshow, LPTSTR lpCmdLine)
537 {
538 if (g_Globals._hMainWnd) {
539 if (IsIconic(g_Globals._hMainWnd))
540 ShowWindow(g_Globals._hMainWnd, SW_RESTORE);
541 else
542 SetForegroundWindow(g_Globals._hMainWnd);
543
544 return;
545 }
546
547 g_Globals._prescan_nodes = false;
548
549 XMLPos explorer_options = g_Globals.get_cfg("general/explorer");
550 XS_String mdiStr = XMLString(explorer_options, "mdi");
551
552 if (mdiStr.empty())
553 Dialog::DoModal(IDD_MDI_SDI, WINDOW_CREATOR(MdiSdiDlg), g_Globals._hwndDesktop);
554
555 bool mdi = XMLBool(explorer_options, "mdi", true);
556
557 // create main window
558 MainFrameBase::Create(lpCmdLine, mdi, cmdshow);
559 }
560
561 #else
562
563 void explorer_show_frame(int cmdshow, LPTSTR lpCmdLine)
564 {
565 if (!lpCmdLine)
566 lpCmdLine = TEXT("explorer.exe");
567
568 launch_file(GetDesktopWindow(), lpCmdLine, cmdshow);
569 }
570
571 #endif
572
573
574 PopupMenu::PopupMenu(UINT nid)
575 {
576 HMENU hMenu = LoadMenu(g_Globals._hInstance, MAKEINTRESOURCE(nid));
577 _hmenu = GetSubMenu(hMenu, 0);
578 }
579
580
581 /// "About Explorer" Dialog
582 struct ExplorerAboutDlg : public
583 CtlColorParent<
584 OwnerDrawParent<Dialog>
585 >
586 {
587 typedef CtlColorParent<
588 OwnerDrawParent<Dialog>
589 > super;
590
591 ExplorerAboutDlg(HWND hwnd)
592 : super(hwnd)
593 {
594 SetWindowIcon(hwnd, IDI_REACTOS);
595
596 new FlatButton(hwnd, IDOK);
597
598 _hfont = CreateFont(20, 0, 0, 0, FW_BOLD, TRUE, 0, 0, 0, 0, 0, 0, 0, TEXT("Sans Serif"));
599 new ColorStatic(hwnd, IDC_ROS_EXPLORER, RGB(32,32,128), 0, _hfont);
600
601 new HyperlinkCtrl(hwnd, IDC_WWW);
602
603 FmtString ver_txt(ResString(IDS_EXPLORER_VERSION_STR), (LPCTSTR)ResString(IDS_VERSION_STR));
604 SetWindowText(GetDlgItem(hwnd, IDC_VERSION_TXT), ver_txt);
605
606 HWND hwnd_winver = GetDlgItem(hwnd, IDC_WIN_VERSION);
607 SetWindowText(hwnd_winver, get_windows_version_str());
608 SetWindowFont(hwnd_winver, GetStockFont(DEFAULT_GUI_FONT), FALSE);
609
610 CenterWindow(hwnd);
611 }
612
613 ~ExplorerAboutDlg()
614 {
615 DeleteObject(_hfont);
616 }
617
618 LRESULT WndProc(UINT nmsg, WPARAM wparam, LPARAM lparam)
619 {
620 switch(nmsg) {
621 case WM_PAINT:
622 Paint();
623 break;
624
625 default:
626 return super::WndProc(nmsg, wparam, lparam);
627 }
628
629 return 0;
630 }
631
632 void Paint()
633 {
634 PaintCanvas canvas(_hwnd);
635
636 HICON hicon = (HICON) LoadImage(g_Globals._hInstance, MAKEINTRESOURCE(IDI_REACTOS_BIG), IMAGE_ICON, 0, 0, LR_SHARED);
637
638 DrawIconEx(canvas, 20, 10, hicon, 0, 0, 0, 0, DI_NORMAL);
639 }
640
641 protected:
642 HFONT _hfont;
643 };
644
645 void explorer_about(HWND hwndParent)
646 {
647 Dialog::DoModal(IDD_ABOUT_EXPLORER, WINDOW_CREATOR(ExplorerAboutDlg), hwndParent);
648 }
649
650
651 static void InitInstance(HINSTANCE hInstance)
652 {
653 CONTEXT("InitInstance");
654
655 setlocale(LC_COLLATE, ""); // set collating rules to local settings for compareName
656
657 #ifndef ROSSHELL
658 // register frame window class
659 g_Globals._hframeClass = IconWindowClass(CLASSNAME_FRAME,IDI_EXPLORER);
660
661 // register child window class
662 WindowClass(CLASSNAME_CHILDWND, CS_CLASSDC|CS_DBLCLKS|CS_VREDRAW).Register();
663
664 // register tree window class
665 WindowClass(CLASSNAME_WINEFILETREE, CS_CLASSDC|CS_DBLCLKS|CS_VREDRAW).Register();
666 #endif
667
668 g_Globals._cfStrFName = RegisterClipboardFormat(CFSTR_FILENAME);
669 }
670
671
672 int explorer_main(HINSTANCE hInstance, LPTSTR lpCmdLine, int cmdshow)
673 {
674 CONTEXT("explorer_main");
675
676 // initialize Common Controls library
677 CommonControlInit usingCmnCtrl;
678
679 try {
680 InitInstance(hInstance);
681 } catch(COMException& e) {
682 HandleException(e, GetDesktopWindow());
683 return -1;
684 }
685
686 #ifndef ROSSHELL
687 if (cmdshow != SW_HIDE) {
688 /* // don't maximize if being called from the ROS desktop
689 if (cmdshow == SW_SHOWNORMAL)
690 ///@todo read window placement from registry
691 cmdshow = SW_MAXIMIZE;
692 */
693
694 explorer_show_frame(cmdshow, lpCmdLine);
695 }
696 #endif
697
698 return Window::MessageLoop();
699 }
700
701
702 // MinGW does not provide a Unicode startup routine, so we have to implement an own.
703 #if defined(__MINGW32__) && defined(UNICODE)
704
705 #define _tWinMain wWinMain
706 int WINAPI wWinMain(HINSTANCE, HINSTANCE, LPWSTR, int);
707
708 int main(int argc, char* argv[])
709 {
710 CONTEXT("main");
711
712 STARTUPINFO startupinfo;
713 int nShowCmd = SW_SHOWNORMAL;
714
715 GetStartupInfo(&startupinfo);
716
717 if (startupinfo.dwFlags & STARTF_USESHOWWINDOW)
718 nShowCmd = startupinfo.wShowWindow;
719
720 LPWSTR cmdline = GetCommandLineW();
721
722 while(*cmdline && !_istspace(*cmdline))
723 ++cmdline;
724
725 while(_istspace(*cmdline))
726 ++cmdline;
727
728 return wWinMain(GetModuleHandle(NULL), 0, cmdline, nShowCmd);
729 }
730
731 #endif // __MINGW && UNICODE
732
733
734 static bool SetShellReadyEvent(LPCTSTR evtName)
735 {
736 HANDLE hEvent = OpenEvent(EVENT_MODIFY_STATE, FALSE, evtName);
737 if (!hEvent)
738 return false;
739
740 SetEvent(hEvent);
741 CloseHandle(hEvent);
742
743 return true;
744 }
745
746
747 int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nShowCmd)
748 {
749 CONTEXT("WinMain()");
750
751 BOOL any_desktop_running = IsAnyDesktopRunning();
752
753 BOOL startup_desktop;
754
755 // command line option "-install" to replace previous shell application with ROS Explorer
756 if (_tcsstr(lpCmdLine,TEXT("-install"))) {
757 // install ROS Explorer into the registry
758 TCHAR path[MAX_PATH];
759
760 int l = GetModuleFileName(0, path, MAX_PATH);
761 if (l) {
762 HKEY hkey;
763
764 if (!RegOpenKey(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon"), &hkey)) {
765
766 ///@todo save previous shell application in config file
767
768 RegSetValueEx(hkey, TEXT("Shell"), 0, REG_SZ, (LPBYTE)path, l*sizeof(TCHAR));
769 RegCloseKey(hkey);
770 }
771
772 if (!RegOpenKey(HKEY_CURRENT_USER, TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon"), &hkey)) {
773
774 ///@todo save previous shell application in config file
775
776 RegSetValueEx(hkey, TEXT("Shell"), 0, REG_SZ, (LPBYTE)TEXT(""), l*sizeof(TCHAR));
777 RegCloseKey(hkey);
778 }
779 }
780
781 HWND shellWindow = GetShellWindow();
782
783 if (shellWindow) {
784 DWORD pid;
785
786 // terminate shell process for NT like systems
787 GetWindowThreadProcessId(shellWindow, &pid);
788 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
789
790 // On Win 9x it's sufficient to destroy the shell window.
791 DestroyWindow(shellWindow);
792
793 if (TerminateProcess(hProcess, 0))
794 WaitForSingleObject(hProcess, INFINITE);
795
796 CloseHandle(hProcess);
797 }
798
799 startup_desktop = TRUE;
800 } else {
801 // create desktop window and task bar only, if there is no other shell and we are
802 // the first explorer instance
803 // MS Explorer looks additionally into the registry entry HKCU\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\shell,
804 // to decide wether it is currently configured as shell application.
805 startup_desktop = !any_desktop_running;
806 }
807
808
809 bool autostart = !any_desktop_running;
810
811 // disable autostart if the SHIFT key is pressed
812 if (GetAsyncKeyState(VK_SHIFT) < 0)
813 autostart = false;
814
815 #ifdef _DEBUG //MF: disabled for debugging
816 autostart = false;
817 #endif
818
819 // If there is given the command line option "-desktop", create desktop window anyways
820 if (_tcsstr(lpCmdLine,TEXT("-desktop")))
821 startup_desktop = TRUE;
822 #ifndef ROSSHELL
823 else if (_tcsstr(lpCmdLine,TEXT("-nodesktop")))
824 startup_desktop = FALSE;
825
826 // Don't display cabinet window in desktop mode
827 if (startup_desktop && !_tcsstr(lpCmdLine,TEXT("-explorer")))
828 nShowCmd = SW_HIDE;
829 #endif
830
831 if (_tcsstr(lpCmdLine,TEXT("-noautostart")))
832 autostart = false;
833 else if (_tcsstr(lpCmdLine,TEXT("-autostart")))
834 autostart = true;
835
836 #ifndef __WINE__
837 if (_tcsstr(lpCmdLine,TEXT("-console"))) {
838 AllocConsole();
839
840 _dup2(_open_osfhandle((long)GetStdHandle(STD_INPUT_HANDLE), _O_RDONLY), 0);
841 _dup2(_open_osfhandle((long)GetStdHandle(STD_OUTPUT_HANDLE), 0), 1);
842 _dup2(_open_osfhandle((long)GetStdHandle(STD_ERROR_HANDLE), 0), 2);
843
844 g_Globals._log = fdopen(1, "w");
845 setvbuf(g_Globals._log, 0, _IONBF, 0);
846
847 LOG(TEXT("starting explorer debug log\n"));
848 }
849 #endif
850
851
852 if (startup_desktop) {
853 // hide the XP login screen (Credit to Nicolas Escuder)
854 // another undocumented event: "Global\\msgina: ReturnToWelcome"
855 if (!SetShellReadyEvent(TEXT("msgina: ShellReadyEvent")))
856 SetShellReadyEvent(TEXT("Global\\msgina: ShellReadyEvent"));
857 }
858 #ifdef ROSSHELL
859 else
860 return 0; // no shell to launch, so exit immediatelly
861 #endif
862
863
864 if (!any_desktop_running) {
865 // launch the shell DDE server
866 if (g_SHDOCVW_ShellDDEInit)
867 (*g_SHDOCVW_ShellDDEInit)(TRUE);
868 }
869
870
871 bool use_gdb_stub = false; // !IsDebuggerPresent();
872
873 if (_tcsstr(lpCmdLine,TEXT("-debug")))
874 use_gdb_stub = true;
875
876 if (_tcsstr(lpCmdLine,TEXT("-break"))) {
877 LOG(TEXT("debugger breakpoint"));
878 #ifdef _MSC_VER
879 __asm int 3
880 #else
881 asm("int3");
882 #endif
883 }
884
885 // activate GDB remote debugging stub if no other debugger is running
886 if (use_gdb_stub) {
887 LOG(TEXT("waiting for debugger connection...\n"));
888
889 initialize_gdb_stub();
890 }
891
892 g_Globals.init(hInstance);
893
894 // initialize COM and OLE before creating the desktop window
895 OleInit usingCOM;
896
897 // init common controls library
898 CommonControlInit usingCmnCtrl;
899
900 g_Globals.read_persistent();
901
902 if (startup_desktop) {
903 WaitCursor wait;
904
905 g_Globals._desktops.init();
906
907 g_Globals._hwndDesktop = DesktopWindow::Create();
908 #ifdef _USE_HDESK
909 g_Globals._desktops.get_current_Desktop()->_hwndDesktop = g_Globals._hwndDesktop;
910 #endif
911 }
912
913 Thread* pSSOThread = NULL;
914
915 if (startup_desktop) {
916 // launch SSO thread to allow message processing independent from the explorer main thread
917 pSSOThread = new SSOThread;
918 pSSOThread->Start();
919 }
920
921 /**TODO launching autostart programs can be moved into a background thread. */
922 if (autostart) {
923 char* argv[] = {"", "s"}; // call startup routine in SESSION_START mode
924 startup(2, argv);
925 }
926
927 #ifndef ROSSHELL
928 if (g_Globals._hwndDesktop)
929 g_Globals._desktop_mode = true;
930
931 /**TODO fix command line handling */
932 if (*lpCmdLine=='"' && lpCmdLine[_tcslen(lpCmdLine)-1]=='"') {
933 ++lpCmdLine;
934 lpCmdLine[_tcslen(lpCmdLine)-1] = '\0';
935 }
936 #endif
937
938
939 int ret = explorer_main(hInstance, lpCmdLine, nShowCmd);
940
941 // write configuration file
942 g_Globals.write_persistent();
943
944 if (pSSOThread) {
945 pSSOThread->Stop();
946 delete pSSOThread;
947 }
948
949 if (!any_desktop_running) {
950 // shutdown the shell DDE server
951 if (g_SHDOCVW_ShellDDEInit)
952 (*g_SHDOCVW_ShellDDEInit)(FALSE);
953 }
954
955 return ret;
956 }