]>
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"
30 #include "wx/msw/gaugemsw.h"
31 #include "wx/msw/private.h"
33 /* gas gauge graph control messages--class "zYzGauge" */
34 #define ZYZG_SETRANGE (WM_USER + 0)
35 #define ZYZG_GETRANGE (WM_USER + 1)
36 #define ZYZG_SETPOSITION (WM_USER + 2)
37 #define ZYZG_GETPOSITION (WM_USER + 3)
38 #define ZYZG_SETORIENTATION (WM_USER + 4)
39 #define ZYZG_GETORIENTATION (WM_USER + 5)
40 #define ZYZG_SETFGCOLOR (WM_USER + 6)
41 #define ZYZG_GETFGCOLOR (WM_USER + 7)
42 #define ZYZG_SETBKCOLOR (WM_USER + 8)
43 #define ZYZG_GETBKCOLOR (WM_USER + 9)
44 #define ZYZG_SETWIDTH3D (WM_USER + 10)
45 #define ZYZG_GETWIDTH3D (WM_USER + 11)
46 #define ZYZG_SETBEZELFACE (WM_USER + 12)
47 #define ZYZG_GETBEZELFACE (WM_USER + 13)
48 #define ZYZG_SETDELTAPOS (WM_USER + 14)
51 /* orientations for ZYZG_WW_ORIENTATION */
52 #define ZYZG_ORIENT_LEFTTORIGHT 0
53 #define ZYZG_ORIENT_RIGHTTOLEFT 1
54 #define ZYZG_ORIENT_BOTTOMTOTOP 2
55 #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 IMPLEMENT_DYNAMIC_CLASS(wxGaugeMSW
, wxControl
)
65 bool wxGaugeMSW::Create(wxWindow
*parent
, wxWindowID id
,
70 const wxValidator
& validator
,
73 static bool wxGaugeMSWInitialised
= FALSE
;
75 if ( !wxGaugeMSWInitialised
)
77 if (!gaugeInit((HINSTANCE
) wxGetInstance()))
78 wxFatalError("Cannot initalize Gauge library");
79 wxGaugeMSWInitialised
= TRUE
;
84 SetValidator(validator
);
85 #endif // wxUSE_VALIDATORS
87 if (parent
) parent
->AddChild(this);
91 SetBackgroundColour(parent
->GetBackgroundColour()) ;
92 SetForegroundColour(parent
->GetForegroundColour()) ;
94 m_windowStyle
= style
;
97 m_windowId
= (int)NewControlId();
106 long msFlags
= WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
;
108 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
);
112 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
113 msFlags
|= WS_CLIPSIBLINGS
;
116 CreateWindowEx(MakeExtendedStyle(m_windowStyle
), wxT("zYzGauge"), NULL
, msFlags
,
117 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
118 wxGetInstance(), NULL
);
120 m_hWnd
= (WXHWND
)wx_button
;
122 // Subclass again for purposes of dialog editing mode
123 SubclassWin((WXHWND
)wx_button
);
127 if (m_windowStyle
& wxGA_HORIZONTAL
)
128 wOrient
= ZYZG_ORIENT_LEFTTORIGHT
;
130 wOrient
= ZYZG_ORIENT_BOTTOMTOTOP
;
132 SendMessage(wx_button
, ZYZG_SETORIENTATION
, wOrient
, 0);
133 SendMessage(wx_button
, ZYZG_SETRANGE
, range
, 0);
135 SendMessage(GetHwnd(), ZYZG_SETFGCOLOR
, 0, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
136 SendMessage(GetHwnd(), ZYZG_SETBKCOLOR
, 0, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
141 SetFont(parent
->GetFont());
147 SetSize(x
, y
, width
, height
);
149 ShowWindow(GetHwnd(), SW_SHOW
);
154 void wxGaugeMSW::SetShadowWidth(int w
)
156 SendMessage(GetHwnd(), ZYZG_SETWIDTH3D
, w
, 0);
159 void wxGaugeMSW::SetBezelFace(int w
)
161 SendMessage(GetHwnd(), ZYZG_SETBEZELFACE
, w
, 0);
164 void wxGaugeMSW::SetRange(int r
)
168 SendMessage(GetHwnd(), ZYZG_SETRANGE
, r
, 0);
171 void wxGaugeMSW::SetValue(int pos
)
175 SendMessage(GetHwnd(), ZYZG_SETPOSITION
, pos
, 0);
178 int wxGaugeMSW::GetShadowWidth(void) const
180 return (int) SendMessage(GetHwnd(), ZYZG_GETWIDTH3D
, 0, 0);
183 int wxGaugeMSW::GetBezelFace(void) const
185 return (int) SendMessage(GetHwnd(), ZYZG_GETBEZELFACE
, 0, 0);
188 int wxGaugeMSW::GetRange(void) const
190 return (int) SendMessage(GetHwnd(), ZYZG_GETRANGE
, 0, 0);
193 int wxGaugeMSW::GetValue(void) const
195 return (int) SendMessage(GetHwnd(), ZYZG_GETPOSITION
, 0, 0);
198 bool wxGaugeMSW::SetForegroundColour(const wxColour
& col
)
200 if ( !wxControl::SetForegroundColour(col
) )
203 SendMessage(GetHwnd(), ZYZG_SETFGCOLOR
, 0, RGB(col
.Red(), col
.Green(), col
.Blue()));
208 bool wxGaugeMSW::SetBackgroundColour(const wxColour
& col
)
210 if ( !wxControl::SetBackgroundColour(col
) )
213 SendMessage(GetHwnd(), ZYZG_SETBKCOLOR
, 0, RGB(col
.Red(), col
.Green(), col
.Blue()));
222 * This module contains functions for creating nifty 3D borders
223 * around controls like zYzGauge.
226 * 3/14/91 cjp put in this comment
227 * 6/19/92 cjp touched it a bit
232 // (C) Copyright Microsoft Corp. 1992. All rights reserved.
234 // You have a royalty-free right to use, modify, reproduce and
235 // distribute the Sample Files (and/or any modified version) in
236 // any way you find useful, provided that you agree that
237 // Microsoft has no warranty obligations or liability for any
238 // Sample Application Files which are modified.
242 /* get the includes we need */
245 /* misc. control flag defines */
246 #define DRAW3D_IN 0x0001
247 #define DRAW3D_OUT 0x0002
249 #define DRAW3D_TOPLINE 0x0004
250 #define DRAW3D_BOTTOMLINE 0x0008
251 #define DRAW3D_LEFTLINE 0x0010
252 #define DRAW3D_RIGHTLINE 0x0020
255 /* public function prototypes */
256 void FAR PASCAL
Draw3DFaceFrame(HDC
, LPRECT
, WORD
);
257 void FAR PASCAL
Draw3DRect(HDC
, LPRECT
, WORD
, WORD
);
258 void FAR PASCAL
Draw3DLine(HDC
, WORD
, WORD
, WORD
, WORD
, WORD
);
261 /** void FAR PASCAL Draw3DFaceFrame(HDC hdc, LPRECT rc, WORD wWidth)
264 * This function draws a flat frame with the current button-face
268 * HDC hdc : The DC to draw into.
270 * LPRECT rc : The containing rect for the new frame.
272 * WORD wWidth : The width of the frame to draw.
274 * RETURN (void FAR PASCAL):
275 * The frame will have been drawn into the DC.
281 void FAR PASCAL
Draw3DFaceFrame(HDC hdc
, LPRECT rc
, WORD wWidth
)
286 /* don't go through a bunch of work if we don't have to */
290 /* set up color to be button-face color--so it may not be gray */
291 rgbOld
= SetBkColor(hdc
, GetSysColor(COLOR_BTNFACE
));
293 /* perform CopyRect w/o bloody windows style overhead */
299 rc1
.bottom
= rc
->top
+ wWidth
;
300 rc1
.right
= rc
->right
;
303 ExtTextOut(hdc
, rc1
.left
, rc1
.top
, ETO_OPAQUE
, &rc1
, NULL
, 0, NULL
);
306 rc1
.left
= rc
->right
- wWidth
;
307 rc1
.bottom
= rc
->bottom
;
309 /* blast this part now */
310 ExtTextOut(hdc
, rc1
.left
, rc1
.top
, ETO_OPAQUE
, &rc1
, NULL
, 0, NULL
);
314 rc1
.right
= rc
->left
+ wWidth
;
316 /* and another part */
317 ExtTextOut(hdc
, rc1
.left
, rc1
.top
, ETO_OPAQUE
, &rc1
, NULL
, 0, NULL
);
320 rc1
.right
= rc
->right
;
321 rc1
.top
= rc
->bottom
- wWidth
;
324 ExtTextOut(hdc
, rc1
.left
, rc1
.top
, ETO_OPAQUE
, &rc1
, NULL
, 0, NULL
);
326 /* restore the old bk color */
327 SetBkColor(hdc
, rgbOld
);
328 } /* Draw3DFaceFrame() */
331 /** void FAR PASCAL Draw3DRect(HDC, LPRECT, WORD, WORD)
334 * Draws a 3D rectangle that is shaded. wFlags can be used to
335 * control how the rectangle looks.
338 * HDC hdc : Handle to the device context that will be
339 * used to display the rectangle.
341 * RECT rect : A rectangle describing the dimensions of
342 * the rectangle in device coordinates.
344 * WORD wShadowWidth : Width of the shadow in device coordinates.
346 * WORD wFlags : The following flags may be passed to describe
347 * the style of the rectangle:
349 * DRAW3D_IN : The shadow is drawn such that
350 * the box appears to be sunk in to the screen.
351 * This is default if 0 is passed.
353 * DRAW3D_OUT : The shadow is drawn such that
354 * the box appears to be sticking out of the
357 * RETURN (void FAR PASCAL):
358 * The 3D looking rectangle will have been drawn into the DC.
364 void FAR PASCAL
Draw3DRect(HDC hdc
, LPRECT lpRect
,
365 WORD wShadowWidth
, WORD wFlags
)
367 /* sanity check--don't work if you don't have to! */
368 if (!wShadowWidth
|| !RectVisible(hdc
, lpRect
))
371 /* draw the top line */
372 Draw3DLine(hdc
, lpRect
->left
, lpRect
->top
,
373 lpRect
->right
- lpRect
->left
,
374 wShadowWidth
, DRAW3D_TOPLINE
| wFlags
);
377 Draw3DLine(hdc
, lpRect
->right
, lpRect
->top
,
378 lpRect
->bottom
- lpRect
->top
,
379 wShadowWidth
, DRAW3D_RIGHTLINE
| wFlags
);
382 Draw3DLine(hdc
, lpRect
->left
, lpRect
->bottom
,
383 lpRect
->right
- lpRect
->left
,
384 wShadowWidth
, DRAW3D_BOTTOMLINE
| wFlags
);
387 Draw3DLine(hdc
, lpRect
->left
, lpRect
->top
,
388 lpRect
->bottom
- lpRect
->top
,
389 wShadowWidth
, DRAW3D_LEFTLINE
| wFlags
);
393 /** void FAR PASCAL Draw3DLine(HDC hdc, WORD x, WORD y, WORD nLen,
396 * Draws a 3D line that can be used to make a 3D box.
399 * HDC hdc : Handle to the device context that will be
400 * used to display the 3D line.
402 * WORD x, y : Coordinates of the beginning of the line.
403 * These coordinates are in device units and
404 * represent the _outside_ most point. Horiz-
405 * ontal lines are drawn from left to right and
406 * vertical lines are drawn from top to bottom.
408 * WORD wShadowWidth : Width of the shadow in device coordinates.
410 * WORD wFlags : The following flags may be passed to
411 * describe the style of the 3D line:
413 * DRAW3D_IN : The shadow is drawn such that
414 * the box appears to be sunk in to the screen.
415 * This is default if 0 is passed.
417 * DRAW3D_OUT : The shadow is drawn such that
418 * the box appears to be sticking out of the
421 * DRAW3D_TOPLINE, _BOTTOMLINE, _LEFTLINE, and
422 * _RIGHTLINE : Specifies that a "top",
423 * "Bottom", "Left", or"Right" line is to be
426 * RETURN (void FAR PASCAL):
427 * The line will have been drawn into the DC.
433 void FAR PASCAL
Draw3DLine(HDC hdc
, WORD x
, WORD y
, WORD nLen
,
434 WORD wShadowWidth
, WORD wFlags
)
439 POINT Point
[ 4 ]; /* define a polgon with 4 points */
441 /* if width is zero, don't do nothin'! */
445 /* define shape of polygon--origin is always the same */
449 /* To do this we'll simply draw a polygon with four sides, using
450 * the appropriate brush. I dare you to ask me why this isn't a
453 if (wFlags
& DRAW3D_TOPLINE
)
455 /* across to right */
456 Point
[1].x
= x
+ nLen
- (wShadowWidth
== 1 ? 1 : 0);
460 Point
[2].x
= x
+ nLen
- wShadowWidth
;
461 Point
[2].y
= y
+ wShadowWidth
;
463 /* accross to left */
464 Point
[3].x
= x
+ wShadowWidth
;
465 Point
[3].y
= y
+ wShadowWidth
;
467 /* select 'dark' brush if 'in'--'light' for 'out' */
468 fDark
= (wFlags
& DRAW3D_IN
) ? TRUE
: FALSE
;
471 /* possibly the bottom? */
472 else if (wFlags
& DRAW3D_BOTTOMLINE
)
474 /* across to right */
475 Point
[1].x
= x
+ nLen
;
479 Point
[2].x
= x
+ nLen
- wShadowWidth
;
480 Point
[2].y
= y
- wShadowWidth
;
482 /* accross to left */
483 Point
[3].x
= x
+ wShadowWidth
;
484 Point
[3].y
= y
- wShadowWidth
;
486 /* select 'light' brush if 'in' */
487 fDark
= (wFlags
& DRAW3D_IN
) ? FALSE
: TRUE
;
490 /* ok, it's gotta be left? */
491 else if (wFlags
& DRAW3D_LEFTLINE
)
495 Point
[1].y
= y
+ nLen
- (wShadowWidth
== 1 ? 1 : 0);
498 Point
[2].x
= x
+ wShadowWidth
;
499 Point
[2].y
= y
+ nLen
- wShadowWidth
;
502 Point
[3].x
= x
+ wShadowWidth
;
503 Point
[3].y
= y
+ wShadowWidth
;
505 /* select 'dark' brush if 'in'--'light' for 'out' */
506 fDark
= (wFlags
& DRAW3D_IN
) ? TRUE
: FALSE
;
509 /* well maybe it's for the right side? */
510 else if (wFlags
& DRAW3D_RIGHTLINE
)
514 Point
[1].y
= y
+ nLen
;
517 Point
[2].x
= x
- wShadowWidth
;
518 Point
[2].y
= y
+ nLen
- wShadowWidth
;
521 Point
[3].x
= x
- wShadowWidth
;
522 Point
[3].y
= y
+ wShadowWidth
;
524 /* select 'light' brush if 'in' */
525 fDark
= (wFlags
& DRAW3D_IN
) ? FALSE
: TRUE
;
531 /* select NULL_PEN for no borders */
532 hOldPen
= (HPEN
) SelectObject(hdc
, GetStockObject(NULL_PEN
));
534 /* select the appropriate color for the fill */
536 hOldBrush
= (HBRUSH
) SelectObject(hdc
, GetStockObject(GRAY_BRUSH
));
538 hOldBrush
= (HBRUSH
) SelectObject(hdc
, GetStockObject(WHITE_BRUSH
));
540 /* finally, draw the dern thing */
541 Polygon(hdc
, (LPPOINT
)&Point
, 4);
543 /* restore what we killed */
544 SelectObject(hdc
, hOldBrush
);
545 SelectObject(hdc
, hOldPen
);
553 * Yet another 'Gas Gauge Custom Control.' This control gives you
554 * a 'progress bar' class (named zYzGauge) for use in your applications.
555 * You can set the range, position, font, color, orientation, and 3d
556 * effect of the gauge by sending messages to the control.
558 * Before you can use this control, you MUST first export the window
559 * procedure for the control (or define it with the _export keyword):
561 * EXPORTS gaugeWndProc
563 * You then need initialize the class before you use it:
565 * if (!gaugeInit(hInstance))
566 * die a horrible death
570 * The colors used by the control default to black and white if you
571 * are running on a mono-display. They default to blue and white
572 * if you are on a color display. You enable the 3D effect by setting
573 * the ZYZGS_3D style flag in the styles field of the control (like
574 * any other control).
576 * To select your own colors, you can send the ZYZG_SETFGCOLOR and
577 * ZYZG_SETBKCOLOR messages to set the foreground (percent done) and
578 * background (percent not done) colors. The lParam is the RGB()
579 * value--wParam is ignored.
581 * In all of the following ZYZG_??? messages, the arguments are
582 * WORDS. If you are setting parameters, the value is sent as
583 * the wParam (lParam is ignored). If you are getting parameters,
584 * the value is returned as a LONG and should be cast to a *signed*
587 * To set the depth of the 3D effect (if enabled), you can send the
588 * ZYZG_SETBEZELFACE and ZYZG_SETWIDTH3D messages. The bezel face
589 * is the flat top on the 3D border--its color will be that of the
590 * button-face. The 3D width is the width of the bezel itself; inside
591 * and outside. The light color is white, the dark color is gray.
592 * Both widths *can* be zero--both default to 2 which looks to me.
594 * The range of the control can be set by sending the ZYZG_SETRANGE
595 * message to the control. It can be any integer from 1 to 32767.
596 * What this specifies is the number of pieces that create a whole.
597 * The default is 100. You can get the current range setting by
598 * sending the ZYZG_GETRANGE message to the control.
600 * The position (number of pieces out of the whole have been used) is
601 * set with the ZYZG_SETPOSITION message. It can be any integer from
602 * 0 to the current range setting of the control--it will be clipped
603 * if the position is out of bounds. The default position is 0. You
604 * can get the current position at any time with the ZYZG_GETPOSITION
607 * You can also set the range using a delta from the current range.
608 * This is done by sending the ZYZG_SETDELTAPOS message with wParam
609 * set to a _signed_ integer value within the range of the control.
611 * The font used for the percentage text can be set using the standard
612 * WM_SETFONT message. You can get the current font at any time with
613 * the WM_GETFONT message.
615 * The orientation can be left to right, right to left, bottom to top,
616 * or top to bottom. Whatever suits your needs. You set this by
617 * sending the ZYZG_ORIENTATION message to the control with one of
618 * the following values (default is ZYZG_ORIENT_LEFTTORIGHT):
620 * ZYZG_ORIENT_LEFTTORIGHT (0)
621 * ZYZG_ORIENT_RIGHTTOLEFT (1)
622 * ZYZG_ORIENT_BOTTOMTOTOP (2)
623 * ZYZG_ORIENT_TOPTOBOTTOM (3)
626 * 3/12/91 cjp put in this comment
627 * 6/19/92 cjp touched it a bit
632 // (C) Copyright Microsoft Corp. 1992. All rights reserved.
634 // You have a royalty-free right to use, modify, reproduce and
635 // distribute the Sample Files (and/or any modified version) in
636 // any way you find useful, provided that you agree that
637 // Microsoft has no warranty obligations or liability for any
638 // Sample Application Files which are modified.
642 /* get the includes we need */
643 #if !defined(__GNUWIN32__) && !defined(__SALFORDC__)
649 // #include "zyz3d.h"
650 // #include "zyzgauge.h"
653 /* static global variables */
654 static wxChar gszzYzGaugeClass
[] = wxT("zYzGauge");
657 /* window word position definitions */
658 #define ZYZG_WW_PZYZGAUGE 0
659 /* #define ZYZG_WW_EXTRABYTES 2 */
660 #define ZYZG_WW_EXTRABYTES 4
663 /* control block structure typedef */
664 typedef struct tZYZGAUGE
670 WORD wWidthBezelFace
;
675 } ZYZGAUGE
, *PZYZGAUGE
, FAR
*LPZYZGAUGE
;
678 /* some default values for the control */
679 #define ZYZG_DEF_RANGE 100
680 #define ZYZG_DEF_POSITION 0
681 #define ZYZG_DEF_ORIENTATION ZYZG_ORIENT_LEFTTORIGHT
682 #define ZYZG_DEF_WIDTH3D 2
683 #define ZYZG_DEF_BEZELFACE 2
687 /* the default settings for drawing colors--display dependent */
688 static DWORD rgbDefTextColor
;
689 static DWORD rgbDefBkColor
;
690 static BOOL fSupport3D
;
692 #if !defined(APIENTRY) // NT defines APIENTRY, 3.x not
693 #define APIENTRY FAR PASCAL
699 #define _EXPORT _export
700 typedef signed short int SHORT
;
703 /* internal function prototypes */
704 static void PASCAL
gaugePaint(HWND
, HDC
);
705 /* LRESULT FAR PASCAL */
706 LRESULT APIENTRY _EXPORT
gaugeWndProc(HWND
, UINT
, WPARAM
, LPARAM
);
710 /** BOOL FAR PASCAL gaugeInit(HINSTANCE hInstance)
713 * Registers the window class for the zYzGauge control. Performs
714 * other initialization for the zYzGauge text control. This must
715 * be done before the zYzGauge control is used--or it will fail
716 * and your dialog box will not open!
719 * HINSTANCE hInstance : Instance handle to register class with.
722 * The return value is TRUE if the zYzGauge class was successfully
723 * registered. It is FALSE if the initialization fails.
729 //#pragma alloc_text(init, gaugeInit)
731 BOOL FAR PASCAL
gaugeInit(HINSTANCE hInstance
)
733 static BOOL fRegistered
= FALSE
;
737 /* assume already registered if not first instance */
741 /* fill in the class structure for the zyzgauge control */
742 wc
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
744 wc
.lpszMenuName
= NULL
;
745 wc
.lpszClassName
= gszzYzGaugeClass
;
746 wc
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+ 1);
747 wc
.hInstance
= hInstance
;
750 wc
.style
= CS_GLOBALCLASS
| CS_HREDRAW
| CS_VREDRAW
;
752 wc
.style
= CS_HREDRAW
| CS_VREDRAW
;
755 wc
.lpfnWndProc
= gaugeWndProc
;
757 wc
.cbWndExtra
= ZYZG_WW_EXTRABYTES
;
759 /* attempt to register it--return FALSE if fail */
760 if (!RegisterClass(&wc
))
763 /* Get a DC to determine whether device is mono or not, and set
764 * default foreground/background colors as appropriate.
766 hdc
= CreateIC(wxT("DISPLAY"), NULL
, NULL
, 0L) ;
769 /* check for mono-display */
770 if ((GetDeviceCaps(hdc
, BITSPIXEL
) == 1) &&
771 (GetDeviceCaps(hdc
, PLANES
) == 1))
773 /* using a mono DC--white foreground, black background */
774 rgbDefTextColor
= RGB(255, 255, 255);
775 rgbDefBkColor
= RGB(0, 0, 0);
778 /* good! we have color: blue foreground, white background */
781 rgbDefTextColor
= RGB(0, 0, 255);
782 rgbDefBkColor
= RGB(255, 255, 255);
785 /* need at _least_ 8 for two shades of gray (>=VGA) */
786 fSupport3D
= (GetDeviceCaps(hdc
, NUMCOLORS
) >= 8) ? TRUE
: FALSE
;
788 /* get rid of the DC (IC) */
792 /* uh-oh... can't get DC (IC)... fail */
795 /* unregister the class */
796 UnregisterClass(gszzYzGaugeClass
, hInstance
);
801 return (fRegistered
= TRUE
);
805 /** static void PASCAL gaugePaint(HWND hwnd, HDC hdc)
808 * This function is responsible for painting the zYzGauge control.
811 * HWND hwnd : The window handle for the gauge.
813 * HDC hdc : The DC for the gauge's window.
816 * The control will have been painted.
822 static void PASCAL
gaugePaint(HWND hwnd
, HDC hdc
)
831 WORD dx
, dy
, wGomerX
, wGomerY
;
832 /* Win32s has no GetTextExtent(); let's try GetTextExtentPoint() instead,
833 * which needs a SIZE* parameter */
834 #if defined(__WIN32__)
838 /* get pointer to the control's control block */
839 // pgauge = (PZYZGAUGE)GetWindowWord(hwnd, ZYZG_WW_PZYZGAUGE);
840 pgauge
= (PZYZGAUGE
)GetWindowLong(hwnd
, ZYZG_WW_PZYZGAUGE
);
842 /* set the colors into for the gauge into the control */
843 SetTextColor(hdc
, pgauge
->rgbTextColor
);
844 SetBkColor(hdc
, pgauge
->rgbBkColor
);
846 /* draw black rectangle for gauge */
847 GetClientRect(hwnd
, &rc1
);
849 /* draw a black border on the _outside_ */
850 FrameRect(hdc
, &rc1
, (HBRUSH
) GetStockObject(BLACK_BRUSH
));
852 /* we want to draw _just inside_ the black border */
853 InflateRect(&rc1
, -1, -1);
855 /* one line thick so far... */
856 // Offset = (WORD) 1;
858 /* for 3D stuff, we need to have at least two shades of gray */
859 if ((GetWindowLong(hwnd
, GWL_STYLE
) & ZYZGS_3D
) && fSupport3D
)
861 Draw3DRect(hdc
, &rc1
, pgauge
->wWidth3D
, DRAW3D_OUT
);
862 InflateRect(&rc1
, ~(pgauge
->wWidth3D
), ~(pgauge
->wWidth3D
));
864 Draw3DFaceFrame(hdc
, &rc1
, pgauge
->wWidthBezelFace
);
865 InflateRect(&rc1
, ~(pgauge
->wWidthBezelFace
), ~(pgauge
->wWidthBezelFace
));
867 Draw3DRect(hdc
, &rc1
, pgauge
->wWidth3D
, DRAW3D_IN
);
868 InflateRect(&rc1
, ~(pgauge
->wWidth3D
), ~(pgauge
->wWidth3D
));
870 /* draw a black border on the _inside_ */
871 FrameRect(hdc
, &rc1
, (HBRUSH
) GetStockObject(BLACK_BRUSH
));
873 /* we want to draw _just inside_ the black border */
874 InflateRect(&rc1
, -1, -1);
876 /* add all the other pixels into the border width */
877 Offset
+= (2 * pgauge
->wWidth3D
) + pgauge
->wWidthBezelFace
+ 1;
880 /* dup--one rc for 'how much filled', one rc for 'how much empty' */
883 /* get the range--make sure it's a valid range */
884 if ((iRange
= pgauge
->wRange
) <= 0)
887 /* get the position--greater than 100% would be bad */
888 if ((iPos
= pgauge
->wPosition
) > iRange
)
891 /* compute the actual size of the gauge */
892 dx
= rc1
.right
- rc1
.left
;
893 dy
= rc1
.bottom
- rc1
.top
;
894 wGomerX
= (WORD
)((DWORD
)iPos
* dx
/ iRange
);
895 wGomerY
= (WORD
)((DWORD
)iPos
* dy
/ iRange
);
897 /* get the orientation and munge rects accordingly */
898 switch (pgauge
->wOrientation
)
900 case ZYZG_ORIENT_RIGHTTOLEFT
:
901 rc1
.left
= rc2
.right
= rc1
.right
- wGomerX
;
904 case ZYZG_ORIENT_BOTTOMTOTOP
:
905 rc1
.top
= rc2
.bottom
= rc1
.bottom
- wGomerY
;
908 case ZYZG_ORIENT_TOPTOBOTTOM
:
909 rc1
.bottom
= rc2
.top
+= wGomerY
;
913 rc1
.right
= rc2
.left
+= wGomerX
;
917 /* select the correct font */
918 hFont
= (HFONT
) SelectObject(hdc
, pgauge
->hFont
);
920 /* build up a string to blit out--ie the meaning of life: "42%" */
921 wsprintf(ach
, wxT("%3d%%"), (WORD
)((DWORD
)iPos
* 100 / iRange
));
922 /* Win32s has no GetTextExtent(); let's try GetTextExtentPoint() instead */
923 #if defined(__WIN32__)
924 GetTextExtentPoint(hdc
, ach
, wGomerX
= lstrlen(ach
), &size
);
927 dwExtent
= GetTextExtent(hdc
, ach
, wGomerX
= lstrlen(ach
));
931 /* Draw the finished (ie the percent done) side of box. If
932 * ZYZG_WW_POSITION is 42, (in range of 0 to 100) this ExtTextOut
933 * draws the meaning of life (42%) bar.
935 ExtTextOut(hdc
, (dx
- LOWORD(dwExtent
)) / 2 + Offset
,
936 (dy
- HIWORD(dwExtent
)) / 2 + Offset
,
937 ETO_OPAQUE
| ETO_CLIPPED
, &rc2
, ach
, wGomerX
, NULL
);
939 /* Reverse fore and back colors for drawing the undone (ie the non-
940 * finished) side of the box.
942 SetBkColor(hdc
, pgauge
->rgbTextColor
);
943 SetTextColor(hdc
, pgauge
->rgbBkColor
);
945 ExtTextOut(hdc
, (dx
- LOWORD(dwExtent
)) / 2 + Offset
,
946 (dy
- HIWORD(dwExtent
)) / 2 + Offset
,
947 ETO_OPAQUE
| ETO_CLIPPED
, &rc1
, ach
, wGomerX
, NULL
);
949 /* unselect the font */
950 SelectObject(hdc
, hFont
);
954 /** LRESULT FAR PASCAL gaugeWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
957 * This is the control's window procedure. Its purpose is to handle
958 * special messages for this custom control.
960 * The special control messages for the gauge control are:
962 * ZYZG_SETRANGE : Sets the range of the gauge. In other
963 * words, the number of parts that make a
966 * ZYZG_GETRANGE : Returns the current range of the gauge.
968 * ZYZG_SETORIENTATION : Sets the orientation of the gauge. This
969 * can be one of the ZYZG_ORIENT_?? msgs.
971 * ZYZG_GETORIENTATION : Gets the current orientation of the
974 * ZYZG_SETPOSITION : Sets the current position of the gauge.
975 * In other words, how many pieces of the
976 * whole have been used.
978 * ZYZG_GETPOSITION : Gets the current position of the gauge.
980 * ZYZG_SETDELTAPOS : Sets the position of the gauge +/- the
983 * ZYZG_SETFGCOLOR : Sets the foreground (percent done) color.
985 * ZYZG_GETFGCOLOR : Gets the foreground (percent done) color.
987 * ZYZG_SETBKCOLOR : Sets the background (percent not done)
990 * ZYZG_GETBKCOLOR : Gets the background (percent not done)
993 * WM_SETFONT : Sets the font to use for the percentage
996 * WM_GETFONT : Gets the current font in use by the
1003 /* LRESULT FAR PASCAL */
1005 LRESULT APIENTRY _EXPORT
gaugeWndProc(HWND hwnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
1012 // pgauge = (PZYZGAUGE)GetWindowWord(hwnd, ZYZG_WW_PZYZGAUGE);
1013 pgauge
= (PZYZGAUGE
)GetWindowLong(hwnd
, ZYZG_WW_PZYZGAUGE
);
1015 /* break to get DefWindowProc() */
1019 /* need to allocate a control block */
1020 // pgauge = (PZYZGAUGE)LocalAlloc(LPTR, sizeof(ZYZGAUGE));
1021 pgauge
= (PZYZGAUGE
)malloc(sizeof(ZYZGAUGE
));
1025 /* hang on to this control block */
1026 // SetWindowWord(hwnd, ZYZG_WW_PZYZGAUGE, (WORD)pgauge);
1027 SetWindowLong(hwnd
, ZYZG_WW_PZYZGAUGE
, (LONG
)pgauge
);
1029 /* fill control block with defaults */
1030 pgauge
->wRange
= ZYZG_DEF_RANGE
;
1031 pgauge
->wPosition
= ZYZG_DEF_POSITION
;
1032 pgauge
->wOrientation
= ZYZG_DEF_ORIENTATION
;
1033 pgauge
->wWidth3D
= ZYZG_DEF_WIDTH3D
;
1034 pgauge
->wWidthBezelFace
= ZYZG_DEF_BEZELFACE
;
1035 pgauge
->rgbTextColor
= rgbDefTextColor
;
1036 pgauge
->rgbBkColor
= rgbDefBkColor
;
1038 /* use system font */
1039 SendMessage(hwnd
, WM_SETFONT
, (WPARAM
)NULL
, 0L);
1041 /* go to DefWindowProc() to finish the job */
1045 /* get rid of the control's memory */
1047 // LocalFree((HANDLE)pgauge);
1051 case ZYZG_GETPOSITION
:
1052 return (pgauge
->wPosition
);
1055 return (pgauge
->wRange
);
1057 case ZYZG_GETORIENTATION
:
1058 return (pgauge
->wOrientation
);
1060 case ZYZG_GETWIDTH3D
:
1061 return (pgauge
->wWidth3D
);
1063 case ZYZG_GETBEZELFACE
:
1064 return (pgauge
->wWidthBezelFace
);
1066 case ZYZG_GETBKCOLOR
:
1067 return (pgauge
->rgbTextColor
);
1069 case ZYZG_GETFGCOLOR
:
1070 return (pgauge
->rgbBkColor
);
1072 case ZYZG_SETBKCOLOR
:
1073 pgauge
->rgbBkColor
= lParam
;
1076 case ZYZG_SETFGCOLOR
:
1077 pgauge
->rgbTextColor
= lParam
;
1081 case ZYZG_SETPOSITION
:
1082 pgauge
->wPosition
= wParam
;
1085 GetClientRect(hwnd
, &rc
);
1086 if ((GetWindowLong(hwnd
, GWL_STYLE
) & ZYZGS_3D
) && fSupport3D
)
1088 wParam
= (2 * pgauge
->wWidth3D
) +
1089 pgauge
->wWidthBezelFace
+ 2;
1095 InflateRect(&rc
, ~(wParam
), ~(wParam
));
1096 InvalidateRect(hwnd
, &rc
, FALSE
);
1101 pgauge
->wRange
= wParam
;
1102 goto zyzgForceRepaint
;
1104 case ZYZG_SETORIENTATION
:
1105 pgauge
->wOrientation
= wParam
;
1106 goto zyzgForceRepaint
;
1108 case ZYZG_SETWIDTH3D
:
1109 pgauge
->wWidth3D
= wParam
;
1112 InvalidateRect(hwnd
, NULL
, FALSE
);
1116 case ZYZG_SETBEZELFACE
:
1117 pgauge
->wWidthBezelFace
= wParam
;
1118 goto zyzgForceRepaint3D
;
1120 case ZYZG_SETDELTAPOS
:
1121 /* Watcom doesn't like the following line so removing typecasts */
1122 /* (int)pgauge->wPosition += (int)wParam; */
1123 pgauge
->wPosition
+= wParam
;
1124 goto zyzgForceRepaint
;
1127 BeginPaint(hwnd
, &ps
);
1128 gaugePaint(hwnd
, ps
.hdc
);
1129 EndPaint(hwnd
, &ps
);
1133 hFont
= pgauge
->hFont
;
1135 /* if system font, then return NULL handle */
1136 return (long)((hFont
== GetStockObject(SYSTEM_FONT
)) ? NULL
: hFont
);
1139 /* if NULL hFont, use system font */
1140 hFont
= (HFONT
)wParam
;
1142 hFont
= (HFONT
) GetStockObject(SYSTEM_FONT
);
1144 pgauge
->hFont
= hFont
;
1146 /* redraw if indicated in message */
1149 InvalidateRect(hwnd
, NULL
, TRUE
);
1155 /* let the dialog mangler take care of this message */
1156 return (DefWindowProc(hwnd
, uMsg
, wParam
, lParam
));
1157 } /* gaugeWndProc() */
1160 /** EOF: zyzgauge.c **/
1162 #endif // wxUSE_GAUGE