ddraw: Fix stupid bug when setting surface client memory.
[wine] / dlls / mshtml / task.c
1 /*
2  * Copyright 2006 Jacek Caban for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include "config.h"
20
21 #include <stdarg.h>
22 #include <stdio.h>
23
24 #define COBJMACROS
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "ole2.h"
30 #include "mshtmcid.h"
31
32 #include "wine/debug.h"
33
34 #include "mshtml_private.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
37
38 #define WM_PROCESSTASK 0x8008
39
40 void push_task(task_t *task)
41 {
42     thread_data_t *thread_data = get_thread_data(TRUE);
43
44     if(thread_data->task_queue_tail)
45         thread_data->task_queue_tail->next = task;
46     else
47         thread_data->task_queue_head = task;
48
49     thread_data->task_queue_tail = task;
50
51     PostMessageW(thread_data->thread_hwnd, WM_PROCESSTASK, 0, 0);
52 }
53
54 static task_t *pop_task(void)
55 {
56     thread_data_t *thread_data = get_thread_data(TRUE);
57     task_t *task = thread_data->task_queue_head;
58
59     if(!task)
60         return NULL;
61
62     thread_data->task_queue_head = task->next;
63     if(!thread_data->task_queue_head)
64         thread_data->task_queue_tail = NULL;
65
66     return task;
67 }
68
69 void remove_doc_tasks(HTMLDocument *doc)
70 {
71     thread_data_t *thread_data = get_thread_data(FALSE);
72     task_t *iter, *tmp;
73
74     if(!thread_data)
75         return;
76
77     while(thread_data->task_queue_head
78           && thread_data->task_queue_head->doc == doc)
79         pop_task();
80
81     for(iter = thread_data->task_queue_head; iter; iter = iter->next) {
82         while(iter->next && iter->next->doc == doc) {
83             tmp = iter->next;
84             iter->next = tmp->next;
85             mshtml_free(tmp);
86         }
87
88         if(!iter->next)
89             thread_data->task_queue_tail = iter;
90     }
91 }
92
93 static void set_downloading(HTMLDocument *doc)
94 {
95     IOleCommandTarget *olecmd;
96     HRESULT hres;
97
98     TRACE("(%p)\n", doc);
99
100     if(doc->frame)
101         IOleInPlaceFrame_SetStatusText(doc->frame, NULL /* FIXME */);
102
103     if(!doc->client)
104         return;
105
106     hres = IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&olecmd);
107     if(SUCCEEDED(hres)) {
108         VARIANT var;
109
110         V_VT(&var) = VT_I4;
111         V_I4(&var) = 1;
112
113         IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_SETDOWNLOADSTATE, OLECMDEXECOPT_DONTPROMPTUSER,
114                                &var, NULL);
115         IOleCommandTarget_Release(olecmd);
116     }
117
118     if(doc->hostui) {
119         IDropTarget *drop_target = NULL;
120
121         hres = IDocHostUIHandler_GetDropTarget(doc->hostui, NULL /* FIXME */, &drop_target);
122         if(drop_target) {
123             FIXME("Use IDropTarget\n");
124             IDropTarget_Release(drop_target);
125         }
126     }
127 }
128
129 static void set_parsecomplete(HTMLDocument *doc)
130 {
131     IOleCommandTarget *olecmd = NULL;
132
133     TRACE("(%p)\n", doc);
134
135     call_property_onchanged(doc->cp_propnotif, 1005);
136
137     doc->readystate = READYSTATE_INTERACTIVE;
138     call_property_onchanged(doc->cp_propnotif, DISPID_READYSTATE);
139
140     if(doc->client)
141         IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&olecmd);
142
143     if(olecmd) {
144         VARIANT state, progress;
145
146         V_VT(&progress) = VT_I4;
147         V_I4(&progress) = 0;
148         IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_SETPROGRESSPOS, OLECMDEXECOPT_DONTPROMPTUSER,
149                                &progress, NULL);
150
151         V_VT(&state) = VT_I4;
152         V_I4(&state) = 0;
153         IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_SETDOWNLOADSTATE, OLECMDEXECOPT_DONTPROMPTUSER,
154                                &state, NULL);
155
156         IOleCommandTarget_Exec(olecmd, &CGID_MSHTML, IDM_PARSECOMPLETE, 0, NULL, NULL);
157         IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_HTTPEQUIV_DONE, 0, NULL, NULL);
158     }
159
160     doc->readystate = READYSTATE_COMPLETE;
161     call_property_onchanged(doc->cp_propnotif, DISPID_READYSTATE);
162
163     if(doc->frame) {
164         static const WCHAR wszDone[] = {'D','o','n','e',0};
165         IOleInPlaceFrame_SetStatusText(doc->frame, wszDone);
166     }
167
168     if(olecmd) {
169         VARIANT title;
170         WCHAR empty[] = {0};
171         
172         V_VT(&title) = VT_BSTR;
173         V_BSTR(&title) = SysAllocString(empty);
174         IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_SETTITLE, OLECMDEXECOPT_DONTPROMPTUSER,
175                                &title, NULL);
176         SysFreeString(V_BSTR(&title));
177
178         IOleCommandTarget_Release(olecmd);
179     }
180 }
181
182 static void set_progress(HTMLDocument *doc)
183 {
184     IOleCommandTarget *olecmd = NULL;
185     HRESULT hres;
186
187     TRACE("(%p)\n", doc);
188
189     if(doc->client)
190         IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&olecmd);
191
192     if(olecmd) {
193         VARIANT progress_max, progress;
194
195         V_VT(&progress_max) = VT_I4;
196         V_I4(&progress_max) = 0; /* FIXME */
197         IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_SETPROGRESSMAX, OLECMDEXECOPT_DONTPROMPTUSER,
198                                &progress_max, NULL);
199
200         V_VT(&progress) = VT_I4;
201         V_I4(&progress) = 0; /* FIXME */
202         IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_SETPROGRESSPOS, OLECMDEXECOPT_DONTPROMPTUSER,
203                                &progress, NULL);
204     }
205
206     if(doc->usermode == EDITMODE && doc->hostui) {
207         DOCHOSTUIINFO hostinfo;
208
209         memset(&hostinfo, 0, sizeof(DOCHOSTUIINFO));
210         hostinfo.cbSize = sizeof(DOCHOSTUIINFO);
211         hres = IDocHostUIHandler_GetHostInfo(doc->hostui, &hostinfo);
212         if(SUCCEEDED(hres))
213             /* FIXME: use hostinfo */
214             TRACE("hostinfo = {%u %08x %08x %s %s}\n",
215                     hostinfo.cbSize, hostinfo.dwFlags, hostinfo.dwDoubleClick,
216                     debugstr_w(hostinfo.pchHostCss), debugstr_w(hostinfo.pchHostNS));
217     }
218 }
219
220 static void task_start_binding(BSCallback *bscallback)
221 {
222     start_binding(bscallback);
223     IBindStatusCallback_Release(STATUSCLB(bscallback));
224 }
225
226 static void process_task(task_t *task)
227 {
228     switch(task->task_id) {
229     case TASK_SETDOWNLOADSTATE:
230         set_downloading(task->doc);
231         break;
232     case TASK_PARSECOMPLETE:
233         set_parsecomplete(task->doc);
234         break;
235     case TASK_SETPROGRESS:
236         set_progress(task->doc);
237         break;
238     case TASK_START_BINDING:
239         task_start_binding(task->bscallback);
240         break;
241     default:
242         ERR("Wrong task_id %d\n", task->task_id);
243     }
244 }
245
246 static LRESULT WINAPI hidden_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
247 {
248     switch(msg) {
249     case WM_PROCESSTASK:
250         while(1) {
251             task_t *task = pop_task();
252             if(!task)
253                 break;
254
255             process_task(task);
256             mshtml_free(task);
257         }
258
259         return 0;
260     }
261
262     if(msg > WM_USER)
263         FIXME("(%p %d %x %lx)\n", hwnd, msg, wParam, lParam);
264
265     return DefWindowProcW(hwnd, msg, wParam, lParam);
266 }
267
268 static HWND create_thread_hwnd(void)
269 {
270     static ATOM hidden_wnd_class = 0;
271     static const WCHAR wszInternetExplorer_Hidden[] = {'I','n','t','e','r','n','e','t',
272             ' ','E','x','p','l','o','r','e','r','_','H','i','d','d','e','n',0};
273
274     if(!hidden_wnd_class) {
275         WNDCLASSEXW wndclass = {
276             sizeof(WNDCLASSEXW), 0,
277             hidden_proc,
278             0, 0, hInst, NULL, NULL, NULL, NULL,
279             wszInternetExplorer_Hidden,
280             NULL
281         };
282
283         hidden_wnd_class = RegisterClassExW(&wndclass);
284     }
285
286     return CreateWindowExW(0, wszInternetExplorer_Hidden, NULL, WS_POPUP,
287                            0, 0, 0, 0, NULL, NULL, hInst, NULL);
288 }
289
290 HWND get_thread_hwnd(void)
291 {
292     thread_data_t *thread_data = get_thread_data(TRUE);
293
294     if(!thread_data->thread_hwnd)
295         thread_data->thread_hwnd = create_thread_hwnd();
296
297     return thread_data->thread_hwnd;
298 }
299
300 thread_data_t *get_thread_data(BOOL create)
301 {
302     thread_data_t *thread_data;
303
304     if(!mshtml_tls) {
305         if(create)
306             mshtml_tls = TlsAlloc();
307         else
308             return NULL;
309     }
310
311     thread_data = TlsGetValue(mshtml_tls);
312     if(!thread_data && create) {
313         thread_data = mshtml_alloc_zero(sizeof(thread_data_t));
314         TlsSetValue(mshtml_tls, thread_data);
315     }
316
317     return thread_data;
318 }