Geant4  10.03
G4Xt.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 // $Id: G4Xt.cc 66892 2013-01-17 10:57:59Z gunter $
28 //
29 // G.Barrand
30 
31 #if defined(G4INTY_BUILD_XT) || defined(G4INTY_USE_XT)
32 
33 #include <stdlib.h>
34 #include <string.h>
35 
36 #include <X11/Intrinsic.h>
37 #include <X11/Shell.h>
38 
39 #include "G4ios.hh"
40 
41 #include "G4Xt.hh"
42 
43 #define NewString(str) \
44  ((str) != NULL ? (strcpy((char*)malloc((unsigned)strlen(str) + 1), str)) : NULL)
45 
46 //static void XWidgetIconify (Widget);
47 //static void XWidgetUniconify (Widget);
48 //static void XDisplaySetWindowToNormalState (Display*,Window);
49 
50 G4Xt* G4Xt::instance = NULL;
51 
52 static G4bool XtInited = FALSE;
53 static int argn = 0;
54 static char** args = NULL;
55 static XtAppContext appContext = NULL;
56 static Widget topWidget = NULL;
57 /***************************************************************************/
58 G4Xt* G4Xt::getInstance (
59 )
60 /***************************************************************************/
62 {
63  return G4Xt::getInstance (0,NULL,(char*)"Geant4");
64 }
65 /***************************************************************************/
66 G4Xt* G4Xt::getInstance (
67  int a_argn
68 ,char** a_args
69 ,char* a_class
70 )
71 /***************************************************************************/
73 {
74  if (instance==NULL) {
75  instance = new G4Xt(a_argn,a_args,a_class);
76  }
77  return instance;
78 }
79 /***************************************************************************/
80 G4Xt::G4Xt (
81  int a_argn
82 ,char** a_args
83 ,char* a_class
84 )
85 /***************************************************************************/
87 {
88  if(XtInited==FALSE) { //Xt should be Inited once !
89  if(a_argn!=0) { //Save args.
90  args = (char**)malloc(a_argn * sizeof(char*));
91  if(args!=NULL) {
92  argn = a_argn;
93  for(int argi=0;argi<a_argn;argi++) {
94  args[argi] = (char*)NewString (a_args[argi]);
95  }
96  }
97  }
98 #if XtSpecificationRelease == 4
99  Cardinal narg;
100  narg = (Cardinal)a_argn;
101 #else
102  int narg;
103  narg = a_argn;
104 #endif
105  Arg xargs[1];
106  XtSetArg (xargs[0],XtNgeometry,"100x100");
107  topWidget = XtAppInitialize (&appContext,a_class,
108  NULL,(Cardinal)0,
109  &narg,a_args,NULL,
110  xargs,1);
111  if(topWidget==NULL) {
112  G4cout << "G4Xt : Unable to init Xt." << G4endl;
113  }
114  // Restore a_args. XtAppInitialize corrupts the given ones !!!
115  if( (a_argn!=0) && (args!=NULL)) {
116  for(int argi=0;argi<a_argn;argi++) {
117  if(args[argi]!=NULL)
118  strcpy(a_args[argi],args[argi]);
119  else
120  a_args[argi] = NULL;
121  }
122  }
123  // If topWidget not realized, pbs with Inventor shells.
124  XtSetMappedWhenManaged (topWidget,False);
125  XtRealizeWidget (topWidget);
126  XtInited = TRUE;
127  }
128  SetArguments (argn,args);
129  SetMainInteractor (topWidget);
130  AddDispatcher ((G4DispatchFunction)XtDispatchEvent);
131 }
132 /***************************************************************************/
133 G4Xt::~G4Xt (
134 )
135 /***************************************************************************/
137 {
138  if(this==instance) {
139  instance = NULL;
140  }
141 }
142 /***************************************************************************/
143 G4bool G4Xt::Inited (
144 )
145 /***************************************************************************/
147 {
148  return XtInited;
149 }
150 /***************************************************************************/
151 void* G4Xt::GetEvent (
152 )
153 /***************************************************************************/
155 {
156  static XEvent event;
157  if(appContext==NULL) return NULL;
158  if(topWidget==NULL) return NULL;
159  XtAppNextEvent (appContext, &event);
160  return &event;
161 }
162 /***************************************************************************/
163 void G4Xt::PutStringInResourceDatabase (
164  char* a_string
165 )
166 /***************************************************************************/
168 {
169  if(topWidget==NULL) return;
170  if(a_string==NULL) return;
171  Display* dpy = XtDisplay(topWidget);
172  XrmDatabase dbres = XrmGetStringDatabase (a_string);
173  if(dbres==NULL) return;
174  XrmDatabase database = XrmGetDatabase (dpy);
175  if(database!=NULL) {
176  XrmMergeDatabases (dbres,&database);
177  } else {
178  XrmSetDatabase (dpy,dbres);
179  }
180 }
181 /***************************************************************************/
182 void G4Xt::FlushAndWaitExecution (
183 )
184 /***************************************************************************/
186 {
187  if(topWidget==NULL) return;
188  XSync(XtDisplay(topWidget),False);
189 }
190 
191 #endif
192 
193 
194 
#define NewString(str)
G4bool(* G4DispatchFunction)(void *)
G4GLOB_DLL std::ostream G4cout
bool G4bool
Definition: G4Types.hh:79
#define FALSE
Definition: globals.hh:52
#define TRUE
Definition: globals.hh:55
#define G4endl
Definition: G4ios.hh:61
static MCTruthManager * instance