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
;
88 m_backgroundColour
= parent
->GetBackgroundColour();
89 m_foregroundColour
= parent
->GetForegroundColour();
91 if (parent
) parent
->AddChild(this);
94 m_windowId
= (int)NewControlId();
98 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
102 if (style
& wxHORIZONTAL
)
104 XtSetArg (args
[0], XmNorientation
, XmHORIZONTAL
);
105 XtSetArg (args
[1], XmNprocessingDirection
, XmMAX_ON_RIGHT
);
109 XtSetArg (args
[0], XmNorientation
, XmVERTICAL
);
110 XtSetArg (args
[1], XmNprocessingDirection
, XmMAX_ON_TOP
);
112 XtSetArg(args
[2], XmNminimum
, 0);
113 XtSetArg(args
[3], XmNmaximum
, range
);
114 Widget gaugeWidget
= XtCreateManagedWidget("gauge", xmGaugeWidgetClass
, parentWidget
, args
, count
);
115 m_mainWidget
= (WXWidget
) gaugeWidget
;
117 XtManageChild (gaugeWidget
);
119 int x
= pos
.x
; int y
= pos
.y
;
120 int width
= size
.x
; int height
= size
.y
;
126 m_windowFont
= parent
->GetFont();
129 SetCanAddEventHandler(TRUE
);
130 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
, x
, y
, width
, height
);
132 ChangeBackgroundColour();
137 void wxGauge::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
139 wxControl::SetSize(x
, y
, width
, height
, sizeFlags
);
142 void wxGauge::SetShadowWidth(int w
)
146 XtVaSetValues((Widget
) m_mainWidget
, XmNshadowThickness
, w
, NULL
);
149 void wxGauge::SetBezelFace(int w
)
153 void wxGauge::SetRange(int r
)
156 XtVaSetValues((Widget
) m_mainWidget
, XmNmaximum
, r
, NULL
);
159 void wxGauge::SetValue(int pos
)
162 XtVaSetValues((Widget
) m_mainWidget
, XmNvalue
, pos
, NULL
);
165 int wxGauge::GetShadowWidth() const
168 XtVaGetValues((Widget
) m_mainWidget
, XmNshadowThickness
, &w
, NULL
);
172 int wxGauge::GetBezelFace() const
177 int wxGauge::GetRange() const
180 XtVaGetValues((Widget
) m_mainWidget
, XmNmaximum
, &r
, NULL
);
182 // return m_rangeMax;
185 int wxGauge::GetValue() const
188 XtVaGetValues((Widget
) m_mainWidget
, XmNvalue
, &pos
, NULL
);
190 // return m_gaugePos;
193 void wxGauge::ChangeFont(bool keepOriginalSize
)
195 wxWindow::ChangeFont(keepOriginalSize
);
198 void wxGauge::ChangeBackgroundColour()
200 wxWindow::ChangeBackgroundColour();
203 void wxGauge::ChangeForegroundColour()
205 wxWindow::ChangeForegroundColour();
208 //// PRIVATE DECLARATIONS FOR XMGAUGE
210 #include <Xm/PrimitiveP.h>
211 #include <Xm/DrawP.h>
217 typedef struct _XmGaugeClassRec
{
218 CoreClassPart core_class
;
219 XmPrimitiveClassPart primitive_class
;
220 XmGaugeClassPart gauge_class
;
224 typedef struct _XmGaugePart
{
228 unsigned char orientation
;
229 unsigned char processingDirection
;
231 XtCallbackList dragCallback
;
232 XtCallbackList valueChangedCallback
;
235 Boolean dragging
; /* drag in progress ? */
241 typedef struct _XmGaugeRec
{
243 XmPrimitivePart primitive
;
247 extern XmGaugeClassRec xmGaugeClassRec
;
249 /* Copyright 1994 GROUPE BULL -- See license conditions in file COPYRIGHT */
251 //// XMGAUGE IMPLEMENTATION
254 GaugePick(Widget w
, XEvent
*e
, String
*args
, Cardinal
*num_args
);
256 GaugeDrag(Widget w
, XEvent
*e
, String
*args
, Cardinal
*num_args
);
258 GaugeDrop(Widget w
, XEvent
*e
, String
*args
, Cardinal
*num_args
);
262 static char translations
[] =
263 "<Btn1Down>: GaugePick()\n\
264 <Btn1Motion>: GaugeDrag()\n\
265 <Btn1Up>: GaugeDrop()\n\
270 static XtActionsRec actions
[] = {
271 {"GaugePick", GaugePick
},
272 {"GaugeDrag", GaugeDrag
},
273 {"GaugeDrop", GaugeDrop
},
277 DrawSlider(XmGaugeWidget gw
, Boolean clear
)
279 #define THIS gw->gauge
285 unsigned long backgr,foregr;
289 sht
= gw
->primitive
.shadow_thickness
;
291 ratio
= (float)THIS
.value
/
292 (float)(THIS
.maximum
- THIS
.minimum
);
294 sprintf(string,"%-d%%",(int)(ratio*100));
296 XtVaGetValues(gw,XmNbackground,&backgr,XmNforeground,&foregr,NULL);
300 XClearArea(XtDisplay(gw
), XtWindow(gw
), sht
, sht
,
301 gw
->core
.width
- 2 * sht
, gw
->core
.height
- 2 * sht
, False
);
303 switch(THIS
.orientation
) {
305 size
= (int) ((gw
->core
.width
- 2 * sht
)*ratio
);
307 XDrawString(XtDisplay(gw), XtWindow(gw), THIS.gc, sht+gw->core.width/2,
308 gw->core.height - 2 * sht, string, len);
310 switch(THIS
.processingDirection
) {
312 case XmMAX_ON_BOTTOM
:
313 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
314 sht
, sht
, size
, gw
->core
.height
- 2 * sht
);
317 rects[0].x = sht; rects[0].y = sht;
318 rects[0].width = size; rects[0].height = gw->core.height - 2 * sht;
323 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
324 gw
->core
.width
- size
- sht
, sht
,
325 size
, gw
->core
.height
- 2 * sht
);
328 rects[0].x = gw->core.width - size - sht; rects[0].y = sht;
329 rects[0].width = size; rects[0].height = gw->core.height - 2 * sht;
334 XSetClipRectangles(XtDisplay(gw), THIS.gc, 0, 0, rects, 1, Unsorted);
335 XSetForeground(XtDisplay(gw), THIS.gc, backgr);
336 XDrawString(XtDisplay(gw), XtWindow(gw), THIS.gc, sht+gw->core.width/2,
337 gw->core.height - 2 * sht, string, len);
342 size
= (int) ((gw
->core
.height
- 2 * sht
)*ratio
);
344 XDrawString(XtDisplay(gw), XtWindow(gw), THIS.gc, sht,
345 sht+gw->core.height/2, string,len);
347 switch(THIS
.processingDirection
) {
349 case XmMAX_ON_BOTTOM
:
350 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
351 sht
, sht
, gw
->core
.width
- 2 * sht
, size
);
354 rects[0].x = sht; rects[0].y = sht;
355 rects[0].width = gw->core.width - 2 * sht; rects[0].height = size;
360 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
361 sht
, gw
->core
.height
- size
- sht
,
362 gw
->core
.width
- 2 * sht
, size
);
365 rects[0].x = sht; rects[0].y = gw->core.height - size - sht;
366 rects[0].width = gw->core.width - 2 * sht; rects[0].height = size;
370 XSetClipRectangles(XtDisplay(gw), THIS.gc, 0, 0, rects, 1, Unsorted);
371 XSetForeground(XtDisplay(gw), THIS.gc, backgr);
372 XDrawString(XtDisplay(gw), XtWindow(gw), THIS.gc, sht,
373 sht+gw->core.height/2, string,len);
378 XSetClipMask(XtDisplay(gw), THIS.gc, None);
379 XSetForeground(XtDisplay(gw), THIS.gc, foregr);
388 DrawSlider(XmGaugeWidget gw
, Boolean clear
)
390 #define THIS gw->gauge
394 sht
= gw
->primitive
.shadow_thickness
;
395 /* See fix comment below: can cause divide by zero error.
396 ratio = (float)((float)THIS.maximum -
397 (float)THIS.minimum) / (float)THIS.value;
400 XClearArea(XtDisplay(gw
), XtWindow(gw
), sht
, sht
,
401 gw
->core
.width
- 2 * sht
, gw
->core
.height
- 2 * sht
, False
);
403 switch(THIS
.orientation
) {
405 /* size = (gw->core.width - 2 * sht) / ratio; */
406 /* A fix suggested by Dmitri Chubraev */
407 size
= (gw
->core
.width
- 2 * sht
) /((float)THIS
.maximum
-(float)THIS
.minimum
)*(float)THIS
.value
;
408 switch(THIS
.processingDirection
) {
410 case XmMAX_ON_BOTTOM
:
411 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
412 sht
, sht
, size
, gw
->core
.height
- 2 * sht
);
416 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
417 gw
->core
.width
- size
- sht
, sht
,
418 size
, gw
->core
.height
- 2 * sht
);
423 size
= (gw
->core
.height
- 2 * sht
) /((float)THIS
.maximum
-(float)THIS
.minimum
)*(float)THIS
.value
;
424 /* size = (gw->core.height - 2 * sht)/ ratio; */
425 switch(THIS
.processingDirection
) {
427 case XmMAX_ON_BOTTOM
:
428 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
429 sht
, sht
, gw
->core
.width
- 2 * sht
, size
);
433 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
434 sht
, gw
->core
.height
- size
- sht
,
435 gw
->core
.width
- 2 * sht
, size
);
444 Initialize(Widget req
, Widget new_w
, ArgList args
, Cardinal
*num_args
)
446 XmGaugeWidget gw
= (XmGaugeWidget
)new_w
;
447 #define THIS gw->gauge
450 values
.foreground
= gw
->primitive
.foreground
;
451 THIS
.gc
= XtGetGC(new_w
, GCForeground
, &values
);
462 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
463 #define THIS gw->gauge
464 XtReleaseGC(w
, THIS
.gc
);
479 XmGaugeWidget cgw
= (XmGaugeWidget
)cw
;
480 XmGaugeWidget ngw
= (XmGaugeWidget
)nw
;
482 Boolean redraw
= False
;
483 if(cgw
->primitive
.foreground
!= ngw
->primitive
.foreground
) {
487 XtReleaseGC(nw
, ngw
->gauge
.gc
);
488 values
.foreground
= ngw
->primitive
.foreground
;
489 ngw
->gauge
.gc
= XtGetGC(nw
, GCForeground
, &values
);
491 if(cgw
->gauge
.value
!= ngw
->gauge
.value
) {
501 ExposeProc(Widget w
, XEvent
*event
, Region r
)
503 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
504 #define THIS gw->gauge
507 sht
= gw
->primitive
.shadow_thickness
;
508 _XmDrawShadows(XtDisplay(w
), XtWindow(w
),
509 gw
->primitive
.top_shadow_GC
,
510 gw
->primitive
.bottom_shadow_GC
,
511 0, 0, w
->core
.width
, w
->core
.height
,
513 DrawSlider(gw
, False
);
523 #define offset(field) XtOffset(XmGaugeWidget, gauge.field)
524 {XmNvalue
, XmCValue
, XtRInt
, sizeof(int),
525 offset(value
), XtRImmediate
, (caddr_t
)10},
527 {XmNminimum
, XmCValue
, XtRInt
, sizeof(int),
528 offset(minimum
), XtRImmediate
, (caddr_t
)0},
530 {XmNmaximum
, XmCValue
, XtRInt
, sizeof(int),
531 offset(maximum
), XtRImmediate
, (caddr_t
)100},
533 {XmNorientation
, XmCOrientation
, XmROrientation
, sizeof(unsigned char),
534 offset(orientation
), XtRImmediate
, (caddr_t
)XmVERTICAL
},
536 {XmNprocessingDirection
, XmCProcessingDirection
,
537 XmRProcessingDirection
, sizeof(unsigned char),
538 offset(processingDirection
), XtRImmediate
, (caddr_t
)XmMAX_ON_RIGHT
},
540 {XmNdragCallback
, XmCCallback
, XmRCallback
, sizeof(XtCallbackList
),
541 offset(dragCallback
), XtRImmediate
, (caddr_t
)NULL
},
543 {XmNvalueChangedCallback
, XmCCallback
, XmRCallback
, sizeof(XtCallbackList
),
544 offset(valueChangedCallback
), XtRImmediate
, (caddr_t
)NULL
},
551 XmGaugeClassRec xmGaugeClassRec
= {
553 (WidgetClass
) &xmPrimitiveClassRec
, /* superclass */
554 "XmGauge", /* class_name */
555 sizeof(XmGaugeRec
), /* widget_size */
556 NULL
, /* class_initialize */
557 NULL
, /* class_part_initialize */
558 FALSE
, /* class_inited */
559 Initialize
, /* initialize */
560 NULL
, /* initialize_hook */
561 XtInheritRealize
, /* realize */
562 actions
, /* actions */
563 XtNumber(actions
), /* num_actions */
564 resources
, /* resources */
565 XtNumber(resources
), /* num_resources */
566 NULLQUARK
, /* xrm_class */
567 TRUE
, /* compress_motion */
568 TRUE
, /* compress_exposure */
569 TRUE
, /* compress_enterleave */
570 FALSE
, /* visible_interest */
571 Destroy
, /* destroy */
573 ExposeProc
, /* expose */
574 SetValues
, /* set_values */
575 NULL
, /* set_values_hook */
576 XtInheritSetValuesAlmost
, /* set_values_almost */
577 NULL
, /* get_values_hook */
578 NULL
, /* accept_focus */
579 XtVersion
, /* version */
580 NULL
, /* callback_private */
581 translations
, /* tm_table */
582 NULL
, /* query_geometry */
583 NULL
, /* display_accelerator */
586 /* primitive_class fields */
588 NULL
, /* border_highlight */
589 NULL
, /* border_unhighlight */
590 NULL
, /* translations */
591 NULL
, /* arm_and_activate */
592 NULL
, /* syn_resources */
593 0, /* num_syn_resources */
601 WidgetClass xmGaugeWidgetClass
= (WidgetClass
)&xmGaugeClassRec
;
607 GaugePick(Widget w
, XEvent
*e
, String
*args
, Cardinal
*num_args
)
609 /* Commented out for a read-only gauge in wxWindows */
611 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
612 #define THIS gw->gauge
615 Boolean dragging
= False
;
616 XButtonEvent
*event
= (XButtonEvent
*)e
;
621 sht
= gw
->primitive
.shadow_thickness
;
622 _XmDrawShadows(XtDisplay(w
), XtWindow(w
),
623 gw
->primitive
.top_shadow_GC
,
624 gw
->primitive
.bottom_shadow_GC
,
625 0, 0, w
->core
.width
, w
->core
.height
,
629 ratio
= (float)((float)THIS
.maximum
-
630 (float)THIS
.minimum
) / (float)THIS
.value
;
631 switch(THIS
.orientation
) {
633 size
= (w
->core
.width
- 2 * sht
) / ratio
;
634 switch(THIS
.processingDirection
) {
636 case XmMAX_ON_BOTTOM
:
637 dragging
= (x
> sht
) && (y
> sht
) &&
638 (x
< sht
+ size
) && (y
< w
->core
.height
- sht
);
642 dragging
= (x
> w
->core
.width
- size
- sht
) && (y
> sht
) &&
643 (x
< w
->core
.width
- sht
) && (y
< w
->core
.height
+ sht
);
648 size
= (w
->core
.height
- 2 * sht
) / ratio
;
649 switch(THIS
.processingDirection
) {
651 case XmMAX_ON_BOTTOM
:
652 dragging
= (x
> sht
) && (y
> sht
) &&
653 (x
< w
->core
.width
- sht
) &&
654 (y
< w
->core
.width
- 2 * sht
+ size
);
658 dragging
= (x
> sht
) && (y
> w
->core
.height
- size
- sht
) &&
659 (x
< w
->core
.width
- sht
) && (y
< w
->core
.height
- sht
);
663 THIS
.dragging
= dragging
;
670 #define round(x) ( (x) > 0 ? ((x) + 0.5) : -(-(x) + 0.5) )
673 GaugeDrag(Widget w
, XEvent
*e
, String
*args
, Cardinal
*num_args
)
675 /* Commented out for a read-only gauge in wxWindows */
677 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
678 #define THIS gw->gauge
679 int sht
, x
, y
, max
, value
;
680 float ratio
, nratio
, size
, nsize
, fvalue
, delta
;
681 XMotionEvent
*event
= (XMotionEvent
*)e
;
683 if( ! THIS
.dragging
) return;
687 sht
= gw
->primitive
.shadow_thickness
;
689 ratio
= (float)THIS
.value
/ (float)((float)THIS
.maximum
-
690 (float)THIS
.minimum
);
691 switch(THIS
.orientation
) {
693 max
= (w
->core
.width
- 2 * sht
);
694 size
= (float)max
* ratio
;
695 delta
= (float)x
- (float)THIS
.oldx
;
698 max
= (w
->core
.height
- 2 * sht
);
699 size
= (float) max
* ratio
;
700 delta
= (float)y
- (float)THIS
.oldy
;
703 switch(THIS
.processingDirection
) {
705 case XmMAX_ON_BOTTOM
:
706 nsize
= size
+ delta
;
709 nsize
= size
- delta
;
711 if(nsize
> (float)max
) nsize
= (float)max
;
712 if(nsize
< (float)0 ) nsize
= (float)0;
713 nratio
= nsize
/ (float)max
;
715 fvalue
= (int)((float)THIS
.maximum
-
716 (float)THIS
.minimum
) * (float)nsize
/ (float)max
;
717 value
= round(fvalue
);
723 /* clear old slider only if it was larger */
724 DrawSlider(gw
, (nsize
< size
));
727 XmGaugeCallbackStruct call
;
729 if(NULL
!= THIS
.dragCallback
) {
730 call
.reason
= XmCR_DRAG
;
732 call
.value
= THIS
.value
;
733 XtCallCallbacks(w
, XmNdragCallback
, &call
);
742 GaugeDrop(Widget w
, XEvent
*e
, String
*args
, Cardinal
*num_args
)
744 /* Commented out for a read-only gauge in wxWindows */
746 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
747 #define THIS gw->gauge
748 if( ! THIS
.dragging
) return;
750 if(NULL
!= THIS
.valueChangedCallback
) {
751 XmGaugeCallbackStruct call
;
752 call
.reason
= XmCR_VALUE_CHANGED
;
754 call
.value
= THIS
.value
;
755 XtCallCallbacks(w
, XmNvalueChangedCallback
, &call
);
757 THIS
.dragging
= False
;
763 XmGaugeSetValue(Widget w
, int value
)
765 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
767 gw
->gauge
.value
= value
;
768 DrawSlider(gw
, True
);
769 XFlush(XtDisplay(w
));
773 XmGaugeGetValue(Widget w
)
775 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
777 return gw
->gauge
.value
;