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