Change parameter in SHGetSpecialFolderPathA/W to int as documented in
[wine] / controls / icontitle.c
1 /*
2  * Icontitle window class.
3  *
4  * Copyright 1997 Alex Korobka
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 <stdarg.h>
24 #include <stdio.h>
25 #include <string.h>
26 #ifdef HAVE_UNISTD_H
27 # include <unistd.h>
28 #endif
29
30 #include "windef.h"
31 #include "winbase.h"
32 #include "wingdi.h"
33 #include "winuser.h"
34 #include "wine/winuser16.h"
35 #include "wine/unicode.h"
36 #include "controls.h"
37 #include "win.h"
38
39 static BOOL bMultiLineTitle;
40 static HFONT hIconTitleFont;
41
42 static LRESULT WINAPI IconTitleWndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam );
43
44 /*********************************************************************
45  * icon title class descriptor
46  */
47 const struct builtin_class_descr ICONTITLE_builtin_class =
48 {
49     ICONTITLE_CLASS_ATOM, /* name */
50     CS_GLOBALCLASS,       /* style */
51     NULL,                 /* procA (winproc is Unicode only) */
52     IconTitleWndProc,     /* procW */
53     0,                    /* extra */
54     IDC_ARROWA,           /* cursor */
55     0                     /* brush */
56 };
57
58
59
60 /***********************************************************************
61  *           ICONTITLE_Create
62  */
63 HWND ICONTITLE_Create( HWND owner )
64 {
65     HWND hWnd;
66     HINSTANCE instance = (HINSTANCE)GetWindowLongA( owner, GWL_HINSTANCE );
67     LONG style = WS_CLIPSIBLINGS;
68
69     if (!IsWindowEnabled(owner)) style |= WS_DISABLED;
70     if( GetWindowLongA( owner, GWL_STYLE ) & WS_CHILD )
71         hWnd = CreateWindowExA( 0, ICONTITLE_CLASS_ATOM, NULL,
72                                 style | WS_CHILD, 0, 0, 1, 1,
73                                 GetParent(owner), 0, instance, NULL );
74     else
75         hWnd = CreateWindowExA( 0, ICONTITLE_CLASS_ATOM, NULL,
76                                 style, 0, 0, 1, 1,
77                                 owner, 0, instance, NULL );
78     WIN_SetOwner( hWnd, owner );  /* MDI depends on this */
79     SetWindowLongW( hWnd, GWL_STYLE,
80                     GetWindowLongW( hWnd, GWL_STYLE ) & ~(WS_CAPTION | WS_BORDER) );
81     return hWnd;
82 }
83
84 /***********************************************************************
85  *           ICONTITLE_SetTitlePos
86  */
87 static BOOL ICONTITLE_SetTitlePos( HWND hwnd, HWND owner )
88 {
89     static WCHAR emptyTitleText[] = {'<','.','.','.','>',0};
90     WCHAR str[80];
91     HDC hDC;
92     HFONT hPrevFont;
93     RECT rect;
94     INT cx, cy;
95     POINT pt;
96
97     int length = GetWindowTextW( owner, str, sizeof(str)/sizeof(WCHAR) );
98
99     while (length && str[length - 1] == ' ') /* remove trailing spaces */
100         str[--length] = 0;
101
102     if( !length )
103     {
104         strcpyW( str, emptyTitleText );
105         length = strlenW( str );
106     }
107
108     if (!(hDC = GetDC( hwnd ))) return FALSE;
109
110     hPrevFont = SelectObject( hDC, hIconTitleFont );
111
112     SetRect( &rect, 0, 0, GetSystemMetrics(SM_CXICONSPACING) -
113              GetSystemMetrics(SM_CXBORDER) * 2,
114              GetSystemMetrics(SM_CYBORDER) * 2 );
115
116     DrawTextW( hDC, str, length, &rect, DT_CALCRECT | DT_CENTER | DT_NOPREFIX | DT_WORDBREAK |
117                (( bMultiLineTitle ) ? 0 : DT_SINGLELINE) );
118
119     SelectObject( hDC, hPrevFont );
120     ReleaseDC( hwnd, hDC );
121
122     cx = rect.right - rect.left +  4 * GetSystemMetrics(SM_CXBORDER);
123     cy = rect.bottom - rect.top;
124
125     pt.x = (GetSystemMetrics(SM_CXICON) - cx) / 2;
126     pt.y = GetSystemMetrics(SM_CYICON);
127
128     /* point is relative to owner, make it relative to parent */
129     MapWindowPoints( owner, GetParent(hwnd), &pt, 1 );
130
131     SetWindowPos( hwnd, owner, pt.x, pt.y, cx, cy, SWP_NOACTIVATE );
132     return TRUE;
133 }
134
135 /***********************************************************************
136  *           ICONTITLE_Paint
137  */
138 static BOOL ICONTITLE_Paint( HWND hwnd, HWND owner, HDC hDC, BOOL bActive )
139 {
140     RECT rect;
141     HFONT hPrevFont;
142     HBRUSH hBrush = 0;
143     COLORREF textColor = 0;
144
145     if( bActive )
146     {
147         hBrush = GetSysColorBrush(COLOR_ACTIVECAPTION);
148         textColor = GetSysColor(COLOR_CAPTIONTEXT);
149     }
150     else
151     {
152         if( GetWindowLongA( hwnd, GWL_STYLE ) & WS_CHILD )
153         {
154             hBrush = (HBRUSH) GetClassLongA(hwnd, GCL_HBRBACKGROUND);
155             if( hBrush )
156             {
157                 INT level;
158                 LOGBRUSH logBrush;
159                 GetObjectA( hBrush, sizeof(logBrush), &logBrush );
160                 level = GetRValue(logBrush.lbColor) +
161                            GetGValue(logBrush.lbColor) +
162                               GetBValue(logBrush.lbColor);
163                 if( level < (0x7F * 3) )
164                     textColor = RGB( 0xFF, 0xFF, 0xFF );
165             }
166             else
167                 hBrush = GetStockObject( WHITE_BRUSH );
168         }
169         else
170         {
171             hBrush = GetStockObject( BLACK_BRUSH );
172             textColor = RGB( 0xFF, 0xFF, 0xFF );
173         }
174     }
175
176     GetClientRect( hwnd, &rect );
177     DPtoLP( hDC, (LPPOINT)&rect, 2 );
178     FillRect( hDC, &rect, hBrush );
179
180     hPrevFont = SelectObject( hDC, hIconTitleFont );
181     if( hPrevFont )
182     {
183         WCHAR buffer[80];
184
185         INT length = GetWindowTextW( owner, buffer, sizeof(buffer) );
186         SetTextColor( hDC, textColor );
187         SetBkMode( hDC, TRANSPARENT );
188
189         DrawTextW( hDC, buffer, length, &rect, DT_CENTER | DT_NOPREFIX |
190                    DT_WORDBREAK | ((bMultiLineTitle) ? 0 : DT_SINGLELINE) );
191
192         SelectObject( hDC, hPrevFont );
193     }
194     return (hPrevFont != 0);
195 }
196
197 /***********************************************************************
198  *           IconTitleWndProc
199  */
200 LRESULT WINAPI IconTitleWndProc( HWND hWnd, UINT msg,
201                                  WPARAM wParam, LPARAM lParam )
202 {
203     HWND owner = GetWindow( hWnd, GW_OWNER );
204
205     if (!IsWindow(hWnd)) return 0;
206
207     switch( msg )
208     {
209         case WM_CREATE:
210             if (!hIconTitleFont)
211             {
212                 LOGFONTA logFont;
213                 SystemParametersInfoA( SPI_GETICONTITLELOGFONT, 0, &logFont, 0 );
214                 SystemParametersInfoA( SPI_GETICONTITLEWRAP, 0, &bMultiLineTitle, 0 );
215                 hIconTitleFont = CreateFontIndirectA( &logFont );
216             }
217             return (hIconTitleFont ? 0 : -1);
218         case WM_NCHITTEST:
219              return HTCAPTION;
220         case WM_NCMOUSEMOVE:
221         case WM_NCLBUTTONDBLCLK:
222              return SendMessageW( owner, msg, wParam, lParam );
223         case WM_ACTIVATE:
224              if( wParam ) SetActiveWindow( owner );
225              return 0;
226         case WM_CLOSE:
227              return 0;
228         case WM_SHOWWINDOW:
229             if (wParam) ICONTITLE_SetTitlePos( hWnd, owner );
230              return 0;
231         case WM_ERASEBKGND:
232             if( GetWindowLongA( owner, GWL_STYLE ) & WS_CHILD )
233                 lParam = SendMessageA( owner, WM_ISACTIVEICON, 0, 0 );
234             else
235                 lParam = (owner == GetActiveWindow());
236             if( ICONTITLE_Paint( hWnd, owner, (HDC)wParam, (BOOL)lParam ) )
237                 ValidateRect( hWnd, NULL );
238             return 1;
239     }
240     return DefWindowProcW( hWnd, msg, wParam, lParam );
241 }