Geant4  10.02.p02
G4VInteractorManager.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: G4VInteractorManager.cc 66892 2013-01-17 10:57:59Z gunter $
28 //
29 // G.Barrand
30 
31 #include <stdlib.h>
32 #include <string.h>
33 
34 #include <algorithm>
35 
36 #include "G4VInteractorManager.hh"
37 
38 #define NewString(str) \
39  ((str) != NULL ? (strcpy((char*)malloc((unsigned)strlen(str) + 1), str)) : NULL)
40 
41 /***************************************************************************/
43 )
44 :argc(0)
45 ,argv(NULL)
46 ,mainInteractor(NULL)
47 ,secondaryLoopEnabled(TRUE)
48 ,alreadyInSecondaryLoop(FALSE)
49 ,exitSecondaryLoop(0)
50 ,parentInteractor(NULL)
51 ,createdInteractor(NULL)
52 ,creationString(NULL)
53 /***************************************************************************/
55 {
56 }
57 /***************************************************************************/
59 )
60 /***************************************************************************/
62 {
63  if(argv!=NULL) {
64  for(G4int argi=0;argi<argc;argi++) {
65  if(argv[argi]!=NULL) free(argv[argi]);
66  }
67  free (argv);
68  }
69  argv = NULL;
70  argc = 0;
71  dispatchers.clear();
72  preActions.clear();
73  postActions.clear();
74  shells.clear();
78 }
79 /***************************************************************************/
81  G4int a_argc
82 ,char** a_argv
83 )
84 /***************************************************************************/
86 {
87  // Free previous values.
88  if(argv!=NULL) {
89  for(G4int argi=0;argi<argc;argi++) {
90  if(argv[argi]!=NULL) free(argv[argi]);
91  }
92  free(argv);
93  }
94  argv = NULL;
95  argc = 0;
96  // Set new values.
97  if(a_argc!=0) {
98  argv = (char**)malloc(a_argc * sizeof(char*));
99  if(argv!=NULL) {
100  argc = a_argc;
101  for(G4int argi=0;argi<a_argc;argi++) {
102  argv[argi] = (char*)NewString (a_argv[argi]);
103  }
104  }
105  }
106 }
107 /***************************************************************************/
109  G4int* a_argc
110 )
111 /***************************************************************************/
113 {
114  if(a_argc!=NULL) *a_argc = argc;
115  return argv;
116 }
117 /***************************************************************************/
119  G4Interactor a_main
120 )
121 /***************************************************************************/
123 {
124  mainInteractor = a_main;
125 }
126 /***************************************************************************/
128 )
129 /***************************************************************************/
131 {
132  return mainInteractor;
133 }
134 /***************************************************************************/
136 )
137 /***************************************************************************/
139 {
141 }
142 /***************************************************************************/
144 )
145 /***************************************************************************/
147 {
149 }
150 /***************************************************************************/
152  G4DispatchFunction a_dispatcher
153 )
154 /***************************************************************************/
156 {
157  if(a_dispatcher==NULL) return;
158  if(std::find(dispatchers.begin(),dispatchers.end(),a_dispatcher)!=dispatchers.end()) return;
159  dispatchers.push_back(a_dispatcher);
160 }
161 /***************************************************************************/
163  G4DispatchFunction a_dispatcher
164 )
165 /***************************************************************************/
167 {
168  std::vector<G4DispatchFunction>::iterator it;
169  for (it = dispatchers.begin(); it != dispatchers.end(); it++) {
170  if (*it == a_dispatcher) {
171  dispatchers.erase(it);
172  break;
173  }
174  }
175 }
176 /***************************************************************************/
178  void* a_event
179 )
180 /***************************************************************************/
182 {
183  G4int dispatchern = dispatchers.size();
184  G4DispatchFunction func;
185  for(G4int count=0;count<dispatchern;count++) {
186  func = dispatchers[count];
187  if(func!=NULL) {
188  if(func(a_event)==true) return;
189  }
190  }
191 }
192 /***************************************************************************/
194  G4SecondaryLoopAction a_preAction
195 )
196 /***************************************************************************/
198 {
199  if(a_preAction==NULL) return;
200  if(std::find(preActions.begin(),preActions.end(),a_preAction)!=preActions.end()) return;
201  preActions.push_back(a_preAction);
202 }
203 /***************************************************************************/
205 )
206 /***************************************************************************/
208 {
209  G4int preActionn = preActions.size();
210  for(G4int count=0;count<preActionn;count++) {
211  if(preActions[count]!=NULL) preActions[count]();
212  }
213 }
214 /***************************************************************************/
216  G4SecondaryLoopAction a_postAction
217 )
218 /***************************************************************************/
220 {
221  if(a_postAction==NULL) return;
222  if(std::find(postActions.begin(),postActions.end(),a_postAction)!=postActions.end()) return;
223  postActions.push_back(a_postAction);
224 }
225 /***************************************************************************/
227 )
228 /***************************************************************************/
230 {
231  G4int postActionn = postActions.size();
232  for(G4int count=0;count<postActionn;count++) {
233  if(postActions[count]!=NULL) postActions[count]();
234  }
235 }
236 /***************************************************************************/
238 )
239 /***************************************************************************/
241 {
242  if(Inited()==FALSE) return;
243 
244  if(secondaryLoopEnabled==FALSE) return;
245 
247  G4cout << "------------------------------------------" << G4endl;
248  G4cout << "You have entered a viewer secondary X event loop." << G4endl;
249  G4cout << "Quit it with an 'Escape' viewer button" << G4endl;
251  exitSecondaryLoop = 0;
253  //for(G4int count=0;count<shelln;count++) XWidgetUniconify(shells[count]);
254  void* event;
255  while(1) {
256  event = GetEvent();
257  if(event==NULL) break;
258  DispatchEvent (event);
259  if(exitSecondaryLoop!=0) break;
260  }
261  G4cout << "Secondary X event loop exited." << G4endl;
263  }
264 }
265 /***************************************************************************/
267  G4int a_code
268 )
269 /***************************************************************************/
271 {
272  if(secondaryLoopEnabled==FALSE) return;
273  if(a_code==0) a_code = 1;
274  exitSecondaryLoop = a_code;
276  // for(G4int count=0;count<shelln;count++) XWidgetIconify(shells[count]);
277  // if(shelln!=0) XSync(XtDisplay(topWidget),False);
278 }
279 /***************************************************************************/
281 )
282 /***************************************************************************/
284 {
285  return exitSecondaryLoop;
286 }
287 /***************************************************************************/
289  G4Interactor a_shell
290 )
291 /***************************************************************************/
293 {
294  if(a_shell==NULL) return;
295  if(std::find(shells.begin(),shells.end(),a_shell)!=shells.end()) return;
296  shells.push_back(a_shell);
297 }
298 /***************************************************************************/
300  G4Interactor a_shell
301 )
302 /***************************************************************************/
304 {
305  std::vector<G4Interactor>::iterator it;
306  for (it = shells.begin(); it != shells.end(); it++) {
307  if (*it == a_shell) {
308  shells.erase(it);
309  break;
310  }
311  }
312 }
313 /***************************************************************************/
315  G4Interactor a_interactor
316 )
317 /***************************************************************************/
319 {
320  parentInteractor = a_interactor;
321 }
322 /***************************************************************************/
324 )
325 /***************************************************************************/
327 {
328  return parentInteractor;
329 }
330 /***************************************************************************/
332  G4Interactor a_interactor
333 )
334 /***************************************************************************/
336 {
337  createdInteractor = a_interactor;
338 }
339 /***************************************************************************/
341 )
342 /***************************************************************************/
344 {
345  return createdInteractor;
346 }
347 /***************************************************************************/
349  char* a_string
350 )
351 /***************************************************************************/
353 {
354  creationString = a_string;
355 }
356 /***************************************************************************/
358 )
359 /***************************************************************************/
361 {
362  return creationString;
363 }
364 
void RemoveShell(G4Interactor)
void(* G4SecondaryLoopAction)()
std::vector< G4Interactor > shells
G4Interactor GetMainInteractor()
void AddShell(G4Interactor)
int G4int
Definition: G4Types.hh:78
virtual G4bool Inited()=0
void * G4Interactor
void SetMainInteractor(G4Interactor)
#define NewString(str)
void AddSecondaryLoopPostAction(G4SecondaryLoopAction)
G4bool(* G4DispatchFunction)(void *)
void SetCreatedInteractor(G4Interactor)
G4GLOB_DLL std::ostream G4cout
std::vector< G4SecondaryLoopAction > postActions
void SetParentInteractor(G4Interactor)
#define FALSE
Definition: globals.hh:52
#define TRUE
Definition: globals.hh:55
virtual void * GetEvent()=0
void SetArguments(int, char **)
void AddSecondaryLoopPreAction(G4SecondaryLoopAction)
void AddDispatcher(G4DispatchFunction)
std::vector< G4SecondaryLoopAction > preActions
G4Interactor GetParentInteractor()
void RemoveDispatcher(G4DispatchFunction)
#define G4endl
Definition: G4ios.hh:61
std::vector< G4DispatchFunction > dispatchers
G4Interactor GetCreatedInteractor()