1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/gauge.cpp
3 // Purpose: wxGauge class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
16 #include "wx/vms_x_fix.h"
36 #pragma message disable nosimpint
41 #endif // __WXMOTIF20__
43 #pragma message enable nosimpint
45 #include "wx/motif/private.h"
47 IMPLEMENT_DYNAMIC_CLASS(wxGauge
, wxControl
)
49 #if !wxCHECK_MOTIF_VERSION( 2, 0 ) || wxCHECK_LESSTIF()
51 // XmGauge copyright notice:
54 * Copyright 1994 GROUPE BULL
56 * Permission to use, copy, modify, and distribute this software and its
57 * documentation for any purpose and without fee is hereby granted, provided
58 * that the above copyright notice appear in all copies and that both that
59 * copyright notice and this permission notice appear in supporting
60 * documentation, and that the name of GROUPE BULL not be used in advertising
61 * or publicity pertaining to distribution of the software without specific,
62 * written prior permission. GROUPE BULL makes no representations about the
63 * suitability of this software for any purpose. It is provided "as is"
64 * without express or implied warranty.
66 * GROUPE BULL disclaims all warranties with regard to this software,
67 * including all implied warranties of merchantability and fitness,
68 * in no event shall GROUPE BULL be liable for any special,
69 * indirect or consequential damages or any damages
70 * whatsoever resulting from loss of use, data or profits,
71 * whether in an action of contract, negligence or other tortious
72 * action, arising out of or in connection with the use
73 * or performance of this software.
77 //// PUBLIC XMGAUGE DECLARATIONS
78 typedef struct _XmGaugeClassRec
* XmGaugeWidgetClass
;
79 typedef struct _XmGaugeRec
* XmGaugeWidget
;
82 extern "C" WidgetClass xmGaugeWidgetClass
;
84 extern WidgetClass xmGaugeWidgetClass
;
87 typedef struct _XmGaugeCallbackStruct
{
91 } XmGaugeCallbackStruct
;
95 XmGaugeSetValue(Widget w
, int value
);
98 XmGaugeGetValue(Widget w
);
100 #endif // !wxCHECK_MOTIF_VERSION( 2, 0 ) || wxCHECK_LESSTIF()
102 bool wxGauge::Create(wxWindow
*parent
, wxWindowID id
,
107 const wxValidator
& validator
,
108 const wxString
& name
)
110 if( !CreateControl( parent
, id
, pos
, size
, style
, validator
, name
) )
113 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
117 if (style
& wxGA_HORIZONTAL
)
119 XtSetArg (args
[0], XmNorientation
, XmHORIZONTAL
);
120 XtSetArg (args
[1], XmNprocessingDirection
, XmMAX_ON_RIGHT
);
124 XtSetArg (args
[0], XmNorientation
, XmVERTICAL
);
125 XtSetArg (args
[1], XmNprocessingDirection
, XmMAX_ON_TOP
);
127 XtSetArg(args
[2], XmNminimum
, 0);
128 XtSetArg(args
[3], XmNmaximum
, range
);
129 #if wxCHECK_MOTIF_VERSION( 2, 0 ) && !wxCHECK_LESSTIF()
130 XtSetArg(args
[4], XmNeditable
, False
); ++count
;
131 XtSetArg(args
[5], XmNslidingMode
, XmTHERMOMETER
); ++count
;
132 // XtSetArg(args[6], XmNsliderVisual, XmFOREGROUND_COLOR ); ++count;
134 XtCreateManagedWidget("gauge", xmScaleWidgetClass
,
135 parentWidget
, args
, count
);
138 XtCreateManagedWidget("gauge", xmGaugeWidgetClass
,
139 parentWidget
, args
, count
);
141 m_mainWidget
= (WXWidget
) gaugeWidget
;
143 XtManageChild (gaugeWidget
);
145 int x
= pos
.x
; int y
= pos
.y
;
146 wxSize best
= GetBestSize();
147 if( size
.x
!= wxDefaultCoord
) best
.x
= size
.x
;
148 if( size
.y
!= wxDefaultCoord
) best
.y
= size
.y
;
152 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
, x
, y
,
155 ChangeBackgroundColour();
160 wxSize
wxGauge::DoGetBestSize() const
162 if( HasFlag(wxGA_HORIZONTAL
) )
163 return wxSize( 100, 18 );
165 return wxSize( 18, 100 );
168 void wxGauge::SetShadowWidth(int w
)
172 XtVaSetValues((Widget
) m_mainWidget
, XmNshadowThickness
, w
, NULL
);
175 void wxGauge::SetRange(int r
)
177 XtVaSetValues((Widget
) m_mainWidget
, XmNmaximum
, r
, NULL
);
180 void wxGauge::SetValue(int pos
)
182 XtVaSetValues((Widget
) m_mainWidget
, XmNvalue
, pos
, NULL
);
185 int wxGauge::GetShadowWidth() const
188 XtVaGetValues((Widget
) m_mainWidget
, XmNshadowThickness
, &w
, NULL
);
192 int wxGauge::GetRange() const
195 XtVaGetValues((Widget
) m_mainWidget
, XmNmaximum
, &r
, NULL
);
199 int wxGauge::GetValue() const
202 XtVaGetValues((Widget
) m_mainWidget
, XmNvalue
, &pos
, NULL
);
206 void wxGauge::DoMoveWindow(int x
, int y
, int width
, int height
)
208 wxGaugeBase::DoMoveWindow( x
, y
, width
, height
);
210 XtVaSetValues( (Widget
)m_mainWidget
,
211 XmNscaleHeight
, height
,
216 #if !wxCHECK_MOTIF_VERSION( 2, 0 ) || wxCHECK_LESSTIF()
218 //// PRIVATE DECLARATIONS FOR XMGAUGE
220 #include <Xm/PrimitiveP.h>
221 #include <Xm/DrawP.h>
227 typedef struct _XmGaugeClassRec
{
228 CoreClassPart core_class
;
229 XmPrimitiveClassPart primitive_class
;
230 XmGaugeClassPart gauge_class
;
234 typedef struct _XmGaugePart
{
238 unsigned char orientation
;
239 unsigned char processingDirection
;
241 XtCallbackList dragCallback
;
242 XtCallbackList valueChangedCallback
;
245 Boolean dragging
; /* drag in progress ? */
251 typedef struct _XmGaugeRec
{
253 XmPrimitivePart primitive
;
257 extern XmGaugeClassRec xmGaugeClassRec
;
259 /* Copyright 1994 GROUPE BULL -- See license conditions in file COPYRIGHT */
261 //// XMGAUGE IMPLEMENTATION
264 GaugePick(Widget w
, XEvent
*e
, String
*args
, Cardinal
*num_args
);
266 GaugeDrag(Widget w
, XEvent
*e
, String
*args
, Cardinal
*num_args
);
268 GaugeDrop(Widget w
, XEvent
*e
, String
*args
, Cardinal
*num_args
);
272 static char translations
[] =
273 "<Btn1Down>: GaugePick()\n\
274 <Btn1Motion>: GaugeDrag()\n\
275 <Btn1Up>: GaugeDrop()\n\
280 static XtActionsRec actions
[] = {
281 {"GaugePick", GaugePick
},
282 {"GaugeDrag", GaugeDrag
},
283 {"GaugeDrop", GaugeDrop
},
287 DrawSlider(XmGaugeWidget gw
, Boolean clear
)
289 #define THIS gw->gauge
295 unsigned long backgr,foregr;
299 sht
= gw
->primitive
.shadow_thickness
;
301 ratio
= (float)THIS
.value
/
302 (float)(THIS
.maximum
- THIS
.minimum
);
304 sprintf(string,"%-d%%",(int)(ratio*100));
306 XtVaGetValues(gw,XmNbackground,&backgr,XmNforeground,&foregr,NULL);
310 XClearArea(XtDisplay(gw
), XtWindow(gw
), sht
, sht
,
311 gw
->core
.width
- 2 * sht
, gw
->core
.height
- 2 * sht
, False
);
313 switch(THIS
.orientation
) {
315 size
= (int) ((gw
->core
.width
- 2 * sht
)*ratio
);
317 XDrawString(XtDisplay(gw), XtWindow(gw), THIS.gc, sht+gw->core.width/2,
318 gw->core.height - 2 * sht, string, len);
320 switch(THIS
.processingDirection
) {
322 case XmMAX_ON_BOTTOM
:
323 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
324 sht
, sht
, size
, gw
->core
.height
- 2 * sht
);
327 rects[0].x = sht; rects[0].y = sht;
328 rects[0].width = size; rects[0].height = gw->core.height - 2 * sht;
333 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
334 gw
->core
.width
- size
- sht
, sht
,
335 size
, gw
->core
.height
- 2 * sht
);
338 rects[0].x = gw->core.width - size - sht; rects[0].y = sht;
339 rects[0].width = size; rects[0].height = gw->core.height - 2 * sht;
344 XSetClipRectangles(XtDisplay(gw), THIS.gc, 0, 0, rects, 1, Unsorted);
345 XSetForeground(XtDisplay(gw), THIS.gc, backgr);
346 XDrawString(XtDisplay(gw), XtWindow(gw), THIS.gc, sht+gw->core.width/2,
347 gw->core.height - 2 * sht, string, len);
352 size
= (int) ((gw
->core
.height
- 2 * sht
)*ratio
);
354 XDrawString(XtDisplay(gw), XtWindow(gw), THIS.gc, sht,
355 sht+gw->core.height/2, string,len);
357 switch(THIS
.processingDirection
) {
359 case XmMAX_ON_BOTTOM
:
360 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
361 sht
, sht
, gw
->core
.width
- 2 * sht
, size
);
364 rects[0].x = sht; rects[0].y = sht;
365 rects[0].width = gw->core.width - 2 * sht; rects[0].height = size;
370 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
371 sht
, gw
->core
.height
- size
- sht
,
372 gw
->core
.width
- 2 * sht
, size
);
375 rects[0].x = sht; rects[0].y = gw->core.height - size - sht;
376 rects[0].width = gw->core.width - 2 * sht; rects[0].height = size;
380 XSetClipRectangles(XtDisplay(gw), THIS.gc, 0, 0, rects, 1, Unsorted);
381 XSetForeground(XtDisplay(gw), THIS.gc, backgr);
382 XDrawString(XtDisplay(gw), XtWindow(gw), THIS.gc, sht,
383 sht+gw->core.height/2, string,len);
388 XSetClipMask(XtDisplay(gw), THIS.gc, None);
389 XSetForeground(XtDisplay(gw), THIS.gc, foregr);
398 DrawSlider(XmGaugeWidget gw
, Boolean clear
)
400 #define THIS gw->gauge
404 sht
= gw
->primitive
.shadow_thickness
;
405 /* See fix comment below: can cause divide by zero error.
406 ratio = (float)((float)THIS.maximum -
407 (float)THIS.minimum) / (float)THIS.value;
410 XClearArea(XtDisplay(gw
), XtWindow(gw
), sht
, sht
,
411 gw
->core
.width
- 2 * sht
, gw
->core
.height
- 2 * sht
, False
);
413 switch(THIS
.orientation
) {
415 /* size = (gw->core.width - 2 * sht) / ratio; */
416 /* A fix suggested by Dmitri Chubraev */
417 size
= (gw
->core
.width
- 2 * sht
) /((float)THIS
.maximum
-(float)THIS
.minimum
)*(float)THIS
.value
;
418 switch(THIS
.processingDirection
) {
420 case XmMAX_ON_BOTTOM
:
421 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
422 sht
, sht
, size
, gw
->core
.height
- 2 * sht
);
426 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
427 gw
->core
.width
- size
- sht
, sht
,
428 size
, gw
->core
.height
- 2 * sht
);
433 size
= (gw
->core
.height
- 2 * sht
) /((float)THIS
.maximum
-(float)THIS
.minimum
)*(float)THIS
.value
;
434 /* size = (gw->core.height - 2 * sht)/ ratio; */
435 switch(THIS
.processingDirection
) {
437 case XmMAX_ON_BOTTOM
:
438 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
439 sht
, sht
, gw
->core
.width
- 2 * sht
, size
);
443 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
444 sht
, gw
->core
.height
- size
- sht
,
445 gw
->core
.width
- 2 * sht
, size
);
454 Initialize(Widget
WXUNUSED(req
), Widget new_w
, ArgList
WXUNUSED(args
), Cardinal
*WXUNUSED(num_args
))
456 XmGaugeWidget gw
= (XmGaugeWidget
)new_w
;
457 #define THIS gw->gauge
460 values
.foreground
= gw
->primitive
.foreground
;
461 THIS
.gc
= XtGetGC(new_w
, GCForeground
, &values
);
472 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
473 #define THIS gw->gauge
474 XtReleaseGC(w
, THIS
.gc
);
486 ArgList
WXUNUSED(args
),
487 Cardinal
*WXUNUSED(num_args
) )
489 XmGaugeWidget cgw
= (XmGaugeWidget
)cw
;
490 XmGaugeWidget ngw
= (XmGaugeWidget
)nw
;
492 Boolean redraw
= False
;
493 if(cgw
->primitive
.foreground
!= ngw
->primitive
.foreground
) {
497 XtReleaseGC(nw
, ngw
->gauge
.gc
);
498 values
.foreground
= ngw
->primitive
.foreground
;
499 ngw
->gauge
.gc
= XtGetGC(nw
, GCForeground
, &values
);
501 if(cgw
->gauge
.value
!= ngw
->gauge
.value
) {
511 ExposeProc(Widget w
, XEvent
*WXUNUSED(event
), Region
WXUNUSED(r
))
513 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
514 #define THIS gw->gauge
517 sht
= gw
->primitive
.shadow_thickness
;
518 _XmDrawShadows(XtDisplay(w
), XtWindow(w
),
519 gw
->primitive
.top_shadow_GC
,
520 gw
->primitive
.bottom_shadow_GC
,
521 0, 0, w
->core
.width
, w
->core
.height
,
522 (Dimension
)sht
, XmSHADOW_IN
);
523 DrawSlider(gw
, False
);
533 #define offset(field) XtOffset(XmGaugeWidget, gauge.field)
534 {XmNvalue
, XmCValue
, XtRInt
, sizeof(int),
535 offset(value
), XtRImmediate
, (caddr_t
)10},
537 {XmNminimum
, XmCValue
, XtRInt
, sizeof(int),
538 offset(minimum
), XtRImmediate
, (caddr_t
)0},
540 {XmNmaximum
, XmCValue
, XtRInt
, sizeof(int),
541 offset(maximum
), XtRImmediate
, (caddr_t
)100},
543 {XmNorientation
, XmCOrientation
, XmROrientation
, sizeof(unsigned char),
544 offset(orientation
), XtRImmediate
, (caddr_t
)XmVERTICAL
},
546 {XmNprocessingDirection
, XmCProcessingDirection
,
547 XmRProcessingDirection
, sizeof(unsigned char),
548 offset(processingDirection
), XtRImmediate
, (caddr_t
)XmMAX_ON_RIGHT
},
550 {XmNdragCallback
, XmCCallback
, XmRCallback
, sizeof(XtCallbackList
),
551 offset(dragCallback
), XtRImmediate
, (caddr_t
)NULL
},
553 {XmNvalueChangedCallback
, XmCCallback
, XmRCallback
, sizeof(XtCallbackList
),
554 offset(valueChangedCallback
), XtRImmediate
, (caddr_t
)NULL
},
561 XmGaugeClassRec xmGaugeClassRec
= {
563 (WidgetClass
) &xmPrimitiveClassRec
, /* superclass */
564 "XmGauge", /* class_name */
565 sizeof(XmGaugeRec
), /* widget_size */
566 NULL
, /* class_initialize */
567 NULL
, /* class_part_initialize */
568 False
, /* class_inited */
569 Initialize
, /* initialize */
570 NULL
, /* initialize_hook */
571 XtInheritRealize
, /* realize */
572 actions
, /* actions */
573 XtNumber(actions
), /* num_actions */
574 resources
, /* resources */
575 XtNumber(resources
), /* num_resources */
576 NULLQUARK
, /* xrm_class */
577 True
, /* compress_motion */
578 True
, /* compress_exposure */
579 True
, /* compress_enterleave */
580 False
, /* visible_interest */
581 Destroy
, /* destroy */
583 ExposeProc
, /* expose */
584 SetValues
, /* set_values */
585 NULL
, /* set_values_hook */
586 XtInheritSetValuesAlmost
, /* set_values_almost */
587 NULL
, /* get_values_hook */
588 NULL
, /* accept_focus */
589 XtVersion
, /* version */
590 NULL
, /* callback_private */
591 translations
, /* tm_table */
592 NULL
, /* query_geometry */
593 NULL
, /* display_accelerator */
596 /* primitive_class fields */
598 NULL
, /* border_highlight */
599 NULL
, /* border_unhighlight */
600 NULL
, /* translations */
601 NULL
, /* arm_and_activate */
602 NULL
, /* syn_resources */
603 0, /* num_syn_resources */
611 WidgetClass xmGaugeWidgetClass
= (WidgetClass
)&xmGaugeClassRec
;
617 GaugePick(Widget
WXUNUSED(w
), XEvent
*WXUNUSED(e
), String
*WXUNUSED(args
), Cardinal
*WXUNUSED(num_args
))
619 /* Commented out for a read-only gauge in wxWidgets */
621 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
622 #define THIS gw->gauge
625 Boolean dragging
= False
;
626 XButtonEvent
*event
= (XButtonEvent
*)e
;
631 sht
= gw
->primitive
.shadow_thickness
;
632 _XmDrawShadows(XtDisplay(w
), XtWindow(w
),
633 gw
->primitive
.top_shadow_GC
,
634 gw
->primitive
.bottom_shadow_GC
,
635 0, 0, w
->core
.width
, w
->core
.height
,
639 ratio
= (float)((float)THIS
.maximum
-
640 (float)THIS
.minimum
) / (float)THIS
.value
;
641 switch(THIS
.orientation
) {
643 size
= (w
->core
.width
- 2 * sht
) / ratio
;
644 switch(THIS
.processingDirection
) {
646 case XmMAX_ON_BOTTOM
:
647 dragging
= (x
> sht
) && (y
> sht
) &&
648 (x
< sht
+ size
) && (y
< w
->core
.height
- sht
);
652 dragging
= (x
> w
->core
.width
- size
- sht
) && (y
> sht
) &&
653 (x
< w
->core
.width
- sht
) && (y
< w
->core
.height
+ sht
);
658 size
= (w
->core
.height
- 2 * sht
) / ratio
;
659 switch(THIS
.processingDirection
) {
661 case XmMAX_ON_BOTTOM
:
662 dragging
= (x
> sht
) && (y
> sht
) &&
663 (x
< w
->core
.width
- sht
) &&
664 (y
< w
->core
.width
- 2 * sht
+ size
);
668 dragging
= (x
> sht
) && (y
> w
->core
.height
- size
- sht
) &&
669 (x
< w
->core
.width
- sht
) && (y
< w
->core
.height
- sht
);
673 THIS
.dragging
= dragging
;
681 GaugeDrag(Widget
WXUNUSED(w
), XEvent
*WXUNUSED(e
), String
*WXUNUSED(args
), Cardinal
*WXUNUSED(num_args
))
683 /* Commented out for a read-only gauge in wxWidgets */
685 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
686 #define THIS gw->gauge
687 int sht
, x
, y
, max
, value
;
688 float ratio
, nratio
, size
, nsize
, fvalue
, delta
;
689 XMotionEvent
*event
= (XMotionEvent
*)e
;
691 if( ! THIS
.dragging
) return;
695 sht
= gw
->primitive
.shadow_thickness
;
697 ratio
= (float)THIS
.value
/ (float)((float)THIS
.maximum
-
698 (float)THIS
.minimum
);
699 switch(THIS
.orientation
) {
701 max
= (w
->core
.width
- 2 * sht
);
702 size
= (float)max
* ratio
;
703 delta
= (float)x
- (float)THIS
.oldx
;
706 max
= (w
->core
.height
- 2 * sht
);
707 size
= (float) max
* ratio
;
708 delta
= (float)y
- (float)THIS
.oldy
;
711 switch(THIS
.processingDirection
) {
713 case XmMAX_ON_BOTTOM
:
714 nsize
= size
+ delta
;
717 nsize
= size
- delta
;
719 if(nsize
> (float)max
) nsize
= (float)max
;
720 if(nsize
< (float)0 ) nsize
= (float)0;
721 nratio
= nsize
/ (float)max
;
723 fvalue
= (int)((float)THIS
.maximum
-
724 (float)THIS
.minimum
) * (float)nsize
/ (float)max
;
725 value
= wxRound(fvalue
);
731 /* clear old slider only if it was larger */
732 DrawSlider(gw
, (nsize
< size
));
735 XmGaugeCallbackStruct call
;
737 if(NULL
!= THIS
.dragCallback
) {
738 call
.reason
= XmCR_DRAG
;
740 call
.value
= THIS
.value
;
741 XtCallCallbacks(w
, XmNdragCallback
, &call
);
750 GaugeDrop(Widget
WXUNUSED(w
), XEvent
*WXUNUSED(e
), String
*WXUNUSED(args
), Cardinal
*WXUNUSED(num_args
))
752 /* Commented out for a read-only gauge in wxWidgets */
754 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
755 #define THIS gw->gauge
756 if( ! THIS
.dragging
) return;
758 if(NULL
!= THIS
.valueChangedCallback
) {
759 XmGaugeCallbackStruct call
;
760 call
.reason
= XmCR_VALUE_CHANGED
;
762 call
.value
= THIS
.value
;
763 XtCallCallbacks(w
, XmNvalueChangedCallback
, &call
);
765 THIS
.dragging
= False
;
771 XmGaugeSetValue(Widget w
, int value
)
773 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
775 gw
->gauge
.value
= value
;
776 DrawSlider(gw
, True
);
777 XFlush(XtDisplay(w
));
781 XmGaugeGetValue(Widget w
)
783 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
785 return gw
->gauge
.value
;
788 #endif // !wxCHECK_MOTIF_VERSION( 2, 0 ) || wxCHECK_LESSTIF()