32e7b52e3d13f16881a81416214b46d26c97d4df
[reactos.git] / dll / win32 / printui / printui.c
1 /*
2 * Implementation of the Printer User Interface Dialogs
3 *
4 * Copyright 2006-2007 Detlef Riekenberg
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 St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #include <stdarg.h>
22
23 #define COBJMACROS
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "wingdi.h"
28 #include "winuser.h"
29 #include "winreg.h"
30 #include "winver.h"
31 #include "winnls.h"
32 #include "shellapi.h"
33
34 #include "wine/unicode.h"
35 #include "wine/debug.h"
36 #include "printui_private.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(printui);
39
40 /* ################################# */
41
42 /* Must be in order with OPT_* */
43 static const WCHAR optionsW[OPT_MAX+1]={'a','b','c','f','h','j','l','m','n','t','r','v',0};
44
45 /* Must be in order with FLAG_* */
46 static const WCHAR flagsW[FLAG_MAX+1]={'q','w','y','z','Z',0};
47
48
49 /* ################################
50 * get_next_wstr() [Internal]
51 *
52 * Get the next WSTR, when available
53 *
54 */
55
56 static LPWSTR get_next_wstr(context_t * cx)
57 {
58 LPWSTR ptr;
59
60 ptr = cx->pNextCharW;
61 if (ptr && ptr[0]) {
62 cx->pNextCharW = NULL;
63 return ptr;
64 }
65
66 /* Get the next Parameter, when available */
67 if (cx->next_arg < cx->argc) {
68 ptr = cx->argv[cx->next_arg];
69 cx->next_arg++;
70 cx->pNextCharW = NULL;
71 return ptr;
72 }
73 return NULL;
74 }
75
76
77 /* ################################
78 * get_next_wchar() [Internal]
79 *
80 * Get the next WCHAR from the Commandline or from the File (@ Filename)
81 *
82 * ToDo: Support Parameter from a File ( "@Filename" )
83 *
84 */
85
86 static WCHAR get_next_wchar(context_t * cx, BOOL use_next_parameter)
87 {
88 WCHAR c;
89
90 /* Try the next WCHAR in the actual Parameter */
91 if (cx->pNextCharW) {
92 c = *cx->pNextCharW;
93 if (c) {
94 cx->pNextCharW++;
95 return c;
96 }
97 /* We reached the end of the Parameter */
98 cx->pNextCharW = NULL;
99 }
100
101 /* Get the next Parameter, when available and allowed */
102 if ((cx->pNextCharW == NULL) && (cx->next_arg < cx->argc) && (use_next_parameter)) {
103 cx->pNextCharW = cx->argv[cx->next_arg];
104 cx->next_arg++;
105 }
106
107 if (cx->pNextCharW) {
108 c = *cx->pNextCharW;
109 if (c) {
110 cx->pNextCharW++;
111 }
112 else
113 {
114 /* We reached the end of the Parameter */
115 cx->pNextCharW = NULL;
116 }
117 return c;
118 }
119 return '\0';
120 }
121
122 /* ################################ */
123 static BOOL parse_rundll(context_t * cx)
124 {
125 LPWSTR ptr;
126 DWORD index;
127 WCHAR txtW[2];
128 WCHAR c;
129
130
131 c = get_next_wchar(cx, TRUE);
132 txtW[1] = '\0';
133
134 while (c)
135 {
136
137 while ( (c == ' ') || (c == '\t'))
138 {
139 c = get_next_wchar(cx, TRUE);
140 }
141 txtW[0] = c;
142
143 if (c == '@') {
144 /* read commands from a File */
145 ptr = get_next_wstr(cx);
146 FIXME("redir not supported: %s\n", debugstr_w(ptr));
147 return FALSE;
148 }
149 else if (c == '/') {
150 c = get_next_wchar(cx, FALSE);
151 while ( c )
152 {
153 txtW[0] = c;
154 ptr = strchrW(optionsW, c);
155 if (ptr) {
156 index = ptr - optionsW;
157 cx->options[index] = get_next_wstr(cx);
158 TRACE(" opt: %s %s\n", debugstr_w(txtW), debugstr_w(cx->options[index]));
159 c = 0;
160 }
161 else
162 {
163 ptr = strchrW(flagsW, c);
164 if (ptr) {
165 index = ptr - flagsW;
166 cx->flags[index] = TRUE;
167 TRACE("flag: %s\n", debugstr_w(txtW));
168 }
169 else
170 {
171 cx->command = c;
172 cx->subcommand = '\0';
173 TRACE(" cmd: %s\n", debugstr_w(txtW));
174 }
175
176 /* help has priority over all commands */
177 if (c == '?') {
178 return TRUE;
179 }
180
181 c = get_next_wchar(cx, FALSE);
182
183 /* Some commands use two wchar */
184 if ((cx->command == 'd') || (cx->command == 'g') || (cx->command == 'i') ||
185 (cx->command == 'S') || (cx->command == 'X') ){
186 cx->subcommand = c;
187 txtW[0] = c;
188 TRACE(" sub: %s\n", debugstr_w(txtW));
189 c = get_next_wchar(cx, FALSE);
190 }
191 }
192 }
193 c = get_next_wchar(cx, TRUE);
194
195 }
196 else
197 {
198 /* The commands 'S' and 'X' have additional Parameter */
199 if ((cx->command == 'S') || (cx->command == 'X')) {
200
201 /* the actual WCHAR is the start from the extra Parameter */
202 cx->pNextCharW--;
203 TRACE("%d extra Parameter, starting with %s\n", 1 + (cx->argc - cx->next_arg), debugstr_w(cx->pNextCharW));
204 return TRUE;
205 }
206 FIXME("0x%x: %s is unknown\n", c, debugstr_wn(&c, 1));
207 return FALSE;
208 }
209
210 }
211 return TRUE;
212 }
213
214 /*****************************************************
215 * DllMain
216 */
217 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
218 {
219 TRACE("(%p, %d, %p)\n",hinstDLL, fdwReason, lpvReserved);
220
221 switch(fdwReason)
222 {
223 case DLL_WINE_PREATTACH:
224 return FALSE; /* prefer native version */
225
226 case DLL_PROCESS_ATTACH:
227 DisableThreadLibraryCalls( hinstDLL );
228 break;
229 }
230 return TRUE;
231 }
232
233
234 /*****************************************************
235 * PrintUIEntryW [printui.@]
236 * Commandline-Interface for using printui.dll with rundll32.exe
237 *
238 */
239 void WINAPI PrintUIEntryW(HWND hWnd, HINSTANCE hInst, LPCWSTR pCommand, DWORD nCmdShow)
240 {
241 context_t cx;
242 BOOL res = FALSE;
243
244 TRACE("(%p, %p, %s, 0x%x)\n", hWnd, hInst, debugstr_w(pCommand), nCmdShow);
245
246 memset(&cx, 0, sizeof(context_t));
247 cx.hWnd = hWnd;
248 cx.nCmdShow = nCmdShow;
249
250 if ((pCommand) && (pCommand[0])) {
251 /* result is allocated with GlobalAlloc() */
252 cx.argv = CommandLineToArgvW(pCommand, &cx.argc);
253 TRACE("got %d args at %p\n", cx.argc, cx.argv);
254
255 res = parse_rundll(&cx);
256 }
257
258 if (res && cx.command) {
259 switch (cx.command)
260 {
261
262 default:
263 {
264 WCHAR txtW[3];
265 txtW[0] = cx.command;
266 txtW[1] = cx.subcommand;
267 txtW[2] = '\0';
268 FIXME("command not implemented: %s\n", debugstr_w(txtW));
269 }
270 }
271 }
272
273 if ((res == FALSE) || (cx.command == '\0')) {
274 FIXME("dialog: Printer / The operation was not successful\n");
275 }
276
277 GlobalFree(cx.argv);
278
279 }