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