source: trunk/src/user32/new/static.cpp@ 373

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

CVS headers added

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