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