source: trunk/source/visualization/OpenInventor/src/G4OpenInventorXtViewer.cc @ 1097

Last change on this file since 1097 was 1097, checked in by garnier, 15 years ago

update

  • Property svn:mime-type set to text/cpp
File size: 12.4 KB
Line 
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,v 1.24 2006/06/29 21:22:26 gunter Exp $
28// GEANT4 tag $Name:  $
29//
30/*
31 * jck 05 Feb 1997 - Initial Implementation
32 * jck 21 Apr 1997
33 *      Mods for SoXtHepViewer
34 * gb : on Win32 use an SoXtExaminerViewer.
35 * gb 05 April 2004 : revisit to separate Windows things.
36 * gb 09 November 2004 : restore the escape button.
37 * gb 09 November 2004 : have a menu bar in the viewer shell.
38 * gb 09 November 2004 : have gl2ps file production.
39 * gb 14 November 2004 : inherit G4OpenInventorViewer.
40 */
41
42#ifdef G4VIS_BUILD_OIX_DRIVER
43
44// this :
45#include "G4OpenInventorXtViewer.hh"
46
47#include <Inventor/nodes/SoSelection.h>
48
49#include <Inventor/Xt/SoXt.h>
50#include <Inventor/Xt/viewers/SoXtExaminerViewer.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 "HEPVis/actions/SoGL2PSAction.h"
63
64#include "G4OpenInventor.hh"
65#include "G4OpenInventorSceneHandler.hh"
66#include "G4VInteractorManager.hh"
67
68G4OpenInventorXtViewer::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{
77  G4cout << "Window name: " << fName << G4endl;
78}
79
80
81void 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 s[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(s,"%dx%d",width,height);
102      sgeometry = s;
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}
206
207G4OpenInventorXtViewer::~G4OpenInventorXtViewer () {
208  if(fShell) fInteractorManager->RemoveShell(fShell);
209  if(fViewer) {
210    fViewer->setSceneGraph(0);
211    //FIXME : SGI : the below "delete" block things.
212    //FIXME : CoinXt : the below "delete" crashe in ~SoXtRenderArea.
213    //FIXME : delete fViewer;
214  }
215  if(fShell) XtDestroyWidget(fShell);
216}
217
218void G4OpenInventorXtViewer::FinishView () {
219  if(!fViewer) return;
220  fViewer->viewAll();
221  fViewer->saveHomePosition();
222}
223
224void G4OpenInventorXtViewer::SetView () {
225  G4OpenInventorViewer::SetView ();
226  if(!fViewer) return;
227  // Background.
228  G4Colour b = fVP.GetBackgroundColour ();
229  fViewer->setBackgroundColor
230    (SbColor((float)b.GetRed(),(float)b.GetGreen(),(float)b.GetBlue()));
231}
232
233
234void G4OpenInventorXtViewer::ViewerRender () {
235  if(!fViewer) return;
236  fViewer->render();
237}
238
239SoCamera* G4OpenInventorXtViewer::GetCamera () {
240  if(!fViewer) return 0;
241  return fViewer->getCamera();
242}
243
244Widget G4OpenInventorXtViewer::AddMenu(
245 Widget aMenuBar
246,const G4String& aName
247,const G4String& aLabel
248)
249{
250  // Pulldown menu :
251  Widget menu = XmCreatePulldownMenu(aMenuBar,(char*)aName.c_str(),NULL,0);
252  // Cascade button :
253  Arg args[2];
254  XmString cps =
255    XmStringLtoRCreate((char*)aLabel.c_str(),XmSTRING_DEFAULT_CHARSET);
256  XtSetArg (args[0],XmNlabelString,cps);
257  XtSetArg (args[1],XmNsubMenuId,menu);
258  Widget widget = XmCreateCascadeButton(aMenuBar,(char*)aName.c_str(),args,2);
259  XmStringFree (cps);
260  XtManageChild(widget);
261  return menu;
262}
263void G4OpenInventorXtViewer::AddButton (
264 Widget aMenu
265,const G4String& aLabel
266,XtCallbackProc aCallback
267)
268{
269  Widget widget = XmCreatePushButton(aMenu,(char*)aLabel.c_str(),NULL,0);
270  XtManageChild(widget);
271  XtAddCallback(widget,XmNactivateCallback,aCallback,(XtPointer)this);
272}
273
274void G4OpenInventorXtViewer::HelpCancelCbk(
275  Widget,XtPointer aData,XtPointer) {
276  G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
277  XtUnmanageChild(This->fHelpForm);
278}
279
280
281//////////////////////////////////////////////////////////////////////////////
282//////////////////////////////////////////////////////////////////////////////
283//////////////////////////////////////////////////////////////////////////////
284
285void G4OpenInventorXtViewer::EscapeCbk(
286  Widget,XtPointer aData,XtPointer) {
287  G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
288  This->Escape();
289}
290
291void G4OpenInventorXtViewer::PostScriptCbk(
292  Widget,XtPointer aData,XtPointer) {
293  G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
294  This->WritePostScript();
295}
296
297void G4OpenInventorXtViewer::PixmapPostScriptCbk(
298  Widget,XtPointer aData,XtPointer) {
299  G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
300  This->WritePixmapPostScript();
301}
302
303void G4OpenInventorXtViewer::SceneGraphStatisticsCbk(
304  Widget,XtPointer aData,XtPointer) {
305  G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
306  This->SceneGraphStatistics();
307}
308
309void G4OpenInventorXtViewer::WriteInventorCbk(
310  Widget,XtPointer aData,XtPointer) {
311  G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
312  This->WriteInventor();
313}
314
315void G4OpenInventorXtViewer::EraseDetectorCbk(
316  Widget,XtPointer aData,XtPointer) {
317  G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
318  This->EraseDetector();
319}
320
321void G4OpenInventorXtViewer::EraseEventCbk(
322  Widget,XtPointer aData,XtPointer) {
323  G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
324  This->EraseEvent();
325}
326
327void G4OpenInventorXtViewer::SetSolidCbk(
328  Widget,XtPointer aData,XtPointer) {
329  G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
330  This->SetSolid();
331}
332
333void G4OpenInventorXtViewer::SetWireFrameCbk(
334  Widget,XtPointer aData,XtPointer) {
335  G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
336  This->SetWireFrame();
337}
338
339void G4OpenInventorXtViewer::SetReducedWireFrameCbk(
340  Widget,XtPointer aData,XtPointer) {
341  G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
342  This->SetReducedWireFrame(true);
343}
344
345void G4OpenInventorXtViewer::SetFullWireFrameCbk(
346  Widget,XtPointer aData,XtPointer) {
347  G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
348  This->SetReducedWireFrame(false);
349}
350
351void G4OpenInventorXtViewer::UpdateSceneCbk(
352  Widget,XtPointer aData,XtPointer) {
353  G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
354  This->UpdateScene();
355}
356
357void G4OpenInventorXtViewer::SetPreviewCbk(
358  Widget,XtPointer aData,XtPointer) {
359  G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
360  This->SetPreview();
361}
362
363void G4OpenInventorXtViewer::SetPreviewAndFullCbk(
364  Widget,XtPointer aData,XtPointer) {
365  G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
366  This->SetPreviewAndFull();
367}
368
369void G4OpenInventorXtViewer::HelpCbk(
370  Widget,XtPointer aData,XtPointer) {
371  G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
372  XtManageChild(This->fHelpForm);
373  XmTextSetString(This->fHelpText,(char*)This->Help().c_str());
374}
375
376
377#endif
378
379
Note: See TracBrowser for help on using the repository browser.