]> git.saurik.com Git - wxWidgets.git/blame - src/msw/gaugemsw.cpp
Removed USING_CONFIGURE define. As VZ pointed out, checking HAVE_CONFIG_H
[wxWidgets.git] / src / msw / gaugemsw.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
da87a1ca
JS
2// Name: gaugemsw.cpp
3// Purpose: wxGaugeMSW class
2bda0e17
KB
4// Author: Julian Smart
5// Modified by:
6// Created: 01/02/97
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart and Markus Holzem
93089472 9// Licence: wxWindows licence
2bda0e17
KB
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"
2432b92d 25#include "wx/utils.h"
2bda0e17
KB
26#endif
27
47d67540 28#if wxUSE_GAUGE
2bda0e17 29
da87a1ca 30#include "wx/msw/gaugemsw.h"
2bda0e17
KB
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
2bda0e17
KB
57/* gauge styles */
58#define ZYZGS_3D 0x8000L /* control will be 3D */
59
60/* public function prototypes */
61BOOL FAR PASCAL gaugeInit(HINSTANCE hInstance);
62
2bda0e17 63#if !USE_SHARED_LIBRARY
da87a1ca 64IMPLEMENT_DYNAMIC_CLASS(wxGaugeMSW, wxControl)
2bda0e17
KB
65#endif
66
debe6624
JS
67bool wxGaugeMSW::Create(wxWindow *parent, wxWindowID id,
68 int range,
2bda0e17
KB
69 const wxPoint& pos,
70 const wxSize& size,
debe6624 71 long style,
2bda0e17
KB
72 const wxValidator& validator,
73 const wxString& name)
74{
da87a1ca 75 static bool wxGaugeMSWInitialised = FALSE;
2bda0e17 76
da87a1ca 77 if ( !wxGaugeMSWInitialised )
2bda0e17 78 {
c4e7c2aa 79 if (!gaugeInit((HINSTANCE) wxGetInstance()))
93089472
VZ
80 wxFatalError("Cannot initalize Gauge library");
81 wxGaugeMSWInitialised = TRUE;
2bda0e17
KB
82 }
83
84 SetName(name);
85 SetValidator(validator);
86
87 if (parent) parent->AddChild(this);
88 m_rangeMax = range;
9c331ded 89 m_gaugePos = 0;
2bda0e17 90
fd71308f
JS
91 SetBackgroundColour(parent->GetBackgroundColour()) ;
92 SetForegroundColour(parent->GetForegroundColour()) ;
2bda0e17 93
2bda0e17
KB
94 m_windowStyle = style;
95
96 if ( id == -1 )
93089472 97 m_windowId = (int)NewControlId();
2bda0e17 98 else
93089472 99 m_windowId = id;
2bda0e17
KB
100
101 int x = pos.x;
102 int y = pos.y;
103 int width = size.x;
104 int height = size.y;
105
da87a1ca
JS
106 long msFlags = WS_CHILD | WS_VISIBLE | WS_TABSTOP;
107 msFlags |= ZYZGS_3D;
2bda0e17 108
da87a1ca 109 HWND wx_button =
837e5743 110 CreateWindowEx(MakeExtendedStyle(m_windowStyle), _T("zYzGauge"), NULL, msFlags,
2bda0e17
KB
111 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId,
112 wxGetInstance(), NULL);
113
da87a1ca 114 m_hWnd = (WXHWND)wx_button;
2bda0e17 115
da87a1ca
JS
116 // Subclass again for purposes of dialog editing mode
117 SubclassWin((WXHWND)wx_button);
2bda0e17 118
da87a1ca 119 int wOrient = 0;
2bda0e17 120
da87a1ca
JS
121 if (m_windowStyle & wxGA_HORIZONTAL)
122 wOrient = ZYZG_ORIENT_LEFTTORIGHT;
123 else
124 wOrient = ZYZG_ORIENT_BOTTOMTOTOP;
2bda0e17 125
da87a1ca
JS
126 SendMessage(wx_button, ZYZG_SETORIENTATION, wOrient, 0);
127 SendMessage(wx_button, ZYZG_SETRANGE, range, 0);
2bda0e17 128
4438caf4
VZ
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()));
2bda0e17 131
c0ed460c 132 SetFont(parent->GetFont());
2bda0e17
KB
133
134 if (width == -1)
135 width = 50;
136 if (height == -1)
137 height = 50;
138 SetSize(x, y, width, height);
139
4438caf4 140 ShowWindow(GetHwnd(), SW_SHOW);
2bda0e17
KB
141
142 return TRUE;
143}
144
debe6624 145void wxGaugeMSW::SetShadowWidth(int w)
2bda0e17 146{
4438caf4 147 SendMessage(GetHwnd(), ZYZG_SETWIDTH3D, w, 0);
2bda0e17
KB
148}
149
debe6624 150void wxGaugeMSW::SetBezelFace(int w)
2bda0e17 151{
4438caf4 152 SendMessage(GetHwnd(), ZYZG_SETBEZELFACE, w, 0);
2bda0e17
KB
153}
154
debe6624 155void wxGaugeMSW::SetRange(int r)
2bda0e17
KB
156{
157 m_rangeMax = r;
158
4438caf4 159 SendMessage(GetHwnd(), ZYZG_SETRANGE, r, 0);
2bda0e17
KB
160}
161
debe6624 162void wxGaugeMSW::SetValue(int pos)
2bda0e17
KB
163{
164 m_gaugePos = pos;
165
4438caf4 166 SendMessage(GetHwnd(), ZYZG_SETPOSITION, pos, 0);
2bda0e17
KB
167}
168
da87a1ca 169int wxGaugeMSW::GetShadowWidth(void) const
2bda0e17 170{
4438caf4 171 return (int) SendMessage(GetHwnd(), ZYZG_GETWIDTH3D, 0, 0);
2bda0e17
KB
172}
173
da87a1ca 174int wxGaugeMSW::GetBezelFace(void) const
2bda0e17 175{
4438caf4 176 return (int) SendMessage(GetHwnd(), ZYZG_GETBEZELFACE, 0, 0);
2bda0e17
KB
177}
178
da87a1ca 179int wxGaugeMSW::GetRange(void) const
2bda0e17 180{
4438caf4 181 return (int) SendMessage(GetHwnd(), ZYZG_GETRANGE, 0, 0);
2bda0e17
KB
182}
183
da87a1ca 184int wxGaugeMSW::GetValue(void) const
2bda0e17 185{
4438caf4 186 return (int) SendMessage(GetHwnd(), ZYZG_GETPOSITION, 0, 0);
2bda0e17
KB
187}
188
93089472 189bool wxGaugeMSW::SetForegroundColour(const wxColour& col)
2bda0e17 190{
93089472
VZ
191 if ( !wxControl::SetForegroundColour(col) )
192 return FALSE;
193
4438caf4 194 SendMessage(GetHwnd(), ZYZG_SETFGCOLOR, 0, RGB(col.Red(), col.Green(), col.Blue()));
93089472
VZ
195
196 return TRUE;
2bda0e17
KB
197}
198
a9ac0bea 199bool wxGaugeMSW::SetBackgroundColour(const wxColour& col)
2bda0e17 200{
93089472
VZ
201 if ( !wxControl::SetBackgroundColour(col) )
202 return FALSE;
203
4438caf4 204 SendMessage(GetHwnd(), ZYZG_SETBKCOLOR, 0, RGB(col.Red(), col.Green(), col.Blue()));
93089472
VZ
205
206 return TRUE;
2bda0e17
KB
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 */
247void FAR PASCAL Draw3DFaceFrame(HDC, LPRECT, WORD);
248void FAR PASCAL Draw3DRect(HDC, LPRECT, WORD, WORD);
249void 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
272void 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
355void 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
424void 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 */
c4e7c2aa 523 hOldPen = (HPEN) SelectObject(hdc, GetStockObject(NULL_PEN));
2bda0e17
KB
524
525 /* select the appropriate color for the fill */
526 if (fDark)
c4e7c2aa 527 hOldBrush = (HBRUSH) SelectObject(hdc, GetStockObject(GRAY_BRUSH));
2bda0e17 528 else
c4e7c2aa 529 hOldBrush = (HBRUSH) SelectObject(hdc, GetStockObject(WHITE_BRUSH));
2bda0e17
KB
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 */
ce3ed50d 634#if !defined(__GNUWIN32__) && !defined(__SALFORDC__)
2bda0e17
KB
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 */
837e5743 645static wxChar gszzYzGaugeClass[] = _T("zYzGauge");
2bda0e17
KB
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 */
655typedef 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 */
679static DWORD rgbDefTextColor;
680static DWORD rgbDefBkColor;
681static BOOL fSupport3D;
682
93089472 683#if !defined(APIENTRY) // NT defines APIENTRY, 3.x not
2bda0e17
KB
684#define APIENTRY FAR PASCAL
685#endif
686
687#ifdef __WIN32__
688#define _EXPORT /**/
689#else
690#define _EXPORT _export
691typedef signed short int SHORT ;
692#endif
693
694/* internal function prototypes */
695static void PASCAL gaugePaint(HWND, HDC);
696/* LRESULT FAR PASCAL */
697LRESULT 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
722BOOL 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 */
837e5743 757 if ((hdc = CreateIC(_T("DISPLAY"), NULL, NULL, 0L)))
2bda0e17
KB
758 {
759 /* check for mono-display */
760 if ((GetDeviceCaps(hdc, BITSPIXEL) == 1) &&
761 (GetDeviceCaps(hdc, PLANES) == 1))
762 {
763 /* using a mono DC--white foreground, black background */
764 rgbDefTextColor = RGB(255, 255, 255);
765 rgbDefBkColor = RGB(0, 0, 0);
766 }
767
768 /* good! we have color: blue foreground, white background */
769 else
770 {
771 rgbDefTextColor = RGB(0, 0, 255);
772 rgbDefBkColor = RGB(255, 255, 255);
773 }
774
775 /* need at _least_ 8 for two shades of gray (>=VGA) */
776 fSupport3D = (GetDeviceCaps(hdc, NUMCOLORS) >= 8) ? TRUE : FALSE;
777
778 /* get rid of the DC (IC) */
779 DeleteDC(hdc);
780 }
781
782 /* uh-oh... can't get DC (IC)... fail */
783 else
784 {
785 /* unregister the class */
786 UnregisterClass(gszzYzGaugeClass, hInstance);
787 return (FALSE);
788 }
789
790 /* return success */
791 return (fRegistered = TRUE);
792} /* gaugeInit() */
793
794
795/** static void PASCAL gaugePaint(HWND hwnd, HDC hdc)
796 *
797 * DESCRIPTION:
798 * This function is responsible for painting the zYzGauge control.
799 *
800 * ARGUMENTS:
801 * HWND hwnd : The window handle for the gauge.
802 *
803 * HDC hdc : The DC for the gauge's window.
804 *
805 * RETURN (void):
806 * The control will have been painted.
807 *
808 * NOTES:
809 *
810 ** cjp */
811
812static void PASCAL gaugePaint(HWND hwnd, HDC hdc)
813{
814 PZYZGAUGE pgauge;
815 WORD iRange, iPos;
816 WORD Offset = 1;
817 DWORD dwExtent;
818 RECT rc1, rc2;
819 HFONT hFont;
837e5743 820 wxChar ach[ 6 ];
2bda0e17
KB
821 WORD dx, dy, wGomerX, wGomerY;
822/* Win32s has no GetTextExtent(); let's try GetTextExtentPoint() instead,
823 * which needs a SIZE* parameter */
824#if defined(__WIN32__)
825 SIZE size;
826#endif
827
828 /* get pointer to the control's control block */
829// pgauge = (PZYZGAUGE)GetWindowWord(hwnd, ZYZG_WW_PZYZGAUGE);
830 pgauge = (PZYZGAUGE)GetWindowLong(hwnd, ZYZG_WW_PZYZGAUGE);
831
832 /* set the colors into for the gauge into the control */
833 SetTextColor(hdc, pgauge->rgbTextColor);
834 SetBkColor(hdc, pgauge->rgbBkColor);
835
836 /* draw black rectangle for gauge */
837 GetClientRect(hwnd, &rc1);
838
839 /* draw a black border on the _outside_ */
c4e7c2aa 840 FrameRect(hdc, &rc1, (HBRUSH) GetStockObject(BLACK_BRUSH));
2bda0e17
KB
841
842 /* we want to draw _just inside_ the black border */
843 InflateRect(&rc1, -1, -1);
844
845 /* one line thick so far... */
846// Offset = (WORD) 1;
847
848 /* for 3D stuff, we need to have at least two shades of gray */
849 if ((GetWindowLong(hwnd, GWL_STYLE) & ZYZGS_3D) && fSupport3D)
850 {
851 Draw3DRect(hdc, &rc1, pgauge->wWidth3D, DRAW3D_OUT);
93089472 852 InflateRect(&rc1, ~(pgauge->wWidth3D), ~(pgauge->wWidth3D));
2bda0e17
KB
853
854 Draw3DFaceFrame(hdc, &rc1, pgauge->wWidthBezelFace);
93089472 855 InflateRect(&rc1, ~(pgauge->wWidthBezelFace), ~(pgauge->wWidthBezelFace));
2bda0e17
KB
856
857 Draw3DRect(hdc, &rc1, pgauge->wWidth3D, DRAW3D_IN);
93089472 858 InflateRect(&rc1, ~(pgauge->wWidth3D), ~(pgauge->wWidth3D));
2bda0e17
KB
859
860 /* draw a black border on the _inside_ */
c4e7c2aa 861 FrameRect(hdc, &rc1, (HBRUSH) GetStockObject(BLACK_BRUSH));
2bda0e17
KB
862
863 /* we want to draw _just inside_ the black border */
864 InflateRect(&rc1, -1, -1);
865
866 /* add all the other pixels into the border width */
867 Offset += (2 * pgauge->wWidth3D) + pgauge->wWidthBezelFace + 1;
868 }
869
870 /* dup--one rc for 'how much filled', one rc for 'how much empty' */
871 rc2 = rc1;
872
873 /* get the range--make sure it's a valid range */
874 if ((iRange = pgauge->wRange) <= 0)
875 iRange = 1;
876
877 /* get the position--greater than 100% would be bad */
878 if ((iPos = pgauge->wPosition) > iRange)
879 iPos = iRange;
880
881 /* compute the actual size of the gauge */
882 dx = rc1.right - rc1.left;
883 dy = rc1.bottom - rc1.top;
884 wGomerX = (WORD)((DWORD)iPos * dx / iRange);
885 wGomerY = (WORD)((DWORD)iPos * dy / iRange);
886
887 /* get the orientation and munge rects accordingly */
888 switch (pgauge->wOrientation)
889 {
890 case ZYZG_ORIENT_RIGHTTOLEFT:
891 rc1.left = rc2.right = rc1.right - wGomerX;
892 break;
893
894 case ZYZG_ORIENT_BOTTOMTOTOP:
895 rc1.top = rc2.bottom = rc1.bottom - wGomerY;
896 break;
897
898 case ZYZG_ORIENT_TOPTOBOTTOM:
899 rc1.bottom = rc2.top += wGomerY;
900 break;
901
902 default:
903 rc1.right = rc2.left += wGomerX;
904 break;
905 } /* switch () */
906
907 /* select the correct font */
c4e7c2aa 908 hFont = (HFONT) SelectObject(hdc, pgauge->hFont);
2bda0e17
KB
909
910 /* build up a string to blit out--ie the meaning of life: "42%" */
837e5743 911 wsprintf(ach, _T("%3d%%"), (WORD)((DWORD)iPos * 100 / iRange));
2bda0e17
KB
912/* Win32s has no GetTextExtent(); let's try GetTextExtentPoint() instead */
913#if defined(__WIN32__)
914 GetTextExtentPoint(hdc, ach, wGomerX = lstrlen(ach), &size);
915 dwExtent = size.cx;
916#else
917 dwExtent = GetTextExtent(hdc, ach, wGomerX = lstrlen(ach));
918#endif
919
920
921 /* Draw the finished (ie the percent done) side of box. If
922 * ZYZG_WW_POSITION is 42, (in range of 0 to 100) this ExtTextOut
923 * draws the meaning of life (42%) bar.
924 */
925 ExtTextOut(hdc, (dx - LOWORD(dwExtent)) / 2 + Offset,
926 (dy - HIWORD(dwExtent)) / 2 + Offset,
927 ETO_OPAQUE | ETO_CLIPPED, &rc2, ach, wGomerX, NULL);
928
929 /* Reverse fore and back colors for drawing the undone (ie the non-
930 * finished) side of the box.
931 */
932 SetBkColor(hdc, pgauge->rgbTextColor);
933 SetTextColor(hdc, pgauge->rgbBkColor);
934
935 ExtTextOut(hdc, (dx - LOWORD(dwExtent)) / 2 + Offset,
936 (dy - HIWORD(dwExtent)) / 2 + Offset,
937 ETO_OPAQUE | ETO_CLIPPED, &rc1, ach, wGomerX, NULL);
938
939 /* unselect the font */
940 SelectObject(hdc, hFont);
941} /* gaugePaint() */
942
943
944/** LRESULT FAR PASCAL gaugeWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
945 *
946 * DESCRIPTION:
947 * This is the control's window procedure. Its purpose is to handle
948 * special messages for this custom control.
949 *
950 * The special control messages for the gauge control are:
951 *
952 * ZYZG_SETRANGE : Sets the range of the gauge. In other
953 * words, the number of parts that make a
954 * whole.
955 *
956 * ZYZG_GETRANGE : Returns the current range of the gauge.
957 *
958 * ZYZG_SETORIENTATION : Sets the orientation of the gauge. This
959 * can be one of the ZYZG_ORIENT_?? msgs.
960 *
961 * ZYZG_GETORIENTATION : Gets the current orientation of the
962 * gauge.
963 *
964 * ZYZG_SETPOSITION : Sets the current position of the gauge.
965 * In other words, how many pieces of the
966 * whole have been used.
967 *
968 * ZYZG_GETPOSITION : Gets the current position of the gauge.
969 *
970 * ZYZG_SETDELTAPOS : Sets the position of the gauge +/- the
971 * specified amount.
972 *
973 * ZYZG_SETFGCOLOR : Sets the foreground (percent done) color.
974 *
975 * ZYZG_GETFGCOLOR : Gets the foreground (percent done) color.
976 *
977 * ZYZG_SETBKCOLOR : Sets the background (percent not done)
978 * color.
979 *
980 * ZYZG_GETBKCOLOR : Gets the background (percent not done)
981 * color.
982 *
983 * WM_SETFONT : Sets the font to use for the percentage
984 * text of the gauge.
985 *
986 * WM_GETFONT : Gets the current font in use by the
987 * gauge.
988 *
989 * NOTES:
990 *
991 ** cjp */
992
993/* LRESULT FAR PASCAL */
994
995LRESULT APIENTRY _EXPORT gaugeWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
996{
997 HFONT hFont;
998 PAINTSTRUCT ps;
999 PZYZGAUGE pgauge;
1000 RECT rc;
1001
1002// pgauge = (PZYZGAUGE)GetWindowWord(hwnd, ZYZG_WW_PZYZGAUGE);
1003 pgauge = (PZYZGAUGE)GetWindowLong(hwnd, ZYZG_WW_PZYZGAUGE);
1004
1005 /* break to get DefWindowProc() */
1006 switch (uMsg)
1007 {
1008 case WM_CREATE:
1009 /* need to allocate a control block */
1010// pgauge = (PZYZGAUGE)LocalAlloc(LPTR, sizeof(ZYZGAUGE));
1011 pgauge = (PZYZGAUGE)malloc(sizeof(ZYZGAUGE));
1012 if (!pgauge)
1013 return (0L);
1014
1015 /* hang on to this control block */
1016// SetWindowWord(hwnd, ZYZG_WW_PZYZGAUGE, (WORD)pgauge);
1017 SetWindowLong(hwnd, ZYZG_WW_PZYZGAUGE, (LONG)pgauge);
1018
1019 /* fill control block with defaults */
1020 pgauge->wRange = ZYZG_DEF_RANGE;
1021 pgauge->wPosition = ZYZG_DEF_POSITION;
1022 pgauge->wOrientation = ZYZG_DEF_ORIENTATION;
1023 pgauge->wWidth3D = ZYZG_DEF_WIDTH3D;
1024 pgauge->wWidthBezelFace = ZYZG_DEF_BEZELFACE;
1025 pgauge->rgbTextColor = rgbDefTextColor;
1026 pgauge->rgbBkColor = rgbDefBkColor;
1027
1028 /* use system font */
1029 SendMessage(hwnd, WM_SETFONT, (WPARAM)NULL, 0L);
1030
1031 /* go to DefWindowProc() to finish the job */
1032 break;
1033
1034 case WM_DESTROY:
1035 /* get rid of the control's memory */
1036 if (pgauge)
1037// LocalFree((HANDLE)pgauge);
1038 free(pgauge);
1039 break;
1040
1041 case ZYZG_GETPOSITION:
1042 return (pgauge->wPosition);
1043
1044 case ZYZG_GETRANGE:
1045 return (pgauge->wRange);
1046
1047 case ZYZG_GETORIENTATION:
1048 return (pgauge->wOrientation);
1049
1050 case ZYZG_GETWIDTH3D:
1051 return (pgauge->wWidth3D);
1052
1053 case ZYZG_GETBEZELFACE:
1054 return (pgauge->wWidthBezelFace);
1055
1056 case ZYZG_GETBKCOLOR:
1057 return (pgauge->rgbTextColor);
1058
1059 case ZYZG_GETFGCOLOR:
1060 return (pgauge->rgbBkColor);
1061
1062 case ZYZG_SETBKCOLOR:
1063 pgauge->rgbBkColor = lParam;
1064 return (0L);
1065
1066 case ZYZG_SETFGCOLOR:
1067 pgauge->rgbTextColor = lParam;
1068 return (0L);
1069
1070
1071 case ZYZG_SETPOSITION:
1072 pgauge->wPosition = wParam;
1073
1074zyzgForceRepaint:
1075 GetClientRect(hwnd, &rc);
1076 if ((GetWindowLong(hwnd, GWL_STYLE) & ZYZGS_3D) && fSupport3D)
1077 {
1078 wParam = (2 * pgauge->wWidth3D) +
1079 pgauge->wWidthBezelFace + 2;
1080 }
1081
1082 else
1083 wParam = 1;
1084
93089472 1085 InflateRect(&rc, ~(wParam), ~(wParam));
2bda0e17
KB
1086 InvalidateRect(hwnd, &rc, FALSE);
1087 UpdateWindow(hwnd);
1088 return (0L);
1089
1090 case ZYZG_SETRANGE:
1091 pgauge->wRange = wParam;
1092 goto zyzgForceRepaint;
1093
1094 case ZYZG_SETORIENTATION:
1095 pgauge->wOrientation = wParam;
1096 goto zyzgForceRepaint;
1097
1098 case ZYZG_SETWIDTH3D:
1099 pgauge->wWidth3D = wParam;
1100
1101zyzgForceRepaint3D:
1102 InvalidateRect(hwnd, NULL, FALSE);
1103 UpdateWindow(hwnd);
1104 return (0L);
1105
1106 case ZYZG_SETBEZELFACE:
1107 pgauge->wWidthBezelFace = wParam;
1108 goto zyzgForceRepaint3D;
1109
1110 case ZYZG_SETDELTAPOS:
1111/* Watcom doesn't like the following line so removing typecasts */
1112/* (int)pgauge->wPosition += (int)wParam; */
1113 pgauge->wPosition += wParam;
1114 goto zyzgForceRepaint;
1115
1116 case WM_PAINT:
1117 BeginPaint(hwnd, &ps);
1118 gaugePaint(hwnd, ps.hdc);
1119 EndPaint(hwnd, &ps);
1120 return (0L);
1121
1122 case WM_GETFONT:
1123 hFont = pgauge->hFont;
1124
1125 /* if system font, then return NULL handle */
1126 return (long)((hFont == GetStockObject(SYSTEM_FONT)) ? NULL : hFont);
1127
1128 case WM_SETFONT:
1129 /* if NULL hFont, use system font */
1130 if (!(hFont = (HFONT)wParam))
c4e7c2aa 1131 hFont = (HFONT) GetStockObject(SYSTEM_FONT);
2bda0e17
KB
1132
1133 pgauge->hFont = hFont;
1134
1135 /* redraw if indicated in message */
1136 if ((BOOL)lParam)
1137 {
1138 InvalidateRect(hwnd, NULL, TRUE);
1139 UpdateWindow(hwnd);
1140 }
1141 return (0L);
1142 } /* switch () */
1143
1144 /* let the dialog mangler take care of this message */
1145 return (DefWindowProc(hwnd, uMsg, wParam, lParam));
1146} /* gaugeWndProc() */
1147
1148
1149/** EOF: zyzgauge.c **/
1150
47d67540 1151#endif // wxUSE_GAUGE