Geant4_10
G4OpenInventorXtViewer.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 // $Id: G4OpenInventorXtViewer.cc 66373 2012-12-18 09:41:34Z gcosmo $
28 //
29 /*
30  * jck 05 Feb 1997 - Initial Implementation
31  * jck 21 Apr 1997
32  * Mods for SoXtHepViewer
33  * gb : on Win32 use an SoXtExaminerViewer.
34  * gb 05 April 2004 : revisit to separate Windows things.
35  * gb 09 November 2004 : restore the escape button.
36  * gb 09 November 2004 : have a menu bar in the viewer shell.
37  * gb 09 November 2004 : have gl2ps file production.
38  * gb 14 November 2004 : inherit G4OpenInventorViewer.
39  */
40 
41 #ifdef G4VIS_BUILD_OIX_DRIVER
42 
43 // this :
45 
46 #include <Inventor/nodes/SoSelection.h>
47 
48 #include <Inventor/Xt/SoXt.h>
49 #include <Inventor/Xt/viewers/SoXtExaminerViewer.h>
50 
51 #include <X11/StringDefs.h>
52 #include <X11/Shell.h>
53 
54 #include <Xm/Xm.h>
55 #include <Xm/PushB.h>
56 #include <Xm/Form.h>
57 #include <Xm/CascadeB.h>
58 #include <Xm/RowColumn.h>
59 #include <Xm/Text.h>
60 
62 
63 #include "G4OpenInventor.hh"
65 #include "G4VInteractorManager.hh"
66 #include "G4VisManager.hh"
67 
68 G4OpenInventorXtViewer::G4OpenInventorXtViewer(
69  G4OpenInventorSceneHandler& sceneHandler
70 ,const G4String& name)
71 :G4OpenInventorViewer (sceneHandler, name)
72 ,fShell(0)
73 ,fViewer(0)
74 ,fHelpForm(0)
75 ,fHelpText(0)
76 {
78  G4cout << "Window name: " << fName << G4endl;
79 }
80 
81 
82 void G4OpenInventorXtViewer::Initialise() {
83 
84  G4String wName = fName;
85 
86  Widget parent = (Widget)fInteractorManager->GetParentInteractor ();
87  int width = 600;
88  int height = 600;
89 
90  if(!parent) {
91  // Check if user has specified an X-Windows-type geometry string...
92  char str[32];
93 
94  G4String sgeometry = fVP.GetXGeometryString();
95  if(sgeometry.empty()) {
96  G4cout << "ERROR: Geometry string \""
97  << sgeometry
98  << "\" is empty. Using \"600x600\"."
99  << G4endl;
100  width = 600;
101  height = 600;
102  sprintf(str,"%dx%d",width,height);
103  sgeometry = str;
104  } else {
105  width = fVP.GetWindowSizeHintX();
106  height = fVP.GetWindowSizeHintX();
107  }
108 
109  //Create a shell window :
110  G4String shellName = wName;
111  shellName += "_shell";
112  Arg args[10];
113  XtSetArg(args[0],XtNgeometry,XtNewString(sgeometry.c_str()));
114  XtSetArg(args[1],XtNborderWidth,0);
115  XtSetArg(args[2],XtNtitle,XtNewString(wName.c_str()));
116  fShell = XtAppCreateShell(shellName.c_str(),"Inventor",
117  topLevelShellWidgetClass,
118  SoXt::getDisplay(),
119  args,3);
120 
121  XtSetArg(args[0],XmNtopAttachment ,XmATTACH_FORM);
122  XtSetArg(args[1],XmNleftAttachment ,XmATTACH_FORM);
123  XtSetArg(args[2],XmNrightAttachment ,XmATTACH_FORM);
124  XtSetArg(args[3],XmNbottomAttachment,XmATTACH_FORM);
125  Widget form = XmCreateForm (fShell,(char*)"form",args,4);
126  XtManageChild (form);
127 
128  XtSetArg(args[0],XmNtopAttachment ,XmATTACH_FORM);
129  XtSetArg(args[1],XmNleftAttachment ,XmATTACH_FORM);
130  XtSetArg(args[2],XmNrightAttachment ,XmATTACH_FORM);
131  Widget menuBar = XmCreateMenuBar (form,(char*)"menuBar",args,3);
132  XtManageChild(menuBar);
133 
134  {Widget menu = AddMenu(menuBar,"File","File");
135  AddButton(menu,"PS (gl2ps)",PostScriptCbk);
136  AddButton(menu,"PS (pixmap)",PixmapPostScriptCbk);
137  AddButton(menu,"IV",WriteInventorCbk);
138  AddButton(menu,"Escape",EscapeCbk);}
139 
140  {Widget menu = AddMenu(menuBar,"Etc","Etc");
141  AddButton(menu,"Erase detector",EraseDetectorCbk);
142  AddButton(menu,"Erase event",EraseEventCbk);
143  AddButton(menu,"Set solid",SetSolidCbk);
144 /* AddButton(menu,"Set (G4) wire frame",SetWireFrameCbk);*/
145  AddButton(menu,"Set (G4) reduced wire frame",SetReducedWireFrameCbk);
146  AddButton(menu,"Set (G4) full wire frame",SetFullWireFrameCbk);
147  AddButton(menu,"Visible mothers + invisible daughters",SetPreviewCbk);
148  AddButton(menu,"Visible mothers + visible daughters",SetPreviewAndFullCbk);
149  AddButton(menu,"Update scene",UpdateSceneCbk);
150  AddButton(menu,"Scene graph stats",SceneGraphStatisticsCbk);
151  }
152 
153  {Widget menu = AddMenu(menuBar,"Help","Help");
154  AddButton(menu,"Controls",HelpCbk);}
155 
156  fViewer = new SoXtExaminerViewer(form,wName.c_str(),TRUE);
157 
158  XtSetArg(args[0],XmNtopAttachment ,XmATTACH_WIDGET);
159  XtSetArg(args[1],XmNtopWidget ,menuBar);
160  XtSetArg(args[2],XmNleftAttachment ,XmATTACH_FORM);
161  XtSetArg(args[3],XmNrightAttachment ,XmATTACH_FORM);
162  XtSetArg(args[4],XmNbottomAttachment,XmATTACH_FORM);
163  XtSetValues(fViewer->getWidget(),args,5);
164 
165  fHelpForm = XmCreateFormDialog(fShell,(char*)"help",NULL,0);
166  XtSetArg(args[0],XmNleftAttachment ,XmATTACH_FORM);
167  XtSetArg(args[1],XmNrightAttachment ,XmATTACH_FORM);
168  XtSetArg(args[2],XmNbottomAttachment,XmATTACH_FORM);
169  Widget cancel = XmCreatePushButton(fHelpForm,(char*)"helpCancel",args,3);
170  XtAddCallback(cancel,XmNactivateCallback,HelpCancelCbk,(XtPointer)this);
171  XtManageChild(cancel);
172  XtSetArg(args[0],XmNtopAttachment ,XmATTACH_FORM);
173  XtSetArg(args[1],XmNleftAttachment ,XmATTACH_FORM);
174  XtSetArg(args[2],XmNrightAttachment ,XmATTACH_FORM);
175  XtSetArg(args[3],XmNbottomAttachment,XmATTACH_WIDGET);
176  XtSetArg(args[4],XmNbottomWidget ,cancel);
177  fHelpText = XmCreateScrolledText(fHelpForm,(char*)"helpText",args,5);
178  XtManageChild(fHelpText);
179 
180  fInteractorManager->AddShell(fShell);
181 
182  } else {
183  char* str = fInteractorManager->GetCreationString();
184  if(str!=0) wName = str;
185  fViewer = new SoXtExaminerViewer(parent,wName.c_str(),TRUE);
186  }
187 
188  fViewer->setSize(SbVec2s(width,height));
189 
190  // Have a GL2PS render action :
191  const SbViewportRegion& vpRegion = fViewer->getViewportRegion();
192  fGL2PSAction = new SoGL2PSAction(vpRegion);
193  fViewer->setGLRenderAction(fGL2PSAction);
194 
195  // Else :
196  fViewer->setSceneGraph(fSoSelection);
197  fViewer->viewAll();
198  fViewer->saveHomePosition();
199  fViewer->setTitle(fName);
200  fViewer->show();
201  if(fShell) {
202  SoXt::show(fShell);
203  fInteractorManager->FlushAndWaitExecution ();
204  }
205  fInteractorManager->SetCreatedInteractor (fViewer -> getWidget());
206 }
207 
208 G4OpenInventorXtViewer::~G4OpenInventorXtViewer () {
209  if(fShell) fInteractorManager->RemoveShell(fShell);
210  if(fViewer) {
211  fViewer->setSceneGraph(0);
212  //FIXME : SGI : the below "delete" block things.
213  //FIXME : CoinXt : the below "delete" crashe in ~SoXtRenderArea.
214  //FIXME : delete fViewer;
215  }
216  if(fShell) XtDestroyWidget(fShell);
217 }
218 
219 void G4OpenInventorXtViewer::FinishView () {
220  if(!fViewer) return;
221  fViewer->viewAll();
222  fViewer->saveHomePosition();
223 }
224 
225 void G4OpenInventorXtViewer::SetView () {
226  G4OpenInventorViewer::SetView ();
227  if(!fViewer) return;
228  // Background.
229  G4Colour b = fVP.GetBackgroundColour ();
230  fViewer->setBackgroundColor
231  (SbColor((float)b.GetRed(),(float)b.GetGreen(),(float)b.GetBlue()));
232 }
233 
234 
235 void G4OpenInventorXtViewer::ViewerRender () {
236  if(!fViewer) return;
237  fViewer->render();
238 }
239 
240 SoCamera* G4OpenInventorXtViewer::GetCamera () {
241  if(!fViewer) return 0;
242  return fViewer->getCamera();
243 }
244 
245 Widget G4OpenInventorXtViewer::AddMenu(
246  Widget aMenuBar
247 ,const G4String& aName
248 ,const G4String& aLabel
249 )
250 {
251  // Pulldown menu :
252  Widget menu = XmCreatePulldownMenu(aMenuBar,(char*)aName.c_str(),NULL,0);
253  // Cascade button :
254  Arg args[2];
255  XmString cps =
256  XmStringLtoRCreate((char*)aLabel.c_str(),XmSTRING_DEFAULT_CHARSET);
257  XtSetArg (args[0],XmNlabelString,cps);
258  XtSetArg (args[1],XmNsubMenuId,menu);
259  Widget widget = XmCreateCascadeButton(aMenuBar,(char*)aName.c_str(),args,2);
260  XmStringFree (cps);
261  XtManageChild(widget);
262  return menu;
263 }
264 void G4OpenInventorXtViewer::AddButton (
265  Widget aMenu
266 ,const G4String& aLabel
267 ,XtCallbackProc aCallback
268 )
269 {
270  Widget widget = XmCreatePushButton(aMenu,(char*)aLabel.c_str(),NULL,0);
271  XtManageChild(widget);
272  XtAddCallback(widget,XmNactivateCallback,aCallback,(XtPointer)this);
273 }
274 
275 void G4OpenInventorXtViewer::HelpCancelCbk(
276  Widget,XtPointer aData,XtPointer) {
277  G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
278  XtUnmanageChild(This->fHelpForm);
279 }
280 
281 
285 
286 void G4OpenInventorXtViewer::EscapeCbk(
287  Widget,XtPointer aData,XtPointer) {
288  G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
289  This->Escape();
290 }
291 
292 void G4OpenInventorXtViewer::PostScriptCbk(
293  Widget,XtPointer aData,XtPointer) {
294  G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
295  This->WritePostScript();
296 }
297 
298 void G4OpenInventorXtViewer::PixmapPostScriptCbk(
299  Widget,XtPointer aData,XtPointer) {
300  G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
301  This->WritePixmapPostScript();
302 }
303 
304 void G4OpenInventorXtViewer::SceneGraphStatisticsCbk(
305  Widget,XtPointer aData,XtPointer) {
306  G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
307  This->SceneGraphStatistics();
308 }
309 
310 void G4OpenInventorXtViewer::WriteInventorCbk(
311  Widget,XtPointer aData,XtPointer) {
312  G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
313  This->WriteInventor();
314 }
315 
316 void G4OpenInventorXtViewer::EraseDetectorCbk(
317  Widget,XtPointer aData,XtPointer) {
318  G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
319  This->EraseDetector();
320 }
321 
322 void G4OpenInventorXtViewer::EraseEventCbk(
323  Widget,XtPointer aData,XtPointer) {
324  G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
325  This->EraseEvent();
326 }
327 
328 void G4OpenInventorXtViewer::SetSolidCbk(
329  Widget,XtPointer aData,XtPointer) {
330  G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
331  This->SetSolid();
332 }
333 
334 void G4OpenInventorXtViewer::SetWireFrameCbk(
335  Widget,XtPointer aData,XtPointer) {
336  G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
337  This->SetWireFrame();
338 }
339 
340 void G4OpenInventorXtViewer::SetReducedWireFrameCbk(
341  Widget,XtPointer aData,XtPointer) {
342  G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
343  This->SetReducedWireFrame(true);
344 }
345 
346 void G4OpenInventorXtViewer::SetFullWireFrameCbk(
347  Widget,XtPointer aData,XtPointer) {
348  G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
349  This->SetReducedWireFrame(false);
350 }
351 
352 void G4OpenInventorXtViewer::UpdateSceneCbk(
353  Widget,XtPointer aData,XtPointer) {
354  G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
355  This->UpdateScene();
356 }
357 
358 void G4OpenInventorXtViewer::SetPreviewCbk(
359  Widget,XtPointer aData,XtPointer) {
360  G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
361  This->SetPreview();
362 }
363 
364 void G4OpenInventorXtViewer::SetPreviewAndFullCbk(
365  Widget,XtPointer aData,XtPointer) {
366  G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
367  This->SetPreviewAndFull();
368 }
369 
370 void G4OpenInventorXtViewer::HelpCbk(
371  Widget,XtPointer aData,XtPointer) {
372  G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
373  XtManageChild(This->fHelpForm);
374  XmTextSetString(This->fHelpText,(char*)This->Help().c_str());
375 }
376 
377 #endif
G4String fName
Definition: G4AttUtils.hh:55
#define width
const XML_Char * name
Definition: expat.h:151
G4double GetBlue() const
Definition: G4Colour.hh:141
tuple b
Definition: test.py:12
#define SoGL2PSAction
Definition: SoGL2PSAction.h:38
G4GLOB_DLL std::ostream G4cout
G4double GetRed() const
Definition: G4Colour.hh:139
G4double GetGreen() const
Definition: G4Colour.hh:140
#define TRUE
Definition: globals.hh:55
static Verbosity GetVerbosity()
#define G4endl
Definition: G4ios.hh:61