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