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