source: trunk/source/interfaces/common/src/G4InteractorMessenger.cc @ 1228

Last change on this file since 1228 was 476, checked in by garnier, 17 years ago

r555@wl-72148: laurentgarnier | 2007-05-15 17:02:26 +0200
ajout de interfaces

File size: 4.8 KB
Line 
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
40static G4bool GetValues (G4String,int,G4String*); 
41
42G4InteractorMessenger::G4InteractorMessenger (
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/system :
77  sys = new G4UIcommand("/gui/system",this);
78  sys->SetGuidance("Send a command to the system.");
79  parameter = new G4UIparameter("Command",'s',false);
80  parameter->SetDefaultValue("");
81  sys->SetParameter (parameter);
82
83}
84
85G4InteractorMessenger::~G4InteractorMessenger()
86{
87  delete addButton;
88  delete addMenu;
89  delete interactorDirectory;
90}
91
92void G4InteractorMessenger::SetNewValue (
93 G4UIcommand* command
94,G4String newValue
95) 
96{
97  int paramn = command->GetParameterEntries();
98  G4String* params = new G4String [paramn];
99  if(GetValues(newValue,paramn,params)==true) {
100    if(command==addMenu) {
101      session->AddMenu((const char*)params[0],(const char*)params[1]);
102    } else if(command==addButton) {
103      session->AddButton((const char*)params[0],(const char*)params[1],(const char*)params[2]);
104    } else if(command==sys) {
105      system((const char*)params[0]);
106    }
107  }
108  delete [] params;
109}
110G4bool GetValues (
111 G4String newValue
112,int paramn
113,G4String* params
114) 
115{
116  char* value = STRDUP(newValue.data());
117  if(value==NULL) return false;
118  char* tok = strtok(value," ");
119  for( int i=0; i<paramn;i++ ) {
120    if(tok==NULL) {
121      STRDEL(value);
122      return false;
123    }
124    G4String token = tok;
125    if( token(0)=='"' ) {
126      while( token(token.length()-1) != '"' ) {
127        tok = strtok(NULL," ");
128        if( (tok==NULL) || (*tok=='\0')) {
129          STRDEL(value);
130          return false;
131        }
132        token += " ";
133        token += tok;
134      }
135      token = (G4String)token.strip(G4String::both,'"');
136    }
137    if( token.isNull() ) {
138      STRDEL(value);
139      return false;
140    } else { 
141      params[i] = token;
142    }
143    tok = strtok(NULL," ");
144  }
145  STRDEL(value);
146  return true;
147}
148
149
Note: See TracBrowser for help on using the repository browser.