Geant4 11.1.1
Toolkit for the simulation of the passage of particles through matter
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
G4UIXm.cc
Go to the documentation of this file.
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26//
27//
28// G.Barrand
29
30//#define DEBUG
31
32#include "G4Types.hh"
33
34#include <string.h>
35
36#include <X11/Intrinsic.h>
37#include <X11/Shell.h>
38#include <X11/keysym.h>
39
40#include <Xm/Xm.h>
41#include <Xm/Command.h>
42#include <Xm/RowColumn.h>
43#include <Xm/Form.h>
44#include <Xm/PushB.h>
45#include <Xm/CascadeB.h>
46#include <Xm/Text.h>
47
48#include "G4UIXm.hh"
49#include "G4UImanager.hh"
50#include "G4StateManager.hh"
51#include "G4UIcommandTree.hh"
52#include "G4UIcommandStatus.hh"
53
54#include "G4Xt.hh"
55
56#include <stdlib.h>
57
58static void XmTextAppendString (Widget,char*);
59
60static void clearButtonCallback (Widget,XtPointer,XtPointer);
61
62static char* XmConvertCompoundStringToString (XmString,G4int);
63static G4bool ConvertStringToInt(const char*,G4int&);
64static void ExecuteChangeSizeFunction(Widget);
65
66static G4bool exitSession = true;
67static G4bool exitPause = true;
68static G4bool exitHelp = true;
69/***************************************************************************/
71 G4int argc
72,char** argv
73)
74:shell(NULL)
75,command(NULL)
76,menuBar(NULL)
77,text(NULL)
78,fHelp(false)
79,fHelpChoice(0)
80/***************************************************************************/
81/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
82{
84 if(UI!=NULL) UI->SetSession(this);
85
86 G4Xt* interactorManager = G4Xt::getInstance (argc,argv,(char*)"Xm");
87
88 Widget top = (Widget)interactorManager->GetMainInteractor();
89
90 if(std::getenv("XENVIRONMENT")==NULL) {
91 XrmDatabase database = XrmGetDatabase(XtDisplay(top));
92 if(database!=NULL) {
93 XrmPutLineResource(&database,"*topShadowColor:white");
94 XrmPutLineResource(&database,"*bottomShadowColor:black");
95 XrmPutLineResource(&database,"*foreground:black");
96 XrmPutLineResource(&database,"*background:lightgrey");
97 XrmPutLineResource(&database,"*borderColor:lightgrey");
98 XrmPutLineResource(&database,"*fontList:-*-helvetica-bold-r-*-*-*-120-*-*-*-*-iso8859-1");
99 XrmPutLineResource(&database,"*text.background:white");
100 XrmPutLineResource(&database,"*text.fontList:*courier*-r-*--14-*");
101 XrmPutLineResource(&database,"*text.maxLength:8000");
102 }
103 }
104
105 Arg args[9];
106 XtSetArg(args[0],XmNkeyboardFocusPolicy,XmPOINTER); // For completion.
107 shell = XtAppCreateShell ("G4UIXm","G4UIXm",
108 topLevelShellWidgetClass,XtDisplay(top),
109 args,1);
110 form = XmCreateForm (shell,(char*)menu_str[0].c_str(),NULL,0);
111 XtManageChild (form);
112
113 XtSetArg(args[0],XmNtopAttachment ,XmATTACH_FORM);
114 XtSetArg(args[1],XmNleftAttachment ,XmATTACH_FORM);
115 XtSetArg(args[2],XmNrightAttachment ,XmATTACH_FORM);
116 menuBar = XmCreateMenuBar (form,(char*)menu_str[1].c_str(),args,3);
117
118 XtSetArg(args[0],XmNtopAttachment ,XmATTACH_NONE);
119 XtSetArg(args[1],XmNleftAttachment ,XmATTACH_FORM);
120 XtSetArg(args[2],XmNrightAttachment ,XmATTACH_FORM);
121 XtSetArg(args[3],XmNbottomAttachment ,XmATTACH_FORM);
122 command = XmCreateCommand (form,(char*)menu_str[2].c_str(),args,4);
123 XtManageChild (command);
124
125 XtSetArg(args[0],XmNtopAttachment ,XmATTACH_NONE);
126 XtSetArg(args[1],XmNleftAttachment ,XmATTACH_FORM);
127 XtSetArg(args[2],XmNrightAttachment ,XmATTACH_FORM);
128 XtSetArg(args[3],XmNbottomAttachment,XmATTACH_WIDGET);
129 XtSetArg(args[4],XmNbottomWidget ,command);
130 XmString cps = XmStringLtoRCreate((char*)menu_str[3].c_str(),(char*)XmSTRING_DEFAULT_CHARSET);
131 XtSetArg (args[5],XmNlabelString,cps);
132 Widget clearButton = XmCreatePushButton(form,(char*)menu_str[4].c_str(),args,6);
133 XmStringFree (cps);
134 XtManageChild (clearButton);
135
136 XtSetArg(args[0],XmNtopAttachment ,XmATTACH_WIDGET);
137 XtSetArg(args[1],XmNtopWidget ,menuBar);
138 XtSetArg(args[2],XmNleftAttachment ,XmATTACH_FORM);
139 XtSetArg(args[3],XmNrightAttachment ,XmATTACH_FORM);
140 XtSetArg(args[4],XmNbottomAttachment,XmATTACH_WIDGET);
141 XtSetArg(args[5],XmNbottomWidget ,clearButton);
142 XtSetArg(args[6],XmNeditMode ,XmMULTI_LINE_EDIT);
143 XtSetArg(args[7],XmNrows ,12);
144 XtSetArg(args[8],XmNcolumns ,80);
145 text = XmCreateScrolledText (form,(char*)menu_str[5].c_str(),args,9);
146 XtManageChild (text);
147
148 XtAddCallback(clearButton,XmNactivateCallback,
149 clearButtonCallback,(XtPointer)text);
150 XtAddCallback(command,XmNcommandEnteredCallback,
151 CommandEnteredCallback,(XtPointer)this);
152
153 Widget commandText = XmCommandGetChild(command,XmDIALOG_COMMAND_TEXT);
154 XtAddEventHandler(commandText,KeyPressMask,False,keyHandler,(XtPointer)this);
155
156 XtRealizeWidget(shell);
157 XtMapWidget(shell);
158
159 if(UI!=NULL) UI->SetCoutDestination(this);
160}
161/***************************************************************************/
163)
164/***************************************************************************/
165/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
166{
168 if(UI!=NULL) {
169 UI->SetSession(NULL);
170 UI->SetCoutDestination(NULL);
171 }
172 XtDestroyWidget(shell);
173}
174/***************************************************************************/
176)
177/***************************************************************************/
178/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
179{
180 G4Xt* interactorManager = G4Xt::getInstance ();
181 Prompt("session");
182 exitSession = false;
183 interactorManager->DisableSecondaryLoop ();
184 void* event;
185 while((event = interactorManager->GetEvent())!=NULL) {
186 interactorManager->DispatchEvent(event);
187 if(exitSession==true) break;
188 }
189 interactorManager->EnableSecondaryLoop ();
190 return this;
191}
192/***************************************************************************/
194 G4String aPrompt
195)
196/***************************************************************************/
197/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
198{
199 Arg args[1];
200 char* str = (char*)XtNewString(aPrompt.data());
201 XmString cps = XmStringLtoRCreate(str,(char*)XmSTRING_DEFAULT_CHARSET);
202 XtFree(str);
203 XtSetArg(args[0],XmNpromptString,cps);
204 XtSetValues(command,args,1);
205 XmStringFree(cps);
206}
207/***************************************************************************/
209)
210/***************************************************************************/
211/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
212{
213}
214/***************************************************************************/
216 const G4String& a_state
217)
218/***************************************************************************/
219/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
220{
221 if(a_state=="G4_pause> ") {
222 SecondaryLoop ("Pause, type continue to exit this state");
223 }
224
225 if(a_state=="EndOfEvent") {
226 // Picking with feed back in event data Done here !!!
227 SecondaryLoop ("End of event, type continue to exit this state");
228 }
229}
230/***************************************************************************/
231void G4UIXm::SecondaryLoop (
232 G4String a_prompt
233)
234/***************************************************************************/
235/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
236{
237 G4Xt* interactorManager = G4Xt::getInstance ();
238 Prompt(a_prompt);
239 exitPause = false;
240 void* event;
241 while((event = interactorManager->GetEvent())!=NULL) {
242 interactorManager->DispatchEvent(event);
243 if(exitPause==true) break;
244 }
245 Prompt("session");
246}
247/***************************************************************************/
249 const G4String& a_string
250)
251/***************************************************************************/
252/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
253{
254 XmTextAppendString(text,(char*)a_string.data());
255 return 0;
256}
257/***************************************************************************/
259 const G4String& a_string
260)
261/***************************************************************************/
262/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
263{
264 XmTextAppendString(text,(char*)a_string.data());
265 return 0;
266}
267/***************************************************************************/
268G4bool G4UIXm::GetHelpChoice(
269 G4int& aInt
270)
271/***************************************************************************/
272/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
273{
274 fHelp = true;
275 // SecondaryLoop :
276 G4Xt* interactorManager = G4Xt::getInstance ();
277 Prompt("Help");
278 exitHelp = false;
279 void* event;
280 while((event = interactorManager->GetEvent())!=NULL) {
281 interactorManager->DispatchEvent(event);
282 if(exitHelp==true) break;
283 }
284 Prompt("session");
285 //
286 if(fHelp==false) return false;
287 aInt = fHelpChoice;
288 fHelp = false;
289 return true;
290}
291/***************************************************************************/
292void G4UIXm::ExitHelp(
293) const
294/***************************************************************************/
295/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
296{
297}
298/***************************************************************************/
300 const char* a_name
301,const char* a_label
302)
303/***************************************************************************/
304/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
305{
306 if(menuBar==NULL) return;
307 if(a_name==NULL) return;
308 if(a_label==NULL) return;
309 XtManageChild (menuBar);
310 // Pulldown menu :
311 Widget widget;
312 widget = XmCreatePulldownMenu (menuBar,(char*)a_name,NULL,0);
313 AddInteractor (a_name,(G4Interactor)widget);
314 // Cascade button :
315 Arg args[2];
316 XmString cps = XmStringLtoRCreate((char*)a_label,(char*)XmSTRING_DEFAULT_CHARSET);
317 XtSetArg (args[0],XmNlabelString,cps);
318 XtSetArg (args[1],XmNsubMenuId,widget);
319 widget = XmCreateCascadeButton (menuBar,(char*)a_name,args,2);
320 XmStringFree (cps);
321 XtManageChild (widget);
322 ExecuteChangeSizeFunction(form);
323}
324/***************************************************************************/
326 const char* a_menu
327,const char* a_label
328,const char* a_command
329)
330/***************************************************************************/
331/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
332{
333 if(a_menu==NULL) return;
334 if(a_label==NULL) return;
335 if(a_command==NULL) return;
336 Widget parent = (Widget)GetInteractor(a_menu);
337 if(parent==NULL) return;
338 Widget widget = XmCreatePushButton(parent,(char*)a_label,NULL,0);
339 XtManageChild (widget);
340 XtAddCallback (widget,XmNactivateCallback,ButtonCallback,(XtPointer)this);
341 commands[widget] = a_command;
342}
343/***************************************************************************/
345 Widget a_widget
346)
347/***************************************************************************/
348/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
349{
350 return commands[a_widget];
351}
352/***************************************************************************/
353/***************************************************************************/
354/***************************************************************************/
355void G4UIXm::CommandEnteredCallback (
356 Widget
357,XtPointer a_tag
358,XtPointer a_data
359)
360/***************************************************************************/
361/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
362{
363 G4UIXm* This = (G4UIXm*)a_tag;
364
365 XmString cps = ((XmCommandCallbackStruct*)a_data)->value;
366 char* ss = XmConvertCompoundStringToString(cps,0);
367 G4String scommand (ss);
368 XtFree (ss);
369
370 if(This->fHelp==true) {
371 exitHelp = true;
372 This->fHelp = ConvertStringToInt(scommand.data(),This->fHelpChoice);
373 } else {
374 This->ApplyShellCommand (scommand,exitSession,exitPause);
375 }
376
377 a_tag = NULL;
378}
379/***************************************************************************/
380void G4UIXm::keyHandler (
381 Widget a_widget
382,XtPointer a_tag
383,XEvent* a_event
384,Boolean*
385)
386/***************************************************************************/
387/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
388{
389 KeySym keySym;
390 XLookupString(&(a_event->xkey),NULL,0,&keySym,NULL);
391 if(keySym!=XK_Tab) return;
392 G4UIXm* This = (G4UIXm*)a_tag;
393 char* s = XmTextGetString(a_widget);
394 G4String ss = This->Complete(s);
395 XmTextSetString(a_widget,(char*)ss.data());
396 XtFree(s);
397 XmTextSetInsertionPosition(a_widget,XmTextGetLastPosition(a_widget));
398}
399/***************************************************************************/
400void clearButtonCallback (
401 Widget
402,XtPointer a_tag
403,XtPointer
404)
405/***************************************************************************/
406/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
407{
408 static const G4String empty_str = "";
409 XmTextSetString((Widget)a_tag,(char*)empty_str.c_str());
410}
411/***************************************************************************/
412void G4UIXm::ButtonCallback (
413 Widget a_widget
414,XtPointer a_tag
415,XtPointer
416)
417/***************************************************************************/
418/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
419{
420 G4UIXm* This = (G4UIXm*)a_tag;
421 if(This->fHelp==true) return; // Disabled when in help.
422 G4String ss = This->GetCommand (a_widget);
423 This->ApplyShellCommand(ss,exitSession,exitPause);
424}
425/***************************************************************************/
426/***************************************************************************/
427/***************************************************************************/
428char* XmConvertCompoundStringToString (
429 XmString a_cps
430,G4int a_number
431)
432/***************************************************************************/
433/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
434{
435 if(a_cps==NULL) return NULL;
436 char* ss = NULL;
437 XmStringContext context;
438 XmStringInitContext(&context,a_cps);
439 G4int icount = 0;
440 Boolean Done = False;
441 while(Done==False) {
442 char* text = NULL;
443 XmStringCharSet charset = NULL;
444 XmStringDirection direct;
445 Boolean sep;
446 if(XmStringGetNextSegment(context,&text,&charset,&direct,&sep)==True) {
447 XtFree(charset);
448 if(sep==True) Done = True;
449 if(icount==a_number) {
450 ss = text;
451 break;
452 }
453 icount++;
454 XtFree(text);
455 }
456 else
457 Done = True;
458 }
459 XmStringFreeContext(context);
460 return ss;
461}
462/***************************************************************************/
463void XmTextAppendString (
464 Widget This
465,char* a_string
466)
467/***************************************************************************/
468/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
469{
470 if(This==NULL) return;
471 if(!XtIsSubclass(This,xmTextWidgetClass)) return;
472 if(a_string==NULL) return;
473 XmTextPosition lastpos = XmTextGetLastPosition(This);
474 XmTextReplace(This,lastpos,lastpos,a_string);
475 XmTextSetInsertionPosition(This,XmTextGetLastPosition(This));
476}
477//////////////////////////////////////////////////////////////////////////////
478G4bool ConvertStringToInt(
479 const char* aString
480,G4int& aInt
481)
482//////////////////////////////////////////////////////////////////////////////
483//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
484{
485 aInt = 0;
486 if(aString==NULL) return false;
487 char* s;
488 G4long value = strtol(aString,&s,10);
489 if(s==aString) return false;
490 aInt = (G4int)value;
491 return true;
492}
493#include <X11/IntrinsicP.h>
494//////////////////////////////////////////////////////////////////////////////
495void ExecuteChangeSizeFunction (
496 Widget aWidget
497)
498//////////////////////////////////////////////////////////////////////////////
499//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
500{
501 if(aWidget==NULL) return;
502 if(aWidget->core.widget_class->core_class.resize==NULL) return;
503 (aWidget->core.widget_class->core_class.resize)(aWidget);
504}
long G4long
Definition: G4Types.hh:87
bool G4bool
Definition: G4Types.hh:86
int G4int
Definition: G4Types.hh:85
void * G4Interactor
Definition: G4UIXm.hh:60
void AddMenu(const char *, const char *)
Definition: G4UIXm.cc:299
G4String GetCommand(Widget)
Definition: G4UIXm.cc:344
~G4UIXm()
Definition: G4UIXm.cc:162
G4UIXm(G4int, char **)
Definition: G4UIXm.cc:70
virtual void PauseSessionStart(const G4String &)
Definition: G4UIXm.cc:215
virtual G4int ReceiveG4cout(const G4String &)
Definition: G4UIXm.cc:248
void Prompt(G4String)
Definition: G4UIXm.cc:193
virtual G4int ReceiveG4cerr(const G4String &)
Definition: G4UIXm.cc:258
G4UIsession * SessionStart()
Definition: G4UIXm.cc:175
void SessionTerminate()
Definition: G4UIXm.cc:208
void AddButton(const char *, const char *, const char *)
Definition: G4UIXm.cc:325
void SetCoutDestination(G4UIsession *const value)
Definition: G4UImanager.cc:747
static G4UImanager * GetUIpointer()
Definition: G4UImanager.cc:77
void SetSession(G4UIsession *const value)
Definition: G4UImanager.hh:190
G4String Complete(const G4String &)
void ApplyShellCommand(const G4String &, G4bool &, G4bool &)
void AddInteractor(G4String, G4Interactor)
G4Interactor GetInteractor(G4String)
G4Interactor GetMainInteractor()
Definition: G4Xt.hh:48
void * GetEvent()
Definition: G4Xt.cc:150
static G4Xt * getInstance()
Definition: G4Xt.cc:55