Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

G4InteractorMessenger.cc

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
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   // /gui/addMenu :
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   // /gui/addButton :
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   // /gui/system :
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 

Generated on Fri Jun 22 11:07:01 2007 by doxygen 1.3.4