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 #include <string.h>
00029 #include <stdlib.h>
00030
00031 #include "G4UIdirectory.hh"
00032 #include "G4UIcommand.hh"
00033 #include "G4VInteractiveSession.hh"
00034
00035 #include "G4InteractorMessenger.hh"
00036
00037 #define STRDUP(str) ((str) != NULL ? (strcpy((char*)malloc((unsigned)strlen(str) + 1), str)) : (char*)NULL)
00038 #define STRDEL(str) {if((str)!=NULL) {free(str);str=NULL;}}
00039
00040 static G4bool GetValues (G4String,int,G4String*);
00041
00042 G4InteractorMessenger::G4InteractorMessenger (
00043 G4VInteractiveSession* a_session
00044 )
00045 {
00046 session = a_session;
00047
00048 G4UIparameter* parameter;
00049
00050 interactorDirectory = new G4UIdirectory("/gui/");
00051 interactorDirectory->SetGuidance("UI interactors commands.");
00052
00053
00054 addMenu = new G4UIcommand("/gui/addMenu",this);
00055 addMenu->SetGuidance("Add a menu to menu bar.");
00056 parameter = new G4UIparameter("Name",'s',false);
00057 parameter->SetDefaultValue("dummy");
00058 addMenu->SetParameter (parameter);
00059 parameter = new G4UIparameter("Label",'s',false);
00060 parameter->SetDefaultValue("dummy");
00061 addMenu->SetParameter (parameter);
00062
00063
00064 addButton = new G4UIcommand("/gui/addButton",this);
00065 addButton->SetGuidance("Add a button to menu.");
00066 parameter = new G4UIparameter("Menu",'s',false);
00067 parameter->SetDefaultValue("dummy");
00068 addButton->SetParameter (parameter);
00069 parameter = new G4UIparameter("Label",'s',false);
00070 parameter->SetDefaultValue("dummy");
00071 addButton->SetParameter (parameter);
00072 parameter = new G4UIparameter("Command",'s',false);
00073 parameter->SetDefaultValue("");
00074 addButton->SetParameter (parameter);
00075
00076
00077 sys = new G4UIcommand("/gui/system",this);
00078 sys->SetGuidance("Send a command to the system.");
00079 parameter = new G4UIparameter("Command",'s',false);
00080 parameter->SetDefaultValue("");
00081 sys->SetParameter (parameter);
00082
00083 }
00084
00085 G4InteractorMessenger::~G4InteractorMessenger()
00086 {
00087 delete addButton;
00088 delete addMenu;
00089 delete interactorDirectory;
00090 }
00091
00092 void G4InteractorMessenger::SetNewValue (
00093 G4UIcommand* command
00094 ,G4String newValue
00095 )
00096 {
00097 int paramn = command->GetParameterEntries();
00098 G4String* params = new G4String [paramn];
00099 if(GetValues(newValue,paramn,params)==true) {
00100 if(command==addMenu) {
00101 session->AddMenu((const char*)params[0],(const char*)params[1]);
00102 } else if(command==addButton) {
00103 session->AddButton((const char*)params[0],(const char*)params[1],(const char*)params[2]);
00104 } else if(command==sys) {
00105 system((const char*)params[0]);
00106 }
00107 }
00108 delete [] params;
00109 }
00110 G4bool GetValues (
00111 G4String newValue
00112 ,int paramn
00113 ,G4String* params
00114 )
00115 {
00116 char* value = STRDUP(newValue.data());
00117 if(value==NULL) return false;
00118 char* tok = strtok(value," ");
00119 for( int i=0; i<paramn;i++ ) {
00120 if(tok==NULL) {
00121 STRDEL(value);
00122 return false;
00123 }
00124 G4String token = tok;
00125 if( token(0)=='"' ) {
00126 while( token(token.length()-1) != '"' ) {
00127 tok = strtok(NULL," ");
00128 if( (tok==NULL) || (*tok=='\0')) {
00129 STRDEL(value);
00130 return false;
00131 }
00132 token += " ";
00133 token += tok;
00134 }
00135 token = (G4String)token.strip(G4String::both,'"');
00136 }
00137 if( token.isNull() ) {
00138 STRDEL(value);
00139 return false;
00140 } else {
00141 params[i] = token;
00142 }
00143 tok = strtok(NULL," ");
00144 }
00145 STRDEL(value);
00146 return true;
00147 }
00148
00149