[OLEDLG] Sync with Wine Staging 1.7.55. CORE-10536
[reactos.git] / reactos / dll / win32 / oledlg / oledlg_main.c
1 /*
2 * OLEDLG library
3 *
4 * Copyright 1998 Patrik Stridvall
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 "oledlg_private.h"
22
23 WINE_DEFAULT_DEBUG_CHANNEL(ole);
24
25 HINSTANCE OLEDLG_hInstance = 0;
26
27 UINT cf_embed_source;
28 UINT cf_embedded_object;
29 UINT cf_link_source;
30 UINT cf_object_descriptor;
31 UINT cf_link_src_descriptor;
32 UINT cf_ownerlink;
33 UINT cf_filename;
34 UINT cf_filenamew;
35
36 UINT oleui_msg_help;
37 UINT oleui_msg_enddialog;
38
39 static void register_clipboard_formats(void)
40 {
41 /* These used to be declared in olestd.h, but that seems to have been removed from the api */
42 static const WCHAR CF_EMBEDSOURCEW[] = { 'E','m','b','e','d',' ','S','o','u','r','c','e',0 };
43 static const WCHAR CF_EMBEDDEDOBJECTW[] = { 'E','m','b','e','d','d','e','d',' ','O','b','j','e','c','t',0 };
44 static const WCHAR CF_LINKSOURCEW[] = { 'L','i','n','k',' ','S','o','u','r','c','e',0 };
45 static const WCHAR CF_OBJECTDESCRIPTORW[] = { 'O','b','j','e','c','t',' ','D','e','s','c','r','i','p','t','o','r',0 };
46 static const WCHAR CF_LINKSRCDESCRIPTORW[] = { 'L','i','n','k',' ','S','o','u','r','c','e',' ','D','e','s','c','r','i','p','t','o','r',0 };
47 static const WCHAR CF_OWNERLINKW[] = { 'O','w','n','e','r','L','i','n','k',0 };
48 static const WCHAR CF_FILENAMEW[] = { 'F','i','l','e','N','a','m','e',0 };
49 static const WCHAR CF_FILENAMEWW[] = { 'F','i','l','e','N','a','m','e','W',0 };
50
51 /* Load in the same order as native to make debugging easier */
52 cf_object_descriptor = RegisterClipboardFormatW(CF_OBJECTDESCRIPTORW);
53 cf_link_src_descriptor = RegisterClipboardFormatW(CF_LINKSRCDESCRIPTORW);
54 cf_embed_source = RegisterClipboardFormatW(CF_EMBEDSOURCEW);
55 cf_embedded_object = RegisterClipboardFormatW(CF_EMBEDDEDOBJECTW);
56 cf_link_source = RegisterClipboardFormatW(CF_LINKSOURCEW);
57 cf_ownerlink = RegisterClipboardFormatW(CF_OWNERLINKW);
58 cf_filename = RegisterClipboardFormatW(CF_FILENAMEW);
59 cf_filenamew = RegisterClipboardFormatW(CF_FILENAMEWW);
60 }
61
62 static void register_messages(void)
63 {
64 oleui_msg_help = RegisterWindowMessageW(SZOLEUI_MSG_HELPW);
65 oleui_msg_enddialog = RegisterWindowMessageW(SZOLEUI_MSG_ENDDIALOGW);
66 }
67
68 /***********************************************************************
69 * DllMain
70 */
71 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID fImpLoad)
72 {
73 TRACE("%p 0x%x %p\n", hinstDLL, fdwReason, fImpLoad);
74
75 switch(fdwReason) {
76 case DLL_PROCESS_ATTACH:
77 DisableThreadLibraryCalls(hinstDLL);
78 OLEDLG_hInstance = hinstDLL;
79 register_clipboard_formats();
80 register_messages();
81 break;
82 }
83 return TRUE;
84 }
85
86
87 /***********************************************************************
88 * OleUIAddVerbMenuA (OLEDLG.1)
89 */
90 BOOL WINAPI OleUIAddVerbMenuA(IOleObject *object, LPCSTR shorttype,
91 HMENU hMenu, UINT uPos, UINT uIDVerbMin, UINT uIDVerbMax,
92 BOOL addConvert, UINT idConvert, HMENU *lphMenu)
93 {
94 WCHAR *shorttypeW = NULL;
95 BOOL ret;
96
97 TRACE("(%p, %s, %p, %d, %d, %d, %d, %d, %p)\n", object, debugstr_a(shorttype),
98 hMenu, uPos, uIDVerbMin, uIDVerbMax, addConvert, idConvert, lphMenu);
99
100 if (shorttype)
101 {
102 INT len = MultiByteToWideChar(CP_ACP, 0, shorttype, -1, NULL, 0);
103 shorttypeW = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
104 if (shorttypeW)
105 MultiByteToWideChar(CP_ACP, 0, shorttype, -1, shorttypeW, len);
106 }
107
108 ret = OleUIAddVerbMenuW(object, shorttypeW, hMenu, uPos, uIDVerbMin, uIDVerbMax,
109 addConvert, idConvert, lphMenu);
110 HeapFree(GetProcessHeap(), 0, shorttypeW);
111 return ret;
112 }
113
114 static inline BOOL is_verb_in_range(const OLEVERB *verb, UINT idmin, UINT idmax)
115 {
116 if (idmax == 0) return TRUE;
117 return (verb->lVerb + idmin <= idmax);
118 }
119
120 static HRESULT get_next_insertable_verb(IEnumOLEVERB *enumverbs, UINT idmin, UINT idmax, OLEVERB *verb)
121 {
122 memset(verb, 0, sizeof(*verb));
123
124 while (IEnumOLEVERB_Next(enumverbs, 1, verb, NULL) == S_OK) {
125 if (is_verb_in_range(verb, idmin, idmax) && (verb->grfAttribs & OLEVERBATTRIB_ONCONTAINERMENU))
126 return S_OK;
127
128 CoTaskMemFree(verb->lpszVerbName);
129 memset(verb, 0, sizeof(*verb));
130 }
131
132 return S_FALSE;
133 }
134
135 static void insert_verb_to_menu(HMENU menu, UINT idmin, const OLEVERB *verb)
136 {
137 InsertMenuW(menu, ~0, verb->fuFlags | MF_BYPOSITION | MF_STRING, verb->lVerb + idmin, verb->lpszVerbName);
138 }
139
140 /***********************************************************************
141 * OleUIAddVerbMenuW (OLEDLG.14)
142 */
143 BOOL WINAPI OleUIAddVerbMenuW(IOleObject *object, LPCWSTR shorttype,
144 HMENU hMenu, UINT uPos, UINT idmin, UINT idmax, BOOL addConvert, UINT idConvert, HMENU *ret_submenu)
145 {
146 IEnumOLEVERB *enumverbs = NULL;
147 LPOLESTR usertype = NULL;
148 OLEVERB firstverb, verb;
149 WCHAR *objecttype;
150 WCHAR resstrW[32]; /* should be enough */
151 DWORD_PTR args[2];
152 BOOL singleverb;
153 HMENU submenu;
154 WCHAR *str;
155
156 TRACE("(%p, %s, %p, %d, %d, %d, %d, %d, %p)\n", object, debugstr_w(shorttype),
157 hMenu, uPos, idmin, idmax, addConvert, idConvert, ret_submenu);
158
159 if (ret_submenu)
160 *ret_submenu = NULL;
161
162 if (!hMenu || !ret_submenu)
163 return FALSE;
164
165 /* check if we can get verbs at all */
166 if (object)
167 IOleObject_EnumVerbs(object, &enumverbs);
168
169 LoadStringW(OLEDLG_hInstance, IDS_VERBMENU_OBJECT, resstrW, sizeof(resstrW)/sizeof(WCHAR));
170 /* no object, or object without enumeration support */
171 if (!object || !enumverbs) {
172 InsertMenuW(hMenu, uPos, MF_BYPOSITION|MF_STRING|MF_GRAYED, idmin, resstrW);
173 return FALSE;
174 }
175
176 /* root entry string */
177 if (!shorttype && (IOleObject_GetUserType(object, USERCLASSTYPE_SHORT, &usertype) == S_OK))
178 objecttype = usertype;
179 else
180 objecttype = (WCHAR*)shorttype;
181
182 /* iterate through verbs */
183
184 /* find first suitable verb */
185 get_next_insertable_verb(enumverbs, idmin, idmax, &firstverb);
186 singleverb = get_next_insertable_verb(enumverbs, idmin, idmax, &verb) != S_OK;
187
188 if (singleverb && !addConvert) {
189 LoadStringW(OLEDLG_hInstance, IDS_VERBMENU_SINGLEVERB_OBJECT, resstrW, sizeof(resstrW)/sizeof(WCHAR));
190
191 args[0] = (DWORD_PTR)firstverb.lpszVerbName;
192 args[1] = (DWORD_PTR)objecttype;
193
194 FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_ARGUMENT_ARRAY,
195 resstrW, 0, 0, (WCHAR*)&str, 0, (__ms_va_list*)args);
196
197 RemoveMenu(hMenu, uPos, MF_BYPOSITION);
198 InsertMenuW(hMenu, uPos, MF_BYPOSITION|MF_STRING, idmin, str);
199 CoTaskMemFree(firstverb.lpszVerbName);
200 HeapFree(GetProcessHeap(), 0, str);
201 IEnumOLEVERB_Release(enumverbs);
202 CoTaskMemFree(usertype);
203 return TRUE;
204 }
205
206 submenu = CreatePopupMenu();
207 insert_verb_to_menu(submenu, idmin, &firstverb);
208 CoTaskMemFree(firstverb.lpszVerbName);
209
210 if (!singleverb) {
211 insert_verb_to_menu(submenu, idmin, &verb);
212 CoTaskMemFree(verb.lpszVerbName);
213 }
214
215 while (get_next_insertable_verb(enumverbs, idmin, idmax, &verb) == S_OK) {
216 insert_verb_to_menu(submenu, idmin, &verb);
217 CoTaskMemFree(verb.lpszVerbName);
218 }
219
220 /* convert verb is at the bottom of a popup, separated from verbs */
221 if (addConvert) {
222 LoadStringW(OLEDLG_hInstance, IDS_VERBMENU_CONVERT, resstrW, sizeof(resstrW)/sizeof(WCHAR));
223 InsertMenuW(submenu, ~0, MF_BYPOSITION|MF_SEPARATOR, 0, NULL);
224 InsertMenuW(submenu, ~0, MF_BYPOSITION|MF_STRING, idConvert, resstrW);
225 }
226
227 if (submenu)
228 *ret_submenu = submenu;
229
230 /* now submenu is ready, add root entry to original menu, attach submenu */
231 LoadStringW(OLEDLG_hInstance, IDS_VERBMENU_OBJECT_WITH_NAME, resstrW, sizeof(resstrW)/sizeof(WCHAR));
232
233 args[0] = (DWORD_PTR)objecttype;
234 FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_ARGUMENT_ARRAY,
235 resstrW, 0, 0, (WCHAR*)&str, 0, (__ms_va_list*)args);
236
237 InsertMenuW(hMenu, uPos, MF_BYPOSITION|MF_POPUP|MF_STRING, (UINT_PTR)submenu, str);
238 HeapFree(GetProcessHeap(), 0, str);
239 IEnumOLEVERB_Release(enumverbs);
240 CoTaskMemFree(usertype);
241 return TRUE;
242 }
243
244 /***********************************************************************
245 * OleUICanConvertOrActivateAs (OLEDLG.2)
246 */
247 BOOL WINAPI OleUICanConvertOrActivateAs(
248 REFCLSID rClsid, BOOL fIsLinkedObject, WORD wFormat)
249 {
250 FIXME("(%p, %d, %hd): stub\n",
251 rClsid, fIsLinkedObject, wFormat
252 );
253 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
254 return FALSE;
255 }
256
257 /***********************************************************************
258 * OleUIInsertObjectW (OLEDLG.20)
259 */
260 UINT WINAPI OleUIInsertObjectW(LPOLEUIINSERTOBJECTW lpOleUIInsertObject)
261 {
262 FIXME("(%p): stub\n", lpOleUIInsertObject);
263 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
264 return OLEUI_FALSE;
265 }
266
267 /***********************************************************************
268 * OleUIEditLinksA (OLEDLG.5)
269 */
270 UINT WINAPI OleUIEditLinksA(LPOLEUIEDITLINKSA lpOleUIEditLinks)
271 {
272 FIXME("(%p): stub\n", lpOleUIEditLinks);
273 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
274 return OLEUI_FALSE;
275 }
276
277 /***********************************************************************
278 * OleUIEditLinksW (OLEDLG.19)
279 */
280 UINT WINAPI OleUIEditLinksW(LPOLEUIEDITLINKSW lpOleUIEditLinks)
281 {
282 FIXME("(%p): stub\n", lpOleUIEditLinks);
283 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
284 return OLEUI_FALSE;
285 }
286
287 /***********************************************************************
288 * OleUIChangeIconA (OLEDLG.6)
289 */
290 UINT WINAPI OleUIChangeIconA(
291 LPOLEUICHANGEICONA lpOleUIChangeIcon)
292 {
293 FIXME("(%p): stub\n", lpOleUIChangeIcon);
294 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
295 return OLEUI_FALSE;
296 }
297
298 /***********************************************************************
299 * OleUIChangeIconW (OLEDLG.16)
300 */
301 UINT WINAPI OleUIChangeIconW(
302 LPOLEUICHANGEICONW lpOleUIChangeIcon)
303 {
304 FIXME("(%p): stub\n", lpOleUIChangeIcon);
305 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
306 return OLEUI_FALSE;
307 }
308
309 /***********************************************************************
310 * OleUIConvertA (OLEDLG.7)
311 */
312 UINT WINAPI OleUIConvertA(LPOLEUICONVERTA lpOleUIConvert)
313 {
314 FIXME("(%p): stub\n", lpOleUIConvert);
315 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
316 return OLEUI_FALSE;
317 }
318
319 /***********************************************************************
320 * OleUIConvertW (OLEDLG.18)
321 */
322 UINT WINAPI OleUIConvertW(LPOLEUICONVERTW lpOleUIConvert)
323 {
324 FIXME("(%p): stub\n", lpOleUIConvert);
325 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
326 return OLEUI_FALSE;
327 }
328
329 /***********************************************************************
330 * OleUIBusyA (OLEDLG.8)
331 */
332 UINT WINAPI OleUIBusyA(LPOLEUIBUSYA lpOleUIBusy)
333 {
334 FIXME("(%p): stub\n", lpOleUIBusy);
335 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
336 return OLEUI_FALSE;
337 }
338
339 /***********************************************************************
340 * OleUIBusyW (OLEDLG.15)
341 */
342 UINT WINAPI OleUIBusyW(LPOLEUIBUSYW lpOleUIBusy)
343 {
344 FIXME("(%p): stub\n", lpOleUIBusy);
345 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
346 return OLEUI_FALSE;
347 }
348
349 /***********************************************************************
350 * OleUIUpdateLinksA (OLEDLG.9)
351 */
352 BOOL WINAPI OleUIUpdateLinksA(
353 LPOLEUILINKCONTAINERA lpOleUILinkCntr,
354 HWND hwndParent, LPSTR lpszTitle, INT cLinks)
355 {
356 FIXME("(%p, %p, %s, %d): stub\n",
357 lpOleUILinkCntr, hwndParent, debugstr_a(lpszTitle), cLinks
358 );
359 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
360 return FALSE;
361 }
362
363 /***********************************************************************
364 * OleUIUpdateLinksW (OLEDLG.23)
365 */
366 BOOL WINAPI OleUIUpdateLinksW(
367 LPOLEUILINKCONTAINERW lpOleUILinkCntr,
368 HWND hwndParent, LPWSTR lpszTitle, INT cLinks)
369 {
370 FIXME("(%p, %p, %s, %d): stub\n",
371 lpOleUILinkCntr, hwndParent, debugstr_w(lpszTitle), cLinks
372 );
373 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
374 return FALSE;
375 }
376
377 /***********************************************************************
378 * OleUIPromptUserA (OLEDLG.10)
379 */
380 INT WINAPIV OleUIPromptUserA(
381 INT nTemplate, HWND hwndParent, ...)
382 {
383 FIXME("(%d, %p, ...): stub\n", nTemplate, hwndParent);
384 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
385 return OLEUI_FALSE;
386 }
387
388 /***********************************************************************
389 * OleUIPromptUserW (OLEDLG.13)
390 */
391 INT WINAPIV OleUIPromptUserW(
392 INT nTemplate, HWND hwndParent, ...)
393 {
394 FIXME("(%d, %p, ...): stub\n", nTemplate, hwndParent);
395 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
396 return OLEUI_FALSE;
397 }
398
399 /***********************************************************************
400 * OleUIObjectPropertiesA (OLEDLG.11)
401 */
402 UINT WINAPI OleUIObjectPropertiesA(
403 LPOLEUIOBJECTPROPSA lpOleUIObjectProps)
404 {
405 FIXME("(%p): stub\n", lpOleUIObjectProps);
406 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
407 return OLEUI_FALSE;
408 }
409
410 /***********************************************************************
411 * OleUIObjectPropertiesW (OLEDLG.21)
412 */
413 UINT WINAPI OleUIObjectPropertiesW(
414 LPOLEUIOBJECTPROPSW lpOleUIObjectProps)
415 {
416 FIXME("(%p): stub\n", lpOleUIObjectProps);
417 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
418 return OLEUI_FALSE;
419 }
420
421 /***********************************************************************
422 * OleUIChangeSourceA (OLEDLG.12)
423 */
424 UINT WINAPI OleUIChangeSourceA(
425 LPOLEUICHANGESOURCEA lpOleUIChangeSource)
426 {
427 FIXME("(%p): stub\n", lpOleUIChangeSource);
428 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
429 return OLEUI_FALSE;
430 }
431
432 /***********************************************************************
433 * OleUIChangeSourceW (OLEDLG.17)
434 */
435 UINT WINAPI OleUIChangeSourceW(
436 LPOLEUICHANGESOURCEW lpOleUIChangeSource)
437 {
438 FIXME("(%p): stub\n", lpOleUIChangeSource);
439 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
440 return OLEUI_FALSE;
441 }