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"
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 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
, x
, y
,
142 ChangeBackgroundColour();
147 wxSize
wxGauge::DoGetBestSize() const
149 if( HasFlag(wxGA_HORIZONTAL
) )
150 return wxSize( 100, 18 );
152 return wxSize( 18, 100 );
155 void wxGauge::SetShadowWidth(int w
)
159 XtVaSetValues((Widget
) m_mainWidget
, XmNshadowThickness
, w
, NULL
);
162 void wxGauge::SetRange(int r
)
164 XtVaSetValues((Widget
) m_mainWidget
, XmNmaximum
, r
, NULL
);
167 void wxGauge::SetValue(int pos
)
169 XtVaSetValues((Widget
) m_mainWidget
, XmNvalue
, pos
, NULL
);
172 int wxGauge::GetShadowWidth() const
175 XtVaGetValues((Widget
) m_mainWidget
, XmNshadowThickness
, &w
, NULL
);
179 int wxGauge::GetRange() const
182 XtVaGetValues((Widget
) m_mainWidget
, XmNmaximum
, &r
, NULL
);
186 int wxGauge::GetValue() const
189 XtVaGetValues((Widget
) m_mainWidget
, XmNvalue
, &pos
, NULL
);
193 void wxGauge::DoMoveWindow(int x
, int y
, int width
, int height
)
195 wxGaugeBase::DoMoveWindow( x
, y
, width
, height
);
197 XtVaSetValues( (Widget
)m_mainWidget
,
198 XmNscaleHeight
, height
,
203 #if !wxCHECK_MOTIF_VERSION( 2, 0 ) || wxCHECK_LESSTIF()
205 //// PRIVATE DECLARATIONS FOR XMGAUGE
207 #include <Xm/PrimitiveP.h>
208 #include <Xm/DrawP.h>
214 typedef struct _XmGaugeClassRec
{
215 CoreClassPart core_class
;
216 XmPrimitiveClassPart primitive_class
;
217 XmGaugeClassPart gauge_class
;
221 typedef struct _XmGaugePart
{
225 unsigned char orientation
;
226 unsigned char processingDirection
;
228 XtCallbackList dragCallback
;
229 XtCallbackList valueChangedCallback
;
232 Boolean dragging
; /* drag in progress ? */
238 typedef struct _XmGaugeRec
{
240 XmPrimitivePart primitive
;
244 extern XmGaugeClassRec xmGaugeClassRec
;
246 /* Copyright 1994 GROUPE BULL -- See license conditions in file COPYRIGHT */
248 //// XMGAUGE IMPLEMENTATION
251 GaugePick(Widget w
, XEvent
*e
, String
*args
, Cardinal
*num_args
);
253 GaugeDrag(Widget w
, XEvent
*e
, String
*args
, Cardinal
*num_args
);
255 GaugeDrop(Widget w
, XEvent
*e
, String
*args
, Cardinal
*num_args
);
259 static char translations
[] =
260 "<Btn1Down>: GaugePick()\n\
261 <Btn1Motion>: GaugeDrag()\n\
262 <Btn1Up>: GaugeDrop()\n\
267 static XtActionsRec actions
[] = {
268 {"GaugePick", GaugePick
},
269 {"GaugeDrag", GaugeDrag
},
270 {"GaugeDrop", GaugeDrop
},
274 DrawSlider(XmGaugeWidget gw
, Boolean clear
)
276 #define THIS gw->gauge
282 unsigned long backgr,foregr;
286 sht
= gw
->primitive
.shadow_thickness
;
288 ratio
= (float)THIS
.value
/
289 (float)(THIS
.maximum
- THIS
.minimum
);
291 sprintf(string,"%-d%%",(int)(ratio*100));
293 XtVaGetValues(gw,XmNbackground,&backgr,XmNforeground,&foregr,NULL);
297 XClearArea(XtDisplay(gw
), XtWindow(gw
), sht
, sht
,
298 gw
->core
.width
- 2 * sht
, gw
->core
.height
- 2 * sht
, False
);
300 switch(THIS
.orientation
) {
302 size
= (int) ((gw
->core
.width
- 2 * sht
)*ratio
);
304 XDrawString(XtDisplay(gw), XtWindow(gw), THIS.gc, sht+gw->core.width/2,
305 gw->core.height - 2 * sht, string, len);
307 switch(THIS
.processingDirection
) {
309 case XmMAX_ON_BOTTOM
:
310 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
311 sht
, sht
, size
, gw
->core
.height
- 2 * sht
);
314 rects[0].x = sht; rects[0].y = sht;
315 rects[0].width = size; rects[0].height = gw->core.height - 2 * sht;
320 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
321 gw
->core
.width
- size
- sht
, sht
,
322 size
, gw
->core
.height
- 2 * sht
);
325 rects[0].x = gw->core.width - size - sht; rects[0].y = sht;
326 rects[0].width = size; rects[0].height = gw->core.height - 2 * sht;
331 XSetClipRectangles(XtDisplay(gw), THIS.gc, 0, 0, rects, 1, Unsorted);
332 XSetForeground(XtDisplay(gw), THIS.gc, backgr);
333 XDrawString(XtDisplay(gw), XtWindow(gw), THIS.gc, sht+gw->core.width/2,
334 gw->core.height - 2 * sht, string, len);
339 size
= (int) ((gw
->core
.height
- 2 * sht
)*ratio
);
341 XDrawString(XtDisplay(gw), XtWindow(gw), THIS.gc, sht,
342 sht+gw->core.height/2, string,len);
344 switch(THIS
.processingDirection
) {
346 case XmMAX_ON_BOTTOM
:
347 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
348 sht
, sht
, gw
->core
.width
- 2 * sht
, size
);
351 rects[0].x = sht; rects[0].y = sht;
352 rects[0].width = gw->core.width - 2 * sht; rects[0].height = size;
357 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
358 sht
, gw
->core
.height
- size
- sht
,
359 gw
->core
.width
- 2 * sht
, size
);
362 rects[0].x = sht; rects[0].y = gw->core.height - size - sht;
363 rects[0].width = gw->core.width - 2 * sht; rects[0].height = size;
367 XSetClipRectangles(XtDisplay(gw), THIS.gc, 0, 0, rects, 1, Unsorted);
368 XSetForeground(XtDisplay(gw), THIS.gc, backgr);
369 XDrawString(XtDisplay(gw), XtWindow(gw), THIS.gc, sht,
370 sht+gw->core.height/2, string,len);
375 XSetClipMask(XtDisplay(gw), THIS.gc, None);
376 XSetForeground(XtDisplay(gw), THIS.gc, foregr);
385 DrawSlider(XmGaugeWidget gw
, Boolean clear
)
387 #define THIS gw->gauge
391 sht
= gw
->primitive
.shadow_thickness
;
392 /* See fix comment below: can cause divide by zero error.
393 ratio = (float)((float)THIS.maximum -
394 (float)THIS.minimum) / (float)THIS.value;
397 XClearArea(XtDisplay(gw
), XtWindow(gw
), sht
, sht
,
398 gw
->core
.width
- 2 * sht
, gw
->core
.height
- 2 * sht
, False
);
400 switch(THIS
.orientation
) {
402 /* size = (gw->core.width - 2 * sht) / ratio; */
403 /* A fix suggested by Dmitri Chubraev */
404 size
= (gw
->core
.width
- 2 * sht
) /((float)THIS
.maximum
-(float)THIS
.minimum
)*(float)THIS
.value
;
405 switch(THIS
.processingDirection
) {
407 case XmMAX_ON_BOTTOM
:
408 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
409 sht
, sht
, size
, gw
->core
.height
- 2 * sht
);
413 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
414 gw
->core
.width
- size
- sht
, sht
,
415 size
, gw
->core
.height
- 2 * sht
);
420 size
= (gw
->core
.height
- 2 * sht
) /((float)THIS
.maximum
-(float)THIS
.minimum
)*(float)THIS
.value
;
421 /* size = (gw->core.height - 2 * sht)/ ratio; */
422 switch(THIS
.processingDirection
) {
424 case XmMAX_ON_BOTTOM
:
425 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
426 sht
, sht
, gw
->core
.width
- 2 * sht
, size
);
430 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
431 sht
, gw
->core
.height
- size
- sht
,
432 gw
->core
.width
- 2 * sht
, size
);
441 Initialize(Widget
WXUNUSED(req
), Widget new_w
, ArgList
WXUNUSED(args
), Cardinal
*WXUNUSED(num_args
))
443 XmGaugeWidget gw
= (XmGaugeWidget
)new_w
;
444 #define THIS gw->gauge
447 values
.foreground
= gw
->primitive
.foreground
;
448 THIS
.gc
= XtGetGC(new_w
, GCForeground
, &values
);
459 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
460 #define THIS gw->gauge
461 XtReleaseGC(w
, THIS
.gc
);
473 ArgList
WXUNUSED(args
),
474 Cardinal
*WXUNUSED(num_args
) )
476 XmGaugeWidget cgw
= (XmGaugeWidget
)cw
;
477 XmGaugeWidget ngw
= (XmGaugeWidget
)nw
;
479 Boolean redraw
= False
;
480 if(cgw
->primitive
.foreground
!= ngw
->primitive
.foreground
) {
484 XtReleaseGC(nw
, ngw
->gauge
.gc
);
485 values
.foreground
= ngw
->primitive
.foreground
;
486 ngw
->gauge
.gc
= XtGetGC(nw
, GCForeground
, &values
);
488 if(cgw
->gauge
.value
!= ngw
->gauge
.value
) {
498 ExposeProc(Widget w
, XEvent
*WXUNUSED(event
), Region
WXUNUSED(r
))
500 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
501 #define THIS gw->gauge
504 sht
= gw
->primitive
.shadow_thickness
;
505 _XmDrawShadows(XtDisplay(w
), XtWindow(w
),
506 gw
->primitive
.top_shadow_GC
,
507 gw
->primitive
.bottom_shadow_GC
,
508 0, 0, w
->core
.width
, w
->core
.height
,
510 DrawSlider(gw
, False
);
520 #define offset(field) XtOffset(XmGaugeWidget, gauge.field)
521 {XmNvalue
, XmCValue
, XtRInt
, sizeof(int),
522 offset(value
), XtRImmediate
, (caddr_t
)10},
524 {XmNminimum
, XmCValue
, XtRInt
, sizeof(int),
525 offset(minimum
), XtRImmediate
, (caddr_t
)0},
527 {XmNmaximum
, XmCValue
, XtRInt
, sizeof(int),
528 offset(maximum
), XtRImmediate
, (caddr_t
)100},
530 {XmNorientation
, XmCOrientation
, XmROrientation
, sizeof(unsigned char),
531 offset(orientation
), XtRImmediate
, (caddr_t
)XmVERTICAL
},
533 {XmNprocessingDirection
, XmCProcessingDirection
,
534 XmRProcessingDirection
, sizeof(unsigned char),
535 offset(processingDirection
), XtRImmediate
, (caddr_t
)XmMAX_ON_RIGHT
},
537 {XmNdragCallback
, XmCCallback
, XmRCallback
, sizeof(XtCallbackList
),
538 offset(dragCallback
), XtRImmediate
, (caddr_t
)NULL
},
540 {XmNvalueChangedCallback
, XmCCallback
, XmRCallback
, sizeof(XtCallbackList
),
541 offset(valueChangedCallback
), XtRImmediate
, (caddr_t
)NULL
},
548 XmGaugeClassRec xmGaugeClassRec
= {
550 (WidgetClass
) &xmPrimitiveClassRec
, /* superclass */
551 "XmGauge", /* class_name */
552 sizeof(XmGaugeRec
), /* widget_size */
553 NULL
, /* class_initialize */
554 NULL
, /* class_part_initialize */
555 FALSE
, /* class_inited */
556 Initialize
, /* initialize */
557 NULL
, /* initialize_hook */
558 XtInheritRealize
, /* realize */
559 actions
, /* actions */
560 XtNumber(actions
), /* num_actions */
561 resources
, /* resources */
562 XtNumber(resources
), /* num_resources */
563 NULLQUARK
, /* xrm_class */
564 TRUE
, /* compress_motion */
565 TRUE
, /* compress_exposure */
566 TRUE
, /* compress_enterleave */
567 FALSE
, /* visible_interest */
568 Destroy
, /* destroy */
570 ExposeProc
, /* expose */
571 SetValues
, /* set_values */
572 NULL
, /* set_values_hook */
573 XtInheritSetValuesAlmost
, /* set_values_almost */
574 NULL
, /* get_values_hook */
575 NULL
, /* accept_focus */
576 XtVersion
, /* version */
577 NULL
, /* callback_private */
578 translations
, /* tm_table */
579 NULL
, /* query_geometry */
580 NULL
, /* display_accelerator */
583 /* primitive_class fields */
585 NULL
, /* border_highlight */
586 NULL
, /* border_unhighlight */
587 NULL
, /* translations */
588 NULL
, /* arm_and_activate */
589 NULL
, /* syn_resources */
590 0, /* num_syn_resources */
598 WidgetClass xmGaugeWidgetClass
= (WidgetClass
)&xmGaugeClassRec
;
604 GaugePick(Widget
WXUNUSED(w
), XEvent
*WXUNUSED(e
), String
*WXUNUSED(args
), Cardinal
*WXUNUSED(num_args
))
606 /* Commented out for a read-only gauge in wxWindows */
608 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
609 #define THIS gw->gauge
612 Boolean dragging
= False
;
613 XButtonEvent
*event
= (XButtonEvent
*)e
;
618 sht
= gw
->primitive
.shadow_thickness
;
619 _XmDrawShadows(XtDisplay(w
), XtWindow(w
),
620 gw
->primitive
.top_shadow_GC
,
621 gw
->primitive
.bottom_shadow_GC
,
622 0, 0, w
->core
.width
, w
->core
.height
,
626 ratio
= (float)((float)THIS
.maximum
-
627 (float)THIS
.minimum
) / (float)THIS
.value
;
628 switch(THIS
.orientation
) {
630 size
= (w
->core
.width
- 2 * sht
) / ratio
;
631 switch(THIS
.processingDirection
) {
633 case XmMAX_ON_BOTTOM
:
634 dragging
= (x
> sht
) && (y
> sht
) &&
635 (x
< sht
+ size
) && (y
< w
->core
.height
- sht
);
639 dragging
= (x
> w
->core
.width
- size
- sht
) && (y
> sht
) &&
640 (x
< w
->core
.width
- sht
) && (y
< w
->core
.height
+ sht
);
645 size
= (w
->core
.height
- 2 * sht
) / ratio
;
646 switch(THIS
.processingDirection
) {
648 case XmMAX_ON_BOTTOM
:
649 dragging
= (x
> sht
) && (y
> sht
) &&
650 (x
< w
->core
.width
- sht
) &&
651 (y
< w
->core
.width
- 2 * sht
+ size
);
655 dragging
= (x
> sht
) && (y
> w
->core
.height
- size
- sht
) &&
656 (x
< w
->core
.width
- sht
) && (y
< w
->core
.height
- sht
);
660 THIS
.dragging
= dragging
;
667 #define round(x) ( (x) > 0 ? ((x) + 0.5) : -(-(x) + 0.5) )
670 GaugeDrag(Widget
WXUNUSED(w
), XEvent
*WXUNUSED(e
), String
*WXUNUSED(args
), Cardinal
*WXUNUSED(num_args
))
672 /* Commented out for a read-only gauge in wxWindows */
674 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
675 #define THIS gw->gauge
676 int sht
, x
, y
, max
, value
;
677 float ratio
, nratio
, size
, nsize
, fvalue
, delta
;
678 XMotionEvent
*event
= (XMotionEvent
*)e
;
680 if( ! THIS
.dragging
) return;
684 sht
= gw
->primitive
.shadow_thickness
;
686 ratio
= (float)THIS
.value
/ (float)((float)THIS
.maximum
-
687 (float)THIS
.minimum
);
688 switch(THIS
.orientation
) {
690 max
= (w
->core
.width
- 2 * sht
);
691 size
= (float)max
* ratio
;
692 delta
= (float)x
- (float)THIS
.oldx
;
695 max
= (w
->core
.height
- 2 * sht
);
696 size
= (float) max
* ratio
;
697 delta
= (float)y
- (float)THIS
.oldy
;
700 switch(THIS
.processingDirection
) {
702 case XmMAX_ON_BOTTOM
:
703 nsize
= size
+ delta
;
706 nsize
= size
- delta
;
708 if(nsize
> (float)max
) nsize
= (float)max
;
709 if(nsize
< (float)0 ) nsize
= (float)0;
710 nratio
= nsize
/ (float)max
;
712 fvalue
= (int)((float)THIS
.maximum
-
713 (float)THIS
.minimum
) * (float)nsize
/ (float)max
;
714 value
= round(fvalue
);
720 /* clear old slider only if it was larger */
721 DrawSlider(gw
, (nsize
< size
));
724 XmGaugeCallbackStruct call
;
726 if(NULL
!= THIS
.dragCallback
) {
727 call
.reason
= XmCR_DRAG
;
729 call
.value
= THIS
.value
;
730 XtCallCallbacks(w
, XmNdragCallback
, &call
);
739 GaugeDrop(Widget
WXUNUSED(w
), XEvent
*WXUNUSED(e
), String
*WXUNUSED(args
), Cardinal
*WXUNUSED(num_args
))
741 /* Commented out for a read-only gauge in wxWindows */
743 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
744 #define THIS gw->gauge
745 if( ! THIS
.dragging
) return;
747 if(NULL
!= THIS
.valueChangedCallback
) {
748 XmGaugeCallbackStruct call
;
749 call
.reason
= XmCR_VALUE_CHANGED
;
751 call
.value
= THIS
.value
;
752 XtCallCallbacks(w
, XmNvalueChangedCallback
, &call
);
754 THIS
.dragging
= False
;
760 XmGaugeSetValue(Widget w
, int value
)
762 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
764 gw
->gauge
.value
= value
;
765 DrawSlider(gw
, True
);
766 XFlush(XtDisplay(w
));
770 XmGaugeGetValue(Widget w
)
772 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
774 return gw
->gauge
.value
;
777 #endif // !wxCHECK_MOTIF_VERSION( 2, 0 ) || wxCHECK_LESSTIF()