xref: /reactos/base/shell/explorer/explorer.cpp (revision 738c8fc2)
1c2c66affSColin Finck /*
2c2c66affSColin Finck  * ReactOS Explorer
3c2c66affSColin Finck  *
4c2c66affSColin Finck  * Copyright 2006 - 2007 Thomas Weidenmueller <w3seek@reactos.org>
5c2c66affSColin Finck  *
6c2c66affSColin Finck  * This library is free software; you can redistribute it and/or
7c2c66affSColin Finck  * modify it under the terms of the GNU Lesser General Public
8c2c66affSColin Finck  * License as published by the Free Software Foundation; either
9c2c66affSColin Finck  * version 2.1 of the License, or (at your option) any later version.
10c2c66affSColin Finck  *
11c2c66affSColin Finck  * This library is distributed in the hope that it will be useful,
12c2c66affSColin Finck  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13c2c66affSColin Finck  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14c2c66affSColin Finck  * Lesser General Public License for more details.
15c2c66affSColin Finck  *
16c2c66affSColin Finck  * You should have received a copy of the GNU Lesser General Public
17c2c66affSColin Finck  * License along with this library; if not, write to the Free Software
18c2c66affSColin Finck  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19c2c66affSColin Finck  */
20c2c66affSColin Finck 
21c2c66affSColin Finck #include "precomp.h"
22c2c66affSColin Finck #include <browseui_undoc.h>
23c2c66affSColin Finck 
24c2c66affSColin Finck HINSTANCE hExplorerInstance;
25c2c66affSColin Finck HANDLE hProcessHeap;
26c2c66affSColin Finck HKEY hkExplorer = NULL;
2712de9654SHermès Bélusca-Maïto BOOL bExplorerIsShell = FALSE;
28c2c66affSColin Finck 
29c2c66affSColin Finck class CExplorerModule : public CComModule
30c2c66affSColin Finck {
31c2c66affSColin Finck public:
32c2c66affSColin Finck };
33c2c66affSColin Finck 
34c2c66affSColin Finck BEGIN_OBJECT_MAP(ObjectMap)
35c2c66affSColin Finck END_OBJECT_MAP()
36c2c66affSColin Finck 
37c2c66affSColin Finck CExplorerModule gModule;
38c2c66affSColin Finck CAtlWinModule   gWinModule;
39c2c66affSColin Finck 
InitializeAtlModule(HINSTANCE hInstance,BOOL bInitialize)40c2c66affSColin Finck static VOID InitializeAtlModule(HINSTANCE hInstance, BOOL bInitialize)
41c2c66affSColin Finck {
42c2c66affSColin Finck     if (bInitialize)
43c2c66affSColin Finck     {
44c2c66affSColin Finck         gModule.Init(ObjectMap, hInstance, NULL);
45c2c66affSColin Finck     }
46c2c66affSColin Finck     else
47c2c66affSColin Finck     {
48c2c66affSColin Finck         gModule.Term();
49c2c66affSColin Finck     }
50c2c66affSColin Finck }
51c2c66affSColin Finck 
52c2c66affSColin Finck #if !WIN7_DEBUG_MODE
53c2c66affSColin Finck static BOOL
SetShellReadyEvent(IN LPCWSTR lpEventName)54c2c66affSColin Finck SetShellReadyEvent(IN LPCWSTR lpEventName)
55c2c66affSColin Finck {
56c2c66affSColin Finck     HANDLE hEvent;
57c2c66affSColin Finck 
58c2c66affSColin Finck     hEvent = OpenEventW(EVENT_MODIFY_STATE, FALSE, lpEventName);
59c2c66affSColin Finck     if (hEvent != NULL)
60c2c66affSColin Finck     {
61c2c66affSColin Finck         SetEvent(hEvent);
62c2c66affSColin Finck 
63c2c66affSColin Finck         CloseHandle(hEvent);
64c2c66affSColin Finck         return TRUE;
65c2c66affSColin Finck     }
66c2c66affSColin Finck 
67c2c66affSColin Finck     return FALSE;
68c2c66affSColin Finck }
69c2c66affSColin Finck 
70c2c66affSColin Finck static VOID
HideMinimizedWindows(IN BOOL bHide)71c2c66affSColin Finck HideMinimizedWindows(IN BOOL bHide)
72c2c66affSColin Finck {
73c2c66affSColin Finck     MINIMIZEDMETRICS mm;
74c2c66affSColin Finck 
75c2c66affSColin Finck     mm.cbSize = sizeof(mm);
76c2c66affSColin Finck     if (!SystemParametersInfoW(SPI_GETMINIMIZEDMETRICS, sizeof(mm), &mm, 0))
77c2c66affSColin Finck     {
78c2c66affSColin Finck         ERR("SystemParametersInfoW failed with %lu\n", GetLastError());
79c2c66affSColin Finck         return;
80c2c66affSColin Finck     }
81c2c66affSColin Finck     if (bHide)
82c2c66affSColin Finck         mm.iArrange |= ARW_HIDE;
83c2c66affSColin Finck     else
84c2c66affSColin Finck         mm.iArrange &= ~ARW_HIDE;
85c2c66affSColin Finck     if (!SystemParametersInfoW(SPI_SETMINIMIZEDMETRICS, sizeof(mm), &mm, 0))
86c2c66affSColin Finck         ERR("SystemParametersInfoW failed with %lu\n", GetLastError());
87c2c66affSColin Finck }
88c2c66affSColin Finck #endif
89c2c66affSColin Finck 
90c2c66affSColin Finck #if !WIN7_COMPAT_MODE
91c2c66affSColin Finck static INT
StartWithCommandLine(IN HINSTANCE hInstance)92c2c66affSColin Finck StartWithCommandLine(IN HINSTANCE hInstance)
93c2c66affSColin Finck {
94c2c66affSColin Finck     BOOL b = FALSE;
95c2c66affSColin Finck     EXPLORER_CMDLINE_PARSE_RESULTS parseResults = { 0 };
96c2c66affSColin Finck 
97c2c66affSColin Finck     if (SHExplorerParseCmdLine(&parseResults))
98c2c66affSColin Finck         b = SHCreateFromDesktop(&parseResults);
99c2c66affSColin Finck 
100c2c66affSColin Finck     if (parseResults.strPath)
101c2c66affSColin Finck         SHFree(parseResults.strPath);
102c2c66affSColin Finck 
103c2c66affSColin Finck     if (parseResults.pidlPath)
104c2c66affSColin Finck         ILFree(parseResults.pidlPath);
105c2c66affSColin Finck 
106c2c66affSColin Finck     if (parseResults.pidlRoot)
107c2c66affSColin Finck         ILFree(parseResults.pidlRoot);
108c2c66affSColin Finck 
109c2c66affSColin Finck     return b;
110c2c66affSColin Finck }
111c2c66affSColin Finck #endif
112c2c66affSColin Finck 
113c2c66affSColin Finck static INT
StartWithDesktop(IN HINSTANCE hInstance)114c2c66affSColin Finck StartWithDesktop(IN HINSTANCE hInstance)
115c2c66affSColin Finck {
116c2c66affSColin Finck     InitializeAtlModule(hInstance, TRUE);
117c2c66affSColin Finck 
118c2c66affSColin Finck     if (RegOpenKeyW(HKEY_CURRENT_USER,
119c2c66affSColin Finck         L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer",
120c2c66affSColin Finck         &hkExplorer) != ERROR_SUCCESS)
121c2c66affSColin Finck     {
122c2c66affSColin Finck         WCHAR Message[256];
123c2c66affSColin Finck         LoadStringW(hInstance, IDS_STARTUP_ERROR, Message, _countof(Message));
124c2c66affSColin Finck         MessageBox(NULL, Message, NULL, MB_ICONERROR);
125c2c66affSColin Finck         return 1;
126c2c66affSColin Finck     }
127c2c66affSColin Finck 
128c2c66affSColin Finck     hExplorerInstance = hInstance;
129c2c66affSColin Finck     hProcessHeap = GetProcessHeap();
130c2c66affSColin Finck 
1312ed535daSGiannis Adamopoulos     g_TaskbarSettings.Load();
1322ed535daSGiannis Adamopoulos 
133c2c66affSColin Finck     InitCommonControls();
134c2c66affSColin Finck     OleInitialize(NULL);
135c2c66affSColin Finck 
136c2c66affSColin Finck #if !WIN7_COMPAT_MODE
137c2c66affSColin Finck     /* Initialize shell dde support */
138c2c66affSColin Finck     _ShellDDEInit(TRUE);
139c2c66affSColin Finck #endif
140c2c66affSColin Finck 
141c2c66affSColin Finck     /* Initialize shell icons */
142c2c66affSColin Finck     FileIconInit(TRUE);
143c2c66affSColin Finck 
144c2c66affSColin Finck     /* Initialize CLSID_ShellWindows class */
145c2c66affSColin Finck     _WinList_Init();
146c2c66affSColin Finck 
147c2c66affSColin Finck     CComPtr<ITrayWindow> Tray;
148c2c66affSColin Finck     CreateTrayWindow(&Tray);
149c2c66affSColin Finck 
150c2c66affSColin Finck #if !WIN7_DEBUG_MODE
151c2c66affSColin Finck     /* This not only hides the minimized window captions in the bottom
152c2c66affSColin Finck     left screen corner, but is also needed in order to receive
153c2c66affSColin Finck     HSHELL_* notification messages (which are required for taskbar
154c2c66affSColin Finck     buttons to work right) */
155c2c66affSColin Finck     HideMinimizedWindows(TRUE);
156c2c66affSColin Finck 
157*738c8fc2SWhindmar Saksit     ProcessRunOnceItems(); // Must be executed before the desktop is created
158*738c8fc2SWhindmar Saksit 
159c2c66affSColin Finck     HANDLE hShellDesktop = NULL;
160c2c66affSColin Finck     if (Tray != NULL)
161c2c66affSColin Finck         hShellDesktop = DesktopCreateWindow(Tray);
162c2c66affSColin Finck 
163c2c66affSColin Finck     /* WinXP: Notify msgina to hide the welcome screen */
164c2c66affSColin Finck     if (!SetShellReadyEvent(L"msgina: ShellReadyEvent"))
165c2c66affSColin Finck         SetShellReadyEvent(L"Global\\msgina: ShellReadyEvent");
1660a8cd95cSKatayama Hirofumi MZ 
1670a8cd95cSKatayama Hirofumi MZ     if (DoStartStartupItems(Tray))
1680a8cd95cSKatayama Hirofumi MZ     {
1690a8cd95cSKatayama Hirofumi MZ         ProcessStartupItems();
1700a8cd95cSKatayama Hirofumi MZ         DoFinishStartupItems();
1710a8cd95cSKatayama Hirofumi MZ     }
172*738c8fc2SWhindmar Saksit     ReleaseStartupMutex(); // For ProcessRunOnceItems
173c2c66affSColin Finck #endif
174c2c66affSColin Finck 
175c2c66affSColin Finck     if (Tray != NULL)
176c2c66affSColin Finck     {
177c2c66affSColin Finck         TrayMessageLoop(Tray);
178c2c66affSColin Finck #if !WIN7_DEBUG_MODE
179c2c66affSColin Finck         HideMinimizedWindows(FALSE);
180c2c66affSColin Finck #endif
181c2c66affSColin Finck     }
182c2c66affSColin Finck 
183c2c66affSColin Finck #if !WIN7_DEBUG_MODE
184c2c66affSColin Finck     if (hShellDesktop != NULL)
185c2c66affSColin Finck         DesktopDestroyShellWindow(hShellDesktop);
186c2c66affSColin Finck #endif
187c2c66affSColin Finck 
188c2c66affSColin Finck     OleUninitialize();
189c2c66affSColin Finck 
190c2c66affSColin Finck     RegCloseKey(hkExplorer);
191c2c66affSColin Finck     hkExplorer = NULL;
192c2c66affSColin Finck 
193c2c66affSColin Finck     InitializeAtlModule(hInstance, FALSE);
194c2c66affSColin Finck 
195c2c66affSColin Finck     return 0;
196c2c66affSColin Finck }
197c2c66affSColin Finck 
198c2c66affSColin Finck INT WINAPI
_tWinMain(IN HINSTANCE hInstance,IN HINSTANCE hPrevInstance,IN LPTSTR lpCmdLine,IN INT nCmdShow)199c2c66affSColin Finck _tWinMain(IN HINSTANCE hInstance,
200c2c66affSColin Finck           IN HINSTANCE hPrevInstance,
201c2c66affSColin Finck           IN LPTSTR lpCmdLine,
202c2c66affSColin Finck           IN INT nCmdShow)
203c2c66affSColin Finck {
204c2c66affSColin Finck     /*
205c2c66affSColin Finck     * Set our shutdown parameters: we want to shutdown the very last,
206c2c66affSColin Finck     * but before any TaskMgr instance (which has a shutdown level of 1).
207c2c66affSColin Finck     */
208c2c66affSColin Finck     SetProcessShutdownParameters(2, 0);
209c2c66affSColin Finck 
210c2c66affSColin Finck     InitRSHELL();
211c2c66affSColin Finck 
212c2c66affSColin Finck     TRACE("Explorer starting... Command line: %S\n", lpCmdLine);
213c2c66affSColin Finck 
21412de9654SHermès Bélusca-Maïto #if !WIN7_COMPAT_MODE
215c2c66affSColin Finck     if (GetShellWindow() == NULL)
21612de9654SHermès Bélusca-Maïto         bExplorerIsShell = TRUE;
217c2c66affSColin Finck 
21812de9654SHermès Bélusca-Maïto     if (!bExplorerIsShell)
219c2c66affSColin Finck     {
220c2c66affSColin Finck         return StartWithCommandLine(hInstance);
221c2c66affSColin Finck     }
22212de9654SHermès Bélusca-Maïto #else
22312de9654SHermès Bélusca-Maïto     bExplorerIsShell = TRUE;
224c2c66affSColin Finck #endif
225c2c66affSColin Finck 
226c2c66affSColin Finck     return StartWithDesktop(hInstance);
227c2c66affSColin Finck }
228