2 * Copyright 2003, 2004, 2005 Martin Fuchs
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.
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.
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
21 // Explorer clone, lean version
25 // Martin Fuchs, 23.07.2003
27 // Credits: Thanks to Leon Finker for his explorer window example
33 #include "explorer_intres.h"
35 #include <locale.h> // for setlocale()
38 DynamicLoadLibFct
<void(__stdcall
*)(BOOL
)> g_SHDOCVW_ShellDDEInit(TEXT("SHDOCVW"), 118);
41 ExplorerGlobals g_Globals
;
44 ExplorerGlobals::ExplorerGlobals()
52 _desktop_mode
= false;
53 _prescan_nodes
= false;
57 #ifndef __MINGW32__ // SHRestricted() missing in MinGW (as of 29.10.2003)
66 void ExplorerGlobals::init(HINSTANCE hInstance
)
68 _hInstance
= hInstance
;
70 #ifndef __MINGW32__ // SHRestricted() missing in MinGW (as of 29.10.2003)
71 _SHRestricted
= (DWORD(STDAPICALLTYPE
*)(RESTRICTIONS
)) GetProcAddress(GetModuleHandle(TEXT("SHELL32")), "SHRestricted");
78 void _log_(LPCTSTR txt
)
80 FmtString
msg(TEXT("%s\n"), txt
);
83 _fputts(msg
, g_Globals
._log
);
85 OutputDebugString(msg
);
89 bool FileTypeManager::is_exe_file(LPCTSTR ext
)
91 static const LPCTSTR s_executable_extensions
[] = {
102 TCHAR ext_buffer
[_MAX_EXT
];
107 for(s
=ext
+1,d
=ext_buffer
; (*d
=toupper(*s
)); s
++)
110 for(p
=s_executable_extensions
; *p
; p
++)
111 if (!lstrcmp(ext_buffer
, *p
))
118 const FileTypeInfo
& FileTypeManager::operator[](String ext
)
120 _tcslwr((LPTSTR
)ext
.c_str());
122 iterator found
= find(ext
);
124 return found
->second
;
126 FileTypeInfo
& ftype
= super::operator[](ext
);
128 ftype
._neverShowExt
= false;
131 TCHAR value
[MAX_PATH
], display_name
[MAX_PATH
];;
132 LONG valuelen
= sizeof(value
);
134 if (!RegQueryValue(HKEY_CLASSES_ROOT
, ext
, value
, &valuelen
)) {
135 ftype
._classname
= value
;
137 valuelen
= sizeof(display_name
);
138 if (!RegQueryValue(HKEY_CLASSES_ROOT
, ftype
._classname
, display_name
, &valuelen
))
139 ftype
._displayname
= display_name
;
141 if (!RegOpenKey(HKEY_CLASSES_ROOT
, ftype
._classname
, &hkey
)) {
142 if (!RegQueryValueEx(hkey
, TEXT("NeverShowExt"), 0, NULL
, NULL
, NULL
))
143 ftype
._neverShowExt
= true;
152 LPCTSTR
FileTypeManager::set_type(Entry
* entry
, bool dont_hide_ext
)
154 LPCTSTR ext
= _tcsrchr(entry
->_data
.cFileName
, TEXT('.'));
157 const FileTypeInfo
& type
= (*this)[ext
];
159 if (!type
._displayname
.empty())
160 entry
->_type_name
= _tcsdup(type
._displayname
);
162 // hide some file extensions
163 if (type
._neverShowExt
&& !dont_hide_ext
) {
164 int len
= ext
- entry
->_data
.cFileName
;
165 entry
->_display_name
= (LPTSTR
) malloc((len
+1)*sizeof(TCHAR
));
166 _tcsncpy(entry
->_display_name
, entry
->_data
.cFileName
, len
);
167 entry
->_display_name
[len
] = TEXT('\0');
170 if (is_exe_file(ext
))
171 entry
->_data
.dwFileAttributes
|= ATTRIBUTE_EXECUTABLE
;
185 Icon::Icon(ICON_ID id
, UINT nid
)
188 _hicon(SmallIcon(nid
))
192 Icon::Icon(ICON_TYPE itype
, int id
, HICON hIcon
)
199 Icon::Icon(ICON_TYPE itype
, int id
, int sys_idx
)
206 void Icon::draw(HDC hdc
, int x
, int y
, int cx
, int cy
, COLORREF bk_color
, HBRUSH bk_brush
) const
208 if (_itype
== IT_SYSCACHE
)
209 ImageList_DrawEx(g_Globals
._icon_cache
.get_sys_imagelist(), _sys_idx
, hdc
, x
, y
, cx
, cy
, bk_color
, CLR_DEFAULT
, ILD_NORMAL
);
211 DrawIconEx(hdc
, x
, y
, _hicon
, cx
, cy
, 0, bk_brush
, DI_NORMAL
);
214 HBITMAP
Icon::create_bitmap(COLORREF bk_color
, HBRUSH hbrBkgnd
, HDC hdc_wnd
) const
216 if (_itype
== IT_SYSCACHE
) {
217 HIMAGELIST himl
= g_Globals
._icon_cache
.get_sys_imagelist();
220 ImageList_GetIconSize(himl
, &cx
, &cy
);
222 HBITMAP hbmp
= CreateCompatibleBitmap(hdc_wnd
, cx
, cy
);
223 HDC hdc
= CreateCompatibleDC(hdc_wnd
);
224 HBITMAP hbmp_old
= SelectBitmap(hdc
, hbmp
);
225 ImageList_DrawEx(himl
, _sys_idx
, hdc
, 0, 0, cx
, cy
, bk_color
, CLR_DEFAULT
, ILD_NORMAL
);
226 SelectBitmap(hdc
, hbmp_old
);
230 return create_bitmap_from_icon(_hicon
, hbrBkgnd
, hdc_wnd
);
233 HBITMAP
create_bitmap_from_icon(HICON hIcon
, HBRUSH hbrush_bkgnd
, HDC hdc_wnd
)
235 HBITMAP hbmp
= CreateCompatibleBitmap(hdc_wnd
, 16, 16);
238 BitmapSelection
sel(canvas
, hbmp
);
240 RECT rect
= {0, 0, 16, 16};
241 FillRect(canvas
, &rect
, hbrush_bkgnd
);
243 DrawIconEx(canvas
, 0, 0, hIcon
, 16, 16, 0, hbrush_bkgnd
, DI_NORMAL
);
249 int IconCache::s_next_id
= ICID_DYNAMIC
;
252 void IconCache::init()
254 _icons
[ICID_NONE
] = Icon(IT_STATIC
, ICID_NONE
, (HICON
)0);
256 _icons
[ICID_FOLDER
] = Icon(ICID_FOLDER
, IDI_FOLDER
);
257 //_icons[ICID_DOCUMENT] = Icon(ICID_DOCUMENT, IDI_DOCUMENT);
258 _icons
[ICID_EXPLORER
] = Icon(ICID_EXPLORER
, IDI_EXPLORER
);
259 _icons
[ICID_APP
] = Icon(ICID_APP
, IDI_APPICON
);
261 _icons
[ICID_CONFIG
] = Icon(ICID_CONFIG
, IDI_CONFIG
);
262 _icons
[ICID_DOCUMENTS
] = Icon(ICID_DOCUMENTS
, IDI_DOCUMENTS
);
263 _icons
[ICID_FAVORITES
] = Icon(ICID_FAVORITES
, IDI_FAVORITES
);
264 _icons
[ICID_INFO
] = Icon(ICID_INFO
, IDI_INFO
);
265 _icons
[ICID_APPS
] = Icon(ICID_APPS
, IDI_APPS
);
266 _icons
[ICID_SEARCH
] = Icon(ICID_SEARCH
, IDI_SEARCH
);
267 _icons
[ICID_ACTION
] = Icon(ICID_ACTION
, IDI_ACTION
);
268 _icons
[ICID_SEARCH_DOC
] = Icon(ICID_SEARCH_DOC
, IDI_SEARCH_DOC
);
269 _icons
[ICID_PRINTER
] = Icon(ICID_PRINTER
, IDI_PRINTER
);
270 _icons
[ICID_NETWORK
] = Icon(ICID_NETWORK
, IDI_NETWORK
);
271 _icons
[ICID_COMPUTER
] = Icon(ICID_COMPUTER
, IDI_COMPUTER
);
272 _icons
[ICID_LOGOFF
] = Icon(ICID_LOGOFF
, IDI_LOGOFF
);
276 const Icon
& IconCache::extract(const String
& path
)
278 PathMap::iterator found
= _pathMap
.find(path
);
280 if (found
!= _pathMap
.end())
281 return _icons
[found
->second
];
285 #if 1 // use system image list
286 HIMAGELIST himlSys
= (HIMAGELIST
) SHGetFileInfo(path
, 0, &sfi
, sizeof(sfi
), SHGFI_SYSICONINDEX
|SHGFI_SMALLICON
);
291 const Icon
& icon
= add(sfi
.iIcon
/*, IT_SYSCACHE*/);
293 if (SHGetFileInfo(path
, 0, &sfi
, sizeof(sfi
), SHGFI_ICON
|SHGFI_SMALLICON
)) {
294 const Icon
& icon
= add(sfi
.hIcon
, IT_CACHED
);
297 ///@todo limit cache size
298 _pathMap
[path
] = icon
;
302 return _icons
[ICID_NONE
];
305 const Icon
& IconCache::extract(LPCTSTR path
, int idx
)
307 CachePair
key(path
, idx
);
309 _tcslwr((LPTSTR
)key
.first
.c_str());
311 PathIdxMap::iterator found
= _pathIdxMap
.find(key
);
313 if (found
!= _pathIdxMap
.end())
314 return _icons
[found
->second
];
318 if ((int)ExtractIconEx(path
, idx
, NULL
, &hIcon
, 1) > 0) {
319 const Icon
& icon
= add(hIcon
, IT_CACHED
);
321 _pathIdxMap
[key
] = icon
;
325 return _icons
[ICID_NONE
];
328 const Icon
& IconCache::extract(IExtractIcon
* pExtract
, LPCTSTR path
, int idx
)
330 HICON hIconLarge
= 0;
333 HRESULT hr
= pExtract
->Extract(path
, idx
, &hIconLarge
, &hIcon
, MAKELONG(0/*GetSystemMetrics(SM_CXICON)*/,GetSystemMetrics(SM_CXSMICON
)));
337 DestroyIcon(hIconLarge
);
343 return _icons
[ICID_NONE
];
346 const Icon
& IconCache::add(HICON hIcon
, ICON_TYPE type
)
348 int id
= ++s_next_id
;
350 return _icons
[id
] = Icon(type
, id
, hIcon
);
353 const Icon
& IconCache::add(int sys_idx
/*, ICON_TYPE type=IT_SYSCACHE*/)
355 int id
= ++s_next_id
;
357 return _icons
[id
] = SysCacheIcon(id
, sys_idx
);
360 const Icon
& IconCache::get_icon(int id
)
365 void IconCache::free_icon(int icon_id
)
367 IconMap::iterator found
= _icons
.find(icon_id
);
369 if (found
!= _icons
.end()) {
370 Icon
& icon
= found
->second
;
378 ResString::ResString(UINT nid
)
380 TCHAR buffer
[BUFFER_LEN
];
382 int len
= LoadString(g_Globals
._hInstance
, nid
, buffer
, sizeof(buffer
)/sizeof(TCHAR
));
384 super::assign(buffer
, len
);
388 ResIcon::ResIcon(UINT nid
)
390 _hicon
= LoadIcon(g_Globals
._hInstance
, MAKEINTRESOURCE(nid
));
393 SmallIcon::SmallIcon(UINT nid
)
395 _hicon
= (HICON
)LoadImage(g_Globals
._hInstance
, MAKEINTRESOURCE(nid
), IMAGE_ICON
, GetSystemMetrics(SM_CXSMICON
), GetSystemMetrics(SM_CYSMICON
), LR_SHARED
);
398 ResIconEx::ResIconEx(UINT nid
, int w
, int h
)
400 _hicon
= (HICON
)LoadImage(g_Globals
._hInstance
, MAKEINTRESOURCE(nid
), IMAGE_ICON
, w
, h
, LR_SHARED
);
404 void SetWindowIcon(HWND hwnd
, UINT nid
)
406 HICON hIcon
= ResIcon(nid
);
407 Window_SetIcon(hwnd
, ICON_BIG
, hIcon
);
409 HICON hIconSmall
= SmallIcon(nid
);
410 Window_SetIcon(hwnd
, ICON_SMALL
, hIconSmall
);
414 ResBitmap::ResBitmap(UINT nid
)
416 _hBmp
= LoadBitmap(g_Globals
._hInstance
, MAKEINTRESOURCE(nid
));
422 void explorer_show_frame(int cmdshow
, LPTSTR lpCmdLine
)
424 if (g_Globals
._hMainWnd
) {
425 if (IsIconic(g_Globals
._hMainWnd
))
426 ShowWindow(g_Globals
._hMainWnd
, SW_RESTORE
);
428 SetForegroundWindow(g_Globals
._hMainWnd
);
433 g_Globals
._prescan_nodes
= false;
435 // create main window
436 HWND hMainFrame
= MainFrame::Create();
439 g_Globals
._hMainWnd
= hMainFrame
;
441 ShowWindow(hMainFrame
, cmdshow
);
442 UpdateWindow(hMainFrame
);
444 bool valid_dir
= false;
447 DWORD attribs
= GetFileAttributes(lpCmdLine
);
449 if (attribs
!=INVALID_FILE_ATTRIBUTES
&& (attribs
&FILE_ATTRIBUTE_DIRECTORY
))
451 else if (*lpCmdLine
==':' || *lpCmdLine
=='"')
455 // Open the first child window after initializing the application
457 PostMessage(hMainFrame
, PM_OPEN_WINDOW
, 0, (LPARAM
)lpCmdLine
);
459 PostMessage(hMainFrame
, PM_OPEN_WINDOW
, 0/*OWM_EXPLORE|OWM_DETAILS*/, 0);
465 void explorer_show_frame(int cmdshow
, LPTSTR lpCmdLine
)
468 lpCmdLine
= TEXT("explorer.exe");
470 launch_file(GetDesktopWindow(), lpCmdLine
, cmdshow
);
476 PopupMenu::PopupMenu(UINT nid
)
478 HMENU hMenu
= LoadMenu(g_Globals
._hInstance
, MAKEINTRESOURCE(nid
));
479 _hmenu
= GetSubMenu(hMenu
, 0);
483 /// "About Explorer" Dialog
484 struct ExplorerAboutDlg
: public
486 OwnerDrawParent
<Dialog
>
489 typedef CtlColorParent
<
490 OwnerDrawParent
<Dialog
>
493 ExplorerAboutDlg(HWND hwnd
)
496 SetWindowIcon(hwnd
, IDI_REACTOS
);
498 new FlatButton(hwnd
, IDOK
);
500 _hfont
= CreateFont(20, 0, 0, 0, FW_BOLD
, TRUE
, 0, 0, 0, 0, 0, 0, 0, TEXT("Sans Serif"));
501 new ColorStatic(hwnd
, IDC_ROS_EXPLORER
, RGB(32,32,128), 0, _hfont
);
503 new HyperlinkCtrl(hwnd
, IDC_WWW
);
505 FmtString
ver_txt(ResString(IDS_EXPLORER_VERSION_STR
), (LPCTSTR
)ResString(IDS_VERSION_STR
));
506 SetWindowText(GetDlgItem(hwnd
, IDC_VERSION_TXT
), ver_txt
);
513 DeleteObject(_hfont
);
516 LRESULT
WndProc(UINT nmsg
, WPARAM wparam
, LPARAM lparam
)
524 return super::WndProc(nmsg
, wparam
, lparam
);
532 PaintCanvas
canvas(_hwnd
);
534 HICON hicon
= (HICON
) LoadImage(g_Globals
._hInstance
, MAKEINTRESOURCE(IDI_REACTOS_BIG
), IMAGE_ICON
, 0, 0, LR_SHARED
);
536 DrawIconEx(canvas
, 20, 10, hicon
, 0, 0, 0, 0, DI_NORMAL
);
543 void explorer_about(HWND hwndParent
)
545 Dialog::DoModal(IDD_ABOUT_EXPLORER
, WINDOW_CREATOR(ExplorerAboutDlg
), hwndParent
);
549 static void InitInstance(HINSTANCE hInstance
)
551 CONTEXT("InitInstance");
553 setlocale(LC_COLLATE
, ""); // set collating rules to local settings for compareName
556 // register frame window class
557 g_Globals
._hframeClass
= IconWindowClass(CLASSNAME_FRAME
,IDI_EXPLORER
);
559 // register child windows class
560 WindowClass(CLASSNAME_CHILDWND
, CS_CLASSDC
|CS_DBLCLKS
|CS_VREDRAW
).Register();
562 // register tree windows class
563 WindowClass(CLASSNAME_WINEFILETREE
, CS_CLASSDC
|CS_DBLCLKS
|CS_VREDRAW
).Register();
566 g_Globals
._cfStrFName
= RegisterClipboardFormat(CFSTR_FILENAME
);
570 int explorer_main(HINSTANCE hInstance
, LPTSTR lpCmdLine
, int cmdshow
)
572 CONTEXT("explorer_main");
574 // initialize Common Controls library
575 CommonControlInit usingCmnCtrl
;
578 InitInstance(hInstance
);
579 } catch(COMException
& e
) {
580 HandleException(e
, GetDesktopWindow());
585 if (cmdshow
!= SW_HIDE
) {
586 /* // don't maximize if being called from the ROS desktop
587 if (cmdshow == SW_SHOWNORMAL)
588 ///@todo read window placement from registry
589 cmdshow = SW_MAXIMIZE;
592 explorer_show_frame(cmdshow
, lpCmdLine
);
596 return Window::MessageLoop();
600 // MinGW does not provide a Unicode startup routine, so we have to implement an own.
601 #if defined(__MINGW32__) && defined(UNICODE)
603 #define _tWinMain wWinMain
604 int WINAPI
wWinMain(HINSTANCE
, HINSTANCE
, LPWSTR
, int);
606 int main(int argc
, char* argv
[])
610 STARTUPINFO startupinfo
;
611 int nShowCmd
= SW_SHOWNORMAL
;
613 GetStartupInfo(&startupinfo
);
615 if (startupinfo
.dwFlags
& STARTF_USESHOWWINDOW
)
616 nShowCmd
= startupinfo
.wShowWindow
;
618 LPWSTR cmdline
= GetCommandLineW();
620 while(*cmdline
&& !_istspace(*cmdline
))
623 return wWinMain(GetModuleHandle(NULL
), 0, cmdline
, nShowCmd
);
626 #endif // __MINGW && UNICODE
629 static bool SetShellReadyEvent(LPCTSTR evtName
)
631 HANDLE hEvent
= OpenEvent(EVENT_MODIFY_STATE
, FALSE
, evtName
);
642 int WINAPI
_tWinMain(HINSTANCE hInstance
, HINSTANCE hPrevInstance
, LPTSTR lpCmdLine
, int nShowCmd
)
644 CONTEXT("WinMain()");
646 BOOL any_desktop_running
= IsAnyDesktopRunning();
648 BOOL startup_desktop
;
650 // command line option "-install" to replace previous shell application with ROS Explorer
651 if (_tcsstr(lpCmdLine
,TEXT("-install"))) {
652 // install ROS Explorer into the registry
653 TCHAR path
[MAX_PATH
];
655 int l
= GetModuleFileName(0, path
, MAX_PATH
);
659 if (!RegOpenKey(HKEY_LOCAL_MACHINE
, TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon"), &hkey
)) {
661 ///@todo save previous shell application in config file
663 RegSetValueEx(hkey
, TEXT("Shell"), 0, REG_SZ
, (LPBYTE
)path
, l
*sizeof(TCHAR
));
668 HWND shellWindow
= GetShellWindow();
673 // terminate shell process for NT like systems
674 GetWindowThreadProcessId(shellWindow
, &pid
);
675 HANDLE hProcess
= OpenProcess(PROCESS_ALL_ACCESS
, FALSE
, pid
);
677 // On Win 9x it's sufficient to destroy the shell window.
678 DestroyWindow(shellWindow
);
680 if (TerminateProcess(hProcess
, 0))
681 WaitForSingleObject(hProcess
, INFINITE
);
683 CloseHandle(hProcess
);
686 startup_desktop
= TRUE
;
688 // create desktop window and task bar only, if there is no other shell and we are
689 // the first explorer instance
690 // MS Explorer looks additionally into the registry entry HKCU\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\shell,
691 // to decide wether it is currently configured as shell application.
692 startup_desktop
= !any_desktop_running
;
696 bool autostart
= !any_desktop_running
;
698 // disable autostart if the SHIFT key is pressed
699 if (GetAsyncKeyState(VK_SHIFT
) < 0)
702 #ifdef _DEBUG //MF: disabled for debugging
706 // If there is given the command line option "-desktop", create desktop window anyways
707 if (_tcsstr(lpCmdLine
,TEXT("-desktop")))
708 startup_desktop
= TRUE
;
710 else if (_tcsstr(lpCmdLine
,TEXT("-nodesktop")))
711 startup_desktop
= FALSE
;
713 // Don't display cabinet window in desktop mode
714 if (startup_desktop
&& !_tcsstr(lpCmdLine
,TEXT("-explorer")))
718 if (_tcsstr(lpCmdLine
,TEXT("-noautostart")))
721 if (startup_desktop
) {
722 // hide the XP login screen (Credit to Nicolas Escuder)
723 // another undocumented event: "Global\\msgina: ReturnToWelcome"
724 if (!SetShellReadyEvent(TEXT("msgina: ShellReadyEvent")))
725 SetShellReadyEvent(TEXT("Global\\msgina: ShellReadyEvent"));
727 // launch the shell DDE server
728 if (g_SHDOCVW_ShellDDEInit
)
729 (*g_SHDOCVW_ShellDDEInit
)(TRUE
);
733 return 0; // no shell to launch, so exit immediatelly
737 g_Globals
.init(hInstance
);
739 // initialize COM and OLE before creating the desktop window
742 if (startup_desktop
) {
743 g_Globals
._desktops
.init();
745 g_Globals
._hwndDesktop
= DesktopWindow::Create();
747 /**TODO launching autostart programs can be moved into a background thread. */
749 char* argv
[] = {"", "s"}; // call startup routine in SESSION_START mode
755 /**TODO fix command line handling */
756 if (*lpCmdLine
=='"' && lpCmdLine
[_tcslen(lpCmdLine
)-1]=='"') {
758 lpCmdLine
[_tcslen(lpCmdLine
)-1] = '\0';
761 if (g_Globals
._hwndDesktop
)
762 g_Globals
._desktop_mode
= true;
765 int ret
= explorer_main(hInstance
, lpCmdLine
, nShowCmd
);
767 // shutdown the shell DDE server
768 if (g_SHDOCVW_ShellDDEInit
)
769 (*g_SHDOCVW_ShellDDEInit
)(FALSE
);