ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4OpenInventorXtViewer.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4OpenInventorXtViewer.cc
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 #include <Inventor/Xt/viewers/SoXtExaminerViewer.h>
49 
50 #include <X11/StringDefs.h>
51 #include <X11/Shell.h>
52 
53 #include <Xm/Xm.h>
54 #include <Xm/PushB.h>
55 #include <Xm/Form.h>
56 #include <Xm/CascadeB.h>
57 #include <Xm/RowColumn.h>
58 #include <Xm/Text.h>
59 
61 
62 #include "G4OpenInventor.hh"
64 #include "G4VInteractorManager.hh"
65 #include "G4VisManager.hh"
66 
67 G4OpenInventorXtViewer::G4OpenInventorXtViewer(
68  G4OpenInventorSceneHandler& sceneHandler
69 ,const G4String& name)
70 :G4OpenInventorViewer (sceneHandler, name)
71 ,fShell(0)
72 ,fViewer(0)
73 ,fHelpForm(0)
74 ,fHelpText(0)
75 {
77  G4cout << "Window name: " << fName << G4endl;
78 }
79 
80 
81 void G4OpenInventorXtViewer::Initialise() {
82 
83  G4String wName = fName;
84 
85  Widget parent = (Widget)fInteractorManager->GetParentInteractor ();
86  int width = 600;
87  int height = 600;
88 
89  if(!parent) {
90  // Check if user has specified an X-Windows-type geometry string...
91  char str[32];
92 
93  G4String sgeometry = fVP.GetXGeometryString();
94  if(sgeometry.empty()) {
95  G4cout << "ERROR: Geometry string \""
96  << sgeometry
97  << "\" is empty. Using \"600x600\"."
98  << G4endl;
99  width = 600;
100  height = 600;
101  sprintf(str,"%dx%d",width,height);
102  sgeometry = str;
103  } else {
104  width = fVP.GetWindowSizeHintX();
105  height = fVP.GetWindowSizeHintX();
106  }
107 
108  //Create a shell window :
109  G4String shellName = wName;
110  shellName += "_shell";
111  Arg args[10];
112  XtSetArg(args[0],XtNgeometry,XtNewString(sgeometry.c_str()));
113  XtSetArg(args[1],XtNborderWidth,0);
114  XtSetArg(args[2],XtNtitle,XtNewString(wName.c_str()));
115  fShell = XtAppCreateShell(shellName.c_str(),"Inventor",
116  topLevelShellWidgetClass,
117  SoXt::getDisplay(),
118  args,3);
119 
120  XtSetArg(args[0],XmNtopAttachment ,XmATTACH_FORM);
121  XtSetArg(args[1],XmNleftAttachment ,XmATTACH_FORM);
122  XtSetArg(args[2],XmNrightAttachment ,XmATTACH_FORM);
123  XtSetArg(args[3],XmNbottomAttachment,XmATTACH_FORM);
124  Widget form = XmCreateForm (fShell,(char*)"form",args,4);
125  XtManageChild (form);
126 
127  XtSetArg(args[0],XmNtopAttachment ,XmATTACH_FORM);
128  XtSetArg(args[1],XmNleftAttachment ,XmATTACH_FORM);
129  XtSetArg(args[2],XmNrightAttachment ,XmATTACH_FORM);
130  Widget menuBar = XmCreateMenuBar (form,(char*)"menuBar",args,3);
131  XtManageChild(menuBar);
132 
133  {Widget menu = AddMenu(menuBar,"File","File");
134  AddButton(menu,"PS (gl2ps)",PostScriptCbk);
135  AddButton(menu,"PS (pixmap)",PixmapPostScriptCbk);
136  AddButton(menu,"IV",WriteInventorCbk);
137  AddButton(menu,"Escape",EscapeCbk);}
138 
139  {Widget menu = AddMenu(menuBar,"Etc","Etc");
140  AddButton(menu,"Erase detector",EraseDetectorCbk);
141  AddButton(menu,"Erase event",EraseEventCbk);
142  AddButton(menu,"Set solid",SetSolidCbk);
143 /* AddButton(menu,"Set (G4) wire frame",SetWireFrameCbk);*/
144  AddButton(menu,"Set (G4) reduced wire frame",SetReducedWireFrameCbk);
145  AddButton(menu,"Set (G4) full wire frame",SetFullWireFrameCbk);
146  AddButton(menu,"Visible mothers + invisible daughters",SetPreviewCbk);
147  AddButton(menu,"Visible mothers + visible daughters",SetPreviewAndFullCbk);
148  AddButton(menu,"Update scene",UpdateSceneCbk);
149  AddButton(menu,"Scene graph stats",SceneGraphStatisticsCbk);
150  }
151 
152  {Widget menu = AddMenu(menuBar,"Help","Help");
153  AddButton(menu,"Controls",HelpCbk);}
154 
155  fViewer = new SoXtExaminerViewer(form,wName.c_str(),TRUE);
156 
157  XtSetArg(args[0],XmNtopAttachment ,XmATTACH_WIDGET);
158  XtSetArg(args[1],XmNtopWidget ,menuBar);
159  XtSetArg(args[2],XmNleftAttachment ,XmATTACH_FORM);
160  XtSetArg(args[3],XmNrightAttachment ,XmATTACH_FORM);
161  XtSetArg(args[4],XmNbottomAttachment,XmATTACH_FORM);
162  XtSetValues(fViewer->getWidget(),args,5);
163 
164  fHelpForm = XmCreateFormDialog(fShell,(char*)"help",NULL,0);
165  XtSetArg(args[0],XmNleftAttachment ,XmATTACH_FORM);
166  XtSetArg(args[1],XmNrightAttachment ,XmATTACH_FORM);
167  XtSetArg(args[2],XmNbottomAttachment,XmATTACH_FORM);
168  Widget cancel = XmCreatePushButton(fHelpForm,(char*)"helpCancel",args,3);
169  XtAddCallback(cancel,XmNactivateCallback,HelpCancelCbk,(XtPointer)this);
170  XtManageChild(cancel);
171  XtSetArg(args[0],XmNtopAttachment ,XmATTACH_FORM);
172  XtSetArg(args[1],XmNleftAttachment ,XmATTACH_FORM);
173  XtSetArg(args[2],XmNrightAttachment ,XmATTACH_FORM);
174  XtSetArg(args[3],XmNbottomAttachment,XmATTACH_WIDGET);
175  XtSetArg(args[4],XmNbottomWidget ,cancel);
176  fHelpText = XmCreateScrolledText(fHelpForm,(char*)"helpText",args,5);
177  XtManageChild(fHelpText);
178 
179  fInteractorManager->AddShell(fShell);
180 
181  } else {
182  char* str = fInteractorManager->GetCreationString();
183  if(str!=0) wName = str;
184  fViewer = new SoXtExaminerViewer(parent,wName.c_str(),TRUE);
185  }
186 
187  fViewer->setSize(SbVec2s(width,height));
188 
189  // Have a GL2PS render action :
190  const SbViewportRegion& vpRegion = fViewer->getViewportRegion();
191  fGL2PSAction = new SoGL2PSAction(vpRegion);
192  fViewer->setGLRenderAction(fGL2PSAction);
193 
194  // Else :
195  fViewer->setSceneGraph(fSoSelection);
196  fViewer->viewAll();
197  fViewer->saveHomePosition();
198  fViewer->setTitle(fName);
199  fViewer->show();
200  if(fShell) {
201  SoXt::show(fShell);
202  fInteractorManager->FlushAndWaitExecution ();
203  }
204  fInteractorManager->SetCreatedInteractor (fViewer -> getWidget());
205  fViewer->setTransparencyType(SoGLRenderAction::SORTED_OBJECT_ADD);
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(),(char*)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