1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxGauge class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "gauge.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
20 #include <wx/vms_x_fix.h>
28 # include "wx/gauge.h"
31 #pragma message disable nosimpint
36 #endif // __WXMOTIF20__
38 #pragma message enable nosimpint
40 #include "wx/motif/private.h"
42 IMPLEMENT_DYNAMIC_CLASS(wxGauge
, wxControl
)
44 #if !wxCHECK_MOTIF_VERSION( 2, 0 ) || wxCHECK_LESSTIF()
46 // XmGauge copyright notice:
49 * Copyright 1994 GROUPE BULL
51 * Permission to use, copy, modify, and distribute this software and its
52 * documentation for any purpose and without fee is hereby granted, provided
53 * that the above copyright notice appear in all copies and that both that
54 * copyright notice and this permission notice appear in supporting
55 * documentation, and that the name of GROUPE BULL not be used in advertising
56 * or publicity pertaining to distribution of the software without specific,
57 * written prior permission. GROUPE BULL makes no representations about the
58 * suitability of this software for any purpose. It is provided "as is"
59 * without express or implied warranty.
61 * GROUPE BULL disclaims all warranties with regard to this software,
62 * including all implied warranties of merchantability and fitness,
63 * in no event shall GROUPE BULL be liable for any special,
64 * indirect or consequential damages or any damages
65 * whatsoever resulting from loss of use, data or profits,
66 * whether in an action of contract, negligence or other tortious
67 * action, arising out of or in connection with the use
68 * or performance of this software.
72 //// PUBLIC XMGAUGE DECLARATIONS
73 typedef struct _XmGaugeClassRec
* XmGaugeWidgetClass
;
74 typedef struct _XmGaugeRec
* XmGaugeWidget
;
77 extern "C" WidgetClass xmGaugeWidgetClass
;
79 extern WidgetClass xmGaugeWidgetClass
;
82 typedef struct _XmGaugeCallbackStruct
{
86 } XmGaugeCallbackStruct
;
90 XmGaugeSetValue(Widget w
, int value
);
93 XmGaugeGetValue(Widget w
);
95 #endif // !wxCHECK_MOTIF_VERSION( 2, 0 ) || wxCHECK_LESSTIF()
97 bool wxGauge::Create(wxWindow
*parent
, wxWindowID id
,
102 const wxValidator
& validator
,
103 const wxString
& name
)
105 if( !CreateControl( parent
, id
, pos
, size
, style
, validator
, name
) )
108 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
112 if (style
& wxGA_HORIZONTAL
)
114 XtSetArg (args
[0], XmNorientation
, XmHORIZONTAL
);
115 XtSetArg (args
[1], XmNprocessingDirection
, XmMAX_ON_RIGHT
);
119 XtSetArg (args
[0], XmNorientation
, XmVERTICAL
);
120 XtSetArg (args
[1], XmNprocessingDirection
, XmMAX_ON_TOP
);
122 XtSetArg(args
[2], XmNminimum
, 0);
123 XtSetArg(args
[3], XmNmaximum
, range
);
124 #if wxCHECK_MOTIF_VERSION( 2, 0 ) && !wxCHECK_LESSTIF()
125 XtSetArg(args
[4], XmNeditable
, False
); ++count
;
126 XtSetArg(args
[5], XmNslidingMode
, XmTHERMOMETER
); ++count
;
127 // XtSetArg(args[6], XmNsliderVisual, XmFOREGROUND_COLOR ); ++count;
129 XtCreateManagedWidget("gauge", xmScaleWidgetClass
,
130 parentWidget
, args
, count
);
133 XtCreateManagedWidget("gauge", xmGaugeWidgetClass
,
134 parentWidget
, args
, count
);
136 m_mainWidget
= (WXWidget
) gaugeWidget
;
138 XtManageChild (gaugeWidget
);
140 int x
= pos
.x
; int y
= pos
.y
;
141 wxSize best
= GetBestSize();
142 if( size
.x
!= -1 ) best
.x
= size
.x
;
143 if( size
.y
!= -1 ) best
.y
= size
.y
;
147 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
, x
, y
,
150 ChangeBackgroundColour();
155 wxSize
wxGauge::DoGetBestSize() const
157 if( HasFlag(wxGA_HORIZONTAL
) )
158 return wxSize( 100, 18 );
160 return wxSize( 18, 100 );
163 void wxGauge::SetShadowWidth(int w
)
167 XtVaSetValues((Widget
) m_mainWidget
, XmNshadowThickness
, w
, NULL
);
170 void wxGauge::SetRange(int r
)
172 XtVaSetValues((Widget
) m_mainWidget
, XmNmaximum
, r
, NULL
);
175 void wxGauge::SetValue(int pos
)
177 XtVaSetValues((Widget
) m_mainWidget
, XmNvalue
, pos
, NULL
);
180 int wxGauge::GetShadowWidth() const
183 XtVaGetValues((Widget
) m_mainWidget
, XmNshadowThickness
, &w
, NULL
);
187 int wxGauge::GetRange() const
190 XtVaGetValues((Widget
) m_mainWidget
, XmNmaximum
, &r
, NULL
);
194 int wxGauge::GetValue() const
197 XtVaGetValues((Widget
) m_mainWidget
, XmNvalue
, &pos
, NULL
);
201 void wxGauge::DoMoveWindow(int x
, int y
, int width
, int height
)
203 wxGaugeBase::DoMoveWindow( x
, y
, width
, height
);
205 XtVaSetValues( (Widget
)m_mainWidget
,
206 XmNscaleHeight
, height
,
211 #if !wxCHECK_MOTIF_VERSION( 2, 0 ) || wxCHECK_LESSTIF()
213 //// PRIVATE DECLARATIONS FOR XMGAUGE
215 #include <Xm/PrimitiveP.h>
216 #include <Xm/DrawP.h>
222 typedef struct _XmGaugeClassRec
{
223 CoreClassPart core_class
;
224 XmPrimitiveClassPart primitive_class
;
225 XmGaugeClassPart gauge_class
;
229 typedef struct _XmGaugePart
{
233 unsigned char orientation
;
234 unsigned char processingDirection
;
236 XtCallbackList dragCallback
;
237 XtCallbackList valueChangedCallback
;
240 Boolean dragging
; /* drag in progress ? */
246 typedef struct _XmGaugeRec
{
248 XmPrimitivePart primitive
;
252 extern XmGaugeClassRec xmGaugeClassRec
;
254 /* Copyright 1994 GROUPE BULL -- See license conditions in file COPYRIGHT */
256 //// XMGAUGE IMPLEMENTATION
259 GaugePick(Widget w
, XEvent
*e
, String
*args
, Cardinal
*num_args
);
261 GaugeDrag(Widget w
, XEvent
*e
, String
*args
, Cardinal
*num_args
);
263 GaugeDrop(Widget w
, XEvent
*e
, String
*args
, Cardinal
*num_args
);
267 static char translations
[] =
268 "<Btn1Down>: GaugePick()\n\
269 <Btn1Motion>: GaugeDrag()\n\
270 <Btn1Up>: GaugeDrop()\n\
275 static XtActionsRec actions
[] = {
276 {"GaugePick", GaugePick
},
277 {"GaugeDrag", GaugeDrag
},
278 {"GaugeDrop", GaugeDrop
},
282 DrawSlider(XmGaugeWidget gw
, Boolean clear
)
284 #define THIS gw->gauge
290 unsigned long backgr,foregr;
294 sht
= gw
->primitive
.shadow_thickness
;
296 ratio
= (float)THIS
.value
/
297 (float)(THIS
.maximum
- THIS
.minimum
);
299 sprintf(string,"%-d%%",(int)(ratio*100));
301 XtVaGetValues(gw,XmNbackground,&backgr,XmNforeground,&foregr,NULL);
305 XClearArea(XtDisplay(gw
), XtWindow(gw
), sht
, sht
,
306 gw
->core
.width
- 2 * sht
, gw
->core
.height
- 2 * sht
, False
);
308 switch(THIS
.orientation
) {
310 size
= (int) ((gw
->core
.width
- 2 * sht
)*ratio
);
312 XDrawString(XtDisplay(gw), XtWindow(gw), THIS.gc, sht+gw->core.width/2,
313 gw->core.height - 2 * sht, string, len);
315 switch(THIS
.processingDirection
) {
317 case XmMAX_ON_BOTTOM
:
318 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
319 sht
, sht
, size
, gw
->core
.height
- 2 * sht
);
322 rects[0].x = sht; rects[0].y = sht;
323 rects[0].width = size; rects[0].height = gw->core.height - 2 * sht;
328 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
329 gw
->core
.width
- size
- sht
, sht
,
330 size
, gw
->core
.height
- 2 * sht
);
333 rects[0].x = gw->core.width - size - sht; rects[0].y = sht;
334 rects[0].width = size; rects[0].height = gw->core.height - 2 * sht;
339 XSetClipRectangles(XtDisplay(gw), THIS.gc, 0, 0, rects, 1, Unsorted);
340 XSetForeground(XtDisplay(gw), THIS.gc, backgr);
341 XDrawString(XtDisplay(gw), XtWindow(gw), THIS.gc, sht+gw->core.width/2,
342 gw->core.height - 2 * sht, string, len);
347 size
= (int) ((gw
->core
.height
- 2 * sht
)*ratio
);
349 XDrawString(XtDisplay(gw), XtWindow(gw), THIS.gc, sht,
350 sht+gw->core.height/2, string,len);
352 switch(THIS
.processingDirection
) {
354 case XmMAX_ON_BOTTOM
:
355 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
356 sht
, sht
, gw
->core
.width
- 2 * sht
, size
);
359 rects[0].x = sht; rects[0].y = sht;
360 rects[0].width = gw->core.width - 2 * sht; rects[0].height = size;
365 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
366 sht
, gw
->core
.height
- size
- sht
,
367 gw
->core
.width
- 2 * sht
, size
);
370 rects[0].x = sht; rects[0].y = gw->core.height - size - sht;
371 rects[0].width = gw->core.width - 2 * sht; rects[0].height = size;
375 XSetClipRectangles(XtDisplay(gw), THIS.gc, 0, 0, rects, 1, Unsorted);
376 XSetForeground(XtDisplay(gw), THIS.gc, backgr);
377 XDrawString(XtDisplay(gw), XtWindow(gw), THIS.gc, sht,
378 sht+gw->core.height/2, string,len);
383 XSetClipMask(XtDisplay(gw), THIS.gc, None);
384 XSetForeground(XtDisplay(gw), THIS.gc, foregr);
393 DrawSlider(XmGaugeWidget gw
, Boolean clear
)
395 #define THIS gw->gauge
399 sht
= gw
->primitive
.shadow_thickness
;
400 /* See fix comment below: can cause divide by zero error.
401 ratio = (float)((float)THIS.maximum -
402 (float)THIS.minimum) / (float)THIS.value;
405 XClearArea(XtDisplay(gw
), XtWindow(gw
), sht
, sht
,
406 gw
->core
.width
- 2 * sht
, gw
->core
.height
- 2 * sht
, False
);
408 switch(THIS
.orientation
) {
410 /* size = (gw->core.width - 2 * sht) / ratio; */
411 /* A fix suggested by Dmitri Chubraev */
412 size
= (gw
->core
.width
- 2 * sht
) /((float)THIS
.maximum
-(float)THIS
.minimum
)*(float)THIS
.value
;
413 switch(THIS
.processingDirection
) {
415 case XmMAX_ON_BOTTOM
:
416 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
417 sht
, sht
, size
, gw
->core
.height
- 2 * sht
);
421 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
422 gw
->core
.width
- size
- sht
, sht
,
423 size
, gw
->core
.height
- 2 * sht
);
428 size
= (gw
->core
.height
- 2 * sht
) /((float)THIS
.maximum
-(float)THIS
.minimum
)*(float)THIS
.value
;
429 /* size = (gw->core.height - 2 * sht)/ ratio; */
430 switch(THIS
.processingDirection
) {
432 case XmMAX_ON_BOTTOM
:
433 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
434 sht
, sht
, gw
->core
.width
- 2 * sht
, size
);
438 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
439 sht
, gw
->core
.height
- size
- sht
,
440 gw
->core
.width
- 2 * sht
, size
);
449 Initialize(Widget
WXUNUSED(req
), Widget new_w
, ArgList
WXUNUSED(args
), Cardinal
*WXUNUSED(num_args
))
451 XmGaugeWidget gw
= (XmGaugeWidget
)new_w
;
452 #define THIS gw->gauge
455 values
.foreground
= gw
->primitive
.foreground
;
456 THIS
.gc
= XtGetGC(new_w
, GCForeground
, &values
);
467 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
468 #define THIS gw->gauge
469 XtReleaseGC(w
, THIS
.gc
);
481 ArgList
WXUNUSED(args
),
482 Cardinal
*WXUNUSED(num_args
) )
484 XmGaugeWidget cgw
= (XmGaugeWidget
)cw
;
485 XmGaugeWidget ngw
= (XmGaugeWidget
)nw
;
487 Boolean redraw
= False
;
488 if(cgw
->primitive
.foreground
!= ngw
->primitive
.foreground
) {
492 XtReleaseGC(nw
, ngw
->gauge
.gc
);
493 values
.foreground
= ngw
->primitive
.foreground
;
494 ngw
->gauge
.gc
= XtGetGC(nw
, GCForeground
, &values
);
496 if(cgw
->gauge
.value
!= ngw
->gauge
.value
) {
506 ExposeProc(Widget w
, XEvent
*WXUNUSED(event
), Region
WXUNUSED(r
))
508 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
509 #define THIS gw->gauge
512 sht
= gw
->primitive
.shadow_thickness
;
513 _XmDrawShadows(XtDisplay(w
), XtWindow(w
),
514 gw
->primitive
.top_shadow_GC
,
515 gw
->primitive
.bottom_shadow_GC
,
516 0, 0, w
->core
.width
, w
->core
.height
,
518 DrawSlider(gw
, False
);
528 #define offset(field) XtOffset(XmGaugeWidget, gauge.field)
529 {XmNvalue
, XmCValue
, XtRInt
, sizeof(int),
530 offset(value
), XtRImmediate
, (caddr_t
)10},
532 {XmNminimum
, XmCValue
, XtRInt
, sizeof(int),
533 offset(minimum
), XtRImmediate
, (caddr_t
)0},
535 {XmNmaximum
, XmCValue
, XtRInt
, sizeof(int),
536 offset(maximum
), XtRImmediate
, (caddr_t
)100},
538 {XmNorientation
, XmCOrientation
, XmROrientation
, sizeof(unsigned char),
539 offset(orientation
), XtRImmediate
, (caddr_t
)XmVERTICAL
},
541 {XmNprocessingDirection
, XmCProcessingDirection
,
542 XmRProcessingDirection
, sizeof(unsigned char),
543 offset(processingDirection
), XtRImmediate
, (caddr_t
)XmMAX_ON_RIGHT
},
545 {XmNdragCallback
, XmCCallback
, XmRCallback
, sizeof(XtCallbackList
),
546 offset(dragCallback
), XtRImmediate
, (caddr_t
)NULL
},
548 {XmNvalueChangedCallback
, XmCCallback
, XmRCallback
, sizeof(XtCallbackList
),
549 offset(valueChangedCallback
), XtRImmediate
, (caddr_t
)NULL
},
556 XmGaugeClassRec xmGaugeClassRec
= {
558 (WidgetClass
) &xmPrimitiveClassRec
, /* superclass */
559 "XmGauge", /* class_name */
560 sizeof(XmGaugeRec
), /* widget_size */
561 NULL
, /* class_initialize */
562 NULL
, /* class_part_initialize */
563 FALSE
, /* class_inited */
564 Initialize
, /* initialize */
565 NULL
, /* initialize_hook */
566 XtInheritRealize
, /* realize */
567 actions
, /* actions */
568 XtNumber(actions
), /* num_actions */
569 resources
, /* resources */
570 XtNumber(resources
), /* num_resources */
571 NULLQUARK
, /* xrm_class */
572 TRUE
, /* compress_motion */
573 TRUE
, /* compress_exposure */
574 TRUE
, /* compress_enterleave */
575 FALSE
, /* visible_interest */
576 Destroy
, /* destroy */
578 ExposeProc
, /* expose */
579 SetValues
, /* set_values */
580 NULL
, /* set_values_hook */
581 XtInheritSetValuesAlmost
, /* set_values_almost */
582 NULL
, /* get_values_hook */
583 NULL
, /* accept_focus */
584 XtVersion
, /* version */
585 NULL
, /* callback_private */
586 translations
, /* tm_table */
587 NULL
, /* query_geometry */
588 NULL
, /* display_accelerator */
591 /* primitive_class fields */
593 NULL
, /* border_highlight */
594 NULL
, /* border_unhighlight */
595 NULL
, /* translations */
596 NULL
, /* arm_and_activate */
597 NULL
, /* syn_resources */
598 0, /* num_syn_resources */
606 WidgetClass xmGaugeWidgetClass
= (WidgetClass
)&xmGaugeClassRec
;
612 GaugePick(Widget
WXUNUSED(w
), XEvent
*WXUNUSED(e
), String
*WXUNUSED(args
), Cardinal
*WXUNUSED(num_args
))
614 /* Commented out for a read-only gauge in wxWidgets */
616 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
617 #define THIS gw->gauge
620 Boolean dragging
= False
;
621 XButtonEvent
*event
= (XButtonEvent
*)e
;
626 sht
= gw
->primitive
.shadow_thickness
;
627 _XmDrawShadows(XtDisplay(w
), XtWindow(w
),
628 gw
->primitive
.top_shadow_GC
,
629 gw
->primitive
.bottom_shadow_GC
,
630 0, 0, w
->core
.width
, w
->core
.height
,
634 ratio
= (float)((float)THIS
.maximum
-
635 (float)THIS
.minimum
) / (float)THIS
.value
;
636 switch(THIS
.orientation
) {
638 size
= (w
->core
.width
- 2 * sht
) / ratio
;
639 switch(THIS
.processingDirection
) {
641 case XmMAX_ON_BOTTOM
:
642 dragging
= (x
> sht
) && (y
> sht
) &&
643 (x
< sht
+ size
) && (y
< w
->core
.height
- sht
);
647 dragging
= (x
> w
->core
.width
- size
- sht
) && (y
> sht
) &&
648 (x
< w
->core
.width
- sht
) && (y
< w
->core
.height
+ sht
);
653 size
= (w
->core
.height
- 2 * sht
) / ratio
;
654 switch(THIS
.processingDirection
) {
656 case XmMAX_ON_BOTTOM
:
657 dragging
= (x
> sht
) && (y
> sht
) &&
658 (x
< w
->core
.width
- sht
) &&
659 (y
< w
->core
.width
- 2 * sht
+ size
);
663 dragging
= (x
> sht
) && (y
> w
->core
.height
- size
- sht
) &&
664 (x
< w
->core
.width
- sht
) && (y
< w
->core
.height
- sht
);
668 THIS
.dragging
= dragging
;
675 #define round(x) ( (x) > 0 ? ((x) + 0.5) : -(-(x) + 0.5) )
678 GaugeDrag(Widget
WXUNUSED(w
), XEvent
*WXUNUSED(e
), String
*WXUNUSED(args
), Cardinal
*WXUNUSED(num_args
))
680 /* Commented out for a read-only gauge in wxWidgets */
682 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
683 #define THIS gw->gauge
684 int sht
, x
, y
, max
, value
;
685 float ratio
, nratio
, size
, nsize
, fvalue
, delta
;
686 XMotionEvent
*event
= (XMotionEvent
*)e
;
688 if( ! THIS
.dragging
) return;
692 sht
= gw
->primitive
.shadow_thickness
;
694 ratio
= (float)THIS
.value
/ (float)((float)THIS
.maximum
-
695 (float)THIS
.minimum
);
696 switch(THIS
.orientation
) {
698 max
= (w
->core
.width
- 2 * sht
);
699 size
= (float)max
* ratio
;
700 delta
= (float)x
- (float)THIS
.oldx
;
703 max
= (w
->core
.height
- 2 * sht
);
704 size
= (float) max
* ratio
;
705 delta
= (float)y
- (float)THIS
.oldy
;
708 switch(THIS
.processingDirection
) {
710 case XmMAX_ON_BOTTOM
:
711 nsize
= size
+ delta
;
714 nsize
= size
- delta
;
716 if(nsize
> (float)max
) nsize
= (float)max
;
717 if(nsize
< (float)0 ) nsize
= (float)0;
718 nratio
= nsize
/ (float)max
;
720 fvalue
= (int)((float)THIS
.maximum
-
721 (float)THIS
.minimum
) * (float)nsize
/ (float)max
;
722 value
= round(fvalue
);
728 /* clear old slider only if it was larger */
729 DrawSlider(gw
, (nsize
< size
));
732 XmGaugeCallbackStruct call
;
734 if(NULL
!= THIS
.dragCallback
) {
735 call
.reason
= XmCR_DRAG
;
737 call
.value
= THIS
.value
;
738 XtCallCallbacks(w
, XmNdragCallback
, &call
);
747 GaugeDrop(Widget
WXUNUSED(w
), XEvent
*WXUNUSED(e
), String
*WXUNUSED(args
), Cardinal
*WXUNUSED(num_args
))
749 /* Commented out for a read-only gauge in wxWidgets */
751 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
752 #define THIS gw->gauge
753 if( ! THIS
.dragging
) return;
755 if(NULL
!= THIS
.valueChangedCallback
) {
756 XmGaugeCallbackStruct call
;
757 call
.reason
= XmCR_VALUE_CHANGED
;
759 call
.value
= THIS
.value
;
760 XtCallCallbacks(w
, XmNvalueChangedCallback
, &call
);
762 THIS
.dragging
= False
;
768 XmGaugeSetValue(Widget w
, int value
)
770 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
772 gw
->gauge
.value
= value
;
773 DrawSlider(gw
, True
);
774 XFlush(XtDisplay(w
));
778 XmGaugeGetValue(Widget w
)
780 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
782 return gw
->gauge
.value
;
785 #endif // !wxCHECK_MOTIF_VERSION( 2, 0 ) || wxCHECK_LESSTIF()