1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxGauge class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "gauge.h"
19 #include <wx/motif/private.h>
21 #if !USE_SHARED_LIBRARY
22 IMPLEMENT_DYNAMIC_CLASS(wxGauge
, wxControl
)
25 // XmGauge copyright notice:
28 * Copyright 1994 GROUPE BULL
30 * Permission to use, copy, modify, and distribute this software and its
31 * documentation for any purpose and without fee is hereby granted, provided
32 * that the above copyright notice appear in all copies and that both that
33 * copyright notice and this permission notice appear in supporting
34 * documentation, and that the name of GROUPE BULL not be used in advertising
35 * or publicity pertaining to distribution of the software without specific,
36 * written prior permission. GROUPE BULL makes no representations about the
37 * suitability of this software for any purpose. It is provided "as is"
38 * without express or implied warranty.
40 * GROUPE BULL disclaims all warranties with regard to this software,
41 * including all implied warranties of merchantability and fitness,
42 * in no event shall GROUPE BULL be liable for any special,
43 * indirect or consequential damages or any damages
44 * whatsoever resulting from loss of use, data or profits,
45 * whether in an action of contract, negligence or other tortious
46 * action, arising out of or in connection with the use
47 * or performance of this software.
51 //// PUBLIC XMGAUGE DECLARATIONS
52 typedef struct _XmGaugeClassRec
* XmGaugeWidgetClass
;
53 typedef struct _XmGaugeRec
* XmGaugeWidget
;
56 extern "C" WidgetClass xmGaugeWidgetClass
;
58 extern WidgetClass xmGaugeWidgetClass
;
61 typedef struct _XmGaugeCallbackStruct
{
65 } XmGaugeCallbackStruct
;
69 XmGaugeSetValue(Widget w
, int value
);
72 XmGaugeGetValue(Widget w
);
76 bool wxGauge::Create(wxWindow
*parent
, wxWindowID id
,
81 const wxValidator
& validator
,
85 SetValidator(validator
);
87 m_windowStyle
= style
;
89 if (parent
) parent
->AddChild(this);
92 m_windowId
= (int)NewControlId();
96 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
100 if (style
& wxHORIZONTAL
)
102 XtSetArg (args
[0], XmNorientation
, XmHORIZONTAL
);
103 XtSetArg (args
[1], XmNprocessingDirection
, XmMAX_ON_RIGHT
);
107 XtSetArg (args
[0], XmNorientation
, XmVERTICAL
);
108 XtSetArg (args
[1], XmNprocessingDirection
, XmMAX_ON_TOP
);
110 XtSetArg(args
[2], XmNminimum
, 0);
111 XtSetArg(args
[3], XmNmaximum
, range
);
112 Widget gaugeWidget
= XtCreateManagedWidget("gauge", xmGaugeWidgetClass
, parentWidget
, args
, count
);
113 m_mainWidget
= (WXWidget
) gaugeWidget
;
115 XtManageChild (gaugeWidget
);
117 int x
= pos
.x
; int y
= pos
.y
;
118 int width
= size
.x
; int height
= size
.y
;
124 SetCanAddEventHandler(TRUE
);
125 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
, x
, y
, width
, height
);
127 SetFont(* parent
->GetFont());
128 ChangeColour(m_mainWidget
);
133 void wxGauge::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
135 wxControl::SetSize(x
, y
, width
, height
, sizeFlags
);
138 void wxGauge::SetShadowWidth(int w
)
142 XtVaSetValues((Widget
) m_mainWidget
, XmNshadowThickness
, w
, NULL
);
145 void wxGauge::SetBezelFace(int w
)
149 void wxGauge::SetRange(int r
)
152 XtVaSetValues((Widget
) m_mainWidget
, XmNmaximum
, r
, NULL
);
155 void wxGauge::SetValue(int pos
)
158 XtVaSetValues((Widget
) m_mainWidget
, XmNvalue
, pos
, NULL
);
161 int wxGauge::GetShadowWidth() const
164 XtVaGetValues((Widget
) m_mainWidget
, XmNshadowThickness
, &w
, NULL
);
168 int wxGauge::GetBezelFace() const
173 int wxGauge::GetRange() const
176 XtVaGetValues((Widget
) m_mainWidget
, XmNmaximum
, &r
, NULL
);
178 // return m_rangeMax;
181 int wxGauge::GetValue() const
184 XtVaGetValues((Widget
) m_mainWidget
, XmNvalue
, &pos
, NULL
);
186 // return m_gaugePos;
189 //// PRIVATE DECLARATIONS FOR XMGAUGE
191 #include <Xm/PrimitiveP.h>
192 #include <Xm/DrawP.h>
198 typedef struct _XmGaugeClassRec
{
199 CoreClassPart core_class
;
200 XmPrimitiveClassPart primitive_class
;
201 XmGaugeClassPart gauge_class
;
205 typedef struct _XmGaugePart
{
209 unsigned char orientation
;
210 unsigned char processingDirection
;
212 XtCallbackList dragCallback
;
213 XtCallbackList valueChangedCallback
;
216 Boolean dragging
; /* drag in progress ? */
222 typedef struct _XmGaugeRec
{
224 XmPrimitivePart primitive
;
228 extern XmGaugeClassRec xmGaugeClassRec
;
230 /* Copyright 1994 GROUPE BULL -- See license conditions in file COPYRIGHT */
232 //// XMGAUGE IMPLEMENTATION
235 GaugePick(Widget w
, XEvent
*e
, String
*args
, Cardinal
*num_args
);
237 GaugeDrag(Widget w
, XEvent
*e
, String
*args
, Cardinal
*num_args
);
239 GaugeDrop(Widget w
, XEvent
*e
, String
*args
, Cardinal
*num_args
);
243 static char translations
[] =
244 "<Btn1Down>: GaugePick()\n\
245 <Btn1Motion>: GaugeDrag()\n\
246 <Btn1Up>: GaugeDrop()\n\
251 static XtActionsRec actions
[] = {
252 {"GaugePick", GaugePick
},
253 {"GaugeDrag", GaugeDrag
},
254 {"GaugeDrop", GaugeDrop
},
258 DrawSlider(XmGaugeWidget gw
, Boolean clear
)
260 #define THIS gw->gauge
266 unsigned long backgr,foregr;
270 sht
= gw
->primitive
.shadow_thickness
;
272 ratio
= (float)THIS
.value
/
273 (float)(THIS
.maximum
- THIS
.minimum
);
275 sprintf(string,"%-d%%",(int)(ratio*100));
277 XtVaGetValues(gw,XmNbackground,&backgr,XmNforeground,&foregr,NULL);
281 XClearArea(XtDisplay(gw
), XtWindow(gw
), sht
, sht
,
282 gw
->core
.width
- 2 * sht
, gw
->core
.height
- 2 * sht
, False
);
284 switch(THIS
.orientation
) {
286 size
= (int) ((gw
->core
.width
- 2 * sht
)*ratio
);
288 XDrawString(XtDisplay(gw), XtWindow(gw), THIS.gc, sht+gw->core.width/2,
289 gw->core.height - 2 * sht, string, len);
291 switch(THIS
.processingDirection
) {
293 case XmMAX_ON_BOTTOM
:
294 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
295 sht
, sht
, size
, gw
->core
.height
- 2 * sht
);
298 rects[0].x = sht; rects[0].y = sht;
299 rects[0].width = size; rects[0].height = gw->core.height - 2 * sht;
304 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
305 gw
->core
.width
- size
- sht
, sht
,
306 size
, gw
->core
.height
- 2 * sht
);
309 rects[0].x = gw->core.width - size - sht; rects[0].y = sht;
310 rects[0].width = size; rects[0].height = gw->core.height - 2 * sht;
315 XSetClipRectangles(XtDisplay(gw), THIS.gc, 0, 0, rects, 1, Unsorted);
316 XSetForeground(XtDisplay(gw), THIS.gc, backgr);
317 XDrawString(XtDisplay(gw), XtWindow(gw), THIS.gc, sht+gw->core.width/2,
318 gw->core.height - 2 * sht, string, len);
323 size
= (int) ((gw
->core
.height
- 2 * sht
)*ratio
);
325 XDrawString(XtDisplay(gw), XtWindow(gw), THIS.gc, sht,
326 sht+gw->core.height/2, string,len);
328 switch(THIS
.processingDirection
) {
330 case XmMAX_ON_BOTTOM
:
331 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
332 sht
, sht
, gw
->core
.width
- 2 * sht
, size
);
335 rects[0].x = sht; rects[0].y = sht;
336 rects[0].width = gw->core.width - 2 * sht; rects[0].height = size;
341 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
342 sht
, gw
->core
.height
- size
- sht
,
343 gw
->core
.width
- 2 * sht
, size
);
346 rects[0].x = sht; rects[0].y = gw->core.height - size - sht;
347 rects[0].width = gw->core.width - 2 * sht; rects[0].height = size;
351 XSetClipRectangles(XtDisplay(gw), THIS.gc, 0, 0, rects, 1, Unsorted);
352 XSetForeground(XtDisplay(gw), THIS.gc, backgr);
353 XDrawString(XtDisplay(gw), XtWindow(gw), THIS.gc, sht,
354 sht+gw->core.height/2, string,len);
359 XSetClipMask(XtDisplay(gw), THIS.gc, None);
360 XSetForeground(XtDisplay(gw), THIS.gc, foregr);
369 DrawSlider(XmGaugeWidget gw
, Boolean clear
)
371 #define THIS gw->gauge
375 sht
= gw
->primitive
.shadow_thickness
;
376 /* See fix comment below: can cause divide by zero error.
377 ratio = (float)((float)THIS.maximum -
378 (float)THIS.minimum) / (float)THIS.value;
381 XClearArea(XtDisplay(gw
), XtWindow(gw
), sht
, sht
,
382 gw
->core
.width
- 2 * sht
, gw
->core
.height
- 2 * sht
, False
);
384 switch(THIS
.orientation
) {
386 /* size = (gw->core.width - 2 * sht) / ratio; */
387 /* A fix suggested by Dmitri Chubraev */
388 size
= (gw
->core
.width
- 2 * sht
) /((float)THIS
.maximum
-(float)THIS
.minimum
)*(float)THIS
.value
;
389 switch(THIS
.processingDirection
) {
391 case XmMAX_ON_BOTTOM
:
392 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
393 sht
, sht
, size
, gw
->core
.height
- 2 * sht
);
397 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
398 gw
->core
.width
- size
- sht
, sht
,
399 size
, gw
->core
.height
- 2 * sht
);
404 size
= (gw
->core
.height
- 2 * sht
) /((float)THIS
.maximum
-(float)THIS
.minimum
)*(float)THIS
.value
;
405 /* size = (gw->core.height - 2 * sht)/ ratio; */
406 switch(THIS
.processingDirection
) {
408 case XmMAX_ON_BOTTOM
:
409 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
410 sht
, sht
, gw
->core
.width
- 2 * sht
, size
);
414 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
415 sht
, gw
->core
.height
- size
- sht
,
416 gw
->core
.width
- 2 * sht
, size
);
425 Initialize(Widget req
, Widget new_w
, ArgList args
, Cardinal
*num_args
)
427 XmGaugeWidget gw
= (XmGaugeWidget
)new_w
;
428 #define THIS gw->gauge
431 values
.foreground
= gw
->primitive
.foreground
;
432 THIS
.gc
= XtGetGC(new_w
, GCForeground
, &values
);
443 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
444 #define THIS gw->gauge
445 XtReleaseGC(w
, THIS
.gc
);
460 XmGaugeWidget cgw
= (XmGaugeWidget
)cw
;
461 XmGaugeWidget ngw
= (XmGaugeWidget
)nw
;
463 Boolean redraw
= False
;
464 if(cgw
->primitive
.foreground
!= ngw
->primitive
.foreground
) {
468 XtReleaseGC(nw
, ngw
->gauge
.gc
);
469 values
.foreground
= ngw
->primitive
.foreground
;
470 ngw
->gauge
.gc
= XtGetGC(nw
, GCForeground
, &values
);
472 if(cgw
->gauge
.value
!= ngw
->gauge
.value
) {
482 ExposeProc(Widget w
, XEvent
*event
, Region r
)
484 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
485 #define THIS gw->gauge
488 sht
= gw
->primitive
.shadow_thickness
;
489 _XmDrawShadows(XtDisplay(w
), XtWindow(w
),
490 gw
->primitive
.top_shadow_GC
,
491 gw
->primitive
.bottom_shadow_GC
,
492 0, 0, w
->core
.width
, w
->core
.height
,
494 DrawSlider(gw
, False
);
504 #define offset(field) XtOffset(XmGaugeWidget, gauge.field)
505 {XmNvalue
, XmCValue
, XtRInt
, sizeof(int),
506 offset(value
), XtRImmediate
, (caddr_t
)10},
508 {XmNminimum
, XmCValue
, XtRInt
, sizeof(int),
509 offset(minimum
), XtRImmediate
, (caddr_t
)0},
511 {XmNmaximum
, XmCValue
, XtRInt
, sizeof(int),
512 offset(maximum
), XtRImmediate
, (caddr_t
)100},
514 {XmNorientation
, XmCOrientation
, XmROrientation
, sizeof(unsigned char),
515 offset(orientation
), XtRImmediate
, (caddr_t
)XmVERTICAL
},
517 {XmNprocessingDirection
, XmCProcessingDirection
,
518 XmRProcessingDirection
, sizeof(unsigned char),
519 offset(processingDirection
), XtRImmediate
, (caddr_t
)XmMAX_ON_RIGHT
},
521 {XmNdragCallback
, XmCCallback
, XmRCallback
, sizeof(XtCallbackList
),
522 offset(dragCallback
), XtRImmediate
, (caddr_t
)NULL
},
524 {XmNvalueChangedCallback
, XmCCallback
, XmRCallback
, sizeof(XtCallbackList
),
525 offset(valueChangedCallback
), XtRImmediate
, (caddr_t
)NULL
},
532 XmGaugeClassRec xmGaugeClassRec
= {
534 (WidgetClass
) &xmPrimitiveClassRec
, /* superclass */
535 "XmGauge", /* class_name */
536 sizeof(XmGaugeRec
), /* widget_size */
537 NULL
, /* class_initialize */
538 NULL
, /* class_part_initialize */
539 FALSE
, /* class_inited */
540 Initialize
, /* initialize */
541 NULL
, /* initialize_hook */
542 XtInheritRealize
, /* realize */
543 actions
, /* actions */
544 XtNumber(actions
), /* num_actions */
545 resources
, /* resources */
546 XtNumber(resources
), /* num_resources */
547 NULLQUARK
, /* xrm_class */
548 TRUE
, /* compress_motion */
549 TRUE
, /* compress_exposure */
550 TRUE
, /* compress_enterleave */
551 FALSE
, /* visible_interest */
552 Destroy
, /* destroy */
554 ExposeProc
, /* expose */
555 SetValues
, /* set_values */
556 NULL
, /* set_values_hook */
557 XtInheritSetValuesAlmost
, /* set_values_almost */
558 NULL
, /* get_values_hook */
559 NULL
, /* accept_focus */
560 XtVersion
, /* version */
561 NULL
, /* callback_private */
562 translations
, /* tm_table */
563 NULL
, /* query_geometry */
564 NULL
, /* display_accelerator */
567 /* primitive_class fields */
569 NULL
, /* border_highlight */
570 NULL
, /* border_unhighlight */
571 NULL
, /* translations */
572 NULL
, /* arm_and_activate */
573 NULL
, /* syn_resources */
574 0, /* num_syn_resources */
582 WidgetClass xmGaugeWidgetClass
= (WidgetClass
)&xmGaugeClassRec
;
588 GaugePick(Widget w
, XEvent
*e
, String
*args
, Cardinal
*num_args
)
590 /* Commented out for a read-only gauge in wxWindows */
592 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
593 #define THIS gw->gauge
596 Boolean dragging
= False
;
597 XButtonEvent
*event
= (XButtonEvent
*)e
;
602 sht
= gw
->primitive
.shadow_thickness
;
603 _XmDrawShadows(XtDisplay(w
), XtWindow(w
),
604 gw
->primitive
.top_shadow_GC
,
605 gw
->primitive
.bottom_shadow_GC
,
606 0, 0, w
->core
.width
, w
->core
.height
,
610 ratio
= (float)((float)THIS
.maximum
-
611 (float)THIS
.minimum
) / (float)THIS
.value
;
612 switch(THIS
.orientation
) {
614 size
= (w
->core
.width
- 2 * sht
) / ratio
;
615 switch(THIS
.processingDirection
) {
617 case XmMAX_ON_BOTTOM
:
618 dragging
= (x
> sht
) && (y
> sht
) &&
619 (x
< sht
+ size
) && (y
< w
->core
.height
- sht
);
623 dragging
= (x
> w
->core
.width
- size
- sht
) && (y
> sht
) &&
624 (x
< w
->core
.width
- sht
) && (y
< w
->core
.height
+ sht
);
629 size
= (w
->core
.height
- 2 * sht
) / ratio
;
630 switch(THIS
.processingDirection
) {
632 case XmMAX_ON_BOTTOM
:
633 dragging
= (x
> sht
) && (y
> sht
) &&
634 (x
< w
->core
.width
- sht
) &&
635 (y
< w
->core
.width
- 2 * sht
+ size
);
639 dragging
= (x
> sht
) && (y
> w
->core
.height
- size
- sht
) &&
640 (x
< w
->core
.width
- sht
) && (y
< w
->core
.height
- sht
);
644 THIS
.dragging
= dragging
;
651 #define round(x) ( (x) > 0 ? ((x) + 0.5) : -(-(x) + 0.5) )
654 GaugeDrag(Widget w
, XEvent
*e
, String
*args
, Cardinal
*num_args
)
656 /* Commented out for a read-only gauge in wxWindows */
658 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
659 #define THIS gw->gauge
660 int sht
, x
, y
, max
, value
;
661 float ratio
, nratio
, size
, nsize
, fvalue
, delta
;
662 XMotionEvent
*event
= (XMotionEvent
*)e
;
664 if( ! THIS
.dragging
) return;
668 sht
= gw
->primitive
.shadow_thickness
;
670 ratio
= (float)THIS
.value
/ (float)((float)THIS
.maximum
-
671 (float)THIS
.minimum
);
672 switch(THIS
.orientation
) {
674 max
= (w
->core
.width
- 2 * sht
);
675 size
= (float)max
* ratio
;
676 delta
= (float)x
- (float)THIS
.oldx
;
679 max
= (w
->core
.height
- 2 * sht
);
680 size
= (float) max
* ratio
;
681 delta
= (float)y
- (float)THIS
.oldy
;
684 switch(THIS
.processingDirection
) {
686 case XmMAX_ON_BOTTOM
:
687 nsize
= size
+ delta
;
690 nsize
= size
- delta
;
692 if(nsize
> (float)max
) nsize
= (float)max
;
693 if(nsize
< (float)0 ) nsize
= (float)0;
694 nratio
= nsize
/ (float)max
;
696 fvalue
= (int)((float)THIS
.maximum
-
697 (float)THIS
.minimum
) * (float)nsize
/ (float)max
;
698 value
= round(fvalue
);
704 /* clear old slider only if it was larger */
705 DrawSlider(gw
, (nsize
< size
));
708 XmGaugeCallbackStruct call
;
710 if(NULL
!= THIS
.dragCallback
) {
711 call
.reason
= XmCR_DRAG
;
713 call
.value
= THIS
.value
;
714 XtCallCallbacks(w
, XmNdragCallback
, &call
);
723 GaugeDrop(Widget w
, XEvent
*e
, String
*args
, Cardinal
*num_args
)
725 /* Commented out for a read-only gauge in wxWindows */
727 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
728 #define THIS gw->gauge
729 if( ! THIS
.dragging
) return;
731 if(NULL
!= THIS
.valueChangedCallback
) {
732 XmGaugeCallbackStruct call
;
733 call
.reason
= XmCR_VALUE_CHANGED
;
735 call
.value
= THIS
.value
;
736 XtCallCallbacks(w
, XmNvalueChangedCallback
, &call
);
738 THIS
.dragging
= False
;
744 XmGaugeSetValue(Widget w
, int value
)
746 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
748 gw
->gauge
.value
= value
;
749 DrawSlider(gw
, True
);
750 XFlush(XtDisplay(w
));
754 XmGaugeGetValue(Widget w
)
756 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
758 return gw
->gauge
.value
;