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