00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include <stdlib.h>
00033 #include <string.h>
00034
00035 #include <algorithm>
00036
00037 #include "G4VInteractorManager.hh"
00038
00039 #define NewString(str) \
00040 ((str) != NULL ? (strcpy((char*)malloc((unsigned)strlen(str) + 1), str)) : NULL)
00041
00042
00043 G4VInteractorManager::G4VInteractorManager (
00044 )
00045 :argc(0)
00046 ,argv(NULL)
00047 ,mainInteractor(NULL)
00048 ,secondaryLoopEnabled(TRUE)
00049 ,alreadyInSecondaryLoop(FALSE)
00050 ,exitSecondaryLoop(0)
00051 ,parentInteractor(NULL)
00052 ,createdInteractor(NULL)
00053 ,creationString(NULL)
00054
00056 {
00057 }
00058
00059 G4VInteractorManager::~G4VInteractorManager (
00060 )
00061
00063 {
00064 if(argv!=NULL) {
00065 for(G4int argi=0;argi<argc;argi++) {
00066 if(argv[argi]!=NULL) free(argv[argi]);
00067 }
00068 free (argv);
00069 }
00070 argv = NULL;
00071 argc = 0;
00072 dispatchers.clear();
00073 preActions.clear();
00074 postActions.clear();
00075 shells.clear();
00076 secondaryLoopEnabled = TRUE;
00077 alreadyInSecondaryLoop = FALSE;
00078 exitSecondaryLoop = 0;
00079 }
00080
00081 void G4VInteractorManager::SetArguments (
00082 G4int a_argc
00083 ,char** a_argv
00084 )
00085
00087 {
00088
00089 if(argv!=NULL) {
00090 for(G4int argi=0;argi<argc;argi++) {
00091 if(argv[argi]!=NULL) free(argv[argi]);
00092 }
00093 free(argv);
00094 }
00095 argv = NULL;
00096 argc = 0;
00097
00098 if(a_argc!=0) {
00099 argv = (char**)malloc(a_argc * sizeof(char*));
00100 if(argv!=NULL) {
00101 argc = a_argc;
00102 for(G4int argi=0;argi<a_argc;argi++) {
00103 argv[argi] = (char*)NewString (a_argv[argi]);
00104 }
00105 }
00106 }
00107 }
00108
00109 char** G4VInteractorManager::GetArguments (
00110 G4int* a_argc
00111 )
00112
00114 {
00115 if(a_argc!=NULL) *a_argc = argc;
00116 return argv;
00117 }
00118
00119 void G4VInteractorManager::SetMainInteractor (
00120 G4Interactor a_main
00121 )
00122
00124 {
00125 mainInteractor = a_main;
00126 }
00127
00128 G4Interactor G4VInteractorManager::GetMainInteractor (
00129 )
00130
00132 {
00133 return mainInteractor;
00134 }
00135
00136 void G4VInteractorManager::EnableSecondaryLoop (
00137 )
00138
00140 {
00141 secondaryLoopEnabled = TRUE;
00142 }
00143
00144 void G4VInteractorManager::DisableSecondaryLoop (
00145 )
00146
00148 {
00149 secondaryLoopEnabled = FALSE;
00150 }
00151
00152 void G4VInteractorManager::AddDispatcher (
00153 G4DispatchFunction a_dispatcher
00154 )
00155
00157 {
00158 if(a_dispatcher==NULL) return;
00159 if(std::find(dispatchers.begin(),dispatchers.end(),a_dispatcher)!=dispatchers.end()) return;
00160 dispatchers.push_back(a_dispatcher);
00161 }
00162
00163 void G4VInteractorManager::RemoveDispatcher (
00164 G4DispatchFunction a_dispatcher
00165 )
00166
00168 {
00169 std::vector<G4DispatchFunction>::iterator it;
00170 for (it = dispatchers.begin(); it != dispatchers.end(); it++) {
00171 if (*it == a_dispatcher) {
00172 dispatchers.erase(it);
00173 break;
00174 }
00175 }
00176 }
00177
00178 void G4VInteractorManager::DispatchEvent (
00179 void* a_event
00180 )
00181
00183 {
00184 G4int dispatchern = dispatchers.size();
00185 G4DispatchFunction func;
00186 for(G4int count=0;count<dispatchern;count++) {
00187 func = dispatchers[count];
00188 if(func!=NULL) {
00189 if(func(a_event)==true) return;
00190 }
00191 }
00192 }
00193
00194 void G4VInteractorManager::AddSecondaryLoopPreAction (
00195 G4SecondaryLoopAction a_preAction
00196 )
00197
00199 {
00200 if(a_preAction==NULL) return;
00201 if(std::find(preActions.begin(),preActions.end(),a_preAction)!=preActions.end()) return;
00202 preActions.push_back(a_preAction);
00203 }
00204
00205 void G4VInteractorManager::SecondaryLoopPreActions (
00206 )
00207
00209 {
00210 G4int preActionn = preActions.size();
00211 for(G4int count=0;count<preActionn;count++) {
00212 if(preActions[count]!=NULL) preActions[count]();
00213 }
00214 }
00215
00216 void G4VInteractorManager::AddSecondaryLoopPostAction (
00217 G4SecondaryLoopAction a_postAction
00218 )
00219
00221 {
00222 if(a_postAction==NULL) return;
00223 if(std::find(postActions.begin(),postActions.end(),a_postAction)!=postActions.end()) return;
00224 postActions.push_back(a_postAction);
00225 }
00226
00227 void G4VInteractorManager::SecondaryLoopPostActions (
00228 )
00229
00231 {
00232 G4int postActionn = postActions.size();
00233 for(G4int count=0;count<postActionn;count++) {
00234 if(postActions[count]!=NULL) postActions[count]();
00235 }
00236 }
00237
00238 void G4VInteractorManager::SecondaryLoop (
00239 )
00240
00242 {
00243 if(Inited()==FALSE) return;
00244
00245 if(secondaryLoopEnabled==FALSE) return;
00246
00247 if (alreadyInSecondaryLoop==FALSE) {
00248 G4cout << "------------------------------------------" << G4endl;
00249 G4cout << "You have entered a viewer secondary X event loop." << G4endl;
00250 G4cout << "Quit it with an 'Escape' viewer button" << G4endl;
00251 alreadyInSecondaryLoop = TRUE;
00252 exitSecondaryLoop = 0;
00253 SecondaryLoopPreActions ();
00254
00255 void* event;
00256 while(1) {
00257 event = GetEvent();
00258 if(event==NULL) break;
00259 DispatchEvent (event);
00260 if(exitSecondaryLoop!=0) break;
00261 }
00262 G4cout << "Secondary X event loop exited." << G4endl;
00263 SecondaryLoopPostActions ();
00264 }
00265 }
00266
00267 void G4VInteractorManager::RequireExitSecondaryLoop (
00268 G4int a_code
00269 )
00270
00272 {
00273 if(secondaryLoopEnabled==FALSE) return;
00274 if(a_code==0) a_code = 1;
00275 exitSecondaryLoop = a_code;
00276 alreadyInSecondaryLoop = FALSE;
00277
00278
00279 }
00280
00281 G4int G4VInteractorManager::GetExitSecondaryLoopCode (
00282 )
00283
00285 {
00286 return exitSecondaryLoop;
00287 }
00288
00289 void G4VInteractorManager::AddShell (
00290 G4Interactor a_shell
00291 )
00292
00294 {
00295 if(a_shell==NULL) return;
00296 if(std::find(shells.begin(),shells.end(),a_shell)!=shells.end()) return;
00297 shells.push_back(a_shell);
00298 }
00299
00300 void G4VInteractorManager::RemoveShell (
00301 G4Interactor a_shell
00302 )
00303
00305 {
00306 std::vector<G4Interactor>::iterator it;
00307 for (it = shells.begin(); it != shells.end(); it++) {
00308 if (*it == a_shell) {
00309 shells.erase(it);
00310 break;
00311 }
00312 }
00313 }
00314
00315 void G4VInteractorManager::SetParentInteractor (
00316 G4Interactor a_interactor
00317 )
00318
00320 {
00321 parentInteractor = a_interactor;
00322 }
00323
00324 G4Interactor G4VInteractorManager::GetParentInteractor (
00325 )
00326
00328 {
00329 return parentInteractor;
00330 }
00331
00332 void G4VInteractorManager::SetCreatedInteractor (
00333 G4Interactor a_interactor
00334 )
00335
00337 {
00338 createdInteractor = a_interactor;
00339 }
00340
00341 G4Interactor G4VInteractorManager::GetCreatedInteractor (
00342 )
00343
00345 {
00346 return createdInteractor;
00347 }
00348
00349 void G4VInteractorManager::SetCreationString (
00350 char* a_string
00351 )
00352
00354 {
00355 creationString = a_string;
00356 }
00357
00358 char* G4VInteractorManager::GetCreationString (
00359 )
00360
00362 {
00363 return creationString;
00364 }
00365