1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxGauge class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "gauge.h"
17 #include <wx/vms_x_fix.h>
20 # include "wx/gauge.h"
23 #pragma message disable nosimpint
27 #pragma message enable nosimpint
29 #include "wx/motif/private.h"
31 IMPLEMENT_DYNAMIC_CLASS(wxGauge
, wxControl
)
33 // XmGauge copyright notice:
36 * Copyright 1994 GROUPE BULL
38 * Permission to use, copy, modify, and distribute this software and its
39 * documentation for any purpose and without fee is hereby granted, provided
40 * that the above copyright notice appear in all copies and that both that
41 * copyright notice and this permission notice appear in supporting
42 * documentation, and that the name of GROUPE BULL not be used in advertising
43 * or publicity pertaining to distribution of the software without specific,
44 * written prior permission. GROUPE BULL makes no representations about the
45 * suitability of this software for any purpose. It is provided "as is"
46 * without express or implied warranty.
48 * GROUPE BULL disclaims all warranties with regard to this software,
49 * including all implied warranties of merchantability and fitness,
50 * in no event shall GROUPE BULL be liable for any special,
51 * indirect or consequential damages or any damages
52 * whatsoever resulting from loss of use, data or profits,
53 * whether in an action of contract, negligence or other tortious
54 * action, arising out of or in connection with the use
55 * or performance of this software.
59 //// PUBLIC XMGAUGE DECLARATIONS
60 typedef struct _XmGaugeClassRec
* XmGaugeWidgetClass
;
61 typedef struct _XmGaugeRec
* XmGaugeWidget
;
64 extern "C" WidgetClass xmGaugeWidgetClass
;
66 extern WidgetClass xmGaugeWidgetClass
;
69 typedef struct _XmGaugeCallbackStruct
{
73 } XmGaugeCallbackStruct
;
77 XmGaugeSetValue(Widget w
, int value
);
80 XmGaugeGetValue(Widget w
);
84 bool wxGauge::Create(wxWindow
*parent
, wxWindowID id
,
89 const wxValidator
& validator
,
93 SetValidator(validator
);
95 m_windowStyle
= style
;
96 m_backgroundColour
= parent
->GetBackgroundColour();
97 m_foregroundColour
= parent
->GetForegroundColour();
99 if (parent
) parent
->AddChild(this);
102 m_windowId
= (int)NewControlId();
106 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
110 if (style
& wxHORIZONTAL
)
112 XtSetArg (args
[0], XmNorientation
, XmHORIZONTAL
);
113 XtSetArg (args
[1], XmNprocessingDirection
, XmMAX_ON_RIGHT
);
117 XtSetArg (args
[0], XmNorientation
, XmVERTICAL
);
118 XtSetArg (args
[1], XmNprocessingDirection
, XmMAX_ON_TOP
);
120 XtSetArg(args
[2], XmNminimum
, 0);
121 XtSetArg(args
[3], XmNmaximum
, range
);
122 Widget gaugeWidget
= XtCreateManagedWidget("gauge", xmGaugeWidgetClass
, parentWidget
, args
, count
);
123 m_mainWidget
= (WXWidget
) gaugeWidget
;
125 XtManageChild (gaugeWidget
);
127 int x
= pos
.x
; int y
= pos
.y
;
128 int width
= size
.x
; int height
= size
.y
;
134 m_font
= parent
->GetFont();
137 SetCanAddEventHandler(TRUE
);
138 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
, x
, y
, width
, height
);
140 ChangeBackgroundColour();
145 void wxGauge::SetShadowWidth(int w
)
149 XtVaSetValues((Widget
) m_mainWidget
, XmNshadowThickness
, w
, NULL
);
152 void wxGauge::SetBezelFace(int WXUNUSED(w
))
156 void wxGauge::SetRange(int r
)
159 XtVaSetValues((Widget
) m_mainWidget
, XmNmaximum
, r
, NULL
);
162 void wxGauge::SetValue(int pos
)
165 XtVaSetValues((Widget
) m_mainWidget
, XmNvalue
, pos
, NULL
);
168 int wxGauge::GetShadowWidth() const
171 XtVaGetValues((Widget
) m_mainWidget
, XmNshadowThickness
, &w
, NULL
);
175 int wxGauge::GetBezelFace() const
180 int wxGauge::GetRange() const
183 XtVaGetValues((Widget
) m_mainWidget
, XmNmaximum
, &r
, NULL
);
185 // return m_rangeMax;
188 int wxGauge::GetValue() const
191 XtVaGetValues((Widget
) m_mainWidget
, XmNvalue
, &pos
, NULL
);
193 // return m_gaugePos;
196 void wxGauge::ChangeFont(bool keepOriginalSize
)
198 wxWindow::ChangeFont(keepOriginalSize
);
201 void wxGauge::ChangeBackgroundColour()
203 wxWindow::ChangeBackgroundColour();
206 void wxGauge::ChangeForegroundColour()
208 wxWindow::ChangeForegroundColour();
211 //// PRIVATE DECLARATIONS FOR XMGAUGE
213 #include <Xm/PrimitiveP.h>
214 #include <Xm/DrawP.h>
220 typedef struct _XmGaugeClassRec
{
221 CoreClassPart core_class
;
222 XmPrimitiveClassPart primitive_class
;
223 XmGaugeClassPart gauge_class
;
227 typedef struct _XmGaugePart
{
231 unsigned char orientation
;
232 unsigned char processingDirection
;
234 XtCallbackList dragCallback
;
235 XtCallbackList valueChangedCallback
;
238 Boolean dragging
; /* drag in progress ? */
244 typedef struct _XmGaugeRec
{
246 XmPrimitivePart primitive
;
250 extern XmGaugeClassRec xmGaugeClassRec
;
252 /* Copyright 1994 GROUPE BULL -- See license conditions in file COPYRIGHT */
254 //// XMGAUGE IMPLEMENTATION
257 GaugePick(Widget w
, XEvent
*e
, String
*args
, Cardinal
*num_args
);
259 GaugeDrag(Widget w
, XEvent
*e
, String
*args
, Cardinal
*num_args
);
261 GaugeDrop(Widget w
, XEvent
*e
, String
*args
, Cardinal
*num_args
);
265 static char translations
[] =
266 "<Btn1Down>: GaugePick()\n\
267 <Btn1Motion>: GaugeDrag()\n\
268 <Btn1Up>: GaugeDrop()\n\
273 static XtActionsRec actions
[] = {
274 {"GaugePick", GaugePick
},
275 {"GaugeDrag", GaugeDrag
},
276 {"GaugeDrop", GaugeDrop
},
280 DrawSlider(XmGaugeWidget gw
, Boolean clear
)
282 #define THIS gw->gauge
288 unsigned long backgr,foregr;
292 sht
= gw
->primitive
.shadow_thickness
;
294 ratio
= (float)THIS
.value
/
295 (float)(THIS
.maximum
- THIS
.minimum
);
297 sprintf(string,"%-d%%",(int)(ratio*100));
299 XtVaGetValues(gw,XmNbackground,&backgr,XmNforeground,&foregr,NULL);
303 XClearArea(XtDisplay(gw
), XtWindow(gw
), sht
, sht
,
304 gw
->core
.width
- 2 * sht
, gw
->core
.height
- 2 * sht
, False
);
306 switch(THIS
.orientation
) {
308 size
= (int) ((gw
->core
.width
- 2 * sht
)*ratio
);
310 XDrawString(XtDisplay(gw), XtWindow(gw), THIS.gc, sht+gw->core.width/2,
311 gw->core.height - 2 * sht, string, len);
313 switch(THIS
.processingDirection
) {
315 case XmMAX_ON_BOTTOM
:
316 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
317 sht
, sht
, size
, gw
->core
.height
- 2 * sht
);
320 rects[0].x = sht; rects[0].y = sht;
321 rects[0].width = size; rects[0].height = gw->core.height - 2 * sht;
326 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
327 gw
->core
.width
- size
- sht
, sht
,
328 size
, gw
->core
.height
- 2 * sht
);
331 rects[0].x = gw->core.width - size - sht; rects[0].y = sht;
332 rects[0].width = size; rects[0].height = gw->core.height - 2 * sht;
337 XSetClipRectangles(XtDisplay(gw), THIS.gc, 0, 0, rects, 1, Unsorted);
338 XSetForeground(XtDisplay(gw), THIS.gc, backgr);
339 XDrawString(XtDisplay(gw), XtWindow(gw), THIS.gc, sht+gw->core.width/2,
340 gw->core.height - 2 * sht, string, len);
345 size
= (int) ((gw
->core
.height
- 2 * sht
)*ratio
);
347 XDrawString(XtDisplay(gw), XtWindow(gw), THIS.gc, sht,
348 sht+gw->core.height/2, string,len);
350 switch(THIS
.processingDirection
) {
352 case XmMAX_ON_BOTTOM
:
353 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
354 sht
, sht
, gw
->core
.width
- 2 * sht
, size
);
357 rects[0].x = sht; rects[0].y = sht;
358 rects[0].width = gw->core.width - 2 * sht; rects[0].height = size;
363 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
364 sht
, gw
->core
.height
- size
- sht
,
365 gw
->core
.width
- 2 * sht
, size
);
368 rects[0].x = sht; rects[0].y = gw->core.height - size - sht;
369 rects[0].width = gw->core.width - 2 * sht; rects[0].height = size;
373 XSetClipRectangles(XtDisplay(gw), THIS.gc, 0, 0, rects, 1, Unsorted);
374 XSetForeground(XtDisplay(gw), THIS.gc, backgr);
375 XDrawString(XtDisplay(gw), XtWindow(gw), THIS.gc, sht,
376 sht+gw->core.height/2, string,len);
381 XSetClipMask(XtDisplay(gw), THIS.gc, None);
382 XSetForeground(XtDisplay(gw), THIS.gc, foregr);
391 DrawSlider(XmGaugeWidget gw
, Boolean clear
)
393 #define THIS gw->gauge
397 sht
= gw
->primitive
.shadow_thickness
;
398 /* See fix comment below: can cause divide by zero error.
399 ratio = (float)((float)THIS.maximum -
400 (float)THIS.minimum) / (float)THIS.value;
403 XClearArea(XtDisplay(gw
), XtWindow(gw
), sht
, sht
,
404 gw
->core
.width
- 2 * sht
, gw
->core
.height
- 2 * sht
, False
);
406 switch(THIS
.orientation
) {
408 /* size = (gw->core.width - 2 * sht) / ratio; */
409 /* A fix suggested by Dmitri Chubraev */
410 size
= (gw
->core
.width
- 2 * sht
) /((float)THIS
.maximum
-(float)THIS
.minimum
)*(float)THIS
.value
;
411 switch(THIS
.processingDirection
) {
413 case XmMAX_ON_BOTTOM
:
414 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
415 sht
, sht
, size
, gw
->core
.height
- 2 * sht
);
419 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
420 gw
->core
.width
- size
- sht
, sht
,
421 size
, gw
->core
.height
- 2 * sht
);
426 size
= (gw
->core
.height
- 2 * sht
) /((float)THIS
.maximum
-(float)THIS
.minimum
)*(float)THIS
.value
;
427 /* size = (gw->core.height - 2 * sht)/ ratio; */
428 switch(THIS
.processingDirection
) {
430 case XmMAX_ON_BOTTOM
:
431 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
432 sht
, sht
, gw
->core
.width
- 2 * sht
, size
);
436 XFillRectangle(XtDisplay(gw
), XtWindow(gw
), THIS
.gc
,
437 sht
, gw
->core
.height
- size
- sht
,
438 gw
->core
.width
- 2 * sht
, size
);
447 Initialize(Widget
WXUNUSED(req
), Widget new_w
, ArgList
WXUNUSED(args
), Cardinal
*WXUNUSED(num_args
))
449 XmGaugeWidget gw
= (XmGaugeWidget
)new_w
;
450 #define THIS gw->gauge
453 values
.foreground
= gw
->primitive
.foreground
;
454 THIS
.gc
= XtGetGC(new_w
, GCForeground
, &values
);
465 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
466 #define THIS gw->gauge
467 XtReleaseGC(w
, THIS
.gc
);
479 ArgList
WXUNUSED(args
),
480 Cardinal
*WXUNUSED(num_args
) )
482 XmGaugeWidget cgw
= (XmGaugeWidget
)cw
;
483 XmGaugeWidget ngw
= (XmGaugeWidget
)nw
;
485 Boolean redraw
= False
;
486 if(cgw
->primitive
.foreground
!= ngw
->primitive
.foreground
) {
490 XtReleaseGC(nw
, ngw
->gauge
.gc
);
491 values
.foreground
= ngw
->primitive
.foreground
;
492 ngw
->gauge
.gc
= XtGetGC(nw
, GCForeground
, &values
);
494 if(cgw
->gauge
.value
!= ngw
->gauge
.value
) {
504 ExposeProc(Widget w
, XEvent
*WXUNUSED(event
), Region
WXUNUSED(r
))
506 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
507 #define THIS gw->gauge
510 sht
= gw
->primitive
.shadow_thickness
;
511 _XmDrawShadows(XtDisplay(w
), XtWindow(w
),
512 gw
->primitive
.top_shadow_GC
,
513 gw
->primitive
.bottom_shadow_GC
,
514 0, 0, w
->core
.width
, w
->core
.height
,
516 DrawSlider(gw
, False
);
526 #define offset(field) XtOffset(XmGaugeWidget, gauge.field)
527 {XmNvalue
, XmCValue
, XtRInt
, sizeof(int),
528 offset(value
), XtRImmediate
, (caddr_t
)10},
530 {XmNminimum
, XmCValue
, XtRInt
, sizeof(int),
531 offset(minimum
), XtRImmediate
, (caddr_t
)0},
533 {XmNmaximum
, XmCValue
, XtRInt
, sizeof(int),
534 offset(maximum
), XtRImmediate
, (caddr_t
)100},
536 {XmNorientation
, XmCOrientation
, XmROrientation
, sizeof(unsigned char),
537 offset(orientation
), XtRImmediate
, (caddr_t
)XmVERTICAL
},
539 {XmNprocessingDirection
, XmCProcessingDirection
,
540 XmRProcessingDirection
, sizeof(unsigned char),
541 offset(processingDirection
), XtRImmediate
, (caddr_t
)XmMAX_ON_RIGHT
},
543 {XmNdragCallback
, XmCCallback
, XmRCallback
, sizeof(XtCallbackList
),
544 offset(dragCallback
), XtRImmediate
, (caddr_t
)NULL
},
546 {XmNvalueChangedCallback
, XmCCallback
, XmRCallback
, sizeof(XtCallbackList
),
547 offset(valueChangedCallback
), XtRImmediate
, (caddr_t
)NULL
},
554 XmGaugeClassRec xmGaugeClassRec
= {
556 (WidgetClass
) &xmPrimitiveClassRec
, /* superclass */
557 "XmGauge", /* class_name */
558 sizeof(XmGaugeRec
), /* widget_size */
559 NULL
, /* class_initialize */
560 NULL
, /* class_part_initialize */
561 FALSE
, /* class_inited */
562 Initialize
, /* initialize */
563 NULL
, /* initialize_hook */
564 XtInheritRealize
, /* realize */
565 actions
, /* actions */
566 XtNumber(actions
), /* num_actions */
567 resources
, /* resources */
568 XtNumber(resources
), /* num_resources */
569 NULLQUARK
, /* xrm_class */
570 TRUE
, /* compress_motion */
571 TRUE
, /* compress_exposure */
572 TRUE
, /* compress_enterleave */
573 FALSE
, /* visible_interest */
574 Destroy
, /* destroy */
576 ExposeProc
, /* expose */
577 SetValues
, /* set_values */
578 NULL
, /* set_values_hook */
579 XtInheritSetValuesAlmost
, /* set_values_almost */
580 NULL
, /* get_values_hook */
581 NULL
, /* accept_focus */
582 XtVersion
, /* version */
583 NULL
, /* callback_private */
584 translations
, /* tm_table */
585 NULL
, /* query_geometry */
586 NULL
, /* display_accelerator */
589 /* primitive_class fields */
591 NULL
, /* border_highlight */
592 NULL
, /* border_unhighlight */
593 NULL
, /* translations */
594 NULL
, /* arm_and_activate */
595 NULL
, /* syn_resources */
596 0, /* num_syn_resources */
604 WidgetClass xmGaugeWidgetClass
= (WidgetClass
)&xmGaugeClassRec
;
610 GaugePick(Widget
WXUNUSED(w
), XEvent
*WXUNUSED(e
), String
*WXUNUSED(args
), Cardinal
*WXUNUSED(num_args
))
612 /* Commented out for a read-only gauge in wxWindows */
614 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
615 #define THIS gw->gauge
618 Boolean dragging
= False
;
619 XButtonEvent
*event
= (XButtonEvent
*)e
;
624 sht
= gw
->primitive
.shadow_thickness
;
625 _XmDrawShadows(XtDisplay(w
), XtWindow(w
),
626 gw
->primitive
.top_shadow_GC
,
627 gw
->primitive
.bottom_shadow_GC
,
628 0, 0, w
->core
.width
, w
->core
.height
,
632 ratio
= (float)((float)THIS
.maximum
-
633 (float)THIS
.minimum
) / (float)THIS
.value
;
634 switch(THIS
.orientation
) {
636 size
= (w
->core
.width
- 2 * sht
) / ratio
;
637 switch(THIS
.processingDirection
) {
639 case XmMAX_ON_BOTTOM
:
640 dragging
= (x
> sht
) && (y
> sht
) &&
641 (x
< sht
+ size
) && (y
< w
->core
.height
- sht
);
645 dragging
= (x
> w
->core
.width
- size
- sht
) && (y
> sht
) &&
646 (x
< w
->core
.width
- sht
) && (y
< w
->core
.height
+ sht
);
651 size
= (w
->core
.height
- 2 * sht
) / ratio
;
652 switch(THIS
.processingDirection
) {
654 case XmMAX_ON_BOTTOM
:
655 dragging
= (x
> sht
) && (y
> sht
) &&
656 (x
< w
->core
.width
- sht
) &&
657 (y
< w
->core
.width
- 2 * sht
+ size
);
661 dragging
= (x
> sht
) && (y
> w
->core
.height
- size
- sht
) &&
662 (x
< w
->core
.width
- sht
) && (y
< w
->core
.height
- sht
);
666 THIS
.dragging
= dragging
;
673 #define round(x) ( (x) > 0 ? ((x) + 0.5) : -(-(x) + 0.5) )
676 GaugeDrag(Widget
WXUNUSED(w
), XEvent
*WXUNUSED(e
), String
*WXUNUSED(args
), Cardinal
*WXUNUSED(num_args
))
678 /* Commented out for a read-only gauge in wxWindows */
680 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
681 #define THIS gw->gauge
682 int sht
, x
, y
, max
, value
;
683 float ratio
, nratio
, size
, nsize
, fvalue
, delta
;
684 XMotionEvent
*event
= (XMotionEvent
*)e
;
686 if( ! THIS
.dragging
) return;
690 sht
= gw
->primitive
.shadow_thickness
;
692 ratio
= (float)THIS
.value
/ (float)((float)THIS
.maximum
-
693 (float)THIS
.minimum
);
694 switch(THIS
.orientation
) {
696 max
= (w
->core
.width
- 2 * sht
);
697 size
= (float)max
* ratio
;
698 delta
= (float)x
- (float)THIS
.oldx
;
701 max
= (w
->core
.height
- 2 * sht
);
702 size
= (float) max
* ratio
;
703 delta
= (float)y
- (float)THIS
.oldy
;
706 switch(THIS
.processingDirection
) {
708 case XmMAX_ON_BOTTOM
:
709 nsize
= size
+ delta
;
712 nsize
= size
- delta
;
714 if(nsize
> (float)max
) nsize
= (float)max
;
715 if(nsize
< (float)0 ) nsize
= (float)0;
716 nratio
= nsize
/ (float)max
;
718 fvalue
= (int)((float)THIS
.maximum
-
719 (float)THIS
.minimum
) * (float)nsize
/ (float)max
;
720 value
= round(fvalue
);
726 /* clear old slider only if it was larger */
727 DrawSlider(gw
, (nsize
< size
));
730 XmGaugeCallbackStruct call
;
732 if(NULL
!= THIS
.dragCallback
) {
733 call
.reason
= XmCR_DRAG
;
735 call
.value
= THIS
.value
;
736 XtCallCallbacks(w
, XmNdragCallback
, &call
);
745 GaugeDrop(Widget
WXUNUSED(w
), XEvent
*WXUNUSED(e
), String
*WXUNUSED(args
), Cardinal
*WXUNUSED(num_args
))
747 /* Commented out for a read-only gauge in wxWindows */
749 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
750 #define THIS gw->gauge
751 if( ! THIS
.dragging
) return;
753 if(NULL
!= THIS
.valueChangedCallback
) {
754 XmGaugeCallbackStruct call
;
755 call
.reason
= XmCR_VALUE_CHANGED
;
757 call
.value
= THIS
.value
;
758 XtCallCallbacks(w
, XmNvalueChangedCallback
, &call
);
760 THIS
.dragging
= False
;
766 XmGaugeSetValue(Widget w
, int value
)
768 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
770 gw
->gauge
.value
= value
;
771 DrawSlider(gw
, True
);
772 XFlush(XtDisplay(w
));
776 XmGaugeGetValue(Widget w
)
778 XmGaugeWidget gw
= (XmGaugeWidget
)w
;
780 return gw
->gauge
.value
;