winsta: fix spec file
[reactos.git] / reactos / base / applications / rapps / misc.c
1 /*
2 * PROJECT: ReactOS Applications Manager
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/applications/rapps/misc.c
5 * PURPOSE: Misc functions
6 * PROGRAMMERS: Dmitry Chapyshev (dmitry@reactos.org)
7 */
8
9 #include "rapps.h"
10
11
12 /* SESSION Operation */
13 #define EXTRACT_FILLFILELIST 0x00000001
14 #define EXTRACT_EXTRACTFILES 0x00000002
15
16 typedef struct
17 {
18 int erfOper;
19 int erfType;
20 BOOL fError;
21 } ERF, *PERF;
22
23 struct FILELIST
24 {
25 LPSTR FileName;
26 struct FILELIST *next;
27 BOOL DoExtract;
28 };
29
30 typedef struct
31 {
32 INT FileSize;
33 ERF Error;
34 struct FILELIST *FileList;
35 INT FileCount;
36 INT Operation;
37 CHAR Destination[MAX_PATH];
38 CHAR CurrentFile[MAX_PATH];
39 CHAR Reserved[MAX_PATH];
40 struct FILELIST *FilterList;
41 } SESSION;
42
43 HRESULT (WINAPI *pfnExtract)(SESSION *dest, LPCSTR szCabName);
44
45
46 INT
47 GetSystemColorDepth(VOID)
48 {
49 DEVMODE pDevMode;
50 INT ColorDepth;
51
52 pDevMode.dmSize = sizeof(DEVMODE);
53 pDevMode.dmDriverExtra = 0;
54
55 if (!EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &pDevMode))
56 {
57 /* TODO: Error message */
58 return ILC_COLOR;
59 }
60
61 switch (pDevMode.dmBitsPerPel)
62 {
63 case 32: ColorDepth = ILC_COLOR32; break;
64 case 24: ColorDepth = ILC_COLOR24; break;
65 case 16: ColorDepth = ILC_COLOR16; break;
66 case 8: ColorDepth = ILC_COLOR8; break;
67 case 4: ColorDepth = ILC_COLOR4; break;
68 default: ColorDepth = ILC_COLOR; break;
69 }
70
71 return ColorDepth;
72 }
73
74 int
75 GetWindowWidth(HWND hwnd)
76 {
77 RECT Rect;
78
79 GetWindowRect(hwnd, &Rect);
80 return (Rect.right - Rect.left);
81 }
82
83 int
84 GetWindowHeight(HWND hwnd)
85 {
86 RECT Rect;
87
88 GetWindowRect(hwnd, &Rect);
89 return (Rect.bottom - Rect.top);
90 }
91
92 int
93 GetClientWindowWidth(HWND hwnd)
94 {
95 RECT Rect;
96
97 GetClientRect(hwnd, &Rect);
98 return (Rect.right - Rect.left);
99 }
100
101 int
102 GetClientWindowHeight(HWND hwnd)
103 {
104 RECT Rect;
105
106 GetClientRect(hwnd, &Rect);
107 return (Rect.bottom - Rect.top);
108 }
109
110 VOID
111 CopyTextToClipboard(LPCWSTR lpszText)
112 {
113 if(OpenClipboard(NULL))
114 {
115 HGLOBAL ClipBuffer;
116 WCHAR *Buffer;
117
118 EmptyClipboard();
119 ClipBuffer = GlobalAlloc(GMEM_DDESHARE, (wcslen(lpszText) + 1) * sizeof(TCHAR));
120 Buffer = (WCHAR*)GlobalLock(ClipBuffer);
121 wcscpy(Buffer, lpszText);
122 GlobalUnlock(ClipBuffer);
123
124 SetClipboardData(CF_UNICODETEXT, ClipBuffer);
125
126 CloseClipboard();
127 }
128 }
129
130 VOID
131 SetWelcomeText(VOID)
132 {
133 WCHAR szText[MAX_STR_LEN*3];
134
135 LoadStringW(hInst, IDS_WELCOME_TITLE, szText, sizeof(szText) / sizeof(WCHAR));
136 NewRichEditText(szText, CFE_BOLD);
137
138 LoadStringW(hInst, IDS_WELCOME_TEXT, szText, sizeof(szText) / sizeof(WCHAR));
139 InsertRichEditText(szText, 0);
140
141 LoadStringW(hInst, IDS_WELCOME_URL, szText, sizeof(szText) / sizeof(WCHAR));
142 InsertRichEditText(szText, CFM_LINK);
143 }
144
145 VOID
146 ShowPopupMenu(HWND hwnd, UINT MenuID)
147 {
148 HMENU hPopupMenu = GetSubMenu(LoadMenuW(hInst, MAKEINTRESOURCEW(MenuID)), 0);
149 POINT pt;
150
151 GetCursorPos(&pt);
152
153 SetForegroundWindow(hwnd);
154 TrackPopupMenu(hPopupMenu, 0, pt.x, pt.y, 0, hMainWnd, NULL);
155
156 DestroyMenu(hPopupMenu);
157 }
158
159 BOOL
160 StartProcess(LPWSTR lpPath, BOOL Wait)
161 {
162 PROCESS_INFORMATION pi;
163 STARTUPINFOW si;
164 DWORD dwRet;
165 MSG msg;
166
167 ZeroMemory(&si, sizeof(si));
168 si.cb = sizeof(si);
169 si.wShowWindow = SW_SHOW;
170
171 if (!CreateProcessW(NULL, lpPath, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
172 {
173 return FALSE;
174 }
175
176 CloseHandle(pi.hThread);
177 if (Wait) EnableWindow(hMainWnd, FALSE);
178
179 while (Wait)
180 {
181 dwRet = MsgWaitForMultipleObjects(1, &pi.hProcess, FALSE, INFINITE, QS_ALLEVENTS);
182 if (dwRet == WAIT_OBJECT_0 + 1)
183 {
184 while (PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE))
185 {
186 TranslateMessage(&msg);
187 DispatchMessage(&msg);
188 }
189 }
190 else
191 {
192 if (dwRet == WAIT_OBJECT_0 || dwRet == WAIT_FAILED)
193 break;
194 }
195 }
196
197 CloseHandle(pi.hProcess);
198
199 if (Wait)
200 {
201 EnableWindow(hMainWnd, TRUE);
202 SetForegroundWindow(hMainWnd);
203 SetFocus(hMainWnd);
204 }
205
206 return TRUE;
207 }
208
209
210 BOOL
211 ExtractFilesFromCab(LPWSTR lpCabName, LPWSTR lpOutputPath)
212 {
213 HINSTANCE hCabinetDll;
214 CHAR szCabName[MAX_PATH];
215 SESSION Dest;
216 HRESULT Result;
217
218 hCabinetDll = LoadLibraryW(L"cabinet.dll");
219 if (hCabinetDll)
220 {
221 pfnExtract = (void *) GetProcAddress(hCabinetDll, "Extract");
222 if (pfnExtract)
223 {
224 ZeroMemory(&Dest, sizeof(SESSION));
225
226 WideCharToMultiByte(CP_ACP, 0, lpOutputPath, -1, Dest.Destination, MAX_PATH, NULL, NULL);
227 WideCharToMultiByte(CP_ACP, 0, lpCabName, -1, szCabName, MAX_PATH, NULL, NULL);
228 Dest.Operation = EXTRACT_FILLFILELIST;
229
230 Result = pfnExtract(&Dest, szCabName);
231 if (Result == S_OK)
232 {
233 Dest.Operation = EXTRACT_EXTRACTFILES;
234 Result = pfnExtract(&Dest, szCabName);
235 if (Result == S_OK)
236 {
237 FreeLibrary(hCabinetDll);
238 return TRUE;
239 }
240 }
241 }
242 FreeLibrary(hCabinetDll);
243 }
244
245 return FALSE;
246 }