]> git.saurik.com Git - wxWidgets.git/blob - src/msw/gauge.cpp
Cured DC/GDI object leak; listbox window proc restored from debugging
[wxWidgets.git] / src / msw / gauge.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: gauge.cpp
3 // Purpose: wxGauge class
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "gauge.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/defs.h"
25 #endif
26
27 #if USE_GAUGE
28
29 #include "wx/gauge.h"
30 #include "wx/msw/private.h"
31
32 /* gas gauge graph control messages--class "zYzGauge" */
33 #define ZYZG_SETRANGE (WM_USER + 0)
34 #define ZYZG_GETRANGE (WM_USER + 1)
35 #define ZYZG_SETPOSITION (WM_USER + 2)
36 #define ZYZG_GETPOSITION (WM_USER + 3)
37 #define ZYZG_SETORIENTATION (WM_USER + 4)
38 #define ZYZG_GETORIENTATION (WM_USER + 5)
39 #define ZYZG_SETFGCOLOR (WM_USER + 6)
40 #define ZYZG_GETFGCOLOR (WM_USER + 7)
41 #define ZYZG_SETBKCOLOR (WM_USER + 8)
42 #define ZYZG_GETBKCOLOR (WM_USER + 9)
43 #define ZYZG_SETWIDTH3D (WM_USER + 10)
44 #define ZYZG_GETWIDTH3D (WM_USER + 11)
45 #define ZYZG_SETBEZELFACE (WM_USER + 12)
46 #define ZYZG_GETBEZELFACE (WM_USER + 13)
47 #define ZYZG_SETDELTAPOS (WM_USER + 14)
48
49
50 /* orientations for ZYZG_WW_ORIENTATION */
51 #define ZYZG_ORIENT_LEFTTORIGHT 0
52 #define ZYZG_ORIENT_RIGHTTOLEFT 1
53 #define ZYZG_ORIENT_BOTTOMTOTOP 2
54 #define ZYZG_ORIENT_TOPTOBOTTOM 3
55
56
57 /* gauge styles */
58 #define ZYZGS_3D 0x8000L /* control will be 3D */
59
60 /* public function prototypes */
61 BOOL FAR PASCAL gaugeInit(HINSTANCE hInstance);
62
63 #define USE_PROGRESS_BAR 1
64
65 #if defined(__WIN95__) && !defined(__GNUWIN32__)
66 #include <commctrl.h>
67 #endif
68
69 #if !USE_SHARED_LIBRARY
70 IMPLEMENT_DYNAMIC_CLASS(wxGauge, wxControl)
71 #endif
72
73 bool wxGauge::Create(wxWindow *parent, const wxWindowID id,
74 const int range,
75 const wxPoint& pos,
76 const wxSize& size,
77 const long style,
78 const wxValidator& validator,
79 const wxString& name)
80 {
81 static bool wxGaugeInitialised = FALSE;
82
83 if ( !wxGaugeInitialised )
84 {
85 if (!gaugeInit((HWND) wxGetInstance()))
86 wxFatalError("Cannot initalize Gauge library");
87 wxGaugeInitialised = TRUE;
88 }
89
90 SetName(name);
91 SetValidator(validator);
92
93 if (parent) parent->AddChild(this);
94 m_rangeMax = range;
95
96 SetBackgroundColour(parent->GetDefaultBackgroundColour()) ;
97 SetForegroundColour(parent->GetDefaultForegroundColour()) ;
98
99 m_useProgressBar = FALSE;
100 m_windowStyle = style;
101
102 if ( id == -1 )
103 m_windowId = (int)NewControlId();
104 else
105 m_windowId = id;
106
107 int x = pos.x;
108 int y = pos.y;
109 int width = size.x;
110 int height = size.y;
111
112 // Use the Win95 progress bar if possible, but not if
113 // we request a vertical gauge.
114 #if defined(__WIN95__) && USE_PROGRESS_BAR
115 if ((m_windowStyle & wxGA_PROGRESSBAR) && ((m_windowStyle & wxGA_HORIZONTAL) == wxGA_HORIZONTAL))
116 m_useProgressBar = TRUE;
117 #endif
118
119 if (m_useProgressBar)
120 {
121 #if defined(__WIN95__) && USE_PROGRESS_BAR
122 long msFlags = WS_CHILD | WS_VISIBLE | WS_TABSTOP;
123
124 HWND wx_button =
125 CreateWindowEx(MakeExtendedStyle(m_windowStyle), PROGRESS_CLASS, NULL, msFlags,
126 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId,
127 wxGetInstance(), NULL);
128
129 m_hWnd = (WXHWND)wx_button;
130
131 // Subclass again for purposes of dialog editing mode
132 SubclassWin((WXHWND) wx_button);
133
134 SendMessage((HWND) GetHWND(), PBM_SETRANGE, 0, MAKELPARAM(0, range));
135 #endif
136 }
137 else
138 {
139 long msFlags = WS_CHILD | WS_VISIBLE | WS_TABSTOP;
140 /* if (m_windowStyle & wxTHREED) */
141 msFlags |= ZYZGS_3D;
142
143 HWND wx_button =
144 CreateWindowEx(MakeExtendedStyle(m_windowStyle), "zYzGauge", NULL, msFlags,
145 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId,
146 wxGetInstance(), NULL);
147
148 m_hWnd = (WXHWND)wx_button;
149
150 // Subclass again for purposes of dialog editing mode
151 SubclassWin((WXHWND)wx_button);
152
153 int wOrient = 0;
154
155 if (m_windowStyle & wxGA_HORIZONTAL)
156 wOrient = ZYZG_ORIENT_LEFTTORIGHT;
157 else
158 wOrient = ZYZG_ORIENT_BOTTOMTOTOP;
159
160 SendMessage(wx_button, ZYZG_SETORIENTATION, wOrient, 0);
161 SendMessage(wx_button, ZYZG_SETRANGE, range, 0);
162
163 SendMessage((HWND) GetHWND(), ZYZG_SETFGCOLOR, 0, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
164 SendMessage((HWND) GetHWND(), ZYZG_SETBKCOLOR, 0, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
165 }
166
167 SetFont(* parent->GetFont());
168
169 if (width == -1)
170 width = 50;
171 if (height == -1)
172 height = 50;
173 SetSize(x, y, width, height);
174
175 ShowWindow((HWND) GetHWND(), SW_SHOW);
176
177 return TRUE;
178 }
179
180 void wxGauge::SetSize(const int x, const int y, const int width, const int height, const int sizeFlags)
181 {
182 int currentX, currentY;
183 GetPosition(&currentX, &currentY);
184 int x1 = x;
185 int y1 = y;
186 int w1 = width;
187 int h1 = height;
188
189 if (x == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
190 x1 = currentX;
191 if (y == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
192 y1 = currentY;
193
194 // If we're prepared to use the existing size, then...
195 if (width == -1 && height == -1 && ((sizeFlags & wxSIZE_AUTO) != wxSIZE_AUTO))
196 {
197 GetSize(&w1, &h1);
198 }
199
200 // Deal with default size (using -1 values)
201 if (w1<=0)
202 w1 = DEFAULT_ITEM_WIDTH;
203
204 if (h1<=0)
205 h1 = DEFAULT_ITEM_HEIGHT;
206
207 MoveWindow((HWND) GetHWND(), x1, y1, w1, h1, TRUE);
208
209 #if WXWIN_COMPATIBILITY
210 GetEventHandler()->OldOnSize(width, height);
211 #else
212 wxSizeEvent event(wxSize(width, height), m_windowId);
213 event.eventObject = this;
214 GetEventHandler()->ProcessEvent(event);
215 #endif
216 }
217
218 void wxGauge::SetShadowWidth(const int w)
219 {
220 if (m_useProgressBar)
221 {
222 }
223 else
224 SendMessage((HWND) GetHWND(), ZYZG_SETWIDTH3D, w, 0);
225 }
226
227 void wxGauge::SetBezelFace(const int w)
228 {
229 if (m_useProgressBar)
230 {
231 }
232 else
233 SendMessage((HWND) GetHWND(), ZYZG_SETBEZELFACE, w, 0);
234 }
235
236 void wxGauge::SetRange(const int r)
237 {
238 m_rangeMax = r;
239
240 #if defined(__WIN95__) && USE_PROGRESS_BAR
241 if (m_useProgressBar)
242 SendMessage((HWND) GetHWND(), PBM_SETRANGE, 0, MAKELPARAM(0, r));
243 else
244 #endif
245 SendMessage((HWND) GetHWND(), ZYZG_SETRANGE, r, 0);
246 }
247
248 void wxGauge::SetValue(const int pos)
249 {
250 m_gaugePos = pos;
251
252 #if defined(__WIN95__) && USE_PROGRESS_BAR
253 if (m_useProgressBar)
254 SendMessage((HWND) GetHWND(), PBM_SETPOS, pos, 0);
255 else
256 #endif
257 SendMessage((HWND) GetHWND(), ZYZG_SETPOSITION, pos, 0);
258 }
259
260 int wxGauge::GetShadowWidth(void) const
261 {
262 if (m_useProgressBar)
263 return 0;
264 else
265 return (int) SendMessage((HWND) GetHWND(), ZYZG_GETWIDTH3D, 0, 0);
266 }
267
268 int wxGauge::GetBezelFace(void) const
269 {
270 if (m_useProgressBar)
271 return 0;
272 else
273 return (int) SendMessage((HWND) GetHWND(), ZYZG_GETBEZELFACE, 0, 0);
274 }
275
276 int wxGauge::GetRange(void) const
277 {
278 if (m_useProgressBar)
279 return m_rangeMax;
280 else
281 return (int) SendMessage((HWND) GetHWND(), ZYZG_GETRANGE, 0, 0);
282 }
283
284 int wxGauge::GetValue(void) const
285 {
286 if (m_useProgressBar)
287 return m_gaugePos;
288 else
289 return (int) SendMessage((HWND) GetHWND(), ZYZG_GETPOSITION, 0, 0);
290 }
291
292 void wxGauge::SetForegroundColour(const wxColour& col)
293 {
294 m_foregroundColour = col ;
295 if (m_useProgressBar)
296 {
297 }
298 else
299 SendMessage((HWND) GetHWND(), ZYZG_SETFGCOLOR, 0, RGB(col.Red(), col.Green(), col.Blue()));
300 }
301
302 void wxGauge::SetBackgroundColour(const wxColour& col)
303 {
304 m_backgroundColour = col ;
305 if (m_useProgressBar)
306 {
307 }
308 else
309 SendMessage((HWND) GetHWND(), ZYZG_SETBKCOLOR, 0, RGB(col.Red(), col.Green(), col.Blue()));
310 }
311
312
313 /** zyz3d.c
314 *
315 * DESCRIPTION:
316 * This module contains functions for creating nifty 3D borders
317 * around controls like zYzGauge.
318 *
319 * HISTORY:
320 * 3/14/91 cjp put in this comment
321 * 6/19/92 cjp touched it a bit
322 *
323 ** cjp */
324 // COPYRIGHT:
325 //
326 // (C) Copyright Microsoft Corp. 1992. All rights reserved.
327 //
328 // You have a royalty-free right to use, modify, reproduce and
329 // distribute the Sample Files (and/or any modified version) in
330 // any way you find useful, provided that you agree that
331 // Microsoft has no warranty obligations or liability for any
332 // Sample Application Files which are modified.
333 //
334
335
336 /* get the includes we need */
337 #include <windows.h>
338
339 /* misc. control flag defines */
340 #define DRAW3D_IN 0x0001
341 #define DRAW3D_OUT 0x0002
342
343 #define DRAW3D_TOPLINE 0x0004
344 #define DRAW3D_BOTTOMLINE 0x0008
345 #define DRAW3D_LEFTLINE 0x0010
346 #define DRAW3D_RIGHTLINE 0x0020
347
348
349 /* public function prototypes */
350 void FAR PASCAL Draw3DFaceFrame(HDC, LPRECT, WORD);
351 void FAR PASCAL Draw3DRect(HDC, LPRECT, WORD, WORD);
352 void FAR PASCAL Draw3DLine(HDC, WORD, WORD, WORD, WORD, WORD);
353
354
355 /** void FAR PASCAL Draw3DFaceFrame(HDC hdc, LPRECT rc, WORD wWidth)
356 *
357 * DESCRIPTION:
358 * This function draws a flat frame with the current button-face
359 * color.
360 *
361 * ARGUMENTS:
362 * HDC hdc : The DC to draw into.
363 *
364 * LPRECT rc : The containing rect for the new frame.
365 *
366 * WORD wWidth : The width of the frame to draw.
367 *
368 * RETURN (void FAR PASCAL):
369 * The frame will have been drawn into the DC.
370 *
371 * NOTES:
372 *
373 ** cjp */
374
375 void FAR PASCAL Draw3DFaceFrame(HDC hdc, LPRECT rc, WORD wWidth)
376 {
377 RECT rc1;
378 DWORD rgbOld;
379
380 /* don't go through a bunch of work if we don't have to */
381 if (!wWidth)
382 return;
383
384 /* set up color to be button-face color--so it may not be gray */
385 rgbOld = SetBkColor(hdc, GetSysColor(COLOR_BTNFACE));
386
387 /* perform CopyRect w/o bloody windows style overhead */
388 rc1 = *rc;
389
390 /* top */
391 rc1.top = rc->top;
392 rc1.left = rc->left;
393 rc1.bottom = rc->top + wWidth;
394 rc1.right = rc->right;
395
396 /* blast it out */
397 ExtTextOut(hdc, rc1.left, rc1.top, ETO_OPAQUE, &rc1, NULL, 0, NULL);
398
399 /* right */
400 rc1.left = rc->right - wWidth;
401 rc1.bottom = rc->bottom;
402
403 /* blast this part now */
404 ExtTextOut(hdc, rc1.left, rc1.top, ETO_OPAQUE, &rc1, NULL, 0, NULL);
405
406 /* left */
407 rc1.left = rc->left;
408 rc1.right = rc->left + wWidth;
409
410 /* and another part */
411 ExtTextOut(hdc, rc1.left, rc1.top, ETO_OPAQUE, &rc1, NULL, 0, NULL);
412
413 /* bottom */
414 rc1.right = rc->right;
415 rc1.top = rc->bottom - wWidth;
416
417 /* finish it off */
418 ExtTextOut(hdc, rc1.left, rc1.top, ETO_OPAQUE, &rc1, NULL, 0, NULL);
419
420 /* restore the old bk color */
421 SetBkColor(hdc, rgbOld);
422 } /* Draw3DFaceFrame() */
423
424
425 /** void FAR PASCAL Draw3DRect(HDC, LPRECT, WORD, WORD)
426 *
427 * DESCRIPTION:
428 * Draws a 3D rectangle that is shaded. wFlags can be used to
429 * control how the rectangle looks.
430 *
431 * ARGUMENTS:
432 * HDC hdc : Handle to the device context that will be
433 * used to display the rectangle.
434 *
435 * RECT rect : A rectangle describing the dimensions of
436 * the rectangle in device coordinates.
437 *
438 * WORD wShadowWidth : Width of the shadow in device coordinates.
439 *
440 * WORD wFlags : The following flags may be passed to describe
441 * the style of the rectangle:
442 *
443 * DRAW3D_IN : The shadow is drawn such that
444 * the box appears to be sunk in to the screen.
445 * This is default if 0 is passed.
446 *
447 * DRAW3D_OUT : The shadow is drawn such that
448 * the box appears to be sticking out of the
449 * screen.
450 *
451 * RETURN (void FAR PASCAL):
452 * The 3D looking rectangle will have been drawn into the DC.
453 *
454 * NOTES:
455 *
456 ** cjp */
457
458 void FAR PASCAL Draw3DRect(HDC hdc, LPRECT lpRect,
459 WORD wShadowWidth, WORD wFlags)
460 {
461 /* sanity check--don't work if you don't have to! */
462 if (!wShadowWidth || !RectVisible(hdc, lpRect))
463 return;
464
465 /* draw the top line */
466 Draw3DLine(hdc, lpRect->left, lpRect->top,
467 lpRect->right - lpRect->left,
468 wShadowWidth, DRAW3D_TOPLINE | wFlags);
469
470 /* right line */
471 Draw3DLine(hdc, lpRect->right, lpRect->top,
472 lpRect->bottom - lpRect->top,
473 wShadowWidth, DRAW3D_RIGHTLINE | wFlags);
474
475 /* bottom line */
476 Draw3DLine(hdc, lpRect->left, lpRect->bottom,
477 lpRect->right - lpRect->left,
478 wShadowWidth, DRAW3D_BOTTOMLINE | wFlags);
479
480 /* left line */
481 Draw3DLine(hdc, lpRect->left, lpRect->top,
482 lpRect->bottom - lpRect->top,
483 wShadowWidth, DRAW3D_LEFTLINE | wFlags);
484 } /* Draw3DRect() */
485
486
487 /** void FAR PASCAL Draw3DLine(HDC hdc, WORD x, WORD y, WORD nLen,
488 *
489 * DESCRIPTION:
490 * Draws a 3D line that can be used to make a 3D box.
491 *
492 * ARGUMENTS:
493 * HDC hdc : Handle to the device context that will be
494 * used to display the 3D line.
495 *
496 * WORD x, y : Coordinates of the beginning of the line.
497 * These coordinates are in device units and
498 * represent the _outside_ most point. Horiz-
499 * ontal lines are drawn from left to right and
500 * vertical lines are drawn from top to bottom.
501 *
502 * WORD wShadowWidth : Width of the shadow in device coordinates.
503 *
504 * WORD wFlags : The following flags may be passed to
505 * describe the style of the 3D line:
506 *
507 * DRAW3D_IN : The shadow is drawn such that
508 * the box appears to be sunk in to the screen.
509 * This is default if 0 is passed.
510 *
511 * DRAW3D_OUT : The shadow is drawn such that
512 * the box appears to be sticking out of the
513 * screen.
514 *
515 * DRAW3D_TOPLINE, _BOTTOMLINE, _LEFTLINE, and
516 * _RIGHTLINE : Specifies that a "top",
517 * "Bottom", "Left", or"Right" line is to be
518 * drawn.
519 *
520 * RETURN (void FAR PASCAL):
521 * The line will have been drawn into the DC.
522 *
523 * NOTES:
524 *
525 ** cjp */
526
527 void FAR PASCAL Draw3DLine(HDC hdc, WORD x, WORD y, WORD nLen,
528 WORD wShadowWidth, WORD wFlags)
529 {
530 HBRUSH hOldBrush;
531 HPEN hOldPen;
532 BOOL fDark;
533 POINT Point[ 4 ]; /* define a polgon with 4 points */
534
535 /* if width is zero, don't do nothin'! */
536 if (!wShadowWidth)
537 return;
538
539 /* define shape of polygon--origin is always the same */
540 Point[0].x = x;
541 Point[0].y = y;
542
543 /* To do this we'll simply draw a polygon with four sides, using
544 * the appropriate brush. I dare you to ask me why this isn't a
545 * switch/case!
546 */
547 if (wFlags & DRAW3D_TOPLINE)
548 {
549 /* across to right */
550 Point[1].x = x + nLen - (wShadowWidth == 1 ? 1 : 0);
551 Point[1].y = y;
552
553 /* down/left */
554 Point[2].x = x + nLen - wShadowWidth;
555 Point[2].y = y + wShadowWidth;
556
557 /* accross to left */
558 Point[3].x = x + wShadowWidth;
559 Point[3].y = y + wShadowWidth;
560
561 /* select 'dark' brush if 'in'--'light' for 'out' */
562 fDark = (wFlags & DRAW3D_IN) ? TRUE : FALSE;
563 }
564
565 /* possibly the bottom? */
566 else if (wFlags & DRAW3D_BOTTOMLINE)
567 {
568 /* across to right */
569 Point[1].x = x + nLen;
570 Point[1].y = y;
571
572 /* up/left */
573 Point[2].x = x + nLen - wShadowWidth;
574 Point[2].y = y - wShadowWidth;
575
576 /* accross to left */
577 Point[3].x = x + wShadowWidth;
578 Point[3].y = y - wShadowWidth;
579
580 /* select 'light' brush if 'in' */
581 fDark = (wFlags & DRAW3D_IN) ? FALSE : TRUE;
582 }
583
584 /* ok, it's gotta be left? */
585 else if (wFlags & DRAW3D_LEFTLINE)
586 {
587 /* down */
588 Point[1].x = x;
589 Point[1].y = y + nLen - (wShadowWidth == 1 ? 1 : 0);
590
591 /* up/right */
592 Point[2].x = x + wShadowWidth;
593 Point[2].y = y + nLen - wShadowWidth;
594
595 /* down */
596 Point[3].x = x + wShadowWidth;
597 Point[3].y = y + wShadowWidth;
598
599 /* select 'dark' brush if 'in'--'light' for 'out' */
600 fDark = (wFlags & DRAW3D_IN) ? TRUE : FALSE;
601 }
602
603 /* well maybe it's for the right side? */
604 else if (wFlags & DRAW3D_RIGHTLINE)
605 {
606 /* down */
607 Point[1].x = x;
608 Point[1].y = y + nLen;
609
610 /* up/left */
611 Point[2].x = x - wShadowWidth;
612 Point[2].y = y + nLen - wShadowWidth;
613
614 /* up */
615 Point[3].x = x - wShadowWidth;
616 Point[3].y = y + wShadowWidth;
617
618 /* select 'light' brush if 'in' */
619 fDark = (wFlags & DRAW3D_IN) ? FALSE : TRUE;
620 }
621
622 /* bad drugs? */
623 else return;
624
625 /* select NULL_PEN for no borders */
626 hOldPen = SelectObject(hdc, GetStockObject(NULL_PEN));
627
628 /* select the appropriate color for the fill */
629 if (fDark)
630 hOldBrush = SelectObject(hdc, GetStockObject(GRAY_BRUSH));
631 else
632 hOldBrush = SelectObject(hdc, GetStockObject(WHITE_BRUSH));
633
634 /* finally, draw the dern thing */
635 Polygon(hdc, (LPPOINT)&Point, 4);
636
637 /* restore what we killed */
638 SelectObject(hdc, hOldBrush);
639 SelectObject(hdc, hOldPen);
640 } /* Draw3DLine() */
641
642 /** EOF: zyz3d.c **/
643
644 /** zyzgauge.c
645 *
646 * DESCRIPTION:
647 * Yet another 'Gas Gauge Custom Control.' This control gives you
648 * a 'progress bar' class (named zYzGauge) for use in your applications.
649 * You can set the range, position, font, color, orientation, and 3d
650 * effect of the gauge by sending messages to the control.
651 *
652 * Before you can use this control, you MUST first export the window
653 * procedure for the control (or define it with the _export keyword):
654 *
655 * EXPORTS gaugeWndProc
656 *
657 * You then need initialize the class before you use it:
658 *
659 * if (!gaugeInit(hInstance))
660 * die a horrible death
661 * else
662 * you are good to go
663 *
664 * The colors used by the control default to black and white if you
665 * are running on a mono-display. They default to blue and white
666 * if you are on a color display. You enable the 3D effect by setting
667 * the ZYZGS_3D style flag in the styles field of the control (like
668 * any other control).
669 *
670 * To select your own colors, you can send the ZYZG_SETFGCOLOR and
671 * ZYZG_SETBKCOLOR messages to set the foreground (percent done) and
672 * background (percent not done) colors. The lParam is the RGB()
673 * value--wParam is ignored.
674 *
675 * In all of the following ZYZG_??? messages, the arguments are
676 * WORDS. If you are setting parameters, the value is sent as
677 * the wParam (lParam is ignored). If you are getting parameters,
678 * the value is returned as a LONG and should be cast to a *signed*
679 * integer.
680 *
681 * To set the depth of the 3D effect (if enabled), you can send the
682 * ZYZG_SETBEZELFACE and ZYZG_SETWIDTH3D messages. The bezel face
683 * is the flat top on the 3D border--its color will be that of the
684 * button-face. The 3D width is the width of the bezel itself; inside
685 * and outside. The light color is white, the dark color is gray.
686 * Both widths *can* be zero--both default to 2 which looks to me.
687 *
688 * The range of the control can be set by sending the ZYZG_SETRANGE
689 * message to the control. It can be any integer from 1 to 32767.
690 * What this specifies is the number of pieces that create a whole.
691 * The default is 100. You can get the current range setting by
692 * sending the ZYZG_GETRANGE message to the control.
693 *
694 * The position (number of pieces out of the whole have been used) is
695 * set with the ZYZG_SETPOSITION message. It can be any integer from
696 * 0 to the current range setting of the control--it will be clipped
697 * if the position is out of bounds. The default position is 0. You
698 * can get the current position at any time with the ZYZG_GETPOSITION
699 * message.
700 *
701 * You can also set the range using a delta from the current range.
702 * This is done by sending the ZYZG_SETDELTAPOS message with wParam
703 * set to a _signed_ integer value within the range of the control.
704 *
705 * The font used for the percentage text can be set using the standard
706 * WM_SETFONT message. You can get the current font at any time with
707 * the WM_GETFONT message.
708 *
709 * The orientation can be left to right, right to left, bottom to top,
710 * or top to bottom. Whatever suits your needs. You set this by
711 * sending the ZYZG_ORIENTATION message to the control with one of
712 * the following values (default is ZYZG_ORIENT_LEFTTORIGHT):
713 *
714 * ZYZG_ORIENT_LEFTTORIGHT (0)
715 * ZYZG_ORIENT_RIGHTTOLEFT (1)
716 * ZYZG_ORIENT_BOTTOMTOTOP (2)
717 * ZYZG_ORIENT_TOPTOBOTTOM (3)
718 *
719 * HISTORY:
720 * 3/12/91 cjp put in this comment
721 * 6/19/92 cjp touched it a bit
722 *
723 ** cjp */
724 // COPYRIGHT:
725 //
726 // (C) Copyright Microsoft Corp. 1992. All rights reserved.
727 //
728 // You have a royalty-free right to use, modify, reproduce and
729 // distribute the Sample Files (and/or any modified version) in
730 // any way you find useful, provided that you agree that
731 // Microsoft has no warranty obligations or liability for any
732 // Sample Application Files which are modified.
733 //
734
735
736 /* get the includes we need */
737 #ifndef __GNUWIN32__
738 #include <malloc.h>
739 #endif
740 #include <stdio.h>
741 #include <string.h>
742 #include <stdlib.h>
743 // #include "zyz3d.h"
744 // #include "zyzgauge.h"
745
746
747 /* static global variables */
748 static char gszzYzGaugeClass[] = "zYzGauge";
749
750
751 /* window word position definitions */
752 #define ZYZG_WW_PZYZGAUGE 0
753 /* #define ZYZG_WW_EXTRABYTES 2 */
754 #define ZYZG_WW_EXTRABYTES 4
755
756
757 /* control block structure typedef */
758 typedef struct tZYZGAUGE
759 {
760 WORD wRange;
761 WORD wPosition;
762 WORD wOrientation;
763 WORD wWidth3D;
764 WORD wWidthBezelFace;
765 HFONT hFont;
766 DWORD rgbTextColor;
767 DWORD rgbBkColor;
768
769 } ZYZGAUGE, *PZYZGAUGE, FAR *LPZYZGAUGE;
770
771
772 /* some default values for the control */
773 #define ZYZG_DEF_RANGE 100
774 #define ZYZG_DEF_POSITION 0
775 #define ZYZG_DEF_ORIENTATION ZYZG_ORIENT_LEFTTORIGHT
776 #define ZYZG_DEF_WIDTH3D 2
777 #define ZYZG_DEF_BEZELFACE 2
778
779
780
781 /* the default settings for drawing colors--display dependent */
782 static DWORD rgbDefTextColor;
783 static DWORD rgbDefBkColor;
784 static BOOL fSupport3D;
785
786 #if !defined(APIENTRY) // NT defines APIENTRY, 3.x not
787 #define APIENTRY FAR PASCAL
788 #endif
789
790 #ifdef __WIN32__
791 #define _EXPORT /**/
792 #else
793 #define _EXPORT _export
794 typedef signed short int SHORT ;
795 #endif
796
797 /* internal function prototypes */
798 static void PASCAL gaugePaint(HWND, HDC);
799 /* LRESULT FAR PASCAL */
800 LRESULT APIENTRY _EXPORT gaugeWndProc(HWND, UINT, WPARAM, LPARAM);
801
802
803
804 /** BOOL FAR PASCAL gaugeInit(HINSTANCE hInstance)
805 *
806 * DESCRIPTION:
807 * Registers the window class for the zYzGauge control. Performs
808 * other initialization for the zYzGauge text control. This must
809 * be done before the zYzGauge control is used--or it will fail
810 * and your dialog box will not open!
811 *
812 * ARGUMENTS:
813 * HINSTANCE hInstance : Instance handle to register class with.
814 *
815 * RETURN (BOOL FAR):
816 * The return value is TRUE if the zYzGauge class was successfully
817 * registered. It is FALSE if the initialization fails.
818 *
819 * NOTES:
820 *
821 ** cjp */
822
823 //#pragma alloc_text(init, gaugeInit)
824
825 BOOL FAR PASCAL gaugeInit(HINSTANCE hInstance)
826 {
827 static BOOL fRegistered = FALSE;
828 WNDCLASS wc;
829 HDC hdc;
830
831 /* assume already registered if not first instance */
832 if (fRegistered)
833 return (TRUE);
834
835 /* fill in the class structure for the zyzgauge control */
836 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
837 wc.hIcon = NULL;
838 wc.lpszMenuName = NULL;
839 wc.lpszClassName = gszzYzGaugeClass;
840 wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
841 wc.hInstance = hInstance;
842
843 #ifdef ZYZGAUGE_DLL
844 wc.style = CS_GLOBALCLASS | CS_HREDRAW | CS_VREDRAW;
845 #else
846 wc.style = CS_HREDRAW | CS_VREDRAW;
847 #endif
848
849 wc.lpfnWndProc = gaugeWndProc;
850 wc.cbClsExtra = 0;
851 wc.cbWndExtra = ZYZG_WW_EXTRABYTES;
852
853 /* attempt to register it--return FALSE if fail */
854 if (!RegisterClass(&wc))
855 return (FALSE);
856
857 /* Get a DC to determine whether device is mono or not, and set
858 * default foreground/background colors as appropriate.
859 */
860 if ((hdc = CreateIC("DISPLAY", NULL, NULL, 0L)))
861 {
862 /* check for mono-display */
863 if ((GetDeviceCaps(hdc, BITSPIXEL) == 1) &&
864 (GetDeviceCaps(hdc, PLANES) == 1))
865 {
866 /* using a mono DC--white foreground, black background */
867 rgbDefTextColor = RGB(255, 255, 255);
868 rgbDefBkColor = RGB(0, 0, 0);
869 }
870
871 /* good! we have color: blue foreground, white background */
872 else
873 {
874 rgbDefTextColor = RGB(0, 0, 255);
875 rgbDefBkColor = RGB(255, 255, 255);
876 }
877
878 /* need at _least_ 8 for two shades of gray (>=VGA) */
879 fSupport3D = (GetDeviceCaps(hdc, NUMCOLORS) >= 8) ? TRUE : FALSE;
880
881 /* get rid of the DC (IC) */
882 DeleteDC(hdc);
883 }
884
885 /* uh-oh... can't get DC (IC)... fail */
886 else
887 {
888 /* unregister the class */
889 UnregisterClass(gszzYzGaugeClass, hInstance);
890 return (FALSE);
891 }
892
893 /* return success */
894 return (fRegistered = TRUE);
895 } /* gaugeInit() */
896
897
898 /** static void PASCAL gaugePaint(HWND hwnd, HDC hdc)
899 *
900 * DESCRIPTION:
901 * This function is responsible for painting the zYzGauge control.
902 *
903 * ARGUMENTS:
904 * HWND hwnd : The window handle for the gauge.
905 *
906 * HDC hdc : The DC for the gauge's window.
907 *
908 * RETURN (void):
909 * The control will have been painted.
910 *
911 * NOTES:
912 *
913 ** cjp */
914
915 static void PASCAL gaugePaint(HWND hwnd, HDC hdc)
916 {
917 PZYZGAUGE pgauge;
918 WORD iRange, iPos;
919 WORD Offset = 1;
920 DWORD dwExtent;
921 RECT rc1, rc2;
922 HFONT hFont;
923 char ach[ 6 ];
924 WORD dx, dy, wGomerX, wGomerY;
925 /* Win32s has no GetTextExtent(); let's try GetTextExtentPoint() instead,
926 * which needs a SIZE* parameter */
927 #if defined(__WIN32__)
928 SIZE size;
929 #endif
930
931 /* get pointer to the control's control block */
932 // pgauge = (PZYZGAUGE)GetWindowWord(hwnd, ZYZG_WW_PZYZGAUGE);
933 pgauge = (PZYZGAUGE)GetWindowLong(hwnd, ZYZG_WW_PZYZGAUGE);
934
935 /* set the colors into for the gauge into the control */
936 SetTextColor(hdc, pgauge->rgbTextColor);
937 SetBkColor(hdc, pgauge->rgbBkColor);
938
939 /* draw black rectangle for gauge */
940 GetClientRect(hwnd, &rc1);
941
942 /* draw a black border on the _outside_ */
943 FrameRect(hdc, &rc1, GetStockObject(BLACK_BRUSH));
944
945 /* we want to draw _just inside_ the black border */
946 InflateRect(&rc1, -1, -1);
947
948 /* one line thick so far... */
949 // Offset = (WORD) 1;
950
951 /* for 3D stuff, we need to have at least two shades of gray */
952 if ((GetWindowLong(hwnd, GWL_STYLE) & ZYZGS_3D) && fSupport3D)
953 {
954 Draw3DRect(hdc, &rc1, pgauge->wWidth3D, DRAW3D_OUT);
955 InflateRect(&rc1, ~(pgauge->wWidth3D), ~(pgauge->wWidth3D));
956
957 Draw3DFaceFrame(hdc, &rc1, pgauge->wWidthBezelFace);
958 InflateRect(&rc1, ~(pgauge->wWidthBezelFace), ~(pgauge->wWidthBezelFace));
959
960 Draw3DRect(hdc, &rc1, pgauge->wWidth3D, DRAW3D_IN);
961 InflateRect(&rc1, ~(pgauge->wWidth3D), ~(pgauge->wWidth3D));
962
963 /* draw a black border on the _inside_ */
964 FrameRect(hdc, &rc1, GetStockObject(BLACK_BRUSH));
965
966 /* we want to draw _just inside_ the black border */
967 InflateRect(&rc1, -1, -1);
968
969 /* add all the other pixels into the border width */
970 Offset += (2 * pgauge->wWidth3D) + pgauge->wWidthBezelFace + 1;
971 }
972
973 /* dup--one rc for 'how much filled', one rc for 'how much empty' */
974 rc2 = rc1;
975
976 /* get the range--make sure it's a valid range */
977 if ((iRange = pgauge->wRange) <= 0)
978 iRange = 1;
979
980 /* get the position--greater than 100% would be bad */
981 if ((iPos = pgauge->wPosition) > iRange)
982 iPos = iRange;
983
984 /* compute the actual size of the gauge */
985 dx = rc1.right - rc1.left;
986 dy = rc1.bottom - rc1.top;
987 wGomerX = (WORD)((DWORD)iPos * dx / iRange);
988 wGomerY = (WORD)((DWORD)iPos * dy / iRange);
989
990 /* get the orientation and munge rects accordingly */
991 switch (pgauge->wOrientation)
992 {
993 case ZYZG_ORIENT_RIGHTTOLEFT:
994 rc1.left = rc2.right = rc1.right - wGomerX;
995 break;
996
997 case ZYZG_ORIENT_BOTTOMTOTOP:
998 rc1.top = rc2.bottom = rc1.bottom - wGomerY;
999 break;
1000
1001 case ZYZG_ORIENT_TOPTOBOTTOM:
1002 rc1.bottom = rc2.top += wGomerY;
1003 break;
1004
1005 default:
1006 rc1.right = rc2.left += wGomerX;
1007 break;
1008 } /* switch () */
1009
1010 /* select the correct font */
1011 hFont = SelectObject(hdc, pgauge->hFont);
1012
1013 /* build up a string to blit out--ie the meaning of life: "42%" */
1014 wsprintf(ach, "%3d%%", (WORD)((DWORD)iPos * 100 / iRange));
1015 /* Win32s has no GetTextExtent(); let's try GetTextExtentPoint() instead */
1016 #if defined(__WIN32__)
1017 GetTextExtentPoint(hdc, ach, wGomerX = lstrlen(ach), &size);
1018 dwExtent = size.cx;
1019 #else
1020 dwExtent = GetTextExtent(hdc, ach, wGomerX = lstrlen(ach));
1021 #endif
1022
1023
1024 /* Draw the finished (ie the percent done) side of box. If
1025 * ZYZG_WW_POSITION is 42, (in range of 0 to 100) this ExtTextOut
1026 * draws the meaning of life (42%) bar.
1027 */
1028 ExtTextOut(hdc, (dx - LOWORD(dwExtent)) / 2 + Offset,
1029 (dy - HIWORD(dwExtent)) / 2 + Offset,
1030 ETO_OPAQUE | ETO_CLIPPED, &rc2, ach, wGomerX, NULL);
1031
1032 /* Reverse fore and back colors for drawing the undone (ie the non-
1033 * finished) side of the box.
1034 */
1035 SetBkColor(hdc, pgauge->rgbTextColor);
1036 SetTextColor(hdc, pgauge->rgbBkColor);
1037
1038 ExtTextOut(hdc, (dx - LOWORD(dwExtent)) / 2 + Offset,
1039 (dy - HIWORD(dwExtent)) / 2 + Offset,
1040 ETO_OPAQUE | ETO_CLIPPED, &rc1, ach, wGomerX, NULL);
1041
1042 /* unselect the font */
1043 SelectObject(hdc, hFont);
1044 } /* gaugePaint() */
1045
1046
1047 /** LRESULT FAR PASCAL gaugeWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1048 *
1049 * DESCRIPTION:
1050 * This is the control's window procedure. Its purpose is to handle
1051 * special messages for this custom control.
1052 *
1053 * The special control messages for the gauge control are:
1054 *
1055 * ZYZG_SETRANGE : Sets the range of the gauge. In other
1056 * words, the number of parts that make a
1057 * whole.
1058 *
1059 * ZYZG_GETRANGE : Returns the current range of the gauge.
1060 *
1061 * ZYZG_SETORIENTATION : Sets the orientation of the gauge. This
1062 * can be one of the ZYZG_ORIENT_?? msgs.
1063 *
1064 * ZYZG_GETORIENTATION : Gets the current orientation of the
1065 * gauge.
1066 *
1067 * ZYZG_SETPOSITION : Sets the current position of the gauge.
1068 * In other words, how many pieces of the
1069 * whole have been used.
1070 *
1071 * ZYZG_GETPOSITION : Gets the current position of the gauge.
1072 *
1073 * ZYZG_SETDELTAPOS : Sets the position of the gauge +/- the
1074 * specified amount.
1075 *
1076 * ZYZG_SETFGCOLOR : Sets the foreground (percent done) color.
1077 *
1078 * ZYZG_GETFGCOLOR : Gets the foreground (percent done) color.
1079 *
1080 * ZYZG_SETBKCOLOR : Sets the background (percent not done)
1081 * color.
1082 *
1083 * ZYZG_GETBKCOLOR : Gets the background (percent not done)
1084 * color.
1085 *
1086 * WM_SETFONT : Sets the font to use for the percentage
1087 * text of the gauge.
1088 *
1089 * WM_GETFONT : Gets the current font in use by the
1090 * gauge.
1091 *
1092 * NOTES:
1093 *
1094 ** cjp */
1095
1096 /* LRESULT FAR PASCAL */
1097
1098 LRESULT APIENTRY _EXPORT gaugeWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1099 {
1100 HFONT hFont;
1101 PAINTSTRUCT ps;
1102 PZYZGAUGE pgauge;
1103 RECT rc;
1104
1105 // pgauge = (PZYZGAUGE)GetWindowWord(hwnd, ZYZG_WW_PZYZGAUGE);
1106 pgauge = (PZYZGAUGE)GetWindowLong(hwnd, ZYZG_WW_PZYZGAUGE);
1107
1108 /* break to get DefWindowProc() */
1109 switch (uMsg)
1110 {
1111 case WM_CREATE:
1112 /* need to allocate a control block */
1113 // pgauge = (PZYZGAUGE)LocalAlloc(LPTR, sizeof(ZYZGAUGE));
1114 pgauge = (PZYZGAUGE)malloc(sizeof(ZYZGAUGE));
1115 if (!pgauge)
1116 return (0L);
1117
1118 /* hang on to this control block */
1119 // SetWindowWord(hwnd, ZYZG_WW_PZYZGAUGE, (WORD)pgauge);
1120 SetWindowLong(hwnd, ZYZG_WW_PZYZGAUGE, (LONG)pgauge);
1121
1122 /* fill control block with defaults */
1123 pgauge->wRange = ZYZG_DEF_RANGE;
1124 pgauge->wPosition = ZYZG_DEF_POSITION;
1125 pgauge->wOrientation = ZYZG_DEF_ORIENTATION;
1126 pgauge->wWidth3D = ZYZG_DEF_WIDTH3D;
1127 pgauge->wWidthBezelFace = ZYZG_DEF_BEZELFACE;
1128 pgauge->rgbTextColor = rgbDefTextColor;
1129 pgauge->rgbBkColor = rgbDefBkColor;
1130
1131 /* use system font */
1132 SendMessage(hwnd, WM_SETFONT, (WPARAM)NULL, 0L);
1133
1134 /* go to DefWindowProc() to finish the job */
1135 break;
1136
1137 case WM_DESTROY:
1138 /* get rid of the control's memory */
1139 if (pgauge)
1140 // LocalFree((HANDLE)pgauge);
1141 free(pgauge);
1142 break;
1143
1144 case ZYZG_GETPOSITION:
1145 return (pgauge->wPosition);
1146
1147 case ZYZG_GETRANGE:
1148 return (pgauge->wRange);
1149
1150 case ZYZG_GETORIENTATION:
1151 return (pgauge->wOrientation);
1152
1153 case ZYZG_GETWIDTH3D:
1154 return (pgauge->wWidth3D);
1155
1156 case ZYZG_GETBEZELFACE:
1157 return (pgauge->wWidthBezelFace);
1158
1159 case ZYZG_GETBKCOLOR:
1160 return (pgauge->rgbTextColor);
1161
1162 case ZYZG_GETFGCOLOR:
1163 return (pgauge->rgbBkColor);
1164
1165 case ZYZG_SETBKCOLOR:
1166 pgauge->rgbBkColor = lParam;
1167 return (0L);
1168
1169 case ZYZG_SETFGCOLOR:
1170 pgauge->rgbTextColor = lParam;
1171 return (0L);
1172
1173
1174 case ZYZG_SETPOSITION:
1175 pgauge->wPosition = wParam;
1176
1177 zyzgForceRepaint:
1178 GetClientRect(hwnd, &rc);
1179 if ((GetWindowLong(hwnd, GWL_STYLE) & ZYZGS_3D) && fSupport3D)
1180 {
1181 wParam = (2 * pgauge->wWidth3D) +
1182 pgauge->wWidthBezelFace + 2;
1183 }
1184
1185 else
1186 wParam = 1;
1187
1188 InflateRect(&rc, ~(wParam), ~(wParam));
1189 InvalidateRect(hwnd, &rc, FALSE);
1190 UpdateWindow(hwnd);
1191 return (0L);
1192
1193 case ZYZG_SETRANGE:
1194 pgauge->wRange = wParam;
1195 goto zyzgForceRepaint;
1196
1197 case ZYZG_SETORIENTATION:
1198 pgauge->wOrientation = wParam;
1199 goto zyzgForceRepaint;
1200
1201 case ZYZG_SETWIDTH3D:
1202 pgauge->wWidth3D = wParam;
1203
1204 zyzgForceRepaint3D:
1205 InvalidateRect(hwnd, NULL, FALSE);
1206 UpdateWindow(hwnd);
1207 return (0L);
1208
1209 case ZYZG_SETBEZELFACE:
1210 pgauge->wWidthBezelFace = wParam;
1211 goto zyzgForceRepaint3D;
1212
1213 case ZYZG_SETDELTAPOS:
1214 /* Watcom doesn't like the following line so removing typecasts */
1215 /* (int)pgauge->wPosition += (int)wParam; */
1216 pgauge->wPosition += wParam;
1217 goto zyzgForceRepaint;
1218
1219 case WM_PAINT:
1220 BeginPaint(hwnd, &ps);
1221 gaugePaint(hwnd, ps.hdc);
1222 EndPaint(hwnd, &ps);
1223 return (0L);
1224
1225 case WM_GETFONT:
1226 hFont = pgauge->hFont;
1227
1228 /* if system font, then return NULL handle */
1229 return (long)((hFont == GetStockObject(SYSTEM_FONT)) ? NULL : hFont);
1230
1231 case WM_SETFONT:
1232 /* if NULL hFont, use system font */
1233 if (!(hFont = (HFONT)wParam))
1234 hFont = GetStockObject(SYSTEM_FONT);
1235
1236 pgauge->hFont = hFont;
1237
1238 /* redraw if indicated in message */
1239 if ((BOOL)lParam)
1240 {
1241 InvalidateRect(hwnd, NULL, TRUE);
1242 UpdateWindow(hwnd);
1243 }
1244 return (0L);
1245 } /* switch () */
1246
1247 /* let the dialog mangler take care of this message */
1248 return (DefWindowProc(hwnd, uMsg, wParam, lParam));
1249 } /* gaugeWndProc() */
1250
1251
1252 /** EOF: zyzgauge.c **/
1253
1254 #endif // USE_GAUGE