1 /***********************************************************************/
2 /* Open Visualization Data Explorer                                    */
3 /* (C) Copyright IBM Corp. 1989,1999                                   */
4 /* ALL RIGHTS RESERVED                                                 */
5 /* This code licensed under the                                        */
6 /*    "IBM PUBLIC LICENSE - Open Visualization Data Explorer"          */
7 /***********************************************************************/
8 
9 #include <dxconfig.h>
10 #include <defines.h>
11 
12 
13 #include <Xm/Form.h>
14 #include <Xm/PushB.h>
15 #include <Xm/TextF.h>
16 #include <Xm/RowColumn.h>
17 
18 #include "TickLabel.h"
19 #include "../widgets/Number.h"
20 #include "DXStrings.h"
21 #include "DXApplication.h"
22 
23 String TickLabel::DefaultResources[] = {
24     "*tlNumber.topOffset:		6",
25     "*tlNumber.bottomOffset:		5",
26     "*tlNumber.leftOffset:		6",
27     "*tlLabel.topOffset:		3",
28     "*tlLabel.leftOffset:		4",
29     "*tlLabel.rightOffset:		6",
30     "*tlLabel.bottomOffset:		3",
31     NULL
32 };
33 #define MAX_TICK_LABEL_LEN 64
34 
35 boolean TickLabel::ClassInitialized = FALSE;
36 
TickLabel(double dval,const char * str,int pos,TickSelectCB tscb,void * clientData)37 TickLabel::TickLabel (double dval, const char *str, int pos,
38 TickSelectCB tscb, void *clientData) :
39 	UIComponent("tickLabel")
40 {
41     this->dval = dval;
42     this->str = DuplicateString(str);
43     this->pos = pos;
44 
45     this->dirty = 0;
46     this->number = NUL(Widget);
47     this->text = NUL(Widget);
48     this->form = NUL(Widget);
49 
50     this->selected = FALSE;
51     this->tscb = tscb;
52     this->clientData = clientData;
53     this->ignoreVerifyCallback = FALSE;
54 }
55 
initialize()56 void TickLabel::initialize()
57 {
58     //
59     // Initialize default resources (once only).
60     //
61     if (NOT TickLabel::ClassInitialized) {
62 	this->setDefaultResources(theApplication->getRootWidget(),
63 	    TickLabel::DefaultResources);
64 	TickLabel::ClassInitialized = TRUE;
65     }
66 }
67 
~TickLabel()68 TickLabel::~TickLabel()
69 {
70     if (this->str) delete this->str;
71 }
72 
73 void
destroyLine()74 TickLabel::destroyLine()
75 {
76     this->getText();
77 
78     //
79     // It should not be necessary to setRootWidget to NULL, but if you don't,
80     // you have to wait for libXt to process the destroyCallback so that UIComponent
81     // can do it for you.  I want it to be NULL right away.
82     //
83     Widget w = this->getRootWidget();
84     XtDestroyWidget(w);
85 
86     this->number = NUL(Widget);
87     this->form = NUL(Widget);
88     this->text = NUL(Widget);
89 }
90 
91 void
createLine(Widget parent)92 TickLabel::createLine (Widget parent)
93 {
94     if (this->number) return ;
95 
96     this->initialize();
97 
98     Widget form = this->form = XtVaCreateManagedWidget(this->name,
99 	xmFormWidgetClass,	parent,
100 	XmNshadowThickness,	1,
101 	XmNshadowType,		XmSHADOW_IN,
102 	XmNpositionIndex,	(this->pos - 1),
103     NULL);
104 
105     XtAddEventHandler (form, ButtonPressMask, False,
106 	(XtEventHandler)TickLabel_SelectEH, (XtPointer)this);
107 
108 #ifdef PASSDOUBLEVALUE
109     XtArgVal    dx_l1, dx_l4;
110 #endif
111     double inc = 0.1;
112     this->number = XtVaCreateManagedWidget("tlNumber",
113 	xmNumberWidgetClass,	form,
114 	XmNleftAttachment,	XmATTACH_FORM,
115 	XmNtopAttachment,	XmATTACH_FORM,
116 	XmNbottomAttachment,	XmATTACH_FORM,
117 	XmNdataType, 		DOUBLE,
118 	XmNdecimalPlaces, 	3,
119 	XmNcharPlaces,		9,
120 	XmNrecomputeSize,	False,
121 	XmNfixedNotation,	False,
122 	XmNdValue, 		DoubleVal(this->dval, dx_l4),
123 	XmNdValueStep,	 	DoubleVal(inc, dx_l1),
124     NULL);
125     XtAddCallback (this->number, XmNactivateCallback,
126 	(XtCallbackProc)TickLabel_NumberCB, (XtPointer)this);
127     XtAddCallback (this->number, XmNactivateCallback,
128 	(XtCallbackProc)TickLabel_NumberArmCB, (XtPointer)this);
129 
130     this->text = XtVaCreateManagedWidget ("tlLabel",
131 	xmTextFieldWidgetClass,	form,
132 	XmNrightAttachment,	XmATTACH_FORM,
133 	XmNtopAttachment,	XmATTACH_FORM,
134 	XmNbottomAttachment,	XmATTACH_FORM,
135 	XmNleftAttachment,	XmATTACH_WIDGET,
136 	XmNleftWidget,		this->number,
137 	XmNmaxLength,		MAX_TICK_LABEL_LEN,
138     NULL);
139     XtAddEventHandler (this->text, ButtonPressMask, False,
140 	(XtEventHandler)TickLabel_SelectEH, (XtPointer)this);
141 
142     this->ignoreVerifyCallback = TRUE;
143     if (this->str) XmTextFieldSetString (this->text, this->str);
144     else XmTextFieldSetString (this->text, "");
145     this->ignoreVerifyCallback = FALSE;
146     XtAddCallback (this->text, XmNmodifyVerifyCallback,
147 	(XtCallbackProc)TickLabel_TextModifyCB, (XtPointer)this);
148 
149     this->setRootWidget(form);
150     this->selected = TRUE;
151     this->setSelected (FALSE, FALSE);
152 }
153 
154 const char *
getText()155 TickLabel::getText()
156 {
157     if ((this->text) && (this->dirty & DIRTY_TICK_TEXT)) {
158 	if (this->str) delete this->str;
159 	char *cp = XmTextFieldGetString (this->text);
160 	this->str = DuplicateString(cp);
161 	XtFree(cp);
162     }
163     return this->str;
164 }
165 
166 void
setText(const char * str)167 TickLabel::setText (const char *str)
168 {
169     if (this->str) delete this->str;
170     this->str = DuplicateString(str);
171     this->ignoreVerifyCallback = TRUE;
172     if (this->text) {
173 	if (this->str)
174 	    XmTextFieldSetString (this->text, this->str);
175 	else
176 	    XmTextFieldSetString (this->text, "");
177     }
178     this->setText();
179     this->ignoreVerifyCallback = FALSE;
180 }
181 
182 void
setNumber(double dval)183 TickLabel::setNumber (double dval)
184 {
185 #ifdef PASSDOUBLEVALUE
186     XtArgVal    dxl;
187 #endif
188 
189     this->dval = dval;
190     if (this->number)
191 	XtVaSetValues (this->number, XmNdValue, DoubleVal(this->dval, dxl), NULL);
192     this->setNumber();
193 }
194 
195 void
setSelected(boolean set,boolean callCallback)196 TickLabel::setSelected (boolean set, boolean callCallback)
197 {
198 Pixel bg,ts,bs,fg;
199 boolean changed = (set != this->selected);
200 
201     this->selected = set;
202     if (!this->form) return ;
203     XtVaGetValues (this->text,
204 	XmNbackground, 		&bg,
205 	XmNforeground, 		&fg,
206 	XmNtopShadowColor, 	&ts,
207 	XmNbottomShadowColor,	&bs,
208     NULL);
209 
210     if (this->selected) {
211 	XtVaSetValues (this->form,
212 	    XmNtopShadowColor,	  fg,
213 	    XmNbottomShadowColor, fg,
214 	NULL);
215     } else {
216 	XtVaSetValues (this->form,
217 	    XmNtopShadowColor,	  bg,
218 	    XmNbottomShadowColor, bg,
219 	NULL);
220     }
221 
222     if ((callCallback) && (changed))  this->tscb(this, this->clientData);
223 }
224 
225 
226 extern "C" {
TickLabel_SelectEH(Widget,XtPointer clientData,XEvent *,Boolean *)227 void TickLabel_SelectEH(Widget , XtPointer clientData,
228                                         XEvent *, Boolean *)
229 {
230     TickLabel *tlab = (TickLabel *)clientData;
231     ASSERT(tlab);
232     tlab->setSelected(TRUE, TRUE);
233 }
234 
235 
TickLabel_DeleteOneCB(Widget,XtPointer clientData,XtPointer)236 void TickLabel_DeleteOneCB(Widget , XtPointer clientData, XtPointer)
237 {
238     TickLabel *tlab = (TickLabel *)clientData;
239     ASSERT(tlab);
240 }
TickLabel_AppendOneCB(Widget,XtPointer clientData,XtPointer)241 void TickLabel_AppendOneCB(Widget , XtPointer clientData, XtPointer)
242 {
243     TickLabel *tlab = (TickLabel *)clientData;
244     ASSERT(tlab);
245 }
246 
TickLabel_NumberArmCB(Widget,XtPointer clientData,XtPointer)247 void TickLabel_NumberArmCB(Widget , XtPointer clientData, XtPointer)
248 {
249     TickLabel *tlab = (TickLabel *)clientData;
250     ASSERT(tlab);
251     tlab->setSelected(!tlab->selected, TRUE);
252 }
253 
TickLabel_NumberCB(Widget,XtPointer clientData,XtPointer)254 void TickLabel_NumberCB(Widget , XtPointer clientData, XtPointer)
255 {
256     TickLabel *tlab = (TickLabel *)clientData;
257     ASSERT(tlab);
258 
259     XtVaGetValues (tlab->number, XmNdValue, &tlab->dval, NULL);
260     tlab->dirty|= DIRTY_TICK_NUMBER;
261 }
262 
TickLabel_TextModifyCB(Widget,XtPointer clientData,XtPointer cbs)263 void TickLabel_TextModifyCB(Widget , XtPointer clientData, XtPointer cbs)
264 {
265     TickLabel *tlab = (TickLabel *)clientData;
266     ASSERT(tlab);
267     XmTextVerifyCallbackStruct *tvcs = (XmTextVerifyCallbackStruct*)cbs;
268 
269     if ((tlab->str) && (tlab->ignoreVerifyCallback == FALSE)) {
270 	delete tlab->str;
271  	tlab->str = NULL;
272     }
273 
274     tlab->dirty|= DIRTY_TICK_TEXT;
275     tvcs->doit = True;
276 }
277 
278 } // extern "C"
279