Geant4 11.2.2
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
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 "G4UIXm.hh"
33
34#include "G4StateManager.hh"
35#include "G4Types.hh"
36#include "G4UIcommandStatus.hh"
37#include "G4UIcommandTree.hh"
38#include "G4UImanager.hh"
39#include "G4Xt.hh"
40
41#include <X11/Intrinsic.h>
42#include <X11/Shell.h>
43#include <X11/keysym.h>
44#include <Xm/CascadeB.h>
45#include <Xm/Command.h>
46#include <Xm/Form.h>
47#include <Xm/PushB.h>
48#include <Xm/RowColumn.h>
49#include <Xm/Text.h>
50#include <Xm/Xm.h>
51#include <stdlib.h>
52#include <string.h>
53
54static void XmTextAppendString(Widget, char*);
55
56static void clearButtonCallback(Widget, XtPointer, XtPointer);
57
58static char* XmConvertCompoundStringToString(XmString, G4int);
59static G4bool ConvertStringToInt(const char*, G4int&);
60static void ExecuteChangeSizeFunction(Widget);
61
62static G4bool exitSession = true;
63static G4bool exitPause = true;
64static G4bool exitHelp = true;
65/***************************************************************************/
66G4UIXm::G4UIXm(G4int argc, char** argv)
67 : shell(NULL), command(NULL), menuBar(NULL), text(NULL), fHelp(false), fHelpChoice(0)
68/***************************************************************************/
69/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
70{
72 if (UI != NULL) UI->SetSession(this);
73
74 G4Xt* interactorManager = G4Xt::getInstance(argc, argv, (char*)"Xm");
75
76 Widget top = (Widget)interactorManager->GetMainInteractor();
77
78 if (std::getenv("XENVIRONMENT") == NULL) {
79 XrmDatabase database = XrmGetDatabase(XtDisplay(top));
80 if (database != NULL) {
81 XrmPutLineResource(&database, "*topShadowColor:white");
82 XrmPutLineResource(&database, "*bottomShadowColor:black");
83 XrmPutLineResource(&database, "*foreground:black");
84 XrmPutLineResource(&database, "*background:lightgrey");
85 XrmPutLineResource(&database, "*borderColor:lightgrey");
86 XrmPutLineResource(&database, "*fontList:-*-helvetica-bold-r-*-*-*-120-*-*-*-*-iso8859-1");
87 XrmPutLineResource(&database, "*text.background:white");
88 XrmPutLineResource(&database, "*text.fontList:*courier*-r-*--14-*");
89 XrmPutLineResource(&database, "*text.maxLength:8000");
90 }
91 }
92
93 Arg args[9];
94 XtSetArg(args[0], XmNkeyboardFocusPolicy, XmPOINTER); // For completion.
95 shell = XtAppCreateShell("G4UIXm", "G4UIXm", topLevelShellWidgetClass, XtDisplay(top), args, 1);
96 form = XmCreateForm(shell, (char*)menu_str[0].c_str(), NULL, 0);
97 XtManageChild(form);
98
99 XtSetArg(args[0], XmNtopAttachment, XmATTACH_FORM);
100 XtSetArg(args[1], XmNleftAttachment, XmATTACH_FORM);
101 XtSetArg(args[2], XmNrightAttachment, XmATTACH_FORM);
102 menuBar = XmCreateMenuBar(form, (char*)menu_str[1].c_str(), args, 3);
103
104 XtSetArg(args[0], XmNtopAttachment, XmATTACH_NONE);
105 XtSetArg(args[1], XmNleftAttachment, XmATTACH_FORM);
106 XtSetArg(args[2], XmNrightAttachment, XmATTACH_FORM);
107 XtSetArg(args[3], XmNbottomAttachment, XmATTACH_FORM);
108 command = XmCreateCommand(form, (char*)menu_str[2].c_str(), args, 4);
109 XtManageChild(command);
110
111 XtSetArg(args[0], XmNtopAttachment, XmATTACH_NONE);
112 XtSetArg(args[1], XmNleftAttachment, XmATTACH_FORM);
113 XtSetArg(args[2], XmNrightAttachment, XmATTACH_FORM);
114 XtSetArg(args[3], XmNbottomAttachment, XmATTACH_WIDGET);
115 XtSetArg(args[4], XmNbottomWidget, command);
116 XmString cps = XmStringLtoRCreate((char*)menu_str[3].c_str(), (char*)XmSTRING_DEFAULT_CHARSET);
117 XtSetArg(args[5], XmNlabelString, cps);
118 Widget clearButton = XmCreatePushButton(form, (char*)menu_str[4].c_str(), args, 6);
119 XmStringFree(cps);
120 XtManageChild(clearButton);
121
122 XtSetArg(args[0], XmNtopAttachment, XmATTACH_WIDGET);
123 XtSetArg(args[1], XmNtopWidget, menuBar);
124 XtSetArg(args[2], XmNleftAttachment, XmATTACH_FORM);
125 XtSetArg(args[3], XmNrightAttachment, XmATTACH_FORM);
126 XtSetArg(args[4], XmNbottomAttachment, XmATTACH_WIDGET);
127 XtSetArg(args[5], XmNbottomWidget, clearButton);
128 XtSetArg(args[6], XmNeditMode, XmMULTI_LINE_EDIT);
129 XtSetArg(args[7], XmNrows, 12);
130 XtSetArg(args[8], XmNcolumns, 80);
131 text = XmCreateScrolledText(form, (char*)menu_str[5].c_str(), args, 9);
132 XtManageChild(text);
133
134 XtAddCallback(clearButton, XmNactivateCallback, clearButtonCallback, (XtPointer)text);
135 XtAddCallback(command, XmNcommandEnteredCallback, CommandEnteredCallback, (XtPointer)this);
136
137 Widget commandText = XmCommandGetChild(command, XmDIALOG_COMMAND_TEXT);
138 XtAddEventHandler(commandText, KeyPressMask, False, keyHandler, (XtPointer)this);
139
140 XtRealizeWidget(shell);
141 XtMapWidget(shell);
142
143 if (UI != NULL) UI->SetCoutDestination(this);
144}
145/***************************************************************************/
147/***************************************************************************/
148/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
149{
151 if (UI != NULL) {
152 UI->SetSession(NULL);
153 UI->SetCoutDestination(NULL);
154 }
155 XtDestroyWidget(shell);
156}
157/***************************************************************************/
159/***************************************************************************/
160/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
161{
162 G4Xt* interactorManager = G4Xt::getInstance();
163 Prompt("session");
164 exitSession = false;
165 interactorManager->DisableSecondaryLoop();
166 void* event;
167 while ((event = interactorManager->GetEvent()) != NULL) {
168 interactorManager->DispatchEvent(event);
169 if (exitSession == true) break;
170 }
171 interactorManager->EnableSecondaryLoop();
172 return this;
173}
174/***************************************************************************/
176/***************************************************************************/
177/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
178{
179 Arg args[1];
180 char* str = (char*)XtNewString(aPrompt.data());
181 XmString cps = XmStringLtoRCreate(str, (char*)XmSTRING_DEFAULT_CHARSET);
182 XtFree(str);
183 XtSetArg(args[0], XmNpromptString, cps);
184 XtSetValues(command, args, 1);
185 XmStringFree(cps);
186}
187/***************************************************************************/
189/***************************************************************************/
190/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
191{}
192/***************************************************************************/
194/***************************************************************************/
195/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
196{
197 if (a_state == "G4_pause> ") {
198 SecondaryLoop("Pause, type continue to exit this state");
199 }
200
201 if (a_state == "EndOfEvent") {
202 // Picking with feed back in event data Done here !!!
203 SecondaryLoop("End of event, type continue to exit this state");
204 }
205}
206/***************************************************************************/
207void G4UIXm::SecondaryLoop(G4String a_prompt)
208/***************************************************************************/
209/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
210{
211 G4Xt* interactorManager = G4Xt::getInstance();
212 Prompt(a_prompt);
213 exitPause = false;
214 void* event;
215 while ((event = interactorManager->GetEvent()) != NULL) {
216 interactorManager->DispatchEvent(event);
217 if (exitPause == true) break;
218 }
219 Prompt("session");
220}
221/***************************************************************************/
223/***************************************************************************/
224/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
225{
226 XmTextAppendString(text, (char*)a_string.data());
227 return 0;
228} /***************************************************************************/
230/***************************************************************************/
231/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
232{
233 XmTextAppendString(text, (char*)a_string.data());
234 return 0;
235}
236/***************************************************************************/
238/***************************************************************************/
239/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
240{
241 XmTextAppendString(text, (char*)a_string.data());
242 return 0;
243}
244/***************************************************************************/
245G4bool G4UIXm::GetHelpChoice(G4int& aInt)
246/***************************************************************************/
247/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
248{
249 fHelp = true;
250 // SecondaryLoop :
251 G4Xt* interactorManager = G4Xt::getInstance();
252 Prompt("Help");
253 exitHelp = false;
254 void* event;
255 while ((event = interactorManager->GetEvent()) != NULL) {
256 interactorManager->DispatchEvent(event);
257 if (exitHelp == true) break;
258 }
259 Prompt("session");
260 //
261 if (fHelp == false) return false;
262 aInt = fHelpChoice;
263 fHelp = false;
264 return true;
265}
266/***************************************************************************/
267void G4UIXm::ExitHelp() const
268/***************************************************************************/
269/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
270{}
271/***************************************************************************/
272void G4UIXm::AddMenu(const char* a_name, const char* a_label)
273/***************************************************************************/
274/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
275{
276 if (menuBar == NULL) return;
277 if (a_name == NULL) return;
278 if (a_label == NULL) return;
279 XtManageChild(menuBar);
280 // Pulldown menu :
281 Widget widget;
282 widget = XmCreatePulldownMenu(menuBar, (char*)a_name, NULL, 0);
283 AddInteractor(a_name, (G4Interactor)widget);
284 // Cascade button :
285 Arg args[2];
286 XmString cps = XmStringLtoRCreate((char*)a_label, (char*)XmSTRING_DEFAULT_CHARSET);
287 XtSetArg(args[0], XmNlabelString, cps);
288 XtSetArg(args[1], XmNsubMenuId, widget);
289 widget = XmCreateCascadeButton(menuBar, (char*)a_name, args, 2);
290 XmStringFree(cps);
291 XtManageChild(widget);
292 ExecuteChangeSizeFunction(form);
293}
294/***************************************************************************/
295void G4UIXm::AddButton(const char* a_menu, const char* a_label, const char* a_command)
296/***************************************************************************/
297/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
298{
299 if (a_menu == NULL) return;
300 if (a_label == NULL) return;
301 if (a_command == NULL) return;
302 Widget parent = (Widget)GetInteractor(a_menu);
303 if (parent == NULL) return;
304 Widget widget = XmCreatePushButton(parent, (char*)a_label, NULL, 0);
305 XtManageChild(widget);
306 XtAddCallback(widget, XmNactivateCallback, ButtonCallback, (XtPointer)this);
307 commands[widget] = a_command;
308}
309/***************************************************************************/
311/***************************************************************************/
312/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
313{
314 return commands[a_widget];
315}
316/***************************************************************************/
317/***************************************************************************/
318/***************************************************************************/
319void G4UIXm::CommandEnteredCallback(Widget, XtPointer a_tag, XtPointer a_data)
320/***************************************************************************/
321/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
322{
323 G4UIXm* This = (G4UIXm*)a_tag;
324
325 XmString cps = ((XmCommandCallbackStruct*)a_data)->value;
326 char* ss = XmConvertCompoundStringToString(cps, 0);
327 G4String scommand(ss);
328 XtFree(ss);
329
330 if (This->fHelp == true) {
331 exitHelp = true;
332 This->fHelp = ConvertStringToInt(scommand.data(), This->fHelpChoice);
333 }
334 else {
335 This->ApplyShellCommand(scommand, exitSession, exitPause);
336 }
337
338 a_tag = NULL;
339}
340/***************************************************************************/
341void G4UIXm::keyHandler(Widget a_widget, XtPointer a_tag, XEvent* a_event, Boolean*)
342/***************************************************************************/
343/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
344{
345 KeySym keySym;
346 XLookupString(&(a_event->xkey), NULL, 0, &keySym, NULL);
347 if (keySym != XK_Tab) return;
348 G4UIXm* This = (G4UIXm*)a_tag;
349 char* s = XmTextGetString(a_widget);
350 G4String ss = This->Complete(s);
351 XmTextSetString(a_widget, (char*)ss.data());
352 XtFree(s);
353 XmTextSetInsertionPosition(a_widget, XmTextGetLastPosition(a_widget));
354}
355/***************************************************************************/
356void clearButtonCallback(Widget, XtPointer a_tag, XtPointer)
357/***************************************************************************/
358/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
359{
360 static const G4String empty_str = "";
361 XmTextSetString((Widget)a_tag, (char*)empty_str.c_str());
362}
363/***************************************************************************/
364void G4UIXm::ButtonCallback(Widget a_widget, XtPointer a_tag, XtPointer)
365/***************************************************************************/
366/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
367{
368 G4UIXm* This = (G4UIXm*)a_tag;
369 if (This->fHelp == true) return; // Disabled when in help.
370 G4String ss = This->GetCommand(a_widget);
371 This->ApplyShellCommand(ss, exitSession, exitPause);
372}
373/***************************************************************************/
374/***************************************************************************/
375/***************************************************************************/
376char* XmConvertCompoundStringToString(XmString a_cps, G4int a_number)
377/***************************************************************************/
378/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
379{
380 if (a_cps == NULL) return NULL;
381 char* ss = NULL;
382 XmStringContext context;
383 XmStringInitContext(&context, a_cps);
384 G4int icount = 0;
385 Boolean Done = False;
386 while (Done == False) {
387 char* text = NULL;
388 XmStringCharSet charset = NULL;
389 XmStringDirection direct;
390 Boolean sep;
391 if (XmStringGetNextSegment(context, &text, &charset, &direct, &sep) == True) {
392 XtFree(charset);
393 if (sep == True) Done = True;
394 if (icount == a_number) {
395 ss = text;
396 break;
397 }
398 icount++;
399 XtFree(text);
400 }
401 else
402 Done = True;
403 }
404 XmStringFreeContext(context);
405 return ss;
406}
407/***************************************************************************/
408void XmTextAppendString(Widget This, char* a_string)
409/***************************************************************************/
410/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
411{
412 if (This == NULL) return;
413 if (! XtIsSubclass(This, xmTextWidgetClass)) return;
414 if (a_string == NULL) return;
415 XmTextPosition lastpos = XmTextGetLastPosition(This);
416 XmTextReplace(This, lastpos, lastpos, a_string);
417 XmTextSetInsertionPosition(This, XmTextGetLastPosition(This));
418}
419//////////////////////////////////////////////////////////////////////////////
420G4bool ConvertStringToInt(const char* aString, G4int& aInt)
421//////////////////////////////////////////////////////////////////////////////
422//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
423{
424 aInt = 0;
425 if (aString == NULL) return false;
426 char* s;
427 G4long value = strtol(aString, &s, 10);
428 if (s == aString) return false;
429 aInt = (G4int)value;
430 return true;
431}
432#include <X11/IntrinsicP.h>
433//////////////////////////////////////////////////////////////////////////////
434void ExecuteChangeSizeFunction(Widget aWidget)
435//////////////////////////////////////////////////////////////////////////////
436//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
437{
438 if (aWidget == NULL) return;
439 if (aWidget->core.widget_class->core_class.resize == NULL) return;
440 (aWidget->core.widget_class->core_class.resize)(aWidget);
441}
long G4long
Definition G4Types.hh:87
bool G4bool
Definition G4Types.hh:86
int G4int
Definition G4Types.hh:85
void * G4Interactor
void AddMenu(const char *, const char *)
Definition G4UIXm.cc:272
G4String GetCommand(Widget)
Definition G4UIXm.cc:310
virtual G4int ReceiveG4debug(const G4String &)
Definition G4UIXm.cc:222
~G4UIXm()
Definition G4UIXm.cc:146
G4UIXm(G4int, char **)
Definition G4UIXm.cc:66
virtual void PauseSessionStart(const G4String &)
Definition G4UIXm.cc:193
virtual G4int ReceiveG4cout(const G4String &)
Definition G4UIXm.cc:229
void Prompt(G4String)
Definition G4UIXm.cc:175
virtual G4int ReceiveG4cerr(const G4String &)
Definition G4UIXm.cc:237
G4UIsession * SessionStart()
Definition G4UIXm.cc:158
void SessionTerminate()
Definition G4UIXm.cc:188
void AddButton(const char *, const char *, const char *)
Definition G4UIXm.cc:295
void SetCoutDestination(G4UIsession *const value)
static G4UImanager * GetUIpointer()
void SetSession(G4UIsession *const value)
G4String Complete(const G4String &)
void ApplyShellCommand(const G4String &, G4bool &, G4bool &)
void AddInteractor(G4String, G4Interactor)
G4Interactor GetInteractor(G4String)
Definition G4Xt.hh:49
void * GetEvent()
Definition G4Xt.cc:136
static G4Xt * getInstance()
Definition G4Xt.cc:54