33 #ifdef G4UI_BUILD_XM_SESSION
39 #include <X11/Intrinsic.h>
40 #include <X11/Shell.h>
41 #include <X11/keysym.h>
44 #include <Xm/Command.h>
45 #include <Xm/RowColumn.h>
48 #include <Xm/CascadeB.h>
61 static void XmTextAppendString (Widget,
char*);
63 static void clearButtonCallback (Widget,XtPointer,XtPointer);
65 static char* XmConvertCompoundStringToString (XmString,
int);
66 static G4bool ConvertStringToInt(
const char*,
int&);
67 static void ExecuteChangeSizeFunction(Widget);
69 static G4bool exitSession =
true;
70 static G4bool exitPause =
true;
71 static G4bool exitHelp =
true;
89 G4Xt* interactorManager = G4Xt::getInstance (argc,argv,(
char*)
"Xm");
91 Widget top = (Widget)interactorManager->GetMainInteractor();
93 if(getenv(
"XENVIRONMENT")==NULL) {
94 XrmDatabase database = XrmGetDatabase(XtDisplay(top));
96 XrmPutLineResource(&database,
"*topShadowColor:white");
97 XrmPutLineResource(&database,
"*bottomShadowColor:black");
98 XrmPutLineResource(&database,
"*foreground:black");
99 XrmPutLineResource(&database,
"*background:lightgrey");
100 XrmPutLineResource(&database,
"*borderColor:lightgrey");
101 XrmPutLineResource(&database,
"*fontList:-*-helvetica-bold-r-*-*-*-120-*-*-*-*-iso8859-1");
102 XrmPutLineResource(&database,
"*text.background:white");
103 XrmPutLineResource(&database,
"*text.fontList:*courier*-r-*--14-*");
104 XrmPutLineResource(&database,
"*text.maxLength:8000");
109 XtSetArg(args[0],XmNkeyboardFocusPolicy,XmPOINTER);
110 shell = XtAppCreateShell (
"G4UIXm",
"G4UIXm",
111 topLevelShellWidgetClass,XtDisplay(top),
113 form = XmCreateForm (
shell,(
char*)
"form",NULL,0);
114 XtManageChild (form);
116 XtSetArg(args[0],XmNtopAttachment ,XmATTACH_FORM);
117 XtSetArg(args[1],XmNleftAttachment ,XmATTACH_FORM);
118 XtSetArg(args[2],XmNrightAttachment ,XmATTACH_FORM);
119 menuBar = XmCreateMenuBar (form,(
char*)
"menuBar",args,3);
121 XtSetArg(args[0],XmNtopAttachment ,XmATTACH_NONE);
122 XtSetArg(args[1],XmNleftAttachment ,XmATTACH_FORM);
123 XtSetArg(args[2],XmNrightAttachment ,XmATTACH_FORM);
124 XtSetArg(args[3],XmNbottomAttachment ,XmATTACH_FORM);
125 command = XmCreateCommand (form,(
char*)
"command",args,4);
126 XtManageChild (command);
128 XtSetArg(args[0],XmNtopAttachment ,XmATTACH_NONE);
129 XtSetArg(args[1],XmNleftAttachment ,XmATTACH_FORM);
130 XtSetArg(args[2],XmNrightAttachment ,XmATTACH_FORM);
131 XtSetArg(args[3],XmNbottomAttachment,XmATTACH_WIDGET);
132 XtSetArg(args[4],XmNbottomWidget ,command);
133 XmString cps = XmStringLtoRCreate((
char*)
"Clear",(
char*)XmSTRING_DEFAULT_CHARSET);
134 XtSetArg (args[5],XmNlabelString,cps);
135 Widget clearButton = XmCreatePushButton(form,(
char*)
"clearButton",args,6);
137 XtManageChild (clearButton);
139 XtSetArg(args[0],XmNtopAttachment ,XmATTACH_WIDGET);
140 XtSetArg(args[1],XmNtopWidget ,menuBar);
141 XtSetArg(args[2],XmNleftAttachment ,XmATTACH_FORM);
142 XtSetArg(args[3],XmNrightAttachment ,XmATTACH_FORM);
143 XtSetArg(args[4],XmNbottomAttachment,XmATTACH_WIDGET);
144 XtSetArg(args[5],XmNbottomWidget ,clearButton);
145 XtSetArg(args[6],XmNeditMode ,XmMULTI_LINE_EDIT);
146 XtSetArg(args[7],XmNrows ,12);
147 XtSetArg(args[8],XmNcolumns ,80);
148 text = XmCreateScrolledText (form,(
char*)
"text",args,9);
149 XtManageChild (text);
151 XtAddCallback(clearButton,XmNactivateCallback,
152 clearButtonCallback,(XtPointer)text);
153 XtAddCallback(command,XmNcommandEnteredCallback,
154 CommandEnteredCallback,(XtPointer)
this);
156 Widget commandText = XmCommandGetChild(command,XmDIALOG_COMMAND_TEXT);
157 XtAddEventHandler(commandText,KeyPressMask,False,keyHandler,(XtPointer)
this);
159 XtRealizeWidget(
shell);
175 XtDestroyWidget(shell);
183 G4Xt* interactorManager = G4Xt::getInstance ();
186 interactorManager->DisableSecondaryLoop ();
188 while((event = interactorManager->GetEvent())!=NULL) {
189 interactorManager->DispatchEvent(event);
190 if(exitSession==
true)
break;
192 interactorManager->EnableSecondaryLoop ();
196 void G4UIXm::Prompt (
203 char* str = (
char*)XtNewString(aPrompt.
data());
204 XmString cps = XmStringLtoRCreate(str,(
char*)XmSTRING_DEFAULT_CHARSET);
206 XtSetArg(args[0],XmNpromptString,cps);
207 XtSetValues(command,args,1);
211 void G4UIXm::SessionTerminate (
218 void G4UIXm::PauseSessionStart (
224 if(a_state==
"G4_pause> ") {
225 SecondaryLoop (
"Pause, type continue to exit this state");
228 if(a_state==
"EndOfEvent") {
230 SecondaryLoop (
"End of event, type continue to exit this state");
234 void G4UIXm::SecondaryLoop (
240 G4Xt* interactorManager = G4Xt::getInstance ();
244 while((event = interactorManager->GetEvent())!=NULL) {
245 interactorManager->DispatchEvent(event);
246 if(exitPause==
true)
break;
251 G4int G4UIXm::ReceiveG4cout (
257 XmTextAppendString(text,(
char*)a_string.
data());
261 G4int G4UIXm::ReceiveG4cerr (
267 XmTextAppendString(text,(
char*)a_string.
data());
271 G4bool G4UIXm::GetHelpChoice(
279 G4Xt* interactorManager = G4Xt::getInstance ();
283 while((event = interactorManager->GetEvent())!=NULL) {
284 interactorManager->DispatchEvent(event);
285 if(exitHelp==
true)
break;
289 if(fHelp==
false)
return false;
295 void G4UIXm::ExitHelp(
302 void G4UIXm::AddMenu (
309 if(menuBar==NULL)
return;
310 if(a_name==NULL)
return;
311 if(a_label==NULL)
return;
312 XtManageChild (menuBar);
315 widget = XmCreatePulldownMenu (menuBar,(
char*)a_name,NULL,0);
319 XmString cps = XmStringLtoRCreate((
char*)a_label,(
char*)XmSTRING_DEFAULT_CHARSET);
320 XtSetArg (args[0],XmNlabelString,cps);
321 XtSetArg (args[1],XmNsubMenuId,widget);
322 widget = XmCreateCascadeButton (menuBar,(
char*)a_name,args,2);
324 XtManageChild (widget);
325 ExecuteChangeSizeFunction(form);
328 void G4UIXm::AddButton (
331 ,
const char* a_command
336 if(a_menu==NULL)
return;
337 if(a_label==NULL)
return;
338 if(a_command==NULL)
return;
339 Widget parent = (Widget)GetInteractor(a_menu);
340 if(parent==NULL)
return;
341 Widget widget = XmCreatePushButton(parent,(
char*)a_label,NULL,0);
342 XtManageChild (widget);
343 XtAddCallback (widget,XmNactivateCallback,ButtonCallback,(XtPointer)
this);
344 commands[widget] = a_command;
353 return commands[a_widget];
358 void G4UIXm::CommandEnteredCallback (
366 G4UIXm* This = (G4UIXm*)a_tag;
368 XmString cps = ((XmCommandCallbackStruct*)a_data)->value;
369 char* ss = XmConvertCompoundStringToString(cps,0);
373 if(This->fHelp==
true) {
375 This->fHelp = ConvertStringToInt(command.data(),This->fHelpChoice);
377 This->ApplyShellCommand (command,exitSession,exitPause);
384 void G4UIXm::keyHandler (
394 XLookupString(&(a_event->xkey),NULL,0,&keySym,NULL);
395 if(keySym!=XK_Tab)
return;
396 G4UIXm* This = (G4UIXm*)a_tag;
397 char*
s = XmTextGetString(a_widget);
399 XmTextSetString(a_widget,(
char*)ss.
data());
401 XmTextSetInsertionPosition(a_widget,XmTextGetLastPosition(a_widget));
404 void clearButtonCallback (
412 XmTextSetString((Widget)a_tag,(
char*)
"");
415 void G4UIXm::ButtonCallback (
423 G4UIXm* This = (G4UIXm*)a_tag;
424 if(This->fHelp==
true)
return;
425 G4String ss = This->GetCommand (a_widget);
426 This->ApplyShellCommand(ss,exitSession,exitPause);
431 char* XmConvertCompoundStringToString (
438 if(a_cps==NULL)
return NULL;
440 XmStringContext context;
441 XmStringInitContext(&context,a_cps);
443 Boolean Done = False;
446 XmStringCharSet charset = NULL;
447 XmStringDirection direct;
449 if(XmStringGetNextSegment(context,&text,&charset,&direct,&sep)==True) {
451 if(sep==True) Done = True;
452 if(icount==a_number) {
462 XmStringFreeContext(context);
466 void XmTextAppendString (
473 if(This==NULL)
return;
474 if(!XtIsSubclass(This,xmTextWidgetClass))
return;
475 if(a_string==NULL)
return;
476 XmTextPosition lastpos = XmTextGetLastPosition(This);
477 XmTextReplace(This,lastpos,lastpos,a_string);
478 XmTextSetInsertionPosition(This,XmTextGetLastPosition(This));
481 G4bool ConvertStringToInt(
489 if(aString==NULL)
return false;
491 long value = strtol(aString,&s,10);
492 if(s==aString)
return false;
496 #include <X11/IntrinsicP.h>
498 void ExecuteChangeSizeFunction (
504 if(aWidget==NULL)
return;
505 if(aWidget->core.widget_class->core_class.resize==NULL)
return;
506 (aWidget->core.widget_class->core_class.resize)(aWidget);
static constexpr double s
void SetSession(G4UIsession *const value)
static G4UImanager * GetUIpointer()
const char * data() const
void SetCoutDestination(G4UIsession *const value)