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