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 "../base/defines.h"
11 
12 
13 #include <Xm/Xm.h>
14 #include <Xm/Form.h>
15 #include <Xm/Frame.h>
16 #include <Xm/PushB.h>
17 #include <Xm/Text.h>
18 #include <Xm/SeparatoG.h>
19 #include <Xm/ScrollBar.h>
20 #include <Xm/ScrolledW.h>
21 #include <Xm/Label.h>
22 
23 #include "Application.h"
24 #include "TextEditDialog.h"
25 #include "ButtonInterface.h"
26 
27 
28 //
29 // These resources are NOT installed for THIS class because this is an
30 // abstract class.  It is up to the subclasses to install these resources.
31 //
32 String TextEditDialog::DefaultResources[] =
33 {
34 	".resizePolicy:			XmRESIZE_NONE",
35 	"*closeButton.labelString:	Close",
36 	"*okButton.labelString:		OK",
37 	"*cancelButton.labelString:	Cancel",
38 	"*applyButton.labelString:	Apply",
39 	"*restoreButton.labelString:	Restore",
40 	"*editorText.editMode:   	XmMULTI_LINE_EDIT",
41 	"*editorText.rows:		12",
42 	"*editorText.columns:		65",
43 	"*editorText.wordWrap:		True",
44 	"*editorText.scrollHorizontal:	False",
45 	"*editorText.shadowThickness:	1",
46 	"*closeButton.width: 		60",
47 	"*closeButton.topOffset: 	10",
48 	"*okButton.width: 		60",
49 	"*okButton.topOffset: 		10",
50 	"*cancelButton.width: 		60",
51 	"*cancelButton.topOffset: 	10",
52 	"*restoreButton.width: 		60",
53 	"*restoreButton.topOffset: 	10",
54 	"*applyButton.width: 		60",
55 	"*applyButton.topOffset: 	10",
56 
57 // If the user defines osfActivate (normally to be the return key) in the
58 // virtual bindings (usually in ~/.motifbind), then the text widget doesn't handle
59 // the return key properly.  The return key triggers the widget's activate()
60 // method rather than the self-insert() or insert-string() methods.
61 // (Caution: If the translation is split into 2 lines, it breaks.)
62 // - Martin
63  "*editorText.translations: #override None<Key>osfActivate: insert-string(\"\\n\")",
64 
65         NULL
66 };
67 
TextEditDialog(const char * name,Widget parent,boolean readonly,boolean wizard)68 TextEditDialog::TextEditDialog(const char *name,
69 			Widget parent, boolean readonly, boolean wizard)
70                        		: Dialog(name, parent)
71 {
72    this->editorText = NULL;
73    this->dialogModality = XmDIALOG_MODELESS;
74    this->readOnly = readonly;
75    this->wizard = wizard;
76 }
77 
~TextEditDialog()78 TextEditDialog::~TextEditDialog()
79 {
80 }
81 
installDefaultResources(Widget baseWidget)82 void TextEditDialog::installDefaultResources(Widget  baseWidget)
83 {
84     this->setDefaultResources(baseWidget, TextEditDialog::DefaultResources);
85     this->Dialog::installDefaultResources( baseWidget);
86 }
87 
createDialog(Widget parent)88 Widget TextEditDialog::createDialog(Widget parent)
89 {
90     Widget separator, label, form;
91     Arg wargs[6];
92 
93     this->initialize();
94 
95     int n = 0;
96     XtSetArg(wargs[n], XmNdialogStyle, this->dialogModality); n++;
97     XtSetArg(wargs[n], XmNautoUnmanage, False); n++;
98 
99     form = this->CreateMainForm(parent, this->name, wargs, n);
100 
101     label = XtVaCreateManagedWidget("nameLabel",xmLabelWidgetClass, form,
102 		    XmNtopAttachment,     XmATTACH_FORM,
103 		    XmNleftAttachment,    XmATTACH_FORM,
104 		    XmNalignment,	  XmALIGNMENT_BEGINNING,
105 		    XmNtopOffset,	  10,
106 		    XmNleftOffset,	  10,
107 		    NULL);
108 
109     if (this->readOnly) {
110 	this->ok = XtVaCreateManagedWidget("closeButton",
111 		    xmPushButtonWidgetClass, form,
112 		    XmNleftAttachment,    XmATTACH_FORM,
113 		    XmNbottomAttachment,  XmATTACH_FORM,
114 		    XmNleftOffset,        5,
115 		    XmNbottomOffset,      10,
116 		    NULL);
117     } else {
118 	this->ok = XtVaCreateManagedWidget("okButton",
119 		    xmPushButtonWidgetClass, form,
120 		    XmNleftAttachment,    XmATTACH_FORM,
121 		    XmNbottomAttachment,  XmATTACH_FORM,
122 		    XmNleftOffset,        5,
123 		    XmNbottomOffset,      10,
124 		    NULL);
125 
126 	// the Ok call back is installed during post() in the super class.
127 
128 	Widget apply = XtVaCreateManagedWidget("applyButton",
129 		    xmPushButtonWidgetClass, form,
130 		    XmNleftAttachment,    XmATTACH_WIDGET,
131 		    XmNleftWidget,        this->ok,
132 		    XmNbottomAttachment,  XmATTACH_FORM,
133 		    XmNleftOffset,        10,
134 		    XmNbottomOffset,      10,
135 		    NULL);
136 
137 	XtAddCallback(apply,XmNactivateCallback,
138 			(XtCallbackProc)TextEditDialog_ApplyCB, (XtPointer)this);
139 
140 	this->cancel = XtVaCreateManagedWidget("cancelButton",
141 		    xmPushButtonWidgetClass, form,
142 		    XmNrightAttachment,   XmATTACH_FORM,
143 		    XmNbottomAttachment,  XmATTACH_FORM,
144 		    XmNrightOffset,       5,
145 		    XmNbottomOffset,      10,
146 		    NULL);
147 
148 	// the Cancel call back is installed during post() in the super class.
149 
150 	Widget restore = XtVaCreateManagedWidget("restoreButton",
151 		    xmPushButtonWidgetClass, form,
152 		    XmNrightAttachment,   XmATTACH_WIDGET,
153 		    XmNrightWidget,       this->cancel,
154 		    XmNbottomAttachment,  XmATTACH_FORM,
155 		    XmNrightOffset,       10,
156 		    XmNbottomOffset,      10,
157 		    NULL);
158 	XtAddCallback(restore,XmNactivateCallback,
159 				(XtCallbackProc)TextEditDialog_RestoreCB, (XtPointer)this);
160 
161     }
162 
163     separator = XtVaCreateManagedWidget("",xmSeparatorGadgetClass, form,
164 		    XmNbottomAttachment,  XmATTACH_FORM,
165 		    XmNleftAttachment,    XmATTACH_FORM,
166 		    XmNbottomAttachment,  XmATTACH_WIDGET,
167 		    XmNbottomWidget,      this->ok,
168 		    XmNrightAttachment,   XmATTACH_FORM,
169 		    XmNleftOffset,        2,
170 		    XmNrightOffset,       2,
171 		    XmNbottomOffset,   	  10,
172 		    NULL);
173 
174 #ifndef AS_OLD_UI
175 
176     this->editorText = XmCreateScrolledText(form,"editorText",NULL,0);
177     XtVaSetValues(XtParent(this->editorText),
178 		    XmNtopAttachment,   XmATTACH_WIDGET,
179 		    XmNtopWidget,       label,
180 		    XmNbottomAttachment,XmATTACH_WIDGET,
181 		    XmNbottomWidget,    separator,
182 		    XmNleftAttachment,  XmATTACH_FORM,
183 		    XmNrightAttachment, XmATTACH_FORM,
184 		    XmNshadowType,  	XmSHADOW_ETCHED_IN,
185 		    XmNshadowThickness, 0,
186 		    XmNmarginWidth,   	3,
187 		    XmNmarginHeight,  	3,
188 		    XmNleftOffset,    	5,
189 		    XmNrightOffset,   	5,
190 		    XmNtopOffset,     	5,
191 		    XmNbottomOffset,  	10,
192 		    NULL);
193 
194     XtVaSetValues(this->editorText,
195 		XmNeditable, (this->readOnly ? False : True), NULL);
196     XtManageChild(this->editorText);
197 
198 #else
199 
200     Widget frame = XtVaCreateManagedWidget("frame",xmFrameWidgetClass, form,
201         XmNshadowThickness,   2,
202 	XmNtopAttachment,     XmATTACH_WIDGET,
203 	XmNtopWidget,         label,
204         XmNleftAttachment,    XmATTACH_FORM,
205         XmNrightAttachment,   XmATTACH_FORM,
206         XmNbottomAttachment,  XmATTACH_WIDGET,
207         XmNbottomWidget,      separator,
208         XmNshadowType,        XmSHADOW_ETCHED_IN,
209         XmNshadowThickness,   0,
210         XmNleftOffset,    5,
211         XmNrightOffset,   5,
212         XmNtopOffset,     10,
213         XmNbottomOffset,  10,
214 	XmNmarginWidth,   3,
215 	XmNmarginHeight,  3,
216         NULL);
217 
218     Widget sw = XtVaCreateManagedWidget("scrolledWindow",
219                                 xmScrolledWindowWidgetClass,
220                                 frame,
221 				XmNscrollHorizontal,False,
222                                 XmNscrollingPolicy, XmAPPLICATION_DEFINED,
223                                 XmNvisualPolicy,    XmVARIABLE,
224 				XmNscrollBarDisplayPolicy, XmAUTOMATIC,
225                                 XmNshadowThickness, 0,
226                                 NULL);
227 
228     this->editorText = XtVaCreateManagedWidget("editorText",
229                                   xmTextWidgetClass,
230                                   sw,
231 				  XmNeditMode, XmMULTI_LINE_EDIT,
232                                   NULL);
233 
234     XtVaSetValues(sw, XmNworkWindow, this->editorText, NULL);
235 
236 #endif
237 
238     //
239     // Turn off the scroll bar highlighting.
240     //
241     Widget vsb, hsb;
242     Pixel  backcolor;
243 
244     XtVaGetValues(form, XmNbackground, &backcolor, NULL);
245 
246 #ifdef AS_OLD_UI
247 
248     XtVaGetValues(sw,
249 		  XmNverticalScrollBar,&vsb,
250 		  XmNhorizontalScrollBar,&hsb,
251 		  NULL);
252 
253 #else
254 
255     XtVaGetValues(XtParent(this->editorText),
256 		  XmNverticalScrollBar,&vsb,
257 		  XmNhorizontalScrollBar,&hsb,
258 		  NULL);
259 
260 #endif
261 
262     if (vsb)
263         XtVaSetValues(vsb,
264 		      XmNhighlightThickness, 0,
265 		      XmNforeground,	     backcolor,
266 		      NULL);
267     if (hsb)
268 	XtVaSetValues(hsb, XmNhighlightThickness, 0, NULL);
269 
270     return form;
271 }
272 
273 
274 
275 
TextEditDialog_ApplyCB(Widget widget,XtPointer clientData,XtPointer callData)276 extern "C" void TextEditDialog_ApplyCB(Widget    widget,
277                                 XtPointer clientData,
278                                 XtPointer callData)
279 {
280     ASSERT(clientData);
281     TextEditDialog *data = (TextEditDialog*) clientData;
282 
283     data->applyCallback(widget, callData);
284 
285 }
applyCallback(Widget widget,XtPointer callData)286 void TextEditDialog::applyCallback(Widget    widget, XtPointer callData)
287 {
288     this->saveEditorText();
289 }
290 
291 
TextEditDialog_RestoreCB(Widget widget,XtPointer clientData,XtPointer callData)292 extern "C" void TextEditDialog_RestoreCB(Widget    widget,
293                                 XtPointer clientData,
294                                 XtPointer callData)
295 {
296     ASSERT(clientData);
297     TextEditDialog *data = (TextEditDialog*) clientData;
298 
299     data->restoreCallback(widget, callData);
300 }
restoreCallback(Widget widget,XtPointer callData)301 void TextEditDialog::restoreCallback(Widget    widget, XtPointer callData)
302 {
303     this->installEditorText();
304 }
installEditorText(const char * text)305 void TextEditDialog::installEditorText(const char *text)
306 {
307     ASSERT(this->editorText);
308     const char *s = text;
309     if (!s)  {
310         s = this->getText();
311         if (!s)
312 	    s = "";
313     }
314     XmTextSetString(this->editorText, (char*)s);
315 }
saveEditorText()316 boolean TextEditDialog::saveEditorText()
317 {
318 boolean return_value;
319 
320     ASSERT(this->editorText);
321     char *s = XmTextGetString(this->editorText);
322     return_value = this->saveText(s);
323     XtFree(s);
324     return return_value;
325 }
326 
okCallback(Dialog * d)327 boolean TextEditDialog::okCallback(Dialog *d)
328 {
329     if (!this->readOnly)
330         return this->saveEditorText();
331     return TRUE;
332 }
manage()333 void TextEditDialog::manage()
334 {
335     //
336     // Don't display the wizard on top of the parent.
337     //
338     Dimension dialogWidth;
339     if (!XtIsRealized(this->getRootWidget()))
340 	XtRealizeWidget(this->getRootWidget());
341 
342     XtVaGetValues(this->getRootWidget(), XmNwidth, &dialogWidth, NULL);
343 
344     Position x;
345     Position y;
346     Dimension width;
347     XtVaGetValues(parent, XmNx, &x, XmNy, &y, XmNwidth, &width, NULL);
348 
349     if (x > dialogWidth + 25) x -= dialogWidth + 25;
350     else x += width + 25;
351 
352     Display *dpy = XtDisplay(this->getRootWidget());
353     if (x > WidthOfScreen(DefaultScreenOfDisplay(dpy)) - dialogWidth)
354 	x = WidthOfScreen(DefaultScreenOfDisplay(dpy)) - dialogWidth;
355     XtVaSetValues(this->getRootWidget(), XmNdefaultPosition, False, NULL);
356     XtVaSetValues(XtParent(this->getRootWidget()), XmNx, x, XmNy, y, NULL);
357 
358     this->Dialog::manage();
359     this->updateDialogTitle();
360     this->installEditorText();
361 
362     XmProcessTraversal(this->editorText, XmTRAVERSE_CURRENT);
363     XmProcessTraversal(this->editorText, XmTRAVERSE_CURRENT);
364 }
365 
366 //
367 // The title to be applied the newly managed dialog.
368 // The returned string is freed by the caller (TextEditDialog::manage()).
369 //
getDialogTitle()370 char *TextEditDialog::getDialogTitle()
371 {
372    return NULL;
373 }
374 //
375 // Ask for the presumably new title from the derived class (usually) and
376 // install it.
377 //
updateDialogTitle()378 void TextEditDialog::updateDialogTitle()
379 {
380    char *title = this->getDialogTitle();
381    if (title) {
382 	this->setDialogTitle(title);
383 	delete title;
384    }
385 }
saveText(const char * s)386 boolean TextEditDialog::saveText(const char *s) { return TRUE; }
getText()387 const char *TextEditDialog::getText() { return NULL; }
388 
389