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