Correct behaviour of DEL key back to deleting the current character.
[wine.git] / controls / static.c
blobd08979a4421446a6304b0fe5fb188a964af24fea
1 /*
2 * Static control
4 * Copyright David W. Metcalfe, 1993
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "windef.h"
22 #include "wingdi.h"
23 #include "wine/winuser16.h"
24 #include "cursoricon.h"
25 #include "controls.h"
26 #include "user.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(static);
31 static void STATIC_PaintOwnerDrawfn( HWND hwnd, HDC hdc, DWORD style );
32 static void STATIC_PaintTextfn( HWND hwnd, HDC hdc, DWORD style );
33 static void STATIC_PaintRectfn( HWND hwnd, HDC hdc, DWORD style );
34 static void STATIC_PaintIconfn( HWND hwnd, HDC hdc, DWORD style );
35 static void STATIC_PaintBitmapfn( HWND hwnd, HDC hdc, DWORD style );
36 static void STATIC_PaintEtchedfn( HWND hwnd, HDC hdc, DWORD style );
37 static LRESULT WINAPI StaticWndProcA( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
38 static LRESULT WINAPI StaticWndProcW( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
40 static COLORREF color_windowframe, color_background, color_window;
42 /* offsets for GetWindowLong for static private information */
43 #define HFONT_GWL_OFFSET 0
44 #define HICON_GWL_OFFSET (sizeof(HFONT))
45 #define STATIC_EXTRA_BYTES (HICON_GWL_OFFSET + sizeof(HICON))
47 typedef void (*pfPaint)( HWND hwnd, HDC hdc, DWORD style );
49 static pfPaint staticPaintFunc[SS_TYPEMASK+1] =
51 STATIC_PaintTextfn, /* SS_LEFT */
52 STATIC_PaintTextfn, /* SS_CENTER */
53 STATIC_PaintTextfn, /* SS_RIGHT */
54 STATIC_PaintIconfn, /* SS_ICON */
55 STATIC_PaintRectfn, /* SS_BLACKRECT */
56 STATIC_PaintRectfn, /* SS_GRAYRECT */
57 STATIC_PaintRectfn, /* SS_WHITERECT */
58 STATIC_PaintRectfn, /* SS_BLACKFRAME */
59 STATIC_PaintRectfn, /* SS_GRAYFRAME */
60 STATIC_PaintRectfn, /* SS_WHITEFRAME */
61 NULL, /* Not defined */
62 STATIC_PaintTextfn, /* SS_SIMPLE */
63 STATIC_PaintTextfn, /* SS_LEFTNOWORDWRAP */
64 STATIC_PaintOwnerDrawfn, /* SS_OWNERDRAW */
65 STATIC_PaintBitmapfn, /* SS_BITMAP */
66 NULL, /* SS_ENHMETAFILE */
67 STATIC_PaintEtchedfn, /* SS_ETCHEDHORIZ */
68 STATIC_PaintEtchedfn, /* SS_ETCHEDVERT */
69 STATIC_PaintEtchedfn, /* SS_ETCHEDFRAME */
73 /*********************************************************************
74 * static class descriptor
76 const struct builtin_class_descr STATIC_builtin_class =
78 "Static", /* name */
79 CS_GLOBALCLASS | CS_DBLCLKS | CS_PARENTDC, /* style */
80 StaticWndProcA, /* procA */
81 StaticWndProcW, /* procW */
82 STATIC_EXTRA_BYTES, /* extra */
83 IDC_ARROWA, /* cursor */
84 0 /* brush */
88 /***********************************************************************
89 * STATIC_SetIcon
91 * Set the icon for an SS_ICON control.
93 static HICON STATIC_SetIcon( HWND hwnd, HICON hicon, DWORD style )
95 HICON prevIcon;
96 CURSORICONINFO *info = hicon?(CURSORICONINFO *) GlobalLock16( hicon ):NULL;
98 if ((style & SS_TYPEMASK) != SS_ICON) return 0;
99 if (hicon && !info) {
100 ERR("huh? hicon!=0, but info=0???\n");
101 return 0;
103 prevIcon = SetWindowLongA( hwnd, HICON_GWL_OFFSET, hicon );
104 if (hicon)
106 SetWindowPos( hwnd, 0, 0, 0, info->nWidth, info->nHeight,
107 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER );
108 GlobalUnlock16( hicon );
110 return prevIcon;
113 /***********************************************************************
114 * STATIC_SetBitmap
116 * Set the bitmap for an SS_BITMAP control.
118 static HBITMAP STATIC_SetBitmap( HWND hwnd, HBITMAP hBitmap, DWORD style )
120 HBITMAP hOldBitmap;
122 if ((style & SS_TYPEMASK) != SS_BITMAP) return 0;
123 if (hBitmap && GetObjectType(hBitmap) != OBJ_BITMAP) {
124 ERR("huh? hBitmap!=0, but not bitmap\n");
125 return 0;
127 hOldBitmap = SetWindowLongA( hwnd, HICON_GWL_OFFSET, hBitmap );
128 if (hBitmap)
130 BITMAP bm;
131 GetObjectW(hBitmap, sizeof(bm), &bm);
132 SetWindowPos( hwnd, 0, 0, 0, bm.bmWidth, bm.bmHeight,
133 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER );
135 return hOldBitmap;
138 /***********************************************************************
139 * STATIC_LoadIconA
141 * Load the icon for an SS_ICON control.
143 static HICON STATIC_LoadIconA( HWND hwnd, LPCSTR name )
145 HINSTANCE hInstance = GetWindowLongA( hwnd, GWL_HINSTANCE );
146 HICON hicon = LoadIconA( hInstance, name );
147 if (!hicon) hicon = LoadIconA( 0, name );
148 return hicon;
151 /***********************************************************************
152 * STATIC_LoadIconW
154 * Load the icon for an SS_ICON control.
156 static HICON STATIC_LoadIconW( HWND hwnd, LPCWSTR name )
158 HINSTANCE hInstance = GetWindowLongA( hwnd, GWL_HINSTANCE );
159 HICON hicon = LoadIconW( hInstance, name );
160 if (!hicon) hicon = LoadIconW( 0, name );
161 return hicon;
164 /***********************************************************************
165 * STATIC_LoadBitmapA
167 * Load the bitmap for an SS_BITMAP control.
169 static HBITMAP STATIC_LoadBitmapA( HWND hwnd, LPCSTR name )
171 HINSTANCE hInstance = GetWindowLongA( hwnd, GWL_HINSTANCE );
172 HBITMAP hbitmap = LoadBitmapA( hInstance, name );
173 if (!hbitmap) /* Try OEM icon (FIXME: is this right?) */
174 hbitmap = LoadBitmapA( 0, name );
175 return hbitmap;
178 /***********************************************************************
179 * STATIC_LoadBitmapW
181 * Load the bitmap for an SS_BITMAP control.
183 static HBITMAP STATIC_LoadBitmapW( HWND hwnd, LPCWSTR name )
185 HINSTANCE hInstance = GetWindowLongA( hwnd, GWL_HINSTANCE );
186 HBITMAP hbitmap = LoadBitmapW( hInstance, name );
187 if (!hbitmap) /* Try OEM icon (FIXME: is this right?) */
188 hbitmap = LoadBitmapW( 0, name );
189 return hbitmap;
192 /***********************************************************************
193 * STATIC_TryPaintFcn
195 * Try to immediately paint the control.
197 static VOID STATIC_TryPaintFcn(HWND hwnd, LONG full_style)
199 LONG style = full_style & SS_TYPEMASK;
200 RECT rc;
202 GetClientRect( hwnd, &rc );
203 if (!IsRectEmpty(&rc) && IsWindowVisible(hwnd) && staticPaintFunc[style])
205 HDC hdc;
206 hdc = GetDC( hwnd );
207 (staticPaintFunc[style])( hwnd, hdc, full_style );
208 ReleaseDC( hwnd, hdc );
212 /***********************************************************************
213 * StaticWndProc_common
215 static LRESULT StaticWndProc_common( HWND hwnd, UINT uMsg, WPARAM wParam,
216 LPARAM lParam, BOOL unicode )
218 LRESULT lResult = 0;
219 LONG full_style = GetWindowLongA( hwnd, GWL_STYLE );
220 LONG style = full_style & SS_TYPEMASK;
222 switch (uMsg)
224 case WM_CREATE:
225 if (style < 0L || style > SS_TYPEMASK)
227 ERR("Unknown style 0x%02lx\n", style );
228 return -1;
230 /* initialise colours */
231 color_windowframe = GetSysColor(COLOR_WINDOWFRAME);
232 color_background = GetSysColor(COLOR_BACKGROUND);
233 color_window = GetSysColor(COLOR_WINDOW);
234 break;
236 case WM_NCDESTROY:
237 if (style == SS_ICON) {
239 * FIXME
240 * DestroyIcon32( STATIC_SetIcon( wndPtr, 0 ) );
242 * We don't want to do this yet because DestroyIcon32 is broken. If the icon
243 * had already been loaded by the application the last thing we want to do is
244 * GlobalFree16 the handle.
246 break;
248 else return unicode ? DefWindowProcW(hwnd, uMsg, wParam, lParam) :
249 DefWindowProcA(hwnd, uMsg, wParam, lParam);
251 case WM_PAINT:
253 PAINTSTRUCT ps;
254 BeginPaint(hwnd, &ps);
255 if (staticPaintFunc[style])
256 (staticPaintFunc[style])( hwnd, ps.hdc, full_style );
257 EndPaint(hwnd, &ps);
259 break;
261 case WM_ENABLE:
262 InvalidateRect(hwnd, NULL, TRUE);
263 break;
265 case WM_SYSCOLORCHANGE:
266 color_windowframe = GetSysColor(COLOR_WINDOWFRAME);
267 color_background = GetSysColor(COLOR_BACKGROUND);
268 color_window = GetSysColor(COLOR_WINDOW);
269 InvalidateRect(hwnd, NULL, TRUE);
270 break;
272 case WM_NCCREATE:
273 if ((TWEAK_WineLook > WIN31_LOOK) && (full_style & SS_SUNKEN))
274 SetWindowLongA( hwnd, GWL_EXSTYLE,
275 GetWindowLongA( hwnd, GWL_EXSTYLE ) | WS_EX_STATICEDGE );
277 if(unicode)
278 lParam = (LPARAM)(((LPCREATESTRUCTW)lParam)->lpszName);
279 else
280 lParam = (LPARAM)(((LPCREATESTRUCTA)lParam)->lpszName);
281 /* fall through */
282 case WM_SETTEXT:
283 switch (style) {
284 case SS_ICON:
286 HICON hIcon;
287 if(unicode)
288 hIcon = STATIC_LoadIconW(hwnd, (LPCWSTR)lParam);
289 else
290 hIcon = STATIC_LoadIconA(hwnd, (LPCSTR)lParam);
291 /* FIXME : should we also return the previous hIcon here ??? */
292 STATIC_SetIcon(hwnd, hIcon, style);
293 break;
295 case SS_BITMAP:
297 HBITMAP hBitmap;
298 if(unicode)
299 hBitmap = STATIC_LoadBitmapW(hwnd, (LPCWSTR)lParam);
300 else
301 hBitmap = STATIC_LoadBitmapA(hwnd, (LPCSTR)lParam);
302 STATIC_SetBitmap(hwnd, hBitmap, style);
303 break;
305 case SS_LEFT:
306 case SS_CENTER:
307 case SS_RIGHT:
308 case SS_SIMPLE:
309 case SS_LEFTNOWORDWRAP:
311 if (HIWORD(lParam))
313 if(unicode)
314 lResult = DefWindowProcW( hwnd, WM_SETTEXT, wParam, lParam );
315 else
316 lResult = DefWindowProcA( hwnd, WM_SETTEXT, wParam, lParam );
318 if (uMsg == WM_SETTEXT)
319 STATIC_TryPaintFcn( hwnd, full_style );
320 break;
322 default:
323 if (HIWORD(lParam))
325 if(unicode)
326 lResult = DefWindowProcW( hwnd, WM_SETTEXT, wParam, lParam );
327 else
328 lResult = DefWindowProcA( hwnd, WM_SETTEXT, wParam, lParam );
330 if(uMsg == WM_SETTEXT)
331 InvalidateRect(hwnd, NULL, TRUE);
333 return 1; /* success. FIXME: check text length */
335 case WM_SETFONT:
336 if ((style == SS_ICON) || (style == SS_BITMAP)) return 0;
337 SetWindowLongA( hwnd, HFONT_GWL_OFFSET, wParam );
338 if (LOWORD(lParam))
339 InvalidateRect( hwnd, NULL, TRUE );
340 break;
342 case WM_GETFONT:
343 return GetWindowLongA( hwnd, HFONT_GWL_OFFSET );
345 case WM_NCHITTEST:
346 if (full_style & SS_NOTIFY)
347 return HTCLIENT;
348 else
349 return HTTRANSPARENT;
351 case WM_GETDLGCODE:
352 return DLGC_STATIC;
354 case STM_GETIMAGE:
355 case STM_GETICON16:
356 case STM_GETICON:
357 return GetWindowLongA( hwnd, HICON_GWL_OFFSET );
359 case STM_SETIMAGE:
360 switch(wParam) {
361 case IMAGE_BITMAP:
362 lResult = STATIC_SetBitmap( hwnd, (HBITMAP)lParam, style );
363 break;
364 case IMAGE_ICON:
365 lResult = STATIC_SetIcon( hwnd, (HICON)lParam, style );
366 break;
367 default:
368 FIXME("STM_SETIMAGE: Unhandled type %x\n", wParam);
369 break;
371 InvalidateRect( hwnd, NULL, TRUE );
372 break;
374 case STM_SETICON16:
375 case STM_SETICON:
376 lResult = STATIC_SetIcon( hwnd, (HICON)wParam, style );
377 InvalidateRect( hwnd, NULL, TRUE );
378 break;
380 default:
381 return unicode ? DefWindowProcW(hwnd, uMsg, wParam, lParam) :
382 DefWindowProcA(hwnd, uMsg, wParam, lParam);
384 return lResult;
387 /***********************************************************************
388 * StaticWndProcA
390 static LRESULT WINAPI StaticWndProcA( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
392 if (!IsWindow( hWnd )) return 0;
393 return StaticWndProc_common(hWnd, uMsg, wParam, lParam, FALSE);
396 /***********************************************************************
397 * StaticWndProcW
399 static LRESULT WINAPI StaticWndProcW( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
401 if (!IsWindow( hWnd )) return 0;
402 return StaticWndProc_common(hWnd, uMsg, wParam, lParam, TRUE);
405 static void STATIC_PaintOwnerDrawfn( HWND hwnd, HDC hdc, DWORD style )
407 DRAWITEMSTRUCT dis;
408 LONG id = GetWindowLongA( hwnd, GWL_ID );
410 dis.CtlType = ODT_STATIC;
411 dis.CtlID = id;
412 dis.itemID = 0;
413 dis.itemAction = ODA_DRAWENTIRE;
414 dis.itemState = 0;
415 dis.hwndItem = hwnd;
416 dis.hDC = hdc;
417 dis.itemData = 0;
418 GetClientRect( hwnd, &dis.rcItem );
420 SendMessageW( GetParent(hwnd), WM_CTLCOLORSTATIC, hdc, hwnd );
421 SendMessageW( GetParent(hwnd), WM_DRAWITEM, id, (LPARAM)&dis );
424 static void STATIC_PaintTextfn( HWND hwnd, HDC hdc, DWORD style )
426 RECT rc;
427 HBRUSH hBrush;
428 HFONT hFont;
429 WORD wFormat;
430 INT len;
431 WCHAR *text;
433 GetClientRect( hwnd, &rc);
435 switch (style & SS_TYPEMASK)
437 case SS_LEFT:
438 wFormat = DT_LEFT | DT_EXPANDTABS | DT_WORDBREAK | DT_NOCLIP;
439 break;
441 case SS_CENTER:
442 wFormat = DT_CENTER | DT_EXPANDTABS | DT_WORDBREAK | DT_NOCLIP;
443 break;
445 case SS_RIGHT:
446 wFormat = DT_RIGHT | DT_EXPANDTABS | DT_WORDBREAK | DT_NOCLIP;
447 break;
449 case SS_SIMPLE:
450 wFormat = DT_LEFT | DT_SINGLELINE | DT_VCENTER | DT_NOCLIP;
451 break;
453 case SS_LEFTNOWORDWRAP:
454 wFormat = DT_LEFT | DT_EXPANDTABS | DT_VCENTER;
455 break;
457 default:
458 return;
461 if (style & SS_NOPREFIX)
462 wFormat |= DT_NOPREFIX;
464 if ((hFont = GetWindowLongA( hwnd, HFONT_GWL_OFFSET ))) SelectObject( hdc, hFont );
466 if ((style & SS_NOPREFIX) || ((style & SS_TYPEMASK) != SS_SIMPLE))
468 hBrush = SendMessageW( GetParent(hwnd), WM_CTLCOLORSTATIC, hdc, hwnd );
469 if (!hBrush) /* did the app forget to call defwindowproc ? */
470 hBrush = DefWindowProcW(GetParent(hwnd), WM_CTLCOLORSTATIC, hdc, hwnd);
471 FillRect( hdc, &rc, hBrush );
473 if (!IsWindowEnabled(hwnd)) SetTextColor(hdc, GetSysColor(COLOR_GRAYTEXT));
475 if (!(len = SendMessageW( hwnd, WM_GETTEXTLENGTH, 0, 0 ))) return;
476 if (!(text = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) ))) return;
477 SendMessageW( hwnd, WM_GETTEXT, len + 1, (LPARAM)text );
478 DrawTextW( hdc, text, -1, &rc, wFormat );
479 HeapFree( GetProcessHeap(), 0, text );
482 static void STATIC_PaintRectfn( HWND hwnd, HDC hdc, DWORD style )
484 RECT rc;
485 HBRUSH hBrush;
487 GetClientRect( hwnd, &rc);
489 switch (style & SS_TYPEMASK)
491 case SS_BLACKRECT:
492 hBrush = CreateSolidBrush(color_windowframe);
493 FillRect( hdc, &rc, hBrush );
494 break;
495 case SS_GRAYRECT:
496 hBrush = CreateSolidBrush(color_background);
497 FillRect( hdc, &rc, hBrush );
498 break;
499 case SS_WHITERECT:
500 hBrush = CreateSolidBrush(color_window);
501 FillRect( hdc, &rc, hBrush );
502 break;
503 case SS_BLACKFRAME:
504 hBrush = CreateSolidBrush(color_windowframe);
505 FrameRect( hdc, &rc, hBrush );
506 break;
507 case SS_GRAYFRAME:
508 hBrush = CreateSolidBrush(color_background);
509 FrameRect( hdc, &rc, hBrush );
510 break;
511 case SS_WHITEFRAME:
512 hBrush = CreateSolidBrush(color_window);
513 FrameRect( hdc, &rc, hBrush );
514 break;
515 default:
516 return;
518 DeleteObject( hBrush );
522 static void STATIC_PaintIconfn( HWND hwnd, HDC hdc, DWORD style )
524 RECT rc;
525 HBRUSH hbrush;
526 HICON hIcon;
528 GetClientRect( hwnd, &rc );
529 hbrush = SendMessageW( GetParent(hwnd), WM_CTLCOLORSTATIC, hdc, hwnd );
530 FillRect( hdc, &rc, hbrush );
531 if ((hIcon = GetWindowLongA( hwnd, HICON_GWL_OFFSET )))
532 DrawIcon( hdc, rc.left, rc.top, hIcon );
535 static void STATIC_PaintBitmapfn(HWND hwnd, HDC hdc, DWORD style )
537 RECT rc;
538 HBRUSH hbrush;
539 HICON hIcon;
540 HDC hMemDC;
541 HBITMAP oldbitmap;
543 GetClientRect( hwnd, &rc );
544 hbrush = SendMessageW( GetParent(hwnd), WM_CTLCOLORSTATIC, hdc, hwnd );
545 FillRect( hdc, &rc, hbrush );
547 if ((hIcon = GetWindowLongA( hwnd, HICON_GWL_OFFSET )))
549 BITMAP bm;
550 SIZE sz;
552 if(GetObjectType(hIcon) != OBJ_BITMAP) return;
553 if (!(hMemDC = CreateCompatibleDC( hdc ))) return;
554 GetObjectW(hIcon, sizeof(bm), &bm);
555 GetBitmapDimensionEx(hIcon, &sz);
556 oldbitmap = SelectObject(hMemDC, hIcon);
557 BitBlt(hdc, sz.cx, sz.cy, bm.bmWidth, bm.bmHeight, hMemDC, 0, 0,
558 SRCCOPY);
559 SelectObject(hMemDC, oldbitmap);
560 DeleteDC(hMemDC);
565 static void STATIC_PaintEtchedfn( HWND hwnd, HDC hdc, DWORD style )
567 RECT rc;
569 if (TWEAK_WineLook == WIN31_LOOK)
570 return;
572 GetClientRect( hwnd, &rc );
573 switch (style & SS_TYPEMASK)
575 case SS_ETCHEDHORZ:
576 DrawEdge(hdc,&rc,EDGE_ETCHED,BF_TOP|BF_BOTTOM);
577 break;
578 case SS_ETCHEDVERT:
579 DrawEdge(hdc,&rc,EDGE_ETCHED,BF_LEFT|BF_RIGHT);
580 break;
581 case SS_ETCHEDFRAME:
582 DrawEdge (hdc, &rc, EDGE_ETCHED, BF_RECT);
583 break;