Geant4  10.03.p01
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 97241 2016-05-30 12:06:54Z 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  fViewer->setTransparencyType(SoGLRenderAction::SORTED_OBJECT_ADD);
207 }
208 
209 G4OpenInventorXtViewer::~G4OpenInventorXtViewer () {
210  if(fShell) fInteractorManager->RemoveShell(fShell);
211  if(fViewer) {
212  fViewer->setSceneGraph(0);
213  //FIXME : SGI : the below "delete" block things.
214  //FIXME : CoinXt : the below "delete" crashe in ~SoXtRenderArea.
215  //FIXME : delete fViewer;
216  }
217  if(fShell) XtDestroyWidget(fShell);
218 }
219 
220 void G4OpenInventorXtViewer::FinishView () {
221  if(!fViewer) return;
222  fViewer->viewAll();
223  fViewer->saveHomePosition();
224 }
225 
226 void G4OpenInventorXtViewer::SetView () {
227  G4OpenInventorViewer::SetView ();
228  if(!fViewer) return;
229  // Background.
230  G4Colour b = fVP.GetBackgroundColour ();
231  fViewer->setBackgroundColor
232  (SbColor((float)b.GetRed(),(float)b.GetGreen(),(float)b.GetBlue()));
233 }
234 
235 
236 void G4OpenInventorXtViewer::ViewerRender () {
237  if(!fViewer) return;
238  fViewer->render();
239 }
240 
241 SoCamera* G4OpenInventorXtViewer::GetCamera () {
242  if(!fViewer) return 0;
243  return fViewer->getCamera();
244 }
245 
246 Widget G4OpenInventorXtViewer::AddMenu(
247  Widget aMenuBar
248 ,const G4String& aName
249 ,const G4String& aLabel
250 )
251 {
252  // Pulldown menu :
253  Widget menu = XmCreatePulldownMenu(aMenuBar,(char*)aName.c_str(),NULL,0);
254  // Cascade button :
255  Arg args[2];
256  XmString cps =
257  XmStringLtoRCreate((char*)aLabel.c_str(),(char*)XmSTRING_DEFAULT_CHARSET);
258  XtSetArg (args[0],XmNlabelString,cps);
259  XtSetArg (args[1],XmNsubMenuId,menu);
260  Widget widget = XmCreateCascadeButton(aMenuBar,(char*)aName.c_str(),args,2);
261  XmStringFree (cps);
262  XtManageChild(widget);
263  return menu;
264 }
265 void G4OpenInventorXtViewer::AddButton (
266  Widget aMenu
267 ,const G4String& aLabel
268 ,XtCallbackProc aCallback
269 )
270 {
271  Widget widget = XmCreatePushButton(aMenu,(char*)aLabel.c_str(),NULL,0);
272  XtManageChild(widget);
273  XtAddCallback(widget,XmNactivateCallback,aCallback,(XtPointer)this);
274 }
275 
276 void G4OpenInventorXtViewer::HelpCancelCbk(
277  Widget,XtPointer aData,XtPointer) {
278  G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
279  XtUnmanageChild(This->fHelpForm);
280 }
281 
282 
286 
287 void G4OpenInventorXtViewer::EscapeCbk(
288  Widget,XtPointer aData,XtPointer) {
289  G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
290  This->Escape();
291 }
292 
293 void G4OpenInventorXtViewer::PostScriptCbk(
294  Widget,XtPointer aData,XtPointer) {
295  G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
296  This->WritePostScript();
297 }
298 
299 void G4OpenInventorXtViewer::PixmapPostScriptCbk(
300  Widget,XtPointer aData,XtPointer) {
301  G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
302  This->WritePixmapPostScript();
303 }
304 
305 void G4OpenInventorXtViewer::SceneGraphStatisticsCbk(
306  Widget,XtPointer aData,XtPointer) {
307  G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
308  This->SceneGraphStatistics();
309 }
310 
311 void G4OpenInventorXtViewer::WriteInventorCbk(
312  Widget,XtPointer aData,XtPointer) {
313  G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
314  This->WriteInventor();
315 }
316 
317 void G4OpenInventorXtViewer::EraseDetectorCbk(
318  Widget,XtPointer aData,XtPointer) {
319  G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
320  This->EraseDetector();
321 }
322 
323 void G4OpenInventorXtViewer::EraseEventCbk(
324  Widget,XtPointer aData,XtPointer) {
325  G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
326  This->EraseEvent();
327 }
328 
329 void G4OpenInventorXtViewer::SetSolidCbk(
330  Widget,XtPointer aData,XtPointer) {
331  G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
332  This->SetSolid();
333 }
334 
335 void G4OpenInventorXtViewer::SetWireFrameCbk(
336  Widget,XtPointer aData,XtPointer) {
337  G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
338  This->SetWireFrame();
339 }
340 
341 void G4OpenInventorXtViewer::SetReducedWireFrameCbk(
342  Widget,XtPointer aData,XtPointer) {
343  G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
344  This->SetReducedWireFrame(true);
345 }
346 
347 void G4OpenInventorXtViewer::SetFullWireFrameCbk(
348  Widget,XtPointer aData,XtPointer) {
349  G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
350  This->SetReducedWireFrame(false);
351 }
352 
353 void G4OpenInventorXtViewer::UpdateSceneCbk(
354  Widget,XtPointer aData,XtPointer) {
355  G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
356  This->UpdateScene();
357 }
358 
359 void G4OpenInventorXtViewer::SetPreviewCbk(
360  Widget,XtPointer aData,XtPointer) {
361  G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
362  This->SetPreview();
363 }
364 
365 void G4OpenInventorXtViewer::SetPreviewAndFullCbk(
366  Widget,XtPointer aData,XtPointer) {
367  G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
368  This->SetPreviewAndFull();
369 }
370 
371 void G4OpenInventorXtViewer::HelpCbk(
372  Widget,XtPointer aData,XtPointer) {
373  G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
374  XtManageChild(This->fHelpForm);
375  XmTextSetString(This->fHelpText,(char*)This->Help().c_str());
376 }
377 
378 #endif
const XML_Char * name
Definition: expat.h:151
G4String fName
Definition: G4AttUtils.hh:55
G4double GetBlue() const
Definition: G4Colour.hh:141
#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