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 #pragma message disable nosimpint
23 #pragma message enable nosimpint
25 #include "wx/motif/private.h"
27 IMPLEMENT_DYNAMIC_CLASS(wxGauge
, wxControl
)
29 // XmGauge copyright notice:
32 * Copyright 1994 GROUPE BULL
34 * Permission to use, copy, modify, and distribute this software and its
35 * documentation for any purpose and without fee is hereby granted, provided
36 * that the above copyright notice appear in all copies and that both that
37 * copyright notice and this permission notice appear in supporting
38 * documentation, and that the name of GROUPE BULL not be used in advertising
39 * or publicity pertaining to distribution of the software without specific,
40 * written prior permission. GROUPE BULL makes no representations about the
41 * suitability of this software for any purpose. It is provided "as is"
42 * without express or implied warranty.
44 * GROUPE BULL disclaims all warranties with regard to this software,
45 * including all implied warranties of merchantability and fitness,
46 * in no event shall GROUPE BULL be liable for any special,
47 * indirect or consequential damages or any damages
48 * whatsoever resulting from loss of use, data or profits,
49 * whether in an action of contract, negligence or other tortious
50 * action, arising out of or in connection with the use
51 * or performance of this software.
55 //// PUBLIC XMGAUGE DECLARATIONS
56 typedef struct _XmGaugeClassRec
* XmGaugeWidgetClass
;
57 typedef struct _XmGaugeRec
* XmGaugeWidget
;
60 extern "C" WidgetClass xmGaugeWidgetClass
;
62 extern WidgetClass xmGaugeWidgetClass
;
65 typedef struct _XmGaugeCallbackStruct
{
69 } XmGaugeCallbackStruct
;
73 XmGaugeSetValue(Widget w
, int value
);
76 XmGaugeGetValue(Widget w
);
80 bool wxGauge::Create(wxWindow
*parent
, wxWindowID id
,
85 const wxValidator
& validator
,
89 SetValidator(validator
);
91 m_windowStyle
= style
;
92 m_backgroundColour
= parent
->GetBackgroundColour();
93 m_foregroundColour
= parent
->GetForegroundColour();
95 if (parent
) parent
->AddChild(this);
98 m_windowId
= (int)NewControlId();
102 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
106 if (style
& wxHORIZONTAL
)
108 XtSetArg (args
[0], XmNorientation
, XmHORIZONTAL
);
109 XtSetArg (args
[1], XmNprocessingDirection
, XmMAX_ON_RIGHT
);
113 XtSetArg (args
[0], XmNorientation
, XmVERTICAL
);
114 XtSetArg (args
[1], XmNprocessingDirection
, XmMAX_ON_TOP
);
116 XtSetArg(args
[2], XmNminimum
, 0);
117 XtSetArg(args
[3], XmNmaximum
, range
);
118 Widget gaugeWidget
= XtCreateManagedWidget("gauge", xmGaugeWidgetClass
, parentWidget
, args
, count
);
119 m_mainWidget
= (WXWidget
) gaugeWidget
;
121 XtManageChild (gaugeWidget
);
123 int x
= pos
.x
; int y
= pos
.y
;
124 int width
= size
.x
; int height
= size
.y
;
130 m_font
= parent
->GetFont();
133 SetCanAddEventHandler(TRUE
);
134 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
, x
, y
, width
, height
);
136 ChangeBackgroundColour();
141 void wxGauge::SetShadowWidth(int w
)
145 XtVaSetValues((Widget
) m_mainWidget
, XmNshadowThickness
, w
, NULL
);
148 void wxGauge::SetBezelFace(int WXUNUSED(w
))
152 void wxGauge::SetRange(int r
)
155 XtVaSetValues((Widget
) m_mainWidget
, XmNmaximum
, r
, NULL
);
158 void wxGauge::SetValue(int pos
)
161 XtVaSetValues((Widget
) m_mainWidget
, XmNvalue
, pos
, NULL
);
164 int wxGauge::GetShadowWidth() const
167 XtVaGetValues((Widget
) m_mainWidget
, XmNshadowThickness
, &w
, NULL
);
171 int wxGauge::GetBezelFace() const
176 int wxGauge::GetRange() const
179 XtVaGetValues((Widget
) m_mainWidget
, XmNmaximum
, &r
, NULL
);
181 // return m_rangeMax;
184 int wxGauge::GetValue() const
187 XtVaGetValues((Widget
) m_mainWidget
, XmNvalue
, &pos
, NULL
);
189 // return m_gaugePos;
192 void wxGauge::ChangeFont(bool keepOriginalSize
)
194 wxWindow::ChangeFont(keepOriginalSize
);
197 void wxGauge::ChangeBackgroundColour()
199 wxWindow::ChangeBackgroundColour();
202 void wxGauge::ChangeForegroundColour()
204 wxWindow::ChangeForegroundColour();
207 //// PRIVATE DECLARATIONS FOR XMGAUGE
209 #include <Xm/PrimitiveP.h>
210 #include <Xm/DrawP.h>
216 typedef struct _XmGaugeClassRec
{
217 CoreClassPart core_class
;
218 XmPrimitiveClassPart primitive_class
;
219 XmGaugeClassPart gauge_class
;
223 typedef struct _XmGaugePart
{
227 unsigned char orientation
;
228 unsigned char processingDirection
;
230 XtCallbackList dragCallback
;
231 XtCallbackList valueChangedCallback
;
234 Boolean dragging
; /* drag in progress ? */
240 typedef struct _XmGaugeRec
{
242 XmPrimitivePart primitive
;
246 extern XmGaugeClassRec xmGaugeClassRec
;
248 /* Copyright 1994 GROUPE BULL -- See license conditions in file COPYRIGHT */
250 //// XMGAUGE IMPLEMENTATION
253 GaugePick(Widget w
, XEvent
*e
, String
*args
, Cardinal
*num_args
);
255 GaugeDrag(Widget w
, XEvent
*e
, String
*args
, Cardinal
*num_args
);
257 GaugeDrop(Widget w
, XEvent
*e
, String
*args
, Cardinal
*num_args
);
261 static char translations
[] =
262 "<Btn1Down>: GaugePick()\n\
263 <Btn1Motion>: GaugeDrag()\n\
264 <Btn1Up>: GaugeDrop()\n\
269 static XtActionsRec actions
[] = {
270 {"GaugePick", GaugePick
},
271 {"GaugeDrag", GaugeDrag
},
272 {"GaugeDrop", GaugeDrop
},
276 DrawSlider(XmGaugeWidget gw
, Boolean clear
)
278 #define THIS gw->gauge
284 unsigned long backgr,foregr;
288 sht
= gw
->primitive
.shadow_thickness
;
290 ratio
= (float)THIS
.value
/
291 (float)(THIS
.maximum
- THIS
.minimum
);
293 sprintf(string,"%-d%%",(int)(ratio*100));
295 XtVaGetValues(gw,XmNbackground,&backgr,XmNforeground,&foregr,NULL);
299 XClearArea(XtDisplay(gw
), XtWindow(gw
), sht
, sht
,
300 gw
->core
.width
- 2 * sht
, gw
->core
.height
- 2 * sht
, False
);
302 switch(THIS
.orientation
) {
304 size
= (int) ((gw
->core
.width
- 2 * sht
)*ratio
);
306 XDrawString(XtDisplay(gw), XtWindow(gw), THIS.gc, sht+gw->core.width/2,
307 gw->core.height - 2 * sht, string, len);
309 switch(THIS
.processingDirection
) {
311 case XmMAX_ON_BOTTOM
:
312 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
313 sht
, sht
, size
, gw
->core
.height
- 2 * sht
);
316 rects[0].x = sht; rects[0].y = sht;
317 rects[0].width = size; rects[0].height = gw->core.height - 2 * sht;
322 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
323 gw
->core
.width
- size
- sht
, sht
,
324 size
, gw
->core
.height
- 2 * sht
);
327 rects[0].x = gw->core.width - size - sht; rects[0].y = sht;
328 rects[0].width = size; rects[0].height = gw->core.height - 2 * sht;
333 XSetClipRectangles(XtDisplay(gw), THIS.gc, 0, 0, rects, 1, Unsorted);
334 XSetForeground(XtDisplay(gw), THIS.gc, backgr);
335 XDrawString(XtDisplay(gw), XtWindow(gw), THIS.gc, sht+gw->core.width/2,
336 gw->core.height - 2 * sht, string, len);
341 size
= (int) ((gw
->core
.height
- 2 * sht
)*ratio
);
343 XDrawString(XtDisplay(gw), XtWindow(gw), THIS.gc, sht,
344 sht+gw->core.height/2, string,len);
346 switch(THIS
.processingDirection
) {
348 case XmMAX_ON_BOTTOM
:
349 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
350 sht
, sht
, gw
->core
.width
- 2 * sht
, size
);
353 rects[0].x = sht; rects[0].y = sht;
354 rects[0].width = gw->core.width - 2 * sht; rects[0].height = size;
359 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
360 sht
, gw
->core
.height
- size
- sht
,
361 gw
->core
.width
- 2 * sht
, size
);
364 rects[0].x = sht; rects[0].y = gw->core.height - size - sht;
365 rects[0].width = gw->core.width - 2 * sht; rects[0].height = size;
369 XSetClipRectangles(XtDisplay(gw), THIS.gc, 0, 0, rects, 1, Unsorted);
370 XSetForeground(XtDisplay(gw), THIS.gc, backgr);
371 XDrawString(XtDisplay(gw), XtWindow(gw), THIS.gc, sht,
372 sht+gw->core.height/2, string,len);
377 XSetClipMask(XtDisplay(gw), THIS.gc, None);
378 XSetForeground(XtDisplay(gw), THIS.gc, foregr);
387 DrawSlider(XmGaugeWidget gw
, Boolean clear
)
389 #define THIS gw->gauge
393 sht
= gw
->primitive
.shadow_thickness
;
394 /* See fix comment below: can cause divide by zero error.
395 ratio = (float)((float)THIS.maximum -
396 (float)THIS.minimum) / (float)THIS.value;
399 XClearArea(XtDisplay(gw
), XtWindow(gw
), sht
, sht
,
400 gw
->core
.width
- 2 * sht
, gw
->core
.height
- 2 * sht
, False
);
402 switch(THIS
.orientation
) {
404 /* size = (gw->core.width - 2 * sht) / ratio; */
405 /* A fix suggested by Dmitri Chubraev */
406 size
= (gw
->core
.width
- 2 * sht
) /((float)THIS
.maximum
-(float)THIS
.minimum
)*(float)THIS
.value
;
407 switch(THIS
.processingDirection
) {
409 case XmMAX_ON_BOTTOM
:
410 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
411 sht
, sht
, size
, gw
->core
.height
- 2 * sht
);
415 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
416 gw
->core
.width
- size
- sht
, sht
,
417 size
, gw
->core
.height
- 2 * sht
);
422 size
= (gw
->core
.height
- 2 * sht
) /((float)THIS
.maximum
-(float)THIS
.minimum
)*(float)THIS
.value
;
423 /* size = (gw->core.height - 2 * sht)/ ratio; */
424 switch(THIS
.processingDirection
) {
426 case XmMAX_ON_BOTTOM
:
427 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
428 sht
, sht
, gw
->core
.width
- 2 * sht
, size
);
432 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
433 sht
, gw
->core
.height
- size
- sht
,
434 gw
->core
.width
- 2 * sht
, size
);
443 Initialize(Widget
WXUNUSED(req
), Widget new_w
, ArgList
WXUNUSED(args
), Cardinal
*WXUNUSED(num_args
))
445 XmGaugeWidget gw
= (XmGaugeWidget
)new_w
;
446 #define THIS gw->gauge
449 values
.foreground
= gw
->primitive
.foreground
;
450 THIS
.gc
= XtGetGC(new_w
, GCForeground
, &values
);
461 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
462 #define THIS gw->gauge
463 XtReleaseGC(w
, THIS
.gc
);
475 ArgList
WXUNUSED(args
),
476 Cardinal
*WXUNUSED(num_args
) )
478 XmGaugeWidget cgw
= (XmGaugeWidget
)cw
;
479 XmGaugeWidget ngw
= (XmGaugeWidget
)nw
;
481 Boolean redraw
= False
;
482 if(cgw
->primitive
.foreground
!= ngw
->primitive
.foreground
) {
486 XtReleaseGC(nw
, ngw
->gauge
.gc
);
487 values
.foreground
= ngw
->primitive
.foreground
;
488 ngw
->gauge
.gc
= XtGetGC(nw
, GCForeground
, &values
);
490 if(cgw
->gauge
.value
!= ngw
->gauge
.value
) {
500 ExposeProc(Widget w
, XEvent
*WXUNUSED(event
), Region
WXUNUSED(r
))
502 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
503 #define THIS gw->gauge
506 sht
= gw
->primitive
.shadow_thickness
;
507 _XmDrawShadows(XtDisplay(w
), XtWindow(w
),
508 gw
->primitive
.top_shadow_GC
,
509 gw
->primitive
.bottom_shadow_GC
,
510 0, 0, w
->core
.width
, w
->core
.height
,
512 DrawSlider(gw
, False
);
522 #define offset(field) XtOffset(XmGaugeWidget, gauge.field)
523 {XmNvalue
, XmCValue
, XtRInt
, sizeof(int),
524 offset(value
), XtRImmediate
, (caddr_t
)10},
526 {XmNminimum
, XmCValue
, XtRInt
, sizeof(int),
527 offset(minimum
), XtRImmediate
, (caddr_t
)0},
529 {XmNmaximum
, XmCValue
, XtRInt
, sizeof(int),
530 offset(maximum
), XtRImmediate
, (caddr_t
)100},
532 {XmNorientation
, XmCOrientation
, XmROrientation
, sizeof(unsigned char),
533 offset(orientation
), XtRImmediate
, (caddr_t
)XmVERTICAL
},
535 {XmNprocessingDirection
, XmCProcessingDirection
,
536 XmRProcessingDirection
, sizeof(unsigned char),
537 offset(processingDirection
), XtRImmediate
, (caddr_t
)XmMAX_ON_RIGHT
},
539 {XmNdragCallback
, XmCCallback
, XmRCallback
, sizeof(XtCallbackList
),
540 offset(dragCallback
), XtRImmediate
, (caddr_t
)NULL
},
542 {XmNvalueChangedCallback
, XmCCallback
, XmRCallback
, sizeof(XtCallbackList
),
543 offset(valueChangedCallback
), XtRImmediate
, (caddr_t
)NULL
},
550 XmGaugeClassRec xmGaugeClassRec
= {
552 (WidgetClass
) &xmPrimitiveClassRec
, /* superclass */
553 "XmGauge", /* class_name */
554 sizeof(XmGaugeRec
), /* widget_size */
555 NULL
, /* class_initialize */
556 NULL
, /* class_part_initialize */
557 FALSE
, /* class_inited */
558 Initialize
, /* initialize */
559 NULL
, /* initialize_hook */
560 XtInheritRealize
, /* realize */
561 actions
, /* actions */
562 XtNumber(actions
), /* num_actions */
563 resources
, /* resources */
564 XtNumber(resources
), /* num_resources */
565 NULLQUARK
, /* xrm_class */
566 TRUE
, /* compress_motion */
567 TRUE
, /* compress_exposure */
568 TRUE
, /* compress_enterleave */
569 FALSE
, /* visible_interest */
570 Destroy
, /* destroy */
572 ExposeProc
, /* expose */
573 SetValues
, /* set_values */
574 NULL
, /* set_values_hook */
575 XtInheritSetValuesAlmost
, /* set_values_almost */
576 NULL
, /* get_values_hook */
577 NULL
, /* accept_focus */
578 XtVersion
, /* version */
579 NULL
, /* callback_private */
580 translations
, /* tm_table */
581 NULL
, /* query_geometry */
582 NULL
, /* display_accelerator */
585 /* primitive_class fields */
587 NULL
, /* border_highlight */
588 NULL
, /* border_unhighlight */
589 NULL
, /* translations */
590 NULL
, /* arm_and_activate */
591 NULL
, /* syn_resources */
592 0, /* num_syn_resources */
600 WidgetClass xmGaugeWidgetClass
= (WidgetClass
)&xmGaugeClassRec
;
606 GaugePick(Widget
WXUNUSED(w
), XEvent
*WXUNUSED(e
), String
*WXUNUSED(args
), Cardinal
*WXUNUSED(num_args
))
608 /* Commented out for a read-only gauge in wxWindows */
610 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
611 #define THIS gw->gauge
614 Boolean dragging
= False
;
615 XButtonEvent
*event
= (XButtonEvent
*)e
;
620 sht
= gw
->primitive
.shadow_thickness
;
621 _XmDrawShadows(XtDisplay(w
), XtWindow(w
),
622 gw
->primitive
.top_shadow_GC
,
623 gw
->primitive
.bottom_shadow_GC
,
624 0, 0, w
->core
.width
, w
->core
.height
,
628 ratio
= (float)((float)THIS
.maximum
-
629 (float)THIS
.minimum
) / (float)THIS
.value
;
630 switch(THIS
.orientation
) {
632 size
= (w
->core
.width
- 2 * sht
) / ratio
;
633 switch(THIS
.processingDirection
) {
635 case XmMAX_ON_BOTTOM
:
636 dragging
= (x
> sht
) && (y
> sht
) &&
637 (x
< sht
+ size
) && (y
< w
->core
.height
- sht
);
641 dragging
= (x
> w
->core
.width
- size
- sht
) && (y
> sht
) &&
642 (x
< w
->core
.width
- sht
) && (y
< w
->core
.height
+ sht
);
647 size
= (w
->core
.height
- 2 * sht
) / ratio
;
648 switch(THIS
.processingDirection
) {
650 case XmMAX_ON_BOTTOM
:
651 dragging
= (x
> sht
) && (y
> sht
) &&
652 (x
< w
->core
.width
- sht
) &&
653 (y
< w
->core
.width
- 2 * sht
+ size
);
657 dragging
= (x
> sht
) && (y
> w
->core
.height
- size
- sht
) &&
658 (x
< w
->core
.width
- sht
) && (y
< w
->core
.height
- sht
);
662 THIS
.dragging
= dragging
;
669 #define round(x) ( (x) > 0 ? ((x) + 0.5) : -(-(x) + 0.5) )
672 GaugeDrag(Widget
WXUNUSED(w
), XEvent
*WXUNUSED(e
), String
*WXUNUSED(args
), Cardinal
*WXUNUSED(num_args
))
674 /* Commented out for a read-only gauge in wxWindows */
676 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
677 #define THIS gw->gauge
678 int sht
, x
, y
, max
, value
;
679 float ratio
, nratio
, size
, nsize
, fvalue
, delta
;
680 XMotionEvent
*event
= (XMotionEvent
*)e
;
682 if( ! THIS
.dragging
) return;
686 sht
= gw
->primitive
.shadow_thickness
;
688 ratio
= (float)THIS
.value
/ (float)((float)THIS
.maximum
-
689 (float)THIS
.minimum
);
690 switch(THIS
.orientation
) {
692 max
= (w
->core
.width
- 2 * sht
);
693 size
= (float)max
* ratio
;
694 delta
= (float)x
- (float)THIS
.oldx
;
697 max
= (w
->core
.height
- 2 * sht
);
698 size
= (float) max
* ratio
;
699 delta
= (float)y
- (float)THIS
.oldy
;
702 switch(THIS
.processingDirection
) {
704 case XmMAX_ON_BOTTOM
:
705 nsize
= size
+ delta
;
708 nsize
= size
- delta
;
710 if(nsize
> (float)max
) nsize
= (float)max
;
711 if(nsize
< (float)0 ) nsize
= (float)0;
712 nratio
= nsize
/ (float)max
;
714 fvalue
= (int)((float)THIS
.maximum
-
715 (float)THIS
.minimum
) * (float)nsize
/ (float)max
;
716 value
= round(fvalue
);
722 /* clear old slider only if it was larger */
723 DrawSlider(gw
, (nsize
< size
));
726 XmGaugeCallbackStruct call
;
728 if(NULL
!= THIS
.dragCallback
) {
729 call
.reason
= XmCR_DRAG
;
731 call
.value
= THIS
.value
;
732 XtCallCallbacks(w
, XmNdragCallback
, &call
);
741 GaugeDrop(Widget
WXUNUSED(w
), XEvent
*WXUNUSED(e
), String
*WXUNUSED(args
), Cardinal
*WXUNUSED(num_args
))
743 /* Commented out for a read-only gauge in wxWindows */
745 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
746 #define THIS gw->gauge
747 if( ! THIS
.dragging
) return;
749 if(NULL
!= THIS
.valueChangedCallback
) {
750 XmGaugeCallbackStruct call
;
751 call
.reason
= XmCR_VALUE_CHANGED
;
753 call
.value
= THIS
.value
;
754 XtCallCallbacks(w
, XmNvalueChangedCallback
, &call
);
756 THIS
.dragging
= False
;
762 XmGaugeSetValue(Widget w
, int value
)
764 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
766 gw
->gauge
.value
= value
;
767 DrawSlider(gw
, True
);
768 XFlush(XtDisplay(w
));
772 XmGaugeGetValue(Widget w
)
774 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
776 return gw
->gauge
.value
;