- Rename dupstrW to strdupW, and remove duplicate definition.
[wine] / dlls / uxtheme / draw.c
1 /*
2  * Win32 5.1 Theme drawing
3  *
4  * Copyright (C) 2003 Kevin Koltzau
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22
23 #include <stdlib.h>
24 #include <stdarg.h>
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "wingdi.h"
30 #include "uxtheme.h"
31 #include "tmschema.h"
32
33 #include "msstyles.h"
34 #include "uxthemedll.h"
35
36 #include "wine/debug.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(uxtheme);
39
40 /***********************************************************************
41  * Defines and global variables
42  */
43
44 DWORD dwDialogTextureFlags;
45
46 /***********************************************************************/
47
48 /***********************************************************************
49  *      EnableThemeDialogTexture                            (UXTHEME.@)
50  */
51 HRESULT WINAPI EnableThemeDialogTexture(HWND hwnd, DWORD dwFlags)
52 {
53     TRACE("(%p,0x%08lx\n", hwnd, dwFlags);
54     dwDialogTextureFlags = dwFlags;
55     return S_OK;
56  }
57
58 /***********************************************************************
59  *      IsThemeDialogTextureEnabled                         (UXTHEME.@)
60  */
61 BOOL WINAPI IsThemeDialogTextureEnabled(void)
62 {
63     TRACE("\n");
64     return (dwDialogTextureFlags & ETDT_ENABLE) && !(dwDialogTextureFlags & ETDT_DISABLE);
65 }
66
67 /***********************************************************************
68  *      DrawThemeParentBackground                           (UXTHEME.@)
69  */
70 HRESULT WINAPI DrawThemeParentBackground(HWND hwnd, HDC hdc, RECT *prc)
71 {
72     RECT rt;
73     POINT org;
74     HWND hParent;
75     HRGN clip = NULL;
76     int hasClip = -1;
77     
78     TRACE("(%p,%p,%p)\n", hwnd, hdc, prc);
79     hParent = GetParent(hwnd);
80     if(!hParent)
81         hParent = hwnd;
82     if(prc) {
83         CopyRect(&rt, prc);
84         MapWindowPoints(hwnd, NULL, (LPPOINT)&rt, 2);
85         
86         clip = CreateRectRgn(0,0,1,1);
87         hasClip = GetClipRgn(hdc, clip);
88         if(hasClip == -1)
89             TRACE("Failed to get original clipping region\n");
90         else
91             IntersectClipRect(hdc, prc->left, prc->top, prc->right, prc->bottom);
92     }
93     else {
94         GetClientRect(hParent, &rt);
95         MapWindowPoints(hParent, NULL, (LPPOINT)&rt, 2);
96     }
97
98     SetViewportOrgEx(hdc, rt.left, rt.top, &org);
99
100     SendMessageW(hParent, WM_ERASEBKGND, (WPARAM)hdc, 0);
101     SendMessageW(hParent, WM_PRINTCLIENT, (WPARAM)hdc, PRF_CLIENT);
102
103     SetViewportOrgEx(hdc, org.x, org.y, NULL);
104     if(prc) {
105         if(hasClip == 0)
106             SelectClipRgn(hdc, NULL);
107         else if(hasClip == 1)
108             SelectClipRgn(hdc, clip);
109         DeleteObject(clip);
110     }
111     return S_OK;
112 }
113
114
115 /***********************************************************************
116  *      DrawThemeBackground                                 (UXTHEME.@)
117  */
118 HRESULT WINAPI DrawThemeBackground(HTHEME hTheme, HDC hdc, int iPartId,
119                                    int iStateId, const RECT *pRect,
120                                    const RECT *pClipRect)
121 {
122     DTBGOPTS opts;
123     opts.dwSize = sizeof(DTBGOPTS);
124     opts.dwFlags = 0;
125     if(pClipRect) {
126         opts.dwFlags |= DTBG_CLIPRECT;
127         CopyRect(&opts.rcClip, pClipRect);
128     }
129     return DrawThemeBackgroundEx(hTheme, hdc, iPartId, iStateId, pRect, &opts);
130 }
131
132 /***********************************************************************
133  *      UXTHEME_SelectImage
134  *
135  * Select the image to use
136  */
137 PTHEME_PROPERTY UXTHEME_SelectImage(HTHEME hTheme, HDC hdc, int iPartId, int iStateId, const RECT *pRect, BOOL glyph)
138 {
139     PTHEME_PROPERTY tp;
140     int imageselecttype = IST_NONE;
141     int i;
142     int image;
143     if(glyph)
144         image = TMT_GLYPHIMAGEFILE;
145     else
146         image = TMT_IMAGEFILE;
147
148     if((tp=MSSTYLES_FindProperty(hTheme, iPartId, iStateId, TMT_FILENAME, image)))
149         return tp;
150     GetThemeEnumValue(hTheme, iPartId, iStateId, TMT_IMAGESELECTTYPE, &imageselecttype);
151
152     if(imageselecttype == IST_DPI) {
153         int reqdpi = 0;
154         int screendpi = GetDeviceCaps(hdc, LOGPIXELSX);
155         for(i=4; i>=0; i--) {
156             reqdpi = 0;
157             if(SUCCEEDED(GetThemeInt(hTheme, iPartId, iStateId, i + TMT_MINDPI1, &reqdpi))) {
158                 if(reqdpi != 0 && screendpi >= reqdpi) {
159                     TRACE("Using %d DPI, image %d\n", reqdpi, i + TMT_IMAGEFILE1);
160                     return MSSTYLES_FindProperty(hTheme, iPartId, iStateId, TMT_FILENAME, i + TMT_IMAGEFILE1);
161                 }
162             }
163         }
164         /* If an image couldnt be selected, choose the first one */
165         return MSSTYLES_FindProperty(hTheme, iPartId, iStateId, TMT_FILENAME, TMT_IMAGEFILE1);
166     }
167     else if(imageselecttype == IST_SIZE) {
168         POINT size = {pRect->right-pRect->left, pRect->bottom-pRect->top};
169         POINT reqsize;
170         for(i=4; i>=0; i--) {
171             if(SUCCEEDED(GetThemePosition(hTheme, iPartId, iStateId, i + TMT_MINSIZE1, &reqsize))) {
172                 if(reqsize.x >= size.x && reqsize.y >= size.y) {
173                     TRACE("Using image size %ldx%ld, image %d\n", reqsize.x, reqsize.y, i + TMT_IMAGEFILE1);
174                     return MSSTYLES_FindProperty(hTheme, iPartId, iStateId, TMT_FILENAME, i + TMT_IMAGEFILE1);
175                 }
176             }
177         }
178         /* If an image couldnt be selected, choose the smallest one */
179         return MSSTYLES_FindProperty(hTheme, iPartId, iStateId, TMT_FILENAME, TMT_IMAGEFILE1);
180     }
181     return NULL;
182 }
183
184 /***********************************************************************
185  *      UXTHEME_LoadImage
186  *
187  * Load image for part/state
188  */
189 HRESULT UXTHEME_LoadImage(HTHEME hTheme, HDC hdc, int iPartId, int iStateId, const RECT *pRect, BOOL glyph,
190                           HBITMAP *hBmp, RECT *bmpRect)
191 {
192     int imagelayout = IL_VERTICAL;
193     int imagecount = 0;
194     BITMAP bmp;
195     WCHAR szPath[MAX_PATH];
196     PTHEME_PROPERTY tp = UXTHEME_SelectImage(hTheme, hdc, iPartId, iStateId, pRect, glyph);
197     if(!tp) {
198         FIXME("Couldn't determine image for part/state %d/%d, invalid theme?\n", iPartId, iStateId);
199         return E_PROP_ID_UNSUPPORTED;
200     }
201     lstrcpynW(szPath, tp->lpValue, min(tp->dwValueLen+1, sizeof(szPath)/sizeof(szPath[0])));
202     *hBmp = MSSTYLES_LoadBitmap(hdc, hTheme, szPath);
203     if(!*hBmp) {
204         TRACE("Failed to load bitmap %s\n", debugstr_w(szPath));
205         return HRESULT_FROM_WIN32(GetLastError());
206     }
207     
208     GetThemeEnumValue(hTheme, iPartId, iStateId, TMT_IMAGELAYOUT, &imagelayout);
209     GetThemeInt(hTheme, iPartId, iStateId, TMT_IMAGECOUNT, &imagecount);
210
211     GetObjectW(*hBmp, sizeof(bmp), &bmp);
212     if(imagelayout == IL_VERTICAL) {
213         int height = bmp.bmHeight/imagecount;
214         bmpRect->left = 0;
215         bmpRect->right = bmp.bmWidth;
216         bmpRect->top = (min(imagecount, iStateId)-1) * height;
217         bmpRect->bottom = bmpRect->top + height;
218     }
219     else {
220         int width = bmp.bmWidth/imagecount;
221         bmpRect->left = (min(imagecount, iStateId)-1) * width;
222         bmpRect->right = bmpRect->left + width;
223         bmpRect->top = 0;
224         bmpRect->bottom = bmp.bmHeight;
225     }
226     return S_OK;
227 }
228
229 /***********************************************************************
230  *      UXTHEME_StretchBlt
231  *
232  * Psudo TransparentBlt/StretchBlt
233  */
234 static inline BOOL UXTHEME_StretchBlt(HDC hdcDst, int nXOriginDst, int nYOriginDst, int nWidthDst, int nHeightDst,
235                                       HDC hdcSrc, int nXOriginSrc, int nYOriginSrc, int nWidthSrc, int nHeightSrc,
236                                       BOOL transparent, COLORREF transcolor)
237 {
238     if(transparent) {
239         /* Ensure we don't pass any negative values to TransparentBlt */
240         return TransparentBlt(hdcDst, nXOriginDst, nYOriginDst, abs(nWidthDst), abs(nHeightDst),
241                               hdcSrc, nXOriginSrc, nYOriginSrc, abs(nWidthSrc), abs(nHeightSrc),
242                               transcolor);
243     }
244     /* This should be using AlphaBlend */
245     return StretchBlt(hdcDst, nXOriginDst, nYOriginDst, nWidthDst, nHeightDst,
246                         hdcSrc, nXOriginSrc, nYOriginSrc, nWidthSrc, nHeightSrc,
247                         SRCCOPY);
248 }
249
250 /***********************************************************************
251  *      UXTHEME_Blt
252  *
253  * Simplify sending same width/height for both source and dest
254  */
255 static inline BOOL UXTHEME_Blt(HDC hdcDest, int nXOriginDest, int nYOriginDest, int nWidthDest, int nHeightDest,
256                                HDC hdcSrc, int nXOriginSrc, int nYOriginSrc,
257                                BOOL transparent, COLORREF transcolor)
258 {
259     return UXTHEME_StretchBlt(hdcDest, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest,
260                               hdcSrc, nXOriginSrc, nYOriginSrc, nWidthDest, nHeightDest,
261                               transparent, transcolor);
262 }
263
264
265 /***********************************************************************
266  *      UXTHEME_DrawImageGlyph
267  *
268  * Draw an imagefile glyph
269  */
270 HRESULT UXTHEME_DrawImageGlyph(HTHEME hTheme, HDC hdc, int iPartId,
271                                int iStateId, RECT *pRect,
272                                const DTBGOPTS *pOptions)
273 {
274     HRESULT hr;
275     HBITMAP bmpSrc = NULL;
276     HDC hdcSrc = NULL;
277     HGDIOBJ oldSrc = NULL;
278     RECT rcSrc;
279     BOOL transparent = FALSE;
280     COLORREF transparentcolor = 0;
281     int valign = VA_CENTER;
282     int halign = HA_CENTER;
283     POINT dstSize;
284     POINT srcSize;
285     POINT topleft;
286
287     hr = UXTHEME_LoadImage(hTheme, hdc, iPartId, iStateId, pRect, TRUE, &bmpSrc, &rcSrc);
288     if(FAILED(hr)) return hr;
289     hdcSrc = CreateCompatibleDC(hdc);
290     if(!hdcSrc) {
291         hr = HRESULT_FROM_WIN32(GetLastError());
292         DeleteObject(bmpSrc);
293         return hr;
294     }
295     oldSrc = SelectObject(hdcSrc, bmpSrc);
296
297     dstSize.x = pRect->right-pRect->left;
298     dstSize.y = pRect->bottom-pRect->top;
299     srcSize.x = rcSrc.right-rcSrc.left;
300     srcSize.y = rcSrc.bottom-rcSrc.top;
301
302     GetThemeBool(hTheme, iPartId, iStateId, TMT_GLYPHTRANSPARENT, &transparent);
303     if(transparent) {
304         if(FAILED(GetThemeColor(hTheme, iPartId, iStateId, TMT_GLYPHTRANSPARENTCOLOR, &transparentcolor))) {
305             /* If image is transparent, but no color was specified, get the color of the upper left corner */
306             transparentcolor = GetPixel(hdcSrc, 0, 0);
307         }
308     }
309     GetThemeEnumValue(hTheme, iPartId, iStateId, TMT_VALIGN, &valign);
310     GetThemeEnumValue(hTheme, iPartId, iStateId, TMT_HALIGN, &halign);
311
312     topleft.x = pRect->left;
313     topleft.y = pRect->top;
314     if(halign == HA_CENTER)      topleft.x += (dstSize.x/2)-(srcSize.x/2);
315     else if(halign == HA_RIGHT)  topleft.x += dstSize.x-srcSize.x;
316     if(valign == VA_CENTER)      topleft.y += (dstSize.y/2)-(srcSize.y/2);
317     else if(valign == VA_BOTTOM) topleft.y += dstSize.y-srcSize.y;
318
319     if(!UXTHEME_Blt(hdc, topleft.x, topleft.y, srcSize.x, srcSize.y,
320                     hdcSrc, rcSrc.left, rcSrc.top,
321                     transparent, transparentcolor)) {
322         hr = HRESULT_FROM_WIN32(GetLastError());
323     }
324
325     SelectObject(hdcSrc, oldSrc);
326     DeleteDC(hdcSrc);
327     DeleteObject(bmpSrc);
328     return hr;
329 }
330
331 /***********************************************************************
332  *      UXTHEME_DrawImageGlyph
333  *
334  * Draw glyph on top of background, if appropriate
335  */
336 HRESULT UXTHEME_DrawGlyph(HTHEME hTheme, HDC hdc, int iPartId,
337                                     int iStateId, RECT *pRect,
338                                     const DTBGOPTS *pOptions)
339 {
340     int glyphtype = GT_NONE;
341
342     GetThemeEnumValue(hTheme, iPartId, iStateId, TMT_GLYPHTYPE, &glyphtype);
343
344     if(glyphtype == GT_IMAGEGLYPH) {
345         return UXTHEME_DrawImageGlyph(hTheme, hdc, iPartId, iStateId, pRect, pOptions);
346     }
347     else if(glyphtype == GT_FONTGLYPH) {
348         /* I don't know what a font glyph is, I've never seen it used in any themes */
349         FIXME("Font glyph\n");
350     }
351     return S_OK;
352 }
353
354 /***********************************************************************
355  *      UXTHEME_DrawImageBackground
356  *
357  * Draw an imagefile background
358  */
359 HRESULT UXTHEME_DrawImageBackground(HTHEME hTheme, HDC hdc, int iPartId,
360                                     int iStateId, RECT *pRect,
361                                     const DTBGOPTS *pOptions)
362 {
363     HRESULT hr = S_OK;
364     HBITMAP bmpSrc;
365     HGDIOBJ oldSrc;
366     HDC hdcSrc;
367     RECT rcSrc;
368     RECT rcDst;
369     POINT dstSize;
370     POINT srcSize;
371     int sizingtype = ST_TRUESIZE;
372     BOOL uniformsizing = FALSE;
373     BOOL transparent = FALSE;
374     COLORREF transparentcolor = 0;
375
376     hr = UXTHEME_LoadImage(hTheme, hdc, iPartId, iStateId, pRect, FALSE, &bmpSrc, &rcSrc);
377     if(FAILED(hr)) return hr;
378     hdcSrc = CreateCompatibleDC(hdc);
379     if(!hdcSrc) {
380         hr = HRESULT_FROM_WIN32(GetLastError());
381         DeleteObject(bmpSrc);
382         return hr;
383     }
384     oldSrc = SelectObject(hdcSrc, bmpSrc);
385
386     CopyRect(&rcDst, pRect);
387     
388     GetThemeBool(hTheme, iPartId, iStateId, TMT_TRANSPARENT, &transparent);
389     if(transparent) {
390         if(FAILED(GetThemeColor(hTheme, iPartId, iStateId, TMT_TRANSPARENTCOLOR, &transparentcolor))) {
391             /* If image is transparent, but no color was specified, get the color of the upper left corner */
392             transparentcolor = GetPixel(hdcSrc, 0, 0);
393         }
394     }
395
396     dstSize.x = rcDst.right-rcDst.left;
397     dstSize.y = rcDst.bottom-rcDst.top;
398     srcSize.x = rcSrc.right-rcSrc.left;
399     srcSize.y = rcSrc.bottom-rcSrc.top;
400
401     if(uniformsizing) {
402         /* Scale height and width equally */
403         int widthDiff = abs(srcSize.x-dstSize.x);
404         int heightDiff = abs(srcSize.y-dstSize.x);
405         if(widthDiff > heightDiff) {
406             dstSize.y -= widthDiff-heightDiff;
407             rcDst.bottom = rcDst.top + dstSize.y;
408         }
409         else if(heightDiff > widthDiff) {
410             dstSize.x -= heightDiff-widthDiff;
411             rcDst.right = rcDst.left + dstSize.x;
412         }
413     }
414
415     GetThemeEnumValue(hTheme, iPartId, iStateId, TMT_SIZINGTYPE, &sizingtype);
416     if(sizingtype == ST_TRUESIZE) {
417         int truesizestretchmark = 0;
418
419         if(dstSize.x < 0 || dstSize.y < 0) {
420             BOOL mirrorimage = TRUE;
421             GetThemeBool(hTheme, iPartId, iStateId, TMT_MIRRORIMAGE, &mirrorimage);
422             if(mirrorimage) {
423                 if(dstSize.x < 0) {
424                     rcDst.left += dstSize.x;
425                     rcDst.right += dstSize.x;
426                 }
427                 if(dstSize.y < 0) {
428                     rcDst.top += dstSize.y;
429                     rcDst.bottom += dstSize.y;
430                 }
431             }
432         }
433         /* Only stretch when target exceeds source by truesizestretchmark percent */
434         GetThemeInt(hTheme, iPartId, iStateId, TMT_TRUESIZESTRETCHMARK, &truesizestretchmark);
435         if(dstSize.x < 0 || dstSize.y < 0 ||
436            MulDiv(srcSize.x, 100, dstSize.x) > truesizestretchmark ||
437            MulDiv(srcSize.y, 100, dstSize.y) > truesizestretchmark) {
438             if(!UXTHEME_StretchBlt(hdc, rcDst.left, rcDst.top, dstSize.x, dstSize.y,
439                                    hdcSrc, rcSrc.left, rcSrc.top, srcSize.x, srcSize.y,
440                                    transparent, transparentcolor))
441                 hr = HRESULT_FROM_WIN32(GetLastError());
442         }
443         else {
444             rcDst.left += (dstSize.x/2)-(srcSize.x/2);
445             rcDst.top  += (dstSize.y/2)-(srcSize.y/2);
446             rcDst.right = rcDst.left + srcSize.x;
447             rcDst.bottom = rcDst.top + srcSize.y;
448             if(!UXTHEME_Blt(hdc, rcDst.left, rcDst.top, srcSize.x, srcSize.y,
449                             hdcSrc, rcSrc.left, rcSrc.top,
450                             transparent, transparentcolor))
451                 hr = HRESULT_FROM_WIN32(GetLastError());
452         }
453     }
454     else {
455         HDC hdcDst = NULL;
456         HBITMAP bmpDst = NULL;
457         HGDIOBJ oldDst = NULL;
458         MARGINS sm;
459
460         dstSize.x = abs(dstSize.x);
461         dstSize.y = abs(dstSize.y);
462
463         GetThemeMargins(hTheme, hdc, iPartId, iStateId, TMT_SIZINGMARGINS, NULL, &sm);
464
465         hdcDst = CreateCompatibleDC(hdc);
466         if(!hdcDst) {
467             hr = HRESULT_FROM_WIN32(GetLastError());
468             goto draw_error; 
469         }
470         bmpDst = CreateCompatibleBitmap(hdc, dstSize.x, dstSize.y);
471         if(!bmpDst) {
472             hr = HRESULT_FROM_WIN32(GetLastError());
473             goto draw_error; 
474         }
475         oldDst = SelectObject(hdcDst, bmpDst);
476
477         /* Upper left corner */
478         if(!BitBlt(hdcDst, 0, 0, sm.cxLeftWidth, sm.cyTopHeight,
479                    hdcSrc, rcSrc.left, rcSrc.top, SRCCOPY)) {
480             hr = HRESULT_FROM_WIN32(GetLastError());
481             goto draw_error; 
482         }
483         /* Upper right corner */
484         if(!BitBlt(hdcDst, dstSize.x-sm.cxRightWidth, 0, sm.cxRightWidth, sm.cyTopHeight,
485                    hdcSrc, rcSrc.right-sm.cxRightWidth, rcSrc.top, SRCCOPY)) {
486             hr = HRESULT_FROM_WIN32(GetLastError());
487             goto draw_error; 
488         }
489         /* Lower left corner */
490         if(!BitBlt(hdcDst, 0, dstSize.y-sm.cyBottomHeight, sm.cxLeftWidth, sm.cyBottomHeight,
491                    hdcSrc, rcSrc.left, rcSrc.bottom-sm.cyBottomHeight, SRCCOPY)) {
492             hr = HRESULT_FROM_WIN32(GetLastError());
493             goto draw_error; 
494         }
495         /* Lower right corner */
496         if(!BitBlt(hdcDst, dstSize.x-sm.cxRightWidth, dstSize.y-sm.cyBottomHeight, sm.cxRightWidth, sm.cyBottomHeight,
497                    hdcSrc, rcSrc.right-sm.cxRightWidth, rcSrc.bottom-sm.cyBottomHeight, SRCCOPY)) {
498             hr = HRESULT_FROM_WIN32(GetLastError());
499             goto draw_error; 
500         }
501
502         if(sizingtype == ST_TILE) {
503             FIXME("Tile\n");
504             sizingtype = ST_STRETCH; /* Just use stretch for now */
505         }
506         if(sizingtype == ST_STRETCH) {
507             int destCenterWidth  = dstSize.x - (sm.cxLeftWidth + sm.cxRightWidth);
508             int srcCenterWidth   = srcSize.x - (sm.cxLeftWidth + sm.cxRightWidth);
509             int destCenterHeight = dstSize.y - (sm.cyTopHeight + sm.cyBottomHeight);
510             int srcCenterHeight  = srcSize.y - (sm.cyTopHeight + sm.cyBottomHeight);
511
512             if(destCenterWidth > 0) {
513                 /* Center top */
514                 if(!StretchBlt(hdcDst, sm.cxLeftWidth, 0, destCenterWidth, sm.cyTopHeight,
515                                hdcSrc, rcSrc.left+sm.cxLeftWidth, rcSrc.top, srcCenterWidth, sm.cyTopHeight, SRCCOPY)) {
516                     hr = HRESULT_FROM_WIN32(GetLastError());
517                     goto draw_error; 
518                 }
519                 /* Center bottom */
520                 if(!StretchBlt(hdcDst, sm.cxLeftWidth, dstSize.y-sm.cyBottomHeight, destCenterWidth, sm.cyBottomHeight,
521                                hdcSrc, rcSrc.left+sm.cxLeftWidth, rcSrc.bottom-sm.cyBottomHeight, srcCenterWidth, sm.cyTopHeight, SRCCOPY)) {
522                     hr = HRESULT_FROM_WIN32(GetLastError());
523                     goto draw_error; 
524                 }
525             }
526             if(destCenterHeight > 0) {
527                 /* Left center */
528                 if(!StretchBlt(hdcDst, 0, sm.cyTopHeight, sm.cxLeftWidth, destCenterHeight,
529                            hdcSrc, rcSrc.left, rcSrc.top+sm.cyTopHeight, sm.cxLeftWidth, srcCenterHeight, SRCCOPY)) {
530                     hr = HRESULT_FROM_WIN32(GetLastError());
531                     goto draw_error; 
532                 }
533                 /* Right center */
534                 if(!StretchBlt(hdcDst, dstSize.x-sm.cxRightWidth, sm.cyTopHeight, sm.cxRightWidth, destCenterHeight,
535                                hdcSrc, rcSrc.right-sm.cxRightWidth, rcSrc.top+sm.cyTopHeight, sm.cxRightWidth, srcCenterHeight, SRCCOPY)) {
536                     hr = HRESULT_FROM_WIN32(GetLastError());
537                     goto draw_error; 
538                 }
539             }
540             if(destCenterHeight > 0 && destCenterWidth > 0) {
541                 BOOL borderonly = FALSE;
542                 GetThemeBool(hTheme, iPartId, iStateId, TMT_BORDERONLY, &borderonly);
543                 if(!borderonly) {
544                     /* Center */
545                     if(!StretchBlt(hdcDst, sm.cxLeftWidth, sm.cyTopHeight, destCenterWidth, destCenterHeight,
546                                    hdcSrc, rcSrc.left+sm.cxLeftWidth, rcSrc.top+sm.cyTopHeight, srcCenterWidth, srcCenterHeight, SRCCOPY)) {
547                         hr = HRESULT_FROM_WIN32(GetLastError());
548                         goto draw_error; 
549                     }
550                 }
551             }
552         }
553
554         if(!UXTHEME_Blt(hdc, rcDst.left, rcDst.top, dstSize.x, dstSize.y,
555                         hdcDst, 0, 0,
556                         transparent, transparentcolor))
557             hr = HRESULT_FROM_WIN32(GetLastError());
558
559 draw_error:
560         if(hdcDst) {
561             SelectObject(hdcDst, oldDst);
562             DeleteDC(hdcDst);
563         }
564         if(bmpDst) DeleteObject(bmpDst);
565     }
566     SelectObject(hdcSrc, oldSrc);
567     DeleteObject(bmpSrc);
568     DeleteDC(hdcSrc);
569     CopyRect(pRect, &rcDst);
570     return hr;
571 }
572
573 /***********************************************************************
574  *      UXTHEME_DrawBorderRectangle
575  *
576  * Draw the bounding rectangle for a borderfill background
577  */
578 HRESULT UXTHEME_DrawBorderRectangle(HTHEME hTheme, HDC hdc, int iPartId,
579                                     int iStateId, RECT *pRect,
580                                     const DTBGOPTS *pOptions)
581 {
582     HRESULT hr = S_OK;
583     HPEN hPen;
584     HGDIOBJ oldPen;
585     COLORREF bordercolor = RGB(0,0,0);
586     int bordersize = 1;
587
588     GetThemeInt(hTheme, iPartId, iStateId, TMT_BORDERSIZE, &bordersize);
589     if(bordersize > 0) {
590         POINT ptCorners[4];
591         ptCorners[0].x = pRect->left;
592         ptCorners[0].y = pRect->top;
593         ptCorners[1].x = pRect->right;
594         ptCorners[1].y = pRect->top;
595         ptCorners[2].x = pRect->right;
596         ptCorners[2].y = pRect->bottom;
597         ptCorners[3].x = pRect->left;
598         ptCorners[3].y = pRect->bottom;
599
600         InflateRect(pRect, -bordersize, -bordersize);
601         if(pOptions->dwFlags & DTBG_OMITBORDER)
602             return S_OK;
603         GetThemeColor(hTheme, iPartId, iStateId, TMT_BORDERCOLOR, &bordercolor);
604         hPen = CreatePen(PS_SOLID, bordersize, bordercolor);
605         if(!hPen)
606             return HRESULT_FROM_WIN32(GetLastError());
607         oldPen = SelectObject(hdc, hPen);
608
609         if(!Polyline(hdc, ptCorners, 4))
610             hr = HRESULT_FROM_WIN32(GetLastError());
611
612         SelectObject(hdc, oldPen);
613         DeleteObject(hPen);
614     }
615     return hr;
616 }
617
618 /***********************************************************************
619  *      UXTHEME_DrawBackgroundFill
620  *
621  * Fill a borderfill background rectangle
622  */
623 HRESULT UXTHEME_DrawBackgroundFill(HTHEME hTheme, HDC hdc, int iPartId,
624                                    int iStateId, RECT *pRect,
625                                    const DTBGOPTS *pOptions)
626 {
627     HRESULT hr = S_OK;
628     int filltype = FT_SOLID;
629
630     TRACE("(%d,%d,%ld)\n", iPartId, iStateId, pOptions->dwFlags);
631
632     if(pOptions->dwFlags & DTBG_OMITCONTENT)
633         return S_OK;
634
635     GetThemeEnumValue(hTheme, iPartId, iStateId, TMT_FILLTYPE, &filltype);
636
637     if(filltype == FT_SOLID) {
638         HBRUSH hBrush;
639         COLORREF fillcolor = RGB(255,255,255);
640
641         GetThemeColor(hTheme, iPartId, iStateId, TMT_FILLCOLOR, &fillcolor);
642         hBrush = CreateSolidBrush(fillcolor);
643         if(!FillRect(hdc, pRect, hBrush))
644             hr = HRESULT_FROM_WIN32(GetLastError());
645         DeleteObject(hBrush);
646     }
647     else if(filltype == FT_VERTGRADIENT || filltype == FT_HORZGRADIENT) {
648         /* FIXME: This only accounts for 2 gradient colors (out of 5) and ignores
649             the gradient ratios (no idea how those work)
650             Few themes use this, and the ones I've seen only use 2 colors with
651             a gradient ratio of 0 and 255 respectivly
652         */
653
654         COLORREF gradient1 = RGB(0,0,0);
655         COLORREF gradient2 = RGB(255,255,255);
656         TRIVERTEX vert[2];
657         GRADIENT_RECT gRect;
658
659         FIXME("Gradient implementation not complete\n");
660
661         GetThemeColor(hTheme, iPartId, iStateId, TMT_GRADIENTCOLOR1, &gradient1);
662         GetThemeColor(hTheme, iPartId, iStateId, TMT_GRADIENTCOLOR2, &gradient2);
663
664         vert[0].x     = pRect->left;
665         vert[0].y     = pRect->top;
666         vert[0].Red   = GetRValue(gradient1) << 8;
667         vert[0].Green = GetGValue(gradient1) << 8;
668         vert[0].Blue  = GetBValue(gradient1) << 8;
669         vert[0].Alpha = 0x0000;
670
671         vert[1].x     = pRect->right;
672         vert[1].y     = pRect->bottom;
673         vert[1].Red   = GetRValue(gradient2) << 8;
674         vert[1].Green = GetGValue(gradient2) << 8;
675         vert[1].Blue  = GetBValue(gradient2) << 8;
676         vert[1].Alpha = 0x0000;
677
678         gRect.UpperLeft  = 0;
679         gRect.LowerRight = 1;
680         GradientFill(hdc,vert,2,&gRect,1,filltype==FT_HORZGRADIENT?GRADIENT_FILL_RECT_H:GRADIENT_FILL_RECT_V);
681     }
682     else if(filltype == FT_RADIALGRADIENT) {
683         /* I've never seen this used in a theme */
684         FIXME("Radial gradient\n");
685     }
686     else if(filltype == FT_TILEIMAGE) {
687         /* I've never seen this used in a theme */
688         FIXME("Tile image\n");
689     }
690     return hr;
691 }
692
693 /***********************************************************************
694  *      UXTHEME_DrawBorderBackground
695  *
696  * Draw an imagefile background
697  */
698 HRESULT UXTHEME_DrawBorderBackground(HTHEME hTheme, HDC hdc, int iPartId,
699                                      int iStateId, const RECT *pRect,
700                                      const DTBGOPTS *pOptions)
701 {
702     HRESULT hr;
703     RECT rt;
704
705     CopyRect(&rt, pRect);
706
707     hr = UXTHEME_DrawBorderRectangle(hTheme, hdc, iPartId, iStateId, &rt, pOptions);
708     if(FAILED(hr))
709         return hr;
710     return UXTHEME_DrawBackgroundFill(hTheme, hdc, iPartId, iStateId, &rt, pOptions);
711 }
712
713 /***********************************************************************
714  *      DrawThemeBackgroundEx                               (UXTHEME.@)
715  */
716 HRESULT WINAPI DrawThemeBackgroundEx(HTHEME hTheme, HDC hdc, int iPartId,
717                                      int iStateId, const RECT *pRect,
718                                      const DTBGOPTS *pOptions)
719 {
720     HRESULT hr;
721     const DTBGOPTS defaultOpts = {sizeof(DTBGOPTS), 0, {0,0,0,0}};
722     const DTBGOPTS *opts;
723     HRGN clip = NULL;
724     int hasClip = -1;
725     int bgtype = BT_BORDERFILL;
726     RECT rt;
727
728     TRACE("(%p,%p,%d,%d,%ld,%ld)\n", hTheme, hdc, iPartId, iStateId,pRect->left,pRect->top);
729     if(!hTheme)
730         return E_HANDLE;
731
732     /* Ensure we have a DTBGOPTS structure available, simplifies some of the code */
733     opts = pOptions;
734     if(!opts) opts = &defaultOpts;
735
736     if(opts->dwFlags & DTBG_CLIPRECT) {
737         clip = CreateRectRgn(0,0,1,1);
738         hasClip = GetClipRgn(hdc, clip);
739         if(hasClip == -1)
740             TRACE("Failed to get original clipping region\n");
741         else
742             IntersectClipRect(hdc, opts->rcClip.left, opts->rcClip.top, opts->rcClip.right, opts->rcClip.bottom);
743     }
744     CopyRect(&rt, pRect);
745
746     GetThemeEnumValue(hTheme, iPartId, iStateId, TMT_BGTYPE, &bgtype);
747     if(bgtype == BT_IMAGEFILE)
748         hr = UXTHEME_DrawImageBackground(hTheme, hdc, iPartId, iStateId, &rt, opts);
749     else if(bgtype == BT_BORDERFILL)
750         hr = UXTHEME_DrawBorderBackground(hTheme, hdc, iPartId, iStateId, pRect, opts);
751     else {
752         FIXME("Unknown background type\n");
753         /* This should never happen, and hence I don't know what to return */
754         hr = E_FAIL;
755     }
756     if(SUCCEEDED(hr))
757         hr = UXTHEME_DrawGlyph(hTheme, hdc, iPartId, iStateId, &rt, opts);
758     if(opts->dwFlags & DTBG_CLIPRECT) {
759         if(hasClip == 0)
760             SelectClipRgn(hdc, NULL);
761         else if(hasClip == 1)
762             SelectClipRgn(hdc, clip);
763         DeleteObject(clip);
764     }
765     return hr;
766 }
767
768 /***********************************************************************
769  *      DrawThemeEdge                                       (UXTHEME.@)
770  */
771 HRESULT WINAPI DrawThemeEdge(HTHEME hTheme, HDC hdc, int iPartId,
772                              int iStateId, const RECT *pDestRect, UINT uEdge,
773                              UINT uFlags, RECT *pContentRect)
774 {
775     FIXME("%d %d 0x%08x 0x%08x: stub\n", iPartId, iStateId, uEdge, uFlags);
776     if(!hTheme)
777         return E_HANDLE;
778     return ERROR_CALL_NOT_IMPLEMENTED;
779 }
780
781 /***********************************************************************
782  *      DrawThemeIcon                                       (UXTHEME.@)
783  */
784 HRESULT WINAPI DrawThemeIcon(HTHEME hTheme, HDC hdc, int iPartId, int iStateId,
785                              const RECT *pRect, HIMAGELIST himl, int iImageIndex)
786 {
787     FIXME("%d %d: stub\n", iPartId, iStateId);
788     if(!hTheme)
789         return E_HANDLE;
790     return ERROR_CALL_NOT_IMPLEMENTED;
791 }
792
793 /***********************************************************************
794  *      DrawThemeText                                       (UXTHEME.@)
795  */
796 HRESULT WINAPI DrawThemeText(HTHEME hTheme, HDC hdc, int iPartId, int iStateId,
797                              LPCWSTR pszText, int iCharCount, DWORD dwTextFlags,
798                              DWORD dwTextFlags2, const RECT *pRect)
799 {
800     HRESULT hr;
801     HFONT hFont = NULL;
802     HGDIOBJ oldFont = NULL;
803     LOGFONTW logfont;
804     COLORREF textColor;
805     COLORREF oldTextColor;
806     int oldBkMode;
807     RECT rt;
808     
809     TRACE("%d %d: stub\n", iPartId, iStateId);
810     if(!hTheme)
811         return E_HANDLE;
812     
813     hr = GetThemeFont(hTheme, hdc, iPartId, iStateId, TMT_FONT, &logfont);
814     if(SUCCEEDED(hr)) {
815         hFont = CreateFontIndirectW(&logfont);
816         if(!hFont)
817             TRACE("Failed to create font\n");
818     }
819     CopyRect(&rt, pRect);
820     if(hFont)
821         oldFont = SelectObject(hdc, hFont);
822         
823     if(dwTextFlags2 & DTT_GRAYED)
824         textColor = GetSysColor(COLOR_GRAYTEXT);
825     else {
826         if(FAILED(GetThemeColor(hTheme, iPartId, iStateId, TMT_TEXTCOLOR, &textColor)))
827             textColor = GetTextColor(hdc);
828     }
829     oldTextColor = SetTextColor(hdc, textColor);
830     oldBkMode = SetBkMode(hdc, TRANSPARENT);
831     DrawTextW(hdc, pszText, iCharCount, &rt, dwTextFlags);
832     SetBkMode(hdc, oldBkMode);
833     SetTextColor(hdc, oldTextColor);
834
835     if(hFont) {
836         SelectObject(hdc, oldFont);
837         DeleteObject(hFont);
838     }
839     return S_OK;
840 }
841
842 /***********************************************************************
843  *      GetThemeBackgroundContentRect                       (UXTHEME.@)
844  */
845 HRESULT WINAPI GetThemeBackgroundContentRect(HTHEME hTheme, HDC hdc, int iPartId,
846                                              int iStateId,
847                                              const RECT *pBoundingRect,
848                                              RECT *pContentRect)
849 {
850     MARGINS margin;
851     HRESULT hr;
852
853     TRACE("(%d,%d)\n", iPartId, iStateId);
854     if(!hTheme)
855         return E_HANDLE;
856
857     hr = GetThemeMargins(hTheme, hdc, iPartId, iStateId, TMT_CONTENTMARGINS, NULL, &margin);
858     if(FAILED(hr)) {
859         TRACE("Margins not found\n");
860         return hr;
861     }
862     pContentRect->left = pBoundingRect->left + margin.cxLeftWidth;
863     pContentRect->top  = pBoundingRect->top + margin.cyTopHeight;
864     pContentRect->right = pBoundingRect->right - margin.cxRightWidth;
865     pContentRect->bottom = pBoundingRect->bottom - margin.cyBottomHeight;
866
867     TRACE("left:%ld,top:%ld,right:%ld,bottom:%ld\n", pContentRect->left, pContentRect->top, pContentRect->right, pContentRect->bottom);
868
869     return S_OK;
870 }
871
872 /***********************************************************************
873  *      GetThemeBackgroundExtent                            (UXTHEME.@)
874  */
875 HRESULT WINAPI GetThemeBackgroundExtent(HTHEME hTheme, HDC hdc, int iPartId,
876                                         int iStateId, const RECT *pContentRect,
877                                         RECT *pExtentRect)
878 {
879     MARGINS margin;
880     HRESULT hr;
881
882     TRACE("(%d,%d)\n", iPartId, iStateId);
883     if(!hTheme)
884         return E_HANDLE;
885
886     hr = GetThemeMargins(hTheme, hdc, iPartId, iStateId, TMT_CONTENTMARGINS, NULL, &margin);
887     if(FAILED(hr)) {
888         TRACE("Margins not found\n");
889         return hr;
890     }
891     pExtentRect->left = pContentRect->left - margin.cxLeftWidth;
892     pExtentRect->top  = pContentRect->top - margin.cyTopHeight;
893     pExtentRect->right = pContentRect->right + margin.cxRightWidth;
894     pExtentRect->bottom = pContentRect->bottom + margin.cyBottomHeight;
895
896     TRACE("left:%ld,top:%ld,right:%ld,bottom:%ld\n", pExtentRect->left, pExtentRect->top, pExtentRect->right, pExtentRect->bottom);
897
898     return S_OK;
899 }
900
901 /***********************************************************************
902  *      GetThemeBackgroundRegion                            (UXTHEME.@)
903  *
904  * Calculate the background region, taking into consideration transparent areas
905  * of the background image.
906  */
907 HRESULT WINAPI GetThemeBackgroundRegion(HTHEME hTheme, HDC hdc, int iPartId,
908                                         int iStateId, const RECT *pRect,
909                                         HRGN *pRegion)
910 {
911     HRESULT hr = S_OK;
912     int bgtype = BT_BORDERFILL;
913
914     TRACE("(%p,%p,%d,%d)\n", hTheme, hdc, iPartId, iStateId);
915     if(!hTheme)
916         return E_HANDLE;
917     if(!pRect || !pRegion)
918         return E_POINTER;
919
920     GetThemeEnumValue(hTheme, iPartId, iStateId, TMT_BGTYPE, &bgtype);
921     if(bgtype == BT_IMAGEFILE) {
922         FIXME("Images not handled yet\n");
923         hr = ERROR_CALL_NOT_IMPLEMENTED;
924     }
925     else if(bgtype == BT_BORDERFILL) {
926         *pRegion = CreateRectRgn(pRect->left, pRect->top, pRect->right, pRect->bottom);
927         if(!*pRegion)
928             hr = HRESULT_FROM_WIN32(GetLastError());
929     }
930     else {
931         FIXME("Unknown background type\n");
932         /* This should never happen, and hence I don't know what to return */
933         hr = E_FAIL;
934     }
935     return hr;
936 }
937
938 /***********************************************************************
939  *      GetThemePartSize                                    (UXTHEME.@)
940  */
941 HRESULT WINAPI GetThemePartSize(HTHEME hTheme, HDC hdc, int iPartId,
942                                 int iStateId, RECT *prc, THEMESIZE eSize,
943                                 SIZE *psz)
944 {
945     FIXME("%d %d %d: stub\n", iPartId, iStateId, eSize);
946     if(!hTheme)
947         return E_HANDLE;
948     return ERROR_CALL_NOT_IMPLEMENTED;
949 }
950
951
952 /***********************************************************************
953  *      GetThemeTextExtent                                  (UXTHEME.@)
954  */
955 HRESULT WINAPI GetThemeTextExtent(HTHEME hTheme, HDC hdc, int iPartId,
956                                   int iStateId, LPCWSTR pszText, int iCharCount,
957                                   DWORD dwTextFlags, const RECT *pBoundingRect,
958                                   RECT *pExtentRect)
959 {
960     HRESULT hr;
961     HFONT hFont = NULL;
962     HGDIOBJ oldFont = NULL;
963     LOGFONTW logfont;
964     RECT rt = {0,0,0xFFFF,0xFFFF};
965     
966     TRACE("%d %d: stub\n", iPartId, iStateId);
967     if(!hTheme)
968         return E_HANDLE;
969
970     if(pBoundingRect)
971         CopyRect(&rt, pBoundingRect);
972             
973     hr = GetThemeFont(hTheme, hdc, iPartId, iStateId, TMT_FONT, &logfont);
974     if(SUCCEEDED(hr)) {
975         hFont = CreateFontIndirectW(&logfont);
976         if(!hFont)
977             TRACE("Failed to create font\n");
978     }
979     if(hFont)
980         oldFont = SelectObject(hdc, hFont);
981         
982     DrawTextW(hdc, pszText, iCharCount, &rt, dwTextFlags|DT_CALCRECT);
983     CopyRect(pExtentRect, &rt);
984
985     if(hFont) {
986         SelectObject(hdc, oldFont);
987         DeleteObject(hFont);
988     }
989     return S_OK;
990 }
991
992 /***********************************************************************
993  *      GetThemeTextMetrics                                 (UXTHEME.@)
994  */
995 HRESULT WINAPI GetThemeTextMetrics(HTHEME hTheme, HDC hdc, int iPartId,
996                                    int iStateId, TEXTMETRICW *ptm)
997 {
998     HRESULT hr;
999     HFONT hFont = NULL;
1000     HGDIOBJ oldFont = NULL;
1001     LOGFONTW logfont;
1002
1003     TRACE("(%p, %p, %d, %d)\n", hTheme, hdc, iPartId, iStateId);
1004     if(!hTheme)
1005         return E_HANDLE;
1006
1007     hr = GetThemeFont(hTheme, hdc, iPartId, iStateId, TMT_FONT, &logfont);
1008     if(SUCCEEDED(hr)) {
1009         hFont = CreateFontIndirectW(&logfont);
1010         if(!hFont)
1011             TRACE("Failed to create font\n");
1012     }
1013     if(hFont)
1014         oldFont = SelectObject(hdc, hFont);
1015
1016     if(!GetTextMetricsW(hdc, ptm))
1017         hr = HRESULT_FROM_WIN32(GetLastError());
1018
1019     if(hFont) {
1020         SelectObject(hdc, oldFont);
1021         DeleteObject(hFont);
1022     }
1023     return hr;
1024 }
1025
1026 /***********************************************************************
1027  *      IsThemeBackgroundPartiallyTransparent               (UXTHEME.@)
1028  */
1029 BOOL WINAPI IsThemeBackgroundPartiallyTransparent(HTHEME hTheme, int iPartId,
1030                                                   int iStateId)
1031 {
1032     BOOL transparent = FALSE;
1033     TRACE("(%d,%d)\n", iPartId, iStateId);
1034     GetThemeBool(hTheme, iPartId, iStateId, TMT_TRANSPARENT, &transparent);
1035     return transparent;
1036 }