]>
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 #if !USE_SHARED_LIBRARY
64 IMPLEMENT_DYNAMIC_CLASS(wxGaugeMSW
, wxControl
)
67 bool wxGaugeMSW::Create(wxWindow
*parent
, wxWindowID id
,
72 const wxValidator
& validator
,
75 static bool wxGaugeMSWInitialised
= FALSE
;
77 if ( !wxGaugeMSWInitialised
)
79 if (!gaugeInit((HINSTANCE
) wxGetInstance()))
80 wxFatalError("Cannot initalize Gauge library");
81 wxGaugeMSWInitialised
= TRUE
;
85 SetValidator(validator
);
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
;
110 CreateWindowEx(MakeExtendedStyle(m_windowStyle
), "zYzGauge", NULL
, msFlags
,
111 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
112 wxGetInstance(), NULL
);
114 m_hWnd
= (WXHWND
)wx_button
;
116 // Subclass again for purposes of dialog editing mode
117 SubclassWin((WXHWND
)wx_button
);
121 if (m_windowStyle
& wxGA_HORIZONTAL
)
122 wOrient
= ZYZG_ORIENT_LEFTTORIGHT
;
124 wOrient
= ZYZG_ORIENT_BOTTOMTOTOP
;
126 SendMessage(wx_button
, ZYZG_SETORIENTATION
, wOrient
, 0);
127 SendMessage(wx_button
, ZYZG_SETRANGE
, range
, 0);
129 SendMessage((HWND
) GetHWND(), ZYZG_SETFGCOLOR
, 0, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
130 SendMessage((HWND
) GetHWND(), ZYZG_SETBKCOLOR
, 0, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
132 SetFont(parent
->GetFont());
138 SetSize(x
, y
, width
, height
);
140 ShowWindow((HWND
) GetHWND(), SW_SHOW
);
145 void wxGaugeMSW::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
147 int currentX
, currentY
;
148 GetPosition(¤tX
, ¤tY
);
154 if (x
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
156 if (y
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
159 AdjustForParentClientOrigin(x1
, y1
, sizeFlags
);
161 // If we're prepared to use the existing size, then...
162 if (width
== -1 && height
== -1 && ((sizeFlags
& wxSIZE_AUTO
) != wxSIZE_AUTO
))
167 // Deal with default size (using -1 values)
169 w1
= DEFAULT_ITEM_WIDTH
;
172 h1
= DEFAULT_ITEM_HEIGHT
;
174 MoveWindow((HWND
) GetHWND(), x1
, y1
, w1
, h1
, TRUE
);
177 void wxGaugeMSW::SetShadowWidth(int w
)
179 SendMessage((HWND
) GetHWND(), ZYZG_SETWIDTH3D
, w
, 0);
182 void wxGaugeMSW::SetBezelFace(int w
)
184 SendMessage((HWND
) GetHWND(), ZYZG_SETBEZELFACE
, w
, 0);
187 void wxGaugeMSW::SetRange(int r
)
191 SendMessage((HWND
) GetHWND(), ZYZG_SETRANGE
, r
, 0);
194 void wxGaugeMSW::SetValue(int pos
)
198 SendMessage((HWND
) GetHWND(), ZYZG_SETPOSITION
, pos
, 0);
201 int wxGaugeMSW::GetShadowWidth(void) const
203 return (int) SendMessage((HWND
) GetHWND(), ZYZG_GETWIDTH3D
, 0, 0);
206 int wxGaugeMSW::GetBezelFace(void) const
208 return (int) SendMessage((HWND
) GetHWND(), ZYZG_GETBEZELFACE
, 0, 0);
211 int wxGaugeMSW::GetRange(void) const
213 return (int) SendMessage((HWND
) GetHWND(), ZYZG_GETRANGE
, 0, 0);
216 int wxGaugeMSW::GetValue(void) const
218 return (int) SendMessage((HWND
) GetHWND(), ZYZG_GETPOSITION
, 0, 0);
221 void wxGaugeMSW::SetForegroundColour(const wxColour
& col
)
223 m_foregroundColour
= col
;
224 SendMessage((HWND
) GetHWND(), ZYZG_SETFGCOLOR
, 0, RGB(col
.Red(), col
.Green(), col
.Blue()));
227 void wxGaugeMSW::SetBackgroundColour(const wxColour
& col
)
229 m_backgroundColour
= col
;
230 SendMessage((HWND
) GetHWND(), ZYZG_SETBKCOLOR
, 0, RGB(col
.Red(), col
.Green(), col
.Blue()));
237 * This module contains functions for creating nifty 3D borders
238 * around controls like zYzGauge.
241 * 3/14/91 cjp put in this comment
242 * 6/19/92 cjp touched it a bit
247 // (C) Copyright Microsoft Corp. 1992. All rights reserved.
249 // You have a royalty-free right to use, modify, reproduce and
250 // distribute the Sample Files (and/or any modified version) in
251 // any way you find useful, provided that you agree that
252 // Microsoft has no warranty obligations or liability for any
253 // Sample Application Files which are modified.
257 /* get the includes we need */
260 /* misc. control flag defines */
261 #define DRAW3D_IN 0x0001
262 #define DRAW3D_OUT 0x0002
264 #define DRAW3D_TOPLINE 0x0004
265 #define DRAW3D_BOTTOMLINE 0x0008
266 #define DRAW3D_LEFTLINE 0x0010
267 #define DRAW3D_RIGHTLINE 0x0020
270 /* public function prototypes */
271 void FAR PASCAL
Draw3DFaceFrame(HDC
, LPRECT
, WORD
);
272 void FAR PASCAL
Draw3DRect(HDC
, LPRECT
, WORD
, WORD
);
273 void FAR PASCAL
Draw3DLine(HDC
, WORD
, WORD
, WORD
, WORD
, WORD
);
276 /** void FAR PASCAL Draw3DFaceFrame(HDC hdc, LPRECT rc, WORD wWidth)
279 * This function draws a flat frame with the current button-face
283 * HDC hdc : The DC to draw into.
285 * LPRECT rc : The containing rect for the new frame.
287 * WORD wWidth : The width of the frame to draw.
289 * RETURN (void FAR PASCAL):
290 * The frame will have been drawn into the DC.
296 void FAR PASCAL
Draw3DFaceFrame(HDC hdc
, LPRECT rc
, WORD wWidth
)
301 /* don't go through a bunch of work if we don't have to */
305 /* set up color to be button-face color--so it may not be gray */
306 rgbOld
= SetBkColor(hdc
, GetSysColor(COLOR_BTNFACE
));
308 /* perform CopyRect w/o bloody windows style overhead */
314 rc1
.bottom
= rc
->top
+ wWidth
;
315 rc1
.right
= rc
->right
;
318 ExtTextOut(hdc
, rc1
.left
, rc1
.top
, ETO_OPAQUE
, &rc1
, NULL
, 0, NULL
);
321 rc1
.left
= rc
->right
- wWidth
;
322 rc1
.bottom
= rc
->bottom
;
324 /* blast this part now */
325 ExtTextOut(hdc
, rc1
.left
, rc1
.top
, ETO_OPAQUE
, &rc1
, NULL
, 0, NULL
);
329 rc1
.right
= rc
->left
+ wWidth
;
331 /* and another part */
332 ExtTextOut(hdc
, rc1
.left
, rc1
.top
, ETO_OPAQUE
, &rc1
, NULL
, 0, NULL
);
335 rc1
.right
= rc
->right
;
336 rc1
.top
= rc
->bottom
- wWidth
;
339 ExtTextOut(hdc
, rc1
.left
, rc1
.top
, ETO_OPAQUE
, &rc1
, NULL
, 0, NULL
);
341 /* restore the old bk color */
342 SetBkColor(hdc
, rgbOld
);
343 } /* Draw3DFaceFrame() */
346 /** void FAR PASCAL Draw3DRect(HDC, LPRECT, WORD, WORD)
349 * Draws a 3D rectangle that is shaded. wFlags can be used to
350 * control how the rectangle looks.
353 * HDC hdc : Handle to the device context that will be
354 * used to display the rectangle.
356 * RECT rect : A rectangle describing the dimensions of
357 * the rectangle in device coordinates.
359 * WORD wShadowWidth : Width of the shadow in device coordinates.
361 * WORD wFlags : The following flags may be passed to describe
362 * the style of the rectangle:
364 * DRAW3D_IN : The shadow is drawn such that
365 * the box appears to be sunk in to the screen.
366 * This is default if 0 is passed.
368 * DRAW3D_OUT : The shadow is drawn such that
369 * the box appears to be sticking out of the
372 * RETURN (void FAR PASCAL):
373 * The 3D looking rectangle will have been drawn into the DC.
379 void FAR PASCAL
Draw3DRect(HDC hdc
, LPRECT lpRect
,
380 WORD wShadowWidth
, WORD wFlags
)
382 /* sanity check--don't work if you don't have to! */
383 if (!wShadowWidth
|| !RectVisible(hdc
, lpRect
))
386 /* draw the top line */
387 Draw3DLine(hdc
, lpRect
->left
, lpRect
->top
,
388 lpRect
->right
- lpRect
->left
,
389 wShadowWidth
, DRAW3D_TOPLINE
| wFlags
);
392 Draw3DLine(hdc
, lpRect
->right
, lpRect
->top
,
393 lpRect
->bottom
- lpRect
->top
,
394 wShadowWidth
, DRAW3D_RIGHTLINE
| wFlags
);
397 Draw3DLine(hdc
, lpRect
->left
, lpRect
->bottom
,
398 lpRect
->right
- lpRect
->left
,
399 wShadowWidth
, DRAW3D_BOTTOMLINE
| wFlags
);
402 Draw3DLine(hdc
, lpRect
->left
, lpRect
->top
,
403 lpRect
->bottom
- lpRect
->top
,
404 wShadowWidth
, DRAW3D_LEFTLINE
| wFlags
);
408 /** void FAR PASCAL Draw3DLine(HDC hdc, WORD x, WORD y, WORD nLen,
411 * Draws a 3D line that can be used to make a 3D box.
414 * HDC hdc : Handle to the device context that will be
415 * used to display the 3D line.
417 * WORD x, y : Coordinates of the beginning of the line.
418 * These coordinates are in device units and
419 * represent the _outside_ most point. Horiz-
420 * ontal lines are drawn from left to right and
421 * vertical lines are drawn from top to bottom.
423 * WORD wShadowWidth : Width of the shadow in device coordinates.
425 * WORD wFlags : The following flags may be passed to
426 * describe the style of the 3D line:
428 * DRAW3D_IN : The shadow is drawn such that
429 * the box appears to be sunk in to the screen.
430 * This is default if 0 is passed.
432 * DRAW3D_OUT : The shadow is drawn such that
433 * the box appears to be sticking out of the
436 * DRAW3D_TOPLINE, _BOTTOMLINE, _LEFTLINE, and
437 * _RIGHTLINE : Specifies that a "top",
438 * "Bottom", "Left", or"Right" line is to be
441 * RETURN (void FAR PASCAL):
442 * The line will have been drawn into the DC.
448 void FAR PASCAL
Draw3DLine(HDC hdc
, WORD x
, WORD y
, WORD nLen
,
449 WORD wShadowWidth
, WORD wFlags
)
454 POINT Point
[ 4 ]; /* define a polgon with 4 points */
456 /* if width is zero, don't do nothin'! */
460 /* define shape of polygon--origin is always the same */
464 /* To do this we'll simply draw a polygon with four sides, using
465 * the appropriate brush. I dare you to ask me why this isn't a
468 if (wFlags
& DRAW3D_TOPLINE
)
470 /* across to right */
471 Point
[1].x
= x
+ nLen
- (wShadowWidth
== 1 ? 1 : 0);
475 Point
[2].x
= x
+ nLen
- wShadowWidth
;
476 Point
[2].y
= y
+ wShadowWidth
;
478 /* accross to left */
479 Point
[3].x
= x
+ wShadowWidth
;
480 Point
[3].y
= y
+ wShadowWidth
;
482 /* select 'dark' brush if 'in'--'light' for 'out' */
483 fDark
= (wFlags
& DRAW3D_IN
) ? TRUE
: FALSE
;
486 /* possibly the bottom? */
487 else if (wFlags
& DRAW3D_BOTTOMLINE
)
489 /* across to right */
490 Point
[1].x
= x
+ nLen
;
494 Point
[2].x
= x
+ nLen
- wShadowWidth
;
495 Point
[2].y
= y
- wShadowWidth
;
497 /* accross to left */
498 Point
[3].x
= x
+ wShadowWidth
;
499 Point
[3].y
= y
- wShadowWidth
;
501 /* select 'light' brush if 'in' */
502 fDark
= (wFlags
& DRAW3D_IN
) ? FALSE
: TRUE
;
505 /* ok, it's gotta be left? */
506 else if (wFlags
& DRAW3D_LEFTLINE
)
510 Point
[1].y
= y
+ nLen
- (wShadowWidth
== 1 ? 1 : 0);
513 Point
[2].x
= x
+ wShadowWidth
;
514 Point
[2].y
= y
+ nLen
- wShadowWidth
;
517 Point
[3].x
= x
+ wShadowWidth
;
518 Point
[3].y
= y
+ wShadowWidth
;
520 /* select 'dark' brush if 'in'--'light' for 'out' */
521 fDark
= (wFlags
& DRAW3D_IN
) ? TRUE
: FALSE
;
524 /* well maybe it's for the right side? */
525 else if (wFlags
& DRAW3D_RIGHTLINE
)
529 Point
[1].y
= y
+ nLen
;
532 Point
[2].x
= x
- wShadowWidth
;
533 Point
[2].y
= y
+ nLen
- wShadowWidth
;
536 Point
[3].x
= x
- wShadowWidth
;
537 Point
[3].y
= y
+ wShadowWidth
;
539 /* select 'light' brush if 'in' */
540 fDark
= (wFlags
& DRAW3D_IN
) ? FALSE
: TRUE
;
546 /* select NULL_PEN for no borders */
547 hOldPen
= (HPEN
) SelectObject(hdc
, GetStockObject(NULL_PEN
));
549 /* select the appropriate color for the fill */
551 hOldBrush
= (HBRUSH
) SelectObject(hdc
, GetStockObject(GRAY_BRUSH
));
553 hOldBrush
= (HBRUSH
) SelectObject(hdc
, GetStockObject(WHITE_BRUSH
));
555 /* finally, draw the dern thing */
556 Polygon(hdc
, (LPPOINT
)&Point
, 4);
558 /* restore what we killed */
559 SelectObject(hdc
, hOldBrush
);
560 SelectObject(hdc
, hOldPen
);
568 * Yet another 'Gas Gauge Custom Control.' This control gives you
569 * a 'progress bar' class (named zYzGauge) for use in your applications.
570 * You can set the range, position, font, color, orientation, and 3d
571 * effect of the gauge by sending messages to the control.
573 * Before you can use this control, you MUST first export the window
574 * procedure for the control (or define it with the _export keyword):
576 * EXPORTS gaugeWndProc
578 * You then need initialize the class before you use it:
580 * if (!gaugeInit(hInstance))
581 * die a horrible death
585 * The colors used by the control default to black and white if you
586 * are running on a mono-display. They default to blue and white
587 * if you are on a color display. You enable the 3D effect by setting
588 * the ZYZGS_3D style flag in the styles field of the control (like
589 * any other control).
591 * To select your own colors, you can send the ZYZG_SETFGCOLOR and
592 * ZYZG_SETBKCOLOR messages to set the foreground (percent done) and
593 * background (percent not done) colors. The lParam is the RGB()
594 * value--wParam is ignored.
596 * In all of the following ZYZG_??? messages, the arguments are
597 * WORDS. If you are setting parameters, the value is sent as
598 * the wParam (lParam is ignored). If you are getting parameters,
599 * the value is returned as a LONG and should be cast to a *signed*
602 * To set the depth of the 3D effect (if enabled), you can send the
603 * ZYZG_SETBEZELFACE and ZYZG_SETWIDTH3D messages. The bezel face
604 * is the flat top on the 3D border--its color will be that of the
605 * button-face. The 3D width is the width of the bezel itself; inside
606 * and outside. The light color is white, the dark color is gray.
607 * Both widths *can* be zero--both default to 2 which looks to me.
609 * The range of the control can be set by sending the ZYZG_SETRANGE
610 * message to the control. It can be any integer from 1 to 32767.
611 * What this specifies is the number of pieces that create a whole.
612 * The default is 100. You can get the current range setting by
613 * sending the ZYZG_GETRANGE message to the control.
615 * The position (number of pieces out of the whole have been used) is
616 * set with the ZYZG_SETPOSITION message. It can be any integer from
617 * 0 to the current range setting of the control--it will be clipped
618 * if the position is out of bounds. The default position is 0. You
619 * can get the current position at any time with the ZYZG_GETPOSITION
622 * You can also set the range using a delta from the current range.
623 * This is done by sending the ZYZG_SETDELTAPOS message with wParam
624 * set to a _signed_ integer value within the range of the control.
626 * The font used for the percentage text can be set using the standard
627 * WM_SETFONT message. You can get the current font at any time with
628 * the WM_GETFONT message.
630 * The orientation can be left to right, right to left, bottom to top,
631 * or top to bottom. Whatever suits your needs. You set this by
632 * sending the ZYZG_ORIENTATION message to the control with one of
633 * the following values (default is ZYZG_ORIENT_LEFTTORIGHT):
635 * ZYZG_ORIENT_LEFTTORIGHT (0)
636 * ZYZG_ORIENT_RIGHTTOLEFT (1)
637 * ZYZG_ORIENT_BOTTOMTOTOP (2)
638 * ZYZG_ORIENT_TOPTOBOTTOM (3)
641 * 3/12/91 cjp put in this comment
642 * 6/19/92 cjp touched it a bit
647 // (C) Copyright Microsoft Corp. 1992. All rights reserved.
649 // You have a royalty-free right to use, modify, reproduce and
650 // distribute the Sample Files (and/or any modified version) in
651 // any way you find useful, provided that you agree that
652 // Microsoft has no warranty obligations or liability for any
653 // Sample Application Files which are modified.
657 /* get the includes we need */
664 // #include "zyz3d.h"
665 // #include "zyzgauge.h"
668 /* static global variables */
669 static char gszzYzGaugeClass
[] = "zYzGauge";
672 /* window word position definitions */
673 #define ZYZG_WW_PZYZGAUGE 0
674 /* #define ZYZG_WW_EXTRABYTES 2 */
675 #define ZYZG_WW_EXTRABYTES 4
678 /* control block structure typedef */
679 typedef struct tZYZGAUGE
685 WORD wWidthBezelFace
;
690 } ZYZGAUGE
, *PZYZGAUGE
, FAR
*LPZYZGAUGE
;
693 /* some default values for the control */
694 #define ZYZG_DEF_RANGE 100
695 #define ZYZG_DEF_POSITION 0
696 #define ZYZG_DEF_ORIENTATION ZYZG_ORIENT_LEFTTORIGHT
697 #define ZYZG_DEF_WIDTH3D 2
698 #define ZYZG_DEF_BEZELFACE 2
702 /* the default settings for drawing colors--display dependent */
703 static DWORD rgbDefTextColor
;
704 static DWORD rgbDefBkColor
;
705 static BOOL fSupport3D
;
707 #if !defined(APIENTRY) // NT defines APIENTRY, 3.x not
708 #define APIENTRY FAR PASCAL
714 #define _EXPORT _export
715 typedef signed short int SHORT
;
718 /* internal function prototypes */
719 static void PASCAL
gaugePaint(HWND
, HDC
);
720 /* LRESULT FAR PASCAL */
721 LRESULT APIENTRY _EXPORT
gaugeWndProc(HWND
, UINT
, WPARAM
, LPARAM
);
725 /** BOOL FAR PASCAL gaugeInit(HINSTANCE hInstance)
728 * Registers the window class for the zYzGauge control. Performs
729 * other initialization for the zYzGauge text control. This must
730 * be done before the zYzGauge control is used--or it will fail
731 * and your dialog box will not open!
734 * HINSTANCE hInstance : Instance handle to register class with.
737 * The return value is TRUE if the zYzGauge class was successfully
738 * registered. It is FALSE if the initialization fails.
744 //#pragma alloc_text(init, gaugeInit)
746 BOOL FAR PASCAL
gaugeInit(HINSTANCE hInstance
)
748 static BOOL fRegistered
= FALSE
;
752 /* assume already registered if not first instance */
756 /* fill in the class structure for the zyzgauge control */
757 wc
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
759 wc
.lpszMenuName
= NULL
;
760 wc
.lpszClassName
= gszzYzGaugeClass
;
761 wc
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+ 1);
762 wc
.hInstance
= hInstance
;
765 wc
.style
= CS_GLOBALCLASS
| CS_HREDRAW
| CS_VREDRAW
;
767 wc
.style
= CS_HREDRAW
| CS_VREDRAW
;
770 wc
.lpfnWndProc
= gaugeWndProc
;
772 wc
.cbWndExtra
= ZYZG_WW_EXTRABYTES
;
774 /* attempt to register it--return FALSE if fail */
775 if (!RegisterClass(&wc
))
778 /* Get a DC to determine whether device is mono or not, and set
779 * default foreground/background colors as appropriate.
781 if ((hdc
= CreateIC("DISPLAY", NULL
, NULL
, 0L)))
783 /* check for mono-display */
784 if ((GetDeviceCaps(hdc
, BITSPIXEL
) == 1) &&
785 (GetDeviceCaps(hdc
, PLANES
) == 1))
787 /* using a mono DC--white foreground, black background */
788 rgbDefTextColor
= RGB(255, 255, 255);
789 rgbDefBkColor
= RGB(0, 0, 0);
792 /* good! we have color: blue foreground, white background */
795 rgbDefTextColor
= RGB(0, 0, 255);
796 rgbDefBkColor
= RGB(255, 255, 255);
799 /* need at _least_ 8 for two shades of gray (>=VGA) */
800 fSupport3D
= (GetDeviceCaps(hdc
, NUMCOLORS
) >= 8) ? TRUE
: FALSE
;
802 /* get rid of the DC (IC) */
806 /* uh-oh... can't get DC (IC)... fail */
809 /* unregister the class */
810 UnregisterClass(gszzYzGaugeClass
, hInstance
);
815 return (fRegistered
= TRUE
);
819 /** static void PASCAL gaugePaint(HWND hwnd, HDC hdc)
822 * This function is responsible for painting the zYzGauge control.
825 * HWND hwnd : The window handle for the gauge.
827 * HDC hdc : The DC for the gauge's window.
830 * The control will have been painted.
836 static void PASCAL
gaugePaint(HWND hwnd
, HDC hdc
)
845 WORD dx
, dy
, wGomerX
, wGomerY
;
846 /* Win32s has no GetTextExtent(); let's try GetTextExtentPoint() instead,
847 * which needs a SIZE* parameter */
848 #if defined(__WIN32__)
852 /* get pointer to the control's control block */
853 // pgauge = (PZYZGAUGE)GetWindowWord(hwnd, ZYZG_WW_PZYZGAUGE);
854 pgauge
= (PZYZGAUGE
)GetWindowLong(hwnd
, ZYZG_WW_PZYZGAUGE
);
856 /* set the colors into for the gauge into the control */
857 SetTextColor(hdc
, pgauge
->rgbTextColor
);
858 SetBkColor(hdc
, pgauge
->rgbBkColor
);
860 /* draw black rectangle for gauge */
861 GetClientRect(hwnd
, &rc1
);
863 /* draw a black border on the _outside_ */
864 FrameRect(hdc
, &rc1
, (HBRUSH
) GetStockObject(BLACK_BRUSH
));
866 /* we want to draw _just inside_ the black border */
867 InflateRect(&rc1
, -1, -1);
869 /* one line thick so far... */
870 // Offset = (WORD) 1;
872 /* for 3D stuff, we need to have at least two shades of gray */
873 if ((GetWindowLong(hwnd
, GWL_STYLE
) & ZYZGS_3D
) && fSupport3D
)
875 Draw3DRect(hdc
, &rc1
, pgauge
->wWidth3D
, DRAW3D_OUT
);
876 InflateRect(&rc1
, ~(pgauge
->wWidth3D
), ~(pgauge
->wWidth3D
));
878 Draw3DFaceFrame(hdc
, &rc1
, pgauge
->wWidthBezelFace
);
879 InflateRect(&rc1
, ~(pgauge
->wWidthBezelFace
), ~(pgauge
->wWidthBezelFace
));
881 Draw3DRect(hdc
, &rc1
, pgauge
->wWidth3D
, DRAW3D_IN
);
882 InflateRect(&rc1
, ~(pgauge
->wWidth3D
), ~(pgauge
->wWidth3D
));
884 /* draw a black border on the _inside_ */
885 FrameRect(hdc
, &rc1
, (HBRUSH
) GetStockObject(BLACK_BRUSH
));
887 /* we want to draw _just inside_ the black border */
888 InflateRect(&rc1
, -1, -1);
890 /* add all the other pixels into the border width */
891 Offset
+= (2 * pgauge
->wWidth3D
) + pgauge
->wWidthBezelFace
+ 1;
894 /* dup--one rc for 'how much filled', one rc for 'how much empty' */
897 /* get the range--make sure it's a valid range */
898 if ((iRange
= pgauge
->wRange
) <= 0)
901 /* get the position--greater than 100% would be bad */
902 if ((iPos
= pgauge
->wPosition
) > iRange
)
905 /* compute the actual size of the gauge */
906 dx
= rc1
.right
- rc1
.left
;
907 dy
= rc1
.bottom
- rc1
.top
;
908 wGomerX
= (WORD
)((DWORD
)iPos
* dx
/ iRange
);
909 wGomerY
= (WORD
)((DWORD
)iPos
* dy
/ iRange
);
911 /* get the orientation and munge rects accordingly */
912 switch (pgauge
->wOrientation
)
914 case ZYZG_ORIENT_RIGHTTOLEFT
:
915 rc1
.left
= rc2
.right
= rc1
.right
- wGomerX
;
918 case ZYZG_ORIENT_BOTTOMTOTOP
:
919 rc1
.top
= rc2
.bottom
= rc1
.bottom
- wGomerY
;
922 case ZYZG_ORIENT_TOPTOBOTTOM
:
923 rc1
.bottom
= rc2
.top
+= wGomerY
;
927 rc1
.right
= rc2
.left
+= wGomerX
;
931 /* select the correct font */
932 hFont
= (HFONT
) SelectObject(hdc
, pgauge
->hFont
);
934 /* build up a string to blit out--ie the meaning of life: "42%" */
935 wsprintf(ach
, "%3d%%", (WORD
)((DWORD
)iPos
* 100 / iRange
));
936 /* Win32s has no GetTextExtent(); let's try GetTextExtentPoint() instead */
937 #if defined(__WIN32__)
938 GetTextExtentPoint(hdc
, ach
, wGomerX
= lstrlen(ach
), &size
);
941 dwExtent
= GetTextExtent(hdc
, ach
, wGomerX
= lstrlen(ach
));
945 /* Draw the finished (ie the percent done) side of box. If
946 * ZYZG_WW_POSITION is 42, (in range of 0 to 100) this ExtTextOut
947 * draws the meaning of life (42%) bar.
949 ExtTextOut(hdc
, (dx
- LOWORD(dwExtent
)) / 2 + Offset
,
950 (dy
- HIWORD(dwExtent
)) / 2 + Offset
,
951 ETO_OPAQUE
| ETO_CLIPPED
, &rc2
, ach
, wGomerX
, NULL
);
953 /* Reverse fore and back colors for drawing the undone (ie the non-
954 * finished) side of the box.
956 SetBkColor(hdc
, pgauge
->rgbTextColor
);
957 SetTextColor(hdc
, pgauge
->rgbBkColor
);
959 ExtTextOut(hdc
, (dx
- LOWORD(dwExtent
)) / 2 + Offset
,
960 (dy
- HIWORD(dwExtent
)) / 2 + Offset
,
961 ETO_OPAQUE
| ETO_CLIPPED
, &rc1
, ach
, wGomerX
, NULL
);
963 /* unselect the font */
964 SelectObject(hdc
, hFont
);
968 /** LRESULT FAR PASCAL gaugeWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
971 * This is the control's window procedure. Its purpose is to handle
972 * special messages for this custom control.
974 * The special control messages for the gauge control are:
976 * ZYZG_SETRANGE : Sets the range of the gauge. In other
977 * words, the number of parts that make a
980 * ZYZG_GETRANGE : Returns the current range of the gauge.
982 * ZYZG_SETORIENTATION : Sets the orientation of the gauge. This
983 * can be one of the ZYZG_ORIENT_?? msgs.
985 * ZYZG_GETORIENTATION : Gets the current orientation of the
988 * ZYZG_SETPOSITION : Sets the current position of the gauge.
989 * In other words, how many pieces of the
990 * whole have been used.
992 * ZYZG_GETPOSITION : Gets the current position of the gauge.
994 * ZYZG_SETDELTAPOS : Sets the position of the gauge +/- the
997 * ZYZG_SETFGCOLOR : Sets the foreground (percent done) color.
999 * ZYZG_GETFGCOLOR : Gets the foreground (percent done) color.
1001 * ZYZG_SETBKCOLOR : Sets the background (percent not done)
1004 * ZYZG_GETBKCOLOR : Gets the background (percent not done)
1007 * WM_SETFONT : Sets the font to use for the percentage
1008 * text of the gauge.
1010 * WM_GETFONT : Gets the current font in use by the
1017 /* LRESULT FAR PASCAL */
1019 LRESULT APIENTRY _EXPORT
gaugeWndProc(HWND hwnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
1026 // pgauge = (PZYZGAUGE)GetWindowWord(hwnd, ZYZG_WW_PZYZGAUGE);
1027 pgauge
= (PZYZGAUGE
)GetWindowLong(hwnd
, ZYZG_WW_PZYZGAUGE
);
1029 /* break to get DefWindowProc() */
1033 /* need to allocate a control block */
1034 // pgauge = (PZYZGAUGE)LocalAlloc(LPTR, sizeof(ZYZGAUGE));
1035 pgauge
= (PZYZGAUGE
)malloc(sizeof(ZYZGAUGE
));
1039 /* hang on to this control block */
1040 // SetWindowWord(hwnd, ZYZG_WW_PZYZGAUGE, (WORD)pgauge);
1041 SetWindowLong(hwnd
, ZYZG_WW_PZYZGAUGE
, (LONG
)pgauge
);
1043 /* fill control block with defaults */
1044 pgauge
->wRange
= ZYZG_DEF_RANGE
;
1045 pgauge
->wPosition
= ZYZG_DEF_POSITION
;
1046 pgauge
->wOrientation
= ZYZG_DEF_ORIENTATION
;
1047 pgauge
->wWidth3D
= ZYZG_DEF_WIDTH3D
;
1048 pgauge
->wWidthBezelFace
= ZYZG_DEF_BEZELFACE
;
1049 pgauge
->rgbTextColor
= rgbDefTextColor
;
1050 pgauge
->rgbBkColor
= rgbDefBkColor
;
1052 /* use system font */
1053 SendMessage(hwnd
, WM_SETFONT
, (WPARAM
)NULL
, 0L);
1055 /* go to DefWindowProc() to finish the job */
1059 /* get rid of the control's memory */
1061 // LocalFree((HANDLE)pgauge);
1065 case ZYZG_GETPOSITION
:
1066 return (pgauge
->wPosition
);
1069 return (pgauge
->wRange
);
1071 case ZYZG_GETORIENTATION
:
1072 return (pgauge
->wOrientation
);
1074 case ZYZG_GETWIDTH3D
:
1075 return (pgauge
->wWidth3D
);
1077 case ZYZG_GETBEZELFACE
:
1078 return (pgauge
->wWidthBezelFace
);
1080 case ZYZG_GETBKCOLOR
:
1081 return (pgauge
->rgbTextColor
);
1083 case ZYZG_GETFGCOLOR
:
1084 return (pgauge
->rgbBkColor
);
1086 case ZYZG_SETBKCOLOR
:
1087 pgauge
->rgbBkColor
= lParam
;
1090 case ZYZG_SETFGCOLOR
:
1091 pgauge
->rgbTextColor
= lParam
;
1095 case ZYZG_SETPOSITION
:
1096 pgauge
->wPosition
= wParam
;
1099 GetClientRect(hwnd
, &rc
);
1100 if ((GetWindowLong(hwnd
, GWL_STYLE
) & ZYZGS_3D
) && fSupport3D
)
1102 wParam
= (2 * pgauge
->wWidth3D
) +
1103 pgauge
->wWidthBezelFace
+ 2;
1109 InflateRect(&rc
, ~(wParam
), ~(wParam
));
1110 InvalidateRect(hwnd
, &rc
, FALSE
);
1115 pgauge
->wRange
= wParam
;
1116 goto zyzgForceRepaint
;
1118 case ZYZG_SETORIENTATION
:
1119 pgauge
->wOrientation
= wParam
;
1120 goto zyzgForceRepaint
;
1122 case ZYZG_SETWIDTH3D
:
1123 pgauge
->wWidth3D
= wParam
;
1126 InvalidateRect(hwnd
, NULL
, FALSE
);
1130 case ZYZG_SETBEZELFACE
:
1131 pgauge
->wWidthBezelFace
= wParam
;
1132 goto zyzgForceRepaint3D
;
1134 case ZYZG_SETDELTAPOS
:
1135 /* Watcom doesn't like the following line so removing typecasts */
1136 /* (int)pgauge->wPosition += (int)wParam; */
1137 pgauge
->wPosition
+= wParam
;
1138 goto zyzgForceRepaint
;
1141 BeginPaint(hwnd
, &ps
);
1142 gaugePaint(hwnd
, ps
.hdc
);
1143 EndPaint(hwnd
, &ps
);
1147 hFont
= pgauge
->hFont
;
1149 /* if system font, then return NULL handle */
1150 return (long)((hFont
== GetStockObject(SYSTEM_FONT
)) ? NULL
: hFont
);
1153 /* if NULL hFont, use system font */
1154 if (!(hFont
= (HFONT
)wParam
))
1155 hFont
= (HFONT
) GetStockObject(SYSTEM_FONT
);
1157 pgauge
->hFont
= hFont
;
1159 /* redraw if indicated in message */
1162 InvalidateRect(hwnd
, NULL
, TRUE
);
1168 /* let the dialog mangler take care of this message */
1169 return (DefWindowProc(hwnd
, uMsg
, wParam
, lParam
));
1170 } /* gaugeWndProc() */
1173 /** EOF: zyzgauge.c **/
1175 #endif // wxUSE_GAUGE