]>
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);
89 SetBackgroundColour(parent
->GetDefaultBackgroundColour()) ;
90 SetForegroundColour(parent
->GetDefaultForegroundColour()) ;
92 m_windowStyle
= style
;
95 m_windowId
= (int)NewControlId();
104 long msFlags
= WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
;
108 CreateWindowEx(MakeExtendedStyle(m_windowStyle
), "zYzGauge", NULL
, msFlags
,
109 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
110 wxGetInstance(), NULL
);
112 m_hWnd
= (WXHWND
)wx_button
;
114 // Subclass again for purposes of dialog editing mode
115 SubclassWin((WXHWND
)wx_button
);
119 if (m_windowStyle
& wxGA_HORIZONTAL
)
120 wOrient
= ZYZG_ORIENT_LEFTTORIGHT
;
122 wOrient
= ZYZG_ORIENT_BOTTOMTOTOP
;
124 SendMessage(wx_button
, ZYZG_SETORIENTATION
, wOrient
, 0);
125 SendMessage(wx_button
, ZYZG_SETRANGE
, range
, 0);
127 SendMessage((HWND
) GetHWND(), ZYZG_SETFGCOLOR
, 0, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
128 SendMessage((HWND
) GetHWND(), ZYZG_SETBKCOLOR
, 0, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
130 SetFont(* parent
->GetFont());
136 SetSize(x
, y
, width
, height
);
138 ShowWindow((HWND
) GetHWND(), SW_SHOW
);
143 void wxGaugeMSW::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
145 int currentX
, currentY
;
146 GetPosition(¤tX
, ¤tY
);
152 if (x
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
154 if (y
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
157 // If we're prepared to use the existing size, then...
158 if (width
== -1 && height
== -1 && ((sizeFlags
& wxSIZE_AUTO
) != wxSIZE_AUTO
))
163 // Deal with default size (using -1 values)
165 w1
= DEFAULT_ITEM_WIDTH
;
168 h1
= DEFAULT_ITEM_HEIGHT
;
170 MoveWindow((HWND
) GetHWND(), x1
, y1
, w1
, h1
, TRUE
);
173 void wxGaugeMSW::SetShadowWidth(int w
)
175 SendMessage((HWND
) GetHWND(), ZYZG_SETWIDTH3D
, w
, 0);
178 void wxGaugeMSW::SetBezelFace(int w
)
180 SendMessage((HWND
) GetHWND(), ZYZG_SETBEZELFACE
, w
, 0);
183 void wxGaugeMSW::SetRange(int r
)
187 SendMessage((HWND
) GetHWND(), ZYZG_SETRANGE
, r
, 0);
190 void wxGaugeMSW::SetValue(int pos
)
194 SendMessage((HWND
) GetHWND(), ZYZG_SETPOSITION
, pos
, 0);
197 int wxGaugeMSW::GetShadowWidth(void) const
199 return (int) SendMessage((HWND
) GetHWND(), ZYZG_GETWIDTH3D
, 0, 0);
202 int wxGaugeMSW::GetBezelFace(void) const
204 return (int) SendMessage((HWND
) GetHWND(), ZYZG_GETBEZELFACE
, 0, 0);
207 int wxGaugeMSW::GetRange(void) const
209 return (int) SendMessage((HWND
) GetHWND(), ZYZG_GETRANGE
, 0, 0);
212 int wxGaugeMSW::GetValue(void) const
214 return (int) SendMessage((HWND
) GetHWND(), ZYZG_GETPOSITION
, 0, 0);
217 void wxGaugeMSW::SetForegroundColour(const wxColour
& col
)
219 m_foregroundColour
= col
;
220 SendMessage((HWND
) GetHWND(), ZYZG_SETFGCOLOR
, 0, RGB(col
.Red(), col
.Green(), col
.Blue()));
223 void wxGaugeMSW::SetBackgroundColour(const wxColour
& col
)
225 m_backgroundColour
= col
;
226 SendMessage((HWND
) GetHWND(), ZYZG_SETBKCOLOR
, 0, RGB(col
.Red(), col
.Green(), col
.Blue()));
233 * This module contains functions for creating nifty 3D borders
234 * around controls like zYzGauge.
237 * 3/14/91 cjp put in this comment
238 * 6/19/92 cjp touched it a bit
243 // (C) Copyright Microsoft Corp. 1992. All rights reserved.
245 // You have a royalty-free right to use, modify, reproduce and
246 // distribute the Sample Files (and/or any modified version) in
247 // any way you find useful, provided that you agree that
248 // Microsoft has no warranty obligations or liability for any
249 // Sample Application Files which are modified.
253 /* get the includes we need */
256 /* misc. control flag defines */
257 #define DRAW3D_IN 0x0001
258 #define DRAW3D_OUT 0x0002
260 #define DRAW3D_TOPLINE 0x0004
261 #define DRAW3D_BOTTOMLINE 0x0008
262 #define DRAW3D_LEFTLINE 0x0010
263 #define DRAW3D_RIGHTLINE 0x0020
266 /* public function prototypes */
267 void FAR PASCAL
Draw3DFaceFrame(HDC
, LPRECT
, WORD
);
268 void FAR PASCAL
Draw3DRect(HDC
, LPRECT
, WORD
, WORD
);
269 void FAR PASCAL
Draw3DLine(HDC
, WORD
, WORD
, WORD
, WORD
, WORD
);
272 /** void FAR PASCAL Draw3DFaceFrame(HDC hdc, LPRECT rc, WORD wWidth)
275 * This function draws a flat frame with the current button-face
279 * HDC hdc : The DC to draw into.
281 * LPRECT rc : The containing rect for the new frame.
283 * WORD wWidth : The width of the frame to draw.
285 * RETURN (void FAR PASCAL):
286 * The frame will have been drawn into the DC.
292 void FAR PASCAL
Draw3DFaceFrame(HDC hdc
, LPRECT rc
, WORD wWidth
)
297 /* don't go through a bunch of work if we don't have to */
301 /* set up color to be button-face color--so it may not be gray */
302 rgbOld
= SetBkColor(hdc
, GetSysColor(COLOR_BTNFACE
));
304 /* perform CopyRect w/o bloody windows style overhead */
310 rc1
.bottom
= rc
->top
+ wWidth
;
311 rc1
.right
= rc
->right
;
314 ExtTextOut(hdc
, rc1
.left
, rc1
.top
, ETO_OPAQUE
, &rc1
, NULL
, 0, NULL
);
317 rc1
.left
= rc
->right
- wWidth
;
318 rc1
.bottom
= rc
->bottom
;
320 /* blast this part now */
321 ExtTextOut(hdc
, rc1
.left
, rc1
.top
, ETO_OPAQUE
, &rc1
, NULL
, 0, NULL
);
325 rc1
.right
= rc
->left
+ wWidth
;
327 /* and another part */
328 ExtTextOut(hdc
, rc1
.left
, rc1
.top
, ETO_OPAQUE
, &rc1
, NULL
, 0, NULL
);
331 rc1
.right
= rc
->right
;
332 rc1
.top
= rc
->bottom
- wWidth
;
335 ExtTextOut(hdc
, rc1
.left
, rc1
.top
, ETO_OPAQUE
, &rc1
, NULL
, 0, NULL
);
337 /* restore the old bk color */
338 SetBkColor(hdc
, rgbOld
);
339 } /* Draw3DFaceFrame() */
342 /** void FAR PASCAL Draw3DRect(HDC, LPRECT, WORD, WORD)
345 * Draws a 3D rectangle that is shaded. wFlags can be used to
346 * control how the rectangle looks.
349 * HDC hdc : Handle to the device context that will be
350 * used to display the rectangle.
352 * RECT rect : A rectangle describing the dimensions of
353 * the rectangle in device coordinates.
355 * WORD wShadowWidth : Width of the shadow in device coordinates.
357 * WORD wFlags : The following flags may be passed to describe
358 * the style of the rectangle:
360 * DRAW3D_IN : The shadow is drawn such that
361 * the box appears to be sunk in to the screen.
362 * This is default if 0 is passed.
364 * DRAW3D_OUT : The shadow is drawn such that
365 * the box appears to be sticking out of the
368 * RETURN (void FAR PASCAL):
369 * The 3D looking rectangle will have been drawn into the DC.
375 void FAR PASCAL
Draw3DRect(HDC hdc
, LPRECT lpRect
,
376 WORD wShadowWidth
, WORD wFlags
)
378 /* sanity check--don't work if you don't have to! */
379 if (!wShadowWidth
|| !RectVisible(hdc
, lpRect
))
382 /* draw the top line */
383 Draw3DLine(hdc
, lpRect
->left
, lpRect
->top
,
384 lpRect
->right
- lpRect
->left
,
385 wShadowWidth
, DRAW3D_TOPLINE
| wFlags
);
388 Draw3DLine(hdc
, lpRect
->right
, lpRect
->top
,
389 lpRect
->bottom
- lpRect
->top
,
390 wShadowWidth
, DRAW3D_RIGHTLINE
| wFlags
);
393 Draw3DLine(hdc
, lpRect
->left
, lpRect
->bottom
,
394 lpRect
->right
- lpRect
->left
,
395 wShadowWidth
, DRAW3D_BOTTOMLINE
| wFlags
);
398 Draw3DLine(hdc
, lpRect
->left
, lpRect
->top
,
399 lpRect
->bottom
- lpRect
->top
,
400 wShadowWidth
, DRAW3D_LEFTLINE
| wFlags
);
404 /** void FAR PASCAL Draw3DLine(HDC hdc, WORD x, WORD y, WORD nLen,
407 * Draws a 3D line that can be used to make a 3D box.
410 * HDC hdc : Handle to the device context that will be
411 * used to display the 3D line.
413 * WORD x, y : Coordinates of the beginning of the line.
414 * These coordinates are in device units and
415 * represent the _outside_ most point. Horiz-
416 * ontal lines are drawn from left to right and
417 * vertical lines are drawn from top to bottom.
419 * WORD wShadowWidth : Width of the shadow in device coordinates.
421 * WORD wFlags : The following flags may be passed to
422 * describe the style of the 3D line:
424 * DRAW3D_IN : The shadow is drawn such that
425 * the box appears to be sunk in to the screen.
426 * This is default if 0 is passed.
428 * DRAW3D_OUT : The shadow is drawn such that
429 * the box appears to be sticking out of the
432 * DRAW3D_TOPLINE, _BOTTOMLINE, _LEFTLINE, and
433 * _RIGHTLINE : Specifies that a "top",
434 * "Bottom", "Left", or"Right" line is to be
437 * RETURN (void FAR PASCAL):
438 * The line will have been drawn into the DC.
444 void FAR PASCAL
Draw3DLine(HDC hdc
, WORD x
, WORD y
, WORD nLen
,
445 WORD wShadowWidth
, WORD wFlags
)
450 POINT Point
[ 4 ]; /* define a polgon with 4 points */
452 /* if width is zero, don't do nothin'! */
456 /* define shape of polygon--origin is always the same */
460 /* To do this we'll simply draw a polygon with four sides, using
461 * the appropriate brush. I dare you to ask me why this isn't a
464 if (wFlags
& DRAW3D_TOPLINE
)
466 /* across to right */
467 Point
[1].x
= x
+ nLen
- (wShadowWidth
== 1 ? 1 : 0);
471 Point
[2].x
= x
+ nLen
- wShadowWidth
;
472 Point
[2].y
= y
+ wShadowWidth
;
474 /* accross to left */
475 Point
[3].x
= x
+ wShadowWidth
;
476 Point
[3].y
= y
+ wShadowWidth
;
478 /* select 'dark' brush if 'in'--'light' for 'out' */
479 fDark
= (wFlags
& DRAW3D_IN
) ? TRUE
: FALSE
;
482 /* possibly the bottom? */
483 else if (wFlags
& DRAW3D_BOTTOMLINE
)
485 /* across to right */
486 Point
[1].x
= x
+ nLen
;
490 Point
[2].x
= x
+ nLen
- wShadowWidth
;
491 Point
[2].y
= y
- wShadowWidth
;
493 /* accross to left */
494 Point
[3].x
= x
+ wShadowWidth
;
495 Point
[3].y
= y
- wShadowWidth
;
497 /* select 'light' brush if 'in' */
498 fDark
= (wFlags
& DRAW3D_IN
) ? FALSE
: TRUE
;
501 /* ok, it's gotta be left? */
502 else if (wFlags
& DRAW3D_LEFTLINE
)
506 Point
[1].y
= y
+ nLen
- (wShadowWidth
== 1 ? 1 : 0);
509 Point
[2].x
= x
+ wShadowWidth
;
510 Point
[2].y
= y
+ nLen
- wShadowWidth
;
513 Point
[3].x
= x
+ wShadowWidth
;
514 Point
[3].y
= y
+ wShadowWidth
;
516 /* select 'dark' brush if 'in'--'light' for 'out' */
517 fDark
= (wFlags
& DRAW3D_IN
) ? TRUE
: FALSE
;
520 /* well maybe it's for the right side? */
521 else if (wFlags
& DRAW3D_RIGHTLINE
)
525 Point
[1].y
= y
+ nLen
;
528 Point
[2].x
= x
- wShadowWidth
;
529 Point
[2].y
= y
+ nLen
- wShadowWidth
;
532 Point
[3].x
= x
- wShadowWidth
;
533 Point
[3].y
= y
+ wShadowWidth
;
535 /* select 'light' brush if 'in' */
536 fDark
= (wFlags
& DRAW3D_IN
) ? FALSE
: TRUE
;
542 /* select NULL_PEN for no borders */
543 hOldPen
= (HPEN
) SelectObject(hdc
, GetStockObject(NULL_PEN
));
545 /* select the appropriate color for the fill */
547 hOldBrush
= (HBRUSH
) SelectObject(hdc
, GetStockObject(GRAY_BRUSH
));
549 hOldBrush
= (HBRUSH
) SelectObject(hdc
, GetStockObject(WHITE_BRUSH
));
551 /* finally, draw the dern thing */
552 Polygon(hdc
, (LPPOINT
)&Point
, 4);
554 /* restore what we killed */
555 SelectObject(hdc
, hOldBrush
);
556 SelectObject(hdc
, hOldPen
);
564 * Yet another 'Gas Gauge Custom Control.' This control gives you
565 * a 'progress bar' class (named zYzGauge) for use in your applications.
566 * You can set the range, position, font, color, orientation, and 3d
567 * effect of the gauge by sending messages to the control.
569 * Before you can use this control, you MUST first export the window
570 * procedure for the control (or define it with the _export keyword):
572 * EXPORTS gaugeWndProc
574 * You then need initialize the class before you use it:
576 * if (!gaugeInit(hInstance))
577 * die a horrible death
581 * The colors used by the control default to black and white if you
582 * are running on a mono-display. They default to blue and white
583 * if you are on a color display. You enable the 3D effect by setting
584 * the ZYZGS_3D style flag in the styles field of the control (like
585 * any other control).
587 * To select your own colors, you can send the ZYZG_SETFGCOLOR and
588 * ZYZG_SETBKCOLOR messages to set the foreground (percent done) and
589 * background (percent not done) colors. The lParam is the RGB()
590 * value--wParam is ignored.
592 * In all of the following ZYZG_??? messages, the arguments are
593 * WORDS. If you are setting parameters, the value is sent as
594 * the wParam (lParam is ignored). If you are getting parameters,
595 * the value is returned as a LONG and should be cast to a *signed*
598 * To set the depth of the 3D effect (if enabled), you can send the
599 * ZYZG_SETBEZELFACE and ZYZG_SETWIDTH3D messages. The bezel face
600 * is the flat top on the 3D border--its color will be that of the
601 * button-face. The 3D width is the width of the bezel itself; inside
602 * and outside. The light color is white, the dark color is gray.
603 * Both widths *can* be zero--both default to 2 which looks to me.
605 * The range of the control can be set by sending the ZYZG_SETRANGE
606 * message to the control. It can be any integer from 1 to 32767.
607 * What this specifies is the number of pieces that create a whole.
608 * The default is 100. You can get the current range setting by
609 * sending the ZYZG_GETRANGE message to the control.
611 * The position (number of pieces out of the whole have been used) is
612 * set with the ZYZG_SETPOSITION message. It can be any integer from
613 * 0 to the current range setting of the control--it will be clipped
614 * if the position is out of bounds. The default position is 0. You
615 * can get the current position at any time with the ZYZG_GETPOSITION
618 * You can also set the range using a delta from the current range.
619 * This is done by sending the ZYZG_SETDELTAPOS message with wParam
620 * set to a _signed_ integer value within the range of the control.
622 * The font used for the percentage text can be set using the standard
623 * WM_SETFONT message. You can get the current font at any time with
624 * the WM_GETFONT message.
626 * The orientation can be left to right, right to left, bottom to top,
627 * or top to bottom. Whatever suits your needs. You set this by
628 * sending the ZYZG_ORIENTATION message to the control with one of
629 * the following values (default is ZYZG_ORIENT_LEFTTORIGHT):
631 * ZYZG_ORIENT_LEFTTORIGHT (0)
632 * ZYZG_ORIENT_RIGHTTOLEFT (1)
633 * ZYZG_ORIENT_BOTTOMTOTOP (2)
634 * ZYZG_ORIENT_TOPTOBOTTOM (3)
637 * 3/12/91 cjp put in this comment
638 * 6/19/92 cjp touched it a bit
643 // (C) Copyright Microsoft Corp. 1992. All rights reserved.
645 // You have a royalty-free right to use, modify, reproduce and
646 // distribute the Sample Files (and/or any modified version) in
647 // any way you find useful, provided that you agree that
648 // Microsoft has no warranty obligations or liability for any
649 // Sample Application Files which are modified.
653 /* get the includes we need */
660 // #include "zyz3d.h"
661 // #include "zyzgauge.h"
664 /* static global variables */
665 static char gszzYzGaugeClass
[] = "zYzGauge";
668 /* window word position definitions */
669 #define ZYZG_WW_PZYZGAUGE 0
670 /* #define ZYZG_WW_EXTRABYTES 2 */
671 #define ZYZG_WW_EXTRABYTES 4
674 /* control block structure typedef */
675 typedef struct tZYZGAUGE
681 WORD wWidthBezelFace
;
686 } ZYZGAUGE
, *PZYZGAUGE
, FAR
*LPZYZGAUGE
;
689 /* some default values for the control */
690 #define ZYZG_DEF_RANGE 100
691 #define ZYZG_DEF_POSITION 0
692 #define ZYZG_DEF_ORIENTATION ZYZG_ORIENT_LEFTTORIGHT
693 #define ZYZG_DEF_WIDTH3D 2
694 #define ZYZG_DEF_BEZELFACE 2
698 /* the default settings for drawing colors--display dependent */
699 static DWORD rgbDefTextColor
;
700 static DWORD rgbDefBkColor
;
701 static BOOL fSupport3D
;
703 #if !defined(APIENTRY) // NT defines APIENTRY, 3.x not
704 #define APIENTRY FAR PASCAL
710 #define _EXPORT _export
711 typedef signed short int SHORT
;
714 /* internal function prototypes */
715 static void PASCAL
gaugePaint(HWND
, HDC
);
716 /* LRESULT FAR PASCAL */
717 LRESULT APIENTRY _EXPORT
gaugeWndProc(HWND
, UINT
, WPARAM
, LPARAM
);
721 /** BOOL FAR PASCAL gaugeInit(HINSTANCE hInstance)
724 * Registers the window class for the zYzGauge control. Performs
725 * other initialization for the zYzGauge text control. This must
726 * be done before the zYzGauge control is used--or it will fail
727 * and your dialog box will not open!
730 * HINSTANCE hInstance : Instance handle to register class with.
733 * The return value is TRUE if the zYzGauge class was successfully
734 * registered. It is FALSE if the initialization fails.
740 //#pragma alloc_text(init, gaugeInit)
742 BOOL FAR PASCAL
gaugeInit(HINSTANCE hInstance
)
744 static BOOL fRegistered
= FALSE
;
748 /* assume already registered if not first instance */
752 /* fill in the class structure for the zyzgauge control */
753 wc
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
755 wc
.lpszMenuName
= NULL
;
756 wc
.lpszClassName
= gszzYzGaugeClass
;
757 wc
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+ 1);
758 wc
.hInstance
= hInstance
;
761 wc
.style
= CS_GLOBALCLASS
| CS_HREDRAW
| CS_VREDRAW
;
763 wc
.style
= CS_HREDRAW
| CS_VREDRAW
;
766 wc
.lpfnWndProc
= gaugeWndProc
;
768 wc
.cbWndExtra
= ZYZG_WW_EXTRABYTES
;
770 /* attempt to register it--return FALSE if fail */
771 if (!RegisterClass(&wc
))
774 /* Get a DC to determine whether device is mono or not, and set
775 * default foreground/background colors as appropriate.
777 if ((hdc
= CreateIC("DISPLAY", NULL
, NULL
, 0L)))
779 /* check for mono-display */
780 if ((GetDeviceCaps(hdc
, BITSPIXEL
) == 1) &&
781 (GetDeviceCaps(hdc
, PLANES
) == 1))
783 /* using a mono DC--white foreground, black background */
784 rgbDefTextColor
= RGB(255, 255, 255);
785 rgbDefBkColor
= RGB(0, 0, 0);
788 /* good! we have color: blue foreground, white background */
791 rgbDefTextColor
= RGB(0, 0, 255);
792 rgbDefBkColor
= RGB(255, 255, 255);
795 /* need at _least_ 8 for two shades of gray (>=VGA) */
796 fSupport3D
= (GetDeviceCaps(hdc
, NUMCOLORS
) >= 8) ? TRUE
: FALSE
;
798 /* get rid of the DC (IC) */
802 /* uh-oh... can't get DC (IC)... fail */
805 /* unregister the class */
806 UnregisterClass(gszzYzGaugeClass
, hInstance
);
811 return (fRegistered
= TRUE
);
815 /** static void PASCAL gaugePaint(HWND hwnd, HDC hdc)
818 * This function is responsible for painting the zYzGauge control.
821 * HWND hwnd : The window handle for the gauge.
823 * HDC hdc : The DC for the gauge's window.
826 * The control will have been painted.
832 static void PASCAL
gaugePaint(HWND hwnd
, HDC hdc
)
841 WORD dx
, dy
, wGomerX
, wGomerY
;
842 /* Win32s has no GetTextExtent(); let's try GetTextExtentPoint() instead,
843 * which needs a SIZE* parameter */
844 #if defined(__WIN32__)
848 /* get pointer to the control's control block */
849 // pgauge = (PZYZGAUGE)GetWindowWord(hwnd, ZYZG_WW_PZYZGAUGE);
850 pgauge
= (PZYZGAUGE
)GetWindowLong(hwnd
, ZYZG_WW_PZYZGAUGE
);
852 /* set the colors into for the gauge into the control */
853 SetTextColor(hdc
, pgauge
->rgbTextColor
);
854 SetBkColor(hdc
, pgauge
->rgbBkColor
);
856 /* draw black rectangle for gauge */
857 GetClientRect(hwnd
, &rc1
);
859 /* draw a black border on the _outside_ */
860 FrameRect(hdc
, &rc1
, (HBRUSH
) GetStockObject(BLACK_BRUSH
));
862 /* we want to draw _just inside_ the black border */
863 InflateRect(&rc1
, -1, -1);
865 /* one line thick so far... */
866 // Offset = (WORD) 1;
868 /* for 3D stuff, we need to have at least two shades of gray */
869 if ((GetWindowLong(hwnd
, GWL_STYLE
) & ZYZGS_3D
) && fSupport3D
)
871 Draw3DRect(hdc
, &rc1
, pgauge
->wWidth3D
, DRAW3D_OUT
);
872 InflateRect(&rc1
, ~(pgauge
->wWidth3D
), ~(pgauge
->wWidth3D
));
874 Draw3DFaceFrame(hdc
, &rc1
, pgauge
->wWidthBezelFace
);
875 InflateRect(&rc1
, ~(pgauge
->wWidthBezelFace
), ~(pgauge
->wWidthBezelFace
));
877 Draw3DRect(hdc
, &rc1
, pgauge
->wWidth3D
, DRAW3D_IN
);
878 InflateRect(&rc1
, ~(pgauge
->wWidth3D
), ~(pgauge
->wWidth3D
));
880 /* draw a black border on the _inside_ */
881 FrameRect(hdc
, &rc1
, (HBRUSH
) GetStockObject(BLACK_BRUSH
));
883 /* we want to draw _just inside_ the black border */
884 InflateRect(&rc1
, -1, -1);
886 /* add all the other pixels into the border width */
887 Offset
+= (2 * pgauge
->wWidth3D
) + pgauge
->wWidthBezelFace
+ 1;
890 /* dup--one rc for 'how much filled', one rc for 'how much empty' */
893 /* get the range--make sure it's a valid range */
894 if ((iRange
= pgauge
->wRange
) <= 0)
897 /* get the position--greater than 100% would be bad */
898 if ((iPos
= pgauge
->wPosition
) > iRange
)
901 /* compute the actual size of the gauge */
902 dx
= rc1
.right
- rc1
.left
;
903 dy
= rc1
.bottom
- rc1
.top
;
904 wGomerX
= (WORD
)((DWORD
)iPos
* dx
/ iRange
);
905 wGomerY
= (WORD
)((DWORD
)iPos
* dy
/ iRange
);
907 /* get the orientation and munge rects accordingly */
908 switch (pgauge
->wOrientation
)
910 case ZYZG_ORIENT_RIGHTTOLEFT
:
911 rc1
.left
= rc2
.right
= rc1
.right
- wGomerX
;
914 case ZYZG_ORIENT_BOTTOMTOTOP
:
915 rc1
.top
= rc2
.bottom
= rc1
.bottom
- wGomerY
;
918 case ZYZG_ORIENT_TOPTOBOTTOM
:
919 rc1
.bottom
= rc2
.top
+= wGomerY
;
923 rc1
.right
= rc2
.left
+= wGomerX
;
927 /* select the correct font */
928 hFont
= (HFONT
) SelectObject(hdc
, pgauge
->hFont
);
930 /* build up a string to blit out--ie the meaning of life: "42%" */
931 wsprintf(ach
, "%3d%%", (WORD
)((DWORD
)iPos
* 100 / iRange
));
932 /* Win32s has no GetTextExtent(); let's try GetTextExtentPoint() instead */
933 #if defined(__WIN32__)
934 GetTextExtentPoint(hdc
, ach
, wGomerX
= lstrlen(ach
), &size
);
937 dwExtent
= GetTextExtent(hdc
, ach
, wGomerX
= lstrlen(ach
));
941 /* Draw the finished (ie the percent done) side of box. If
942 * ZYZG_WW_POSITION is 42, (in range of 0 to 100) this ExtTextOut
943 * draws the meaning of life (42%) bar.
945 ExtTextOut(hdc
, (dx
- LOWORD(dwExtent
)) / 2 + Offset
,
946 (dy
- HIWORD(dwExtent
)) / 2 + Offset
,
947 ETO_OPAQUE
| ETO_CLIPPED
, &rc2
, ach
, wGomerX
, NULL
);
949 /* Reverse fore and back colors for drawing the undone (ie the non-
950 * finished) side of the box.
952 SetBkColor(hdc
, pgauge
->rgbTextColor
);
953 SetTextColor(hdc
, pgauge
->rgbBkColor
);
955 ExtTextOut(hdc
, (dx
- LOWORD(dwExtent
)) / 2 + Offset
,
956 (dy
- HIWORD(dwExtent
)) / 2 + Offset
,
957 ETO_OPAQUE
| ETO_CLIPPED
, &rc1
, ach
, wGomerX
, NULL
);
959 /* unselect the font */
960 SelectObject(hdc
, hFont
);
964 /** LRESULT FAR PASCAL gaugeWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
967 * This is the control's window procedure. Its purpose is to handle
968 * special messages for this custom control.
970 * The special control messages for the gauge control are:
972 * ZYZG_SETRANGE : Sets the range of the gauge. In other
973 * words, the number of parts that make a
976 * ZYZG_GETRANGE : Returns the current range of the gauge.
978 * ZYZG_SETORIENTATION : Sets the orientation of the gauge. This
979 * can be one of the ZYZG_ORIENT_?? msgs.
981 * ZYZG_GETORIENTATION : Gets the current orientation of the
984 * ZYZG_SETPOSITION : Sets the current position of the gauge.
985 * In other words, how many pieces of the
986 * whole have been used.
988 * ZYZG_GETPOSITION : Gets the current position of the gauge.
990 * ZYZG_SETDELTAPOS : Sets the position of the gauge +/- the
993 * ZYZG_SETFGCOLOR : Sets the foreground (percent done) color.
995 * ZYZG_GETFGCOLOR : Gets the foreground (percent done) color.
997 * ZYZG_SETBKCOLOR : Sets the background (percent not done)
1000 * ZYZG_GETBKCOLOR : Gets the background (percent not done)
1003 * WM_SETFONT : Sets the font to use for the percentage
1004 * text of the gauge.
1006 * WM_GETFONT : Gets the current font in use by the
1013 /* LRESULT FAR PASCAL */
1015 LRESULT APIENTRY _EXPORT
gaugeWndProc(HWND hwnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
1022 // pgauge = (PZYZGAUGE)GetWindowWord(hwnd, ZYZG_WW_PZYZGAUGE);
1023 pgauge
= (PZYZGAUGE
)GetWindowLong(hwnd
, ZYZG_WW_PZYZGAUGE
);
1025 /* break to get DefWindowProc() */
1029 /* need to allocate a control block */
1030 // pgauge = (PZYZGAUGE)LocalAlloc(LPTR, sizeof(ZYZGAUGE));
1031 pgauge
= (PZYZGAUGE
)malloc(sizeof(ZYZGAUGE
));
1035 /* hang on to this control block */
1036 // SetWindowWord(hwnd, ZYZG_WW_PZYZGAUGE, (WORD)pgauge);
1037 SetWindowLong(hwnd
, ZYZG_WW_PZYZGAUGE
, (LONG
)pgauge
);
1039 /* fill control block with defaults */
1040 pgauge
->wRange
= ZYZG_DEF_RANGE
;
1041 pgauge
->wPosition
= ZYZG_DEF_POSITION
;
1042 pgauge
->wOrientation
= ZYZG_DEF_ORIENTATION
;
1043 pgauge
->wWidth3D
= ZYZG_DEF_WIDTH3D
;
1044 pgauge
->wWidthBezelFace
= ZYZG_DEF_BEZELFACE
;
1045 pgauge
->rgbTextColor
= rgbDefTextColor
;
1046 pgauge
->rgbBkColor
= rgbDefBkColor
;
1048 /* use system font */
1049 SendMessage(hwnd
, WM_SETFONT
, (WPARAM
)NULL
, 0L);
1051 /* go to DefWindowProc() to finish the job */
1055 /* get rid of the control's memory */
1057 // LocalFree((HANDLE)pgauge);
1061 case ZYZG_GETPOSITION
:
1062 return (pgauge
->wPosition
);
1065 return (pgauge
->wRange
);
1067 case ZYZG_GETORIENTATION
:
1068 return (pgauge
->wOrientation
);
1070 case ZYZG_GETWIDTH3D
:
1071 return (pgauge
->wWidth3D
);
1073 case ZYZG_GETBEZELFACE
:
1074 return (pgauge
->wWidthBezelFace
);
1076 case ZYZG_GETBKCOLOR
:
1077 return (pgauge
->rgbTextColor
);
1079 case ZYZG_GETFGCOLOR
:
1080 return (pgauge
->rgbBkColor
);
1082 case ZYZG_SETBKCOLOR
:
1083 pgauge
->rgbBkColor
= lParam
;
1086 case ZYZG_SETFGCOLOR
:
1087 pgauge
->rgbTextColor
= lParam
;
1091 case ZYZG_SETPOSITION
:
1092 pgauge
->wPosition
= wParam
;
1095 GetClientRect(hwnd
, &rc
);
1096 if ((GetWindowLong(hwnd
, GWL_STYLE
) & ZYZGS_3D
) && fSupport3D
)
1098 wParam
= (2 * pgauge
->wWidth3D
) +
1099 pgauge
->wWidthBezelFace
+ 2;
1105 InflateRect(&rc
, ~(wParam
), ~(wParam
));
1106 InvalidateRect(hwnd
, &rc
, FALSE
);
1111 pgauge
->wRange
= wParam
;
1112 goto zyzgForceRepaint
;
1114 case ZYZG_SETORIENTATION
:
1115 pgauge
->wOrientation
= wParam
;
1116 goto zyzgForceRepaint
;
1118 case ZYZG_SETWIDTH3D
:
1119 pgauge
->wWidth3D
= wParam
;
1122 InvalidateRect(hwnd
, NULL
, FALSE
);
1126 case ZYZG_SETBEZELFACE
:
1127 pgauge
->wWidthBezelFace
= wParam
;
1128 goto zyzgForceRepaint3D
;
1130 case ZYZG_SETDELTAPOS
:
1131 /* Watcom doesn't like the following line so removing typecasts */
1132 /* (int)pgauge->wPosition += (int)wParam; */
1133 pgauge
->wPosition
+= wParam
;
1134 goto zyzgForceRepaint
;
1137 BeginPaint(hwnd
, &ps
);
1138 gaugePaint(hwnd
, ps
.hdc
);
1139 EndPaint(hwnd
, &ps
);
1143 hFont
= pgauge
->hFont
;
1145 /* if system font, then return NULL handle */
1146 return (long)((hFont
== GetStockObject(SYSTEM_FONT
)) ? NULL
: hFont
);
1149 /* if NULL hFont, use system font */
1150 if (!(hFont
= (HFONT
)wParam
))
1151 hFont
= (HFONT
) GetStockObject(SYSTEM_FONT
);
1153 pgauge
->hFont
= hFont
;
1155 /* redraw if indicated in message */
1158 InvalidateRect(hwnd
, NULL
, TRUE
);
1164 /* let the dialog mangler take care of this message */
1165 return (DefWindowProc(hwnd
, uMsg
, wParam
, lParam
));
1166 } /* gaugeWndProc() */
1169 /** EOF: zyzgauge.c **/