]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/gauge.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxGauge 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"
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
58 #define ZYZGS_3D 0x8000L /* control will be 3D */
60 /* public function prototypes */
61 BOOL FAR PASCAL
gaugeInit(HINSTANCE hInstance
);
63 #define USE_PROGRESS_BAR 1
65 #if defined(__WIN95__) && !defined(__GNUWIN32__)
69 #if !USE_SHARED_LIBRARY
70 IMPLEMENT_DYNAMIC_CLASS(wxGauge
, wxControl
)
73 bool wxGauge::Create(wxWindow
*parent
, const wxWindowID id
,
78 const wxValidator
& validator
,
81 static bool wxGaugeInitialised
= FALSE
;
83 if ( !wxGaugeInitialised
)
85 if (!gaugeInit((HWND
) wxGetInstance()))
86 wxFatalError("Cannot initalize Gauge library");
87 wxGaugeInitialised
= TRUE
;
91 SetValidator(validator
);
93 if (parent
) parent
->AddChild(this);
96 SetBackgroundColour(parent
->GetDefaultBackgroundColour()) ;
97 SetForegroundColour(parent
->GetDefaultForegroundColour()) ;
99 m_useProgressBar
= FALSE
;
100 m_windowStyle
= style
;
103 m_windowId
= (int)NewControlId();
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
;
119 if (m_useProgressBar
)
121 #if defined(__WIN95__) && USE_PROGRESS_BAR
122 long msFlags
= WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
;
125 CreateWindowEx(MakeExtendedStyle(m_windowStyle
), PROGRESS_CLASS
, NULL
, msFlags
,
126 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
127 wxGetInstance(), NULL
);
129 m_hWnd
= (WXHWND
)wx_button
;
131 // Subclass again for purposes of dialog editing mode
132 SubclassWin((WXHWND
) wx_button
);
134 SendMessage((HWND
) GetHWND(), PBM_SETRANGE
, 0, MAKELPARAM(0, range
));
139 long msFlags
= WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
;
140 /* if (m_windowStyle & wxTHREED) */
144 CreateWindowEx(MakeExtendedStyle(m_windowStyle
), "zYzGauge", NULL
, msFlags
,
145 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
146 wxGetInstance(), NULL
);
148 m_hWnd
= (WXHWND
)wx_button
;
150 // Subclass again for purposes of dialog editing mode
151 SubclassWin((WXHWND
)wx_button
);
155 if (m_windowStyle
& wxGA_HORIZONTAL
)
156 wOrient
= ZYZG_ORIENT_LEFTTORIGHT
;
158 wOrient
= ZYZG_ORIENT_BOTTOMTOTOP
;
160 SendMessage(wx_button
, ZYZG_SETORIENTATION
, wOrient
, 0);
161 SendMessage(wx_button
, ZYZG_SETRANGE
, range
, 0);
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()));
167 SetFont(* parent
->GetFont());
173 SetSize(x
, y
, width
, height
);
175 ShowWindow((HWND
) GetHWND(), SW_SHOW
);
180 void wxGauge::SetSize(const int x
, const int y
, const int width
, const int height
, const int sizeFlags
)
182 int currentX
, currentY
;
183 GetPosition(¤tX
, ¤tY
);
189 if (x
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
191 if (y
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
194 // If we're prepared to use the existing size, then...
195 if (width
== -1 && height
== -1 && ((sizeFlags
& wxSIZE_AUTO
) != wxSIZE_AUTO
))
200 // Deal with default size (using -1 values)
202 w1
= DEFAULT_ITEM_WIDTH
;
205 h1
= DEFAULT_ITEM_HEIGHT
;
207 MoveWindow((HWND
) GetHWND(), x1
, y1
, w1
, h1
, TRUE
);
209 #if WXWIN_COMPATIBILITY
210 GetEventHandler()->OldOnSize(width
, height
);
212 wxSizeEvent
event(wxSize(width
, height
), m_windowId
);
213 event
.eventObject
= this;
214 GetEventHandler()->ProcessEvent(event
);
218 void wxGauge::SetShadowWidth(const int w
)
220 if (m_useProgressBar
)
224 SendMessage((HWND
) GetHWND(), ZYZG_SETWIDTH3D
, w
, 0);
227 void wxGauge::SetBezelFace(const int w
)
229 if (m_useProgressBar
)
233 SendMessage((HWND
) GetHWND(), ZYZG_SETBEZELFACE
, w
, 0);
236 void wxGauge::SetRange(const int r
)
240 #if defined(__WIN95__) && USE_PROGRESS_BAR
241 if (m_useProgressBar
)
242 SendMessage((HWND
) GetHWND(), PBM_SETRANGE
, 0, MAKELPARAM(0, r
));
245 SendMessage((HWND
) GetHWND(), ZYZG_SETRANGE
, r
, 0);
248 void wxGauge::SetValue(const int pos
)
252 #if defined(__WIN95__) && USE_PROGRESS_BAR
253 if (m_useProgressBar
)
254 SendMessage((HWND
) GetHWND(), PBM_SETPOS
, pos
, 0);
257 SendMessage((HWND
) GetHWND(), ZYZG_SETPOSITION
, pos
, 0);
260 int wxGauge::GetShadowWidth(void) const
262 if (m_useProgressBar
)
265 return (int) SendMessage((HWND
) GetHWND(), ZYZG_GETWIDTH3D
, 0, 0);
268 int wxGauge::GetBezelFace(void) const
270 if (m_useProgressBar
)
273 return (int) SendMessage((HWND
) GetHWND(), ZYZG_GETBEZELFACE
, 0, 0);
276 int wxGauge::GetRange(void) const
278 if (m_useProgressBar
)
281 return (int) SendMessage((HWND
) GetHWND(), ZYZG_GETRANGE
, 0, 0);
284 int wxGauge::GetValue(void) const
286 if (m_useProgressBar
)
289 return (int) SendMessage((HWND
) GetHWND(), ZYZG_GETPOSITION
, 0, 0);
292 void wxGauge::SetForegroundColour(const wxColour
& col
)
294 m_foregroundColour
= col
;
295 if (m_useProgressBar
)
299 SendMessage((HWND
) GetHWND(), ZYZG_SETFGCOLOR
, 0, RGB(col
.Red(), col
.Green(), col
.Blue()));
302 void wxGauge::SetBackgroundColour(const wxColour
& col
)
304 m_backgroundColour
= col
;
305 if (m_useProgressBar
)
309 SendMessage((HWND
) GetHWND(), ZYZG_SETBKCOLOR
, 0, RGB(col
.Red(), col
.Green(), col
.Blue()));
316 * This module contains functions for creating nifty 3D borders
317 * around controls like zYzGauge.
320 * 3/14/91 cjp put in this comment
321 * 6/19/92 cjp touched it a bit
326 // (C) Copyright Microsoft Corp. 1992. All rights reserved.
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.
336 /* get the includes we need */
339 /* misc. control flag defines */
340 #define DRAW3D_IN 0x0001
341 #define DRAW3D_OUT 0x0002
343 #define DRAW3D_TOPLINE 0x0004
344 #define DRAW3D_BOTTOMLINE 0x0008
345 #define DRAW3D_LEFTLINE 0x0010
346 #define DRAW3D_RIGHTLINE 0x0020
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
);
355 /** void FAR PASCAL Draw3DFaceFrame(HDC hdc, LPRECT rc, WORD wWidth)
358 * This function draws a flat frame with the current button-face
362 * HDC hdc : The DC to draw into.
364 * LPRECT rc : The containing rect for the new frame.
366 * WORD wWidth : The width of the frame to draw.
368 * RETURN (void FAR PASCAL):
369 * The frame will have been drawn into the DC.
375 void FAR PASCAL
Draw3DFaceFrame(HDC hdc
, LPRECT rc
, WORD wWidth
)
380 /* don't go through a bunch of work if we don't have to */
384 /* set up color to be button-face color--so it may not be gray */
385 rgbOld
= SetBkColor(hdc
, GetSysColor(COLOR_BTNFACE
));
387 /* perform CopyRect w/o bloody windows style overhead */
393 rc1
.bottom
= rc
->top
+ wWidth
;
394 rc1
.right
= rc
->right
;
397 ExtTextOut(hdc
, rc1
.left
, rc1
.top
, ETO_OPAQUE
, &rc1
, NULL
, 0, NULL
);
400 rc1
.left
= rc
->right
- wWidth
;
401 rc1
.bottom
= rc
->bottom
;
403 /* blast this part now */
404 ExtTextOut(hdc
, rc1
.left
, rc1
.top
, ETO_OPAQUE
, &rc1
, NULL
, 0, NULL
);
408 rc1
.right
= rc
->left
+ wWidth
;
410 /* and another part */
411 ExtTextOut(hdc
, rc1
.left
, rc1
.top
, ETO_OPAQUE
, &rc1
, NULL
, 0, NULL
);
414 rc1
.right
= rc
->right
;
415 rc1
.top
= rc
->bottom
- wWidth
;
418 ExtTextOut(hdc
, rc1
.left
, rc1
.top
, ETO_OPAQUE
, &rc1
, NULL
, 0, NULL
);
420 /* restore the old bk color */
421 SetBkColor(hdc
, rgbOld
);
422 } /* Draw3DFaceFrame() */
425 /** void FAR PASCAL Draw3DRect(HDC, LPRECT, WORD, WORD)
428 * Draws a 3D rectangle that is shaded. wFlags can be used to
429 * control how the rectangle looks.
432 * HDC hdc : Handle to the device context that will be
433 * used to display the rectangle.
435 * RECT rect : A rectangle describing the dimensions of
436 * the rectangle in device coordinates.
438 * WORD wShadowWidth : Width of the shadow in device coordinates.
440 * WORD wFlags : The following flags may be passed to describe
441 * the style of the rectangle:
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.
447 * DRAW3D_OUT : The shadow is drawn such that
448 * the box appears to be sticking out of the
451 * RETURN (void FAR PASCAL):
452 * The 3D looking rectangle will have been drawn into the DC.
458 void FAR PASCAL
Draw3DRect(HDC hdc
, LPRECT lpRect
,
459 WORD wShadowWidth
, WORD wFlags
)
461 /* sanity check--don't work if you don't have to! */
462 if (!wShadowWidth
|| !RectVisible(hdc
, lpRect
))
465 /* draw the top line */
466 Draw3DLine(hdc
, lpRect
->left
, lpRect
->top
,
467 lpRect
->right
- lpRect
->left
,
468 wShadowWidth
, DRAW3D_TOPLINE
| wFlags
);
471 Draw3DLine(hdc
, lpRect
->right
, lpRect
->top
,
472 lpRect
->bottom
- lpRect
->top
,
473 wShadowWidth
, DRAW3D_RIGHTLINE
| wFlags
);
476 Draw3DLine(hdc
, lpRect
->left
, lpRect
->bottom
,
477 lpRect
->right
- lpRect
->left
,
478 wShadowWidth
, DRAW3D_BOTTOMLINE
| wFlags
);
481 Draw3DLine(hdc
, lpRect
->left
, lpRect
->top
,
482 lpRect
->bottom
- lpRect
->top
,
483 wShadowWidth
, DRAW3D_LEFTLINE
| wFlags
);
487 /** void FAR PASCAL Draw3DLine(HDC hdc, WORD x, WORD y, WORD nLen,
490 * Draws a 3D line that can be used to make a 3D box.
493 * HDC hdc : Handle to the device context that will be
494 * used to display the 3D line.
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.
502 * WORD wShadowWidth : Width of the shadow in device coordinates.
504 * WORD wFlags : The following flags may be passed to
505 * describe the style of the 3D line:
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.
511 * DRAW3D_OUT : The shadow is drawn such that
512 * the box appears to be sticking out of the
515 * DRAW3D_TOPLINE, _BOTTOMLINE, _LEFTLINE, and
516 * _RIGHTLINE : Specifies that a "top",
517 * "Bottom", "Left", or"Right" line is to be
520 * RETURN (void FAR PASCAL):
521 * The line will have been drawn into the DC.
527 void FAR PASCAL
Draw3DLine(HDC hdc
, WORD x
, WORD y
, WORD nLen
,
528 WORD wShadowWidth
, WORD wFlags
)
533 POINT Point
[ 4 ]; /* define a polgon with 4 points */
535 /* if width is zero, don't do nothin'! */
539 /* define shape of polygon--origin is always the same */
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
547 if (wFlags
& DRAW3D_TOPLINE
)
549 /* across to right */
550 Point
[1].x
= x
+ nLen
- (wShadowWidth
== 1 ? 1 : 0);
554 Point
[2].x
= x
+ nLen
- wShadowWidth
;
555 Point
[2].y
= y
+ wShadowWidth
;
557 /* accross to left */
558 Point
[3].x
= x
+ wShadowWidth
;
559 Point
[3].y
= y
+ wShadowWidth
;
561 /* select 'dark' brush if 'in'--'light' for 'out' */
562 fDark
= (wFlags
& DRAW3D_IN
) ? TRUE
: FALSE
;
565 /* possibly the bottom? */
566 else if (wFlags
& DRAW3D_BOTTOMLINE
)
568 /* across to right */
569 Point
[1].x
= x
+ nLen
;
573 Point
[2].x
= x
+ nLen
- wShadowWidth
;
574 Point
[2].y
= y
- wShadowWidth
;
576 /* accross to left */
577 Point
[3].x
= x
+ wShadowWidth
;
578 Point
[3].y
= y
- wShadowWidth
;
580 /* select 'light' brush if 'in' */
581 fDark
= (wFlags
& DRAW3D_IN
) ? FALSE
: TRUE
;
584 /* ok, it's gotta be left? */
585 else if (wFlags
& DRAW3D_LEFTLINE
)
589 Point
[1].y
= y
+ nLen
- (wShadowWidth
== 1 ? 1 : 0);
592 Point
[2].x
= x
+ wShadowWidth
;
593 Point
[2].y
= y
+ nLen
- wShadowWidth
;
596 Point
[3].x
= x
+ wShadowWidth
;
597 Point
[3].y
= y
+ wShadowWidth
;
599 /* select 'dark' brush if 'in'--'light' for 'out' */
600 fDark
= (wFlags
& DRAW3D_IN
) ? TRUE
: FALSE
;
603 /* well maybe it's for the right side? */
604 else if (wFlags
& DRAW3D_RIGHTLINE
)
608 Point
[1].y
= y
+ nLen
;
611 Point
[2].x
= x
- wShadowWidth
;
612 Point
[2].y
= y
+ nLen
- wShadowWidth
;
615 Point
[3].x
= x
- wShadowWidth
;
616 Point
[3].y
= y
+ wShadowWidth
;
618 /* select 'light' brush if 'in' */
619 fDark
= (wFlags
& DRAW3D_IN
) ? FALSE
: TRUE
;
625 /* select NULL_PEN for no borders */
626 hOldPen
= SelectObject(hdc
, GetStockObject(NULL_PEN
));
628 /* select the appropriate color for the fill */
630 hOldBrush
= SelectObject(hdc
, GetStockObject(GRAY_BRUSH
));
632 hOldBrush
= SelectObject(hdc
, GetStockObject(WHITE_BRUSH
));
634 /* finally, draw the dern thing */
635 Polygon(hdc
, (LPPOINT
)&Point
, 4);
637 /* restore what we killed */
638 SelectObject(hdc
, hOldBrush
);
639 SelectObject(hdc
, hOldPen
);
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.
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):
655 * EXPORTS gaugeWndProc
657 * You then need initialize the class before you use it:
659 * if (!gaugeInit(hInstance))
660 * die a horrible death
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).
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.
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*
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.
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.
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
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.
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.
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):
714 * ZYZG_ORIENT_LEFTTORIGHT (0)
715 * ZYZG_ORIENT_RIGHTTOLEFT (1)
716 * ZYZG_ORIENT_BOTTOMTOTOP (2)
717 * ZYZG_ORIENT_TOPTOBOTTOM (3)
720 * 3/12/91 cjp put in this comment
721 * 6/19/92 cjp touched it a bit
726 // (C) Copyright Microsoft Corp. 1992. All rights reserved.
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.
736 /* get the includes we need */
743 // #include "zyz3d.h"
744 // #include "zyzgauge.h"
747 /* static global variables */
748 static char gszzYzGaugeClass
[] = "zYzGauge";
751 /* window word position definitions */
752 #define ZYZG_WW_PZYZGAUGE 0
753 /* #define ZYZG_WW_EXTRABYTES 2 */
754 #define ZYZG_WW_EXTRABYTES 4
757 /* control block structure typedef */
758 typedef struct tZYZGAUGE
764 WORD wWidthBezelFace
;
769 } ZYZGAUGE
, *PZYZGAUGE
, FAR
*LPZYZGAUGE
;
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
781 /* the default settings for drawing colors--display dependent */
782 static DWORD rgbDefTextColor
;
783 static DWORD rgbDefBkColor
;
784 static BOOL fSupport3D
;
786 #if !defined(APIENTRY) // NT defines APIENTRY, 3.x not
787 #define APIENTRY FAR PASCAL
793 #define _EXPORT _export
794 typedef signed short int SHORT
;
797 /* internal function prototypes */
798 static void PASCAL
gaugePaint(HWND
, HDC
);
799 /* LRESULT FAR PASCAL */
800 LRESULT APIENTRY _EXPORT
gaugeWndProc(HWND
, UINT
, WPARAM
, LPARAM
);
804 /** BOOL FAR PASCAL gaugeInit(HINSTANCE hInstance)
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!
813 * HINSTANCE hInstance : Instance handle to register class with.
816 * The return value is TRUE if the zYzGauge class was successfully
817 * registered. It is FALSE if the initialization fails.
823 //#pragma alloc_text(init, gaugeInit)
825 BOOL FAR PASCAL
gaugeInit(HINSTANCE hInstance
)
827 static BOOL fRegistered
= FALSE
;
831 /* assume already registered if not first instance */
835 /* fill in the class structure for the zyzgauge control */
836 wc
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
838 wc
.lpszMenuName
= NULL
;
839 wc
.lpszClassName
= gszzYzGaugeClass
;
840 wc
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+ 1);
841 wc
.hInstance
= hInstance
;
844 wc
.style
= CS_GLOBALCLASS
| CS_HREDRAW
| CS_VREDRAW
;
846 wc
.style
= CS_HREDRAW
| CS_VREDRAW
;
849 wc
.lpfnWndProc
= gaugeWndProc
;
851 wc
.cbWndExtra
= ZYZG_WW_EXTRABYTES
;
853 /* attempt to register it--return FALSE if fail */
854 if (!RegisterClass(&wc
))
857 /* Get a DC to determine whether device is mono or not, and set
858 * default foreground/background colors as appropriate.
860 if ((hdc
= CreateIC("DISPLAY", NULL
, NULL
, 0L)))
862 /* check for mono-display */
863 if ((GetDeviceCaps(hdc
, BITSPIXEL
) == 1) &&
864 (GetDeviceCaps(hdc
, PLANES
) == 1))
866 /* using a mono DC--white foreground, black background */
867 rgbDefTextColor
= RGB(255, 255, 255);
868 rgbDefBkColor
= RGB(0, 0, 0);
871 /* good! we have color: blue foreground, white background */
874 rgbDefTextColor
= RGB(0, 0, 255);
875 rgbDefBkColor
= RGB(255, 255, 255);
878 /* need at _least_ 8 for two shades of gray (>=VGA) */
879 fSupport3D
= (GetDeviceCaps(hdc
, NUMCOLORS
) >= 8) ? TRUE
: FALSE
;
881 /* get rid of the DC (IC) */
885 /* uh-oh... can't get DC (IC)... fail */
888 /* unregister the class */
889 UnregisterClass(gszzYzGaugeClass
, hInstance
);
894 return (fRegistered
= TRUE
);
898 /** static void PASCAL gaugePaint(HWND hwnd, HDC hdc)
901 * This function is responsible for painting the zYzGauge control.
904 * HWND hwnd : The window handle for the gauge.
906 * HDC hdc : The DC for the gauge's window.
909 * The control will have been painted.
915 static void PASCAL
gaugePaint(HWND hwnd
, HDC hdc
)
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__)
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
);
935 /* set the colors into for the gauge into the control */
936 SetTextColor(hdc
, pgauge
->rgbTextColor
);
937 SetBkColor(hdc
, pgauge
->rgbBkColor
);
939 /* draw black rectangle for gauge */
940 GetClientRect(hwnd
, &rc1
);
942 /* draw a black border on the _outside_ */
943 FrameRect(hdc
, &rc1
, GetStockObject(BLACK_BRUSH
));
945 /* we want to draw _just inside_ the black border */
946 InflateRect(&rc1
, -1, -1);
948 /* one line thick so far... */
949 // Offset = (WORD) 1;
951 /* for 3D stuff, we need to have at least two shades of gray */
952 if ((GetWindowLong(hwnd
, GWL_STYLE
) & ZYZGS_3D
) && fSupport3D
)
954 Draw3DRect(hdc
, &rc1
, pgauge
->wWidth3D
, DRAW3D_OUT
);
955 InflateRect(&rc1
, ~(pgauge
->wWidth3D
), ~(pgauge
->wWidth3D
));
957 Draw3DFaceFrame(hdc
, &rc1
, pgauge
->wWidthBezelFace
);
958 InflateRect(&rc1
, ~(pgauge
->wWidthBezelFace
), ~(pgauge
->wWidthBezelFace
));
960 Draw3DRect(hdc
, &rc1
, pgauge
->wWidth3D
, DRAW3D_IN
);
961 InflateRect(&rc1
, ~(pgauge
->wWidth3D
), ~(pgauge
->wWidth3D
));
963 /* draw a black border on the _inside_ */
964 FrameRect(hdc
, &rc1
, GetStockObject(BLACK_BRUSH
));
966 /* we want to draw _just inside_ the black border */
967 InflateRect(&rc1
, -1, -1);
969 /* add all the other pixels into the border width */
970 Offset
+= (2 * pgauge
->wWidth3D
) + pgauge
->wWidthBezelFace
+ 1;
973 /* dup--one rc for 'how much filled', one rc for 'how much empty' */
976 /* get the range--make sure it's a valid range */
977 if ((iRange
= pgauge
->wRange
) <= 0)
980 /* get the position--greater than 100% would be bad */
981 if ((iPos
= pgauge
->wPosition
) > iRange
)
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
);
990 /* get the orientation and munge rects accordingly */
991 switch (pgauge
->wOrientation
)
993 case ZYZG_ORIENT_RIGHTTOLEFT
:
994 rc1
.left
= rc2
.right
= rc1
.right
- wGomerX
;
997 case ZYZG_ORIENT_BOTTOMTOTOP
:
998 rc1
.top
= rc2
.bottom
= rc1
.bottom
- wGomerY
;
1001 case ZYZG_ORIENT_TOPTOBOTTOM
:
1002 rc1
.bottom
= rc2
.top
+= wGomerY
;
1006 rc1
.right
= rc2
.left
+= wGomerX
;
1010 /* select the correct font */
1011 hFont
= SelectObject(hdc
, pgauge
->hFont
);
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
);
1020 dwExtent
= GetTextExtent(hdc
, ach
, wGomerX
= lstrlen(ach
));
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.
1028 ExtTextOut(hdc
, (dx
- LOWORD(dwExtent
)) / 2 + Offset
,
1029 (dy
- HIWORD(dwExtent
)) / 2 + Offset
,
1030 ETO_OPAQUE
| ETO_CLIPPED
, &rc2
, ach
, wGomerX
, NULL
);
1032 /* Reverse fore and back colors for drawing the undone (ie the non-
1033 * finished) side of the box.
1035 SetBkColor(hdc
, pgauge
->rgbTextColor
);
1036 SetTextColor(hdc
, pgauge
->rgbBkColor
);
1038 ExtTextOut(hdc
, (dx
- LOWORD(dwExtent
)) / 2 + Offset
,
1039 (dy
- HIWORD(dwExtent
)) / 2 + Offset
,
1040 ETO_OPAQUE
| ETO_CLIPPED
, &rc1
, ach
, wGomerX
, NULL
);
1042 /* unselect the font */
1043 SelectObject(hdc
, hFont
);
1044 } /* gaugePaint() */
1047 /** LRESULT FAR PASCAL gaugeWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1050 * This is the control's window procedure. Its purpose is to handle
1051 * special messages for this custom control.
1053 * The special control messages for the gauge control are:
1055 * ZYZG_SETRANGE : Sets the range of the gauge. In other
1056 * words, the number of parts that make a
1059 * ZYZG_GETRANGE : Returns the current range of the gauge.
1061 * ZYZG_SETORIENTATION : Sets the orientation of the gauge. This
1062 * can be one of the ZYZG_ORIENT_?? msgs.
1064 * ZYZG_GETORIENTATION : Gets the current orientation of the
1067 * ZYZG_SETPOSITION : Sets the current position of the gauge.
1068 * In other words, how many pieces of the
1069 * whole have been used.
1071 * ZYZG_GETPOSITION : Gets the current position of the gauge.
1073 * ZYZG_SETDELTAPOS : Sets the position of the gauge +/- the
1076 * ZYZG_SETFGCOLOR : Sets the foreground (percent done) color.
1078 * ZYZG_GETFGCOLOR : Gets the foreground (percent done) color.
1080 * ZYZG_SETBKCOLOR : Sets the background (percent not done)
1083 * ZYZG_GETBKCOLOR : Gets the background (percent not done)
1086 * WM_SETFONT : Sets the font to use for the percentage
1087 * text of the gauge.
1089 * WM_GETFONT : Gets the current font in use by the
1096 /* LRESULT FAR PASCAL */
1098 LRESULT APIENTRY _EXPORT
gaugeWndProc(HWND hwnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
1105 // pgauge = (PZYZGAUGE)GetWindowWord(hwnd, ZYZG_WW_PZYZGAUGE);
1106 pgauge
= (PZYZGAUGE
)GetWindowLong(hwnd
, ZYZG_WW_PZYZGAUGE
);
1108 /* break to get DefWindowProc() */
1112 /* need to allocate a control block */
1113 // pgauge = (PZYZGAUGE)LocalAlloc(LPTR, sizeof(ZYZGAUGE));
1114 pgauge
= (PZYZGAUGE
)malloc(sizeof(ZYZGAUGE
));
1118 /* hang on to this control block */
1119 // SetWindowWord(hwnd, ZYZG_WW_PZYZGAUGE, (WORD)pgauge);
1120 SetWindowLong(hwnd
, ZYZG_WW_PZYZGAUGE
, (LONG
)pgauge
);
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
;
1131 /* use system font */
1132 SendMessage(hwnd
, WM_SETFONT
, (WPARAM
)NULL
, 0L);
1134 /* go to DefWindowProc() to finish the job */
1138 /* get rid of the control's memory */
1140 // LocalFree((HANDLE)pgauge);
1144 case ZYZG_GETPOSITION
:
1145 return (pgauge
->wPosition
);
1148 return (pgauge
->wRange
);
1150 case ZYZG_GETORIENTATION
:
1151 return (pgauge
->wOrientation
);
1153 case ZYZG_GETWIDTH3D
:
1154 return (pgauge
->wWidth3D
);
1156 case ZYZG_GETBEZELFACE
:
1157 return (pgauge
->wWidthBezelFace
);
1159 case ZYZG_GETBKCOLOR
:
1160 return (pgauge
->rgbTextColor
);
1162 case ZYZG_GETFGCOLOR
:
1163 return (pgauge
->rgbBkColor
);
1165 case ZYZG_SETBKCOLOR
:
1166 pgauge
->rgbBkColor
= lParam
;
1169 case ZYZG_SETFGCOLOR
:
1170 pgauge
->rgbTextColor
= lParam
;
1174 case ZYZG_SETPOSITION
:
1175 pgauge
->wPosition
= wParam
;
1178 GetClientRect(hwnd
, &rc
);
1179 if ((GetWindowLong(hwnd
, GWL_STYLE
) & ZYZGS_3D
) && fSupport3D
)
1181 wParam
= (2 * pgauge
->wWidth3D
) +
1182 pgauge
->wWidthBezelFace
+ 2;
1188 InflateRect(&rc
, ~(wParam
), ~(wParam
));
1189 InvalidateRect(hwnd
, &rc
, FALSE
);
1194 pgauge
->wRange
= wParam
;
1195 goto zyzgForceRepaint
;
1197 case ZYZG_SETORIENTATION
:
1198 pgauge
->wOrientation
= wParam
;
1199 goto zyzgForceRepaint
;
1201 case ZYZG_SETWIDTH3D
:
1202 pgauge
->wWidth3D
= wParam
;
1205 InvalidateRect(hwnd
, NULL
, FALSE
);
1209 case ZYZG_SETBEZELFACE
:
1210 pgauge
->wWidthBezelFace
= wParam
;
1211 goto zyzgForceRepaint3D
;
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
;
1220 BeginPaint(hwnd
, &ps
);
1221 gaugePaint(hwnd
, ps
.hdc
);
1222 EndPaint(hwnd
, &ps
);
1226 hFont
= pgauge
->hFont
;
1228 /* if system font, then return NULL handle */
1229 return (long)((hFont
== GetStockObject(SYSTEM_FONT
)) ? NULL
: hFont
);
1232 /* if NULL hFont, use system font */
1233 if (!(hFont
= (HFONT
)wParam
))
1234 hFont
= GetStockObject(SYSTEM_FONT
);
1236 pgauge
->hFont
= hFont
;
1238 /* redraw if indicated in message */
1241 InvalidateRect(hwnd
, NULL
, TRUE
);
1247 /* let the dialog mangler take care of this message */
1248 return (DefWindowProc(hwnd
, uMsg
, wParam
, lParam
));
1249 } /* gaugeWndProc() */
1252 /** EOF: zyzgauge.c **/