1 /////////////////////////////////////////////////////////////////////////////
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>
25 # include "wx/gauge.h"
28 #pragma message disable nosimpint
33 #endif // __WXMOTIF20__
35 #pragma message enable nosimpint
37 #include "wx/motif/private.h"
39 IMPLEMENT_DYNAMIC_CLASS(wxGauge
, wxControl
)
41 #if !wxCHECK_MOTIF_VERSION( 2, 0 ) || wxCHECK_LESSTIF()
43 // XmGauge copyright notice:
46 * Copyright 1994 GROUPE BULL
48 * Permission to use, copy, modify, and distribute this software and its
49 * documentation for any purpose and without fee is hereby granted, provided
50 * that the above copyright notice appear in all copies and that both that
51 * copyright notice and this permission notice appear in supporting
52 * documentation, and that the name of GROUPE BULL not be used in advertising
53 * or publicity pertaining to distribution of the software without specific,
54 * written prior permission. GROUPE BULL makes no representations about the
55 * suitability of this software for any purpose. It is provided "as is"
56 * without express or implied warranty.
58 * GROUPE BULL disclaims all warranties with regard to this software,
59 * including all implied warranties of merchantability and fitness,
60 * in no event shall GROUPE BULL be liable for any special,
61 * indirect or consequential damages or any damages
62 * whatsoever resulting from loss of use, data or profits,
63 * whether in an action of contract, negligence or other tortious
64 * action, arising out of or in connection with the use
65 * or performance of this software.
69 //// PUBLIC XMGAUGE DECLARATIONS
70 typedef struct _XmGaugeClassRec
* XmGaugeWidgetClass
;
71 typedef struct _XmGaugeRec
* XmGaugeWidget
;
74 extern "C" WidgetClass xmGaugeWidgetClass
;
76 extern WidgetClass xmGaugeWidgetClass
;
79 typedef struct _XmGaugeCallbackStruct
{
83 } XmGaugeCallbackStruct
;
87 XmGaugeSetValue(Widget w
, int value
);
90 XmGaugeGetValue(Widget w
);
92 #endif // !wxCHECK_MOTIF_VERSION( 2, 0 ) || wxCHECK_LESSTIF()
94 bool wxGauge::Create(wxWindow
*parent
, wxWindowID id
,
99 const wxValidator
& validator
,
100 const wxString
& name
)
102 if( !CreateControl( parent
, id
, pos
, size
, style
, validator
, name
) )
105 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
109 if (style
& wxGA_HORIZONTAL
)
111 XtSetArg (args
[0], XmNorientation
, XmHORIZONTAL
);
112 XtSetArg (args
[1], XmNprocessingDirection
, XmMAX_ON_RIGHT
);
116 XtSetArg (args
[0], XmNorientation
, XmVERTICAL
);
117 XtSetArg (args
[1], XmNprocessingDirection
, XmMAX_ON_TOP
);
119 XtSetArg(args
[2], XmNminimum
, 0);
120 XtSetArg(args
[3], XmNmaximum
, range
);
121 #if wxCHECK_MOTIF_VERSION( 2, 0 ) && !wxCHECK_LESSTIF()
122 XtSetArg(args
[4], XmNeditable
, False
); ++count
;
123 XtSetArg(args
[5], XmNslidingMode
, XmTHERMOMETER
); ++count
;
124 // XtSetArg(args[6], XmNsliderVisual, XmFOREGROUND_COLOR ); ++count;
126 XtCreateManagedWidget("gauge", xmScaleWidgetClass
,
127 parentWidget
, args
, count
);
130 XtCreateManagedWidget("gauge", xmGaugeWidgetClass
,
131 parentWidget
, args
, count
);
133 m_mainWidget
= (WXWidget
) gaugeWidget
;
135 XtManageChild (gaugeWidget
);
137 int x
= pos
.x
; int y
= pos
.y
;
138 wxSize best
= GetBestSize();
139 if( size
.x
!= -1 ) best
.x
= size
.x
;
140 if( size
.y
!= -1 ) best
.y
= size
.y
;
144 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
, x
, y
,
147 ChangeBackgroundColour();
152 wxSize
wxGauge::DoGetBestSize() const
154 if( HasFlag(wxGA_HORIZONTAL
) )
155 return wxSize( 100, 18 );
157 return wxSize( 18, 100 );
160 void wxGauge::SetShadowWidth(int w
)
164 XtVaSetValues((Widget
) m_mainWidget
, XmNshadowThickness
, w
, NULL
);
167 void wxGauge::SetRange(int r
)
169 XtVaSetValues((Widget
) m_mainWidget
, XmNmaximum
, r
, NULL
);
172 void wxGauge::SetValue(int pos
)
174 XtVaSetValues((Widget
) m_mainWidget
, XmNvalue
, pos
, NULL
);
177 int wxGauge::GetShadowWidth() const
180 XtVaGetValues((Widget
) m_mainWidget
, XmNshadowThickness
, &w
, NULL
);
184 int wxGauge::GetRange() const
187 XtVaGetValues((Widget
) m_mainWidget
, XmNmaximum
, &r
, NULL
);
191 int wxGauge::GetValue() const
194 XtVaGetValues((Widget
) m_mainWidget
, XmNvalue
, &pos
, NULL
);
198 void wxGauge::DoMoveWindow(int x
, int y
, int width
, int height
)
200 wxGaugeBase::DoMoveWindow( x
, y
, width
, height
);
202 XtVaSetValues( (Widget
)m_mainWidget
,
203 XmNscaleHeight
, height
,
208 #if !wxCHECK_MOTIF_VERSION( 2, 0 ) || wxCHECK_LESSTIF()
210 //// PRIVATE DECLARATIONS FOR XMGAUGE
212 #include <Xm/PrimitiveP.h>
213 #include <Xm/DrawP.h>
219 typedef struct _XmGaugeClassRec
{
220 CoreClassPart core_class
;
221 XmPrimitiveClassPart primitive_class
;
222 XmGaugeClassPart gauge_class
;
226 typedef struct _XmGaugePart
{
230 unsigned char orientation
;
231 unsigned char processingDirection
;
233 XtCallbackList dragCallback
;
234 XtCallbackList valueChangedCallback
;
237 Boolean dragging
; /* drag in progress ? */
243 typedef struct _XmGaugeRec
{
245 XmPrimitivePart primitive
;
249 extern XmGaugeClassRec xmGaugeClassRec
;
251 /* Copyright 1994 GROUPE BULL -- See license conditions in file COPYRIGHT */
253 //// XMGAUGE IMPLEMENTATION
256 GaugePick(Widget w
, XEvent
*e
, String
*args
, Cardinal
*num_args
);
258 GaugeDrag(Widget w
, XEvent
*e
, String
*args
, Cardinal
*num_args
);
260 GaugeDrop(Widget w
, XEvent
*e
, String
*args
, Cardinal
*num_args
);
264 static char translations
[] =
265 "<Btn1Down>: GaugePick()\n\
266 <Btn1Motion>: GaugeDrag()\n\
267 <Btn1Up>: GaugeDrop()\n\
272 static XtActionsRec actions
[] = {
273 {"GaugePick", GaugePick
},
274 {"GaugeDrag", GaugeDrag
},
275 {"GaugeDrop", GaugeDrop
},
279 DrawSlider(XmGaugeWidget gw
, Boolean clear
)
281 #define THIS gw->gauge
287 unsigned long backgr,foregr;
291 sht
= gw
->primitive
.shadow_thickness
;
293 ratio
= (float)THIS
.value
/
294 (float)(THIS
.maximum
- THIS
.minimum
);
296 sprintf(string,"%-d%%",(int)(ratio*100));
298 XtVaGetValues(gw,XmNbackground,&backgr,XmNforeground,&foregr,NULL);
302 XClearArea(XtDisplay(gw
), XtWindow(gw
), sht
, sht
,
303 gw
->core
.width
- 2 * sht
, gw
->core
.height
- 2 * sht
, False
);
305 switch(THIS
.orientation
) {
307 size
= (int) ((gw
->core
.width
- 2 * sht
)*ratio
);
309 XDrawString(XtDisplay(gw), XtWindow(gw), THIS.gc, sht+gw->core.width/2,
310 gw->core.height - 2 * sht, string, len);
312 switch(THIS
.processingDirection
) {
314 case XmMAX_ON_BOTTOM
:
315 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
316 sht
, sht
, size
, gw
->core
.height
- 2 * sht
);
319 rects[0].x = sht; rects[0].y = sht;
320 rects[0].width = size; rects[0].height = gw->core.height - 2 * sht;
325 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
326 gw
->core
.width
- size
- sht
, sht
,
327 size
, gw
->core
.height
- 2 * sht
);
330 rects[0].x = gw->core.width - size - sht; rects[0].y = sht;
331 rects[0].width = size; rects[0].height = gw->core.height - 2 * sht;
336 XSetClipRectangles(XtDisplay(gw), THIS.gc, 0, 0, rects, 1, Unsorted);
337 XSetForeground(XtDisplay(gw), THIS.gc, backgr);
338 XDrawString(XtDisplay(gw), XtWindow(gw), THIS.gc, sht+gw->core.width/2,
339 gw->core.height - 2 * sht, string, len);
344 size
= (int) ((gw
->core
.height
- 2 * sht
)*ratio
);
346 XDrawString(XtDisplay(gw), XtWindow(gw), THIS.gc, sht,
347 sht+gw->core.height/2, string,len);
349 switch(THIS
.processingDirection
) {
351 case XmMAX_ON_BOTTOM
:
352 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
353 sht
, sht
, gw
->core
.width
- 2 * sht
, size
);
356 rects[0].x = sht; rects[0].y = sht;
357 rects[0].width = gw->core.width - 2 * sht; rects[0].height = size;
362 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
363 sht
, gw
->core
.height
- size
- sht
,
364 gw
->core
.width
- 2 * sht
, size
);
367 rects[0].x = sht; rects[0].y = gw->core.height - size - sht;
368 rects[0].width = gw->core.width - 2 * sht; rects[0].height = size;
372 XSetClipRectangles(XtDisplay(gw), THIS.gc, 0, 0, rects, 1, Unsorted);
373 XSetForeground(XtDisplay(gw), THIS.gc, backgr);
374 XDrawString(XtDisplay(gw), XtWindow(gw), THIS.gc, sht,
375 sht+gw->core.height/2, string,len);
380 XSetClipMask(XtDisplay(gw), THIS.gc, None);
381 XSetForeground(XtDisplay(gw), THIS.gc, foregr);
390 DrawSlider(XmGaugeWidget gw
, Boolean clear
)
392 #define THIS gw->gauge
396 sht
= gw
->primitive
.shadow_thickness
;
397 /* See fix comment below: can cause divide by zero error.
398 ratio = (float)((float)THIS.maximum -
399 (float)THIS.minimum) / (float)THIS.value;
402 XClearArea(XtDisplay(gw
), XtWindow(gw
), sht
, sht
,
403 gw
->core
.width
- 2 * sht
, gw
->core
.height
- 2 * sht
, False
);
405 switch(THIS
.orientation
) {
407 /* size = (gw->core.width - 2 * sht) / ratio; */
408 /* A fix suggested by Dmitri Chubraev */
409 size
= (gw
->core
.width
- 2 * sht
) /((float)THIS
.maximum
-(float)THIS
.minimum
)*(float)THIS
.value
;
410 switch(THIS
.processingDirection
) {
412 case XmMAX_ON_BOTTOM
:
413 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
414 sht
, sht
, size
, gw
->core
.height
- 2 * sht
);
418 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
419 gw
->core
.width
- size
- sht
, sht
,
420 size
, gw
->core
.height
- 2 * sht
);
425 size
= (gw
->core
.height
- 2 * sht
) /((float)THIS
.maximum
-(float)THIS
.minimum
)*(float)THIS
.value
;
426 /* size = (gw->core.height - 2 * sht)/ ratio; */
427 switch(THIS
.processingDirection
) {
429 case XmMAX_ON_BOTTOM
:
430 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
431 sht
, sht
, gw
->core
.width
- 2 * sht
, size
);
435 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
436 sht
, gw
->core
.height
- size
- sht
,
437 gw
->core
.width
- 2 * sht
, size
);
446 Initialize(Widget
WXUNUSED(req
), Widget new_w
, ArgList
WXUNUSED(args
), Cardinal
*WXUNUSED(num_args
))
448 XmGaugeWidget gw
= (XmGaugeWidget
)new_w
;
449 #define THIS gw->gauge
452 values
.foreground
= gw
->primitive
.foreground
;
453 THIS
.gc
= XtGetGC(new_w
, GCForeground
, &values
);
464 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
465 #define THIS gw->gauge
466 XtReleaseGC(w
, THIS
.gc
);
478 ArgList
WXUNUSED(args
),
479 Cardinal
*WXUNUSED(num_args
) )
481 XmGaugeWidget cgw
= (XmGaugeWidget
)cw
;
482 XmGaugeWidget ngw
= (XmGaugeWidget
)nw
;
484 Boolean redraw
= False
;
485 if(cgw
->primitive
.foreground
!= ngw
->primitive
.foreground
) {
489 XtReleaseGC(nw
, ngw
->gauge
.gc
);
490 values
.foreground
= ngw
->primitive
.foreground
;
491 ngw
->gauge
.gc
= XtGetGC(nw
, GCForeground
, &values
);
493 if(cgw
->gauge
.value
!= ngw
->gauge
.value
) {
503 ExposeProc(Widget w
, XEvent
*WXUNUSED(event
), Region
WXUNUSED(r
))
505 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
506 #define THIS gw->gauge
509 sht
= gw
->primitive
.shadow_thickness
;
510 _XmDrawShadows(XtDisplay(w
), XtWindow(w
),
511 gw
->primitive
.top_shadow_GC
,
512 gw
->primitive
.bottom_shadow_GC
,
513 0, 0, w
->core
.width
, w
->core
.height
,
515 DrawSlider(gw
, False
);
525 #define offset(field) XtOffset(XmGaugeWidget, gauge.field)
526 {XmNvalue
, XmCValue
, XtRInt
, sizeof(int),
527 offset(value
), XtRImmediate
, (caddr_t
)10},
529 {XmNminimum
, XmCValue
, XtRInt
, sizeof(int),
530 offset(minimum
), XtRImmediate
, (caddr_t
)0},
532 {XmNmaximum
, XmCValue
, XtRInt
, sizeof(int),
533 offset(maximum
), XtRImmediate
, (caddr_t
)100},
535 {XmNorientation
, XmCOrientation
, XmROrientation
, sizeof(unsigned char),
536 offset(orientation
), XtRImmediate
, (caddr_t
)XmVERTICAL
},
538 {XmNprocessingDirection
, XmCProcessingDirection
,
539 XmRProcessingDirection
, sizeof(unsigned char),
540 offset(processingDirection
), XtRImmediate
, (caddr_t
)XmMAX_ON_RIGHT
},
542 {XmNdragCallback
, XmCCallback
, XmRCallback
, sizeof(XtCallbackList
),
543 offset(dragCallback
), XtRImmediate
, (caddr_t
)NULL
},
545 {XmNvalueChangedCallback
, XmCCallback
, XmRCallback
, sizeof(XtCallbackList
),
546 offset(valueChangedCallback
), XtRImmediate
, (caddr_t
)NULL
},
553 XmGaugeClassRec xmGaugeClassRec
= {
555 (WidgetClass
) &xmPrimitiveClassRec
, /* superclass */
556 "XmGauge", /* class_name */
557 sizeof(XmGaugeRec
), /* widget_size */
558 NULL
, /* class_initialize */
559 NULL
, /* class_part_initialize */
560 False
, /* class_inited */
561 Initialize
, /* initialize */
562 NULL
, /* initialize_hook */
563 XtInheritRealize
, /* realize */
564 actions
, /* actions */
565 XtNumber(actions
), /* num_actions */
566 resources
, /* resources */
567 XtNumber(resources
), /* num_resources */
568 NULLQUARK
, /* xrm_class */
569 True
, /* compress_motion */
570 True
, /* compress_exposure */
571 True
, /* compress_enterleave */
572 False
, /* visible_interest */
573 Destroy
, /* destroy */
575 ExposeProc
, /* expose */
576 SetValues
, /* set_values */
577 NULL
, /* set_values_hook */
578 XtInheritSetValuesAlmost
, /* set_values_almost */
579 NULL
, /* get_values_hook */
580 NULL
, /* accept_focus */
581 XtVersion
, /* version */
582 NULL
, /* callback_private */
583 translations
, /* tm_table */
584 NULL
, /* query_geometry */
585 NULL
, /* display_accelerator */
588 /* primitive_class fields */
590 NULL
, /* border_highlight */
591 NULL
, /* border_unhighlight */
592 NULL
, /* translations */
593 NULL
, /* arm_and_activate */
594 NULL
, /* syn_resources */
595 0, /* num_syn_resources */
603 WidgetClass xmGaugeWidgetClass
= (WidgetClass
)&xmGaugeClassRec
;
609 GaugePick(Widget
WXUNUSED(w
), XEvent
*WXUNUSED(e
), String
*WXUNUSED(args
), Cardinal
*WXUNUSED(num_args
))
611 /* Commented out for a read-only gauge in wxWidgets */
613 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
614 #define THIS gw->gauge
617 Boolean dragging
= False
;
618 XButtonEvent
*event
= (XButtonEvent
*)e
;
623 sht
= gw
->primitive
.shadow_thickness
;
624 _XmDrawShadows(XtDisplay(w
), XtWindow(w
),
625 gw
->primitive
.top_shadow_GC
,
626 gw
->primitive
.bottom_shadow_GC
,
627 0, 0, w
->core
.width
, w
->core
.height
,
631 ratio
= (float)((float)THIS
.maximum
-
632 (float)THIS
.minimum
) / (float)THIS
.value
;
633 switch(THIS
.orientation
) {
635 size
= (w
->core
.width
- 2 * sht
) / ratio
;
636 switch(THIS
.processingDirection
) {
638 case XmMAX_ON_BOTTOM
:
639 dragging
= (x
> sht
) && (y
> sht
) &&
640 (x
< sht
+ size
) && (y
< w
->core
.height
- sht
);
644 dragging
= (x
> w
->core
.width
- size
- sht
) && (y
> sht
) &&
645 (x
< w
->core
.width
- sht
) && (y
< w
->core
.height
+ sht
);
650 size
= (w
->core
.height
- 2 * sht
) / ratio
;
651 switch(THIS
.processingDirection
) {
653 case XmMAX_ON_BOTTOM
:
654 dragging
= (x
> sht
) && (y
> sht
) &&
655 (x
< w
->core
.width
- sht
) &&
656 (y
< w
->core
.width
- 2 * sht
+ size
);
660 dragging
= (x
> sht
) && (y
> w
->core
.height
- size
- sht
) &&
661 (x
< w
->core
.width
- sht
) && (y
< w
->core
.height
- sht
);
665 THIS
.dragging
= dragging
;
672 #define round(x) ( (x) > 0 ? ((x) + 0.5) : -(-(x) + 0.5) )
675 GaugeDrag(Widget
WXUNUSED(w
), XEvent
*WXUNUSED(e
), String
*WXUNUSED(args
), Cardinal
*WXUNUSED(num_args
))
677 /* Commented out for a read-only gauge in wxWidgets */
679 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
680 #define THIS gw->gauge
681 int sht
, x
, y
, max
, value
;
682 float ratio
, nratio
, size
, nsize
, fvalue
, delta
;
683 XMotionEvent
*event
= (XMotionEvent
*)e
;
685 if( ! THIS
.dragging
) return;
689 sht
= gw
->primitive
.shadow_thickness
;
691 ratio
= (float)THIS
.value
/ (float)((float)THIS
.maximum
-
692 (float)THIS
.minimum
);
693 switch(THIS
.orientation
) {
695 max
= (w
->core
.width
- 2 * sht
);
696 size
= (float)max
* ratio
;
697 delta
= (float)x
- (float)THIS
.oldx
;
700 max
= (w
->core
.height
- 2 * sht
);
701 size
= (float) max
* ratio
;
702 delta
= (float)y
- (float)THIS
.oldy
;
705 switch(THIS
.processingDirection
) {
707 case XmMAX_ON_BOTTOM
:
708 nsize
= size
+ delta
;
711 nsize
= size
- delta
;
713 if(nsize
> (float)max
) nsize
= (float)max
;
714 if(nsize
< (float)0 ) nsize
= (float)0;
715 nratio
= nsize
/ (float)max
;
717 fvalue
= (int)((float)THIS
.maximum
-
718 (float)THIS
.minimum
) * (float)nsize
/ (float)max
;
719 value
= round(fvalue
);
725 /* clear old slider only if it was larger */
726 DrawSlider(gw
, (nsize
< size
));
729 XmGaugeCallbackStruct call
;
731 if(NULL
!= THIS
.dragCallback
) {
732 call
.reason
= XmCR_DRAG
;
734 call
.value
= THIS
.value
;
735 XtCallCallbacks(w
, XmNdragCallback
, &call
);
744 GaugeDrop(Widget
WXUNUSED(w
), XEvent
*WXUNUSED(e
), String
*WXUNUSED(args
), Cardinal
*WXUNUSED(num_args
))
746 /* Commented out for a read-only gauge in wxWidgets */
748 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
749 #define THIS gw->gauge
750 if( ! THIS
.dragging
) return;
752 if(NULL
!= THIS
.valueChangedCallback
) {
753 XmGaugeCallbackStruct call
;
754 call
.reason
= XmCR_VALUE_CHANGED
;
756 call
.value
= THIS
.value
;
757 XtCallCallbacks(w
, XmNvalueChangedCallback
, &call
);
759 THIS
.dragging
= False
;
765 XmGaugeSetValue(Widget w
, int value
)
767 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
769 gw
->gauge
.value
= value
;
770 DrawSlider(gw
, True
);
771 XFlush(XtDisplay(w
));
775 XmGaugeGetValue(Widget w
)
777 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
779 return gw
->gauge
.value
;
782 #endif // !wxCHECK_MOTIF_VERSION( 2, 0 ) || wxCHECK_LESSTIF()