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/addIcon :
00077   addIcon = new G4UIcommand("/gui/addIcon",this);
00078   addIcon->SetGuidance
00079   ("Add a non-checkable icon to the Icon toolbar.");
00080   addIcon->SetGuidance
00081   ("If the Icon parameter is set to \"user_icon\", you should provide the icon file in xpm format, otherwise you have to choose one of the candidate icons");
00082   addIcon->SetGuidance
00083   ("A command given without parameters will display a window that will allow one to choose the parameters (if needed) for this command.");
00084   addIcon->SetGuidance
00085   ("E.g: /gui/addIcon \"Change background color\" user_icon /vis/viewer/set/background ../Images/background.xpm");
00086   addIcon->SetGuidance
00087   ("Special cases for the Icon parameter:");
00088   addIcon->SetGuidance
00089   (" - open: Open an open-file-selector that can run the Command with File as argument.");
00090   addIcon->SetGuidance
00091   (" - save: Open a save-file-selector that can run the Command with File as argument.");
00092   addIcon->SetGuidance
00093   (" - move/rotate/pick/zoom_in/zoom_out: Theses icons are radio-button icons that can change cursor action.");
00094   addIcon->SetGuidance
00095   (" - wireframe/solid/hidden_line_removal/hidden_line_and_surface_removal: These icons are radio-button icons that can change drawing style.");
00096   addIcon->SetGuidance
00097   (" - perspective/ortho: These icons are radio-button icons that can change projection style.");
00098 
00099   parameter = new G4UIparameter("Label",'s',false);
00100   parameter->SetDefaultValue("");
00101   addIcon->SetParameter (parameter);
00102 
00103   parameter = new G4UIparameter("Icon",'s',false);
00104   parameter->SetDefaultValue("");
00105   parameter->SetParameterCandidates
00106   ("open save move rotate pick zoom_in zoom_out wireframe solid hidden_line_removal hidden_line_and_surface_removal perspective ortho user_icon");
00107   addIcon->SetParameter (parameter);
00108 
00109   parameter = new G4UIparameter("Command",'s',true);
00110   parameter->SetDefaultValue("no_command");
00111   addIcon->SetParameter (parameter);
00112 
00113   parameter = new G4UIparameter("File",'s',true);
00114   parameter->SetDefaultValue("no_file");
00115   addIcon->SetParameter (parameter);
00116 
00117   // /gui/system :
00118   sys = new G4UIcommand("/gui/system",this);
00119   sys->SetGuidance("Send a command to the system.");
00120   parameter = new G4UIparameter("Command",'s',false);
00121   parameter->SetDefaultValue("");
00122   sys->SetParameter (parameter);
00123 
00124 }
00125 
00126 G4InteractorMessenger::~G4InteractorMessenger()
00127 {
00128   delete addButton;
00129   delete addIcon;
00130   delete addMenu;
00131   delete interactorDirectory;
00132 }
00133 
00134 void G4InteractorMessenger::SetNewValue (
00135  G4UIcommand* command
00136 ,G4String newValue
00137 ) 
00138 {
00139   int paramn = command->GetParameterEntries();
00140   G4String* params = new G4String [paramn];
00141   if(GetValues(newValue,paramn,params)==true) {
00142     if(command==addMenu) {
00143       session->AddMenu((const char*)params[0],(const char*)params[1]);
00144     } else if(command==addButton) {
00145       session->AddButton((const char*)params[0],(const char*)params[1],(const char*)params[2]);
00146     } else if(command==addIcon) {
00147       session->AddIcon((const char*)params[0],(const char*)params[1],(const char*)params[2],(const char*)params[3]);
00148     } else if(command==sys) {
00149       system((const char*)params[0]);
00150     }
00151   }
00152   delete [] params;
00153 }
00154 G4bool GetValues (
00155  G4String newValue
00156 ,int paramn
00157 ,G4String* params
00158 ) 
00159 {
00160   char* value = STRDUP(newValue.data());
00161   if(value==NULL) return false;
00162   char* tok = strtok(value," ");
00163   for( int i=0; i<paramn;i++ ) {
00164     if(tok==NULL) {
00165       STRDEL(value);
00166       return false;
00167     }
00168     G4String token = tok;
00169     if( token(0)=='"' ) {
00170       while( token(token.length()-1) != '"' ) {
00171         tok = strtok(NULL," ");
00172         if( (tok==NULL) || (*tok=='\0')) {
00173           STRDEL(value);
00174           return false;
00175         }
00176         token += " ";
00177         token += tok;
00178       }
00179       token = (G4String)token.strip(G4String::both,'"');
00180     }
00181     if( token.isNull() ) {
00182       STRDEL(value);
00183       return false;
00184     } else { 
00185       params[i] = token;
00186     }
00187     tok = strtok(NULL," ");
00188   }
00189   STRDEL(value);
00190   return true;
00191 }
00192 
00193 

Generated on Mon May 27 17:48:38 2013 for Geant4 by  doxygen 1.4.7