mshtml: Added IHTMLWindow::open implementation.
[wine] / dlls / ddraw / clipper.c
1 /* DirectDrawClipper implementation
2  *
3  * Copyright 2000 (c) Marcus Meissner
4  * Copyright 2000 (c) TransGaming Technologies Inc.
5  * Copyright 2006 (c) Stefan Dösinger
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 #include "config.h"
23 #include "wine/port.h"
24
25 #include "ddraw_private.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
28
29 static inline struct ddraw_clipper *impl_from_IDirectDrawClipper(IDirectDrawClipper *iface)
30 {
31     return CONTAINING_RECORD(iface, struct ddraw_clipper, IDirectDrawClipper_iface);
32 }
33
34 static HRESULT WINAPI ddraw_clipper_QueryInterface(IDirectDrawClipper *iface, REFIID iid, void **object)
35 {
36     struct ddraw_clipper *clipper = impl_from_IDirectDrawClipper(iface);
37
38     TRACE("iface %p, iid %s, object %p.\n", iface, debugstr_guid(iid), object);
39
40     if (IsEqualGUID(&IID_IDirectDrawClipper, iid)
41             || IsEqualGUID(&IID_IUnknown, iid))
42     {
43         IDirectDrawClipper_AddRef(&clipper->IDirectDrawClipper_iface);
44         *object = &clipper->IDirectDrawClipper_iface;
45         return S_OK;
46     }
47
48     WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(iid));
49     *object = NULL;
50
51     return E_NOINTERFACE;
52 }
53
54 static ULONG WINAPI ddraw_clipper_AddRef(IDirectDrawClipper *iface)
55 {
56     struct ddraw_clipper *clipper = impl_from_IDirectDrawClipper(iface);
57     ULONG refcount = InterlockedIncrement(&clipper->ref);
58
59     TRACE("%p increasing refcount to %u.\n", clipper, refcount);
60
61     return refcount;
62 }
63
64 static ULONG WINAPI ddraw_clipper_Release(IDirectDrawClipper *iface)
65 {
66     struct ddraw_clipper *clipper = impl_from_IDirectDrawClipper(iface);
67     ULONG refcount = InterlockedDecrement(&clipper->ref);
68
69     TRACE("%p decreasing refcount to %u.\n", clipper, refcount);
70
71     if (!refcount)
72     {
73         if (clipper->region)
74             DeleteObject(clipper->region);
75         HeapFree(GetProcessHeap(), 0, clipper);
76     }
77
78     return refcount;
79 }
80
81 static HRESULT WINAPI ddraw_clipper_SetHWnd(IDirectDrawClipper *iface, DWORD flags, HWND window)
82 {
83     struct ddraw_clipper *clipper = impl_from_IDirectDrawClipper(iface);
84
85     TRACE("iface %p, flags %#x, window %p.\n", iface, flags, window);
86
87     if (flags)
88     {
89         FIXME("flags %#x, not supported.\n", flags);
90         return DDERR_INVALIDPARAMS;
91     }
92
93     wined3d_mutex_lock();
94     clipper->window = window;
95     wined3d_mutex_unlock();
96
97     return DD_OK;
98 }
99
100 static HRGN get_window_region(HWND window)
101 {
102     POINT origin = {0, 0};
103     RECT client_rect;
104
105     if (!GetClientRect(window, &client_rect))
106     {
107         ERR("Failed to get client rect.\n");
108         return NULL;
109     }
110
111     if (!ClientToScreen(window, &origin))
112     {
113         ERR("Failed to translate origin.\n");
114         return NULL;
115     }
116
117     if (!OffsetRect(&client_rect, origin.x, origin.y))
118     {
119         ERR("Failed to translate client rect.\n");
120         return NULL;
121     }
122
123     return CreateRectRgnIndirect(&client_rect);
124 }
125
126 /*****************************************************************************
127  * IDirectDrawClipper::GetClipList
128  *
129  * Retrieve a copy of the clip list
130  *
131  * Arguments:
132  *  rect: Rectangle to be used to clip the clip list or NULL for the
133  *      entire clip list.
134  *  clip_list: structure for the resulting copy of the clip list.
135  *      If NULL, fills Size up to the number of bytes necessary to hold
136  *      the entire clip.
137  *  clip_list_size: Size of resulting clip list; size of the buffer at clip_list
138  *      or, if clip_list is NULL, receives the required size of the buffer
139  *      in bytes.
140  *
141  * RETURNS
142  *  Either DD_OK or DDERR_*
143  ************************************************************************/
144 static HRESULT WINAPI ddraw_clipper_GetClipList(IDirectDrawClipper *iface, RECT *rect,
145         RGNDATA *clip_list, DWORD *clip_list_size)
146 {
147     struct ddraw_clipper *clipper = impl_from_IDirectDrawClipper(iface);
148     HRGN region;
149
150     TRACE("iface %p, rect %s, clip_list %p, clip_list_size %p.\n",
151             iface, wine_dbgstr_rect(rect), clip_list, clip_list_size);
152
153     wined3d_mutex_lock();
154
155     if (clipper->window)
156     {
157         if (!(region = get_window_region(clipper->window)))
158         {
159             wined3d_mutex_unlock();
160             ERR("Failed to get window region.\n");
161             return E_FAIL;
162         }
163     }
164     else
165     {
166         if (!(region = clipper->region))
167         {
168             wined3d_mutex_unlock();
169             WARN("No clip list set.\n");
170             return DDERR_NOCLIPLIST;
171         }
172     }
173
174     if (rect)
175     {
176         HRGN clip_region;
177
178         if (!(clip_region = CreateRectRgnIndirect(rect)))
179         {
180             wined3d_mutex_unlock();
181             ERR("Failed to create region.\n");
182             if (clipper->window)
183                 DeleteObject(region);
184             return E_FAIL;
185         }
186
187         if (CombineRgn(clip_region, region, clip_region, RGN_AND) == ERROR)
188         {
189             wined3d_mutex_unlock();
190             ERR("Failed to combine regions.\n");
191             DeleteObject(clip_region);
192             if (clipper->window)
193                 DeleteObject(region);
194             return E_FAIL;
195         }
196
197         if (clipper->window)
198             DeleteObject(region);
199         region = clip_region;
200     }
201
202     *clip_list_size = GetRegionData(region, *clip_list_size, clip_list);
203     if (rect || clipper->window)
204         DeleteObject(region);
205
206     wined3d_mutex_unlock();
207     return DD_OK;
208 }
209
210 /*****************************************************************************
211  * IDirectDrawClipper::SetClipList
212  *
213  * Sets or deletes (if region is NULL) the clip list
214  *
215  * This implementation is a stub and returns DD_OK always to make the app
216  * happy.
217  *
218  * PARAMS
219  *  region  Pointer to a LRGNDATA structure or NULL
220  *  flags   not used, must be 0
221  * RETURNS
222  *  Either DD_OK or DDERR_*
223  *****************************************************************************/
224 static HRESULT WINAPI ddraw_clipper_SetClipList(IDirectDrawClipper *iface, RGNDATA *region, DWORD flags)
225 {
226     struct ddraw_clipper *clipper = impl_from_IDirectDrawClipper(iface);
227
228     TRACE("iface %p, region %p, flags %#x.\n", iface, region, flags);
229
230     wined3d_mutex_lock();
231
232     if (clipper->window)
233     {
234         wined3d_mutex_unlock();
235         return DDERR_CLIPPERISUSINGHWND;
236     }
237
238     if (clipper->region)
239         DeleteObject(clipper->region);
240     if (!(clipper->region = ExtCreateRegion(NULL, 0, region)))
241     {
242         wined3d_mutex_unlock();
243         ERR("Failed to create creation.\n");
244         return E_FAIL;
245     }
246
247     wined3d_mutex_unlock();
248
249     return DD_OK;
250 }
251
252 static HRESULT WINAPI ddraw_clipper_GetHWnd(IDirectDrawClipper *iface, HWND *window)
253 {
254     struct ddraw_clipper *clipper = impl_from_IDirectDrawClipper(iface);
255
256     TRACE("iface %p, window %p.\n", iface, window);
257
258     wined3d_mutex_lock();
259     *window = clipper->window;
260     wined3d_mutex_unlock();
261
262     return DD_OK;
263 }
264
265 static HRESULT WINAPI ddraw_clipper_Initialize(IDirectDrawClipper *iface,
266         IDirectDraw *ddraw, DWORD flags)
267 {
268     struct ddraw_clipper *clipper = impl_from_IDirectDrawClipper(iface);
269
270     TRACE("iface %p, ddraw %p, flags %#x.\n", iface, ddraw, flags);
271
272     wined3d_mutex_lock();
273     if (clipper->initialized)
274     {
275         wined3d_mutex_unlock();
276         return DDERR_ALREADYINITIALIZED;
277     }
278
279     clipper->initialized = TRUE;
280     wined3d_mutex_unlock();
281
282     return DD_OK;
283 }
284
285 static HRESULT WINAPI ddraw_clipper_IsClipListChanged(IDirectDrawClipper *iface, BOOL *changed)
286 {
287     FIXME("iface %p, changed %p stub!\n", iface, changed);
288
289     /* XXX What is safest? */
290     *changed = FALSE;
291
292     return DD_OK;
293 }
294
295 static const struct IDirectDrawClipperVtbl ddraw_clipper_vtbl =
296 {
297     ddraw_clipper_QueryInterface,
298     ddraw_clipper_AddRef,
299     ddraw_clipper_Release,
300     ddraw_clipper_GetClipList,
301     ddraw_clipper_GetHWnd,
302     ddraw_clipper_Initialize,
303     ddraw_clipper_IsClipListChanged,
304     ddraw_clipper_SetClipList,
305     ddraw_clipper_SetHWnd,
306 };
307
308 HRESULT ddraw_clipper_init(struct ddraw_clipper *clipper)
309 {
310     clipper->IDirectDrawClipper_iface.lpVtbl = &ddraw_clipper_vtbl;
311     clipper->ref = 1;
312
313     return DD_OK;
314 }
315
316 struct ddraw_clipper *unsafe_impl_from_IDirectDrawClipper(IDirectDrawClipper *iface)
317 {
318     if (!iface)
319         return NULL;
320     assert(iface->lpVtbl == &ddraw_clipper_vtbl);
321
322     return impl_from_IDirectDrawClipper(iface);
323 }