Convert GetCharWidth to Unicode.
[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     HWND hWnd;
47     HINSTANCE instance = GetWindowLongA( owner, GWL_HINSTANCE );
48     LONG style = WS_CLIPSIBLINGS;
49
50     if (!IsWindowEnabled(owner)) style |= WS_DISABLED;
51     if( GetWindowLongA( owner, GWL_STYLE ) & WS_CHILD )
52         hWnd = CreateWindowExA( 0, ICONTITLE_CLASS_ATOM, NULL,
53                                 style | WS_CHILD, 0, 0, 1, 1,
54                                 GetParent(owner), 0, instance, NULL );
55     else
56         hWnd = CreateWindowExA( 0, ICONTITLE_CLASS_ATOM, NULL,
57                                 style, 0, 0, 1, 1,
58                                 owner, 0, instance, NULL );
59     WIN_SetOwner( hWnd, owner );  /* MDI depends on this */
60     SetWindowLongW( hWnd, GWL_STYLE,
61                     GetWindowLongW( hWnd, GWL_STYLE ) & ~(WS_CAPTION | WS_BORDER) );
62     return hWnd;
63 }
64
65 /***********************************************************************
66  *           ICONTITLE_SetTitlePos
67  */
68 static BOOL ICONTITLE_SetTitlePos( HWND hwnd, HWND owner )
69 {
70     static WCHAR emptyTitleText[] = {'<','.','.','.','>',0};
71     WCHAR str[80];
72     HDC hDC;
73     HFONT hPrevFont;
74     RECT rect;
75     INT cx, cy;
76     POINT pt;
77
78     int length = GetWindowTextW( owner, str, sizeof(str)/sizeof(WCHAR) );
79
80     while (length && str[length - 1] == ' ') /* remove trailing spaces */
81         str[--length] = 0;
82
83     if( !length )
84     {
85         strcpyW( str, emptyTitleText );
86         length = strlenW( str );
87     }
88
89     if (!(hDC = GetDC( hwnd ))) return FALSE;
90
91     hPrevFont = SelectObject( hDC, hIconTitleFont );
92
93     SetRect( &rect, 0, 0, GetSystemMetrics(SM_CXICONSPACING) -
94              GetSystemMetrics(SM_CXBORDER) * 2,
95              GetSystemMetrics(SM_CYBORDER) * 2 );
96
97     DrawTextW( hDC, str, length, &rect, DT_CALCRECT | DT_CENTER | DT_NOPREFIX | DT_WORDBREAK |
98                (( bMultiLineTitle ) ? 0 : DT_SINGLELINE) );
99
100     SelectObject( hDC, hPrevFont );
101     ReleaseDC( hwnd, hDC );
102
103     cx = rect.right - rect.left +  4 * GetSystemMetrics(SM_CXBORDER);
104     cy = rect.bottom - rect.top;
105
106     pt.x = (GetSystemMetrics(SM_CXICON) - cx) / 2;
107     pt.y = GetSystemMetrics(SM_CYICON);
108
109     /* point is relative to owner, make it relative to parent */
110     MapWindowPoints( owner, GetParent(hwnd), &pt, 1 );
111
112     SetWindowPos( hwnd, owner, pt.x, pt.y, cx, cy, SWP_NOACTIVATE );
113     return TRUE;
114 }
115
116 /***********************************************************************
117  *           ICONTITLE_Paint
118  */
119 static BOOL ICONTITLE_Paint( HWND hwnd, HWND owner, HDC hDC, BOOL bActive )
120 {
121     RECT rect;
122     HFONT hPrevFont;
123     HBRUSH hBrush = 0;
124     COLORREF textColor = 0;
125
126     if( bActive )
127     {
128         hBrush = GetSysColorBrush(COLOR_ACTIVECAPTION);
129         textColor = GetSysColor(COLOR_CAPTIONTEXT);
130     }
131     else 
132     {
133         if( GetWindowLongA( hwnd, GWL_STYLE ) & WS_CHILD )
134         { 
135             hBrush = (HBRUSH) GetClassLongA(hwnd, GCL_HBRBACKGROUND);
136             if( hBrush )
137             {
138                 INT level;
139                 LOGBRUSH logBrush;
140                 GetObjectA( hBrush, sizeof(logBrush), &logBrush );
141                 level = GetRValue(logBrush.lbColor) +
142                            GetGValue(logBrush.lbColor) +
143                               GetBValue(logBrush.lbColor);
144                 if( level < (0x7F * 3) )
145                     textColor = RGB( 0xFF, 0xFF, 0xFF );
146             }
147             else
148                 hBrush = GetStockObject( WHITE_BRUSH );
149         }
150         else
151         {
152             hBrush = GetStockObject( BLACK_BRUSH );
153             textColor = RGB( 0xFF, 0xFF, 0xFF );    
154         }
155     }
156
157     GetClientRect( hwnd, &rect );
158     DPtoLP( hDC, (LPPOINT)&rect, 2 );
159     FillRect( hDC, &rect, hBrush );
160
161     hPrevFont = SelectObject( hDC, hIconTitleFont );
162     if( hPrevFont )
163     {
164         WCHAR buffer[80];
165
166         INT length = GetWindowTextW( owner, buffer, sizeof(buffer) );
167         SetTextColor( hDC, textColor );
168         SetBkMode( hDC, TRANSPARENT );
169
170         DrawTextW( hDC, buffer, length, &rect, DT_CENTER | DT_NOPREFIX |
171                    DT_WORDBREAK | ((bMultiLineTitle) ? 0 : DT_SINGLELINE) );
172
173         SelectObject( hDC, hPrevFont );
174     }
175     return (hPrevFont != 0);
176 }
177
178 /***********************************************************************
179  *           IconTitleWndProc
180  */
181 LRESULT WINAPI IconTitleWndProc( HWND hWnd, UINT msg,
182                                  WPARAM wParam, LPARAM lParam )
183 {
184     HWND owner = GetWindow( hWnd, GW_OWNER );
185
186     if (!IsWindow(hWnd)) return 0;
187
188     switch( msg )
189     {
190         case WM_CREATE:
191             if (!hIconTitleFont)
192             {
193                 LOGFONTA logFont;
194                 SystemParametersInfoA( SPI_GETICONTITLELOGFONT, 0, &logFont, 0 );
195                 SystemParametersInfoA( SPI_GETICONTITLEWRAP, 0, &bMultiLineTitle, 0 );
196                 hIconTitleFont = CreateFontIndirectA( &logFont );
197             }
198             return (hIconTitleFont ? 0 : -1);
199         case WM_NCHITTEST:
200              return HTCAPTION;
201         case WM_NCMOUSEMOVE:
202         case WM_NCLBUTTONDBLCLK:
203              return SendMessageW( owner, msg, wParam, lParam );
204         case WM_ACTIVATE:
205              if( wParam ) SetActiveWindow( owner );
206              return 0;
207         case WM_CLOSE:
208              return 0;
209         case WM_SHOWWINDOW:
210             if (wParam) ICONTITLE_SetTitlePos( hWnd, owner );
211              return 0;
212         case WM_ERASEBKGND:
213             if( GetWindowLongA( owner, GWL_STYLE ) & WS_CHILD )
214                 lParam = SendMessageA( owner, WM_ISACTIVEICON, 0, 0 );
215             else
216                 lParam = (owner == GetActiveWindow());
217             if( ICONTITLE_Paint( hWnd, owner, (HDC)wParam, (BOOL)lParam ) )
218                 ValidateRect( hWnd, NULL );
219             return 1;
220     }
221     return DefWindowProcW( hWnd, msg, wParam, lParam );
222 }
223
224