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