Fix the return value of WM_GETTEXT in the edit control.
[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
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"
20
21 static BOOL bMultiLineTitle;
22 static HFONT hIconTitleFont;
23
24 static LRESULT WINAPI IconTitleWndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam );
25
26 /*********************************************************************
27  * icon title class descriptor
28  */
29 const struct builtin_class_descr ICONTITLE_builtin_class =
30 {
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 */
38 };
39
40
41
42 /***********************************************************************
43  *           ICONTITLE_Create
44  */
45 HWND ICONTITLE_Create( WND* wnd )
46 {
47     WND* wndPtr;
48     HWND hWnd;
49
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 )
60     {
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;
66     }
67     return 0;
68 }
69
70 /***********************************************************************
71  *           ICONTITLE_GetTitlePos
72  */
73 static BOOL ICONTITLE_GetTitlePos( WND* wnd, LPRECT lpRect )
74 {
75     static WCHAR emptyTitleText[] = {'<','.','.','.','>',0};
76     LPWSTR str = NULL;
77     int length = lstrlenW( wnd->owner->text );
78
79     if( length )
80     {
81         str = HeapAlloc( GetProcessHeap(), 0, (length + 1) * sizeof(WCHAR) );
82         strcpyW( str, wnd->owner->text );
83         while( str[length - 1] == ' ' ) /* remove trailing spaces */
84         { 
85             str[--length] = '\0';
86             if( !length )
87             {
88                 HeapFree( GetProcessHeap(), 0, str );
89                 break;
90             }
91         }
92     }
93     if( !length ) 
94     {
95         str = emptyTitleText;
96         length = lstrlenW( str );
97     }
98
99     if( str )
100     {
101         HDC hDC = GetDC( wnd->hwndSelf );
102         if( hDC )
103         {
104             HFONT hPrevFont = SelectObject( hDC, hIconTitleFont );
105
106             SetRect( lpRect, 0, 0, GetSystemMetrics(SM_CXICONSPACING) -
107                        GetSystemMetrics(SM_CXBORDER) * 2,
108                        GetSystemMetrics(SM_CYBORDER) * 2 );
109
110             DrawTextW( hDC, str, length, lpRect, DT_CALCRECT |
111                          DT_CENTER | DT_NOPREFIX | DT_WORDBREAK |
112                          (( bMultiLineTitle ) ? 0 : DT_SINGLELINE) );
113
114             SelectObject( hDC, hPrevFont );
115             ReleaseDC( wnd->hwndSelf, hDC );
116
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);
122         }
123         if( str != emptyTitleText ) HeapFree( GetProcessHeap(), 0, str );
124         return ( hDC ) ? TRUE : FALSE;
125     }
126     return FALSE;
127 }
128
129 /***********************************************************************
130  *           ICONTITLE_Paint
131  */
132 static BOOL ICONTITLE_Paint( WND* wnd, HDC hDC, BOOL bActive )
133 {
134     HFONT hPrevFont;
135     HBRUSH hBrush = 0;
136     COLORREF textColor = 0;
137
138     if( bActive )
139     {
140         hBrush = GetSysColorBrush(COLOR_ACTIVECAPTION);
141         textColor = GetSysColor(COLOR_CAPTIONTEXT);
142     }
143     else 
144     {
145         if( wnd->dwStyle & WS_CHILD ) 
146         { 
147             hBrush = (HBRUSH) GetClassLongA(wnd->hwndSelf, GCL_HBRBACKGROUND);
148             if( hBrush )
149             {
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 );
158             }
159             else
160                 hBrush = GetStockObject( WHITE_BRUSH );
161         }
162         else
163         {
164             hBrush = GetStockObject( BLACK_BRUSH );
165             textColor = RGB( 0xFF, 0xFF, 0xFF );    
166         }
167     }
168
169     FillWindow16( wnd->parent->hwndSelf, wnd->hwndSelf, hDC, hBrush );
170
171     hPrevFont = SelectObject( hDC, hIconTitleFont );
172     if( hPrevFont )
173     {
174         RECT  rect;
175         INT     length;
176         char    buffer[80];
177
178         rect.left = rect.top = 0;
179         rect.right = wnd->rectWindow.right - wnd->rectWindow.left;
180         rect.bottom = wnd->rectWindow.bottom - wnd->rectWindow.top;
181
182         length = GetWindowTextA( wnd->owner->hwndSelf, buffer, 80 );
183         SetTextColor( hDC, textColor );
184         SetBkMode( hDC, TRANSPARENT );
185         
186         DrawTextA( hDC, buffer, length, &rect, DT_CENTER | DT_NOPREFIX |
187                      DT_WORDBREAK | ((bMultiLineTitle) ? 0 : DT_SINGLELINE) ); 
188
189         SelectObject( hDC, hPrevFont );
190     }
191     return ( hPrevFont ) ? TRUE : FALSE;
192 }
193
194 /***********************************************************************
195  *           IconTitleWndProc
196  */
197 LRESULT WINAPI IconTitleWndProc( HWND hWnd, UINT msg,
198                                  WPARAM wParam, LPARAM lParam )
199 {
200     LRESULT retvalue;
201     WND *wnd = WIN_FindWndPtr( hWnd );
202
203     switch( msg )
204     {
205         case WM_CREATE:
206             if (!hIconTitleFont)
207             {
208                 LOGFONTA logFont;
209                 SystemParametersInfoA( SPI_GETICONTITLELOGFONT, 0, &logFont, 0 );
210                 SystemParametersInfoA( SPI_GETICONTITLEWRAP, 0, &bMultiLineTitle, 0 );
211                 hIconTitleFont = CreateFontIndirectA( &logFont );
212             }
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 */
225
226         case WM_CLOSE:
227              retvalue = 0;
228              goto END;
229         case WM_SHOWWINDOW:
230              if( wnd && wParam )
231              {
232                  RECT titleRect;
233
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 );
243              }
244              retvalue = 0;
245              goto END;
246         case WM_ERASEBKGND:
247              if( wnd )
248              {
249                  WND* iconWnd = WIN_LockWndPtr(wnd->owner);
250
251                  if( iconWnd->dwStyle & WS_CHILD )
252                      lParam = SendMessageA( iconWnd->hwndSelf, WM_ISACTIVEICON, 0, 0 );
253                  else
254                      lParam = (iconWnd->hwndSelf == GetActiveWindow16());
255
256                  WIN_ReleaseWndPtr(iconWnd);
257                  if( ICONTITLE_Paint( wnd, (HDC)wParam, (BOOL)lParam ) )
258                      ValidateRect( hWnd, NULL );
259                  retvalue = 1;
260                  goto END;
261              }
262     }
263
264     retvalue = DefWindowProcW( hWnd, msg, wParam, lParam );
265 END:
266     WIN_ReleaseWndPtr(wnd);
267     return retvalue;
268 }
269
270