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::SetShadowWidth(int w
)
141 XtVaSetValues((Widget
) m_mainWidget
, XmNshadowThickness
, w
, NULL
);
144 void wxGauge::SetBezelFace(int w
)
148 void wxGauge::SetRange(int r
)
151 XtVaSetValues((Widget
) m_mainWidget
, XmNmaximum
, r
, NULL
);
154 void wxGauge::SetValue(int pos
)
157 XtVaSetValues((Widget
) m_mainWidget
, XmNvalue
, pos
, NULL
);
160 int wxGauge::GetShadowWidth() const
163 XtVaGetValues((Widget
) m_mainWidget
, XmNshadowThickness
, &w
, NULL
);
167 int wxGauge::GetBezelFace() const
172 int wxGauge::GetRange() const
175 XtVaGetValues((Widget
) m_mainWidget
, XmNmaximum
, &r
, NULL
);
177 // return m_rangeMax;
180 int wxGauge::GetValue() const
183 XtVaGetValues((Widget
) m_mainWidget
, XmNvalue
, &pos
, NULL
);
185 // return m_gaugePos;
188 void wxGauge::ChangeFont(bool keepOriginalSize
)
190 wxWindow::ChangeFont(keepOriginalSize
);
193 void wxGauge::ChangeBackgroundColour()
195 wxWindow::ChangeBackgroundColour();
198 void wxGauge::ChangeForegroundColour()
200 wxWindow::ChangeForegroundColour();
203 //// PRIVATE DECLARATIONS FOR XMGAUGE
205 #include <Xm/PrimitiveP.h>
206 #include <Xm/DrawP.h>
212 typedef struct _XmGaugeClassRec
{
213 CoreClassPart core_class
;
214 XmPrimitiveClassPart primitive_class
;
215 XmGaugeClassPart gauge_class
;
219 typedef struct _XmGaugePart
{
223 unsigned char orientation
;
224 unsigned char processingDirection
;
226 XtCallbackList dragCallback
;
227 XtCallbackList valueChangedCallback
;
230 Boolean dragging
; /* drag in progress ? */
236 typedef struct _XmGaugeRec
{
238 XmPrimitivePart primitive
;
242 extern XmGaugeClassRec xmGaugeClassRec
;
244 /* Copyright 1994 GROUPE BULL -- See license conditions in file COPYRIGHT */
246 //// XMGAUGE IMPLEMENTATION
249 GaugePick(Widget w
, XEvent
*e
, String
*args
, Cardinal
*num_args
);
251 GaugeDrag(Widget w
, XEvent
*e
, String
*args
, Cardinal
*num_args
);
253 GaugeDrop(Widget w
, XEvent
*e
, String
*args
, Cardinal
*num_args
);
257 static char translations
[] =
258 "<Btn1Down>: GaugePick()\n\
259 <Btn1Motion>: GaugeDrag()\n\
260 <Btn1Up>: GaugeDrop()\n\
265 static XtActionsRec actions
[] = {
266 {"GaugePick", GaugePick
},
267 {"GaugeDrag", GaugeDrag
},
268 {"GaugeDrop", GaugeDrop
},
272 DrawSlider(XmGaugeWidget gw
, Boolean clear
)
274 #define THIS gw->gauge
280 unsigned long backgr,foregr;
284 sht
= gw
->primitive
.shadow_thickness
;
286 ratio
= (float)THIS
.value
/
287 (float)(THIS
.maximum
- THIS
.minimum
);
289 sprintf(string,"%-d%%",(int)(ratio*100));
291 XtVaGetValues(gw,XmNbackground,&backgr,XmNforeground,&foregr,NULL);
295 XClearArea(XtDisplay(gw
), XtWindow(gw
), sht
, sht
,
296 gw
->core
.width
- 2 * sht
, gw
->core
.height
- 2 * sht
, False
);
298 switch(THIS
.orientation
) {
300 size
= (int) ((gw
->core
.width
- 2 * sht
)*ratio
);
302 XDrawString(XtDisplay(gw), XtWindow(gw), THIS.gc, sht+gw->core.width/2,
303 gw->core.height - 2 * sht, string, len);
305 switch(THIS
.processingDirection
) {
307 case XmMAX_ON_BOTTOM
:
308 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
309 sht
, sht
, size
, gw
->core
.height
- 2 * sht
);
312 rects[0].x = sht; rects[0].y = sht;
313 rects[0].width = size; rects[0].height = gw->core.height - 2 * sht;
318 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
319 gw
->core
.width
- size
- sht
, sht
,
320 size
, gw
->core
.height
- 2 * sht
);
323 rects[0].x = gw->core.width - size - sht; rects[0].y = sht;
324 rects[0].width = size; rects[0].height = gw->core.height - 2 * sht;
329 XSetClipRectangles(XtDisplay(gw), THIS.gc, 0, 0, rects, 1, Unsorted);
330 XSetForeground(XtDisplay(gw), THIS.gc, backgr);
331 XDrawString(XtDisplay(gw), XtWindow(gw), THIS.gc, sht+gw->core.width/2,
332 gw->core.height - 2 * sht, string, len);
337 size
= (int) ((gw
->core
.height
- 2 * sht
)*ratio
);
339 XDrawString(XtDisplay(gw), XtWindow(gw), THIS.gc, sht,
340 sht+gw->core.height/2, string,len);
342 switch(THIS
.processingDirection
) {
344 case XmMAX_ON_BOTTOM
:
345 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
346 sht
, sht
, gw
->core
.width
- 2 * sht
, size
);
349 rects[0].x = sht; rects[0].y = sht;
350 rects[0].width = gw->core.width - 2 * sht; rects[0].height = size;
355 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
356 sht
, gw
->core
.height
- size
- sht
,
357 gw
->core
.width
- 2 * sht
, size
);
360 rects[0].x = sht; rects[0].y = gw->core.height - size - sht;
361 rects[0].width = gw->core.width - 2 * sht; rects[0].height = size;
365 XSetClipRectangles(XtDisplay(gw), THIS.gc, 0, 0, rects, 1, Unsorted);
366 XSetForeground(XtDisplay(gw), THIS.gc, backgr);
367 XDrawString(XtDisplay(gw), XtWindow(gw), THIS.gc, sht,
368 sht+gw->core.height/2, string,len);
373 XSetClipMask(XtDisplay(gw), THIS.gc, None);
374 XSetForeground(XtDisplay(gw), THIS.gc, foregr);
383 DrawSlider(XmGaugeWidget gw
, Boolean clear
)
385 #define THIS gw->gauge
389 sht
= gw
->primitive
.shadow_thickness
;
390 /* See fix comment below: can cause divide by zero error.
391 ratio = (float)((float)THIS.maximum -
392 (float)THIS.minimum) / (float)THIS.value;
395 XClearArea(XtDisplay(gw
), XtWindow(gw
), sht
, sht
,
396 gw
->core
.width
- 2 * sht
, gw
->core
.height
- 2 * sht
, False
);
398 switch(THIS
.orientation
) {
400 /* size = (gw->core.width - 2 * sht) / ratio; */
401 /* A fix suggested by Dmitri Chubraev */
402 size
= (gw
->core
.width
- 2 * sht
) /((float)THIS
.maximum
-(float)THIS
.minimum
)*(float)THIS
.value
;
403 switch(THIS
.processingDirection
) {
405 case XmMAX_ON_BOTTOM
:
406 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
407 sht
, sht
, size
, gw
->core
.height
- 2 * sht
);
411 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
412 gw
->core
.width
- size
- sht
, sht
,
413 size
, gw
->core
.height
- 2 * sht
);
418 size
= (gw
->core
.height
- 2 * sht
) /((float)THIS
.maximum
-(float)THIS
.minimum
)*(float)THIS
.value
;
419 /* size = (gw->core.height - 2 * sht)/ ratio; */
420 switch(THIS
.processingDirection
) {
422 case XmMAX_ON_BOTTOM
:
423 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
424 sht
, sht
, gw
->core
.width
- 2 * sht
, size
);
428 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
429 sht
, gw
->core
.height
- size
- sht
,
430 gw
->core
.width
- 2 * sht
, size
);
439 Initialize(Widget req
, Widget new_w
, ArgList args
, Cardinal
*num_args
)
441 XmGaugeWidget gw
= (XmGaugeWidget
)new_w
;
442 #define THIS gw->gauge
445 values
.foreground
= gw
->primitive
.foreground
;
446 THIS
.gc
= XtGetGC(new_w
, GCForeground
, &values
);
457 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
458 #define THIS gw->gauge
459 XtReleaseGC(w
, THIS
.gc
);
474 XmGaugeWidget cgw
= (XmGaugeWidget
)cw
;
475 XmGaugeWidget ngw
= (XmGaugeWidget
)nw
;
477 Boolean redraw
= False
;
478 if(cgw
->primitive
.foreground
!= ngw
->primitive
.foreground
) {
482 XtReleaseGC(nw
, ngw
->gauge
.gc
);
483 values
.foreground
= ngw
->primitive
.foreground
;
484 ngw
->gauge
.gc
= XtGetGC(nw
, GCForeground
, &values
);
486 if(cgw
->gauge
.value
!= ngw
->gauge
.value
) {
496 ExposeProc(Widget w
, XEvent
*event
, Region r
)
498 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
499 #define THIS gw->gauge
502 sht
= gw
->primitive
.shadow_thickness
;
503 _XmDrawShadows(XtDisplay(w
), XtWindow(w
),
504 gw
->primitive
.top_shadow_GC
,
505 gw
->primitive
.bottom_shadow_GC
,
506 0, 0, w
->core
.width
, w
->core
.height
,
508 DrawSlider(gw
, False
);
518 #define offset(field) XtOffset(XmGaugeWidget, gauge.field)
519 {XmNvalue
, XmCValue
, XtRInt
, sizeof(int),
520 offset(value
), XtRImmediate
, (caddr_t
)10},
522 {XmNminimum
, XmCValue
, XtRInt
, sizeof(int),
523 offset(minimum
), XtRImmediate
, (caddr_t
)0},
525 {XmNmaximum
, XmCValue
, XtRInt
, sizeof(int),
526 offset(maximum
), XtRImmediate
, (caddr_t
)100},
528 {XmNorientation
, XmCOrientation
, XmROrientation
, sizeof(unsigned char),
529 offset(orientation
), XtRImmediate
, (caddr_t
)XmVERTICAL
},
531 {XmNprocessingDirection
, XmCProcessingDirection
,
532 XmRProcessingDirection
, sizeof(unsigned char),
533 offset(processingDirection
), XtRImmediate
, (caddr_t
)XmMAX_ON_RIGHT
},
535 {XmNdragCallback
, XmCCallback
, XmRCallback
, sizeof(XtCallbackList
),
536 offset(dragCallback
), XtRImmediate
, (caddr_t
)NULL
},
538 {XmNvalueChangedCallback
, XmCCallback
, XmRCallback
, sizeof(XtCallbackList
),
539 offset(valueChangedCallback
), XtRImmediate
, (caddr_t
)NULL
},
546 XmGaugeClassRec xmGaugeClassRec
= {
548 (WidgetClass
) &xmPrimitiveClassRec
, /* superclass */
549 "XmGauge", /* class_name */
550 sizeof(XmGaugeRec
), /* widget_size */
551 NULL
, /* class_initialize */
552 NULL
, /* class_part_initialize */
553 FALSE
, /* class_inited */
554 Initialize
, /* initialize */
555 NULL
, /* initialize_hook */
556 XtInheritRealize
, /* realize */
557 actions
, /* actions */
558 XtNumber(actions
), /* num_actions */
559 resources
, /* resources */
560 XtNumber(resources
), /* num_resources */
561 NULLQUARK
, /* xrm_class */
562 TRUE
, /* compress_motion */
563 TRUE
, /* compress_exposure */
564 TRUE
, /* compress_enterleave */
565 FALSE
, /* visible_interest */
566 Destroy
, /* destroy */
568 ExposeProc
, /* expose */
569 SetValues
, /* set_values */
570 NULL
, /* set_values_hook */
571 XtInheritSetValuesAlmost
, /* set_values_almost */
572 NULL
, /* get_values_hook */
573 NULL
, /* accept_focus */
574 XtVersion
, /* version */
575 NULL
, /* callback_private */
576 translations
, /* tm_table */
577 NULL
, /* query_geometry */
578 NULL
, /* display_accelerator */
581 /* primitive_class fields */
583 NULL
, /* border_highlight */
584 NULL
, /* border_unhighlight */
585 NULL
, /* translations */
586 NULL
, /* arm_and_activate */
587 NULL
, /* syn_resources */
588 0, /* num_syn_resources */
596 WidgetClass xmGaugeWidgetClass
= (WidgetClass
)&xmGaugeClassRec
;
602 GaugePick(Widget w
, XEvent
*e
, String
*args
, Cardinal
*num_args
)
604 /* Commented out for a read-only gauge in wxWindows */
606 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
607 #define THIS gw->gauge
610 Boolean dragging
= False
;
611 XButtonEvent
*event
= (XButtonEvent
*)e
;
616 sht
= gw
->primitive
.shadow_thickness
;
617 _XmDrawShadows(XtDisplay(w
), XtWindow(w
),
618 gw
->primitive
.top_shadow_GC
,
619 gw
->primitive
.bottom_shadow_GC
,
620 0, 0, w
->core
.width
, w
->core
.height
,
624 ratio
= (float)((float)THIS
.maximum
-
625 (float)THIS
.minimum
) / (float)THIS
.value
;
626 switch(THIS
.orientation
) {
628 size
= (w
->core
.width
- 2 * sht
) / ratio
;
629 switch(THIS
.processingDirection
) {
631 case XmMAX_ON_BOTTOM
:
632 dragging
= (x
> sht
) && (y
> sht
) &&
633 (x
< sht
+ size
) && (y
< w
->core
.height
- sht
);
637 dragging
= (x
> w
->core
.width
- size
- sht
) && (y
> sht
) &&
638 (x
< w
->core
.width
- sht
) && (y
< w
->core
.height
+ sht
);
643 size
= (w
->core
.height
- 2 * sht
) / ratio
;
644 switch(THIS
.processingDirection
) {
646 case XmMAX_ON_BOTTOM
:
647 dragging
= (x
> sht
) && (y
> sht
) &&
648 (x
< w
->core
.width
- sht
) &&
649 (y
< w
->core
.width
- 2 * sht
+ size
);
653 dragging
= (x
> sht
) && (y
> w
->core
.height
- size
- sht
) &&
654 (x
< w
->core
.width
- sht
) && (y
< w
->core
.height
- sht
);
658 THIS
.dragging
= dragging
;
665 #define round(x) ( (x) > 0 ? ((x) + 0.5) : -(-(x) + 0.5) )
668 GaugeDrag(Widget w
, XEvent
*e
, String
*args
, Cardinal
*num_args
)
670 /* Commented out for a read-only gauge in wxWindows */
672 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
673 #define THIS gw->gauge
674 int sht
, x
, y
, max
, value
;
675 float ratio
, nratio
, size
, nsize
, fvalue
, delta
;
676 XMotionEvent
*event
= (XMotionEvent
*)e
;
678 if( ! THIS
.dragging
) return;
682 sht
= gw
->primitive
.shadow_thickness
;
684 ratio
= (float)THIS
.value
/ (float)((float)THIS
.maximum
-
685 (float)THIS
.minimum
);
686 switch(THIS
.orientation
) {
688 max
= (w
->core
.width
- 2 * sht
);
689 size
= (float)max
* ratio
;
690 delta
= (float)x
- (float)THIS
.oldx
;
693 max
= (w
->core
.height
- 2 * sht
);
694 size
= (float) max
* ratio
;
695 delta
= (float)y
- (float)THIS
.oldy
;
698 switch(THIS
.processingDirection
) {
700 case XmMAX_ON_BOTTOM
:
701 nsize
= size
+ delta
;
704 nsize
= size
- delta
;
706 if(nsize
> (float)max
) nsize
= (float)max
;
707 if(nsize
< (float)0 ) nsize
= (float)0;
708 nratio
= nsize
/ (float)max
;
710 fvalue
= (int)((float)THIS
.maximum
-
711 (float)THIS
.minimum
) * (float)nsize
/ (float)max
;
712 value
= round(fvalue
);
718 /* clear old slider only if it was larger */
719 DrawSlider(gw
, (nsize
< size
));
722 XmGaugeCallbackStruct call
;
724 if(NULL
!= THIS
.dragCallback
) {
725 call
.reason
= XmCR_DRAG
;
727 call
.value
= THIS
.value
;
728 XtCallCallbacks(w
, XmNdragCallback
, &call
);
737 GaugeDrop(Widget w
, XEvent
*e
, String
*args
, Cardinal
*num_args
)
739 /* Commented out for a read-only gauge in wxWindows */
741 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
742 #define THIS gw->gauge
743 if( ! THIS
.dragging
) return;
745 if(NULL
!= THIS
.valueChangedCallback
) {
746 XmGaugeCallbackStruct call
;
747 call
.reason
= XmCR_VALUE_CHANGED
;
749 call
.value
= THIS
.value
;
750 XtCallCallbacks(w
, XmNvalueChangedCallback
, &call
);
752 THIS
.dragging
= False
;
758 XmGaugeSetValue(Widget w
, int value
)
760 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
762 gw
->gauge
.value
= value
;
763 DrawSlider(gw
, True
);
764 XFlush(XtDisplay(w
));
768 XmGaugeGetValue(Widget w
)
770 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
772 return gw
->gauge
.value
;