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