source: trunk/source/interfaces/basic/src/G4UIXm.cc @ 1337

Last change on this file since 1337 was 1337, checked in by garnier, 14 years ago

tag geant4.9.4 beta 1 + modifs locales

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