1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/gauge.cpp
3 // Purpose: wxGauge class
4 // Author: Julian Smart
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
15 #include "wx/vms_x_fix.h"
35 #pragma message disable nosimpint
40 #endif // __WXMOTIF20__
42 #pragma message enable nosimpint
44 #include "wx/motif/private.h"
46 #if !wxCHECK_MOTIF_VERSION( 2, 0 ) || wxCHECK_LESSTIF()
48 // XmGauge copyright notice:
51 * Copyright 1994 GROUPE BULL
53 * Permission to use, copy, modify, and distribute this software and its
54 * documentation for any purpose and without fee is hereby granted, provided
55 * that the above copyright notice appear in all copies and that both that
56 * copyright notice and this permission notice appear in supporting
57 * documentation, and that the name of GROUPE BULL not be used in advertising
58 * or publicity pertaining to distribution of the software without specific,
59 * written prior permission. GROUPE BULL makes no representations about the
60 * suitability of this software for any purpose. It is provided "as is"
61 * without express or implied warranty.
63 * GROUPE BULL disclaims all warranties with regard to this software,
64 * including all implied warranties of merchantability and fitness,
65 * in no event shall GROUPE BULL be liable for any special,
66 * indirect or consequential damages or any damages
67 * whatsoever resulting from loss of use, data or profits,
68 * whether in an action of contract, negligence or other tortious
69 * action, arising out of or in connection with the use
70 * or performance of this software.
74 //// PUBLIC XMGAUGE DECLARATIONS
75 typedef struct _XmGaugeClassRec
* XmGaugeWidgetClass
;
76 typedef struct _XmGaugeRec
* XmGaugeWidget
;
79 extern "C" WidgetClass xmGaugeWidgetClass
;
81 extern WidgetClass xmGaugeWidgetClass
;
84 typedef struct _XmGaugeCallbackStruct
{
88 } XmGaugeCallbackStruct
;
92 XmGaugeSetValue(Widget w
, int value
);
95 XmGaugeGetValue(Widget w
);
97 #endif // !wxCHECK_MOTIF_VERSION( 2, 0 ) || wxCHECK_LESSTIF()
99 bool wxGauge::Create(wxWindow
*parent
, wxWindowID id
,
104 const wxValidator
& validator
,
105 const wxString
& name
)
107 if( !CreateControl( parent
, id
, pos
, size
, style
, validator
, name
) )
111 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
115 if (style
& wxGA_HORIZONTAL
)
117 XtSetArg (args
[0], XmNorientation
, XmHORIZONTAL
);
118 XtSetArg (args
[1], XmNprocessingDirection
, XmMAX_ON_RIGHT
);
122 XtSetArg (args
[0], XmNorientation
, XmVERTICAL
);
123 XtSetArg (args
[1], XmNprocessingDirection
, XmMAX_ON_TOP
);
125 XtSetArg(args
[2], XmNminimum
, 0);
126 XtSetArg(args
[3], XmNmaximum
, range
);
127 #if wxCHECK_MOTIF_VERSION( 2, 0 ) && !wxCHECK_LESSTIF()
128 XtSetArg(args
[4], XmNeditable
, False
); ++count
;
129 XtSetArg(args
[5], XmNslidingMode
, XmTHERMOMETER
); ++count
;
130 // XtSetArg(args[6], XmNsliderVisual, XmFOREGROUND_COLOR ); ++count;
132 XtCreateManagedWidget("gauge", xmScaleWidgetClass
,
133 parentWidget
, args
, count
);
136 XtCreateManagedWidget("gauge", xmGaugeWidgetClass
,
137 parentWidget
, args
, count
);
139 m_mainWidget
= (WXWidget
) gaugeWidget
;
141 XtManageChild (gaugeWidget
);
143 int x
= pos
.x
; int y
= pos
.y
;
144 wxSize best
= GetBestSize();
145 if( size
.x
!= wxDefaultCoord
) best
.x
= size
.x
;
146 if( size
.y
!= wxDefaultCoord
) best
.y
= size
.y
;
149 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
, x
, y
,
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
,
517 (Dimension
)sht
, XmSHADOW_IN
);
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
;
676 GaugeDrag(Widget
WXUNUSED(w
), XEvent
*WXUNUSED(e
), String
*WXUNUSED(args
), Cardinal
*WXUNUSED(num_args
))
678 /* Commented out for a read-only gauge in wxWidgets */
680 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
681 #define THIS gw->gauge
682 int sht
, x
, y
, max
, value
;
683 float ratio
, nratio
, size
, nsize
, fvalue
, delta
;
684 XMotionEvent
*event
= (XMotionEvent
*)e
;
686 if( ! THIS
.dragging
) return;
690 sht
= gw
->primitive
.shadow_thickness
;
692 ratio
= (float)THIS
.value
/ (float)((float)THIS
.maximum
-
693 (float)THIS
.minimum
);
694 switch(THIS
.orientation
) {
696 max
= (w
->core
.width
- 2 * sht
);
697 size
= (float)max
* ratio
;
698 delta
= (float)x
- (float)THIS
.oldx
;
701 max
= (w
->core
.height
- 2 * sht
);
702 size
= (float) max
* ratio
;
703 delta
= (float)y
- (float)THIS
.oldy
;
706 switch(THIS
.processingDirection
) {
708 case XmMAX_ON_BOTTOM
:
709 nsize
= size
+ delta
;
712 nsize
= size
- delta
;
714 if(nsize
> (float)max
) nsize
= (float)max
;
715 if(nsize
< (float)0 ) nsize
= (float)0;
716 nratio
= nsize
/ (float)max
;
718 fvalue
= (int)((float)THIS
.maximum
-
719 (float)THIS
.minimum
) * (float)nsize
/ (float)max
;
720 value
= wxRound(fvalue
);
726 /* clear old slider only if it was larger */
727 DrawSlider(gw
, (nsize
< size
));
730 XmGaugeCallbackStruct call
;
732 if(NULL
!= THIS
.dragCallback
) {
733 call
.reason
= XmCR_DRAG
;
735 call
.value
= THIS
.value
;
736 XtCallCallbacks(w
, XmNdragCallback
, &call
);
745 GaugeDrop(Widget
WXUNUSED(w
), XEvent
*WXUNUSED(e
), String
*WXUNUSED(args
), Cardinal
*WXUNUSED(num_args
))
747 /* Commented out for a read-only gauge in wxWidgets */
749 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
750 #define THIS gw->gauge
751 if( ! THIS
.dragging
) return;
753 if(NULL
!= THIS
.valueChangedCallback
) {
754 XmGaugeCallbackStruct call
;
755 call
.reason
= XmCR_VALUE_CHANGED
;
757 call
.value
= THIS
.value
;
758 XtCallCallbacks(w
, XmNvalueChangedCallback
, &call
);
760 THIS
.dragging
= False
;
766 XmGaugeSetValue(Widget w
, int value
)
768 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
770 gw
->gauge
.value
= value
;
771 DrawSlider(gw
, True
);
772 XFlush(XtDisplay(w
));
776 XmGaugeGetValue(Widget w
)
778 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
780 return gw
->gauge
.value
;
783 #endif // !wxCHECK_MOTIF_VERSION( 2, 0 ) || wxCHECK_LESSTIF()