[SHELL] Reapply r65477 (rename explorer-new to explorer).
[reactos.git] / reactos / base / shell / explorer / explorer.cpp
1 /*
2 * ReactOS Explorer
3 *
4 * Copyright 2006 - 2007 Thomas Weidenmueller <w3seek@reactos.org>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include "precomp.h"
22 #include <shlwapi_undoc.h>
23
24 DWORD WINAPI _WinList_Init(void);
25 void WINAPI _ShellDDEInit(BOOL bInit);
26
27 HINSTANCE hExplorerInstance;
28 HMODULE hUser32;
29 HANDLE hProcessHeap;
30 HKEY hkExplorer = NULL;
31 DRAWCAPTEMP DrawCapTemp = NULL;
32
33 class CExplorerModule : public CComModule
34 {
35 public:
36 };
37
38 BEGIN_OBJECT_MAP(ObjectMap)
39 END_OBJECT_MAP()
40
41 CExplorerModule gModule;
42 CAtlWinModule gWinModule;
43
44 void *operator new (size_t, void *buf)
45 {
46 return buf;
47 }
48
49 static VOID InitializeAtlModule(HINSTANCE hInstance, BOOL bInitialize)
50 {
51 if (bInitialize)
52 {
53 /* HACK - the global constructors don't run, so I placement new them here */
54 new (&gModule) CExplorerModule;
55 new (&gWinModule) CAtlWinModule;
56 new (&_AtlBaseModule) CAtlBaseModule;
57 new (&_AtlComModule) CAtlComModule;
58
59 gModule.Init(ObjectMap, hInstance, NULL);
60 }
61 else
62 {
63 gModule.Term();
64 }
65 }
66
67 #if !WIN7_COMPAT_MODE
68 static BOOL
69 SetShellReadyEvent(IN LPCTSTR lpEventName)
70 {
71 HANDLE hEvent;
72
73 hEvent = OpenEvent(EVENT_MODIFY_STATE, FALSE, lpEventName);
74 if (hEvent != NULL)
75 {
76 SetEvent(hEvent);
77
78 CloseHandle(hEvent);
79 return TRUE;
80 }
81
82 return FALSE;
83 }
84
85 static VOID
86 HideMinimizedWindows(IN BOOL bHide)
87 {
88 MINIMIZEDMETRICS mm;
89
90 mm.cbSize = sizeof(mm);
91 if (!SystemParametersInfo(SPI_GETMINIMIZEDMETRICS, sizeof(mm), &mm, 0))
92 {
93 ERR("SystemParametersInfo failed with %lu\n", GetLastError());
94 return;
95 }
96 if (bHide)
97 mm.iArrange |= ARW_HIDE;
98 else
99 mm.iArrange &= ~ARW_HIDE;
100 if (!SystemParametersInfo(SPI_SETMINIMIZEDMETRICS, sizeof(mm), &mm, 0))
101 ERR("SystemParametersInfo failed with %lu\n", GetLastError());
102 }
103
104 static INT
105 StartWithCommandLine(IN HINSTANCE hInstance)
106 {
107 BOOL b = FALSE;
108 EXPLORER_CMDLINE_PARSE_RESULTS parseResults = { 0 };
109
110 if (SHExplorerParseCmdLine(&parseResults))
111 b = SHCreateFromDesktop(&parseResults);
112
113 if (parseResults.strPath)
114 SHFree(parseResults.strPath);
115
116 if (parseResults.pidlPath)
117 ILFree(parseResults.pidlPath);
118
119 if (parseResults.pidlRoot)
120 ILFree(parseResults.pidlRoot);
121
122 return b;
123 }
124 #endif
125
126 static INT
127 StartWithDesktop(IN HINSTANCE hInstance)
128 {
129 InitializeAtlModule(hInstance, TRUE);
130
131 if (RegOpenKey(HKEY_CURRENT_USER,
132 L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer",
133 &hkExplorer) != ERROR_SUCCESS)
134 {
135 WCHAR Message[256];
136 LoadString(hInstance, IDS_STARTUP_ERROR, Message, 256);
137 MessageBox(NULL, Message, NULL, MB_ICONERROR);
138 return 1;
139 }
140
141 hExplorerInstance = hInstance;
142 hProcessHeap = GetProcessHeap();
143 LoadAdvancedSettings();
144
145 hUser32 = GetModuleHandle(TEXT("USER32.DLL"));
146 if (hUser32 != NULL)
147 {
148 DrawCapTemp = (DRAWCAPTEMP) GetProcAddress(hUser32, PROC_NAME_DRAWCAPTIONTEMP);
149 }
150
151 InitCommonControls();
152 OleInitialize(NULL);
153
154 #if !WIN7_COMPAT_MODE
155 ProcessStartupItems();
156
157 /* Initialize shell dde support */
158 _ShellDDEInit(TRUE);
159 #endif
160
161 /* Initialize shell icons */
162 FileIconInit(TRUE);
163
164 /* Initialize CLSID_ShellWindows class */
165 _WinList_Init();
166
167 CComPtr<ITrayWindow> Tray;
168 CreateTrayWindow(&Tray);
169
170 #if !WIN7_COMPAT_MODE
171 /* This not only hides the minimized window captions in the bottom
172 left screen corner, but is also needed in order to receive
173 HSHELL_* notification messages (which are required for taskbar
174 buttons to work right) */
175 HideMinimizedWindows(TRUE);
176
177 HANDLE hShellDesktop = NULL;
178 if (Tray != NULL)
179 hShellDesktop = DesktopCreateWindow(Tray);
180
181 /* WinXP: Notify msgina to hide the welcome screen */
182 if (!SetShellReadyEvent(TEXT("msgina: ShellReadyEvent")))
183 SetShellReadyEvent(TEXT("Global\\msgina: ShellReadyEvent"));
184 #endif
185
186 if (Tray != NULL)
187 {
188 #if !WIN7_COMPAT_MODE
189 RegisterHotKey(NULL, IDHK_RUN, MOD_WIN, 'R');
190 #endif
191 TrayMessageLoop(Tray);
192 #if !WIN7_COMPAT_MODE
193 HideMinimizedWindows(FALSE);
194 #endif
195 }
196
197 #if !WIN7_COMPAT_MODE
198 if (hShellDesktop != NULL)
199 DesktopDestroyShellWindow(hShellDesktop);
200 #endif
201
202 OleUninitialize();
203
204 RegCloseKey(hkExplorer);
205 hkExplorer = NULL;
206
207 InitializeAtlModule(hInstance, FALSE);
208
209 return 0;
210 }
211
212 INT WINAPI
213 _tWinMain(IN HINSTANCE hInstance,
214 IN HINSTANCE hPrevInstance,
215 IN LPTSTR lpCmdLine,
216 IN INT nCmdShow)
217 {
218 #if !WIN7_COMPAT_MODE
219 BOOL CreateShellDesktop = FALSE;
220
221 TRACE("Explorer starting... Commandline: %S\n", lpCmdLine);
222
223 /*
224 * Set our shutdown parameters: we want to shutdown the very last,
225 * but before any TaskMgr instance (which has a shutdown level of 1).
226 */
227 SetProcessShutdownParameters(2, 0);
228
229 if (GetShellWindow() == NULL)
230 CreateShellDesktop = TRUE;
231
232 if (!CreateShellDesktop)
233 {
234 return StartWithCommandLine(hInstance);
235 }
236 #endif
237
238 return StartWithDesktop(hInstance);
239 }