1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxGauge class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "gauge.h"
19 #pragma message disable nosimpint
23 #pragma message enable nosimpint
25 #include "wx/motif/private.h"
27 #if !USE_SHARED_LIBRARY
28 IMPLEMENT_DYNAMIC_CLASS(wxGauge
, wxControl
)
31 // XmGauge copyright notice:
34 * Copyright 1994 GROUPE BULL
36 * Permission to use, copy, modify, and distribute this software and its
37 * documentation for any purpose and without fee is hereby granted, provided
38 * that the above copyright notice appear in all copies and that both that
39 * copyright notice and this permission notice appear in supporting
40 * documentation, and that the name of GROUPE BULL not be used in advertising
41 * or publicity pertaining to distribution of the software without specific,
42 * written prior permission. GROUPE BULL makes no representations about the
43 * suitability of this software for any purpose. It is provided "as is"
44 * without express or implied warranty.
46 * GROUPE BULL disclaims all warranties with regard to this software,
47 * including all implied warranties of merchantability and fitness,
48 * in no event shall GROUPE BULL be liable for any special,
49 * indirect or consequential damages or any damages
50 * whatsoever resulting from loss of use, data or profits,
51 * whether in an action of contract, negligence or other tortious
52 * action, arising out of or in connection with the use
53 * or performance of this software.
57 //// PUBLIC XMGAUGE DECLARATIONS
58 typedef struct _XmGaugeClassRec
* XmGaugeWidgetClass
;
59 typedef struct _XmGaugeRec
* XmGaugeWidget
;
62 extern "C" WidgetClass xmGaugeWidgetClass
;
64 extern WidgetClass xmGaugeWidgetClass
;
67 typedef struct _XmGaugeCallbackStruct
{
71 } XmGaugeCallbackStruct
;
75 XmGaugeSetValue(Widget w
, int value
);
78 XmGaugeGetValue(Widget w
);
82 bool wxGauge::Create(wxWindow
*parent
, wxWindowID id
,
87 const wxValidator
& validator
,
91 SetValidator(validator
);
93 m_windowStyle
= style
;
94 m_backgroundColour
= parent
->GetBackgroundColour();
95 m_foregroundColour
= parent
->GetForegroundColour();
97 if (parent
) parent
->AddChild(this);
100 m_windowId
= (int)NewControlId();
104 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
108 if (style
& wxHORIZONTAL
)
110 XtSetArg (args
[0], XmNorientation
, XmHORIZONTAL
);
111 XtSetArg (args
[1], XmNprocessingDirection
, XmMAX_ON_RIGHT
);
115 XtSetArg (args
[0], XmNorientation
, XmVERTICAL
);
116 XtSetArg (args
[1], XmNprocessingDirection
, XmMAX_ON_TOP
);
118 XtSetArg(args
[2], XmNminimum
, 0);
119 XtSetArg(args
[3], XmNmaximum
, range
);
120 Widget gaugeWidget
= XtCreateManagedWidget("gauge", xmGaugeWidgetClass
, parentWidget
, args
, count
);
121 m_mainWidget
= (WXWidget
) gaugeWidget
;
123 XtManageChild (gaugeWidget
);
125 int x
= pos
.x
; int y
= pos
.y
;
126 int width
= size
.x
; int height
= size
.y
;
132 m_font
= parent
->GetFont();
135 SetCanAddEventHandler(TRUE
);
136 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
, x
, y
, width
, height
);
138 ChangeBackgroundColour();
143 void wxGauge::SetShadowWidth(int w
)
147 XtVaSetValues((Widget
) m_mainWidget
, XmNshadowThickness
, w
, NULL
);
150 void wxGauge::SetBezelFace(int WXUNUSED(w
))
154 void wxGauge::SetRange(int r
)
157 XtVaSetValues((Widget
) m_mainWidget
, XmNmaximum
, r
, NULL
);
160 void wxGauge::SetValue(int pos
)
163 XtVaSetValues((Widget
) m_mainWidget
, XmNvalue
, pos
, NULL
);
166 int wxGauge::GetShadowWidth() const
169 XtVaGetValues((Widget
) m_mainWidget
, XmNshadowThickness
, &w
, NULL
);
173 int wxGauge::GetBezelFace() const
178 int wxGauge::GetRange() const
181 XtVaGetValues((Widget
) m_mainWidget
, XmNmaximum
, &r
, NULL
);
183 // return m_rangeMax;
186 int wxGauge::GetValue() const
189 XtVaGetValues((Widget
) m_mainWidget
, XmNvalue
, &pos
, NULL
);
191 // return m_gaugePos;
194 void wxGauge::ChangeFont(bool keepOriginalSize
)
196 wxWindow::ChangeFont(keepOriginalSize
);
199 void wxGauge::ChangeBackgroundColour()
201 wxWindow::ChangeBackgroundColour();
204 void wxGauge::ChangeForegroundColour()
206 wxWindow::ChangeForegroundColour();
209 //// PRIVATE DECLARATIONS FOR XMGAUGE
211 #include <Xm/PrimitiveP.h>
212 #include <Xm/DrawP.h>
218 typedef struct _XmGaugeClassRec
{
219 CoreClassPart core_class
;
220 XmPrimitiveClassPart primitive_class
;
221 XmGaugeClassPart gauge_class
;
225 typedef struct _XmGaugePart
{
229 unsigned char orientation
;
230 unsigned char processingDirection
;
232 XtCallbackList dragCallback
;
233 XtCallbackList valueChangedCallback
;
236 Boolean dragging
; /* drag in progress ? */
242 typedef struct _XmGaugeRec
{
244 XmPrimitivePart primitive
;
248 extern XmGaugeClassRec xmGaugeClassRec
;
250 /* Copyright 1994 GROUPE BULL -- See license conditions in file COPYRIGHT */
252 //// XMGAUGE IMPLEMENTATION
255 GaugePick(Widget w
, XEvent
*e
, String
*args
, Cardinal
*num_args
);
257 GaugeDrag(Widget w
, XEvent
*e
, String
*args
, Cardinal
*num_args
);
259 GaugeDrop(Widget w
, XEvent
*e
, String
*args
, Cardinal
*num_args
);
263 static char translations
[] =
264 "<Btn1Down>: GaugePick()\n\
265 <Btn1Motion>: GaugeDrag()\n\
266 <Btn1Up>: GaugeDrop()\n\
271 static XtActionsRec actions
[] = {
272 {"GaugePick", GaugePick
},
273 {"GaugeDrag", GaugeDrag
},
274 {"GaugeDrop", GaugeDrop
},
278 DrawSlider(XmGaugeWidget gw
, Boolean clear
)
280 #define THIS gw->gauge
286 unsigned long backgr,foregr;
290 sht
= gw
->primitive
.shadow_thickness
;
292 ratio
= (float)THIS
.value
/
293 (float)(THIS
.maximum
- THIS
.minimum
);
295 sprintf(string,"%-d%%",(int)(ratio*100));
297 XtVaGetValues(gw,XmNbackground,&backgr,XmNforeground,&foregr,NULL);
301 XClearArea(XtDisplay(gw
), XtWindow(gw
), sht
, sht
,
302 gw
->core
.width
- 2 * sht
, gw
->core
.height
- 2 * sht
, False
);
304 switch(THIS
.orientation
) {
306 size
= (int) ((gw
->core
.width
- 2 * sht
)*ratio
);
308 XDrawString(XtDisplay(gw), XtWindow(gw), THIS.gc, sht+gw->core.width/2,
309 gw->core.height - 2 * sht, string, len);
311 switch(THIS
.processingDirection
) {
313 case XmMAX_ON_BOTTOM
:
314 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
315 sht
, sht
, size
, gw
->core
.height
- 2 * sht
);
318 rects[0].x = sht; rects[0].y = sht;
319 rects[0].width = size; rects[0].height = gw->core.height - 2 * sht;
324 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
325 gw
->core
.width
- size
- sht
, sht
,
326 size
, gw
->core
.height
- 2 * sht
);
329 rects[0].x = gw->core.width - size - sht; rects[0].y = sht;
330 rects[0].width = size; rects[0].height = gw->core.height - 2 * sht;
335 XSetClipRectangles(XtDisplay(gw), THIS.gc, 0, 0, rects, 1, Unsorted);
336 XSetForeground(XtDisplay(gw), THIS.gc, backgr);
337 XDrawString(XtDisplay(gw), XtWindow(gw), THIS.gc, sht+gw->core.width/2,
338 gw->core.height - 2 * sht, string, len);
343 size
= (int) ((gw
->core
.height
- 2 * sht
)*ratio
);
345 XDrawString(XtDisplay(gw), XtWindow(gw), THIS.gc, sht,
346 sht+gw->core.height/2, string,len);
348 switch(THIS
.processingDirection
) {
350 case XmMAX_ON_BOTTOM
:
351 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
352 sht
, sht
, gw
->core
.width
- 2 * sht
, size
);
355 rects[0].x = sht; rects[0].y = sht;
356 rects[0].width = gw->core.width - 2 * sht; rects[0].height = size;
361 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
362 sht
, gw
->core
.height
- size
- sht
,
363 gw
->core
.width
- 2 * sht
, size
);
366 rects[0].x = sht; rects[0].y = gw->core.height - size - sht;
367 rects[0].width = gw->core.width - 2 * sht; rects[0].height = size;
371 XSetClipRectangles(XtDisplay(gw), THIS.gc, 0, 0, rects, 1, Unsorted);
372 XSetForeground(XtDisplay(gw), THIS.gc, backgr);
373 XDrawString(XtDisplay(gw), XtWindow(gw), THIS.gc, sht,
374 sht+gw->core.height/2, string,len);
379 XSetClipMask(XtDisplay(gw), THIS.gc, None);
380 XSetForeground(XtDisplay(gw), THIS.gc, foregr);
389 DrawSlider(XmGaugeWidget gw
, Boolean clear
)
391 #define THIS gw->gauge
395 sht
= gw
->primitive
.shadow_thickness
;
396 /* See fix comment below: can cause divide by zero error.
397 ratio = (float)((float)THIS.maximum -
398 (float)THIS.minimum) / (float)THIS.value;
401 XClearArea(XtDisplay(gw
), XtWindow(gw
), sht
, sht
,
402 gw
->core
.width
- 2 * sht
, gw
->core
.height
- 2 * sht
, False
);
404 switch(THIS
.orientation
) {
406 /* size = (gw->core.width - 2 * sht) / ratio; */
407 /* A fix suggested by Dmitri Chubraev */
408 size
= (gw
->core
.width
- 2 * sht
) /((float)THIS
.maximum
-(float)THIS
.minimum
)*(float)THIS
.value
;
409 switch(THIS
.processingDirection
) {
411 case XmMAX_ON_BOTTOM
:
412 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
413 sht
, sht
, size
, gw
->core
.height
- 2 * sht
);
417 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
418 gw
->core
.width
- size
- sht
, sht
,
419 size
, gw
->core
.height
- 2 * sht
);
424 size
= (gw
->core
.height
- 2 * sht
) /((float)THIS
.maximum
-(float)THIS
.minimum
)*(float)THIS
.value
;
425 /* size = (gw->core.height - 2 * sht)/ ratio; */
426 switch(THIS
.processingDirection
) {
428 case XmMAX_ON_BOTTOM
:
429 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
430 sht
, sht
, gw
->core
.width
- 2 * sht
, size
);
434 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
435 sht
, gw
->core
.height
- size
- sht
,
436 gw
->core
.width
- 2 * sht
, size
);
445 Initialize(Widget
WXUNUSED(req
), Widget new_w
, ArgList
WXUNUSED(args
), Cardinal
*WXUNUSED(num_args
))
447 XmGaugeWidget gw
= (XmGaugeWidget
)new_w
;
448 #define THIS gw->gauge
451 values
.foreground
= gw
->primitive
.foreground
;
452 THIS
.gc
= XtGetGC(new_w
, GCForeground
, &values
);
463 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
464 #define THIS gw->gauge
465 XtReleaseGC(w
, THIS
.gc
);
477 ArgList
WXUNUSED(args
),
478 Cardinal
*WXUNUSED(num_args
) )
480 XmGaugeWidget cgw
= (XmGaugeWidget
)cw
;
481 XmGaugeWidget ngw
= (XmGaugeWidget
)nw
;
483 Boolean redraw
= False
;
484 if(cgw
->primitive
.foreground
!= ngw
->primitive
.foreground
) {
488 XtReleaseGC(nw
, ngw
->gauge
.gc
);
489 values
.foreground
= ngw
->primitive
.foreground
;
490 ngw
->gauge
.gc
= XtGetGC(nw
, GCForeground
, &values
);
492 if(cgw
->gauge
.value
!= ngw
->gauge
.value
) {
502 ExposeProc(Widget w
, XEvent
*WXUNUSED(event
), Region
WXUNUSED(r
))
504 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
505 #define THIS gw->gauge
508 sht
= gw
->primitive
.shadow_thickness
;
509 _XmDrawShadows(XtDisplay(w
), XtWindow(w
),
510 gw
->primitive
.top_shadow_GC
,
511 gw
->primitive
.bottom_shadow_GC
,
512 0, 0, w
->core
.width
, w
->core
.height
,
514 DrawSlider(gw
, False
);
524 #define offset(field) XtOffset(XmGaugeWidget, gauge.field)
525 {XmNvalue
, XmCValue
, XtRInt
, sizeof(int),
526 offset(value
), XtRImmediate
, (caddr_t
)10},
528 {XmNminimum
, XmCValue
, XtRInt
, sizeof(int),
529 offset(minimum
), XtRImmediate
, (caddr_t
)0},
531 {XmNmaximum
, XmCValue
, XtRInt
, sizeof(int),
532 offset(maximum
), XtRImmediate
, (caddr_t
)100},
534 {XmNorientation
, XmCOrientation
, XmROrientation
, sizeof(unsigned char),
535 offset(orientation
), XtRImmediate
, (caddr_t
)XmVERTICAL
},
537 {XmNprocessingDirection
, XmCProcessingDirection
,
538 XmRProcessingDirection
, sizeof(unsigned char),
539 offset(processingDirection
), XtRImmediate
, (caddr_t
)XmMAX_ON_RIGHT
},
541 {XmNdragCallback
, XmCCallback
, XmRCallback
, sizeof(XtCallbackList
),
542 offset(dragCallback
), XtRImmediate
, (caddr_t
)NULL
},
544 {XmNvalueChangedCallback
, XmCCallback
, XmRCallback
, sizeof(XtCallbackList
),
545 offset(valueChangedCallback
), XtRImmediate
, (caddr_t
)NULL
},
552 XmGaugeClassRec xmGaugeClassRec
= {
554 (WidgetClass
) &xmPrimitiveClassRec
, /* superclass */
555 "XmGauge", /* class_name */
556 sizeof(XmGaugeRec
), /* widget_size */
557 NULL
, /* class_initialize */
558 NULL
, /* class_part_initialize */
559 FALSE
, /* class_inited */
560 Initialize
, /* initialize */
561 NULL
, /* initialize_hook */
562 XtInheritRealize
, /* realize */
563 actions
, /* actions */
564 XtNumber(actions
), /* num_actions */
565 resources
, /* resources */
566 XtNumber(resources
), /* num_resources */
567 NULLQUARK
, /* xrm_class */
568 TRUE
, /* compress_motion */
569 TRUE
, /* compress_exposure */
570 TRUE
, /* compress_enterleave */
571 FALSE
, /* visible_interest */
572 Destroy
, /* destroy */
574 ExposeProc
, /* expose */
575 SetValues
, /* set_values */
576 NULL
, /* set_values_hook */
577 XtInheritSetValuesAlmost
, /* set_values_almost */
578 NULL
, /* get_values_hook */
579 NULL
, /* accept_focus */
580 XtVersion
, /* version */
581 NULL
, /* callback_private */
582 translations
, /* tm_table */
583 NULL
, /* query_geometry */
584 NULL
, /* display_accelerator */
587 /* primitive_class fields */
589 NULL
, /* border_highlight */
590 NULL
, /* border_unhighlight */
591 NULL
, /* translations */
592 NULL
, /* arm_and_activate */
593 NULL
, /* syn_resources */
594 0, /* num_syn_resources */
602 WidgetClass xmGaugeWidgetClass
= (WidgetClass
)&xmGaugeClassRec
;
608 GaugePick(Widget
WXUNUSED(w
), XEvent
*WXUNUSED(e
), String
*WXUNUSED(args
), Cardinal
*WXUNUSED(num_args
))
610 /* Commented out for a read-only gauge in wxWindows */
612 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
613 #define THIS gw->gauge
616 Boolean dragging
= False
;
617 XButtonEvent
*event
= (XButtonEvent
*)e
;
622 sht
= gw
->primitive
.shadow_thickness
;
623 _XmDrawShadows(XtDisplay(w
), XtWindow(w
),
624 gw
->primitive
.top_shadow_GC
,
625 gw
->primitive
.bottom_shadow_GC
,
626 0, 0, w
->core
.width
, w
->core
.height
,
630 ratio
= (float)((float)THIS
.maximum
-
631 (float)THIS
.minimum
) / (float)THIS
.value
;
632 switch(THIS
.orientation
) {
634 size
= (w
->core
.width
- 2 * sht
) / ratio
;
635 switch(THIS
.processingDirection
) {
637 case XmMAX_ON_BOTTOM
:
638 dragging
= (x
> sht
) && (y
> sht
) &&
639 (x
< sht
+ size
) && (y
< w
->core
.height
- sht
);
643 dragging
= (x
> w
->core
.width
- size
- sht
) && (y
> sht
) &&
644 (x
< w
->core
.width
- sht
) && (y
< w
->core
.height
+ sht
);
649 size
= (w
->core
.height
- 2 * sht
) / ratio
;
650 switch(THIS
.processingDirection
) {
652 case XmMAX_ON_BOTTOM
:
653 dragging
= (x
> sht
) && (y
> sht
) &&
654 (x
< w
->core
.width
- sht
) &&
655 (y
< w
->core
.width
- 2 * sht
+ size
);
659 dragging
= (x
> sht
) && (y
> w
->core
.height
- size
- sht
) &&
660 (x
< w
->core
.width
- sht
) && (y
< w
->core
.height
- sht
);
664 THIS
.dragging
= dragging
;
671 #define round(x) ( (x) > 0 ? ((x) + 0.5) : -(-(x) + 0.5) )
674 GaugeDrag(Widget
WXUNUSED(w
), XEvent
*WXUNUSED(e
), String
*WXUNUSED(args
), Cardinal
*WXUNUSED(num_args
))
676 /* Commented out for a read-only gauge in wxWindows */
678 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
679 #define THIS gw->gauge
680 int sht
, x
, y
, max
, value
;
681 float ratio
, nratio
, size
, nsize
, fvalue
, delta
;
682 XMotionEvent
*event
= (XMotionEvent
*)e
;
684 if( ! THIS
.dragging
) return;
688 sht
= gw
->primitive
.shadow_thickness
;
690 ratio
= (float)THIS
.value
/ (float)((float)THIS
.maximum
-
691 (float)THIS
.minimum
);
692 switch(THIS
.orientation
) {
694 max
= (w
->core
.width
- 2 * sht
);
695 size
= (float)max
* ratio
;
696 delta
= (float)x
- (float)THIS
.oldx
;
699 max
= (w
->core
.height
- 2 * sht
);
700 size
= (float) max
* ratio
;
701 delta
= (float)y
- (float)THIS
.oldy
;
704 switch(THIS
.processingDirection
) {
706 case XmMAX_ON_BOTTOM
:
707 nsize
= size
+ delta
;
710 nsize
= size
- delta
;
712 if(nsize
> (float)max
) nsize
= (float)max
;
713 if(nsize
< (float)0 ) nsize
= (float)0;
714 nratio
= nsize
/ (float)max
;
716 fvalue
= (int)((float)THIS
.maximum
-
717 (float)THIS
.minimum
) * (float)nsize
/ (float)max
;
718 value
= round(fvalue
);
724 /* clear old slider only if it was larger */
725 DrawSlider(gw
, (nsize
< size
));
728 XmGaugeCallbackStruct call
;
730 if(NULL
!= THIS
.dragCallback
) {
731 call
.reason
= XmCR_DRAG
;
733 call
.value
= THIS
.value
;
734 XtCallCallbacks(w
, XmNdragCallback
, &call
);
743 GaugeDrop(Widget
WXUNUSED(w
), XEvent
*WXUNUSED(e
), String
*WXUNUSED(args
), Cardinal
*WXUNUSED(num_args
))
745 /* Commented out for a read-only gauge in wxWindows */
747 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
748 #define THIS gw->gauge
749 if( ! THIS
.dragging
) return;
751 if(NULL
!= THIS
.valueChangedCallback
) {
752 XmGaugeCallbackStruct call
;
753 call
.reason
= XmCR_VALUE_CHANGED
;
755 call
.value
= THIS
.value
;
756 XtCallCallbacks(w
, XmNvalueChangedCallback
, &call
);
758 THIS
.dragging
= False
;
764 XmGaugeSetValue(Widget w
, int value
)
766 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
768 gw
->gauge
.value
= value
;
769 DrawSlider(gw
, True
);
770 XFlush(XtDisplay(w
));
774 XmGaugeGetValue(Widget w
)
776 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
778 return gw
->gauge
.value
;