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>
25 # include "wx/gauge.h"
32 #pragma message disable nosimpint
37 #endif // __WXMOTIF20__
39 #pragma message enable nosimpint
41 #include "wx/motif/private.h"
43 IMPLEMENT_DYNAMIC_CLASS(wxGauge
, wxControl
)
45 #if !wxCHECK_MOTIF_VERSION( 2, 0 ) || wxCHECK_LESSTIF()
47 // XmGauge copyright notice:
50 * Copyright 1994 GROUPE BULL
52 * Permission to use, copy, modify, and distribute this software and its
53 * documentation for any purpose and without fee is hereby granted, provided
54 * that the above copyright notice appear in all copies and that both that
55 * copyright notice and this permission notice appear in supporting
56 * documentation, and that the name of GROUPE BULL not be used in advertising
57 * or publicity pertaining to distribution of the software without specific,
58 * written prior permission. GROUPE BULL makes no representations about the
59 * suitability of this software for any purpose. It is provided "as is"
60 * without express or implied warranty.
62 * GROUPE BULL disclaims all warranties with regard to this software,
63 * including all implied warranties of merchantability and fitness,
64 * in no event shall GROUPE BULL be liable for any special,
65 * indirect or consequential damages or any damages
66 * whatsoever resulting from loss of use, data or profits,
67 * whether in an action of contract, negligence or other tortious
68 * action, arising out of or in connection with the use
69 * or performance of this software.
73 //// PUBLIC XMGAUGE DECLARATIONS
74 typedef struct _XmGaugeClassRec
* XmGaugeWidgetClass
;
75 typedef struct _XmGaugeRec
* XmGaugeWidget
;
78 extern "C" WidgetClass xmGaugeWidgetClass
;
80 extern WidgetClass xmGaugeWidgetClass
;
83 typedef struct _XmGaugeCallbackStruct
{
87 } XmGaugeCallbackStruct
;
91 XmGaugeSetValue(Widget w
, int value
);
94 XmGaugeGetValue(Widget w
);
96 #endif // !wxCHECK_MOTIF_VERSION( 2, 0 ) || wxCHECK_LESSTIF()
98 bool wxGauge::Create(wxWindow
*parent
, wxWindowID id
,
103 const wxValidator
& validator
,
104 const wxString
& name
)
106 if( !CreateControl( parent
, id
, pos
, size
, style
, validator
, name
) )
109 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
113 if (style
& wxGA_HORIZONTAL
)
115 XtSetArg (args
[0], XmNorientation
, XmHORIZONTAL
);
116 XtSetArg (args
[1], XmNprocessingDirection
, XmMAX_ON_RIGHT
);
120 XtSetArg (args
[0], XmNorientation
, XmVERTICAL
);
121 XtSetArg (args
[1], XmNprocessingDirection
, XmMAX_ON_TOP
);
123 XtSetArg(args
[2], XmNminimum
, 0);
124 XtSetArg(args
[3], XmNmaximum
, range
);
125 #if wxCHECK_MOTIF_VERSION( 2, 0 ) && !wxCHECK_LESSTIF()
126 XtSetArg(args
[4], XmNeditable
, False
); ++count
;
127 XtSetArg(args
[5], XmNslidingMode
, XmTHERMOMETER
); ++count
;
128 // XtSetArg(args[6], XmNsliderVisual, XmFOREGROUND_COLOR ); ++count;
130 XtCreateManagedWidget("gauge", xmScaleWidgetClass
,
131 parentWidget
, args
, count
);
134 XtCreateManagedWidget("gauge", xmGaugeWidgetClass
,
135 parentWidget
, args
, count
);
137 m_mainWidget
= (WXWidget
) gaugeWidget
;
139 XtManageChild (gaugeWidget
);
141 int x
= pos
.x
; int y
= pos
.y
;
142 wxSize best
= GetBestSize();
143 if( size
.x
!= -1 ) best
.x
= size
.x
;
144 if( size
.y
!= -1 ) best
.y
= size
.y
;
148 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
, x
, y
,
151 ChangeBackgroundColour();
156 wxSize
wxGauge::DoGetBestSize() const
158 if( HasFlag(wxGA_HORIZONTAL
) )
159 return wxSize( 100, 18 );
161 return wxSize( 18, 100 );
164 void wxGauge::SetShadowWidth(int w
)
168 XtVaSetValues((Widget
) m_mainWidget
, XmNshadowThickness
, w
, NULL
);
171 void wxGauge::SetRange(int r
)
173 XtVaSetValues((Widget
) m_mainWidget
, XmNmaximum
, r
, NULL
);
176 void wxGauge::SetValue(int pos
)
178 XtVaSetValues((Widget
) m_mainWidget
, XmNvalue
, pos
, NULL
);
181 int wxGauge::GetShadowWidth() const
184 XtVaGetValues((Widget
) m_mainWidget
, XmNshadowThickness
, &w
, NULL
);
188 int wxGauge::GetRange() const
191 XtVaGetValues((Widget
) m_mainWidget
, XmNmaximum
, &r
, NULL
);
195 int wxGauge::GetValue() const
198 XtVaGetValues((Widget
) m_mainWidget
, XmNvalue
, &pos
, NULL
);
202 void wxGauge::DoMoveWindow(int x
, int y
, int width
, int height
)
204 wxGaugeBase::DoMoveWindow( x
, y
, width
, height
);
206 XtVaSetValues( (Widget
)m_mainWidget
,
207 XmNscaleHeight
, height
,
212 #if !wxCHECK_MOTIF_VERSION( 2, 0 ) || wxCHECK_LESSTIF()
214 //// PRIVATE DECLARATIONS FOR XMGAUGE
216 #include <Xm/PrimitiveP.h>
217 #include <Xm/DrawP.h>
223 typedef struct _XmGaugeClassRec
{
224 CoreClassPart core_class
;
225 XmPrimitiveClassPart primitive_class
;
226 XmGaugeClassPart gauge_class
;
230 typedef struct _XmGaugePart
{
234 unsigned char orientation
;
235 unsigned char processingDirection
;
237 XtCallbackList dragCallback
;
238 XtCallbackList valueChangedCallback
;
241 Boolean dragging
; /* drag in progress ? */
247 typedef struct _XmGaugeRec
{
249 XmPrimitivePart primitive
;
253 extern XmGaugeClassRec xmGaugeClassRec
;
255 /* Copyright 1994 GROUPE BULL -- See license conditions in file COPYRIGHT */
257 //// XMGAUGE IMPLEMENTATION
260 GaugePick(Widget w
, XEvent
*e
, String
*args
, Cardinal
*num_args
);
262 GaugeDrag(Widget w
, XEvent
*e
, String
*args
, Cardinal
*num_args
);
264 GaugeDrop(Widget w
, XEvent
*e
, String
*args
, Cardinal
*num_args
);
268 static char translations
[] =
269 "<Btn1Down>: GaugePick()\n\
270 <Btn1Motion>: GaugeDrag()\n\
271 <Btn1Up>: GaugeDrop()\n\
276 static XtActionsRec actions
[] = {
277 {"GaugePick", GaugePick
},
278 {"GaugeDrag", GaugeDrag
},
279 {"GaugeDrop", GaugeDrop
},
283 DrawSlider(XmGaugeWidget gw
, Boolean clear
)
285 #define THIS gw->gauge
291 unsigned long backgr,foregr;
295 sht
= gw
->primitive
.shadow_thickness
;
297 ratio
= (float)THIS
.value
/
298 (float)(THIS
.maximum
- THIS
.minimum
);
300 sprintf(string,"%-d%%",(int)(ratio*100));
302 XtVaGetValues(gw,XmNbackground,&backgr,XmNforeground,&foregr,NULL);
306 XClearArea(XtDisplay(gw
), XtWindow(gw
), sht
, sht
,
307 gw
->core
.width
- 2 * sht
, gw
->core
.height
- 2 * sht
, False
);
309 switch(THIS
.orientation
) {
311 size
= (int) ((gw
->core
.width
- 2 * sht
)*ratio
);
313 XDrawString(XtDisplay(gw), XtWindow(gw), THIS.gc, sht+gw->core.width/2,
314 gw->core.height - 2 * sht, string, len);
316 switch(THIS
.processingDirection
) {
318 case XmMAX_ON_BOTTOM
:
319 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
320 sht
, sht
, size
, gw
->core
.height
- 2 * sht
);
323 rects[0].x = sht; rects[0].y = sht;
324 rects[0].width = size; rects[0].height = gw->core.height - 2 * sht;
329 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
330 gw
->core
.width
- size
- sht
, sht
,
331 size
, gw
->core
.height
- 2 * sht
);
334 rects[0].x = gw->core.width - size - sht; rects[0].y = sht;
335 rects[0].width = size; rects[0].height = gw->core.height - 2 * sht;
340 XSetClipRectangles(XtDisplay(gw), THIS.gc, 0, 0, rects, 1, Unsorted);
341 XSetForeground(XtDisplay(gw), THIS.gc, backgr);
342 XDrawString(XtDisplay(gw), XtWindow(gw), THIS.gc, sht+gw->core.width/2,
343 gw->core.height - 2 * sht, string, len);
348 size
= (int) ((gw
->core
.height
- 2 * sht
)*ratio
);
350 XDrawString(XtDisplay(gw), XtWindow(gw), THIS.gc, sht,
351 sht+gw->core.height/2, string,len);
353 switch(THIS
.processingDirection
) {
355 case XmMAX_ON_BOTTOM
:
356 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
357 sht
, sht
, gw
->core
.width
- 2 * sht
, size
);
360 rects[0].x = sht; rects[0].y = sht;
361 rects[0].width = gw->core.width - 2 * sht; rects[0].height = size;
366 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
367 sht
, gw
->core
.height
- size
- sht
,
368 gw
->core
.width
- 2 * sht
, size
);
371 rects[0].x = sht; rects[0].y = gw->core.height - size - sht;
372 rects[0].width = gw->core.width - 2 * sht; rects[0].height = size;
376 XSetClipRectangles(XtDisplay(gw), THIS.gc, 0, 0, rects, 1, Unsorted);
377 XSetForeground(XtDisplay(gw), THIS.gc, backgr);
378 XDrawString(XtDisplay(gw), XtWindow(gw), THIS.gc, sht,
379 sht+gw->core.height/2, string,len);
384 XSetClipMask(XtDisplay(gw), THIS.gc, None);
385 XSetForeground(XtDisplay(gw), THIS.gc, foregr);
394 DrawSlider(XmGaugeWidget gw
, Boolean clear
)
396 #define THIS gw->gauge
400 sht
= gw
->primitive
.shadow_thickness
;
401 /* See fix comment below: can cause divide by zero error.
402 ratio = (float)((float)THIS.maximum -
403 (float)THIS.minimum) / (float)THIS.value;
406 XClearArea(XtDisplay(gw
), XtWindow(gw
), sht
, sht
,
407 gw
->core
.width
- 2 * sht
, gw
->core
.height
- 2 * sht
, False
);
409 switch(THIS
.orientation
) {
411 /* size = (gw->core.width - 2 * sht) / ratio; */
412 /* A fix suggested by Dmitri Chubraev */
413 size
= (gw
->core
.width
- 2 * sht
) /((float)THIS
.maximum
-(float)THIS
.minimum
)*(float)THIS
.value
;
414 switch(THIS
.processingDirection
) {
416 case XmMAX_ON_BOTTOM
:
417 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
418 sht
, sht
, size
, gw
->core
.height
- 2 * sht
);
422 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
423 gw
->core
.width
- size
- sht
, sht
,
424 size
, gw
->core
.height
- 2 * sht
);
429 size
= (gw
->core
.height
- 2 * sht
) /((float)THIS
.maximum
-(float)THIS
.minimum
)*(float)THIS
.value
;
430 /* size = (gw->core.height - 2 * sht)/ ratio; */
431 switch(THIS
.processingDirection
) {
433 case XmMAX_ON_BOTTOM
:
434 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
435 sht
, sht
, gw
->core
.width
- 2 * sht
, size
);
439 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
440 sht
, gw
->core
.height
- size
- sht
,
441 gw
->core
.width
- 2 * sht
, size
);
450 Initialize(Widget
WXUNUSED(req
), Widget new_w
, ArgList
WXUNUSED(args
), Cardinal
*WXUNUSED(num_args
))
452 XmGaugeWidget gw
= (XmGaugeWidget
)new_w
;
453 #define THIS gw->gauge
456 values
.foreground
= gw
->primitive
.foreground
;
457 THIS
.gc
= XtGetGC(new_w
, GCForeground
, &values
);
468 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
469 #define THIS gw->gauge
470 XtReleaseGC(w
, THIS
.gc
);
482 ArgList
WXUNUSED(args
),
483 Cardinal
*WXUNUSED(num_args
) )
485 XmGaugeWidget cgw
= (XmGaugeWidget
)cw
;
486 XmGaugeWidget ngw
= (XmGaugeWidget
)nw
;
488 Boolean redraw
= False
;
489 if(cgw
->primitive
.foreground
!= ngw
->primitive
.foreground
) {
493 XtReleaseGC(nw
, ngw
->gauge
.gc
);
494 values
.foreground
= ngw
->primitive
.foreground
;
495 ngw
->gauge
.gc
= XtGetGC(nw
, GCForeground
, &values
);
497 if(cgw
->gauge
.value
!= ngw
->gauge
.value
) {
507 ExposeProc(Widget w
, XEvent
*WXUNUSED(event
), Region
WXUNUSED(r
))
509 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
510 #define THIS gw->gauge
513 sht
= gw
->primitive
.shadow_thickness
;
514 _XmDrawShadows(XtDisplay(w
), XtWindow(w
),
515 gw
->primitive
.top_shadow_GC
,
516 gw
->primitive
.bottom_shadow_GC
,
517 0, 0, w
->core
.width
, w
->core
.height
,
518 (Dimension
)sht
, XmSHADOW_IN
);
519 DrawSlider(gw
, False
);
529 #define offset(field) XtOffset(XmGaugeWidget, gauge.field)
530 {XmNvalue
, XmCValue
, XtRInt
, sizeof(int),
531 offset(value
), XtRImmediate
, (caddr_t
)10},
533 {XmNminimum
, XmCValue
, XtRInt
, sizeof(int),
534 offset(minimum
), XtRImmediate
, (caddr_t
)0},
536 {XmNmaximum
, XmCValue
, XtRInt
, sizeof(int),
537 offset(maximum
), XtRImmediate
, (caddr_t
)100},
539 {XmNorientation
, XmCOrientation
, XmROrientation
, sizeof(unsigned char),
540 offset(orientation
), XtRImmediate
, (caddr_t
)XmVERTICAL
},
542 {XmNprocessingDirection
, XmCProcessingDirection
,
543 XmRProcessingDirection
, sizeof(unsigned char),
544 offset(processingDirection
), XtRImmediate
, (caddr_t
)XmMAX_ON_RIGHT
},
546 {XmNdragCallback
, XmCCallback
, XmRCallback
, sizeof(XtCallbackList
),
547 offset(dragCallback
), XtRImmediate
, (caddr_t
)NULL
},
549 {XmNvalueChangedCallback
, XmCCallback
, XmRCallback
, sizeof(XtCallbackList
),
550 offset(valueChangedCallback
), XtRImmediate
, (caddr_t
)NULL
},
557 XmGaugeClassRec xmGaugeClassRec
= {
559 (WidgetClass
) &xmPrimitiveClassRec
, /* superclass */
560 "XmGauge", /* class_name */
561 sizeof(XmGaugeRec
), /* widget_size */
562 NULL
, /* class_initialize */
563 NULL
, /* class_part_initialize */
564 False
, /* class_inited */
565 Initialize
, /* initialize */
566 NULL
, /* initialize_hook */
567 XtInheritRealize
, /* realize */
568 actions
, /* actions */
569 XtNumber(actions
), /* num_actions */
570 resources
, /* resources */
571 XtNumber(resources
), /* num_resources */
572 NULLQUARK
, /* xrm_class */
573 True
, /* compress_motion */
574 True
, /* compress_exposure */
575 True
, /* compress_enterleave */
576 False
, /* visible_interest */
577 Destroy
, /* destroy */
579 ExposeProc
, /* expose */
580 SetValues
, /* set_values */
581 NULL
, /* set_values_hook */
582 XtInheritSetValuesAlmost
, /* set_values_almost */
583 NULL
, /* get_values_hook */
584 NULL
, /* accept_focus */
585 XtVersion
, /* version */
586 NULL
, /* callback_private */
587 translations
, /* tm_table */
588 NULL
, /* query_geometry */
589 NULL
, /* display_accelerator */
592 /* primitive_class fields */
594 NULL
, /* border_highlight */
595 NULL
, /* border_unhighlight */
596 NULL
, /* translations */
597 NULL
, /* arm_and_activate */
598 NULL
, /* syn_resources */
599 0, /* num_syn_resources */
607 WidgetClass xmGaugeWidgetClass
= (WidgetClass
)&xmGaugeClassRec
;
613 GaugePick(Widget
WXUNUSED(w
), XEvent
*WXUNUSED(e
), String
*WXUNUSED(args
), Cardinal
*WXUNUSED(num_args
))
615 /* Commented out for a read-only gauge in wxWidgets */
617 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
618 #define THIS gw->gauge
621 Boolean dragging
= False
;
622 XButtonEvent
*event
= (XButtonEvent
*)e
;
627 sht
= gw
->primitive
.shadow_thickness
;
628 _XmDrawShadows(XtDisplay(w
), XtWindow(w
),
629 gw
->primitive
.top_shadow_GC
,
630 gw
->primitive
.bottom_shadow_GC
,
631 0, 0, w
->core
.width
, w
->core
.height
,
635 ratio
= (float)((float)THIS
.maximum
-
636 (float)THIS
.minimum
) / (float)THIS
.value
;
637 switch(THIS
.orientation
) {
639 size
= (w
->core
.width
- 2 * sht
) / ratio
;
640 switch(THIS
.processingDirection
) {
642 case XmMAX_ON_BOTTOM
:
643 dragging
= (x
> sht
) && (y
> sht
) &&
644 (x
< sht
+ size
) && (y
< w
->core
.height
- sht
);
648 dragging
= (x
> w
->core
.width
- size
- sht
) && (y
> sht
) &&
649 (x
< w
->core
.width
- sht
) && (y
< w
->core
.height
+ sht
);
654 size
= (w
->core
.height
- 2 * sht
) / ratio
;
655 switch(THIS
.processingDirection
) {
657 case XmMAX_ON_BOTTOM
:
658 dragging
= (x
> sht
) && (y
> sht
) &&
659 (x
< w
->core
.width
- sht
) &&
660 (y
< w
->core
.width
- 2 * sht
+ size
);
664 dragging
= (x
> sht
) && (y
> w
->core
.height
- size
- sht
) &&
665 (x
< w
->core
.width
- sht
) && (y
< w
->core
.height
- sht
);
669 THIS
.dragging
= dragging
;
676 #define round(x) ( (x) > 0 ? ((x) + 0.5) : -(-(x) + 0.5) )
679 GaugeDrag(Widget
WXUNUSED(w
), XEvent
*WXUNUSED(e
), String
*WXUNUSED(args
), Cardinal
*WXUNUSED(num_args
))
681 /* Commented out for a read-only gauge in wxWidgets */
683 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
684 #define THIS gw->gauge
685 int sht
, x
, y
, max
, value
;
686 float ratio
, nratio
, size
, nsize
, fvalue
, delta
;
687 XMotionEvent
*event
= (XMotionEvent
*)e
;
689 if( ! THIS
.dragging
) return;
693 sht
= gw
->primitive
.shadow_thickness
;
695 ratio
= (float)THIS
.value
/ (float)((float)THIS
.maximum
-
696 (float)THIS
.minimum
);
697 switch(THIS
.orientation
) {
699 max
= (w
->core
.width
- 2 * sht
);
700 size
= (float)max
* ratio
;
701 delta
= (float)x
- (float)THIS
.oldx
;
704 max
= (w
->core
.height
- 2 * sht
);
705 size
= (float) max
* ratio
;
706 delta
= (float)y
- (float)THIS
.oldy
;
709 switch(THIS
.processingDirection
) {
711 case XmMAX_ON_BOTTOM
:
712 nsize
= size
+ delta
;
715 nsize
= size
- delta
;
717 if(nsize
> (float)max
) nsize
= (float)max
;
718 if(nsize
< (float)0 ) nsize
= (float)0;
719 nratio
= nsize
/ (float)max
;
721 fvalue
= (int)((float)THIS
.maximum
-
722 (float)THIS
.minimum
) * (float)nsize
/ (float)max
;
723 value
= round(fvalue
);
729 /* clear old slider only if it was larger */
730 DrawSlider(gw
, (nsize
< size
));
733 XmGaugeCallbackStruct call
;
735 if(NULL
!= THIS
.dragCallback
) {
736 call
.reason
= XmCR_DRAG
;
738 call
.value
= THIS
.value
;
739 XtCallCallbacks(w
, XmNdragCallback
, &call
);
748 GaugeDrop(Widget
WXUNUSED(w
), XEvent
*WXUNUSED(e
), String
*WXUNUSED(args
), Cardinal
*WXUNUSED(num_args
))
750 /* Commented out for a read-only gauge in wxWidgets */
752 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
753 #define THIS gw->gauge
754 if( ! THIS
.dragging
) return;
756 if(NULL
!= THIS
.valueChangedCallback
) {
757 XmGaugeCallbackStruct call
;
758 call
.reason
= XmCR_VALUE_CHANGED
;
760 call
.value
= THIS
.value
;
761 XtCallCallbacks(w
, XmNvalueChangedCallback
, &call
);
763 THIS
.dragging
= False
;
769 XmGaugeSetValue(Widget w
, int value
)
771 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
773 gw
->gauge
.value
= value
;
774 DrawSlider(gw
, True
);
775 XFlush(XtDisplay(w
));
779 XmGaugeGetValue(Widget w
)
781 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
783 return gw
->gauge
.value
;
786 #endif // !wxCHECK_MOTIF_VERSION( 2, 0 ) || wxCHECK_LESSTIF()