[SHELL32] -CShellDispatch: Properly register Shell.Application. Implement CShellDispa...
[reactos.git] / reactos / base / applications / rapps / winmain.cpp
1 /*
2 * PROJECT: ReactOS Applications Manager
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/applications/rapps/winmain.cpp
5 * PURPOSE: Main program
6 * PROGRAMMERS: Dmitry Chapyshev (dmitry@reactos.org)
7 * Ismael Ferreras Morezuelas (swyterzone+ros@gmail.com)
8 */
9
10 #include "rapps.h"
11
12 #include <atlbase.h>
13 #include <atlcom.h>
14 #include <shellapi.h>
15
16 HWND hMainWnd;
17 HINSTANCE hInst;
18 INT SelectedEnumType = ENUM_ALL_COMPONENTS;
19 SETTINGS_INFO SettingsInfo;
20
21 WCHAR szSearchPattern[MAX_STR_LEN] = L"";
22
23 class CRAppsModule : public CComModule
24 {
25 public:
26 };
27
28 BEGIN_OBJECT_MAP(ObjectMap)
29 END_OBJECT_MAP()
30
31 CRAppsModule gModule;
32 CAtlWinModule gWinModule;
33
34 void *operator new (size_t, void *buf)
35 {
36 return buf;
37 }
38
39 static VOID InitializeAtlModule(HINSTANCE hInstance, BOOL bInitialize)
40 {
41 if (bInitialize)
42 {
43 gModule.Init(ObjectMap, hInstance, NULL);
44 }
45 else
46 {
47 gModule.Term();
48 }
49 }
50
51 VOID
52 FillDefaultSettings(PSETTINGS_INFO pSettingsInfo)
53 {
54 pSettingsInfo->bSaveWndPos = TRUE;
55 pSettingsInfo->bUpdateAtStart = FALSE;
56 pSettingsInfo->bLogEnabled = TRUE;
57 StringCbCopyW(pSettingsInfo->szDownloadDir,
58 sizeof(pSettingsInfo->szDownloadDir),
59 L"C:\\Downloads");
60 pSettingsInfo->bDelInstaller = FALSE;
61
62 pSettingsInfo->Maximized = FALSE;
63 pSettingsInfo->Left = CW_USEDEFAULT;
64 pSettingsInfo->Top = CW_USEDEFAULT;
65 pSettingsInfo->Width = 680;
66 pSettingsInfo->Height = 450;
67
68 pSettingsInfo->Proxy = 0;
69 StringCbCopyW(pSettingsInfo->szProxyServer, sizeof(pSettingsInfo->szProxyServer), L"");
70 StringCbCopyW(pSettingsInfo->szNoProxyFor, sizeof(pSettingsInfo->szNoProxyFor), L"");
71 }
72
73 static BOOL
74 LoadSettings(VOID)
75 {
76 HKEY hKey;
77 DWORD dwSize;
78
79 if (RegOpenKeyExW(HKEY_CURRENT_USER, L"Software\\ReactOS\\rapps", 0, KEY_READ, &hKey) == ERROR_SUCCESS)
80 {
81 dwSize = sizeof(SettingsInfo);
82 if (RegQueryValueExW(hKey, L"Settings", NULL, NULL, (LPBYTE)&SettingsInfo, &dwSize) == ERROR_SUCCESS)
83 {
84 RegCloseKey(hKey);
85 return TRUE;
86 }
87
88 RegCloseKey(hKey);
89 }
90
91 return FALSE;
92 }
93
94 VOID
95 SaveSettings(HWND hwnd)
96 {
97 WINDOWPLACEMENT wp;
98 HKEY hKey;
99
100 if (SettingsInfo.bSaveWndPos)
101 {
102 wp.length = sizeof(wp);
103 GetWindowPlacement(hwnd, &wp);
104
105 SettingsInfo.Left = wp.rcNormalPosition.left;
106 SettingsInfo.Top = wp.rcNormalPosition.top;
107 SettingsInfo.Width = wp.rcNormalPosition.right - wp.rcNormalPosition.left;
108 SettingsInfo.Height = wp.rcNormalPosition.bottom - wp.rcNormalPosition.top;
109 SettingsInfo.Maximized = (wp.showCmd == SW_MAXIMIZE || (wp.showCmd == SW_SHOWMINIMIZED && (wp.flags & WPF_RESTORETOMAXIMIZED)));
110 }
111
112 if (RegCreateKeyExW(HKEY_CURRENT_USER, L"Software\\ReactOS\\rapps", 0, NULL,
113 REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL) == ERROR_SUCCESS)
114 {
115 RegSetValueExW(hKey, L"Settings", 0, REG_BINARY, (LPBYTE)&SettingsInfo, sizeof(SettingsInfo));
116 RegCloseKey(hKey);
117 }
118 }
119
120 int WINAPI
121 wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nShowCmd)
122 {
123 WCHAR szWindowClass[] = L"ROSAPPMGR";
124 HANDLE hMutex = NULL;
125 HACCEL KeyBrd;
126 MSG Msg;
127
128 InitializeAtlModule(hInstance, TRUE);
129
130 switch (GetUserDefaultUILanguage())
131 {
132 case MAKELANGID(LANG_HEBREW, SUBLANG_DEFAULT):
133 SetProcessDefaultLayout(LAYOUT_RTL);
134 break;
135
136 default:
137 break;
138 }
139
140 hInst = hInstance;
141
142 hMutex = CreateMutexW(NULL, FALSE, szWindowClass);
143 if ((!hMutex) || (GetLastError() == ERROR_ALREADY_EXISTS))
144 {
145 /* If already started, it is found its window */
146 HWND hWindow = FindWindowW(szWindowClass, NULL);
147
148 /* Activate window */
149 ShowWindow(hWindow, SW_SHOWNORMAL);
150 SetForegroundWindow(hWindow);
151 return 1;
152 }
153
154 if (!LoadSettings())
155 {
156 FillDefaultSettings(&SettingsInfo);
157 }
158
159 InitLogs();
160
161 InitCommonControls();
162
163 hMainWnd = CreateMainWindow();
164 if (!hMainWnd) goto Exit;
165
166 /* Maximize it if we must */
167 ShowWindow(hMainWnd, (SettingsInfo.bSaveWndPos && SettingsInfo.Maximized ? SW_MAXIMIZE : nShowCmd));
168 UpdateWindow(hMainWnd);
169
170 if (SettingsInfo.bUpdateAtStart)
171 UpdateAppsDB();
172
173 /* Load the menu hotkeys */
174 KeyBrd = LoadAccelerators(NULL, MAKEINTRESOURCE(HOTKEYS));
175
176 /* Message Loop */
177 while (GetMessage(&Msg, NULL, 0, 0))
178 {
179 if (!TranslateAccelerator(hMainWnd, KeyBrd, &Msg))
180 {
181 TranslateMessage(&Msg);
182 DispatchMessage(&Msg);
183 }
184 }
185
186 Exit:
187 if (hMutex)
188 CloseHandle(hMutex);
189
190 InitializeAtlModule(hInstance, FALSE);
191
192 return 0;
193 }