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