Added WIN_ListParents function and renamed WIN_BuildWinArray into
[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
20 static BOOL bMultiLineTitle;
21 static HFONT hIconTitleFont;
22
23 static LRESULT WINAPI IconTitleWndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam );
24
25 /*********************************************************************
26  * icon title class descriptor
27  */
28 const struct builtin_class_descr ICONTITLE_builtin_class =
29 {
30     ICONTITLE_CLASS_ATOM, /* name */
31     CS_GLOBALCLASS,       /* style */
32     NULL,                 /* procA (winproc is Unicode only) */
33     IconTitleWndProc,     /* procW */
34     0,                    /* extra */
35     IDC_ARROWA,           /* cursor */
36     0                     /* brush */
37 };
38
39
40
41 /***********************************************************************
42  *           ICONTITLE_Create
43  */
44 HWND ICONTITLE_Create( HWND owner )
45 {
46     WND* wndPtr;
47     HWND hWnd;
48     HINSTANCE instance = GetWindowLongA( owner, GWL_HINSTANCE );
49
50     if( GetWindowLongA( owner, GWL_STYLE ) & WS_CHILD )
51         hWnd = CreateWindowExA( 0, ICONTITLE_CLASS_ATOM, NULL,
52                                   WS_CHILD | WS_CLIPSIBLINGS, 0, 0, 1, 1,
53                                   GetParent(owner), 0, instance, NULL );
54     else
55         hWnd = CreateWindowExA( 0, ICONTITLE_CLASS_ATOM, NULL,
56                                   WS_CLIPSIBLINGS, 0, 0, 1, 1,
57                                   owner, 0, instance, NULL );
58     wndPtr = WIN_FindWndPtr( hWnd );
59     if( wndPtr )
60     {
61         wndPtr->owner = owner; /* MDI depends on this */
62         wndPtr->dwStyle &= ~(WS_CAPTION | WS_BORDER);
63         if (!IsWindowEnabled(owner)) wndPtr->dwStyle |= WS_DISABLED;
64         WIN_ReleaseWndPtr(wndPtr);
65         return hWnd;
66     }
67     return 0;
68 }
69
70 /***********************************************************************
71  *           ICONTITLE_SetTitlePos
72  */
73 static BOOL ICONTITLE_SetTitlePos( HWND hwnd, HWND owner )
74 {
75     static WCHAR emptyTitleText[] = {'<','.','.','.','>',0};
76     WCHAR str[80];
77     HDC hDC;
78     HFONT hPrevFont;
79     RECT rect;
80     INT cx, cy;
81     POINT pt;
82
83     int length = GetWindowTextW( owner, str, sizeof(str)/sizeof(WCHAR) );
84
85     while (length && str[length - 1] == ' ') /* remove trailing spaces */
86         str[--length] = 0;
87
88     if( !length )
89     {
90         strcpyW( str, emptyTitleText );
91         length = strlenW( str );
92     }
93
94     if (!(hDC = GetDC( hwnd ))) return FALSE;
95
96     hPrevFont = SelectObject( hDC, hIconTitleFont );
97
98     SetRect( &rect, 0, 0, GetSystemMetrics(SM_CXICONSPACING) -
99              GetSystemMetrics(SM_CXBORDER) * 2,
100              GetSystemMetrics(SM_CYBORDER) * 2 );
101
102     DrawTextW( hDC, str, length, &rect, DT_CALCRECT | DT_CENTER | DT_NOPREFIX | DT_WORDBREAK |
103                (( bMultiLineTitle ) ? 0 : DT_SINGLELINE) );
104
105     SelectObject( hDC, hPrevFont );
106     ReleaseDC( hwnd, hDC );
107
108     cx = rect.right - rect.left +  4 * GetSystemMetrics(SM_CXBORDER);
109     cy = rect.bottom - rect.top;
110
111     pt.x = (GetSystemMetrics(SM_CXICON) - cx) / 2;
112     pt.y = GetSystemMetrics(SM_CYICON);
113
114     /* point is relative to owner, make it relative to parent */
115     MapWindowPoints( owner, GetParent(hwnd), &pt, 1 );
116
117     SetWindowPos( hwnd, owner, pt.x, pt.y, cx, cy, SWP_NOACTIVATE );
118     return TRUE;
119 }
120
121 /***********************************************************************
122  *           ICONTITLE_Paint
123  */
124 static BOOL ICONTITLE_Paint( HWND hwnd, HWND owner, 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( GetWindowLongA( hwnd, GWL_STYLE ) & WS_CHILD )
138         { 
139             hBrush = (HBRUSH) GetClassLongA(hwnd, 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( GetParent(hwnd), hwnd, hDC, hBrush );
162
163     hPrevFont = SelectObject( hDC, hIconTitleFont );
164     if( hPrevFont )
165     {
166         RECT  rect;
167         INT     length;
168         WCHAR buffer[80];
169
170         GetClientRect( hwnd, &rect );
171
172         length = GetWindowTextW( owner, buffer, 80 );
173         SetTextColor( hDC, textColor );
174         SetBkMode( hDC, TRANSPARENT );
175
176         DrawTextW( hDC, buffer, length, &rect, DT_CENTER | DT_NOPREFIX |
177                    DT_WORDBREAK | ((bMultiLineTitle) ? 0 : DT_SINGLELINE) );
178
179         SelectObject( hDC, hPrevFont );
180     }
181     return ( hPrevFont ) ? TRUE : FALSE;
182 }
183
184 /***********************************************************************
185  *           IconTitleWndProc
186  */
187 LRESULT WINAPI IconTitleWndProc( HWND hWnd, UINT msg,
188                                  WPARAM wParam, LPARAM lParam )
189 {
190     LRESULT retvalue;
191     HWND owner = GetWindow( hWnd, GW_OWNER );
192     WND *wnd = WIN_FindWndPtr( hWnd );
193
194     if( !wnd )
195       return 0;
196
197     switch( msg )
198     {
199         case WM_CREATE:
200             if (!hIconTitleFont)
201             {
202                 LOGFONTA logFont;
203                 SystemParametersInfoA( SPI_GETICONTITLELOGFONT, 0, &logFont, 0 );
204                 SystemParametersInfoA( SPI_GETICONTITLEWRAP, 0, &bMultiLineTitle, 0 );
205                 hIconTitleFont = CreateFontIndirectA( &logFont );
206             }
207             retvalue = (hIconTitleFont) ? 0 : -1;
208             goto END;
209         case WM_NCHITTEST:
210              retvalue = HTCAPTION;
211              goto END;
212         case WM_NCMOUSEMOVE:
213         case WM_NCLBUTTONDBLCLK:
214              retvalue = SendMessageW( owner, msg, wParam, lParam );
215              goto END;
216         case WM_ACTIVATE:
217              if( wParam ) SetActiveWindow( owner );
218              /* fall through */
219
220         case WM_CLOSE:
221              retvalue = 0;
222              goto END;
223         case WM_SHOWWINDOW:
224              if( wnd && wParam ) ICONTITLE_SetTitlePos( hWnd, owner );
225              retvalue = 0;
226              goto END;
227         case WM_ERASEBKGND:
228              if( wnd )
229              {
230                  if( GetWindowLongA( owner, GWL_STYLE ) & WS_CHILD )
231                      lParam = SendMessageA( owner, WM_ISACTIVEICON, 0, 0 );
232                  else
233                      lParam = (owner == GetActiveWindow());
234                  if( ICONTITLE_Paint( hWnd, owner, (HDC)wParam, (BOOL)lParam ) )
235                      ValidateRect( hWnd, NULL );
236                  retvalue = 1;
237                  goto END;
238              }
239     }
240
241     retvalue = DefWindowProcW( hWnd, msg, wParam, lParam );
242 END:
243     WIN_ReleaseWndPtr(wnd);
244     return retvalue;
245 }
246
247