Geant4  10.01.p02
G4InteractorMessenger.cc
Go to the documentation of this file.
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 //
27 
28 #include <string.h>
29 #include <stdlib.h>
30 
31 #include "G4UIdirectory.hh"
32 #include "G4UIcommand.hh"
33 #include "G4VInteractiveSession.hh"
34 
35 #include "G4InteractorMessenger.hh"
36 
37 #define STRDUP(str) ((str) != NULL ? (strcpy((char*)malloc((unsigned)strlen(str) + 1), str)) : (char*)NULL)
38 #define STRDEL(str) {if((str)!=NULL) {free(str);str=NULL;}}
39 
40 static G4bool GetValues (G4String,int,G4String*);
41 
43  G4VInteractiveSession* a_session
44 )
45 {
46  session = a_session;
47 
48  G4UIparameter* parameter;
49 
50  interactorDirectory = new G4UIdirectory("/gui/");
51  interactorDirectory->SetGuidance("UI interactors commands.");
52 
53  // /gui/addMenu :
54  addMenu = new G4UIcommand("/gui/addMenu",this);
55  addMenu->SetGuidance("Add a menu to menu bar.");
56  parameter = new G4UIparameter("Name",'s',false);
57  parameter->SetDefaultValue("dummy");
58  addMenu->SetParameter (parameter);
59  parameter = new G4UIparameter("Label",'s',false);
60  parameter->SetDefaultValue("dummy");
61  addMenu->SetParameter (parameter);
62 
63  // /gui/addButton :
64  addButton = new G4UIcommand("/gui/addButton",this);
65  addButton->SetGuidance("Add a button to menu.");
66  parameter = new G4UIparameter("Menu",'s',false);
67  parameter->SetDefaultValue("dummy");
68  addButton->SetParameter (parameter);
69  parameter = new G4UIparameter("Label",'s',false);
70  parameter->SetDefaultValue("dummy");
71  addButton->SetParameter (parameter);
72  parameter = new G4UIparameter("Command",'s',false);
73  parameter->SetDefaultValue("");
74  addButton->SetParameter (parameter);
75 
76  // /gui/addIcon :
77  addIcon = new G4UIcommand("/gui/addIcon",this);
79  ("Add a non-checkable icon to the Icon toolbar.");
81  ("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");
83  ("A command given without parameters will display a window that will allow one to choose the parameters (if needed) for this command.");
85  ("E.g: /gui/addIcon \"Change background color\" user_icon /vis/viewer/set/background ../Images/background.xpm");
87  ("Special cases for the Icon parameter:");
89  (" - open: Open an open-file-selector that can run the Command with File as argument.");
91  (" - save: Open a save-file-selector that can run the Command with File as argument.");
93  (" - move/rotate/pick/zoom_in/zoom_out: Theses icons are radio-button icons that can change cursor action.");
95  (" - wireframe/solid/hidden_line_removal/hidden_line_and_surface_removal: These icons are radio-button icons that can change drawing style.");
97  (" - perspective/ortho: These icons are radio-button icons that can change projection style.");
98 
99  parameter = new G4UIparameter("Label",'s',false);
100  parameter->SetDefaultValue("");
101  addIcon->SetParameter (parameter);
102 
103  parameter = new G4UIparameter("Icon",'s',false);
104  parameter->SetDefaultValue("");
105  parameter->SetParameterCandidates
106  ("open save move rotate pick zoom_in zoom_out wireframe solid hidden_line_removal hidden_line_and_surface_removal perspective ortho user_icon");
107  addIcon->SetParameter (parameter);
108 
109  parameter = new G4UIparameter("Command",'s',true);
110  parameter->SetDefaultValue("no_command");
111  addIcon->SetParameter (parameter);
112 
113  parameter = new G4UIparameter("File",'s',true);
114  parameter->SetDefaultValue("no_file");
115  addIcon->SetParameter (parameter);
116 
117  // /gui/system :
118  sys = new G4UIcommand("/gui/system",this);
119  sys->SetGuidance("Send a command to the system.");
120  parameter = new G4UIparameter("Command",'s',false);
121  parameter->SetDefaultValue("");
122  sys->SetParameter (parameter);
123 
124 }
125 
127 {
128  delete addButton;
129  delete addIcon;
130  delete addMenu;
131  delete interactorDirectory;
132 }
133 
135  G4UIcommand* command
136 ,G4String newValue
137 )
138 {
139  int paramn = command->GetParameterEntries();
140  G4String* params = new G4String [paramn];
141  if(GetValues(newValue,paramn,params)==true) {
142  if(command==addMenu) {
143  session->AddMenu((const char*)params[0],(const char*)params[1]);
144  } else if(command==addButton) {
145  session->AddButton((const char*)params[0],(const char*)params[1],(const char*)params[2]);
146  } else if(command==addIcon) {
147  session->AddIcon((const char*)params[0],(const char*)params[1],(const char*)params[2],(const char*)params[3]);
148  } else if(command==sys) {
149  system((const char*)params[0]);
150  }
151  }
152  delete [] params;
153 }
155  G4String newValue
156 ,int paramn
157 ,G4String* params
158 )
159 {
160  char* value = STRDUP(newValue.data());
161  if(value==NULL) return false;
162  char* tok = strtok(value," ");
163  for( int i=0; i<paramn;i++ ) {
164  if(tok==NULL) {
165  STRDEL(value);
166  return false;
167  }
168  G4String token = tok;
169  if( token(0)=='"' ) {
170  while( token(token.length()-1) != '"' ) {
171  tok = strtok(NULL," ");
172  if( (tok==NULL) || (*tok=='\0')) {
173  STRDEL(value);
174  return false;
175  }
176  token += " ";
177  token += tok;
178  }
179  token = (G4String)token.strip(G4String::both,'"');
180  }
181  if( token.isNull() ) {
182  STRDEL(value);
183  return false;
184  } else {
185  params[i] = token;
186  }
187  tok = strtok(NULL," ");
188  }
189  STRDEL(value);
190  return true;
191 }
192 
193 
void SetParameter(G4UIparameter *const newParameter)
Definition: G4UIcommand.hh:152
G4String strip(G4int strip_Type=trailing, char c=' ')
void SetParameterCandidates(const char *theString)
void SetDefaultValue(const char *theDefaultValue)
virtual void AddMenu(const char *, const char *)
G4UIdirectory * interactorDirectory
bool G4bool
Definition: G4Types.hh:79
void SetGuidance(const char *aGuidance)
Definition: G4UIcommand.hh:161
G4InteractorMessenger(G4VInteractiveSession *session)
const char * data() const
#define STRDUP(str)
virtual void AddButton(const char *, const char *, const char *)
static G4bool GetValues(G4String, int, G4String *)
void SetNewValue(G4UIcommand *command, G4String newValue)
#define STRDEL(str)
G4int GetParameterEntries() const
Definition: G4UIcommand.hh:143
virtual void AddIcon(const char *, const char *, const char *, const char *)
G4bool isNull() const
G4VInteractiveSession * session