Release 980822
[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 "windows.h"
11 #include "sysmetrics.h"
12 #include "win.h"
13 #include "desktop.h"
14 #include "graphics.h"
15 #include "heap.h"
16
17 static  LPCSTR  emptyTitleText = "<...>";
18
19         BOOL32  bMultiLineTitle;
20         HFONT32 hIconTitleFont;
21
22 /***********************************************************************
23  *           ICONTITLE_Init
24  */
25 BOOL32 ICONTITLE_Init(void)
26 {
27     LOGFONT32A logFont;
28
29     SystemParametersInfo32A( SPI_GETICONTITLELOGFONT, 0, &logFont, 0 );
30     SystemParametersInfo32A( SPI_GETICONTITLEWRAP, 0, &bMultiLineTitle, 0 );
31     hIconTitleFont = CreateFontIndirect32A( &logFont );
32     return (hIconTitleFont) ? TRUE : FALSE;
33 }
34
35 /***********************************************************************
36  *           ICONTITLE_Create
37  */
38 HWND32 ICONTITLE_Create( WND* wnd )
39 {
40     WND* wndPtr;
41     HWND32 hWnd;
42
43     if( wnd->dwStyle & WS_CHILD )
44         hWnd = CreateWindowEx32A( 0, ICONTITLE_CLASS_ATOM, NULL,
45                                   WS_CHILD | WS_CLIPSIBLINGS, 0, 0, 1, 1,
46                                   wnd->parent->hwndSelf, 0, wnd->hInstance, NULL );
47     else
48         hWnd = CreateWindowEx32A( 0, ICONTITLE_CLASS_ATOM, NULL,
49                                   WS_CLIPSIBLINGS, 0, 0, 1, 1,
50                                   wnd->hwndSelf, 0, wnd->hInstance, NULL );
51     wndPtr = WIN_FindWndPtr( hWnd );
52     if( wndPtr )
53     {
54         wndPtr->owner = wnd;    /* MDI depends on this */
55         wndPtr->dwStyle &= ~(WS_CAPTION | WS_BORDER);
56         if( wnd->dwStyle & WS_DISABLED ) wndPtr->dwStyle |= WS_DISABLED;
57         return hWnd;
58     }
59     return 0;
60 }
61
62 /***********************************************************************
63  *           ICONTITLE_GetTitlePos
64  */
65 static BOOL32 ICONTITLE_GetTitlePos( WND* wnd, LPRECT32 lpRect )
66 {
67     LPSTR str;
68     int length = lstrlen32A( wnd->owner->text );
69
70     if( length )
71     {
72         str = HeapAlloc( GetProcessHeap(), 0, length + 1 );
73         lstrcpy32A( str, wnd->owner->text );
74         while( str[length - 1] == ' ' ) /* remove trailing spaces */
75         { 
76             str[--length] = '\0';
77             if( !length )
78             {
79                 HeapFree( GetProcessHeap(), 0, str );
80                 break;
81             }
82         }
83     }
84     if( !length ) 
85     {
86         str = (LPSTR)emptyTitleText;
87         length = lstrlen32A( str );
88     }
89
90     if( str )
91     {
92         HDC32 hDC = GetDC32( wnd->hwndSelf );
93         if( hDC )
94         {
95             HFONT32 hPrevFont = SelectObject32( hDC, hIconTitleFont );
96
97             SetRect32( lpRect, 0, 0, sysMetrics[SM_CXICONSPACING] -
98                        SYSMETRICS_CXBORDER * 2, SYSMETRICS_CYBORDER * 2 );
99
100             DrawText32A( hDC, str, length, lpRect, DT_CALCRECT |
101                          DT_CENTER | DT_NOPREFIX | DT_WORDBREAK |
102                          (( bMultiLineTitle ) ? 0 : DT_SINGLELINE) );
103
104             SelectObject32( hDC, hPrevFont );
105             ReleaseDC32( wnd->hwndSelf, hDC );
106
107             lpRect->right += 4 * SYSMETRICS_CXBORDER - lpRect->left;
108             lpRect->left = wnd->owner->rectWindow.left + SYSMETRICS_CXICON / 2 -
109                                       (lpRect->right - lpRect->left) / 2;
110             lpRect->bottom -= lpRect->top;
111             lpRect->top = wnd->owner->rectWindow.top + SYSMETRICS_CYICON;
112         }
113         if( str != emptyTitleText ) HeapFree( GetProcessHeap(), 0, str );
114         return ( hDC ) ? TRUE : FALSE;
115     }
116     return FALSE;
117 }
118
119 /***********************************************************************
120  *           ICONTITLE_Paint
121  */
122 static BOOL32 ICONTITLE_Paint( WND* wnd, HDC32 hDC, BOOL32 bActive )
123 {
124     HFONT32 hPrevFont;
125     HBRUSH32 hBrush = 0;
126     COLORREF textColor = 0;
127
128     if( bActive )
129     {
130         hBrush = GetSysColorBrush32(COLOR_ACTIVECAPTION);
131         textColor = GetSysColor32(COLOR_CAPTIONTEXT);
132     }
133     else 
134     {
135         if( wnd->dwStyle & WS_CHILD ) 
136         { 
137             hBrush = wnd->parent->class->hbrBackground;
138             if( hBrush )
139             {
140                 INT32 level;
141                 LOGBRUSH32 logBrush;
142                 GetObject32A( hBrush, sizeof(logBrush), &logBrush );
143                 level = GetRValue(logBrush.lbColor) +
144                            GetGValue(logBrush.lbColor) +
145                               GetBValue(logBrush.lbColor);
146                 if( level < (0x7F * 3) )
147                     textColor = RGB( 0xFF, 0xFF, 0xFF );
148             }
149             else
150                 hBrush = GetStockObject32( WHITE_BRUSH );
151         }
152         else
153         {
154             hBrush = GetStockObject32( BLACK_BRUSH );
155             textColor = RGB( 0xFF, 0xFF, 0xFF );    
156         }
157     }
158
159     FillWindow( wnd->parent->hwndSelf, wnd->hwndSelf, hDC, hBrush );
160
161     hPrevFont = SelectObject32( hDC, hIconTitleFont );
162     if( hPrevFont )
163     {
164         RECT32  rect;
165         INT32   length;
166         char    buffer[80];
167
168         rect.left = rect.top = 0;
169         rect.right = wnd->rectWindow.right - wnd->rectWindow.left;
170         rect.bottom = wnd->rectWindow.bottom - wnd->rectWindow.top;
171
172         length = GetWindowText32A( wnd->owner->hwndSelf, buffer, 80 );
173         SetTextColor32( hDC, textColor );
174         SetBkMode32( hDC, TRANSPARENT );
175         
176         DrawText32A( hDC, buffer, length, &rect, DT_CENTER | DT_NOPREFIX |
177                      DT_WORDBREAK | ((bMultiLineTitle) ? 0 : DT_SINGLELINE) ); 
178
179         SelectObject32( hDC, hPrevFont );
180     }
181     return ( hPrevFont ) ? TRUE : FALSE;
182 }
183
184 /***********************************************************************
185  *           IconTitleWndProc
186  */
187 LRESULT WINAPI IconTitleWndProc( HWND32 hWnd, UINT32 msg,
188                                  WPARAM32 wParam, LPARAM lParam )
189 {
190     WND *wnd = WIN_FindWndPtr( hWnd );
191
192     switch( msg )
193     {
194         case WM_NCHITTEST:
195              return HTCAPTION;
196
197         case WM_NCMOUSEMOVE:
198         case WM_NCLBUTTONDBLCLK:
199              return SendMessage32A( wnd->owner->hwndSelf, msg, wParam, lParam );        
200
201         case WM_ACTIVATE:
202              if( wParam ) SetActiveWindow32( wnd->owner->hwndSelf );
203              /* fall through */
204
205         case WM_CLOSE:
206              return 0;
207
208         case WM_SHOWWINDOW:
209              if( wnd && wParam )
210              {
211                  RECT32 titleRect;
212
213                  ICONTITLE_GetTitlePos( wnd, &titleRect );
214                  if( wnd->owner->next != wnd )  /* keep icon title behind the owner */
215                      SetWindowPos32( hWnd, wnd->owner->hwndSelf, 
216                                      titleRect.left, titleRect.top,
217                                      titleRect.right, titleRect.bottom, SWP_NOACTIVATE );
218                  else
219                      SetWindowPos32( hWnd, 0, titleRect.left, titleRect.top,
220                                      titleRect.right, titleRect.bottom, 
221                                      SWP_NOACTIVATE | SWP_NOZORDER );
222              }
223              return 0;
224
225         case WM_ERASEBKGND:
226              if( wnd )
227              {
228                  WND* iconWnd = wnd->owner;
229
230                  if( iconWnd->dwStyle & WS_CHILD )
231                      lParam = SendMessage32A( iconWnd->hwndSelf, WM_ISACTIVEICON, 0, 0 );
232                  else
233                      lParam = (iconWnd->hwndSelf == GetActiveWindow16());
234
235                  if( ICONTITLE_Paint( wnd, (HDC32)wParam, (BOOL32)lParam ) )
236                      ValidateRect32( hWnd, NULL );
237                  return 1;
238              }
239     }
240
241     return DefWindowProc32A( hWnd, msg, wParam, lParam );
242 }
243
244