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