source: trunk/src/user32/static.cpp@ 989

Last change on this file since 989 was 989, checked in by cbratschi, 26 years ago

DrawText fix

File size: 16.0 KB
Line 
1/* $Id: static.cpp,v 1.2 1999-09-20 16:04:44 cbratschi Exp $ */
2/*
3 * Static control
4 *
5 * Copyright 1999 Christoph Bratschi (ported from WINE)
6 *
7 * Copyright David W. Metcalfe, 1993
8 *
9 */
10
11#include <stdlib.h>
12#include "winuser.h"
13#include "winbase.h"
14#include "controls.h"
15#include "static.h"
16
17//Prototypes
18
19static void STATIC_PaintTextfn( HWND hwnd, HDC hdc );
20static void STATIC_PaintRectfn( HWND hwnd, HDC hdc );
21static void STATIC_PaintIconfn( HWND hwnd, HDC hdc );
22static void STATIC_PaintBitmapfn( HWND hwnd, HDC hdc );
23static void STATIC_PaintEtchedfn( HWND hwnd, HDC hdc );
24
25static COLORREF color_windowframe, color_background, color_window;
26
27
28typedef void (*pfPaint)( HWND, HDC );
29
30static pfPaint staticPaintFunc[SS_TYPEMASK+1] =
31{
32 STATIC_PaintTextfn, /* SS_LEFT */
33 STATIC_PaintTextfn, /* SS_CENTER */
34 STATIC_PaintTextfn, /* SS_RIGHT */
35 STATIC_PaintIconfn, /* SS_ICON */
36 STATIC_PaintRectfn, /* SS_BLACKRECT */
37 STATIC_PaintRectfn, /* SS_GRAYRECT */
38 STATIC_PaintRectfn, /* SS_WHITERECT */
39 STATIC_PaintRectfn, /* SS_BLACKFRAME */
40 STATIC_PaintRectfn, /* SS_GRAYFRAME */
41 STATIC_PaintRectfn, /* SS_WHITEFRAME */
42 NULL, /* Not defined */
43 STATIC_PaintTextfn, /* SS_SIMPLE */
44 STATIC_PaintTextfn, /* SS_LEFTNOWORDWRAP */
45 NULL, /* SS_OWNERDRAW */
46 STATIC_PaintBitmapfn, /* SS_BITMAP */
47 NULL, /* SS_ENHMETAFILE */
48 STATIC_PaintEtchedfn, /* SS_ETCHEDHORIZ */
49 STATIC_PaintEtchedfn, /* SS_ETCHEDVERT */
50 STATIC_PaintEtchedfn, /* SS_ETCHEDFRAME */
51};
52
53
54/***********************************************************************
55 * STATIC_SetIcon
56 *
57 * Set the icon for an SS_ICON control.
58 */
59static HICON STATIC_SetIcon( HWND hwnd, HICON hicon )
60{
61 HICON prevIcon;
62 STATICINFO *infoPtr = (STATICINFO *)GetInfoPtr(hwnd);
63 DWORD dwStyle = GetWindowLongA(hwnd,GWL_STYLE);
64 INT cx,cy;
65
66 if ((dwStyle & SS_TYPEMASK) != SS_ICON) return 0;
67
68 if (infoPtr->hIcon) DestroyIcon(infoPtr->hIcon);
69 prevIcon = infoPtr->hIcon;
70 infoPtr->hIcon = hicon;
71
72 cx = GetSystemMetrics(SM_CXICON);
73 cy = GetSystemMetrics(SM_CYICON);
74
75 SetWindowPos(hwnd,0,0,0,cx,cy,SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER );
76
77 return prevIcon;
78}
79
80/***********************************************************************
81 * STATIC_SetBitmap
82 *
83 * Set the bitmap for an SS_BITMAP control.
84 */
85static HBITMAP STATIC_SetBitmap( HWND hwnd, HBITMAP hBitmap )
86{
87 HBITMAP hOldBitmap;
88 STATICINFO *infoPtr = (STATICINFO *)GetInfoPtr(hwnd);
89 DWORD dwStyle = GetWindowLongA(hwnd,GWL_STYLE);
90
91 if ((dwStyle & SS_TYPEMASK) != SS_BITMAP) return 0;
92
93 if (hBitmap && GetObjectType(hBitmap) != OBJ_BITMAP) {
94 //ERR("huh? hBitmap!=0, but not bitmap\n");
95 return 0;
96 }
97 hOldBitmap = infoPtr->hIcon;
98 infoPtr->hIcon = hBitmap;
99 if (hBitmap)
100 {
101 BITMAP bm;
102 GetObjectA(hBitmap, sizeof(bm), &bm);
103 SetWindowPos( hwnd, 0, 0, 0, bm.bmWidth, bm.bmHeight,
104 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER );
105/* CB: alternative code, if necessary
106 HDC hdc = GetDC(hwnd);
107 BITMAPINFO info;
108
109 ZeroMemory(&info,sizeof(info));
110 if (GetDIBits(hdc,hbitmap,0,0,NULL,&info,0) != 0)
111 {
112 SetWindowPos(hwnd,0,0,0,info.bmiHeader.biWidth,info.bmiHeader.biHeight,SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER);
113 }
114 ReleaseDC(hwnd,hdc);
115*/
116
117 }
118 return hOldBitmap;
119}
120
121/***********************************************************************
122 * STATIC_LoadIcon
123 *
124 * Load the icon for an SS_ICON control.
125 */
126static HICON STATIC_LoadIcon( HWND hwnd, LPCSTR name )
127{
128 HICON hicon;
129
130 hicon = LoadIconA(GetWindowLongA(hwnd,GWL_HINSTANCE),name);
131
132 if (!hicon)
133 hicon = LoadIconA(0, name);
134
135 return hicon;
136}
137
138/***********************************************************************
139 * STATIC_LoadBitmap
140 *
141 * Load the bitmap for an SS_BITMAP control.
142 */
143static HBITMAP STATIC_LoadBitmap( HWND hwnd, LPCSTR name )
144{
145 HBITMAP hbitmap;
146
147 hbitmap = LoadBitmapA(GetWindowLongA(hwnd,GWL_HINSTANCE),name);
148
149 if (!hbitmap) /* Try OEM icon (FIXME: is this right?) */
150 hbitmap = LoadBitmapA(0,name);
151
152 return hbitmap;
153}
154
155/* message handler */
156
157LRESULT STATIC_NCCreate(HWND hwnd,WPARAM wParam,LPARAM lParam)
158{
159 CREATESTRUCTA *cs = (CREATESTRUCTA *)lParam;
160 STATICINFO* infoPtr;
161 DWORD dwStyle = GetWindowLongA(hwnd,GWL_STYLE);
162 DWORD style = dwStyle & SS_TYPEMASK;
163 DWORD dwExStyle = GetWindowLongA(hwnd,GWL_EXSTYLE);
164
165 infoPtr = (STATICINFO*)malloc(sizeof(STATICINFO));
166 infoPtr->hFont = 0;
167 infoPtr->dummy = 0;
168 infoPtr->hIcon = 0;
169 SetInfoPtr(hwnd,(DWORD)infoPtr);
170
171 if (dwStyle & SS_SUNKEN)
172 {
173 dwExStyle |= WS_EX_STATICEDGE;
174 SetWindowLongA(hwnd,GWL_EXSTYLE,dwExStyle);
175 }
176
177 if (style == SS_ICON)
178 {
179 if (cs->lpszName)
180 {
181 if (!HIWORD(cs->lpszName) || cs->lpszName[0])
182 STATIC_SetIcon(hwnd,STATIC_LoadIcon(hwnd,cs->lpszName));
183 }
184 return TRUE;
185 }
186 if (style == SS_BITMAP)
187 {
188 if (cs->lpszName)
189 STATIC_SetBitmap(hwnd,STATIC_LoadBitmap(hwnd,cs->lpszName));
190 return TRUE;
191 }
192 if (!HIWORD(cs->lpszName) && (cs->lpszName)) return TRUE;
193
194 return DefWindowProcA(hwnd,WM_NCCREATE,wParam,lParam);
195}
196
197LRESULT STATIC_Create(HWND hwnd,WPARAM wParam,LPARAM lParam)
198{
199 DWORD style = GetWindowLongA(hwnd,GWL_STYLE) & SS_TYPEMASK;
200
201 if (style < 0L || style > SS_TYPEMASK)
202 {
203 //Unknown style
204 return (LRESULT)-1;
205 }
206
207 /* initialise colours */
208 color_windowframe = GetSysColor(COLOR_WINDOWFRAME);
209 color_background = GetSysColor(COLOR_BACKGROUND);
210 color_window = GetSysColor(COLOR_WINDOW);
211
212 return 0;
213}
214
215LRESULT STATIC_NCDestroy(HWND hwnd,WPARAM wParam,LPARAM lParam)
216{
217 STATICINFO* infoPtr = (STATICINFO*)GetInfoPtr(hwnd);
218 DWORD style = GetWindowLongA(hwnd,GWL_STYLE) & SS_TYPEMASK;
219
220 if (style == SS_ICON && infoPtr->hIcon)
221 {
222 DestroyIcon(infoPtr->hIcon);
223 } else if (style == SS_BITMAP && infoPtr->hIcon)
224 {
225 DeleteObject(infoPtr->hIcon);
226 }
227
228 free(infoPtr);
229
230 return DefWindowProcA(hwnd,WM_NCDESTROY,wParam,lParam);
231}
232
233LRESULT STATIC_Paint(HWND hwnd,WPARAM wParam,LPARAM lParam)
234{
235 DWORD style = GetWindowLongA(hwnd,GWL_STYLE) & SS_TYPEMASK;
236 PAINTSTRUCT ps;
237
238 BeginPaint(hwnd,&ps);
239 if (staticPaintFunc[style]) (staticPaintFunc[style])(hwnd,ps.hdc);
240 EndPaint(hwnd,&ps);
241
242 return 0;
243}
244
245LRESULT STATIC_Enable(HWND hwnd,WPARAM wParam,LPARAM lParam)
246{
247 InvalidateRect(hwnd,NULL,FALSE);
248
249 return 0;
250}
251
252LRESULT STATIC_SysColorChange(HWND hwnd,WPARAM wParam,LPARAM lParam)
253{
254 color_windowframe = GetSysColor(COLOR_WINDOWFRAME);
255 color_background = GetSysColor(COLOR_BACKGROUND);
256 color_window = GetSysColor(COLOR_WINDOW);
257
258 InvalidateRect(hwnd,NULL,TRUE);
259
260 return 0;
261}
262
263LRESULT STATIC_SetText(HWND hwnd,WPARAM wParam,LPARAM lParam)
264{
265 DWORD style = GetWindowLongA(hwnd,GWL_STYLE) & SS_TYPEMASK;
266
267 if (style == SS_ICON)
268 STATIC_SetIcon(hwnd,STATIC_LoadIcon(hwnd,(LPCSTR)lParam));
269 else if (style == SS_BITMAP)
270 STATIC_SetBitmap(hwnd,STATIC_LoadBitmap(hwnd,(LPCSTR)lParam));
271 else
272 DefWindowProcA(hwnd,WM_SETTEXT,wParam,lParam);
273
274 InvalidateRect(hwnd,NULL,FALSE);
275 UpdateWindow(hwnd);
276
277 return 0;
278}
279
280LRESULT STATIC_SetFont(HWND hwnd,WPARAM wParam,LPARAM lParam)
281{
282 STATICINFO* infoPtr = (STATICINFO*)GetInfoPtr(hwnd);
283 DWORD style = GetWindowLongA(hwnd,GWL_STYLE) & SS_TYPEMASK;
284
285 if (style == SS_ICON) return 0;
286 if (style == SS_BITMAP) return 0;
287
288 infoPtr->hFont = (HFONT)wParam;
289
290 if (LOWORD(lParam))
291 {
292 InvalidateRect(hwnd,NULL,FALSE);
293 UpdateWindow(hwnd);
294 }
295
296 return 0;
297}
298
299LRESULT STATIC_GetFont(HWND hwnd,WPARAM wParam,LPARAM lParam)
300{
301 STATICINFO* infoPtr = (STATICINFO*)GetInfoPtr(hwnd);
302
303 return infoPtr->hFont;
304}
305
306LRESULT STATIC_NCHitTest(HWND hwnd,WPARAM wParam,LPARAM lParam)
307{
308 return HTTRANSPARENT;
309}
310
311LRESULT STATIC_GetDlgCode(HWND hwnd,WPARAM wParam,LPARAM lParam)
312{
313 return DLGC_STATIC;
314}
315
316LRESULT STATIC_GetIcon(HWND hwnd,WPARAM wParam,LPARAM lParam)
317{
318 STATICINFO* infoPtr = (STATICINFO*)GetInfoPtr(hwnd);
319
320 return infoPtr->hIcon;
321}
322
323LRESULT STATIC_SetImage(HWND hwnd,WPARAM wParam,LPARAM lParam)
324{
325 LRESULT lResult;
326
327 switch (wParam)
328 {
329 case IMAGE_CURSOR:
330 case IMAGE_ICON:
331 lResult = STATIC_SetIcon(hwnd,(HICON)lParam);
332 break;
333 case IMAGE_BITMAP:
334 lResult = STATIC_SetBitmap(hwnd,(HBITMAP)lParam);
335 case IMAGE_ENHMETAFILE:
336 return 0; //CB: not supported!
337 default:
338 return 0;
339 }
340
341 InvalidateRect(hwnd,NULL,FALSE);
342 UpdateWindow(hwnd);
343
344 return lResult;
345}
346
347LRESULT STATIC_SetIcon(HWND hwnd,WPARAM wParam,LPARAM lParam)
348{
349 LRESULT lResult;
350
351 lResult = STATIC_SetIcon(hwnd,(HICON)wParam);
352
353 InvalidateRect(hwnd,NULL,FALSE);
354 UpdateWindow(hwnd);
355
356 return lResult;
357}
358
359/***********************************************************************
360 * StaticWndProc
361 */
362LRESULT WINAPI StaticWndProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
363{
364 switch (uMsg)
365 {
366 case WM_NCCREATE:
367 return STATIC_NCCreate(hwnd,wParam,lParam);
368
369 case WM_CREATE:
370 return STATIC_Create(hwnd,wParam,lParam);
371
372 case WM_NCDESTROY:
373 return STATIC_NCDestroy(hwnd,wParam,lParam);
374
375 case WM_PAINT:
376 return STATIC_Paint(hwnd,wParam,lParam);
377
378 case WM_ENABLE:
379 return STATIC_Enable(hwnd,wParam,lParam);
380
381 case WM_SYSCOLORCHANGE:
382 return STATIC_SysColorChange(hwnd,wParam,lParam);
383
384 case WM_SETTEXT:
385 return STATIC_SetText(hwnd,wParam,lParam);
386
387 case WM_SETFONT:
388 return STATIC_SetFont(hwnd,wParam,lParam);
389
390 case WM_GETFONT:
391 return STATIC_GetFont(hwnd,wParam,lParam);
392
393 case WM_NCHITTEST:
394 return STATIC_NCHitTest(hwnd,wParam,lParam);
395
396 case WM_GETDLGCODE:
397 return STATIC_GetDlgCode(hwnd,wParam,lParam);
398
399 case STM_GETIMAGE:
400 case STM_GETICON:
401 return STATIC_GetIcon(hwnd,wParam,lParam);
402
403 case STM_SETIMAGE:
404 return STATIC_SetImage(hwnd,wParam,lParam);
405
406 case STM_SETICON:
407 return STATIC_SetIcon(hwnd,wParam,lParam);
408
409 default:
410 return DefWindowProcA(hwnd,uMsg,wParam,lParam);
411 break;
412 }
413
414 return DefWindowProcA(hwnd,uMsg,wParam,lParam);
415}
416
417
418static void STATIC_PaintTextfn(HWND hwnd, HDC hdc )
419{
420 RECT rc;
421 HBRUSH hBrush;
422 WORD wFormat;
423 DWORD dwStyle = GetWindowLongA(hwnd,GWL_STYLE);
424 LONG style = dwStyle;
425 STATICINFO *infoPtr = (STATICINFO *)GetInfoPtr(hwnd);
426 INT textLen;
427
428 GetClientRect(hwnd,&rc);
429
430 switch (style & SS_TYPEMASK)
431 {
432 case SS_LEFT:
433 wFormat = DT_LEFT | DT_EXPANDTABS | DT_WORDBREAK | DT_NOCLIP;
434 break;
435
436 case SS_CENTER:
437 wFormat = DT_CENTER | DT_EXPANDTABS | DT_WORDBREAK | DT_NOCLIP;
438 break;
439
440 case SS_RIGHT:
441 wFormat = DT_RIGHT | DT_EXPANDTABS | DT_WORDBREAK | DT_NOCLIP;
442 break;
443
444 case SS_SIMPLE:
445 wFormat = DT_LEFT | DT_SINGLELINE | DT_VCENTER | DT_NOCLIP;
446 break;
447
448 case SS_LEFTNOWORDWRAP:
449 wFormat = DT_LEFT | DT_EXPANDTABS | DT_VCENTER;
450 break;
451
452 default:
453 return;
454 }
455
456 if (style & SS_NOPREFIX)
457 wFormat |= DT_NOPREFIX;
458
459 if (infoPtr->hFont) SelectObject( hdc, infoPtr->hFont );
460 hBrush = SendMessageA( GetParent(hwnd), WM_CTLCOLORSTATIC,
461 hdc, hwnd );
462 if (!hBrush) hBrush = GetStockObject(WHITE_BRUSH);
463 FillRect( hdc, &rc, hBrush );
464
465 textLen = GetWindowTextLengthA(hwnd);
466 if (textLen > 0)
467 {
468 char* text;
469
470 textLen++;
471 text = (char*)malloc(textLen);
472 GetWindowTextA(hwnd,text,textLen);
473 SetBkMode(hdc,TRANSPARENT);
474 DrawTextA( hdc, text, -1, &rc, wFormat );
475 free(text);
476 }
477}
478
479static void STATIC_PaintRectfn( HWND hwnd, HDC hdc )
480{
481 DWORD dwStyle = GetWindowLongA(hwnd,GWL_STYLE);
482 RECT rc;
483 HBRUSH hBrush;
484
485 GetClientRect( hwnd, &rc);
486
487 switch (dwStyle & SS_TYPEMASK)
488 {
489 case SS_BLACKRECT:
490 hBrush = CreateSolidBrush(color_windowframe);
491 FillRect( hdc, &rc, hBrush );
492 break;
493 case SS_GRAYRECT:
494 hBrush = CreateSolidBrush(color_background);
495 FillRect( hdc, &rc, hBrush );
496 break;
497 case SS_WHITERECT:
498 hBrush = CreateSolidBrush(color_window);
499 FillRect( hdc, &rc, hBrush );
500 break;
501 case SS_BLACKFRAME:
502 hBrush = CreateSolidBrush(color_windowframe);
503 FrameRect( hdc, &rc, hBrush );
504 break;
505 case SS_GRAYFRAME:
506 hBrush = CreateSolidBrush(color_background);
507 FrameRect( hdc, &rc, hBrush );
508 break;
509 case SS_WHITEFRAME:
510 hBrush = CreateSolidBrush(color_window);
511 FrameRect( hdc, &rc, hBrush );
512 break;
513 default:
514 return;
515 }
516 DeleteObject( hBrush );
517}
518
519
520static void STATIC_PaintIconfn( HWND hwnd, HDC hdc )
521{
522 RECT rc;
523 HBRUSH hbrush;
524 STATICINFO *infoPtr = (STATICINFO *)GetInfoPtr(hwnd);
525
526 GetClientRect( hwnd, &rc );
527 hbrush = SendMessageA( GetParent(hwnd), WM_CTLCOLORSTATIC,
528 hdc, hwnd );
529 FillRect( hdc, &rc, hbrush );
530 if (infoPtr->hIcon) DrawIcon( hdc, rc.left, rc.top, infoPtr->hIcon );
531}
532
533static void STATIC_PaintBitmapfn(HWND hwnd, HDC hdc )
534{
535 RECT rc;
536 HBRUSH hbrush;
537 STATICINFO *infoPtr = (STATICINFO *)GetInfoPtr(hwnd);
538 HDC hMemDC;
539 HBITMAP oldbitmap;
540
541 GetClientRect( hwnd, &rc );
542 hbrush = SendMessageA( GetParent(hwnd), WM_CTLCOLORSTATIC,
543 hdc, hwnd );
544 FillRect( hdc, &rc, hbrush );
545
546 if (infoPtr->hIcon) {
547 BITMAP bm;
548 SIZE sz;
549
550 if(GetObjectType(infoPtr->hIcon) != OBJ_BITMAP)
551 return;
552 if (!(hMemDC = CreateCompatibleDC( hdc ))) return;
553 GetObjectA(infoPtr->hIcon, sizeof(bm), &bm);
554 GetBitmapDimensionEx(infoPtr->hIcon, &sz);
555 oldbitmap = SelectObject(hMemDC, infoPtr->hIcon);
556 BitBlt(hdc, sz.cx, sz.cy, bm.bmWidth, bm.bmHeight, hMemDC, 0, 0,
557 SRCCOPY);
558 SelectObject(hMemDC, oldbitmap);
559 DeleteDC(hMemDC);
560 }
561}
562
563
564static void STATIC_PaintEtchedfn( HWND hwnd, HDC hdc )
565{
566 DWORD dwStyle = GetWindowLongA(hwnd,GWL_STYLE);
567 RECT rc;
568 HBRUSH hbrush;
569 HPEN hpen;
570
571 GetClientRect( hwnd, &rc );
572 hbrush = SendMessageA( GetParent(hwnd), WM_CTLCOLORSTATIC,
573 hdc, hwnd );
574 FillRect( hdc, &rc, hbrush );
575
576 switch (dwStyle & SS_TYPEMASK)
577 {
578 case SS_ETCHEDHORZ:
579 hpen = SelectObject (hdc, GetSysColorPen (COLOR_3DSHADOW));
580 MoveToEx (hdc, rc.left, rc.bottom / 2 - 1, NULL);
581 LineTo (hdc, rc.right - 1, rc.bottom / 2 - 1);
582 SelectObject (hdc, GetSysColorPen (COLOR_3DHIGHLIGHT));
583 MoveToEx (hdc, rc.left, rc.bottom / 2, NULL);
584 LineTo (hdc, rc.right, rc.bottom / 2);
585 LineTo (hdc, rc.right, rc.bottom / 2 - 1);
586 SelectObject (hdc, hpen);
587 break;
588
589 case SS_ETCHEDVERT:
590 hpen = SelectObject (hdc, GetSysColorPen (COLOR_3DSHADOW));
591 MoveToEx (hdc, rc.right / 2 - 1, rc.top, NULL);
592 LineTo (hdc, rc.right / 2 - 1, rc.bottom - 1);
593 SelectObject (hdc, GetSysColorPen (COLOR_3DHIGHLIGHT));
594 MoveToEx (hdc, rc.right / 2, rc.top, NULL);
595 LineTo (hdc, rc.right / 2, rc.bottom);
596 LineTo (hdc, rc.right / 2 -1 , rc.bottom);
597 SelectObject (hdc, hpen);
598 break;
599
600 case SS_ETCHEDFRAME:
601 DrawEdge (hdc, &rc, EDGE_ETCHED, BF_RECT);
602 break;
603 }
604}
605
606BOOL STATIC_Register()
607{
608 WNDCLASSA wndClass;
609
610 if (GlobalFindAtomA(STATICCLASSNAME)) return FALSE;
611
612 ZeroMemory(&wndClass,sizeof(WNDCLASSA));
613 wndClass.style = CS_GLOBALCLASS | CS_HREDRAW | CS_PARENTDC;
614 wndClass.lpfnWndProc = (WNDPROC)StaticWndProc;
615 wndClass.cbClsExtra = 0;
616 wndClass.cbWndExtra = sizeof(STATICINFO);
617 wndClass.hCursor = LoadCursorA (0,IDC_ARROWA);
618 wndClass.hbrBackground = (HBRUSH)0;
619 wndClass.lpszClassName = STATICCLASSNAME;
620
621 return RegisterClassA(&wndClass);
622}
623
624
625BOOL STATIC_Unregister()
626{
627 if (GlobalFindAtomA (STATICCLASSNAME))
628 return UnregisterClassA(STATICCLASSNAME,(HINSTANCE)NULL);
629 else return FALSE;
630}
Note: See TracBrowser for help on using the repository browser.