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

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

Added static control

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