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