Fixed calculation of buffer length after switch of window text to
[wine] / controls / icontitle.c
1 /*
2  * Icontitle window class.
3  *
4  * Copyright 1997 Alex Korobka
5  */
6
7 #include <stdio.h>
8 #include <string.h>
9 #include <unistd.h>
10 #include "windef.h"
11 #include "wingdi.h"
12 #include "winuser.h"
13 #include "wine/winuser16.h"
14 #include "win.h"
15 #include "desktop.h"
16 #include "heap.h"
17
18         BOOL    bMultiLineTitle;
19         HFONT   hIconTitleFont;
20
21 /***********************************************************************
22  *           ICONTITLE_Init
23  */
24 BOOL ICONTITLE_Init(void)
25 {
26     LOGFONTA logFont;
27
28     SystemParametersInfoA( SPI_GETICONTITLELOGFONT, 0, &logFont, 0 );
29     SystemParametersInfoA( SPI_GETICONTITLEWRAP, 0, &bMultiLineTitle, 0 );
30     hIconTitleFont = CreateFontIndirectA( &logFont );
31     return (hIconTitleFont) ? TRUE : FALSE;
32 }
33
34 /***********************************************************************
35  *           ICONTITLE_Create
36  */
37 HWND ICONTITLE_Create( WND* wnd )
38 {
39     WND* wndPtr;
40     HWND hWnd;
41
42     if( wnd->dwStyle & WS_CHILD )
43         hWnd = CreateWindowExA( 0, ICONTITLE_CLASS_ATOM, NULL,
44                                   WS_CHILD | WS_CLIPSIBLINGS, 0, 0, 1, 1,
45                                   wnd->parent->hwndSelf, 0, wnd->hInstance, NULL );
46     else
47         hWnd = CreateWindowExA( 0, ICONTITLE_CLASS_ATOM, NULL,
48                                   WS_CLIPSIBLINGS, 0, 0, 1, 1,
49                                   wnd->hwndSelf, 0, wnd->hInstance, NULL );
50     wndPtr = WIN_FindWndPtr( hWnd );
51     if( wndPtr )
52     {
53         wndPtr->owner = wnd;    /* MDI depends on this */
54         wndPtr->dwStyle &= ~(WS_CAPTION | WS_BORDER);
55         if( wnd->dwStyle & WS_DISABLED ) wndPtr->dwStyle |= WS_DISABLED;
56         WIN_ReleaseWndPtr(wndPtr);
57         return hWnd;
58     }
59     return 0;
60 }
61
62 /***********************************************************************
63  *           ICONTITLE_GetTitlePos
64  */
65 static BOOL ICONTITLE_GetTitlePos( WND* wnd, LPRECT lpRect )
66 {
67     static WCHAR emptyTitleText[] = {'<','.','.','.','>',0};
68     LPWSTR str;
69     int length = lstrlenW( wnd->owner->text );
70
71     if( length )
72     {
73         str = HeapAlloc( GetProcessHeap(), 0, (length + 1) * sizeof(WCHAR) );
74         lstrcpyW( str, wnd->owner->text );
75         while( str[length - 1] == ' ' ) /* remove trailing spaces */
76         { 
77             str[--length] = '\0';
78             if( !length )
79             {
80                 HeapFree( GetProcessHeap(), 0, str );
81                 break;
82             }
83         }
84     }
85     if( !length ) 
86     {
87         str = emptyTitleText;
88         length = lstrlenW( str );
89     }
90
91     if( str )
92     {
93         HDC hDC = GetDC( wnd->hwndSelf );
94         if( hDC )
95         {
96             HFONT hPrevFont = SelectObject( hDC, hIconTitleFont );
97
98             SetRect( lpRect, 0, 0, GetSystemMetrics(SM_CXICONSPACING) -
99                        GetSystemMetrics(SM_CXBORDER) * 2,
100                        GetSystemMetrics(SM_CYBORDER) * 2 );
101
102             DrawTextW( hDC, str, length, lpRect, DT_CALCRECT |
103                          DT_CENTER | DT_NOPREFIX | DT_WORDBREAK |
104                          (( bMultiLineTitle ) ? 0 : DT_SINGLELINE) );
105
106             SelectObject( hDC, hPrevFont );
107             ReleaseDC( wnd->hwndSelf, hDC );
108
109             lpRect->right += 4 * GetSystemMetrics(SM_CXBORDER) - lpRect->left;
110             lpRect->left = wnd->owner->rectWindow.left + GetSystemMetrics(SM_CXICON) / 2 -
111                                       (lpRect->right - lpRect->left) / 2;
112             lpRect->bottom -= lpRect->top;
113             lpRect->top = wnd->owner->rectWindow.top + GetSystemMetrics(SM_CYICON);
114         }
115         if( str != emptyTitleText ) HeapFree( GetProcessHeap(), 0, str );
116         return ( hDC ) ? TRUE : FALSE;
117     }
118     return FALSE;
119 }
120
121 /***********************************************************************
122  *           ICONTITLE_Paint
123  */
124 static BOOL ICONTITLE_Paint( WND* wnd, HDC hDC, BOOL bActive )
125 {
126     HFONT hPrevFont;
127     HBRUSH hBrush = 0;
128     COLORREF textColor = 0;
129
130     if( bActive )
131     {
132         hBrush = GetSysColorBrush(COLOR_ACTIVECAPTION);
133         textColor = GetSysColor(COLOR_CAPTIONTEXT);
134     }
135     else 
136     {
137         if( wnd->dwStyle & WS_CHILD ) 
138         { 
139             hBrush = (HBRUSH) GetClassLongA(wnd->hwndSelf, GCL_HBRBACKGROUND);
140             if( hBrush )
141             {
142                 INT level;
143                 LOGBRUSH logBrush;
144                 GetObjectA( hBrush, sizeof(logBrush), &logBrush );
145                 level = GetRValue(logBrush.lbColor) +
146                            GetGValue(logBrush.lbColor) +
147                               GetBValue(logBrush.lbColor);
148                 if( level < (0x7F * 3) )
149                     textColor = RGB( 0xFF, 0xFF, 0xFF );
150             }
151             else
152                 hBrush = GetStockObject( WHITE_BRUSH );
153         }
154         else
155         {
156             hBrush = GetStockObject( BLACK_BRUSH );
157             textColor = RGB( 0xFF, 0xFF, 0xFF );    
158         }
159     }
160
161     FillWindow16( wnd->parent->hwndSelf, wnd->hwndSelf, hDC, hBrush );
162
163     hPrevFont = SelectObject( hDC, hIconTitleFont );
164     if( hPrevFont )
165     {
166         RECT  rect;
167         INT     length;
168         char    buffer[80];
169
170         rect.left = rect.top = 0;
171         rect.right = wnd->rectWindow.right - wnd->rectWindow.left;
172         rect.bottom = wnd->rectWindow.bottom - wnd->rectWindow.top;
173
174         length = GetWindowTextA( wnd->owner->hwndSelf, buffer, 80 );
175         SetTextColor( hDC, textColor );
176         SetBkMode( hDC, TRANSPARENT );
177         
178         DrawTextA( hDC, buffer, length, &rect, DT_CENTER | DT_NOPREFIX |
179                      DT_WORDBREAK | ((bMultiLineTitle) ? 0 : DT_SINGLELINE) ); 
180
181         SelectObject( hDC, hPrevFont );
182     }
183     return ( hPrevFont ) ? TRUE : FALSE;
184 }
185
186 /***********************************************************************
187  *           IconTitleWndProc
188  */
189 LRESULT WINAPI IconTitleWndProc( HWND hWnd, UINT msg,
190                                  WPARAM wParam, LPARAM lParam )
191 {
192     LRESULT retvalue;
193     WND *wnd = WIN_FindWndPtr( hWnd );
194
195     switch( msg )
196     {
197         case WM_NCHITTEST:
198              retvalue = HTCAPTION;
199              goto END;
200         case WM_NCMOUSEMOVE:
201         case WM_NCLBUTTONDBLCLK:
202              retvalue = SendMessageA( wnd->owner->hwndSelf, msg, wParam, lParam );      
203              goto END;
204         case WM_ACTIVATE:
205              if( wParam ) SetActiveWindow( wnd->owner->hwndSelf );
206              /* fall through */
207
208         case WM_CLOSE:
209              retvalue = 0;
210              goto END;
211         case WM_SHOWWINDOW:
212              if( wnd && wParam )
213              {
214                  RECT titleRect;
215
216                  ICONTITLE_GetTitlePos( wnd, &titleRect );
217                  if( wnd->owner->next != wnd )  /* keep icon title behind the owner */
218                      SetWindowPos( hWnd, wnd->owner->hwndSelf, 
219                                      titleRect.left, titleRect.top,
220                                      titleRect.right, titleRect.bottom, SWP_NOACTIVATE );
221                  else
222                      SetWindowPos( hWnd, 0, titleRect.left, titleRect.top,
223                                      titleRect.right, titleRect.bottom, 
224                                      SWP_NOACTIVATE | SWP_NOZORDER );
225              }
226              retvalue = 0;
227              goto END;
228         case WM_ERASEBKGND:
229              if( wnd )
230              {
231                  WND* iconWnd = WIN_LockWndPtr(wnd->owner);
232
233                  if( iconWnd->dwStyle & WS_CHILD )
234                      lParam = SendMessageA( iconWnd->hwndSelf, WM_ISACTIVEICON, 0, 0 );
235                  else
236                      lParam = (iconWnd->hwndSelf == GetActiveWindow16());
237
238                  WIN_ReleaseWndPtr(iconWnd);
239                  if( ICONTITLE_Paint( wnd, (HDC)wParam, (BOOL)lParam ) )
240                      ValidateRect( hWnd, NULL );
241                  retvalue = 1;
242                  goto END;
243              }
244     }
245
246     retvalue = DefWindowProcA( hWnd, msg, wParam, lParam );
247 END:
248     WIN_ReleaseWndPtr(wnd);
249     return retvalue;
250 }
251
252