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 #include <wx/motif/private.h>
21 #if !USE_SHARED_LIBRARY
22 IMPLEMENT_DYNAMIC_CLASS(wxGauge
, wxControl
)
25 // XmGauge copyright notice:
28 * Copyright 1994 GROUPE BULL
30 * Permission to use, copy, modify, and distribute this software and its
31 * documentation for any purpose and without fee is hereby granted, provided
32 * that the above copyright notice appear in all copies and that both that
33 * copyright notice and this permission notice appear in supporting
34 * documentation, and that the name of GROUPE BULL not be used in advertising
35 * or publicity pertaining to distribution of the software without specific,
36 * written prior permission. GROUPE BULL makes no representations about the
37 * suitability of this software for any purpose. It is provided "as is"
38 * without express or implied warranty.
40 * GROUPE BULL disclaims all warranties with regard to this software,
41 * including all implied warranties of merchantability and fitness,
42 * in no event shall GROUPE BULL be liable for any special,
43 * indirect or consequential damages or any damages
44 * whatsoever resulting from loss of use, data or profits,
45 * whether in an action of contract, negligence or other tortious
46 * action, arising out of or in connection with the use
47 * or performance of this software.
51 //// PUBLIC XMGAUGE DECLARATIONS
52 typedef struct _XmGaugeClassRec
* XmGaugeWidgetClass
;
53 typedef struct _XmGaugeRec
* XmGaugeWidget
;
56 extern "C" WidgetClass xmGaugeWidgetClass
;
58 extern WidgetClass xmGaugeWidgetClass
;
61 typedef struct _XmGaugeCallbackStruct
{
65 } XmGaugeCallbackStruct
;
69 XmGaugeSetValue(Widget w
, int value
);
72 XmGaugeGetValue(Widget w
);
76 bool wxGauge::Create(wxWindow
*parent
, wxWindowID id
,
81 const wxValidator
& validator
,
85 SetValidator(validator
);
87 m_windowStyle
= style
;
88 m_backgroundColour
= parent
->GetBackgroundColour();
89 m_foregroundColour
= parent
->GetForegroundColour();
91 if (parent
) parent
->AddChild(this);
94 m_windowId
= (int)NewControlId();
98 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
102 if (style
& wxHORIZONTAL
)
104 XtSetArg (args
[0], XmNorientation
, XmHORIZONTAL
);
105 XtSetArg (args
[1], XmNprocessingDirection
, XmMAX_ON_RIGHT
);
109 XtSetArg (args
[0], XmNorientation
, XmVERTICAL
);
110 XtSetArg (args
[1], XmNprocessingDirection
, XmMAX_ON_TOP
);
112 XtSetArg(args
[2], XmNminimum
, 0);
113 XtSetArg(args
[3], XmNmaximum
, range
);
114 Widget gaugeWidget
= XtCreateManagedWidget("gauge", xmGaugeWidgetClass
, parentWidget
, args
, count
);
115 m_mainWidget
= (WXWidget
) gaugeWidget
;
117 XtManageChild (gaugeWidget
);
119 int x
= pos
.x
; int y
= pos
.y
;
120 int width
= size
.x
; int height
= size
.y
;
126 SetCanAddEventHandler(TRUE
);
127 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
, x
, y
, width
, height
);
129 SetFont(* parent
->GetFont());
130 ChangeBackgroundColour();
135 void wxGauge::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
137 wxControl::SetSize(x
, y
, width
, height
, sizeFlags
);
140 void wxGauge::SetShadowWidth(int w
)
144 XtVaSetValues((Widget
) m_mainWidget
, XmNshadowThickness
, w
, NULL
);
147 void wxGauge::SetBezelFace(int w
)
151 void wxGauge::SetRange(int r
)
154 XtVaSetValues((Widget
) m_mainWidget
, XmNmaximum
, r
, NULL
);
157 void wxGauge::SetValue(int pos
)
160 XtVaSetValues((Widget
) m_mainWidget
, XmNvalue
, pos
, NULL
);
163 int wxGauge::GetShadowWidth() const
166 XtVaGetValues((Widget
) m_mainWidget
, XmNshadowThickness
, &w
, NULL
);
170 int wxGauge::GetBezelFace() const
175 int wxGauge::GetRange() const
178 XtVaGetValues((Widget
) m_mainWidget
, XmNmaximum
, &r
, NULL
);
180 // return m_rangeMax;
183 int wxGauge::GetValue() const
186 XtVaGetValues((Widget
) m_mainWidget
, XmNvalue
, &pos
, NULL
);
188 // return m_gaugePos;
191 void wxGauge::ChangeFont()
196 void wxGauge::ChangeBackgroundColour()
201 void wxGauge::ChangeForegroundColour()
206 //// PRIVATE DECLARATIONS FOR XMGAUGE
208 #include <Xm/PrimitiveP.h>
209 #include <Xm/DrawP.h>
215 typedef struct _XmGaugeClassRec
{
216 CoreClassPart core_class
;
217 XmPrimitiveClassPart primitive_class
;
218 XmGaugeClassPart gauge_class
;
222 typedef struct _XmGaugePart
{
226 unsigned char orientation
;
227 unsigned char processingDirection
;
229 XtCallbackList dragCallback
;
230 XtCallbackList valueChangedCallback
;
233 Boolean dragging
; /* drag in progress ? */
239 typedef struct _XmGaugeRec
{
241 XmPrimitivePart primitive
;
245 extern XmGaugeClassRec xmGaugeClassRec
;
247 /* Copyright 1994 GROUPE BULL -- See license conditions in file COPYRIGHT */
249 //// XMGAUGE IMPLEMENTATION
252 GaugePick(Widget w
, XEvent
*e
, String
*args
, Cardinal
*num_args
);
254 GaugeDrag(Widget w
, XEvent
*e
, String
*args
, Cardinal
*num_args
);
256 GaugeDrop(Widget w
, XEvent
*e
, String
*args
, Cardinal
*num_args
);
260 static char translations
[] =
261 "<Btn1Down>: GaugePick()\n\
262 <Btn1Motion>: GaugeDrag()\n\
263 <Btn1Up>: GaugeDrop()\n\
268 static XtActionsRec actions
[] = {
269 {"GaugePick", GaugePick
},
270 {"GaugeDrag", GaugeDrag
},
271 {"GaugeDrop", GaugeDrop
},
275 DrawSlider(XmGaugeWidget gw
, Boolean clear
)
277 #define THIS gw->gauge
283 unsigned long backgr,foregr;
287 sht
= gw
->primitive
.shadow_thickness
;
289 ratio
= (float)THIS
.value
/
290 (float)(THIS
.maximum
- THIS
.minimum
);
292 sprintf(string,"%-d%%",(int)(ratio*100));
294 XtVaGetValues(gw,XmNbackground,&backgr,XmNforeground,&foregr,NULL);
298 XClearArea(XtDisplay(gw
), XtWindow(gw
), sht
, sht
,
299 gw
->core
.width
- 2 * sht
, gw
->core
.height
- 2 * sht
, False
);
301 switch(THIS
.orientation
) {
303 size
= (int) ((gw
->core
.width
- 2 * sht
)*ratio
);
305 XDrawString(XtDisplay(gw), XtWindow(gw), THIS.gc, sht+gw->core.width/2,
306 gw->core.height - 2 * sht, string, len);
308 switch(THIS
.processingDirection
) {
310 case XmMAX_ON_BOTTOM
:
311 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
312 sht
, sht
, size
, gw
->core
.height
- 2 * sht
);
315 rects[0].x = sht; rects[0].y = sht;
316 rects[0].width = size; rects[0].height = gw->core.height - 2 * sht;
321 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
322 gw
->core
.width
- size
- sht
, sht
,
323 size
, gw
->core
.height
- 2 * sht
);
326 rects[0].x = gw->core.width - size - sht; rects[0].y = sht;
327 rects[0].width = size; rects[0].height = gw->core.height - 2 * sht;
332 XSetClipRectangles(XtDisplay(gw), THIS.gc, 0, 0, rects, 1, Unsorted);
333 XSetForeground(XtDisplay(gw), THIS.gc, backgr);
334 XDrawString(XtDisplay(gw), XtWindow(gw), THIS.gc, sht+gw->core.width/2,
335 gw->core.height - 2 * sht, string, len);
340 size
= (int) ((gw
->core
.height
- 2 * sht
)*ratio
);
342 XDrawString(XtDisplay(gw), XtWindow(gw), THIS.gc, sht,
343 sht+gw->core.height/2, string,len);
345 switch(THIS
.processingDirection
) {
347 case XmMAX_ON_BOTTOM
:
348 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
349 sht
, sht
, gw
->core
.width
- 2 * sht
, size
);
352 rects[0].x = sht; rects[0].y = sht;
353 rects[0].width = gw->core.width - 2 * sht; rects[0].height = size;
358 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
359 sht
, gw
->core
.height
- size
- sht
,
360 gw
->core
.width
- 2 * sht
, size
);
363 rects[0].x = sht; rects[0].y = gw->core.height - size - sht;
364 rects[0].width = gw->core.width - 2 * sht; rects[0].height = size;
368 XSetClipRectangles(XtDisplay(gw), THIS.gc, 0, 0, rects, 1, Unsorted);
369 XSetForeground(XtDisplay(gw), THIS.gc, backgr);
370 XDrawString(XtDisplay(gw), XtWindow(gw), THIS.gc, sht,
371 sht+gw->core.height/2, string,len);
376 XSetClipMask(XtDisplay(gw), THIS.gc, None);
377 XSetForeground(XtDisplay(gw), THIS.gc, foregr);
386 DrawSlider(XmGaugeWidget gw
, Boolean clear
)
388 #define THIS gw->gauge
392 sht
= gw
->primitive
.shadow_thickness
;
393 /* See fix comment below: can cause divide by zero error.
394 ratio = (float)((float)THIS.maximum -
395 (float)THIS.minimum) / (float)THIS.value;
398 XClearArea(XtDisplay(gw
), XtWindow(gw
), sht
, sht
,
399 gw
->core
.width
- 2 * sht
, gw
->core
.height
- 2 * sht
, False
);
401 switch(THIS
.orientation
) {
403 /* size = (gw->core.width - 2 * sht) / ratio; */
404 /* A fix suggested by Dmitri Chubraev */
405 size
= (gw
->core
.width
- 2 * sht
) /((float)THIS
.maximum
-(float)THIS
.minimum
)*(float)THIS
.value
;
406 switch(THIS
.processingDirection
) {
408 case XmMAX_ON_BOTTOM
:
409 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
410 sht
, sht
, size
, gw
->core
.height
- 2 * sht
);
414 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
415 gw
->core
.width
- size
- sht
, sht
,
416 size
, gw
->core
.height
- 2 * sht
);
421 size
= (gw
->core
.height
- 2 * sht
) /((float)THIS
.maximum
-(float)THIS
.minimum
)*(float)THIS
.value
;
422 /* size = (gw->core.height - 2 * sht)/ ratio; */
423 switch(THIS
.processingDirection
) {
425 case XmMAX_ON_BOTTOM
:
426 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
427 sht
, sht
, gw
->core
.width
- 2 * sht
, size
);
431 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
432 sht
, gw
->core
.height
- size
- sht
,
433 gw
->core
.width
- 2 * sht
, size
);
442 Initialize(Widget req
, Widget new_w
, ArgList args
, Cardinal
*num_args
)
444 XmGaugeWidget gw
= (XmGaugeWidget
)new_w
;
445 #define THIS gw->gauge
448 values
.foreground
= gw
->primitive
.foreground
;
449 THIS
.gc
= XtGetGC(new_w
, GCForeground
, &values
);
460 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
461 #define THIS gw->gauge
462 XtReleaseGC(w
, THIS
.gc
);
477 XmGaugeWidget cgw
= (XmGaugeWidget
)cw
;
478 XmGaugeWidget ngw
= (XmGaugeWidget
)nw
;
480 Boolean redraw
= False
;
481 if(cgw
->primitive
.foreground
!= ngw
->primitive
.foreground
) {
485 XtReleaseGC(nw
, ngw
->gauge
.gc
);
486 values
.foreground
= ngw
->primitive
.foreground
;
487 ngw
->gauge
.gc
= XtGetGC(nw
, GCForeground
, &values
);
489 if(cgw
->gauge
.value
!= ngw
->gauge
.value
) {
499 ExposeProc(Widget w
, XEvent
*event
, Region r
)
501 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
502 #define THIS gw->gauge
505 sht
= gw
->primitive
.shadow_thickness
;
506 _XmDrawShadows(XtDisplay(w
), XtWindow(w
),
507 gw
->primitive
.top_shadow_GC
,
508 gw
->primitive
.bottom_shadow_GC
,
509 0, 0, w
->core
.width
, w
->core
.height
,
511 DrawSlider(gw
, False
);
521 #define offset(field) XtOffset(XmGaugeWidget, gauge.field)
522 {XmNvalue
, XmCValue
, XtRInt
, sizeof(int),
523 offset(value
), XtRImmediate
, (caddr_t
)10},
525 {XmNminimum
, XmCValue
, XtRInt
, sizeof(int),
526 offset(minimum
), XtRImmediate
, (caddr_t
)0},
528 {XmNmaximum
, XmCValue
, XtRInt
, sizeof(int),
529 offset(maximum
), XtRImmediate
, (caddr_t
)100},
531 {XmNorientation
, XmCOrientation
, XmROrientation
, sizeof(unsigned char),
532 offset(orientation
), XtRImmediate
, (caddr_t
)XmVERTICAL
},
534 {XmNprocessingDirection
, XmCProcessingDirection
,
535 XmRProcessingDirection
, sizeof(unsigned char),
536 offset(processingDirection
), XtRImmediate
, (caddr_t
)XmMAX_ON_RIGHT
},
538 {XmNdragCallback
, XmCCallback
, XmRCallback
, sizeof(XtCallbackList
),
539 offset(dragCallback
), XtRImmediate
, (caddr_t
)NULL
},
541 {XmNvalueChangedCallback
, XmCCallback
, XmRCallback
, sizeof(XtCallbackList
),
542 offset(valueChangedCallback
), XtRImmediate
, (caddr_t
)NULL
},
549 XmGaugeClassRec xmGaugeClassRec
= {
551 (WidgetClass
) &xmPrimitiveClassRec
, /* superclass */
552 "XmGauge", /* class_name */
553 sizeof(XmGaugeRec
), /* widget_size */
554 NULL
, /* class_initialize */
555 NULL
, /* class_part_initialize */
556 FALSE
, /* class_inited */
557 Initialize
, /* initialize */
558 NULL
, /* initialize_hook */
559 XtInheritRealize
, /* realize */
560 actions
, /* actions */
561 XtNumber(actions
), /* num_actions */
562 resources
, /* resources */
563 XtNumber(resources
), /* num_resources */
564 NULLQUARK
, /* xrm_class */
565 TRUE
, /* compress_motion */
566 TRUE
, /* compress_exposure */
567 TRUE
, /* compress_enterleave */
568 FALSE
, /* visible_interest */
569 Destroy
, /* destroy */
571 ExposeProc
, /* expose */
572 SetValues
, /* set_values */
573 NULL
, /* set_values_hook */
574 XtInheritSetValuesAlmost
, /* set_values_almost */
575 NULL
, /* get_values_hook */
576 NULL
, /* accept_focus */
577 XtVersion
, /* version */
578 NULL
, /* callback_private */
579 translations
, /* tm_table */
580 NULL
, /* query_geometry */
581 NULL
, /* display_accelerator */
584 /* primitive_class fields */
586 NULL
, /* border_highlight */
587 NULL
, /* border_unhighlight */
588 NULL
, /* translations */
589 NULL
, /* arm_and_activate */
590 NULL
, /* syn_resources */
591 0, /* num_syn_resources */
599 WidgetClass xmGaugeWidgetClass
= (WidgetClass
)&xmGaugeClassRec
;
605 GaugePick(Widget w
, XEvent
*e
, String
*args
, Cardinal
*num_args
)
607 /* Commented out for a read-only gauge in wxWindows */
609 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
610 #define THIS gw->gauge
613 Boolean dragging
= False
;
614 XButtonEvent
*event
= (XButtonEvent
*)e
;
619 sht
= gw
->primitive
.shadow_thickness
;
620 _XmDrawShadows(XtDisplay(w
), XtWindow(w
),
621 gw
->primitive
.top_shadow_GC
,
622 gw
->primitive
.bottom_shadow_GC
,
623 0, 0, w
->core
.width
, w
->core
.height
,
627 ratio
= (float)((float)THIS
.maximum
-
628 (float)THIS
.minimum
) / (float)THIS
.value
;
629 switch(THIS
.orientation
) {
631 size
= (w
->core
.width
- 2 * sht
) / ratio
;
632 switch(THIS
.processingDirection
) {
634 case XmMAX_ON_BOTTOM
:
635 dragging
= (x
> sht
) && (y
> sht
) &&
636 (x
< sht
+ size
) && (y
< w
->core
.height
- sht
);
640 dragging
= (x
> w
->core
.width
- size
- sht
) && (y
> sht
) &&
641 (x
< w
->core
.width
- sht
) && (y
< w
->core
.height
+ sht
);
646 size
= (w
->core
.height
- 2 * sht
) / ratio
;
647 switch(THIS
.processingDirection
) {
649 case XmMAX_ON_BOTTOM
:
650 dragging
= (x
> sht
) && (y
> sht
) &&
651 (x
< w
->core
.width
- sht
) &&
652 (y
< w
->core
.width
- 2 * sht
+ size
);
656 dragging
= (x
> sht
) && (y
> w
->core
.height
- size
- sht
) &&
657 (x
< w
->core
.width
- sht
) && (y
< w
->core
.height
- sht
);
661 THIS
.dragging
= dragging
;
668 #define round(x) ( (x) > 0 ? ((x) + 0.5) : -(-(x) + 0.5) )
671 GaugeDrag(Widget w
, XEvent
*e
, String
*args
, Cardinal
*num_args
)
673 /* Commented out for a read-only gauge in wxWindows */
675 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
676 #define THIS gw->gauge
677 int sht
, x
, y
, max
, value
;
678 float ratio
, nratio
, size
, nsize
, fvalue
, delta
;
679 XMotionEvent
*event
= (XMotionEvent
*)e
;
681 if( ! THIS
.dragging
) return;
685 sht
= gw
->primitive
.shadow_thickness
;
687 ratio
= (float)THIS
.value
/ (float)((float)THIS
.maximum
-
688 (float)THIS
.minimum
);
689 switch(THIS
.orientation
) {
691 max
= (w
->core
.width
- 2 * sht
);
692 size
= (float)max
* ratio
;
693 delta
= (float)x
- (float)THIS
.oldx
;
696 max
= (w
->core
.height
- 2 * sht
);
697 size
= (float) max
* ratio
;
698 delta
= (float)y
- (float)THIS
.oldy
;
701 switch(THIS
.processingDirection
) {
703 case XmMAX_ON_BOTTOM
:
704 nsize
= size
+ delta
;
707 nsize
= size
- delta
;
709 if(nsize
> (float)max
) nsize
= (float)max
;
710 if(nsize
< (float)0 ) nsize
= (float)0;
711 nratio
= nsize
/ (float)max
;
713 fvalue
= (int)((float)THIS
.maximum
-
714 (float)THIS
.minimum
) * (float)nsize
/ (float)max
;
715 value
= round(fvalue
);
721 /* clear old slider only if it was larger */
722 DrawSlider(gw
, (nsize
< size
));
725 XmGaugeCallbackStruct call
;
727 if(NULL
!= THIS
.dragCallback
) {
728 call
.reason
= XmCR_DRAG
;
730 call
.value
= THIS
.value
;
731 XtCallCallbacks(w
, XmNdragCallback
, &call
);
740 GaugeDrop(Widget w
, XEvent
*e
, String
*args
, Cardinal
*num_args
)
742 /* Commented out for a read-only gauge in wxWindows */
744 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
745 #define THIS gw->gauge
746 if( ! THIS
.dragging
) return;
748 if(NULL
!= THIS
.valueChangedCallback
) {
749 XmGaugeCallbackStruct call
;
750 call
.reason
= XmCR_VALUE_CHANGED
;
752 call
.value
= THIS
.value
;
753 XtCallCallbacks(w
, XmNvalueChangedCallback
, &call
);
755 THIS
.dragging
= False
;
761 XmGaugeSetValue(Widget w
, int value
)
763 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
765 gw
->gauge
.value
= value
;
766 DrawSlider(gw
, True
);
767 XFlush(XtDisplay(w
));
771 XmGaugeGetValue(Widget w
)
773 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
775 return gw
->gauge
.value
;