Fix signed/unsigned comparison warnings.
[wine.git] / controls / icontitle.c
blob018a4a0847ebe19f3077ad3efbbd6f0dbb43d5d2
1 /*
2 * Icontitle window class.
4 * Copyright 1997 Alex Korobka
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.
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.
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
21 #include "config.h"
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <string.h>
26 #ifdef HAVE_UNISTD_H
27 # include <unistd.h>
28 #endif
30 #include "windef.h"
31 #include "winbase.h"
32 #include "wingdi.h"
33 #include "winuser.h"
34 #include "wine/unicode.h"
35 #include "controls.h"
36 #include "win.h"
38 static BOOL bMultiLineTitle;
39 static HFONT hIconTitleFont;
41 static LRESULT WINAPI IconTitleWndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam );
43 /*********************************************************************
44 * icon title class descriptor
46 const struct builtin_class_descr ICONTITLE_builtin_class =
48 ICONTITLE_CLASS_ATOM, /* name */
49 0, /* style */
50 NULL, /* procA (winproc is Unicode only) */
51 IconTitleWndProc, /* procW */
52 0, /* extra */
53 IDC_ARROW, /* cursor */
54 0 /* brush */
59 /***********************************************************************
60 * ICONTITLE_Create
62 HWND ICONTITLE_Create( HWND owner )
64 HWND hWnd;
65 HINSTANCE instance = (HINSTANCE)GetWindowLongA( owner, GWL_HINSTANCE );
66 LONG style = WS_CLIPSIBLINGS;
68 if (!IsWindowEnabled(owner)) style |= WS_DISABLED;
69 if( GetWindowLongA( owner, GWL_STYLE ) & WS_CHILD )
70 hWnd = CreateWindowExA( 0, ICONTITLE_CLASS_ATOM, NULL,
71 style | WS_CHILD, 0, 0, 1, 1,
72 GetParent(owner), 0, instance, NULL );
73 else
74 hWnd = CreateWindowExA( 0, ICONTITLE_CLASS_ATOM, NULL,
75 style, 0, 0, 1, 1,
76 owner, 0, instance, NULL );
77 WIN_SetOwner( hWnd, owner ); /* MDI depends on this */
78 SetWindowLongW( hWnd, GWL_STYLE,
79 GetWindowLongW( hWnd, GWL_STYLE ) & ~(WS_CAPTION | WS_BORDER) );
80 return hWnd;
83 /***********************************************************************
84 * ICONTITLE_SetTitlePos
86 static BOOL ICONTITLE_SetTitlePos( HWND hwnd, HWND owner )
88 static const WCHAR emptyTitleText[] = {'<','.','.','.','>',0};
89 WCHAR str[80];
90 HDC hDC;
91 HFONT hPrevFont;
92 RECT rect;
93 INT cx, cy;
94 POINT pt;
96 int length = GetWindowTextW( owner, str, sizeof(str)/sizeof(WCHAR) );
98 while (length && str[length - 1] == ' ') /* remove trailing spaces */
99 str[--length] = 0;
101 if( !length )
103 strcpyW( str, emptyTitleText );
104 length = strlenW( str );
107 if (!(hDC = GetDC( hwnd ))) return FALSE;
109 hPrevFont = SelectObject( hDC, hIconTitleFont );
111 SetRect( &rect, 0, 0, GetSystemMetrics(SM_CXICONSPACING) -
112 GetSystemMetrics(SM_CXBORDER) * 2,
113 GetSystemMetrics(SM_CYBORDER) * 2 );
115 DrawTextW( hDC, str, length, &rect, DT_CALCRECT | DT_CENTER | DT_NOPREFIX | DT_WORDBREAK |
116 (( bMultiLineTitle ) ? 0 : DT_SINGLELINE) );
118 SelectObject( hDC, hPrevFont );
119 ReleaseDC( hwnd, hDC );
121 cx = rect.right - rect.left + 4 * GetSystemMetrics(SM_CXBORDER);
122 cy = rect.bottom - rect.top;
124 pt.x = (GetSystemMetrics(SM_CXICON) - cx) / 2;
125 pt.y = GetSystemMetrics(SM_CYICON);
127 /* point is relative to owner, make it relative to parent */
128 MapWindowPoints( owner, GetParent(hwnd), &pt, 1 );
130 SetWindowPos( hwnd, owner, pt.x, pt.y, cx, cy, SWP_NOACTIVATE );
131 return TRUE;
134 /***********************************************************************
135 * ICONTITLE_Paint
137 static BOOL ICONTITLE_Paint( HWND hwnd, HWND owner, HDC hDC, BOOL bActive )
139 RECT rect;
140 HFONT hPrevFont;
141 HBRUSH hBrush = 0;
142 COLORREF textColor = 0;
144 if( bActive )
146 hBrush = GetSysColorBrush(COLOR_ACTIVECAPTION);
147 textColor = GetSysColor(COLOR_CAPTIONTEXT);
149 else
151 if( GetWindowLongA( hwnd, GWL_STYLE ) & WS_CHILD )
153 hBrush = (HBRUSH) GetClassLongA(hwnd, GCL_HBRBACKGROUND);
154 if( hBrush )
156 INT level;
157 LOGBRUSH logBrush;
158 GetObjectA( hBrush, sizeof(logBrush), &logBrush );
159 level = GetRValue(logBrush.lbColor) +
160 GetGValue(logBrush.lbColor) +
161 GetBValue(logBrush.lbColor);
162 if( level < (0x7F * 3) )
163 textColor = RGB( 0xFF, 0xFF, 0xFF );
165 else
166 hBrush = GetStockObject( WHITE_BRUSH );
168 else
170 hBrush = GetStockObject( BLACK_BRUSH );
171 textColor = RGB( 0xFF, 0xFF, 0xFF );
175 GetClientRect( hwnd, &rect );
176 DPtoLP( hDC, (LPPOINT)&rect, 2 );
177 FillRect( hDC, &rect, hBrush );
179 hPrevFont = SelectObject( hDC, hIconTitleFont );
180 if( hPrevFont )
182 WCHAR buffer[80];
184 INT length = GetWindowTextW( owner, buffer, sizeof(buffer) );
185 SetTextColor( hDC, textColor );
186 SetBkMode( hDC, TRANSPARENT );
188 DrawTextW( hDC, buffer, length, &rect, DT_CENTER | DT_NOPREFIX |
189 DT_WORDBREAK | ((bMultiLineTitle) ? 0 : DT_SINGLELINE) );
191 SelectObject( hDC, hPrevFont );
193 return (hPrevFont != 0);
196 /***********************************************************************
197 * IconTitleWndProc
199 LRESULT WINAPI IconTitleWndProc( HWND hWnd, UINT msg,
200 WPARAM wParam, LPARAM lParam )
202 HWND owner = GetWindow( hWnd, GW_OWNER );
204 if (!IsWindow(hWnd)) return 0;
206 switch( msg )
208 case WM_CREATE:
209 if (!hIconTitleFont)
211 LOGFONTA logFont;
212 SystemParametersInfoA( SPI_GETICONTITLELOGFONT, 0, &logFont, 0 );
213 SystemParametersInfoA( SPI_GETICONTITLEWRAP, 0, &bMultiLineTitle, 0 );
214 hIconTitleFont = CreateFontIndirectA( &logFont );
216 return (hIconTitleFont ? 0 : -1);
217 case WM_NCHITTEST:
218 return HTCAPTION;
219 case WM_NCMOUSEMOVE:
220 case WM_NCLBUTTONDBLCLK:
221 return SendMessageW( owner, msg, wParam, lParam );
222 case WM_ACTIVATE:
223 if( wParam ) SetActiveWindow( owner );
224 return 0;
225 case WM_CLOSE:
226 return 0;
227 case WM_SHOWWINDOW:
228 if (wParam) ICONTITLE_SetTitlePos( hWnd, owner );
229 return 0;
230 case WM_ERASEBKGND:
231 if( GetWindowLongA( owner, GWL_STYLE ) & WS_CHILD )
232 lParam = SendMessageA( owner, WM_ISACTIVEICON, 0, 0 );
233 else
234 lParam = (owner == GetActiveWindow());
235 if( ICONTITLE_Paint( hWnd, owner, (HDC)wParam, (BOOL)lParam ) )
236 ValidateRect( hWnd, NULL );
237 return 1;
239 return DefWindowProcW( hWnd, msg, wParam, lParam );