32 #ifdef G4UI_BUILD_XM_SESSION
38 #include <X11/Intrinsic.h>
39 #include <X11/Shell.h>
40 #include <X11/keysym.h>
43 #include <Xm/Command.h>
44 #include <Xm/RowColumn.h>
47 #include <Xm/CascadeB.h>
60 static void XmTextAppendString (Widget,
char*);
62 static void clearButtonCallback (Widget,XtPointer,XtPointer);
64 static char* XmConvertCompoundStringToString (XmString,
int);
65 static G4bool ConvertStringToInt(
const char*,
int&);
66 static void ExecuteChangeSizeFunction(Widget);
68 static G4bool exitSession =
true;
69 static G4bool exitPause =
true;
70 static G4bool exitHelp =
true;
88 G4Xt* interactorManager = G4Xt::getInstance (argc,argv,(
char*)
"Xm");
90 Widget top = (Widget)interactorManager->GetMainInteractor();
92 if(std::getenv(
"XENVIRONMENT")==NULL) {
93 XrmDatabase database = XrmGetDatabase(XtDisplay(top));
95 XrmPutLineResource(&database,
"*topShadowColor:white");
96 XrmPutLineResource(&database,
"*bottomShadowColor:black");
97 XrmPutLineResource(&database,
"*foreground:black");
98 XrmPutLineResource(&database,
"*background:lightgrey");
99 XrmPutLineResource(&database,
"*borderColor:lightgrey");
100 XrmPutLineResource(&database,
"*fontList:-*-helvetica-bold-r-*-*-*-120-*-*-*-*-iso8859-1");
101 XrmPutLineResource(&database,
"*text.background:white");
102 XrmPutLineResource(&database,
"*text.fontList:*courier*-r-*--14-*");
103 XrmPutLineResource(&database,
"*text.maxLength:8000");
108 XtSetArg(args[0],XmNkeyboardFocusPolicy,XmPOINTER);
109 shell = XtAppCreateShell (
"G4UIXm",
"G4UIXm",
110 topLevelShellWidgetClass,XtDisplay(top),
112 form = XmCreateForm (
shell,(
char*)
"form",NULL,0);
113 XtManageChild (form);
115 XtSetArg(args[0],XmNtopAttachment ,XmATTACH_FORM);
116 XtSetArg(args[1],XmNleftAttachment ,XmATTACH_FORM);
117 XtSetArg(args[2],XmNrightAttachment ,XmATTACH_FORM);
118 menuBar = XmCreateMenuBar (form,(
char*)
"menuBar",args,3);
120 XtSetArg(args[0],XmNtopAttachment ,XmATTACH_NONE);
121 XtSetArg(args[1],XmNleftAttachment ,XmATTACH_FORM);
122 XtSetArg(args[2],XmNrightAttachment ,XmATTACH_FORM);
123 XtSetArg(args[3],XmNbottomAttachment ,XmATTACH_FORM);
124 command = XmCreateCommand (form,(
char*)
"command",args,4);
125 XtManageChild (command);
127 XtSetArg(args[0],XmNtopAttachment ,XmATTACH_NONE);
128 XtSetArg(args[1],XmNleftAttachment ,XmATTACH_FORM);
129 XtSetArg(args[2],XmNrightAttachment ,XmATTACH_FORM);
130 XtSetArg(args[3],XmNbottomAttachment,XmATTACH_WIDGET);
131 XtSetArg(args[4],XmNbottomWidget ,command);
132 XmString cps = XmStringLtoRCreate((
char*)
"Clear",(
char*)XmSTRING_DEFAULT_CHARSET);
133 XtSetArg (args[5],XmNlabelString,cps);
134 Widget clearButton = XmCreatePushButton(form,(
char*)
"clearButton",args,6);
136 XtManageChild (clearButton);
138 XtSetArg(args[0],XmNtopAttachment ,XmATTACH_WIDGET);
139 XtSetArg(args[1],XmNtopWidget ,menuBar);
140 XtSetArg(args[2],XmNleftAttachment ,XmATTACH_FORM);
141 XtSetArg(args[3],XmNrightAttachment ,XmATTACH_FORM);
142 XtSetArg(args[4],XmNbottomAttachment,XmATTACH_WIDGET);
143 XtSetArg(args[5],XmNbottomWidget ,clearButton);
144 XtSetArg(args[6],XmNeditMode ,XmMULTI_LINE_EDIT);
145 XtSetArg(args[7],XmNrows ,12);
146 XtSetArg(args[8],XmNcolumns ,80);
147 text = XmCreateScrolledText (form,(
char*)
"text",args,9);
148 XtManageChild (text);
150 XtAddCallback(clearButton,XmNactivateCallback,
151 clearButtonCallback,(XtPointer)text);
152 XtAddCallback(command,XmNcommandEnteredCallback,
153 CommandEnteredCallback,(XtPointer)
this);
155 Widget commandText = XmCommandGetChild(command,XmDIALOG_COMMAND_TEXT);
156 XtAddEventHandler(commandText,KeyPressMask,False,keyHandler,(XtPointer)
this);
158 XtRealizeWidget(
shell);
174 XtDestroyWidget(shell);
182 G4Xt* interactorManager = G4Xt::getInstance ();
185 interactorManager->DisableSecondaryLoop ();
187 while((event = interactorManager->GetEvent())!=NULL) {
188 interactorManager->DispatchEvent(event);
189 if(exitSession==
true)
break;
191 interactorManager->EnableSecondaryLoop ();
195 void G4UIXm::Prompt (
202 char* str = (
char*)XtNewString(aPrompt.
data());
203 XmString cps = XmStringLtoRCreate(str,(
char*)XmSTRING_DEFAULT_CHARSET);
205 XtSetArg(args[0],XmNpromptString,cps);
206 XtSetValues(command,args,1);
210 void G4UIXm::SessionTerminate (
217 void G4UIXm::PauseSessionStart (
223 if(a_state==
"G4_pause> ") {
224 SecondaryLoop (
"Pause, type continue to exit this state");
227 if(a_state==
"EndOfEvent") {
229 SecondaryLoop (
"End of event, type continue to exit this state");
233 void G4UIXm::SecondaryLoop (
239 G4Xt* interactorManager = G4Xt::getInstance ();
243 while((event = interactorManager->GetEvent())!=NULL) {
244 interactorManager->DispatchEvent(event);
245 if(exitPause==
true)
break;
250 G4int G4UIXm::ReceiveG4cout (
256 XmTextAppendString(text,(
char*)a_string.
data());
260 G4int G4UIXm::ReceiveG4cerr (
266 XmTextAppendString(text,(
char*)a_string.
data());
270 G4bool G4UIXm::GetHelpChoice(
278 G4Xt* interactorManager = G4Xt::getInstance ();
282 while((event = interactorManager->GetEvent())!=NULL) {
283 interactorManager->DispatchEvent(event);
284 if(exitHelp==
true)
break;
288 if(fHelp==
false)
return false;
294 void G4UIXm::ExitHelp(
301 void G4UIXm::AddMenu (
308 if(menuBar==NULL)
return;
309 if(a_name==NULL)
return;
310 if(a_label==NULL)
return;
311 XtManageChild (menuBar);
314 widget = XmCreatePulldownMenu (menuBar,(
char*)a_name,NULL,0);
318 XmString cps = XmStringLtoRCreate((
char*)a_label,(
char*)XmSTRING_DEFAULT_CHARSET);
319 XtSetArg (args[0],XmNlabelString,cps);
320 XtSetArg (args[1],XmNsubMenuId,widget);
321 widget = XmCreateCascadeButton (menuBar,(
char*)a_name,args,2);
323 XtManageChild (widget);
324 ExecuteChangeSizeFunction(form);
327 void G4UIXm::AddButton (
330 ,
const char* a_command
335 if(a_menu==NULL)
return;
336 if(a_label==NULL)
return;
337 if(a_command==NULL)
return;
338 Widget parent = (Widget)GetInteractor(a_menu);
339 if(parent==NULL)
return;
340 Widget widget = XmCreatePushButton(parent,(
char*)a_label,NULL,0);
341 XtManageChild (widget);
342 XtAddCallback (widget,XmNactivateCallback,ButtonCallback,(XtPointer)
this);
343 commands[widget] = a_command;
352 return commands[a_widget];
357 void G4UIXm::CommandEnteredCallback (
365 G4UIXm* This = (G4UIXm*)a_tag;
367 XmString cps = ((XmCommandCallbackStruct*)a_data)->value;
368 char* ss = XmConvertCompoundStringToString(cps,0);
372 if(This->fHelp==
true) {
374 This->fHelp = ConvertStringToInt(command.data(),This->fHelpChoice);
376 This->ApplyShellCommand (command,exitSession,exitPause);
383 void G4UIXm::keyHandler (
393 XLookupString(&(a_event->xkey),NULL,0,&keySym,NULL);
394 if(keySym!=XK_Tab)
return;
395 G4UIXm* This = (G4UIXm*)a_tag;
396 char*
s = XmTextGetString(a_widget);
398 XmTextSetString(a_widget,(
char*)ss.
data());
400 XmTextSetInsertionPosition(a_widget,XmTextGetLastPosition(a_widget));
403 void clearButtonCallback (
411 XmTextSetString((Widget)a_tag,(
char*)
"");
414 void G4UIXm::ButtonCallback (
422 G4UIXm* This = (G4UIXm*)a_tag;
423 if(This->fHelp==
true)
return;
424 G4String ss = This->GetCommand (a_widget);
425 This->ApplyShellCommand(ss,exitSession,exitPause);
430 char* XmConvertCompoundStringToString (
437 if(a_cps==NULL)
return NULL;
440 XmStringInitContext(&context,a_cps);
442 Boolean Done = False;
445 XmStringCharSet
charset = NULL;
446 XmStringDirection direct;
448 if(XmStringGetNextSegment(context,&text,&charset,&direct,&sep)==True) {
450 if(sep==True) Done = True;
451 if(icount==a_number) {
461 XmStringFreeContext(context);
465 void XmTextAppendString (
472 if(This==NULL)
return;
473 if(!XtIsSubclass(This,xmTextWidgetClass))
return;
474 if(a_string==NULL)
return;
475 XmTextPosition lastpos = XmTextGetLastPosition(This);
476 XmTextReplace(This,lastpos,lastpos,a_string);
477 XmTextSetInsertionPosition(This,XmTextGetLastPosition(This));
480 G4bool ConvertStringToInt(
488 if(aString==NULL)
return false;
490 long value = strtol(aString,&s,10);
491 if(s==aString)
return false;
495 #include <X11/IntrinsicP.h>
497 void ExecuteChangeSizeFunction (
503 if(aWidget==NULL)
return;
504 if(aWidget->core.widget_class->core_class.resize==NULL)
return;
505 (aWidget->core.widget_class->core_class.resize)(aWidget);