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