merge ROS Shell without integrated explorer part into trunk
[reactos.git] / reactos / subsys / system / explorer / explorer.cpp
index b57a89a..435fb23 100644 (file)
 
 
  //
- // Explorer clone, lean version
+ // Explorer clone
  //
  // explorer.cpp
  //
  // Martin Fuchs, 23.07.2003
  //
- // Credits: Thanks to Leon Finker for his explorer window example
+ // Credits: Thanks to Leon Finker for his explorer cabinet window example
  //
 
 
 
 #include <locale.h>    // for setlocale()
 
+#ifndef __WINE__
+#include <io.h>                // for dup2()
+#include <fcntl.h>     // for _O_RDONLY
+#endif
+
+#include "dialogs/settings.h"  // for MdiSdiDlg
+
+#include "services/shellservices.h"
+
+
+extern "C" int initialize_gdb_stub();  // start up GDB stub
+
 
 DynamicLoadLibFct<void(__stdcall*)(BOOL)> g_SHDOCVW_ShellDDEInit(TEXT("SHDOCVW"), 118);
 
@@ -75,6 +87,59 @@ void ExplorerGlobals::init(HINSTANCE hInstance)
 }
 
 
+void ExplorerGlobals::read_persistent()
+{
+        // read configuration file
+       _cfg_dir.printf(TEXT("%s\\ReactOS"), (LPCTSTR)SpecialFolderFSPath(CSIDL_APPDATA,0));
+       _cfg_path.printf(TEXT("%s\\ros-explorer-cfg.xml"), _cfg_dir.c_str());
+
+       if (!_cfg.read(_cfg_path)) {
+               if (_cfg._last_error != XML_ERROR_NO_ELEMENTS)
+                       MessageBox(g_Globals._hwndDesktop, String(_cfg._last_error_msg.c_str()),
+                                               TEXT("ROS Explorer - reading user settings"), MB_OK);
+
+               _cfg.read(TEXT("explorer-cfg-template.xml"));
+       }
+
+        // read bookmarks
+       _favorites_path.printf(TEXT("%s\\ros-explorer-bookmarks.xml"), _cfg_dir.c_str());
+
+       if (!_favorites.read(_favorites_path)) {
+               _favorites.import_IE_favorites(0);
+               _favorites.write(_favorites_path);
+       }
+}
+
+void ExplorerGlobals::write_persistent()
+{
+        // write configuration file
+       RecursiveCreateDirectory(_cfg_dir);
+
+       _cfg.write(_cfg_path);
+       _favorites.write(_favorites_path);
+}
+
+
+XMLPos ExplorerGlobals::get_cfg()
+{
+       XMLPos cfg_pos(&_cfg);
+
+       cfg_pos.smart_create("explorer-cfg");
+
+       return cfg_pos;
+}
+
+XMLPos ExplorerGlobals::get_cfg(const char* path)
+{
+       XMLPos cfg_pos(&_cfg);
+
+       cfg_pos.smart_create("explorer-cfg");
+       cfg_pos.create_relative(path);
+
+       return cfg_pos;
+}
+
+
 void _log_(LPCTSTR txt)
 {
        FmtString msg(TEXT("%s\n"), txt);
@@ -117,7 +182,9 @@ bool FileTypeManager::is_exe_file(LPCTSTR ext)
 
 const FileTypeInfo& FileTypeManager::operator[](String ext)
 {
+#ifndef __WINE__ ///@todo _tcslwr() for Wine
        _tcslwr((LPTSTR)ext.c_str());
+#endif
 
        iterator found = find(ext);
        if (found != end())
@@ -128,7 +195,7 @@ const FileTypeInfo& FileTypeManager::operator[](String ext)
        ftype._neverShowExt = false;
 
        HKEY hkey;
-       TCHAR value[MAX_PATH], display_name[MAX_PATH];;
+       TCHAR value[MAX_PATH], display_name[MAX_PATH];
        LONG valuelen = sizeof(value);
 
        if (!RegQueryValue(HKEY_CLASSES_ROOT, ext, value, &valuelen)) {
@@ -225,26 +292,67 @@ HBITMAP   Icon::create_bitmap(COLORREF bk_color, HBRUSH hbrBkgnd, HDC hdc_wnd) con
                ImageList_DrawEx(himl, _sys_idx, hdc, 0, 0, cx, cy, bk_color, CLR_DEFAULT, ILD_NORMAL);
                SelectBitmap(hdc, hbmp_old);
                DeleteDC(hdc);
+
                return hbmp;
        } else
                return create_bitmap_from_icon(_hicon, hbrBkgnd, hdc_wnd);
 }
 
+
+int Icon::add_to_imagelist(HIMAGELIST himl, HDC hdc_wnd, COLORREF bk_color, HBRUSH bk_brush) const
+{
+       int ret;
+
+       if (_itype == IT_SYSCACHE) {
+               HIMAGELIST himl = g_Globals._icon_cache.get_sys_imagelist();
+
+               int cx, cy;
+               ImageList_GetIconSize(himl, &cx, &cy);
+
+               HBITMAP hbmp = CreateCompatibleBitmap(hdc_wnd, cx, cy);
+               HDC hdc = CreateCompatibleDC(hdc_wnd);
+               HBITMAP hbmp_old = SelectBitmap(hdc, hbmp);
+               ImageList_DrawEx(himl, _sys_idx, hdc, 0, 0, cx, cy, bk_color, CLR_DEFAULT, ILD_NORMAL);
+               SelectBitmap(hdc, hbmp_old);
+               DeleteDC(hdc);
+
+               ret = ImageList_Add(himl, hbmp, 0);
+
+               DeleteObject(hbmp);
+       } else
+               ret = ImageList_AddAlphaIcon(himl, _hicon, bk_brush, hdc_wnd);
+
+       return ret;
+}
+
 HBITMAP create_bitmap_from_icon(HICON hIcon, HBRUSH hbrush_bkgnd, HDC hdc_wnd)
 {
-       HBITMAP hbmp = CreateCompatibleBitmap(hdc_wnd, 16, 16);
+       int cx = GetSystemMetrics(SM_CXSMICON);
+       int cy = GetSystemMetrics(SM_CYSMICON);
+       HBITMAP hbmp = CreateCompatibleBitmap(hdc_wnd, cx, cy);
 
        MemCanvas canvas;
        BitmapSelection sel(canvas, hbmp);
 
-       RECT rect = {0, 0, 16, 16};
+       RECT rect = {0, 0, cx, cy};
        FillRect(canvas, &rect, hbrush_bkgnd);
 
-       DrawIconEx(canvas, 0, 0, hIcon, 16, 16, 0, hbrush_bkgnd, DI_NORMAL);
+       DrawIconEx(canvas, 0, 0, hIcon, cx, cy, 0, hbrush_bkgnd, DI_NORMAL);
 
        return hbmp;
 }
 
+int ImageList_AddAlphaIcon(HIMAGELIST himl, HICON hIcon, HBRUSH hbrush_bkgnd, HDC hdc_wnd)
+{
+       HBITMAP hbmp = create_bitmap_from_icon(hIcon, hbrush_bkgnd, hdc_wnd);
+
+       int ret = ImageList_Add(himl, hbmp, 0);
+
+       DeleteObject(hbmp);
+
+       return ret;
+}
+
 
 int IconCache::s_next_id = ICID_DYNAMIC;
 
@@ -254,7 +362,7 @@ void IconCache::init()
        _icons[ICID_NONE]               = Icon(IT_STATIC, ICID_NONE, (HICON)0);
 
        _icons[ICID_FOLDER]             = Icon(ICID_FOLDER,             IDI_FOLDER);
-       //_icons[ICID_DOCUMENT] = Icon(ICID_DOCUMENT,   IDI_DOCUMENT);
+       //_icons[ICID_DOCUMENT] = Icon(ICID_DOCUMENT,   IDI_DOCUMENT);
        _icons[ICID_EXPLORER]   = Icon(ICID_EXPLORER,   IDI_EXPLORER);
        _icons[ICID_APP]                = Icon(ICID_APP,                IDI_APPICON);
 
@@ -270,6 +378,7 @@ void IconCache::init()
        _icons[ICID_NETWORK]    = Icon(ICID_NETWORK,    IDI_NETWORK);
        _icons[ICID_COMPUTER]   = Icon(ICID_COMPUTER,   IDI_COMPUTER);
        _icons[ICID_LOGOFF]             = Icon(ICID_LOGOFF,             IDI_LOGOFF);
+       _icons[ICID_BOOKMARK]   = Icon(ICID_BOOKMARK,   IDI_DOT_TRANS);
 }
 
 
@@ -282,7 +391,7 @@ const Icon& IconCache::extract(const String& path)
 
        SHFILEINFO sfi;
 
-#if 1  // use system image list
+#if 1  // use system image list - the "search program dialog" needs it
        HIMAGELIST himlSys = (HIMAGELIST) SHGetFileInfo(path, 0, &sfi, sizeof(sfi), SHGFI_SYSICONINDEX|SHGFI_SMALLICON);
 
        if (himlSys) {
@@ -306,7 +415,9 @@ const Icon& IconCache::extract(LPCTSTR path, int idx)
 {
        CachePair key(path, idx);
 
+#ifndef __WINE__ ///@todo _tcslwr() for Wine
        _tcslwr((LPTSTR)key.first.c_str());
+#endif
 
        PathIdxMap::iterator found = _pathIdxMap.find(key);
 
@@ -321,8 +432,12 @@ const Icon& IconCache::extract(LPCTSTR path, int idx)
                _pathIdxMap[key] = icon;
 
                return icon;
-       } else
+       } else {
+
+               ///@todo retreive "http://.../favicon.ico" format icons
+
                return _icons[ICID_NONE];
+       }
 }
 
 const Icon& IconCache::extract(IExtractIcon* pExtract, LPCTSTR path, int idx)
@@ -332,7 +447,7 @@ const Icon& IconCache::extract(IExtractIcon* pExtract, LPCTSTR path, int idx)
 
        HRESULT hr = pExtract->Extract(path, idx, &hIconLarge, &hIcon, MAKELONG(0/*GetSystemMetrics(SM_CXICON)*/,GetSystemMetrics(SM_CXSMICON)));
 
-       if (hr == NOERROR) {
+       if (hr == NOERROR) {    //@@ oder SUCCEEDED(hr) ?
                if (hIconLarge)
                        DestroyIcon(hIconLarge);
 
@@ -432,32 +547,16 @@ void explorer_show_frame(int cmdshow, LPTSTR lpCmdLine)
 
        g_Globals._prescan_nodes = false;
 
-        // create main window
-       HWND hMainFrame = MainFrame::Create();
-
-       if (hMainFrame) {
-               g_Globals._hMainWnd = hMainFrame;
-
-               ShowWindow(hMainFrame, cmdshow);
-               UpdateWindow(hMainFrame);
+       XMLPos explorer_options = g_Globals.get_cfg("general/explorer");
+       XS_String mdiStr = XMLString(explorer_options, "mdi");
 
-               bool valid_dir = false;
+       if (mdiStr.empty())
+               Dialog::DoModal(IDD_MDI_SDI, WINDOW_CREATOR(MdiSdiDlg), g_Globals._hwndDesktop);
 
-               if (lpCmdLine) {
-                       DWORD attribs = GetFileAttributes(lpCmdLine);
+       bool mdi = XMLBool(explorer_options, "mdi", true);
 
-                       if (attribs!=INVALID_FILE_ATTRIBUTES && (attribs&FILE_ATTRIBUTE_DIRECTORY))
-                               valid_dir = true;
-                       else if (*lpCmdLine==':' || *lpCmdLine=='"')
-                               valid_dir = true;
-               }
-
-                // Open the first child window after initializing the application
-               if (valid_dir)
-                       PostMessage(hMainFrame, PM_OPEN_WINDOW, 0, (LPARAM)lpCmdLine);
-               else
-                       PostMessage(hMainFrame, PM_OPEN_WINDOW, 0/*OWM_EXPLORE|OWM_DETAILS*/, 0);
-       }
+        // create main window
+       MainFrameBase::Create(lpCmdLine, mdi, cmdshow);
 }
 
 #else
@@ -505,6 +604,10 @@ struct ExplorerAboutDlg : public
                FmtString ver_txt(ResString(IDS_EXPLORER_VERSION_STR), (LPCTSTR)ResString(IDS_VERSION_STR));
                SetWindowText(GetDlgItem(hwnd, IDC_VERSION_TXT), ver_txt);
 
+               HWND hwnd_winver = GetDlgItem(hwnd, IDC_WIN_VERSION);
+               SetWindowText(hwnd_winver, get_windows_version_str());
+               SetWindowFont(hwnd_winver, GetStockFont(DEFAULT_GUI_FONT), FALSE);
+
                CenterWindow(hwnd);
        }
 
@@ -556,10 +659,10 @@ static void InitInstance(HINSTANCE hInstance)
         // register frame window class
        g_Globals._hframeClass = IconWindowClass(CLASSNAME_FRAME,IDI_EXPLORER);
 
-        // register child windows class
+        // register child window class
        WindowClass(CLASSNAME_CHILDWND, CS_CLASSDC|CS_DBLCLKS|CS_VREDRAW).Register();
 
-        // register tree windows class
+        // register tree window class
        WindowClass(CLASSNAME_WINEFILETREE, CS_CLASSDC|CS_DBLCLKS|CS_VREDRAW).Register();
 #endif
 
@@ -620,6 +723,9 @@ int main(int argc, char* argv[])
        while(*cmdline && !_istspace(*cmdline))
                ++cmdline;
 
+       while(_istspace(*cmdline))
+               ++cmdline;
+
        return wWinMain(GetModuleHandle(NULL), 0, cmdline, nShowCmd);
 }
 
@@ -663,6 +769,14 @@ int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdL
                                RegSetValueEx(hkey, TEXT("Shell"), 0, REG_SZ, (LPBYTE)path, l*sizeof(TCHAR));
                                RegCloseKey(hkey);
                        }
+
+                       if (!RegOpenKey(HKEY_CURRENT_USER, TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon"), &hkey)) {
+
+                               ///@todo save previous shell application in config file
+
+                               RegSetValueEx(hkey, TEXT("Shell"), 0, REG_SZ, (LPBYTE)TEXT(""), l*sizeof(TCHAR));
+                               RegCloseKey(hkey);
+                       }
                }
 
                HWND shellWindow = GetShellWindow();
@@ -717,56 +831,127 @@ int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdL
 
        if (_tcsstr(lpCmdLine,TEXT("-noautostart")))
                autostart = false;
+       else if (_tcsstr(lpCmdLine,TEXT("-autostart")))
+               autostart = true;
+
+#ifndef __WINE__
+       if (_tcsstr(lpCmdLine,TEXT("-console"))) {
+               AllocConsole();
+
+               _dup2(_open_osfhandle((long)GetStdHandle(STD_INPUT_HANDLE), _O_RDONLY), 0);
+               _dup2(_open_osfhandle((long)GetStdHandle(STD_OUTPUT_HANDLE), 0), 1);
+               _dup2(_open_osfhandle((long)GetStdHandle(STD_ERROR_HANDLE), 0), 2);
+
+               g_Globals._log = fdopen(1, "w");
+               setvbuf(g_Globals._log, 0, _IONBF, 0);
+
+               LOG(TEXT("starting explorer debug log\n"));
+       }
+#endif
+
 
        if (startup_desktop) {
                 // hide the XP login screen (Credit to Nicolas Escuder)
                 // another undocumented event: "Global\\msgina: ReturnToWelcome"
                if (!SetShellReadyEvent(TEXT("msgina: ShellReadyEvent")))
                        SetShellReadyEvent(TEXT("Global\\msgina: ShellReadyEvent"));
+       }
+#ifdef ROSSHELL
+       else
+               return 0;       // no shell to launch, so exit immediatelly
+#endif
 
+
+       if (!any_desktop_running) {
                 // launch the shell DDE server
                if (g_SHDOCVW_ShellDDEInit)
                        (*g_SHDOCVW_ShellDDEInit)(TRUE);
        }
-#ifdef ROSSHELL
-       else
-               return 0;       // no shell to launch, so exit immediatelly
+
+
+       bool use_gdb_stub = false;      // !IsDebuggerPresent();
+
+       if (_tcsstr(lpCmdLine,TEXT("-debug")))
+               use_gdb_stub = true;
+
+       if (_tcsstr(lpCmdLine,TEXT("-break"))) {
+               LOG(TEXT("debugger breakpoint"));
+#ifdef _MSC_VER
+               __asm int 3
+#else
+               asm("int3");
 #endif
+       }
 
+        // activate GDB remote debugging stub if no other debugger is running
+       if (use_gdb_stub) {
+               LOG(TEXT("waiting for debugger connection...\n"));
+
+               initialize_gdb_stub();
+       }
 
        g_Globals.init(hInstance);
 
         // initialize COM and OLE before creating the desktop window
        OleInit usingCOM;
 
+        // init common controls library
+       CommonControlInit usingCmnCtrl;
+
+       g_Globals.read_persistent();
+
        if (startup_desktop) {
+               WaitCursor wait;
+
                g_Globals._desktops.init();
 
                g_Globals._hwndDesktop = DesktopWindow::Create();
+#ifdef _USE_HDESK
+               g_Globals._desktops.get_current_Desktop()->_hwndDesktop = g_Globals._hwndDesktop;
+#endif
+       }
 
-               /**TODO launching autostart programs can be moved into a background thread. */
-               if (autostart) {
-                       char* argv[] = {"", "s"};       // call startup routine in SESSION_START mode
-                       startup(2, argv);
-               }
+       Thread* pSSOThread = NULL;
+
+       if (startup_desktop) {
+                // launch SSO thread to allow message processing independent from the explorer main thread
+               pSSOThread = new SSOThread;
+               pSSOThread->Start();
+       }
+
+       /**TODO launching autostart programs can be moved into a background thread. */
+       if (autostart) {
+               char* argv[] = {"", "s"};       // call startup routine in SESSION_START mode
+               startup(2, argv);
        }
 
 #ifndef ROSSHELL
+       if (g_Globals._hwndDesktop)
+               g_Globals._desktop_mode = true;
+
        /**TODO fix command line handling */
        if (*lpCmdLine=='"' && lpCmdLine[_tcslen(lpCmdLine)-1]=='"') {
                ++lpCmdLine;
                lpCmdLine[_tcslen(lpCmdLine)-1] = '\0';
        }
-
-       if (g_Globals._hwndDesktop)
-               g_Globals._desktop_mode = true;
 #endif
 
+
        int ret = explorer_main(hInstance, lpCmdLine, nShowCmd);
 
-        // shutdown the shell DDE server
-       if (g_SHDOCVW_ShellDDEInit)
-               (*g_SHDOCVW_ShellDDEInit)(FALSE);
+        // write configuration file
+       g_Globals.write_persistent();
+
+       if (pSSOThread) {
+               pSSOThread->Stop();
+               delete pSSOThread;
+       }
+
+       if (!any_desktop_running) {
+                // shutdown the shell DDE server
+               if (g_SHDOCVW_ShellDDEInit)
+                       (*g_SHDOCVW_ShellDDEInit)(FALSE);
+       }
 
        return ret;
 }