1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxGauge class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "gauge.h"
17 #include <wx/vms_x_fix.h>
20 # include "wx/gauge.h"
23 #pragma message disable nosimpint
28 #endif // __WXMOTIF20__
30 #pragma message enable nosimpint
32 #include "wx/motif/private.h"
34 IMPLEMENT_DYNAMIC_CLASS(wxGauge
, wxControl
)
36 #if !wxCHECK_MOTIF_VERSION( 2, 0 ) || wxCHECK_LESSTIF()
38 // XmGauge copyright notice:
41 * Copyright 1994 GROUPE BULL
43 * Permission to use, copy, modify, and distribute this software and its
44 * documentation for any purpose and without fee is hereby granted, provided
45 * that the above copyright notice appear in all copies and that both that
46 * copyright notice and this permission notice appear in supporting
47 * documentation, and that the name of GROUPE BULL not be used in advertising
48 * or publicity pertaining to distribution of the software without specific,
49 * written prior permission. GROUPE BULL makes no representations about the
50 * suitability of this software for any purpose. It is provided "as is"
51 * without express or implied warranty.
53 * GROUPE BULL disclaims all warranties with regard to this software,
54 * including all implied warranties of merchantability and fitness,
55 * in no event shall GROUPE BULL be liable for any special,
56 * indirect or consequential damages or any damages
57 * whatsoever resulting from loss of use, data or profits,
58 * whether in an action of contract, negligence or other tortious
59 * action, arising out of or in connection with the use
60 * or performance of this software.
64 //// PUBLIC XMGAUGE DECLARATIONS
65 typedef struct _XmGaugeClassRec
* XmGaugeWidgetClass
;
66 typedef struct _XmGaugeRec
* XmGaugeWidget
;
69 extern "C" WidgetClass xmGaugeWidgetClass
;
71 extern WidgetClass xmGaugeWidgetClass
;
74 typedef struct _XmGaugeCallbackStruct
{
78 } XmGaugeCallbackStruct
;
82 XmGaugeSetValue(Widget w
, int value
);
85 XmGaugeGetValue(Widget w
);
87 #endif // !wxCHECK_MOTIF_VERSION( 2, 0 ) || wxCHECK_LESSTIF()
89 bool wxGauge::Create(wxWindow
*parent
, wxWindowID id
,
94 const wxValidator
& validator
,
97 if( !CreateControl( parent
, id
, pos
, size
, style
, validator
, name
) )
100 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
104 if (style
& wxGA_HORIZONTAL
)
106 XtSetArg (args
[0], XmNorientation
, XmHORIZONTAL
);
107 XtSetArg (args
[1], XmNprocessingDirection
, XmMAX_ON_RIGHT
);
111 XtSetArg (args
[0], XmNorientation
, XmVERTICAL
);
112 XtSetArg (args
[1], XmNprocessingDirection
, XmMAX_ON_TOP
);
114 XtSetArg(args
[2], XmNminimum
, 0);
115 XtSetArg(args
[3], XmNmaximum
, range
);
116 #if wxCHECK_MOTIF_VERSION( 2, 0 ) && !wxCHECK_LESSTIF()
117 XtSetArg(args
[4], XmNeditable
, False
); ++count
;
118 XtSetArg(args
[5], XmNslidingMode
, XmTHERMOMETER
); ++count
;
119 // XtSetArg(args[6], XmNsliderVisual, XmFOREGROUND_COLOR ); ++count;
121 XtCreateManagedWidget("gauge", xmScaleWidgetClass
,
122 parentWidget
, args
, count
);
125 XtCreateManagedWidget("gauge", xmGaugeWidgetClass
,
126 parentWidget
, args
, count
);
128 m_mainWidget
= (WXWidget
) gaugeWidget
;
130 XtManageChild (gaugeWidget
);
132 int x
= pos
.x
; int y
= pos
.y
;
133 wxSize best
= GetBestSize();
134 if( size
.x
!= -1 ) best
.x
= size
.x
;
135 if( size
.y
!= -1 ) best
.y
= size
.y
;
139 SetCanAddEventHandler(TRUE
);
140 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
, x
, y
,
143 ChangeBackgroundColour();
148 wxSize
wxGauge::DoGetBestSize() const
150 if( HasFlag(wxGA_HORIZONTAL
) )
151 return wxSize( 100, 18 );
153 return wxSize( 18, 100 );
156 void wxGauge::SetShadowWidth(int w
)
160 XtVaSetValues((Widget
) m_mainWidget
, XmNshadowThickness
, w
, NULL
);
163 void wxGauge::SetRange(int r
)
165 XtVaSetValues((Widget
) m_mainWidget
, XmNmaximum
, r
, NULL
);
168 void wxGauge::SetValue(int pos
)
170 XtVaSetValues((Widget
) m_mainWidget
, XmNvalue
, pos
, NULL
);
173 int wxGauge::GetShadowWidth() const
176 XtVaGetValues((Widget
) m_mainWidget
, XmNshadowThickness
, &w
, NULL
);
180 int wxGauge::GetRange() const
183 XtVaGetValues((Widget
) m_mainWidget
, XmNmaximum
, &r
, NULL
);
187 int wxGauge::GetValue() const
190 XtVaGetValues((Widget
) m_mainWidget
, XmNvalue
, &pos
, NULL
);
194 void wxGauge::DoMoveWindow(int x
, int y
, int width
, int height
)
196 wxGaugeBase::DoMoveWindow( x
, y
, width
, height
);
198 XtVaSetValues( (Widget
)m_mainWidget
,
199 XmNscaleHeight
, height
,
204 #if !wxCHECK_MOTIF_VERSION( 2, 0 ) || wxCHECK_LESSTIF()
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
WXUNUSED(req
), Widget new_w
, ArgList
WXUNUSED(args
), Cardinal
*WXUNUSED(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
);
474 ArgList
WXUNUSED(args
),
475 Cardinal
*WXUNUSED(num_args
) )
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
*WXUNUSED(event
), Region
WXUNUSED(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
WXUNUSED(w
), XEvent
*WXUNUSED(e
), String
*WXUNUSED(args
), Cardinal
*WXUNUSED(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
WXUNUSED(w
), XEvent
*WXUNUSED(e
), String
*WXUNUSED(args
), Cardinal
*WXUNUSED(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
WXUNUSED(w
), XEvent
*WXUNUSED(e
), String
*WXUNUSED(args
), Cardinal
*WXUNUSED(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
;
778 #endif // !wxCHECK_MOTIF_VERSION( 2, 0 ) || wxCHECK_LESSTIF()