]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/gaugemsw.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxGaugeMSW class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "gauge.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
29 #include "wx/msw/gaugemsw.h"
30 #include "wx/msw/private.h"
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)
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
57 #define ZYZGS_3D 0x8000L /* control will be 3D */
59 /* public function prototypes */
60 BOOL FAR PASCAL
gaugeInit(HINSTANCE hInstance
);
62 #if !USE_SHARED_LIBRARY
63 IMPLEMENT_DYNAMIC_CLASS(wxGaugeMSW
, wxControl
)
66 bool wxGaugeMSW::Create(wxWindow
*parent
, wxWindowID id
,
71 const wxValidator
& validator
,
74 static bool wxGaugeMSWInitialised
= FALSE
;
76 if ( !wxGaugeMSWInitialised
)
78 if (!gaugeInit((HINSTANCE
) wxGetInstance()))
79 wxFatalError("Cannot initalize Gauge library");
80 wxGaugeMSWInitialised
= TRUE
;
84 SetValidator(validator
);
86 if (parent
) parent
->AddChild(this);
90 SetBackgroundColour(parent
->GetDefaultBackgroundColour()) ;
91 SetForegroundColour(parent
->GetDefaultForegroundColour()) ;
93 m_windowStyle
= style
;
96 m_windowId
= (int)NewControlId();
105 long msFlags
= WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
;
109 CreateWindowEx(MakeExtendedStyle(m_windowStyle
), "zYzGauge", NULL
, msFlags
,
110 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
111 wxGetInstance(), NULL
);
113 m_hWnd
= (WXHWND
)wx_button
;
115 // Subclass again for purposes of dialog editing mode
116 SubclassWin((WXHWND
)wx_button
);
120 if (m_windowStyle
& wxGA_HORIZONTAL
)
121 wOrient
= ZYZG_ORIENT_LEFTTORIGHT
;
123 wOrient
= ZYZG_ORIENT_BOTTOMTOTOP
;
125 SendMessage(wx_button
, ZYZG_SETORIENTATION
, wOrient
, 0);
126 SendMessage(wx_button
, ZYZG_SETRANGE
, range
, 0);
128 SendMessage((HWND
) GetHWND(), ZYZG_SETFGCOLOR
, 0, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
129 SendMessage((HWND
) GetHWND(), ZYZG_SETBKCOLOR
, 0, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
131 SetFont(* parent
->GetFont());
137 SetSize(x
, y
, width
, height
);
139 ShowWindow((HWND
) GetHWND(), SW_SHOW
);
144 void wxGaugeMSW::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
146 int currentX
, currentY
;
147 GetPosition(¤tX
, ¤tY
);
153 if (x
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
155 if (y
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
158 AdjustForParentClientOrigin(x1
, y1
, sizeFlags
);
160 // If we're prepared to use the existing size, then...
161 if (width
== -1 && height
== -1 && ((sizeFlags
& wxSIZE_AUTO
) != wxSIZE_AUTO
))
166 // Deal with default size (using -1 values)
168 w1
= DEFAULT_ITEM_WIDTH
;
171 h1
= DEFAULT_ITEM_HEIGHT
;
173 MoveWindow((HWND
) GetHWND(), x1
, y1
, w1
, h1
, TRUE
);
176 void wxGaugeMSW::SetShadowWidth(int w
)
178 SendMessage((HWND
) GetHWND(), ZYZG_SETWIDTH3D
, w
, 0);
181 void wxGaugeMSW::SetBezelFace(int w
)
183 SendMessage((HWND
) GetHWND(), ZYZG_SETBEZELFACE
, w
, 0);
186 void wxGaugeMSW::SetRange(int r
)
190 SendMessage((HWND
) GetHWND(), ZYZG_SETRANGE
, r
, 0);
193 void wxGaugeMSW::SetValue(int pos
)
197 SendMessage((HWND
) GetHWND(), ZYZG_SETPOSITION
, pos
, 0);
200 int wxGaugeMSW::GetShadowWidth(void) const
202 return (int) SendMessage((HWND
) GetHWND(), ZYZG_GETWIDTH3D
, 0, 0);
205 int wxGaugeMSW::GetBezelFace(void) const
207 return (int) SendMessage((HWND
) GetHWND(), ZYZG_GETBEZELFACE
, 0, 0);
210 int wxGaugeMSW::GetRange(void) const
212 return (int) SendMessage((HWND
) GetHWND(), ZYZG_GETRANGE
, 0, 0);
215 int wxGaugeMSW::GetValue(void) const
217 return (int) SendMessage((HWND
) GetHWND(), ZYZG_GETPOSITION
, 0, 0);
220 void wxGaugeMSW::SetForegroundColour(const wxColour
& col
)
222 m_foregroundColour
= col
;
223 SendMessage((HWND
) GetHWND(), ZYZG_SETFGCOLOR
, 0, RGB(col
.Red(), col
.Green(), col
.Blue()));
226 void wxGaugeMSW::SetBackgroundColour(const wxColour
& col
)
228 m_backgroundColour
= col
;
229 SendMessage((HWND
) GetHWND(), ZYZG_SETBKCOLOR
, 0, RGB(col
.Red(), col
.Green(), col
.Blue()));
236 * This module contains functions for creating nifty 3D borders
237 * around controls like zYzGauge.
240 * 3/14/91 cjp put in this comment
241 * 6/19/92 cjp touched it a bit
246 // (C) Copyright Microsoft Corp. 1992. All rights reserved.
248 // You have a royalty-free right to use, modify, reproduce and
249 // distribute the Sample Files (and/or any modified version) in
250 // any way you find useful, provided that you agree that
251 // Microsoft has no warranty obligations or liability for any
252 // Sample Application Files which are modified.
256 /* get the includes we need */
259 /* misc. control flag defines */
260 #define DRAW3D_IN 0x0001
261 #define DRAW3D_OUT 0x0002
263 #define DRAW3D_TOPLINE 0x0004
264 #define DRAW3D_BOTTOMLINE 0x0008
265 #define DRAW3D_LEFTLINE 0x0010
266 #define DRAW3D_RIGHTLINE 0x0020
269 /* public function prototypes */
270 void FAR PASCAL
Draw3DFaceFrame(HDC
, LPRECT
, WORD
);
271 void FAR PASCAL
Draw3DRect(HDC
, LPRECT
, WORD
, WORD
);
272 void FAR PASCAL
Draw3DLine(HDC
, WORD
, WORD
, WORD
, WORD
, WORD
);
275 /** void FAR PASCAL Draw3DFaceFrame(HDC hdc, LPRECT rc, WORD wWidth)
278 * This function draws a flat frame with the current button-face
282 * HDC hdc : The DC to draw into.
284 * LPRECT rc : The containing rect for the new frame.
286 * WORD wWidth : The width of the frame to draw.
288 * RETURN (void FAR PASCAL):
289 * The frame will have been drawn into the DC.
295 void FAR PASCAL
Draw3DFaceFrame(HDC hdc
, LPRECT rc
, WORD wWidth
)
300 /* don't go through a bunch of work if we don't have to */
304 /* set up color to be button-face color--so it may not be gray */
305 rgbOld
= SetBkColor(hdc
, GetSysColor(COLOR_BTNFACE
));
307 /* perform CopyRect w/o bloody windows style overhead */
313 rc1
.bottom
= rc
->top
+ wWidth
;
314 rc1
.right
= rc
->right
;
317 ExtTextOut(hdc
, rc1
.left
, rc1
.top
, ETO_OPAQUE
, &rc1
, NULL
, 0, NULL
);
320 rc1
.left
= rc
->right
- wWidth
;
321 rc1
.bottom
= rc
->bottom
;
323 /* blast this part now */
324 ExtTextOut(hdc
, rc1
.left
, rc1
.top
, ETO_OPAQUE
, &rc1
, NULL
, 0, NULL
);
328 rc1
.right
= rc
->left
+ wWidth
;
330 /* and another part */
331 ExtTextOut(hdc
, rc1
.left
, rc1
.top
, ETO_OPAQUE
, &rc1
, NULL
, 0, NULL
);
334 rc1
.right
= rc
->right
;
335 rc1
.top
= rc
->bottom
- wWidth
;
338 ExtTextOut(hdc
, rc1
.left
, rc1
.top
, ETO_OPAQUE
, &rc1
, NULL
, 0, NULL
);
340 /* restore the old bk color */
341 SetBkColor(hdc
, rgbOld
);
342 } /* Draw3DFaceFrame() */
345 /** void FAR PASCAL Draw3DRect(HDC, LPRECT, WORD, WORD)
348 * Draws a 3D rectangle that is shaded. wFlags can be used to
349 * control how the rectangle looks.
352 * HDC hdc : Handle to the device context that will be
353 * used to display the rectangle.
355 * RECT rect : A rectangle describing the dimensions of
356 * the rectangle in device coordinates.
358 * WORD wShadowWidth : Width of the shadow in device coordinates.
360 * WORD wFlags : The following flags may be passed to describe
361 * the style of the rectangle:
363 * DRAW3D_IN : The shadow is drawn such that
364 * the box appears to be sunk in to the screen.
365 * This is default if 0 is passed.
367 * DRAW3D_OUT : The shadow is drawn such that
368 * the box appears to be sticking out of the
371 * RETURN (void FAR PASCAL):
372 * The 3D looking rectangle will have been drawn into the DC.
378 void FAR PASCAL
Draw3DRect(HDC hdc
, LPRECT lpRect
,
379 WORD wShadowWidth
, WORD wFlags
)
381 /* sanity check--don't work if you don't have to! */
382 if (!wShadowWidth
|| !RectVisible(hdc
, lpRect
))
385 /* draw the top line */
386 Draw3DLine(hdc
, lpRect
->left
, lpRect
->top
,
387 lpRect
->right
- lpRect
->left
,
388 wShadowWidth
, DRAW3D_TOPLINE
| wFlags
);
391 Draw3DLine(hdc
, lpRect
->right
, lpRect
->top
,
392 lpRect
->bottom
- lpRect
->top
,
393 wShadowWidth
, DRAW3D_RIGHTLINE
| wFlags
);
396 Draw3DLine(hdc
, lpRect
->left
, lpRect
->bottom
,
397 lpRect
->right
- lpRect
->left
,
398 wShadowWidth
, DRAW3D_BOTTOMLINE
| wFlags
);
401 Draw3DLine(hdc
, lpRect
->left
, lpRect
->top
,
402 lpRect
->bottom
- lpRect
->top
,
403 wShadowWidth
, DRAW3D_LEFTLINE
| wFlags
);
407 /** void FAR PASCAL Draw3DLine(HDC hdc, WORD x, WORD y, WORD nLen,
410 * Draws a 3D line that can be used to make a 3D box.
413 * HDC hdc : Handle to the device context that will be
414 * used to display the 3D line.
416 * WORD x, y : Coordinates of the beginning of the line.
417 * These coordinates are in device units and
418 * represent the _outside_ most point. Horiz-
419 * ontal lines are drawn from left to right and
420 * vertical lines are drawn from top to bottom.
422 * WORD wShadowWidth : Width of the shadow in device coordinates.
424 * WORD wFlags : The following flags may be passed to
425 * describe the style of the 3D line:
427 * DRAW3D_IN : The shadow is drawn such that
428 * the box appears to be sunk in to the screen.
429 * This is default if 0 is passed.
431 * DRAW3D_OUT : The shadow is drawn such that
432 * the box appears to be sticking out of the
435 * DRAW3D_TOPLINE, _BOTTOMLINE, _LEFTLINE, and
436 * _RIGHTLINE : Specifies that a "top",
437 * "Bottom", "Left", or"Right" line is to be
440 * RETURN (void FAR PASCAL):
441 * The line will have been drawn into the DC.
447 void FAR PASCAL
Draw3DLine(HDC hdc
, WORD x
, WORD y
, WORD nLen
,
448 WORD wShadowWidth
, WORD wFlags
)
453 POINT Point
[ 4 ]; /* define a polgon with 4 points */
455 /* if width is zero, don't do nothin'! */
459 /* define shape of polygon--origin is always the same */
463 /* To do this we'll simply draw a polygon with four sides, using
464 * the appropriate brush. I dare you to ask me why this isn't a
467 if (wFlags
& DRAW3D_TOPLINE
)
469 /* across to right */
470 Point
[1].x
= x
+ nLen
- (wShadowWidth
== 1 ? 1 : 0);
474 Point
[2].x
= x
+ nLen
- wShadowWidth
;
475 Point
[2].y
= y
+ wShadowWidth
;
477 /* accross to left */
478 Point
[3].x
= x
+ wShadowWidth
;
479 Point
[3].y
= y
+ wShadowWidth
;
481 /* select 'dark' brush if 'in'--'light' for 'out' */
482 fDark
= (wFlags
& DRAW3D_IN
) ? TRUE
: FALSE
;
485 /* possibly the bottom? */
486 else if (wFlags
& DRAW3D_BOTTOMLINE
)
488 /* across to right */
489 Point
[1].x
= x
+ nLen
;
493 Point
[2].x
= x
+ nLen
- wShadowWidth
;
494 Point
[2].y
= y
- wShadowWidth
;
496 /* accross to left */
497 Point
[3].x
= x
+ wShadowWidth
;
498 Point
[3].y
= y
- wShadowWidth
;
500 /* select 'light' brush if 'in' */
501 fDark
= (wFlags
& DRAW3D_IN
) ? FALSE
: TRUE
;
504 /* ok, it's gotta be left? */
505 else if (wFlags
& DRAW3D_LEFTLINE
)
509 Point
[1].y
= y
+ nLen
- (wShadowWidth
== 1 ? 1 : 0);
512 Point
[2].x
= x
+ wShadowWidth
;
513 Point
[2].y
= y
+ nLen
- wShadowWidth
;
516 Point
[3].x
= x
+ wShadowWidth
;
517 Point
[3].y
= y
+ wShadowWidth
;
519 /* select 'dark' brush if 'in'--'light' for 'out' */
520 fDark
= (wFlags
& DRAW3D_IN
) ? TRUE
: FALSE
;
523 /* well maybe it's for the right side? */
524 else if (wFlags
& DRAW3D_RIGHTLINE
)
528 Point
[1].y
= y
+ nLen
;
531 Point
[2].x
= x
- wShadowWidth
;
532 Point
[2].y
= y
+ nLen
- wShadowWidth
;
535 Point
[3].x
= x
- wShadowWidth
;
536 Point
[3].y
= y
+ wShadowWidth
;
538 /* select 'light' brush if 'in' */
539 fDark
= (wFlags
& DRAW3D_IN
) ? FALSE
: TRUE
;
545 /* select NULL_PEN for no borders */
546 hOldPen
= (HPEN
) SelectObject(hdc
, GetStockObject(NULL_PEN
));
548 /* select the appropriate color for the fill */
550 hOldBrush
= (HBRUSH
) SelectObject(hdc
, GetStockObject(GRAY_BRUSH
));
552 hOldBrush
= (HBRUSH
) SelectObject(hdc
, GetStockObject(WHITE_BRUSH
));
554 /* finally, draw the dern thing */
555 Polygon(hdc
, (LPPOINT
)&Point
, 4);
557 /* restore what we killed */
558 SelectObject(hdc
, hOldBrush
);
559 SelectObject(hdc
, hOldPen
);
567 * Yet another 'Gas Gauge Custom Control.' This control gives you
568 * a 'progress bar' class (named zYzGauge) for use in your applications.
569 * You can set the range, position, font, color, orientation, and 3d
570 * effect of the gauge by sending messages to the control.
572 * Before you can use this control, you MUST first export the window
573 * procedure for the control (or define it with the _export keyword):
575 * EXPORTS gaugeWndProc
577 * You then need initialize the class before you use it:
579 * if (!gaugeInit(hInstance))
580 * die a horrible death
584 * The colors used by the control default to black and white if you
585 * are running on a mono-display. They default to blue and white
586 * if you are on a color display. You enable the 3D effect by setting
587 * the ZYZGS_3D style flag in the styles field of the control (like
588 * any other control).
590 * To select your own colors, you can send the ZYZG_SETFGCOLOR and
591 * ZYZG_SETBKCOLOR messages to set the foreground (percent done) and
592 * background (percent not done) colors. The lParam is the RGB()
593 * value--wParam is ignored.
595 * In all of the following ZYZG_??? messages, the arguments are
596 * WORDS. If you are setting parameters, the value is sent as
597 * the wParam (lParam is ignored). If you are getting parameters,
598 * the value is returned as a LONG and should be cast to a *signed*
601 * To set the depth of the 3D effect (if enabled), you can send the
602 * ZYZG_SETBEZELFACE and ZYZG_SETWIDTH3D messages. The bezel face
603 * is the flat top on the 3D border--its color will be that of the
604 * button-face. The 3D width is the width of the bezel itself; inside
605 * and outside. The light color is white, the dark color is gray.
606 * Both widths *can* be zero--both default to 2 which looks to me.
608 * The range of the control can be set by sending the ZYZG_SETRANGE
609 * message to the control. It can be any integer from 1 to 32767.
610 * What this specifies is the number of pieces that create a whole.
611 * The default is 100. You can get the current range setting by
612 * sending the ZYZG_GETRANGE message to the control.
614 * The position (number of pieces out of the whole have been used) is
615 * set with the ZYZG_SETPOSITION message. It can be any integer from
616 * 0 to the current range setting of the control--it will be clipped
617 * if the position is out of bounds. The default position is 0. You
618 * can get the current position at any time with the ZYZG_GETPOSITION
621 * You can also set the range using a delta from the current range.
622 * This is done by sending the ZYZG_SETDELTAPOS message with wParam
623 * set to a _signed_ integer value within the range of the control.
625 * The font used for the percentage text can be set using the standard
626 * WM_SETFONT message. You can get the current font at any time with
627 * the WM_GETFONT message.
629 * The orientation can be left to right, right to left, bottom to top,
630 * or top to bottom. Whatever suits your needs. You set this by
631 * sending the ZYZG_ORIENTATION message to the control with one of
632 * the following values (default is ZYZG_ORIENT_LEFTTORIGHT):
634 * ZYZG_ORIENT_LEFTTORIGHT (0)
635 * ZYZG_ORIENT_RIGHTTOLEFT (1)
636 * ZYZG_ORIENT_BOTTOMTOTOP (2)
637 * ZYZG_ORIENT_TOPTOBOTTOM (3)
640 * 3/12/91 cjp put in this comment
641 * 6/19/92 cjp touched it a bit
646 // (C) Copyright Microsoft Corp. 1992. All rights reserved.
648 // You have a royalty-free right to use, modify, reproduce and
649 // distribute the Sample Files (and/or any modified version) in
650 // any way you find useful, provided that you agree that
651 // Microsoft has no warranty obligations or liability for any
652 // Sample Application Files which are modified.
656 /* get the includes we need */
663 // #include "zyz3d.h"
664 // #include "zyzgauge.h"
667 /* static global variables */
668 static char gszzYzGaugeClass
[] = "zYzGauge";
671 /* window word position definitions */
672 #define ZYZG_WW_PZYZGAUGE 0
673 /* #define ZYZG_WW_EXTRABYTES 2 */
674 #define ZYZG_WW_EXTRABYTES 4
677 /* control block structure typedef */
678 typedef struct tZYZGAUGE
684 WORD wWidthBezelFace
;
689 } ZYZGAUGE
, *PZYZGAUGE
, FAR
*LPZYZGAUGE
;
692 /* some default values for the control */
693 #define ZYZG_DEF_RANGE 100
694 #define ZYZG_DEF_POSITION 0
695 #define ZYZG_DEF_ORIENTATION ZYZG_ORIENT_LEFTTORIGHT
696 #define ZYZG_DEF_WIDTH3D 2
697 #define ZYZG_DEF_BEZELFACE 2
701 /* the default settings for drawing colors--display dependent */
702 static DWORD rgbDefTextColor
;
703 static DWORD rgbDefBkColor
;
704 static BOOL fSupport3D
;
706 #if !defined(APIENTRY) // NT defines APIENTRY, 3.x not
707 #define APIENTRY FAR PASCAL
713 #define _EXPORT _export
714 typedef signed short int SHORT
;
717 /* internal function prototypes */
718 static void PASCAL
gaugePaint(HWND
, HDC
);
719 /* LRESULT FAR PASCAL */
720 LRESULT APIENTRY _EXPORT
gaugeWndProc(HWND
, UINT
, WPARAM
, LPARAM
);
724 /** BOOL FAR PASCAL gaugeInit(HINSTANCE hInstance)
727 * Registers the window class for the zYzGauge control. Performs
728 * other initialization for the zYzGauge text control. This must
729 * be done before the zYzGauge control is used--or it will fail
730 * and your dialog box will not open!
733 * HINSTANCE hInstance : Instance handle to register class with.
736 * The return value is TRUE if the zYzGauge class was successfully
737 * registered. It is FALSE if the initialization fails.
743 //#pragma alloc_text(init, gaugeInit)
745 BOOL FAR PASCAL
gaugeInit(HINSTANCE hInstance
)
747 static BOOL fRegistered
= FALSE
;
751 /* assume already registered if not first instance */
755 /* fill in the class structure for the zyzgauge control */
756 wc
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
758 wc
.lpszMenuName
= NULL
;
759 wc
.lpszClassName
= gszzYzGaugeClass
;
760 wc
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+ 1);
761 wc
.hInstance
= hInstance
;
764 wc
.style
= CS_GLOBALCLASS
| CS_HREDRAW
| CS_VREDRAW
;
766 wc
.style
= CS_HREDRAW
| CS_VREDRAW
;
769 wc
.lpfnWndProc
= gaugeWndProc
;
771 wc
.cbWndExtra
= ZYZG_WW_EXTRABYTES
;
773 /* attempt to register it--return FALSE if fail */
774 if (!RegisterClass(&wc
))
777 /* Get a DC to determine whether device is mono or not, and set
778 * default foreground/background colors as appropriate.
780 if ((hdc
= CreateIC("DISPLAY", NULL
, NULL
, 0L)))
782 /* check for mono-display */
783 if ((GetDeviceCaps(hdc
, BITSPIXEL
) == 1) &&
784 (GetDeviceCaps(hdc
, PLANES
) == 1))
786 /* using a mono DC--white foreground, black background */
787 rgbDefTextColor
= RGB(255, 255, 255);
788 rgbDefBkColor
= RGB(0, 0, 0);
791 /* good! we have color: blue foreground, white background */
794 rgbDefTextColor
= RGB(0, 0, 255);
795 rgbDefBkColor
= RGB(255, 255, 255);
798 /* need at _least_ 8 for two shades of gray (>=VGA) */
799 fSupport3D
= (GetDeviceCaps(hdc
, NUMCOLORS
) >= 8) ? TRUE
: FALSE
;
801 /* get rid of the DC (IC) */
805 /* uh-oh... can't get DC (IC)... fail */
808 /* unregister the class */
809 UnregisterClass(gszzYzGaugeClass
, hInstance
);
814 return (fRegistered
= TRUE
);
818 /** static void PASCAL gaugePaint(HWND hwnd, HDC hdc)
821 * This function is responsible for painting the zYzGauge control.
824 * HWND hwnd : The window handle for the gauge.
826 * HDC hdc : The DC for the gauge's window.
829 * The control will have been painted.
835 static void PASCAL
gaugePaint(HWND hwnd
, HDC hdc
)
844 WORD dx
, dy
, wGomerX
, wGomerY
;
845 /* Win32s has no GetTextExtent(); let's try GetTextExtentPoint() instead,
846 * which needs a SIZE* parameter */
847 #if defined(__WIN32__)
851 /* get pointer to the control's control block */
852 // pgauge = (PZYZGAUGE)GetWindowWord(hwnd, ZYZG_WW_PZYZGAUGE);
853 pgauge
= (PZYZGAUGE
)GetWindowLong(hwnd
, ZYZG_WW_PZYZGAUGE
);
855 /* set the colors into for the gauge into the control */
856 SetTextColor(hdc
, pgauge
->rgbTextColor
);
857 SetBkColor(hdc
, pgauge
->rgbBkColor
);
859 /* draw black rectangle for gauge */
860 GetClientRect(hwnd
, &rc1
);
862 /* draw a black border on the _outside_ */
863 FrameRect(hdc
, &rc1
, (HBRUSH
) GetStockObject(BLACK_BRUSH
));
865 /* we want to draw _just inside_ the black border */
866 InflateRect(&rc1
, -1, -1);
868 /* one line thick so far... */
869 // Offset = (WORD) 1;
871 /* for 3D stuff, we need to have at least two shades of gray */
872 if ((GetWindowLong(hwnd
, GWL_STYLE
) & ZYZGS_3D
) && fSupport3D
)
874 Draw3DRect(hdc
, &rc1
, pgauge
->wWidth3D
, DRAW3D_OUT
);
875 InflateRect(&rc1
, ~(pgauge
->wWidth3D
), ~(pgauge
->wWidth3D
));
877 Draw3DFaceFrame(hdc
, &rc1
, pgauge
->wWidthBezelFace
);
878 InflateRect(&rc1
, ~(pgauge
->wWidthBezelFace
), ~(pgauge
->wWidthBezelFace
));
880 Draw3DRect(hdc
, &rc1
, pgauge
->wWidth3D
, DRAW3D_IN
);
881 InflateRect(&rc1
, ~(pgauge
->wWidth3D
), ~(pgauge
->wWidth3D
));
883 /* draw a black border on the _inside_ */
884 FrameRect(hdc
, &rc1
, (HBRUSH
) GetStockObject(BLACK_BRUSH
));
886 /* we want to draw _just inside_ the black border */
887 InflateRect(&rc1
, -1, -1);
889 /* add all the other pixels into the border width */
890 Offset
+= (2 * pgauge
->wWidth3D
) + pgauge
->wWidthBezelFace
+ 1;
893 /* dup--one rc for 'how much filled', one rc for 'how much empty' */
896 /* get the range--make sure it's a valid range */
897 if ((iRange
= pgauge
->wRange
) <= 0)
900 /* get the position--greater than 100% would be bad */
901 if ((iPos
= pgauge
->wPosition
) > iRange
)
904 /* compute the actual size of the gauge */
905 dx
= rc1
.right
- rc1
.left
;
906 dy
= rc1
.bottom
- rc1
.top
;
907 wGomerX
= (WORD
)((DWORD
)iPos
* dx
/ iRange
);
908 wGomerY
= (WORD
)((DWORD
)iPos
* dy
/ iRange
);
910 /* get the orientation and munge rects accordingly */
911 switch (pgauge
->wOrientation
)
913 case ZYZG_ORIENT_RIGHTTOLEFT
:
914 rc1
.left
= rc2
.right
= rc1
.right
- wGomerX
;
917 case ZYZG_ORIENT_BOTTOMTOTOP
:
918 rc1
.top
= rc2
.bottom
= rc1
.bottom
- wGomerY
;
921 case ZYZG_ORIENT_TOPTOBOTTOM
:
922 rc1
.bottom
= rc2
.top
+= wGomerY
;
926 rc1
.right
= rc2
.left
+= wGomerX
;
930 /* select the correct font */
931 hFont
= (HFONT
) SelectObject(hdc
, pgauge
->hFont
);
933 /* build up a string to blit out--ie the meaning of life: "42%" */
934 wsprintf(ach
, "%3d%%", (WORD
)((DWORD
)iPos
* 100 / iRange
));
935 /* Win32s has no GetTextExtent(); let's try GetTextExtentPoint() instead */
936 #if defined(__WIN32__)
937 GetTextExtentPoint(hdc
, ach
, wGomerX
= lstrlen(ach
), &size
);
940 dwExtent
= GetTextExtent(hdc
, ach
, wGomerX
= lstrlen(ach
));
944 /* Draw the finished (ie the percent done) side of box. If
945 * ZYZG_WW_POSITION is 42, (in range of 0 to 100) this ExtTextOut
946 * draws the meaning of life (42%) bar.
948 ExtTextOut(hdc
, (dx
- LOWORD(dwExtent
)) / 2 + Offset
,
949 (dy
- HIWORD(dwExtent
)) / 2 + Offset
,
950 ETO_OPAQUE
| ETO_CLIPPED
, &rc2
, ach
, wGomerX
, NULL
);
952 /* Reverse fore and back colors for drawing the undone (ie the non-
953 * finished) side of the box.
955 SetBkColor(hdc
, pgauge
->rgbTextColor
);
956 SetTextColor(hdc
, pgauge
->rgbBkColor
);
958 ExtTextOut(hdc
, (dx
- LOWORD(dwExtent
)) / 2 + Offset
,
959 (dy
- HIWORD(dwExtent
)) / 2 + Offset
,
960 ETO_OPAQUE
| ETO_CLIPPED
, &rc1
, ach
, wGomerX
, NULL
);
962 /* unselect the font */
963 SelectObject(hdc
, hFont
);
967 /** LRESULT FAR PASCAL gaugeWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
970 * This is the control's window procedure. Its purpose is to handle
971 * special messages for this custom control.
973 * The special control messages for the gauge control are:
975 * ZYZG_SETRANGE : Sets the range of the gauge. In other
976 * words, the number of parts that make a
979 * ZYZG_GETRANGE : Returns the current range of the gauge.
981 * ZYZG_SETORIENTATION : Sets the orientation of the gauge. This
982 * can be one of the ZYZG_ORIENT_?? msgs.
984 * ZYZG_GETORIENTATION : Gets the current orientation of the
987 * ZYZG_SETPOSITION : Sets the current position of the gauge.
988 * In other words, how many pieces of the
989 * whole have been used.
991 * ZYZG_GETPOSITION : Gets the current position of the gauge.
993 * ZYZG_SETDELTAPOS : Sets the position of the gauge +/- the
996 * ZYZG_SETFGCOLOR : Sets the foreground (percent done) color.
998 * ZYZG_GETFGCOLOR : Gets the foreground (percent done) color.
1000 * ZYZG_SETBKCOLOR : Sets the background (percent not done)
1003 * ZYZG_GETBKCOLOR : Gets the background (percent not done)
1006 * WM_SETFONT : Sets the font to use for the percentage
1007 * text of the gauge.
1009 * WM_GETFONT : Gets the current font in use by the
1016 /* LRESULT FAR PASCAL */
1018 LRESULT APIENTRY _EXPORT
gaugeWndProc(HWND hwnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
1025 // pgauge = (PZYZGAUGE)GetWindowWord(hwnd, ZYZG_WW_PZYZGAUGE);
1026 pgauge
= (PZYZGAUGE
)GetWindowLong(hwnd
, ZYZG_WW_PZYZGAUGE
);
1028 /* break to get DefWindowProc() */
1032 /* need to allocate a control block */
1033 // pgauge = (PZYZGAUGE)LocalAlloc(LPTR, sizeof(ZYZGAUGE));
1034 pgauge
= (PZYZGAUGE
)malloc(sizeof(ZYZGAUGE
));
1038 /* hang on to this control block */
1039 // SetWindowWord(hwnd, ZYZG_WW_PZYZGAUGE, (WORD)pgauge);
1040 SetWindowLong(hwnd
, ZYZG_WW_PZYZGAUGE
, (LONG
)pgauge
);
1042 /* fill control block with defaults */
1043 pgauge
->wRange
= ZYZG_DEF_RANGE
;
1044 pgauge
->wPosition
= ZYZG_DEF_POSITION
;
1045 pgauge
->wOrientation
= ZYZG_DEF_ORIENTATION
;
1046 pgauge
->wWidth3D
= ZYZG_DEF_WIDTH3D
;
1047 pgauge
->wWidthBezelFace
= ZYZG_DEF_BEZELFACE
;
1048 pgauge
->rgbTextColor
= rgbDefTextColor
;
1049 pgauge
->rgbBkColor
= rgbDefBkColor
;
1051 /* use system font */
1052 SendMessage(hwnd
, WM_SETFONT
, (WPARAM
)NULL
, 0L);
1054 /* go to DefWindowProc() to finish the job */
1058 /* get rid of the control's memory */
1060 // LocalFree((HANDLE)pgauge);
1064 case ZYZG_GETPOSITION
:
1065 return (pgauge
->wPosition
);
1068 return (pgauge
->wRange
);
1070 case ZYZG_GETORIENTATION
:
1071 return (pgauge
->wOrientation
);
1073 case ZYZG_GETWIDTH3D
:
1074 return (pgauge
->wWidth3D
);
1076 case ZYZG_GETBEZELFACE
:
1077 return (pgauge
->wWidthBezelFace
);
1079 case ZYZG_GETBKCOLOR
:
1080 return (pgauge
->rgbTextColor
);
1082 case ZYZG_GETFGCOLOR
:
1083 return (pgauge
->rgbBkColor
);
1085 case ZYZG_SETBKCOLOR
:
1086 pgauge
->rgbBkColor
= lParam
;
1089 case ZYZG_SETFGCOLOR
:
1090 pgauge
->rgbTextColor
= lParam
;
1094 case ZYZG_SETPOSITION
:
1095 pgauge
->wPosition
= wParam
;
1098 GetClientRect(hwnd
, &rc
);
1099 if ((GetWindowLong(hwnd
, GWL_STYLE
) & ZYZGS_3D
) && fSupport3D
)
1101 wParam
= (2 * pgauge
->wWidth3D
) +
1102 pgauge
->wWidthBezelFace
+ 2;
1108 InflateRect(&rc
, ~(wParam
), ~(wParam
));
1109 InvalidateRect(hwnd
, &rc
, FALSE
);
1114 pgauge
->wRange
= wParam
;
1115 goto zyzgForceRepaint
;
1117 case ZYZG_SETORIENTATION
:
1118 pgauge
->wOrientation
= wParam
;
1119 goto zyzgForceRepaint
;
1121 case ZYZG_SETWIDTH3D
:
1122 pgauge
->wWidth3D
= wParam
;
1125 InvalidateRect(hwnd
, NULL
, FALSE
);
1129 case ZYZG_SETBEZELFACE
:
1130 pgauge
->wWidthBezelFace
= wParam
;
1131 goto zyzgForceRepaint3D
;
1133 case ZYZG_SETDELTAPOS
:
1134 /* Watcom doesn't like the following line so removing typecasts */
1135 /* (int)pgauge->wPosition += (int)wParam; */
1136 pgauge
->wPosition
+= wParam
;
1137 goto zyzgForceRepaint
;
1140 BeginPaint(hwnd
, &ps
);
1141 gaugePaint(hwnd
, ps
.hdc
);
1142 EndPaint(hwnd
, &ps
);
1146 hFont
= pgauge
->hFont
;
1148 /* if system font, then return NULL handle */
1149 return (long)((hFont
== GetStockObject(SYSTEM_FONT
)) ? NULL
: hFont
);
1152 /* if NULL hFont, use system font */
1153 if (!(hFont
= (HFONT
)wParam
))
1154 hFont
= (HFONT
) GetStockObject(SYSTEM_FONT
);
1156 pgauge
->hFont
= hFont
;
1158 /* redraw if indicated in message */
1161 InvalidateRect(hwnd
, NULL
, TRUE
);
1167 /* let the dialog mangler take care of this message */
1168 return (DefWindowProc(hwnd
, uMsg
, wParam
, lParam
));
1169 } /* gaugeWndProc() */
1172 /** EOF: zyzgauge.c **/