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