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