oleaut32: Add a test for loading/saving an empty picture.
[wine] / dlls / user32 / icontitle.c
1 /*
2  * Icontitle window class.
3  *
4  * Copyright 1997 Alex Korobka
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <string.h>
26 #ifdef HAVE_UNISTD_H
27 # include <unistd.h>
28 #endif
29
30 #include "windef.h"
31 #include "winbase.h"
32 #include "wingdi.h"
33 #include "winuser.h"
34 #include "wine/unicode.h"
35 #include "controls.h"
36 #include "win.h"
37
38 static BOOL bMultiLineTitle;
39 static HFONT hIconTitleFont;
40
41 /*********************************************************************
42  * icon title class descriptor
43  */
44 const struct builtin_class_descr ICONTITLE_builtin_class =
45 {
46     (LPCWSTR)ICONTITLE_CLASS_ATOM, /* name */
47     0,                    /* style */
48     WINPROC_ICONTITLE,    /* proc */
49     0,                    /* extra */
50     IDC_ARROW,            /* cursor */
51     0                     /* brush */
52 };
53
54
55
56 /***********************************************************************
57  *           ICONTITLE_Create
58  */
59 HWND ICONTITLE_Create( HWND owner )
60 {
61     HWND hWnd;
62     HINSTANCE instance = (HINSTANCE)GetWindowLongPtrA( owner, GWLP_HINSTANCE );
63     LONG style = WS_CLIPSIBLINGS;
64
65     if (!IsWindowEnabled(owner)) style |= WS_DISABLED;
66     if( GetWindowLongA( owner, GWL_STYLE ) & WS_CHILD )
67         hWnd = CreateWindowExA( 0, (LPCSTR)ICONTITLE_CLASS_ATOM, NULL,
68                                 style | WS_CHILD, 0, 0, 1, 1,
69                                 GetParent(owner), 0, instance, NULL );
70     else
71         hWnd = CreateWindowExA( 0, (LPCSTR)ICONTITLE_CLASS_ATOM, NULL,
72                                 style, 0, 0, 1, 1,
73                                 owner, 0, instance, NULL );
74     WIN_SetOwner( hWnd, owner );  /* MDI depends on this */
75     SetWindowLongW( hWnd, GWL_STYLE,
76                     GetWindowLongW( hWnd, GWL_STYLE ) & ~(WS_CAPTION | WS_BORDER) );
77     return hWnd;
78 }
79
80 /***********************************************************************
81  *           ICONTITLE_SetTitlePos
82  */
83 static BOOL ICONTITLE_SetTitlePos( HWND hwnd, HWND owner )
84 {
85     static const WCHAR emptyTitleText[] = {'<','.','.','.','>',0};
86     WCHAR str[80];
87     HDC hDC;
88     HFONT hPrevFont;
89     RECT rect;
90     INT cx, cy;
91     POINT pt;
92
93     int length = GetWindowTextW( owner, str, sizeof(str)/sizeof(WCHAR) );
94
95     while (length && str[length - 1] == ' ') /* remove trailing spaces */
96         str[--length] = 0;
97
98     if( !length )
99     {
100         strcpyW( str, emptyTitleText );
101         length = strlenW( str );
102     }
103
104     if (!(hDC = GetDC( hwnd ))) return FALSE;
105
106     hPrevFont = SelectObject( hDC, hIconTitleFont );
107
108     SetRect( &rect, 0, 0, GetSystemMetrics(SM_CXICONSPACING) -
109              GetSystemMetrics(SM_CXBORDER) * 2,
110              GetSystemMetrics(SM_CYBORDER) * 2 );
111
112     DrawTextW( hDC, str, length, &rect, DT_CALCRECT | DT_CENTER | DT_NOPREFIX | DT_WORDBREAK |
113                (( bMultiLineTitle ) ? 0 : DT_SINGLELINE) );
114
115     SelectObject( hDC, hPrevFont );
116     ReleaseDC( hwnd, hDC );
117
118     cx = rect.right - rect.left +  4 * GetSystemMetrics(SM_CXBORDER);
119     cy = rect.bottom - rect.top;
120
121     pt.x = (GetSystemMetrics(SM_CXICON) - cx) / 2;
122     pt.y = GetSystemMetrics(SM_CYICON);
123
124     /* point is relative to owner, make it relative to parent */
125     MapWindowPoints( owner, GetParent(hwnd), &pt, 1 );
126
127     SetWindowPos( hwnd, owner, pt.x, pt.y, cx, cy, SWP_NOACTIVATE );
128     return TRUE;
129 }
130
131 /***********************************************************************
132  *           ICONTITLE_Paint
133  */
134 static BOOL ICONTITLE_Paint( HWND hwnd, HWND owner, HDC hDC, BOOL bActive )
135 {
136     RECT rect;
137     HFONT hPrevFont;
138     HBRUSH hBrush = 0;
139     COLORREF textColor = 0;
140
141     if( bActive )
142     {
143         hBrush = GetSysColorBrush(COLOR_ACTIVECAPTION);
144         textColor = GetSysColor(COLOR_CAPTIONTEXT);
145     }
146     else
147     {
148         if( GetWindowLongA( hwnd, GWL_STYLE ) & WS_CHILD )
149         {
150             hBrush = (HBRUSH) GetClassLongPtrW(hwnd, GCLP_HBRBACKGROUND);
151             if( hBrush )
152             {
153                 INT level;
154                 LOGBRUSH logBrush;
155                 GetObjectA( hBrush, sizeof(logBrush), &logBrush );
156                 level = GetRValue(logBrush.lbColor) +
157                            GetGValue(logBrush.lbColor) +
158                               GetBValue(logBrush.lbColor);
159                 if( level < (0x7F * 3) )
160                     textColor = RGB( 0xFF, 0xFF, 0xFF );
161             }
162             else
163                 hBrush = GetStockObject( WHITE_BRUSH );
164         }
165         else
166         {
167             hBrush = GetStockObject( BLACK_BRUSH );
168             textColor = RGB( 0xFF, 0xFF, 0xFF );
169         }
170     }
171
172     GetClientRect( hwnd, &rect );
173     DPtoLP( hDC, (LPPOINT)&rect, 2 );
174     FillRect( hDC, &rect, hBrush );
175
176     hPrevFont = SelectObject( hDC, hIconTitleFont );
177     if( hPrevFont )
178     {
179         WCHAR buffer[80];
180
181         INT length = GetWindowTextW( owner, buffer, sizeof(buffer)/sizeof(buffer[0]) );
182         SetTextColor( hDC, textColor );
183         SetBkMode( hDC, TRANSPARENT );
184
185         DrawTextW( hDC, buffer, length, &rect, DT_CENTER | DT_NOPREFIX |
186                    DT_WORDBREAK | ((bMultiLineTitle) ? 0 : DT_SINGLELINE) );
187
188         SelectObject( hDC, hPrevFont );
189     }
190     return (hPrevFont != 0);
191 }
192
193 /***********************************************************************
194  *           IconTitleWndProc
195  */
196 LRESULT WINAPI IconTitleWndProc( HWND hWnd, UINT msg,
197                                  WPARAM wParam, LPARAM lParam )
198 {
199     HWND owner = GetWindow( hWnd, GW_OWNER );
200
201     if (!IsWindow(hWnd)) return 0;
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             return (hIconTitleFont ? 0 : -1);
214         case WM_NCHITTEST:
215              return HTCAPTION;
216         case WM_NCMOUSEMOVE:
217         case WM_NCLBUTTONDBLCLK:
218              return SendMessageW( owner, msg, wParam, lParam );
219         case WM_ACTIVATE:
220              if( wParam ) SetActiveWindow( owner );
221              return 0;
222         case WM_CLOSE:
223              return 0;
224         case WM_SHOWWINDOW:
225             if (wParam) ICONTITLE_SetTitlePos( hWnd, owner );
226              return 0;
227         case WM_ERASEBKGND:
228             if( GetWindowLongW( owner, GWL_STYLE ) & WS_CHILD )
229                 lParam = SendMessageW( owner, WM_ISACTIVEICON, 0, 0 );
230             else
231                 lParam = (owner == GetActiveWindow());
232             if( ICONTITLE_Paint( hWnd, owner, (HDC)wParam, (BOOL)lParam ) )
233                 ValidateRect( hWnd, NULL );
234             return 1;
235     }
236     return DefWindowProcW( hWnd, msg, wParam, lParam );
237 }