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