display version information in about dialog and on the desktop
[reactos.git] / reactos / subsys / system / explorer / explorer.cpp
1 /*
2 * Copyright 2003, 2004 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 "explorer_intres.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
43 extern "C" int initialize_gdb_stub(); // start up GDB stub
44
45
46 ExplorerGlobals g_Globals;
47
48
49 ExplorerGlobals::ExplorerGlobals()
50 {
51 _hInstance = 0;
52 _hframeClass = 0;
53 _cfStrFName = 0;
54 _hMainWnd = 0;
55 _prescan_nodes = false;
56 _desktop_mode = false;
57 _log = NULL;
58 #ifndef __MINGW32__ // SHRestricted() missing in MinGW (as of 29.10.2003)
59 _SHRestricted = 0;
60 #endif
61 _hwndDesktopBar = 0;
62 _hwndShellView = 0;
63 _hwndDesktop = 0;
64 }
65
66
67 void ExplorerGlobals::init(HINSTANCE hInstance)
68 {
69 _hInstance = hInstance;
70
71 #ifndef __MINGW32__ // SHRestricted() missing in MinGW (as of 29.10.2003)
72 _SHRestricted = (DWORD(STDAPICALLTYPE*)(RESTRICTIONS)) GetProcAddress(GetModuleHandle(TEXT("SHELL32")), "SHRestricted");
73 #endif
74
75 _icon_cache.init();
76 }
77
78
79 void ExplorerGlobals::read_persistent()
80 {
81 // read configuration file
82 _cfg_dir.printf(TEXT("%s\\ReactOS"), (LPCTSTR)SpecialFolderFSPath(CSIDL_APPDATA,0));
83 _cfg_path.printf(TEXT("%s\\ros-explorer-cfg.xml"), _cfg_dir.c_str());
84
85 if (!_cfg.read(_cfg_path)) {
86 if (_cfg._last_error != XML_ERROR_NO_ELEMENTS)
87 MessageBox(g_Globals._hwndDesktop, String(_cfg._last_error_msg.c_str()),
88 TEXT("ROS Explorer - reading user settings"), MB_OK);
89
90 _cfg.read(TEXT("explorer-cfg-template.xml"));
91 }
92
93 // read bookmarks
94 _favorites_path.printf(TEXT("%s\\ros-explorer-bookmarks.xml"), _cfg_dir.c_str());
95
96 if (!_favorites.read(_favorites_path)) {
97 _favorites.import_IE_favorites(0);
98 _favorites.write(_favorites_path);
99 }
100 }
101
102 void ExplorerGlobals::write_persistent()
103 {
104 // write configuration file
105 RecursiveCreateDirectory(_cfg_dir);
106
107 _cfg.write(_cfg_path);
108 _favorites.write(_favorites_path);
109 }
110
111
112 XMLPos ExplorerGlobals::get_cfg()
113 {
114 XMLPos pos(&_cfg);
115
116 pos.smart_create("explorer-cfg");
117
118 return pos;
119 }
120
121 XMLPos ExplorerGlobals::get_cfg(const String& name)
122 {
123 XMLPos pos(&_cfg);
124
125 pos.smart_create("explorer-cfg");
126 pos.smart_create(name);
127
128 return pos;
129 }
130
131
132 void _log_(LPCTSTR txt)
133 {
134 FmtString msg(TEXT("%s\n"), txt);
135
136 if (g_Globals._log)
137 _fputts(msg, g_Globals._log);
138
139 OutputDebugString(msg);
140 }
141
142
143 bool FileTypeManager::is_exe_file(LPCTSTR ext)
144 {
145 static const LPCTSTR s_executable_extensions[] = {
146 TEXT("COM"),
147 TEXT("EXE"),
148 TEXT("BAT"),
149 TEXT("CMD"),
150 TEXT("CMM"),
151 TEXT("BTM"),
152 TEXT("AWK"),
153 0
154 };
155
156 TCHAR ext_buffer[_MAX_EXT];
157 const LPCTSTR* p;
158 LPCTSTR s;
159 LPTSTR d;
160
161 for(s=ext+1,d=ext_buffer; (*d=toupper(*s)); s++)
162 ++d;
163
164 for(p=s_executable_extensions; *p; p++)
165 if (!lstrcmp(ext_buffer, *p))
166 return true;
167
168 return false;
169 }
170
171
172 const FileTypeInfo& FileTypeManager::operator[](String ext)
173 {
174 #ifndef __WINE__ ///@todo _tcslwr() for Wine
175 _tcslwr((LPTSTR)ext.c_str());
176 #endif
177
178 iterator found = find(ext);
179 if (found != end())
180 return found->second;
181
182 FileTypeInfo& ftype = super::operator[](ext);
183
184 ftype._neverShowExt = false;
185
186 HKEY hkey;
187 TCHAR value[MAX_PATH], display_name[MAX_PATH];
188 LONG valuelen = sizeof(value);
189
190 if (!RegQueryValue(HKEY_CLASSES_ROOT, ext, value, &valuelen)) {
191 ftype._classname = value;
192
193 valuelen = sizeof(display_name);
194 if (!RegQueryValue(HKEY_CLASSES_ROOT, ftype._classname, display_name, &valuelen))
195 ftype._displayname = display_name;
196
197 if (!RegOpenKey(HKEY_CLASSES_ROOT, ftype._classname, &hkey)) {
198 if (!RegQueryValueEx(hkey, TEXT("NeverShowExt"), 0, NULL, NULL, NULL))
199 ftype._neverShowExt = true;
200
201 RegCloseKey(hkey);
202 }
203 }
204
205 return ftype;
206 }
207
208 LPCTSTR FileTypeManager::set_type(Entry* entry, bool dont_hide_ext)
209 {
210 LPCTSTR ext = _tcsrchr(entry->_data.cFileName, TEXT('.'));
211
212 if (ext) {
213 const FileTypeInfo& type = (*this)[ext];
214
215 if (!type._displayname.empty())
216 entry->_type_name = _tcsdup(type._displayname);
217
218 // hide some file extensions
219 if (type._neverShowExt && !dont_hide_ext) {
220 int len = ext - entry->_data.cFileName;
221 entry->_display_name = (LPTSTR) malloc((len+1)*sizeof(TCHAR));
222 _tcsncpy(entry->_display_name, entry->_data.cFileName, len);
223 entry->_display_name[len] = TEXT('\0');
224 }
225
226 if (is_exe_file(ext))
227 entry->_data.dwFileAttributes |= ATTRIBUTE_EXECUTABLE;
228 }
229
230 return ext;
231 }
232
233
234 Icon::Icon()
235 : _id(ICID_UNKNOWN),
236 _itype(IT_STATIC),
237 _hicon(0)
238 {
239 }
240
241 Icon::Icon(ICON_ID id, UINT nid)
242 : _id(id),
243 _itype(IT_STATIC),
244 _hicon(SmallIcon(nid))
245 {
246 }
247
248 Icon::Icon(ICON_TYPE itype, int id, HICON hIcon)
249 : _id((ICON_ID)id),
250 _itype(itype),
251 _hicon(hIcon)
252 {
253 }
254
255 Icon::Icon(ICON_TYPE itype, int id, int sys_idx)
256 : _id((ICON_ID)id),
257 _itype(itype),
258 _sys_idx(sys_idx)
259 {
260 }
261
262 void Icon::draw(HDC hdc, int x, int y, int cx, int cy, COLORREF bk_color, HBRUSH bk_brush) const
263 {
264 if (_itype == IT_SYSCACHE)
265 ImageList_DrawEx(g_Globals._icon_cache.get_sys_imagelist(), _sys_idx, hdc, x, y, cx, cy, bk_color, CLR_DEFAULT, ILD_NORMAL);
266 else
267 DrawIconEx(hdc, x, y, _hicon, cx, cy, 0, bk_brush, DI_NORMAL);
268 }
269
270 HBITMAP Icon::create_bitmap(COLORREF bk_color, HBRUSH hbrBkgnd, HDC hdc_wnd) const
271 {
272 if (_itype == IT_SYSCACHE) {
273 HIMAGELIST himl = g_Globals._icon_cache.get_sys_imagelist();
274
275 int cx, cy;
276 ImageList_GetIconSize(himl, &cx, &cy);
277
278 HBITMAP hbmp = CreateCompatibleBitmap(hdc_wnd, cx, cy);
279 HDC hdc = CreateCompatibleDC(hdc_wnd);
280 HBITMAP hbmp_old = SelectBitmap(hdc, hbmp);
281 ImageList_DrawEx(himl, _sys_idx, hdc, 0, 0, cx, cy, bk_color, CLR_DEFAULT, ILD_NORMAL);
282 SelectBitmap(hdc, hbmp_old);
283 DeleteDC(hdc);
284
285 return hbmp;
286 } else
287 return create_bitmap_from_icon(_hicon, hbrBkgnd, hdc_wnd);
288 }
289
290
291 int Icon::add_to_imagelist(HIMAGELIST himl, HDC hdc_wnd, COLORREF bk_color, HBRUSH bk_brush) const
292 {
293 int ret;
294
295 if (_itype == IT_SYSCACHE) {
296 HIMAGELIST himl = g_Globals._icon_cache.get_sys_imagelist();
297
298 int cx, cy;
299 ImageList_GetIconSize(himl, &cx, &cy);
300
301 HBITMAP hbmp = CreateCompatibleBitmap(hdc_wnd, cx, cy);
302 HDC hdc = CreateCompatibleDC(hdc_wnd);
303 HBITMAP hbmp_old = SelectBitmap(hdc, hbmp);
304 ImageList_DrawEx(himl, _sys_idx, hdc, 0, 0, cx, cy, bk_color, CLR_DEFAULT, ILD_NORMAL);
305 SelectBitmap(hdc, hbmp_old);
306 DeleteDC(hdc);
307
308 ret = ImageList_Add(himl, hbmp, 0);
309
310 DeleteObject(hbmp);
311 } else
312 ret = ImageList_AddAlphaIcon(himl, _hicon, bk_brush, hdc_wnd);
313
314 return ret;
315 }
316
317 HBITMAP create_bitmap_from_icon(HICON hIcon, HBRUSH hbrush_bkgnd, HDC hdc_wnd)
318 {
319 int cx = GetSystemMetrics(SM_CXSMICON);
320 int cy = GetSystemMetrics(SM_CYSMICON);
321 HBITMAP hbmp = CreateCompatibleBitmap(hdc_wnd, cx, cy);
322
323 MemCanvas canvas;
324 BitmapSelection sel(canvas, hbmp);
325
326 RECT rect = {0, 0, cx, cy};
327 FillRect(canvas, &rect, hbrush_bkgnd);
328
329 DrawIconEx(canvas, 0, 0, hIcon, cx, cy, 0, hbrush_bkgnd, DI_NORMAL);
330
331 return hbmp;
332 }
333
334 int ImageList_AddAlphaIcon(HIMAGELIST himl, HICON hIcon, HBRUSH hbrush_bkgnd, HDC hdc_wnd)
335 {
336 HBITMAP hbmp = create_bitmap_from_icon(hIcon, hbrush_bkgnd, hdc_wnd);
337
338 int ret = ImageList_Add(himl, hbmp, 0);
339
340 DeleteObject(hbmp);
341
342 return ret;
343 }
344
345
346 int IconCache::s_next_id = ICID_DYNAMIC;
347
348
349 void IconCache::init()
350 {
351 _icons[ICID_NONE] = Icon(IT_STATIC, ICID_NONE, (HICON)0);
352
353 _icons[ICID_FOLDER] = Icon(ICID_FOLDER, IDI_FOLDER);
354 //_icons[ICID_DOCUMENT] = Icon(ICID_DOCUMENT, IDI_DOCUMENT);
355 _icons[ICID_EXPLORER] = Icon(ICID_EXPLORER, IDI_EXPLORER);
356 _icons[ICID_APP] = Icon(ICID_APP, IDI_APPICON);
357
358 _icons[ICID_CONFIG] = Icon(ICID_CONFIG, IDI_CONFIG);
359 _icons[ICID_DOCUMENTS] = Icon(ICID_DOCUMENTS, IDI_DOCUMENTS);
360 _icons[ICID_FAVORITES] = Icon(ICID_FAVORITES, IDI_FAVORITES);
361 _icons[ICID_INFO] = Icon(ICID_INFO, IDI_INFO);
362 _icons[ICID_APPS] = Icon(ICID_APPS, IDI_APPS);
363 _icons[ICID_SEARCH] = Icon(ICID_SEARCH, IDI_SEARCH);
364 _icons[ICID_ACTION] = Icon(ICID_ACTION, IDI_ACTION);
365 _icons[ICID_SEARCH_DOC] = Icon(ICID_SEARCH_DOC, IDI_SEARCH_DOC);
366 _icons[ICID_PRINTER] = Icon(ICID_PRINTER, IDI_PRINTER);
367 _icons[ICID_NETWORK] = Icon(ICID_NETWORK, IDI_NETWORK);
368 _icons[ICID_COMPUTER] = Icon(ICID_COMPUTER, IDI_COMPUTER);
369 _icons[ICID_LOGOFF] = Icon(ICID_LOGOFF, IDI_LOGOFF);
370 _icons[ICID_BOOKMARK] = Icon(ICID_BOOKMARK, IDI_DOT_TRANS);
371 }
372
373
374 const Icon& IconCache::extract(const String& path)
375 {
376 PathMap::iterator found = _pathMap.find(path);
377
378 if (found != _pathMap.end())
379 return _icons[found->second];
380
381 SHFILEINFO sfi;
382
383 #if 1 // use system image list
384 HIMAGELIST himlSys = (HIMAGELIST) SHGetFileInfo(path, 0, &sfi, sizeof(sfi), SHGFI_SYSICONINDEX|SHGFI_SMALLICON);
385
386 if (himlSys) {
387 _himlSys = himlSys;
388
389 const Icon& icon = add(sfi.iIcon/*, IT_SYSCACHE*/);
390 #else
391 if (SHGetFileInfo(path, 0, &sfi, sizeof(sfi), SHGFI_ICON|SHGFI_SMALLICON)) {
392 const Icon& icon = add(sfi.hIcon, IT_CACHED);
393 #endif
394
395 ///@todo limit cache size
396 _pathMap[path] = icon;
397
398 return icon;
399 } else
400 return _icons[ICID_NONE];
401 }
402
403 const Icon& IconCache::extract(LPCTSTR path, int idx)
404 {
405 CachePair key(path, idx);
406
407 #ifndef __WINE__ ///@todo _tcslwr() for Wine
408 _tcslwr((LPTSTR)key.first.c_str());
409 #endif
410
411 PathIdxMap::iterator found = _pathIdxMap.find(key);
412
413 if (found != _pathIdxMap.end())
414 return _icons[found->second];
415
416 HICON hIcon;
417
418 if ((int)ExtractIconEx(path, idx, NULL, &hIcon, 1) > 0) {
419 const Icon& icon = add(hIcon, IT_CACHED);
420
421 _pathIdxMap[key] = icon;
422
423 return icon;
424 } else {
425
426 ///@todo retreive "http://.../favicon.ico" format icons
427
428 return _icons[ICID_NONE];
429 }
430 }
431
432 const Icon& IconCache::extract(IExtractIcon* pExtract, LPCTSTR path, int idx)
433 {
434 HICON hIconLarge = 0;
435 HICON hIcon;
436
437 HRESULT hr = pExtract->Extract(path, idx, &hIconLarge, &hIcon, MAKELONG(0/*GetSystemMetrics(SM_CXICON)*/,GetSystemMetrics(SM_CXSMICON)));
438
439 if (hr == NOERROR) {
440 if (hIconLarge)
441 DestroyIcon(hIconLarge);
442
443 if (hIcon)
444 return add(hIcon);
445 }
446
447 return _icons[ICID_NONE];
448 }
449
450 const Icon& IconCache::add(HICON hIcon, ICON_TYPE type)
451 {
452 int id = ++s_next_id;
453
454 return _icons[id] = Icon(type, id, hIcon);
455 }
456
457 const Icon& IconCache::add(int sys_idx/*, ICON_TYPE type=IT_SYSCACHE*/)
458 {
459 int id = ++s_next_id;
460
461 return _icons[id] = SysCacheIcon(id, sys_idx);
462 }
463
464 const Icon& IconCache::get_icon(int id)
465 {
466 return _icons[id];
467 }
468
469 void IconCache::free_icon(int icon_id)
470 {
471 IconMap::iterator found = _icons.find(icon_id);
472
473 if (found != _icons.end()) {
474 Icon& icon = found->second;
475
476 if (icon.destroy())
477 _icons.erase(found);
478 }
479 }
480
481
482 ResString::ResString(UINT nid)
483 {
484 TCHAR buffer[BUFFER_LEN];
485
486 int len = LoadString(g_Globals._hInstance, nid, buffer, sizeof(buffer)/sizeof(TCHAR));
487
488 super::assign(buffer, len);
489 }
490
491
492 ResIcon::ResIcon(UINT nid)
493 {
494 _hicon = LoadIcon(g_Globals._hInstance, MAKEINTRESOURCE(nid));
495 }
496
497 SmallIcon::SmallIcon(UINT nid)
498 {
499 _hicon = (HICON)LoadImage(g_Globals._hInstance, MAKEINTRESOURCE(nid), IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED);
500 }
501
502 ResIconEx::ResIconEx(UINT nid, int w, int h)
503 {
504 _hicon = (HICON)LoadImage(g_Globals._hInstance, MAKEINTRESOURCE(nid), IMAGE_ICON, w, h, LR_SHARED);
505 }
506
507
508 void SetWindowIcon(HWND hwnd, UINT nid)
509 {
510 HICON hIcon = ResIcon(nid);
511 Window_SetIcon(hwnd, ICON_BIG, hIcon);
512
513 HICON hIconSmall = SmallIcon(nid);
514 Window_SetIcon(hwnd, ICON_SMALL, hIconSmall);
515 }
516
517
518 ResBitmap::ResBitmap(UINT nid)
519 {
520 _hBmp = LoadBitmap(g_Globals._hInstance, MAKEINTRESOURCE(nid));
521 }
522
523
524 void explorer_show_frame(int cmdshow, LPTSTR lpCmdLine)
525 {
526 if (g_Globals._hMainWnd) {
527 if (IsIconic(g_Globals._hMainWnd))
528 ShowWindow(g_Globals._hMainWnd, SW_RESTORE);
529 else
530 SetForegroundWindow(g_Globals._hMainWnd);
531
532 return;
533 }
534
535 g_Globals._prescan_nodes = false;
536
537 // create main window
538 MainFrameBase::Create(lpCmdLine, true, cmdshow);
539 }
540
541
542 PopupMenu::PopupMenu(UINT nid)
543 {
544 HMENU hMenu = LoadMenu(g_Globals._hInstance, MAKEINTRESOURCE(nid));
545 _hmenu = GetSubMenu(hMenu, 0);
546 }
547
548
549 /// "About Explorer" Dialog
550 struct ExplorerAboutDlg : public
551 CtlColorParent<
552 OwnerDrawParent<Dialog>
553 >
554 {
555 typedef CtlColorParent<
556 OwnerDrawParent<Dialog>
557 > super;
558
559 ExplorerAboutDlg(HWND hwnd)
560 : super(hwnd)
561 {
562 SetWindowIcon(hwnd, IDI_REACTOS);
563
564 new FlatButton(hwnd, IDOK);
565
566 _hfont = CreateFont(20, 0, 0, 0, FW_BOLD, TRUE, 0, 0, 0, 0, 0, 0, 0, TEXT("Sans Serif"));
567 new ColorStatic(hwnd, IDC_ROS_EXPLORER, RGB(32,32,128), 0, _hfont);
568
569 new HyperlinkCtrl(hwnd, IDC_WWW);
570
571 FmtString ver_txt(ResString(IDS_EXPLORER_VERSION_STR), (LPCTSTR)ResString(IDS_VERSION_STR));
572 SetWindowText(GetDlgItem(hwnd, IDC_VERSION_TXT), ver_txt);
573
574 HWND hwnd_winver = GetDlgItem(hwnd, IDC_WIN_VERSION);
575 SetWindowText(hwnd_winver, get_windows_version_str());
576 SetWindowFont(hwnd_winver, GetStockFont(DEFAULT_GUI_FONT), FALSE);
577
578 CenterWindow(hwnd);
579 }
580
581 ~ExplorerAboutDlg()
582 {
583 DeleteObject(_hfont);
584 }
585
586 LRESULT WndProc(UINT nmsg, WPARAM wparam, LPARAM lparam)
587 {
588 switch(nmsg) {
589 case WM_PAINT:
590 Paint();
591 break;
592
593 default:
594 return super::WndProc(nmsg, wparam, lparam);
595 }
596
597 return 0;
598 }
599
600 void Paint()
601 {
602 PaintCanvas canvas(_hwnd);
603
604 HICON hicon = (HICON) LoadImage(g_Globals._hInstance, MAKEINTRESOURCE(IDI_REACTOS_BIG), IMAGE_ICON, 0, 0, LR_SHARED);
605
606 DrawIconEx(canvas, 20, 10, hicon, 0, 0, 0, 0, DI_NORMAL);
607 }
608
609 protected:
610 HFONT _hfont;
611 };
612
613 void explorer_about(HWND hwndParent)
614 {
615 Dialog::DoModal(IDD_ABOUT_EXPLORER, WINDOW_CREATOR(ExplorerAboutDlg), hwndParent);
616 }
617
618
619 static void InitInstance(HINSTANCE hInstance)
620 {
621 CONTEXT("InitInstance");
622
623 setlocale(LC_COLLATE, ""); // set collating rules to local settings for compareName
624
625 // register frame window class
626 g_Globals._hframeClass = IconWindowClass(CLASSNAME_FRAME,IDI_EXPLORER);
627
628 // register child windows class
629 WindowClass(CLASSNAME_CHILDWND, CS_CLASSDC|CS_DBLCLKS|CS_VREDRAW).Register();
630
631 // register tree windows class
632 WindowClass(CLASSNAME_WINEFILETREE, CS_CLASSDC|CS_DBLCLKS|CS_VREDRAW).Register();
633
634 g_Globals._cfStrFName = RegisterClipboardFormat(CFSTR_FILENAME);
635 }
636
637
638 int explorer_main(HINSTANCE hInstance, LPTSTR lpCmdLine, int cmdshow)
639 {
640 CONTEXT("explorer_main");
641
642 // initialize Common Controls library
643 CommonControlInit usingCmnCtrl;
644
645 try {
646 InitInstance(hInstance);
647 } catch(COMException& e) {
648 HandleException(e, GetDesktopWindow());
649 return -1;
650 }
651
652 if (cmdshow != SW_HIDE) {
653 /* // don't maximize if being called from the ROS desktop
654 if (cmdshow == SW_SHOWNORMAL)
655 ///@todo read window placement from registry
656 cmdshow = SW_MAXIMIZE;
657 */
658
659 explorer_show_frame(cmdshow, lpCmdLine);
660 }
661
662 return Window::MessageLoop();
663 }
664
665
666 // MinGW does not provide a Unicode startup routine, so we have to implement an own.
667 #if defined(__MINGW32__) && defined(UNICODE)
668
669 #define _tWinMain wWinMain
670 int WINAPI wWinMain(HINSTANCE, HINSTANCE, LPWSTR, int);
671
672 int main(int argc, char* argv[])
673 {
674 CONTEXT("main");
675
676 STARTUPINFO startupinfo;
677 int nShowCmd = SW_SHOWNORMAL;
678
679 GetStartupInfo(&startupinfo);
680
681 if (startupinfo.dwFlags & STARTF_USESHOWWINDOW)
682 nShowCmd = startupinfo.wShowWindow;
683
684 LPWSTR cmdline = GetCommandLineW();
685
686 while(*cmdline && !_istspace(*cmdline))
687 ++cmdline;
688
689 return wWinMain(GetModuleHandle(NULL), 0, cmdline, nShowCmd);
690 }
691
692 #endif // __MINGW && UNICODE
693
694
695 int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nShowCmd)
696 {
697 CONTEXT("WinMain()");
698
699 BOOL any_desktop_running = IsAnyDesktopRunning();
700
701 BOOL startup_desktop;
702
703 // command line option "-install" to replace previous shell application with ROS Explorer
704 if (_tcsstr(lpCmdLine,TEXT("-install"))) {
705 // install ROS Explorer into the registry
706 TCHAR path[MAX_PATH];
707
708 int l = GetModuleFileName(0, path, MAX_PATH);
709 if (l) {
710 HKEY hkey;
711
712 if (!RegOpenKey(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon"), &hkey)) {
713
714 ///@todo save previous shell application in config file
715
716 RegSetValueEx(hkey, TEXT("Shell"), 0, REG_SZ, (LPBYTE)path, l*sizeof(TCHAR));
717 RegCloseKey(hkey);
718 }
719 }
720
721 HWND shellWindow = GetShellWindow();
722
723 if (shellWindow) {
724 DWORD pid;
725
726 // terminate shell process for NT like systems
727 GetWindowThreadProcessId(shellWindow, &pid);
728 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
729
730 // On Win 9x it's sufficient to destroy the shell window.
731 DestroyWindow(shellWindow);
732
733 if (TerminateProcess(hProcess, 0))
734 WaitForSingleObject(hProcess, INFINITE);
735
736 CloseHandle(hProcess);
737 }
738
739 startup_desktop = TRUE;
740 } else
741 // create desktop window and task bar only, if there is no other shell and we are
742 // the first explorer instance
743 startup_desktop = !any_desktop_running;
744
745 bool autostart = !any_desktop_running;
746
747 // disable autostart if the SHIFT key is pressed
748 if (GetAsyncKeyState(VK_SHIFT) < 0)
749 autostart = false;
750
751 #ifdef _DEBUG //MF: disabled for debugging
752 autostart = false;
753 #endif
754
755 // If there is given the command line option "-desktop", create desktop window anyways
756 if (_tcsstr(lpCmdLine,TEXT("-desktop")))
757 startup_desktop = TRUE;
758 else if (_tcsstr(lpCmdLine,TEXT("-nodesktop")))
759 startup_desktop = FALSE;
760
761 // Don't display cabinet window in desktop mode
762 if (startup_desktop && !_tcsstr(lpCmdLine,TEXT("-explorer")))
763 nShowCmd = SW_HIDE;
764
765 if (_tcsstr(lpCmdLine,TEXT("-noautostart")))
766 autostart = false;
767 else if (_tcsstr(lpCmdLine,TEXT("-autostart")))
768 autostart = true;
769
770 #ifndef __WINE__
771 if (_tcsstr(lpCmdLine,TEXT("-console"))) {
772 AllocConsole();
773
774 _dup2(_open_osfhandle((long)GetStdHandle(STD_INPUT_HANDLE), _O_RDONLY), 0);
775 _dup2(_open_osfhandle((long)GetStdHandle(STD_OUTPUT_HANDLE), 0), 1);
776 _dup2(_open_osfhandle((long)GetStdHandle(STD_ERROR_HANDLE), 0), 2);
777
778 g_Globals._log = fdopen(1, "w");
779 setvbuf(g_Globals._log, 0, _IONBF, 0);
780
781 LOG(TEXT("starting explorer debug log\n"));
782 }
783 #endif
784
785 bool use_gdb_stub = false; // !IsDebuggerPresent();
786
787 if (_tcsstr(lpCmdLine,TEXT("-debug")))
788 use_gdb_stub = true;
789
790 if (_tcsstr(lpCmdLine,TEXT("-break"))) {
791 LOG(TEXT("debugger breakpoint"));
792 #ifdef _MSC_VER
793 __asm int 3
794 #else
795 asm("int3");
796 #endif
797 }
798
799 // activate GDB remote debugging stub if no other debugger is running
800 if (use_gdb_stub) {
801 LOG(TEXT("waiting for debugger connection...\n"));
802
803 initialize_gdb_stub();
804 }
805
806 g_Globals.init(hInstance);
807
808 // initialize COM and OLE before creating the desktop window
809 OleInit usingCOM;
810
811 // init common controls library
812 CommonControlInit usingCmnCtrl;
813
814 g_Globals.read_persistent();
815
816 if (startup_desktop) {
817 g_Globals._desktops.init();
818
819 g_Globals._hwndDesktop = DesktopWindow::Create();
820 #ifdef _USE_HDESK
821 g_Globals._desktops.get_current_Desktop()->_hwndDesktop = g_Globals._hwndDesktop;
822 #endif
823
824 if (autostart) {
825 char* argv[] = {"", "s"}; // call startup routine in SESSION_START mode
826 startup(2, argv);
827 }
828 }
829
830 /**TODO fix command line handling */
831 if (*lpCmdLine=='"' && lpCmdLine[_tcslen(lpCmdLine)-1]=='"') {
832 ++lpCmdLine;
833 lpCmdLine[_tcslen(lpCmdLine)-1] = '\0';
834 }
835
836 if (g_Globals._hwndDesktop)
837 g_Globals._desktop_mode = true;
838
839 int ret = explorer_main(hInstance, lpCmdLine, nShowCmd);
840
841 // write configuration file
842 g_Globals.write_persistent();
843
844 return ret;
845 }