]> git.saurik.com Git - wxWidgets.git/blob - src/msw/gaugemsw.cpp
Applied patch #420395 (adding wxCLIP_SIBBLINGS capability -- but not by default)
[wxWidgets.git] / src / msw / gaugemsw.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: gaugemsw.cpp
3 // Purpose: wxGaugeMSW class
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "gauge.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/defs.h"
25 #include "wx/utils.h"
26 #endif
27
28 #if wxUSE_GAUGE
29
30 #include "wx/msw/gaugemsw.h"
31 #include "wx/msw/private.h"
32
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)
49
50
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
56
57 /* gauge styles */
58 #define ZYZGS_3D 0x8000L /* control will be 3D */
59
60 /* public function prototypes */
61 BOOL FAR PASCAL gaugeInit(HINSTANCE hInstance);
62
63 IMPLEMENT_DYNAMIC_CLASS(wxGaugeMSW, wxControl)
64
65 bool wxGaugeMSW::Create(wxWindow *parent, wxWindowID id,
66 int range,
67 const wxPoint& pos,
68 const wxSize& size,
69 long style,
70 const wxValidator& validator,
71 const wxString& name)
72 {
73 static bool wxGaugeMSWInitialised = FALSE;
74
75 if ( !wxGaugeMSWInitialised )
76 {
77 if (!gaugeInit((HINSTANCE) wxGetInstance()))
78 wxFatalError("Cannot initalize Gauge library");
79 wxGaugeMSWInitialised = TRUE;
80 }
81
82 SetName(name);
83 #if wxUSE_VALIDATORS
84 SetValidator(validator);
85 #endif // wxUSE_VALIDATORS
86
87 if (parent) parent->AddChild(this);
88 m_rangeMax = range;
89 m_gaugePos = 0;
90
91 SetBackgroundColour(parent->GetBackgroundColour()) ;
92 SetForegroundColour(parent->GetForegroundColour()) ;
93
94 m_windowStyle = style;
95
96 if ( id == -1 )
97 m_windowId = (int)NewControlId();
98 else
99 m_windowId = id;
100
101 int x = pos.x;
102 int y = pos.y;
103 int width = size.x;
104 int height = size.y;
105
106 long msFlags = WS_CHILD | WS_VISIBLE | WS_TABSTOP;
107 bool want3D;
108 WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D);
109 if (want3D)
110 msFlags |= ZYZGS_3D;
111
112 if ( m_windowStyle & wxCLIP_SIBLINGS )
113 msFlags |= WS_CLIPSIBLINGS;
114
115 HWND wx_button =
116 CreateWindowEx(MakeExtendedStyle(m_windowStyle), wxT("zYzGauge"), NULL, msFlags,
117 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId,
118 wxGetInstance(), NULL);
119
120 m_hWnd = (WXHWND)wx_button;
121
122 // Subclass again for purposes of dialog editing mode
123 SubclassWin((WXHWND)wx_button);
124
125 int wOrient = 0;
126
127 if (m_windowStyle & wxGA_HORIZONTAL)
128 wOrient = ZYZG_ORIENT_LEFTTORIGHT;
129 else
130 wOrient = ZYZG_ORIENT_BOTTOMTOTOP;
131
132 SendMessage(wx_button, ZYZG_SETORIENTATION, wOrient, 0);
133 SendMessage(wx_button, ZYZG_SETRANGE, range, 0);
134
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()));
137
138 //SetBezelFace(1);
139 //SetShadowWidth(1);
140
141 SetFont(parent->GetFont());
142
143 if (width == -1)
144 width = 50;
145 if (height == -1)
146 height = 50;
147 SetSize(x, y, width, height);
148
149 ShowWindow(GetHwnd(), SW_SHOW);
150
151 return TRUE;
152 }
153
154 void wxGaugeMSW::SetShadowWidth(int w)
155 {
156 SendMessage(GetHwnd(), ZYZG_SETWIDTH3D, w, 0);
157 }
158
159 void wxGaugeMSW::SetBezelFace(int w)
160 {
161 SendMessage(GetHwnd(), ZYZG_SETBEZELFACE, w, 0);
162 }
163
164 void wxGaugeMSW::SetRange(int r)
165 {
166 m_rangeMax = r;
167
168 SendMessage(GetHwnd(), ZYZG_SETRANGE, r, 0);
169 }
170
171 void wxGaugeMSW::SetValue(int pos)
172 {
173 m_gaugePos = pos;
174
175 SendMessage(GetHwnd(), ZYZG_SETPOSITION, pos, 0);
176 }
177
178 int wxGaugeMSW::GetShadowWidth(void) const
179 {
180 return (int) SendMessage(GetHwnd(), ZYZG_GETWIDTH3D, 0, 0);
181 }
182
183 int wxGaugeMSW::GetBezelFace(void) const
184 {
185 return (int) SendMessage(GetHwnd(), ZYZG_GETBEZELFACE, 0, 0);
186 }
187
188 int wxGaugeMSW::GetRange(void) const
189 {
190 return (int) SendMessage(GetHwnd(), ZYZG_GETRANGE, 0, 0);
191 }
192
193 int wxGaugeMSW::GetValue(void) const
194 {
195 return (int) SendMessage(GetHwnd(), ZYZG_GETPOSITION, 0, 0);
196 }
197
198 bool wxGaugeMSW::SetForegroundColour(const wxColour& col)
199 {
200 if ( !wxControl::SetForegroundColour(col) )
201 return FALSE;
202
203 SendMessage(GetHwnd(), ZYZG_SETFGCOLOR, 0, RGB(col.Red(), col.Green(), col.Blue()));
204
205 return TRUE;
206 }
207
208 bool wxGaugeMSW::SetBackgroundColour(const wxColour& col)
209 {
210 if ( !wxControl::SetBackgroundColour(col) )
211 return FALSE;
212
213 SendMessage(GetHwnd(), ZYZG_SETBKCOLOR, 0, RGB(col.Red(), col.Green(), col.Blue()));
214
215 return TRUE;
216 }
217
218
219 /** zyz3d.c
220 *
221 * DESCRIPTION:
222 * This module contains functions for creating nifty 3D borders
223 * around controls like zYzGauge.
224 *
225 * HISTORY:
226 * 3/14/91 cjp put in this comment
227 * 6/19/92 cjp touched it a bit
228 *
229 ** cjp */
230 // COPYRIGHT:
231 //
232 // (C) Copyright Microsoft Corp. 1992. All rights reserved.
233 //
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.
239 //
240
241
242 /* get the includes we need */
243 #include <windows.h>
244
245 /* misc. control flag defines */
246 #define DRAW3D_IN 0x0001
247 #define DRAW3D_OUT 0x0002
248
249 #define DRAW3D_TOPLINE 0x0004
250 #define DRAW3D_BOTTOMLINE 0x0008
251 #define DRAW3D_LEFTLINE 0x0010
252 #define DRAW3D_RIGHTLINE 0x0020
253
254
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);
259
260
261 /** void FAR PASCAL Draw3DFaceFrame(HDC hdc, LPRECT rc, WORD wWidth)
262 *
263 * DESCRIPTION:
264 * This function draws a flat frame with the current button-face
265 * color.
266 *
267 * ARGUMENTS:
268 * HDC hdc : The DC to draw into.
269 *
270 * LPRECT rc : The containing rect for the new frame.
271 *
272 * WORD wWidth : The width of the frame to draw.
273 *
274 * RETURN (void FAR PASCAL):
275 * The frame will have been drawn into the DC.
276 *
277 * NOTES:
278 *
279 ** cjp */
280
281 void FAR PASCAL Draw3DFaceFrame(HDC hdc, LPRECT rc, WORD wWidth)
282 {
283 RECT rc1;
284 DWORD rgbOld;
285
286 /* don't go through a bunch of work if we don't have to */
287 if (!wWidth)
288 return;
289
290 /* set up color to be button-face color--so it may not be gray */
291 rgbOld = SetBkColor(hdc, GetSysColor(COLOR_BTNFACE));
292
293 /* perform CopyRect w/o bloody windows style overhead */
294 rc1 = *rc;
295
296 /* top */
297 rc1.top = rc->top;
298 rc1.left = rc->left;
299 rc1.bottom = rc->top + wWidth;
300 rc1.right = rc->right;
301
302 /* blast it out */
303 ExtTextOut(hdc, rc1.left, rc1.top, ETO_OPAQUE, &rc1, NULL, 0, NULL);
304
305 /* right */
306 rc1.left = rc->right - wWidth;
307 rc1.bottom = rc->bottom;
308
309 /* blast this part now */
310 ExtTextOut(hdc, rc1.left, rc1.top, ETO_OPAQUE, &rc1, NULL, 0, NULL);
311
312 /* left */
313 rc1.left = rc->left;
314 rc1.right = rc->left + wWidth;
315
316 /* and another part */
317 ExtTextOut(hdc, rc1.left, rc1.top, ETO_OPAQUE, &rc1, NULL, 0, NULL);
318
319 /* bottom */
320 rc1.right = rc->right;
321 rc1.top = rc->bottom - wWidth;
322
323 /* finish it off */
324 ExtTextOut(hdc, rc1.left, rc1.top, ETO_OPAQUE, &rc1, NULL, 0, NULL);
325
326 /* restore the old bk color */
327 SetBkColor(hdc, rgbOld);
328 } /* Draw3DFaceFrame() */
329
330
331 /** void FAR PASCAL Draw3DRect(HDC, LPRECT, WORD, WORD)
332 *
333 * DESCRIPTION:
334 * Draws a 3D rectangle that is shaded. wFlags can be used to
335 * control how the rectangle looks.
336 *
337 * ARGUMENTS:
338 * HDC hdc : Handle to the device context that will be
339 * used to display the rectangle.
340 *
341 * RECT rect : A rectangle describing the dimensions of
342 * the rectangle in device coordinates.
343 *
344 * WORD wShadowWidth : Width of the shadow in device coordinates.
345 *
346 * WORD wFlags : The following flags may be passed to describe
347 * the style of the rectangle:
348 *
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.
352 *
353 * DRAW3D_OUT : The shadow is drawn such that
354 * the box appears to be sticking out of the
355 * screen.
356 *
357 * RETURN (void FAR PASCAL):
358 * The 3D looking rectangle will have been drawn into the DC.
359 *
360 * NOTES:
361 *
362 ** cjp */
363
364 void FAR PASCAL Draw3DRect(HDC hdc, LPRECT lpRect,
365 WORD wShadowWidth, WORD wFlags)
366 {
367 /* sanity check--don't work if you don't have to! */
368 if (!wShadowWidth || !RectVisible(hdc, lpRect))
369 return;
370
371 /* draw the top line */
372 Draw3DLine(hdc, lpRect->left, lpRect->top,
373 lpRect->right - lpRect->left,
374 wShadowWidth, DRAW3D_TOPLINE | wFlags);
375
376 /* right line */
377 Draw3DLine(hdc, lpRect->right, lpRect->top,
378 lpRect->bottom - lpRect->top,
379 wShadowWidth, DRAW3D_RIGHTLINE | wFlags);
380
381 /* bottom line */
382 Draw3DLine(hdc, lpRect->left, lpRect->bottom,
383 lpRect->right - lpRect->left,
384 wShadowWidth, DRAW3D_BOTTOMLINE | wFlags);
385
386 /* left line */
387 Draw3DLine(hdc, lpRect->left, lpRect->top,
388 lpRect->bottom - lpRect->top,
389 wShadowWidth, DRAW3D_LEFTLINE | wFlags);
390 } /* Draw3DRect() */
391
392
393 /** void FAR PASCAL Draw3DLine(HDC hdc, WORD x, WORD y, WORD nLen,
394 *
395 * DESCRIPTION:
396 * Draws a 3D line that can be used to make a 3D box.
397 *
398 * ARGUMENTS:
399 * HDC hdc : Handle to the device context that will be
400 * used to display the 3D line.
401 *
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.
407 *
408 * WORD wShadowWidth : Width of the shadow in device coordinates.
409 *
410 * WORD wFlags : The following flags may be passed to
411 * describe the style of the 3D line:
412 *
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.
416 *
417 * DRAW3D_OUT : The shadow is drawn such that
418 * the box appears to be sticking out of the
419 * screen.
420 *
421 * DRAW3D_TOPLINE, _BOTTOMLINE, _LEFTLINE, and
422 * _RIGHTLINE : Specifies that a "top",
423 * "Bottom", "Left", or"Right" line is to be
424 * drawn.
425 *
426 * RETURN (void FAR PASCAL):
427 * The line will have been drawn into the DC.
428 *
429 * NOTES:
430 *
431 ** cjp */
432
433 void FAR PASCAL Draw3DLine(HDC hdc, WORD x, WORD y, WORD nLen,
434 WORD wShadowWidth, WORD wFlags)
435 {
436 HBRUSH hOldBrush;
437 HPEN hOldPen;
438 BOOL fDark;
439 POINT Point[ 4 ]; /* define a polgon with 4 points */
440
441 /* if width is zero, don't do nothin'! */
442 if (!wShadowWidth)
443 return;
444
445 /* define shape of polygon--origin is always the same */
446 Point[0].x = x;
447 Point[0].y = y;
448
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
451 * switch/case!
452 */
453 if (wFlags & DRAW3D_TOPLINE)
454 {
455 /* across to right */
456 Point[1].x = x + nLen - (wShadowWidth == 1 ? 1 : 0);
457 Point[1].y = y;
458
459 /* down/left */
460 Point[2].x = x + nLen - wShadowWidth;
461 Point[2].y = y + wShadowWidth;
462
463 /* accross to left */
464 Point[3].x = x + wShadowWidth;
465 Point[3].y = y + wShadowWidth;
466
467 /* select 'dark' brush if 'in'--'light' for 'out' */
468 fDark = (wFlags & DRAW3D_IN) ? TRUE : FALSE;
469 }
470
471 /* possibly the bottom? */
472 else if (wFlags & DRAW3D_BOTTOMLINE)
473 {
474 /* across to right */
475 Point[1].x = x + nLen;
476 Point[1].y = y;
477
478 /* up/left */
479 Point[2].x = x + nLen - wShadowWidth;
480 Point[2].y = y - wShadowWidth;
481
482 /* accross to left */
483 Point[3].x = x + wShadowWidth;
484 Point[3].y = y - wShadowWidth;
485
486 /* select 'light' brush if 'in' */
487 fDark = (wFlags & DRAW3D_IN) ? FALSE : TRUE;
488 }
489
490 /* ok, it's gotta be left? */
491 else if (wFlags & DRAW3D_LEFTLINE)
492 {
493 /* down */
494 Point[1].x = x;
495 Point[1].y = y + nLen - (wShadowWidth == 1 ? 1 : 0);
496
497 /* up/right */
498 Point[2].x = x + wShadowWidth;
499 Point[2].y = y + nLen - wShadowWidth;
500
501 /* down */
502 Point[3].x = x + wShadowWidth;
503 Point[3].y = y + wShadowWidth;
504
505 /* select 'dark' brush if 'in'--'light' for 'out' */
506 fDark = (wFlags & DRAW3D_IN) ? TRUE : FALSE;
507 }
508
509 /* well maybe it's for the right side? */
510 else if (wFlags & DRAW3D_RIGHTLINE)
511 {
512 /* down */
513 Point[1].x = x;
514 Point[1].y = y + nLen;
515
516 /* up/left */
517 Point[2].x = x - wShadowWidth;
518 Point[2].y = y + nLen - wShadowWidth;
519
520 /* up */
521 Point[3].x = x - wShadowWidth;
522 Point[3].y = y + wShadowWidth;
523
524 /* select 'light' brush if 'in' */
525 fDark = (wFlags & DRAW3D_IN) ? FALSE : TRUE;
526 }
527
528 /* bad drugs? */
529 else return;
530
531 /* select NULL_PEN for no borders */
532 hOldPen = (HPEN) SelectObject(hdc, GetStockObject(NULL_PEN));
533
534 /* select the appropriate color for the fill */
535 if (fDark)
536 hOldBrush = (HBRUSH) SelectObject(hdc, GetStockObject(GRAY_BRUSH));
537 else
538 hOldBrush = (HBRUSH) SelectObject(hdc, GetStockObject(WHITE_BRUSH));
539
540 /* finally, draw the dern thing */
541 Polygon(hdc, (LPPOINT)&Point, 4);
542
543 /* restore what we killed */
544 SelectObject(hdc, hOldBrush);
545 SelectObject(hdc, hOldPen);
546 } /* Draw3DLine() */
547
548 /** EOF: zyz3d.c **/
549
550 /** zyzgauge.c
551 *
552 * DESCRIPTION:
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.
557 *
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):
560 *
561 * EXPORTS gaugeWndProc
562 *
563 * You then need initialize the class before you use it:
564 *
565 * if (!gaugeInit(hInstance))
566 * die a horrible death
567 * else
568 * you are good to go
569 *
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).
575 *
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.
580 *
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*
585 * integer.
586 *
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.
593 *
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.
599 *
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
605 * message.
606 *
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.
610 *
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.
614 *
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):
619 *
620 * ZYZG_ORIENT_LEFTTORIGHT (0)
621 * ZYZG_ORIENT_RIGHTTOLEFT (1)
622 * ZYZG_ORIENT_BOTTOMTOTOP (2)
623 * ZYZG_ORIENT_TOPTOBOTTOM (3)
624 *
625 * HISTORY:
626 * 3/12/91 cjp put in this comment
627 * 6/19/92 cjp touched it a bit
628 *
629 ** cjp */
630 // COPYRIGHT:
631 //
632 // (C) Copyright Microsoft Corp. 1992. All rights reserved.
633 //
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.
639 //
640
641
642 /* get the includes we need */
643 #if !defined(__GNUWIN32__) && !defined(__SALFORDC__)
644 #include <malloc.h>
645 #endif
646 #include <stdio.h>
647 #include <string.h>
648 #include <stdlib.h>
649 // #include "zyz3d.h"
650 // #include "zyzgauge.h"
651
652
653 /* static global variables */
654 static wxChar gszzYzGaugeClass[] = wxT("zYzGauge");
655
656
657 /* window word position definitions */
658 #define ZYZG_WW_PZYZGAUGE 0
659 /* #define ZYZG_WW_EXTRABYTES 2 */
660 #define ZYZG_WW_EXTRABYTES 4
661
662
663 /* control block structure typedef */
664 typedef struct tZYZGAUGE
665 {
666 WORD wRange;
667 WORD wPosition;
668 WORD wOrientation;
669 WORD wWidth3D;
670 WORD wWidthBezelFace;
671 HFONT hFont;
672 DWORD rgbTextColor;
673 DWORD rgbBkColor;
674
675 } ZYZGAUGE, *PZYZGAUGE, FAR *LPZYZGAUGE;
676
677
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
684
685
686
687 /* the default settings for drawing colors--display dependent */
688 static DWORD rgbDefTextColor;
689 static DWORD rgbDefBkColor;
690 static BOOL fSupport3D;
691
692 #if !defined(APIENTRY) // NT defines APIENTRY, 3.x not
693 #define APIENTRY FAR PASCAL
694 #endif
695
696 #ifdef __WIN32__
697 #define _EXPORT /**/
698 #else
699 #define _EXPORT _export
700 typedef signed short int SHORT ;
701 #endif
702
703 /* internal function prototypes */
704 static void PASCAL gaugePaint(HWND, HDC);
705 /* LRESULT FAR PASCAL */
706 LRESULT APIENTRY _EXPORT gaugeWndProc(HWND, UINT, WPARAM, LPARAM);
707
708
709
710 /** BOOL FAR PASCAL gaugeInit(HINSTANCE hInstance)
711 *
712 * DESCRIPTION:
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!
717 *
718 * ARGUMENTS:
719 * HINSTANCE hInstance : Instance handle to register class with.
720 *
721 * RETURN (BOOL FAR):
722 * The return value is TRUE if the zYzGauge class was successfully
723 * registered. It is FALSE if the initialization fails.
724 *
725 * NOTES:
726 *
727 ** cjp */
728
729 //#pragma alloc_text(init, gaugeInit)
730
731 BOOL FAR PASCAL gaugeInit(HINSTANCE hInstance)
732 {
733 static BOOL fRegistered = FALSE;
734 WNDCLASS wc;
735 HDC hdc;
736
737 /* assume already registered if not first instance */
738 if (fRegistered)
739 return (TRUE);
740
741 /* fill in the class structure for the zyzgauge control */
742 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
743 wc.hIcon = NULL;
744 wc.lpszMenuName = NULL;
745 wc.lpszClassName = gszzYzGaugeClass;
746 wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
747 wc.hInstance = hInstance;
748
749 #ifdef ZYZGAUGE_DLL
750 wc.style = CS_GLOBALCLASS | CS_HREDRAW | CS_VREDRAW;
751 #else
752 wc.style = CS_HREDRAW | CS_VREDRAW;
753 #endif
754
755 wc.lpfnWndProc = gaugeWndProc;
756 wc.cbClsExtra = 0;
757 wc.cbWndExtra = ZYZG_WW_EXTRABYTES;
758
759 /* attempt to register it--return FALSE if fail */
760 if (!RegisterClass(&wc))
761 return (FALSE);
762
763 /* Get a DC to determine whether device is mono or not, and set
764 * default foreground/background colors as appropriate.
765 */
766 hdc = CreateIC(wxT("DISPLAY"), NULL, NULL, 0L) ;
767 if (hdc)
768 {
769 /* check for mono-display */
770 if ((GetDeviceCaps(hdc, BITSPIXEL) == 1) &&
771 (GetDeviceCaps(hdc, PLANES) == 1))
772 {
773 /* using a mono DC--white foreground, black background */
774 rgbDefTextColor = RGB(255, 255, 255);
775 rgbDefBkColor = RGB(0, 0, 0);
776 }
777
778 /* good! we have color: blue foreground, white background */
779 else
780 {
781 rgbDefTextColor = RGB(0, 0, 255);
782 rgbDefBkColor = RGB(255, 255, 255);
783 }
784
785 /* need at _least_ 8 for two shades of gray (>=VGA) */
786 fSupport3D = (GetDeviceCaps(hdc, NUMCOLORS) >= 8) ? TRUE : FALSE;
787
788 /* get rid of the DC (IC) */
789 DeleteDC(hdc);
790 }
791
792 /* uh-oh... can't get DC (IC)... fail */
793 else
794 {
795 /* unregister the class */
796 UnregisterClass(gszzYzGaugeClass, hInstance);
797 return (FALSE);
798 }
799
800 /* return success */
801 return (fRegistered = TRUE);
802 } /* gaugeInit() */
803
804
805 /** static void PASCAL gaugePaint(HWND hwnd, HDC hdc)
806 *
807 * DESCRIPTION:
808 * This function is responsible for painting the zYzGauge control.
809 *
810 * ARGUMENTS:
811 * HWND hwnd : The window handle for the gauge.
812 *
813 * HDC hdc : The DC for the gauge's window.
814 *
815 * RETURN (void):
816 * The control will have been painted.
817 *
818 * NOTES:
819 *
820 ** cjp */
821
822 static void PASCAL gaugePaint(HWND hwnd, HDC hdc)
823 {
824 PZYZGAUGE pgauge;
825 WORD iRange, iPos;
826 WORD Offset = 1;
827 DWORD dwExtent;
828 RECT rc1, rc2;
829 HFONT hFont;
830 wxChar ach[ 6 ];
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__)
835 SIZE size;
836 #endif
837
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);
841
842 /* set the colors into for the gauge into the control */
843 SetTextColor(hdc, pgauge->rgbTextColor);
844 SetBkColor(hdc, pgauge->rgbBkColor);
845
846 /* draw black rectangle for gauge */
847 GetClientRect(hwnd, &rc1);
848
849 /* draw a black border on the _outside_ */
850 FrameRect(hdc, &rc1, (HBRUSH) GetStockObject(BLACK_BRUSH));
851
852 /* we want to draw _just inside_ the black border */
853 InflateRect(&rc1, -1, -1);
854
855 /* one line thick so far... */
856 // Offset = (WORD) 1;
857
858 /* for 3D stuff, we need to have at least two shades of gray */
859 if ((GetWindowLong(hwnd, GWL_STYLE) & ZYZGS_3D) && fSupport3D)
860 {
861 Draw3DRect(hdc, &rc1, pgauge->wWidth3D, DRAW3D_OUT);
862 InflateRect(&rc1, ~(pgauge->wWidth3D), ~(pgauge->wWidth3D));
863
864 Draw3DFaceFrame(hdc, &rc1, pgauge->wWidthBezelFace);
865 InflateRect(&rc1, ~(pgauge->wWidthBezelFace), ~(pgauge->wWidthBezelFace));
866
867 Draw3DRect(hdc, &rc1, pgauge->wWidth3D, DRAW3D_IN);
868 InflateRect(&rc1, ~(pgauge->wWidth3D), ~(pgauge->wWidth3D));
869
870 /* draw a black border on the _inside_ */
871 FrameRect(hdc, &rc1, (HBRUSH) GetStockObject(BLACK_BRUSH));
872
873 /* we want to draw _just inside_ the black border */
874 InflateRect(&rc1, -1, -1);
875
876 /* add all the other pixels into the border width */
877 Offset += (2 * pgauge->wWidth3D) + pgauge->wWidthBezelFace + 1;
878 }
879
880 /* dup--one rc for 'how much filled', one rc for 'how much empty' */
881 rc2 = rc1;
882
883 /* get the range--make sure it's a valid range */
884 if ((iRange = pgauge->wRange) <= 0)
885 iRange = 1;
886
887 /* get the position--greater than 100% would be bad */
888 if ((iPos = pgauge->wPosition) > iRange)
889 iPos = iRange;
890
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);
896
897 /* get the orientation and munge rects accordingly */
898 switch (pgauge->wOrientation)
899 {
900 case ZYZG_ORIENT_RIGHTTOLEFT:
901 rc1.left = rc2.right = rc1.right - wGomerX;
902 break;
903
904 case ZYZG_ORIENT_BOTTOMTOTOP:
905 rc1.top = rc2.bottom = rc1.bottom - wGomerY;
906 break;
907
908 case ZYZG_ORIENT_TOPTOBOTTOM:
909 rc1.bottom = rc2.top += wGomerY;
910 break;
911
912 default:
913 rc1.right = rc2.left += wGomerX;
914 break;
915 } /* switch () */
916
917 /* select the correct font */
918 hFont = (HFONT) SelectObject(hdc, pgauge->hFont);
919
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);
925 dwExtent = size.cx;
926 #else
927 dwExtent = GetTextExtent(hdc, ach, wGomerX = lstrlen(ach));
928 #endif
929
930
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.
934 */
935 ExtTextOut(hdc, (dx - LOWORD(dwExtent)) / 2 + Offset,
936 (dy - HIWORD(dwExtent)) / 2 + Offset,
937 ETO_OPAQUE | ETO_CLIPPED, &rc2, ach, wGomerX, NULL);
938
939 /* Reverse fore and back colors for drawing the undone (ie the non-
940 * finished) side of the box.
941 */
942 SetBkColor(hdc, pgauge->rgbTextColor);
943 SetTextColor(hdc, pgauge->rgbBkColor);
944
945 ExtTextOut(hdc, (dx - LOWORD(dwExtent)) / 2 + Offset,
946 (dy - HIWORD(dwExtent)) / 2 + Offset,
947 ETO_OPAQUE | ETO_CLIPPED, &rc1, ach, wGomerX, NULL);
948
949 /* unselect the font */
950 SelectObject(hdc, hFont);
951 } /* gaugePaint() */
952
953
954 /** LRESULT FAR PASCAL gaugeWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
955 *
956 * DESCRIPTION:
957 * This is the control's window procedure. Its purpose is to handle
958 * special messages for this custom control.
959 *
960 * The special control messages for the gauge control are:
961 *
962 * ZYZG_SETRANGE : Sets the range of the gauge. In other
963 * words, the number of parts that make a
964 * whole.
965 *
966 * ZYZG_GETRANGE : Returns the current range of the gauge.
967 *
968 * ZYZG_SETORIENTATION : Sets the orientation of the gauge. This
969 * can be one of the ZYZG_ORIENT_?? msgs.
970 *
971 * ZYZG_GETORIENTATION : Gets the current orientation of the
972 * gauge.
973 *
974 * ZYZG_SETPOSITION : Sets the current position of the gauge.
975 * In other words, how many pieces of the
976 * whole have been used.
977 *
978 * ZYZG_GETPOSITION : Gets the current position of the gauge.
979 *
980 * ZYZG_SETDELTAPOS : Sets the position of the gauge +/- the
981 * specified amount.
982 *
983 * ZYZG_SETFGCOLOR : Sets the foreground (percent done) color.
984 *
985 * ZYZG_GETFGCOLOR : Gets the foreground (percent done) color.
986 *
987 * ZYZG_SETBKCOLOR : Sets the background (percent not done)
988 * color.
989 *
990 * ZYZG_GETBKCOLOR : Gets the background (percent not done)
991 * color.
992 *
993 * WM_SETFONT : Sets the font to use for the percentage
994 * text of the gauge.
995 *
996 * WM_GETFONT : Gets the current font in use by the
997 * gauge.
998 *
999 * NOTES:
1000 *
1001 ** cjp */
1002
1003 /* LRESULT FAR PASCAL */
1004
1005 LRESULT APIENTRY _EXPORT gaugeWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1006 {
1007 HFONT hFont;
1008 PAINTSTRUCT ps;
1009 PZYZGAUGE pgauge;
1010 RECT rc;
1011
1012 // pgauge = (PZYZGAUGE)GetWindowWord(hwnd, ZYZG_WW_PZYZGAUGE);
1013 pgauge = (PZYZGAUGE)GetWindowLong(hwnd, ZYZG_WW_PZYZGAUGE);
1014
1015 /* break to get DefWindowProc() */
1016 switch (uMsg)
1017 {
1018 case WM_CREATE:
1019 /* need to allocate a control block */
1020 // pgauge = (PZYZGAUGE)LocalAlloc(LPTR, sizeof(ZYZGAUGE));
1021 pgauge = (PZYZGAUGE)malloc(sizeof(ZYZGAUGE));
1022 if (!pgauge)
1023 return (0L);
1024
1025 /* hang on to this control block */
1026 // SetWindowWord(hwnd, ZYZG_WW_PZYZGAUGE, (WORD)pgauge);
1027 SetWindowLong(hwnd, ZYZG_WW_PZYZGAUGE, (LONG)pgauge);
1028
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;
1037
1038 /* use system font */
1039 SendMessage(hwnd, WM_SETFONT, (WPARAM)NULL, 0L);
1040
1041 /* go to DefWindowProc() to finish the job */
1042 break;
1043
1044 case WM_DESTROY:
1045 /* get rid of the control's memory */
1046 if (pgauge)
1047 // LocalFree((HANDLE)pgauge);
1048 free(pgauge);
1049 break;
1050
1051 case ZYZG_GETPOSITION:
1052 return (pgauge->wPosition);
1053
1054 case ZYZG_GETRANGE:
1055 return (pgauge->wRange);
1056
1057 case ZYZG_GETORIENTATION:
1058 return (pgauge->wOrientation);
1059
1060 case ZYZG_GETWIDTH3D:
1061 return (pgauge->wWidth3D);
1062
1063 case ZYZG_GETBEZELFACE:
1064 return (pgauge->wWidthBezelFace);
1065
1066 case ZYZG_GETBKCOLOR:
1067 return (pgauge->rgbTextColor);
1068
1069 case ZYZG_GETFGCOLOR:
1070 return (pgauge->rgbBkColor);
1071
1072 case ZYZG_SETBKCOLOR:
1073 pgauge->rgbBkColor = lParam;
1074 return (0L);
1075
1076 case ZYZG_SETFGCOLOR:
1077 pgauge->rgbTextColor = lParam;
1078 return (0L);
1079
1080
1081 case ZYZG_SETPOSITION:
1082 pgauge->wPosition = wParam;
1083
1084 zyzgForceRepaint:
1085 GetClientRect(hwnd, &rc);
1086 if ((GetWindowLong(hwnd, GWL_STYLE) & ZYZGS_3D) && fSupport3D)
1087 {
1088 wParam = (2 * pgauge->wWidth3D) +
1089 pgauge->wWidthBezelFace + 2;
1090 }
1091
1092 else
1093 wParam = 1;
1094
1095 InflateRect(&rc, ~(wParam), ~(wParam));
1096 InvalidateRect(hwnd, &rc, FALSE);
1097 UpdateWindow(hwnd);
1098 return (0L);
1099
1100 case ZYZG_SETRANGE:
1101 pgauge->wRange = wParam;
1102 goto zyzgForceRepaint;
1103
1104 case ZYZG_SETORIENTATION:
1105 pgauge->wOrientation = wParam;
1106 goto zyzgForceRepaint;
1107
1108 case ZYZG_SETWIDTH3D:
1109 pgauge->wWidth3D = wParam;
1110
1111 zyzgForceRepaint3D:
1112 InvalidateRect(hwnd, NULL, FALSE);
1113 UpdateWindow(hwnd);
1114 return (0L);
1115
1116 case ZYZG_SETBEZELFACE:
1117 pgauge->wWidthBezelFace = wParam;
1118 goto zyzgForceRepaint3D;
1119
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;
1125
1126 case WM_PAINT:
1127 BeginPaint(hwnd, &ps);
1128 gaugePaint(hwnd, ps.hdc);
1129 EndPaint(hwnd, &ps);
1130 return (0L);
1131
1132 case WM_GETFONT:
1133 hFont = pgauge->hFont;
1134
1135 /* if system font, then return NULL handle */
1136 return (long)((hFont == GetStockObject(SYSTEM_FONT)) ? NULL : hFont);
1137
1138 case WM_SETFONT:
1139 /* if NULL hFont, use system font */
1140 hFont = (HFONT)wParam ;
1141 if (!hFont)
1142 hFont = (HFONT) GetStockObject(SYSTEM_FONT);
1143
1144 pgauge->hFont = hFont;
1145
1146 /* redraw if indicated in message */
1147 if ((BOOL)lParam)
1148 {
1149 InvalidateRect(hwnd, NULL, TRUE);
1150 UpdateWindow(hwnd);
1151 }
1152 return (0L);
1153 } /* switch () */
1154
1155 /* let the dialog mangler take care of this message */
1156 return (DefWindowProc(hwnd, uMsg, wParam, lParam));
1157 } /* gaugeWndProc() */
1158
1159
1160 /** EOF: zyzgauge.c **/
1161
1162 #endif // wxUSE_GAUGE