shdocvw: Add Open URL dialog to IE.
[wine] / dlls / shdocvw / iexplore.c
1 /*
2  * SHDOCVW - Internet Explorer main frame window
3  *
4  * Copyright 2006 Mike McCormack (for CodeWeavers)
5  * Copyright 2006 Jacek Caban (for CodeWeavers)
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #define COBJMACROS
23
24 #include <stdarg.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "wingdi.h"
29 #include "winnls.h"
30 #include "ole2.h"
31 #include "exdisp.h"
32 #include "oleidl.h"
33
34 #include "shdocvw.h"
35 #include "mshtmcid.h"
36 #include "shellapi.h"
37
38 #include "wine/debug.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(shdocvw);
41
42 #define IDI_APPICON 1
43
44 static const WCHAR szIEWinFrame[] = { 'I','E','F','r','a','m','e',0 };
45
46 /* Windows uses "Microsoft Internet Explorer" */
47 static const WCHAR wszWineInternetExplorer[] =
48         {'W','i','n','e',' ','I','n','t','e','r','n','e','t',' ','E','x','p','l','o','r','e','r',0};
49
50 static INT_PTR CALLBACK ie_dialog_open_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
51 {
52     switch(msg)
53     {
54         case WM_INITDIALOG:
55             return TRUE;
56
57         case WM_COMMAND:
58             switch(LOWORD(wparam))
59             {
60                 case IDOK:
61                 case IDCANCEL:
62                     EndDialog(hwnd, wparam);
63                     return TRUE;
64             }
65     }
66     return FALSE;
67 }
68
69 static void ie_dialog_about(HWND hwnd)
70 {
71     HICON icon = LoadImageW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDI_APPICON), IMAGE_ICON, 48, 48, LR_SHARED);
72
73     ShellAboutW(hwnd, wszWineInternetExplorer, NULL, icon);
74
75     DestroyIcon(icon);
76 }
77
78 static LRESULT iewnd_OnCreate(HWND hwnd, LPCREATESTRUCTW lpcs)
79 {
80     SetWindowLongPtrW(hwnd, 0, (LONG_PTR) lpcs->lpCreateParams);
81     return 0;
82 }
83
84 static LRESULT iewnd_OnSize(InternetExplorer *This, INT width, INT height)
85 {
86     if(This->doc_host.hwnd)
87         SetWindowPos(This->doc_host.hwnd, NULL, 0, 0, width, height,
88                      SWP_NOZORDER | SWP_NOACTIVATE);
89
90     return 0;
91 }
92
93 static LRESULT iewnd_OnDestroy(InternetExplorer *This)
94 {
95     TRACE("%p\n", This);
96
97     This->frame_hwnd = NULL;
98     PostQuitMessage(0); /* FIXME */
99
100     return 0;
101 }
102
103 static LRESULT CALLBACK iewnd_OnCommand(InternetExplorer *This, HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
104 {
105     switch(LOWORD(wparam))
106     {
107         case ID_BROWSE_OPEN:
108             DialogBoxW(shdocvw_hinstance, MAKEINTRESOURCEW(IDD_BROWSE_OPEN), hwnd, ie_dialog_open_proc);
109             break;
110
111         case ID_BROWSE_PRINT:
112             if(This->doc_host.document)
113             {
114                 IOleCommandTarget* target;
115
116                 if(FAILED(IUnknown_QueryInterface(This->doc_host.document, &IID_IOleCommandTarget, (LPVOID*)&target)))
117                     break;
118
119                 IOleCommandTarget_Exec(target, &CGID_MSHTML, IDM_PRINT, OLECMDEXECOPT_DODEFAULT, NULL, NULL);
120
121                 IOleCommandTarget_Release(target);
122             }
123             break;
124
125         case ID_BROWSE_ABOUT:
126             ie_dialog_about(hwnd);
127             break;
128
129         default:
130             return DefWindowProcW(hwnd, msg, wparam, lparam);
131     }
132     return 0;
133 }
134
135 static LRESULT CALLBACK
136 ie_window_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
137 {
138     InternetExplorer *This = (InternetExplorer*) GetWindowLongPtrW(hwnd, 0);
139
140     switch (msg)
141     {
142     case WM_CREATE:
143         return iewnd_OnCreate(hwnd, (LPCREATESTRUCTW)lparam);
144     case WM_DESTROY:
145         return iewnd_OnDestroy(This);
146     case WM_SIZE:
147         return iewnd_OnSize(This, LOWORD(lparam), HIWORD(lparam));
148     case WM_COMMAND:
149         return iewnd_OnCommand(This, hwnd, msg, wparam, lparam);
150     case WM_DOCHOSTTASK:
151         return process_dochost_task(&This->doc_host, lparam);
152     }
153     return DefWindowProcW(hwnd, msg, wparam, lparam);
154 }
155
156 void register_iewindow_class(void)
157 {
158     WNDCLASSEXW wc;
159
160     memset(&wc, 0, sizeof wc);
161     wc.cbSize = sizeof(wc);
162     wc.style = 0;
163     wc.lpfnWndProc = ie_window_proc;
164     wc.cbClsExtra = 0;
165     wc.cbWndExtra = sizeof(InternetExplorer*);
166     wc.hInstance = shdocvw_hinstance;
167     wc.hIcon = LoadIconW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDI_APPICON));
168     wc.hIconSm = LoadImageW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDI_APPICON), IMAGE_ICON,
169                             GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED);
170     wc.hCursor = LoadCursorW(0, MAKEINTRESOURCEW(IDC_ARROW));
171     wc.hbrBackground = 0;
172     wc.lpszClassName = szIEWinFrame;
173     wc.lpszMenuName = NULL;
174
175     RegisterClassExW(&wc);
176 }
177
178 void unregister_iewindow_class(void)
179 {
180     UnregisterClassW(szIEWinFrame, shdocvw_hinstance);
181 }
182
183 static void create_frame_hwnd(InternetExplorer *This)
184 {
185     This->frame_hwnd = CreateWindowExW(
186             WS_EX_WINDOWEDGE,
187             szIEWinFrame, wszWineInternetExplorer,
188             WS_CLIPCHILDREN | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME
189                 | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,
190             CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
191             NULL, NULL /* FIXME */, shdocvw_hinstance, This);
192 }
193
194 static IWebBrowser2 *create_ie_window(LPCSTR cmdline)
195 {
196     IWebBrowser2 *wb = NULL;
197
198     InternetExplorer_Create(NULL, &IID_IWebBrowser2, (void**)&wb);
199     if(!wb)
200         return NULL;
201
202     IWebBrowser2_put_Visible(wb, VARIANT_TRUE);
203     IWebBrowser2_put_MenuBar(wb, VARIANT_TRUE);
204
205     if(!*cmdline) {
206         IWebBrowser2_GoHome(wb);
207     }else {
208         VARIANT var_url;
209         DWORD len;
210         int cmdlen;
211
212         if(!strncasecmp(cmdline, "-nohome", 7))
213             cmdline += 7;
214         while(*cmdline == ' ' || *cmdline == '\t')
215             cmdline++;
216         cmdlen = lstrlenA(cmdline);
217         if(cmdlen > 2 && cmdline[0] == '"' && cmdline[cmdlen-1] == '"') {
218             cmdline++;
219             cmdlen -= 2;
220         }
221
222         V_VT(&var_url) = VT_BSTR;
223
224         len = MultiByteToWideChar(CP_ACP, 0, cmdline, cmdlen, NULL, 0);
225         V_BSTR(&var_url) = SysAllocStringLen(NULL, len);
226         MultiByteToWideChar(CP_ACP, 0, cmdline, cmdlen, V_BSTR(&var_url), len);
227
228         /* navigate to the first page */
229         IWebBrowser2_Navigate2(wb, &var_url, NULL, NULL, NULL, NULL);
230
231         SysFreeString(V_BSTR(&var_url));
232     }
233
234     return wb;
235 }
236
237 HRESULT InternetExplorer_Create(IUnknown *pOuter, REFIID riid, void **ppv)
238 {
239     InternetExplorer *ret;
240     HRESULT hres;
241
242     TRACE("(%p %s %p)\n", pOuter, debugstr_guid(riid), ppv);
243
244     ret = heap_alloc_zero(sizeof(InternetExplorer));
245     ret->ref = 0;
246
247     ret->doc_host.disp = (IDispatch*)WEBBROWSER2(ret);
248     DocHost_Init(&ret->doc_host, (IDispatch*)WEBBROWSER2(ret));
249
250     InternetExplorer_WebBrowser_Init(ret);
251
252     create_frame_hwnd(ret);
253     ret->doc_host.frame_hwnd = ret->frame_hwnd;
254
255     hres = IWebBrowser2_QueryInterface(WEBBROWSER2(ret), riid, ppv);
256     if(FAILED(hres)) {
257         heap_free(ret);
258         return hres;
259     }
260
261     return hres;
262 }
263
264 /******************************************************************
265  *              IEWinMain            (SHDOCVW.101)
266  *
267  * Only returns on error.
268  */
269 DWORD WINAPI IEWinMain(LPSTR szCommandLine, int nShowWindow)
270 {
271     IWebBrowser2 *wb = NULL;
272     MSG msg;
273     HRESULT hres;
274
275     TRACE("%s %d\n", debugstr_a(szCommandLine), nShowWindow);
276
277     if(*szCommandLine == '-' || *szCommandLine == '/') {
278         if(!strcasecmp(szCommandLine+1, "regserver"))
279             return register_iexplore(TRUE);
280         if(!strcasecmp(szCommandLine+1, "unregserver"))
281             return register_iexplore(FALSE);
282     }
283
284     CoInitialize(NULL);
285
286     hres = register_class_object(TRUE);
287     if(FAILED(hres)) {
288         CoUninitialize();
289         ExitProcess(1);
290     }
291
292     if(strcasecmp(szCommandLine, "-embedding"))
293         wb = create_ie_window(szCommandLine);
294
295     /* run the message loop for this thread */
296     while (GetMessageW(&msg, 0, 0, 0))
297     {
298         TranslateMessage(&msg);
299         DispatchMessageW(&msg);
300     }
301
302     if(wb)
303         IWebBrowser2_Release(wb);
304
305     register_class_object(FALSE);
306
307     CoUninitialize();
308
309     ExitProcess(0);
310     return 0;
311 }