Guard against incoming string of NULL in WM_SETTEXT.
[wine.git] / controls / icontitle.c
blob0f4e4e6a3d6d87449f299f63700d790f8b777cec
1 /*
2 * Icontitle window class.
4 * Copyright 1997 Alex Korobka
5 */
7 #include <stdio.h>
8 #include <string.h>
9 #include <unistd.h>
11 #include "windef.h"
12 #include "winbase.h"
13 #include "wingdi.h"
14 #include "winuser.h"
15 #include "wine/winuser16.h"
16 #include "wine/unicode.h"
17 #include "controls.h"
18 #include "win.h"
19 #include "heap.h"
21 static BOOL bMultiLineTitle;
22 static HFONT hIconTitleFont;
24 static LRESULT WINAPI IconTitleWndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam );
26 /*********************************************************************
27 * icon title class descriptor
29 const struct builtin_class_descr ICONTITLE_builtin_class =
31 ICONTITLE_CLASS_ATOM, /* name */
32 CS_GLOBALCLASS, /* style */
33 NULL, /* procA (winproc is Unicode only) */
34 IconTitleWndProc, /* procW */
35 0, /* extra */
36 IDC_ARROWA, /* cursor */
37 0 /* brush */
42 /***********************************************************************
43 * ICONTITLE_Create
45 HWND ICONTITLE_Create( WND* wnd )
47 WND* wndPtr;
48 HWND hWnd;
50 if( wnd->dwStyle & WS_CHILD )
51 hWnd = CreateWindowExA( 0, ICONTITLE_CLASS_ATOM, NULL,
52 WS_CHILD | WS_CLIPSIBLINGS, 0, 0, 1, 1,
53 wnd->parent->hwndSelf, 0, wnd->hInstance, NULL );
54 else
55 hWnd = CreateWindowExA( 0, ICONTITLE_CLASS_ATOM, NULL,
56 WS_CLIPSIBLINGS, 0, 0, 1, 1,
57 wnd->hwndSelf, 0, wnd->hInstance, NULL );
58 wndPtr = WIN_FindWndPtr( hWnd );
59 if( wndPtr )
61 wndPtr->owner = wnd; /* MDI depends on this */
62 wndPtr->dwStyle &= ~(WS_CAPTION | WS_BORDER);
63 if( wnd->dwStyle & WS_DISABLED ) wndPtr->dwStyle |= WS_DISABLED;
64 WIN_ReleaseWndPtr(wndPtr);
65 return hWnd;
67 return 0;
70 /***********************************************************************
71 * ICONTITLE_GetTitlePos
73 static BOOL ICONTITLE_GetTitlePos( WND* wnd, LPRECT lpRect )
75 static WCHAR emptyTitleText[] = {'<','.','.','.','>',0};
76 LPWSTR str = NULL;
77 int length = lstrlenW( wnd->owner->text );
79 if( length )
81 str = HeapAlloc( GetProcessHeap(), 0, (length + 1) * sizeof(WCHAR) );
82 strcpyW( str, wnd->owner->text );
83 while( str[length - 1] == ' ' ) /* remove trailing spaces */
85 str[--length] = '\0';
86 if( !length )
88 HeapFree( GetProcessHeap(), 0, str );
89 break;
93 if( !length )
95 str = emptyTitleText;
96 length = lstrlenW( str );
99 if( str )
101 HDC hDC = GetDC( wnd->hwndSelf );
102 if( hDC )
104 HFONT hPrevFont = SelectObject( hDC, hIconTitleFont );
106 SetRect( lpRect, 0, 0, GetSystemMetrics(SM_CXICONSPACING) -
107 GetSystemMetrics(SM_CXBORDER) * 2,
108 GetSystemMetrics(SM_CYBORDER) * 2 );
110 DrawTextW( hDC, str, length, lpRect, DT_CALCRECT |
111 DT_CENTER | DT_NOPREFIX | DT_WORDBREAK |
112 (( bMultiLineTitle ) ? 0 : DT_SINGLELINE) );
114 SelectObject( hDC, hPrevFont );
115 ReleaseDC( wnd->hwndSelf, hDC );
117 lpRect->right += 4 * GetSystemMetrics(SM_CXBORDER) - lpRect->left;
118 lpRect->left = wnd->owner->rectWindow.left + GetSystemMetrics(SM_CXICON) / 2 -
119 (lpRect->right - lpRect->left) / 2;
120 lpRect->bottom -= lpRect->top;
121 lpRect->top = wnd->owner->rectWindow.top + GetSystemMetrics(SM_CYICON);
123 if( str != emptyTitleText ) HeapFree( GetProcessHeap(), 0, str );
124 return ( hDC ) ? TRUE : FALSE;
126 return FALSE;
129 /***********************************************************************
130 * ICONTITLE_Paint
132 static BOOL ICONTITLE_Paint( WND* wnd, HDC hDC, BOOL bActive )
134 HFONT hPrevFont;
135 HBRUSH hBrush = 0;
136 COLORREF textColor = 0;
138 if( bActive )
140 hBrush = GetSysColorBrush(COLOR_ACTIVECAPTION);
141 textColor = GetSysColor(COLOR_CAPTIONTEXT);
143 else
145 if( wnd->dwStyle & WS_CHILD )
147 hBrush = (HBRUSH) GetClassLongA(wnd->hwndSelf, GCL_HBRBACKGROUND);
148 if( hBrush )
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 );
159 else
160 hBrush = GetStockObject( WHITE_BRUSH );
162 else
164 hBrush = GetStockObject( BLACK_BRUSH );
165 textColor = RGB( 0xFF, 0xFF, 0xFF );
169 FillWindow16( wnd->parent->hwndSelf, wnd->hwndSelf, hDC, hBrush );
171 hPrevFont = SelectObject( hDC, hIconTitleFont );
172 if( hPrevFont )
174 RECT rect;
175 INT length;
176 char buffer[80];
178 rect.left = rect.top = 0;
179 rect.right = wnd->rectWindow.right - wnd->rectWindow.left;
180 rect.bottom = wnd->rectWindow.bottom - wnd->rectWindow.top;
182 length = GetWindowTextA( wnd->owner->hwndSelf, buffer, 80 );
183 SetTextColor( hDC, textColor );
184 SetBkMode( hDC, TRANSPARENT );
186 DrawTextA( hDC, buffer, length, &rect, DT_CENTER | DT_NOPREFIX |
187 DT_WORDBREAK | ((bMultiLineTitle) ? 0 : DT_SINGLELINE) );
189 SelectObject( hDC, hPrevFont );
191 return ( hPrevFont ) ? TRUE : FALSE;
194 /***********************************************************************
195 * IconTitleWndProc
197 LRESULT WINAPI IconTitleWndProc( HWND hWnd, UINT msg,
198 WPARAM wParam, LPARAM lParam )
200 LRESULT retvalue;
201 WND *wnd = WIN_FindWndPtr( hWnd );
203 switch( msg )
205 case WM_CREATE:
206 if (!hIconTitleFont)
208 LOGFONTA logFont;
209 SystemParametersInfoA( SPI_GETICONTITLELOGFONT, 0, &logFont, 0 );
210 SystemParametersInfoA( SPI_GETICONTITLEWRAP, 0, &bMultiLineTitle, 0 );
211 hIconTitleFont = CreateFontIndirectA( &logFont );
213 retvalue = (hIconTitleFont) ? 0 : -1;
214 goto END;
215 case WM_NCHITTEST:
216 retvalue = HTCAPTION;
217 goto END;
218 case WM_NCMOUSEMOVE:
219 case WM_NCLBUTTONDBLCLK:
220 retvalue = SendMessageA( wnd->owner->hwndSelf, msg, wParam, lParam );
221 goto END;
222 case WM_ACTIVATE:
223 if( wParam ) SetActiveWindow( wnd->owner->hwndSelf );
224 /* fall through */
226 case WM_CLOSE:
227 retvalue = 0;
228 goto END;
229 case WM_SHOWWINDOW:
230 if( wnd && wParam )
232 RECT titleRect;
234 ICONTITLE_GetTitlePos( wnd, &titleRect );
235 if( wnd->owner->next != wnd ) /* keep icon title behind the owner */
236 SetWindowPos( hWnd, wnd->owner->hwndSelf,
237 titleRect.left, titleRect.top,
238 titleRect.right, titleRect.bottom, SWP_NOACTIVATE );
239 else
240 SetWindowPos( hWnd, 0, titleRect.left, titleRect.top,
241 titleRect.right, titleRect.bottom,
242 SWP_NOACTIVATE | SWP_NOZORDER );
244 retvalue = 0;
245 goto END;
246 case WM_ERASEBKGND:
247 if( wnd )
249 WND* iconWnd = WIN_LockWndPtr(wnd->owner);
251 if( iconWnd->dwStyle & WS_CHILD )
252 lParam = SendMessageA( iconWnd->hwndSelf, WM_ISACTIVEICON, 0, 0 );
253 else
254 lParam = (iconWnd->hwndSelf == GetActiveWindow16());
256 WIN_ReleaseWndPtr(iconWnd);
257 if( ICONTITLE_Paint( wnd, (HDC)wParam, (BOOL)lParam ) )
258 ValidateRect( hWnd, NULL );
259 retvalue = 1;
260 goto END;
264 retvalue = DefWindowProcW( hWnd, msg, wParam, lParam );
265 END:
266 WIN_ReleaseWndPtr(wnd);
267 return retvalue;