G4OpenInventorXtViewer.cc

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 //
00027 // $Id$
00028 //
00029 /*
00030  * jck 05 Feb 1997 - Initial Implementation
00031  * jck 21 Apr 1997 
00032  *      Mods for SoXtHepViewer
00033  * gb : on Win32 use an SoXtExaminerViewer.
00034  * gb 05 April 2004 : revisit to separate Windows things.
00035  * gb 09 November 2004 : restore the escape button.
00036  * gb 09 November 2004 : have a menu bar in the viewer shell.
00037  * gb 09 November 2004 : have gl2ps file production.
00038  * gb 14 November 2004 : inherit G4OpenInventorViewer.
00039  */
00040 
00041 #ifdef G4VIS_BUILD_OIX_DRIVER
00042 
00043 // this :
00044 #include "G4OpenInventorXtViewer.hh"
00045 
00046 #include <Inventor/nodes/SoSelection.h>
00047 
00048 #include <Inventor/Xt/SoXt.h>
00049 #include <Inventor/Xt/viewers/SoXtExaminerViewer.h>
00050 
00051 #include <X11/StringDefs.h>
00052 #include <X11/Shell.h>
00053 
00054 #include <Xm/Xm.h>
00055 #include <Xm/PushB.h>
00056 #include <Xm/Form.h>
00057 #include <Xm/CascadeB.h>
00058 #include <Xm/RowColumn.h>
00059 #include <Xm/Text.h>
00060 
00061 #include "HEPVis/actions/SoGL2PSAction.h"
00062 
00063 #include "G4OpenInventor.hh"
00064 #include "G4OpenInventorSceneHandler.hh"
00065 #include "G4VInteractorManager.hh"
00066 #include "G4VisManager.hh"
00067 
00068 G4OpenInventorXtViewer::G4OpenInventorXtViewer(
00069  G4OpenInventorSceneHandler& sceneHandler
00070 ,const G4String& name)
00071 :G4OpenInventorViewer (sceneHandler, name)
00072 ,fShell(0)
00073 ,fViewer(0)
00074 ,fHelpForm(0)
00075 ,fHelpText(0)
00076 {
00077   if (G4VisManager::GetVerbosity() >= G4VisManager::confirmations)
00078     G4cout << "Window name: " << fName << G4endl;
00079 }
00080 
00081 
00082 void G4OpenInventorXtViewer::Initialise() {
00083   
00084   G4String wName = fName;
00085   
00086   Widget parent = (Widget)fInteractorManager->GetParentInteractor ();
00087   int width = 600;
00088   int height = 600;
00089 
00090   if(!parent) {  
00091     // Check if user has specified an X-Windows-type geometry string...
00092     char str[32];
00093 
00094     G4String sgeometry = fVP.GetXGeometryString();
00095     if(sgeometry.empty()) {
00096       G4cout << "ERROR: Geometry string \""
00097              << sgeometry          
00098              << "\" is empty.  Using \"600x600\"."
00099              << G4endl;
00100       width = 600;
00101       height = 600;  
00102       sprintf(str,"%dx%d",width,height);
00103       sgeometry = str;
00104     } else {
00105       width = fVP.GetWindowSizeHintX();
00106       height = fVP.GetWindowSizeHintX();
00107     }
00108 
00109     //Create a shell window :
00110     G4String shellName = wName;
00111     shellName += "_shell"; 
00112     Arg args[10];
00113     XtSetArg(args[0],XtNgeometry,XtNewString(sgeometry.c_str()));
00114     XtSetArg(args[1],XtNborderWidth,0);
00115     XtSetArg(args[2],XtNtitle,XtNewString(wName.c_str()));
00116     fShell = XtAppCreateShell(shellName.c_str(),"Inventor",
00117                                topLevelShellWidgetClass,
00118                                SoXt::getDisplay(),
00119                                args,3); 
00120 
00121     XtSetArg(args[0],XmNtopAttachment   ,XmATTACH_FORM);
00122     XtSetArg(args[1],XmNleftAttachment  ,XmATTACH_FORM);
00123     XtSetArg(args[2],XmNrightAttachment ,XmATTACH_FORM);
00124     XtSetArg(args[3],XmNbottomAttachment,XmATTACH_FORM);
00125     Widget form = XmCreateForm (fShell,(char*)"form",args,4);
00126     XtManageChild (form);
00127 
00128     XtSetArg(args[0],XmNtopAttachment   ,XmATTACH_FORM);
00129     XtSetArg(args[1],XmNleftAttachment  ,XmATTACH_FORM);
00130     XtSetArg(args[2],XmNrightAttachment ,XmATTACH_FORM);
00131     Widget menuBar = XmCreateMenuBar (form,(char*)"menuBar",args,3);
00132     XtManageChild(menuBar);
00133 
00134    {Widget menu = AddMenu(menuBar,"File","File");
00135     AddButton(menu,"PS (gl2ps)",PostScriptCbk);
00136     AddButton(menu,"PS (pixmap)",PixmapPostScriptCbk);
00137     AddButton(menu,"IV",WriteInventorCbk);
00138     AddButton(menu,"Escape",EscapeCbk);}
00139 
00140    {Widget menu = AddMenu(menuBar,"Etc","Etc");
00141     AddButton(menu,"Erase detector",EraseDetectorCbk);
00142     AddButton(menu,"Erase event",EraseEventCbk);
00143     AddButton(menu,"Set solid",SetSolidCbk);
00144 /*    AddButton(menu,"Set (G4) wire frame",SetWireFrameCbk);*/
00145     AddButton(menu,"Set (G4) reduced wire frame",SetReducedWireFrameCbk);
00146     AddButton(menu,"Set (G4) full wire frame",SetFullWireFrameCbk);
00147     AddButton(menu,"Visible mothers + invisible daughters",SetPreviewCbk);
00148     AddButton(menu,"Visible mothers + visible daughters",SetPreviewAndFullCbk);
00149     AddButton(menu,"Update scene",UpdateSceneCbk);
00150     AddButton(menu,"Scene graph stats",SceneGraphStatisticsCbk);
00151    }
00152 
00153    {Widget menu = AddMenu(menuBar,"Help","Help");
00154     AddButton(menu,"Controls",HelpCbk);}
00155 
00156     fViewer = new SoXtExaminerViewer(form,wName.c_str(),TRUE);
00157     
00158     XtSetArg(args[0],XmNtopAttachment   ,XmATTACH_WIDGET);
00159     XtSetArg(args[1],XmNtopWidget       ,menuBar);
00160     XtSetArg(args[2],XmNleftAttachment  ,XmATTACH_FORM);
00161     XtSetArg(args[3],XmNrightAttachment ,XmATTACH_FORM);
00162     XtSetArg(args[4],XmNbottomAttachment,XmATTACH_FORM);
00163     XtSetValues(fViewer->getWidget(),args,5);
00164 
00165     fHelpForm = XmCreateFormDialog(fShell,(char*)"help",NULL,0);
00166     XtSetArg(args[0],XmNleftAttachment  ,XmATTACH_FORM);
00167     XtSetArg(args[1],XmNrightAttachment ,XmATTACH_FORM);
00168     XtSetArg(args[2],XmNbottomAttachment,XmATTACH_FORM);
00169     Widget cancel = XmCreatePushButton(fHelpForm,(char*)"helpCancel",args,3);
00170     XtAddCallback(cancel,XmNactivateCallback,HelpCancelCbk,(XtPointer)this);
00171     XtManageChild(cancel);
00172     XtSetArg(args[0],XmNtopAttachment   ,XmATTACH_FORM);
00173     XtSetArg(args[1],XmNleftAttachment  ,XmATTACH_FORM);
00174     XtSetArg(args[2],XmNrightAttachment ,XmATTACH_FORM);
00175     XtSetArg(args[3],XmNbottomAttachment,XmATTACH_WIDGET);
00176     XtSetArg(args[4],XmNbottomWidget    ,cancel);
00177     fHelpText = XmCreateScrolledText(fHelpForm,(char*)"helpText",args,5);
00178     XtManageChild(fHelpText);
00179 
00180     fInteractorManager->AddShell(fShell);
00181 
00182   } else {
00183     char* str = fInteractorManager->GetCreationString();
00184     if(str!=0) wName = str;
00185     fViewer = new SoXtExaminerViewer(parent,wName.c_str(),TRUE);
00186   }
00187 
00188   fViewer->setSize(SbVec2s(width,height));
00189 
00190   // Have a GL2PS render action :
00191   const SbViewportRegion& vpRegion = fViewer->getViewportRegion();
00192   fGL2PSAction = new SoGL2PSAction(vpRegion);
00193   fViewer->setGLRenderAction(fGL2PSAction);
00194 
00195   // Else :
00196   fViewer->setSceneGraph(fSoSelection);
00197   fViewer->viewAll();
00198   fViewer->saveHomePosition();
00199   fViewer->setTitle(fName);
00200   fViewer->show();
00201   if(fShell) {
00202     SoXt::show(fShell);
00203     fInteractorManager->FlushAndWaitExecution ();
00204   }
00205   fInteractorManager->SetCreatedInteractor (fViewer -> getWidget());
00206 }
00207 
00208 G4OpenInventorXtViewer::~G4OpenInventorXtViewer () {
00209   if(fShell) fInteractorManager->RemoveShell(fShell);
00210   if(fViewer) {
00211     fViewer->setSceneGraph(0);
00212     //FIXME : SGI : the below "delete" block things.
00213     //FIXME : CoinXt : the below "delete" crashe in ~SoXtRenderArea.
00214     //FIXME : delete fViewer;
00215   }
00216   if(fShell) XtDestroyWidget(fShell);
00217 }
00218 
00219 void G4OpenInventorXtViewer::FinishView () {
00220   if(!fViewer) return;
00221   fViewer->viewAll();
00222   fViewer->saveHomePosition();
00223 }
00224 
00225 void G4OpenInventorXtViewer::SetView () {
00226   G4OpenInventorViewer::SetView ();
00227   if(!fViewer) return;
00228   // Background.
00229   G4Colour b = fVP.GetBackgroundColour ();
00230   fViewer->setBackgroundColor
00231     (SbColor((float)b.GetRed(),(float)b.GetGreen(),(float)b.GetBlue()));
00232 }
00233 
00234 
00235 void G4OpenInventorXtViewer::ViewerRender () {
00236   if(!fViewer) return;
00237   fViewer->render();
00238 }
00239 
00240 SoCamera* G4OpenInventorXtViewer::GetCamera () {
00241   if(!fViewer) return 0;
00242   return fViewer->getCamera();
00243 }
00244 
00245 Widget G4OpenInventorXtViewer::AddMenu(
00246  Widget aMenuBar
00247 ,const G4String& aName
00248 ,const G4String& aLabel
00249 )
00250 {
00251   // Pulldown menu :
00252   Widget menu = XmCreatePulldownMenu(aMenuBar,(char*)aName.c_str(),NULL,0);
00253   // Cascade button :
00254   Arg args[2];
00255   XmString cps = 
00256     XmStringLtoRCreate((char*)aLabel.c_str(),XmSTRING_DEFAULT_CHARSET);
00257   XtSetArg (args[0],XmNlabelString,cps);
00258   XtSetArg (args[1],XmNsubMenuId,menu);
00259   Widget widget = XmCreateCascadeButton(aMenuBar,(char*)aName.c_str(),args,2);
00260   XmStringFree (cps);
00261   XtManageChild(widget);
00262   return menu;
00263 }
00264 void G4OpenInventorXtViewer::AddButton (
00265  Widget aMenu
00266 ,const G4String& aLabel
00267 ,XtCallbackProc aCallback
00268 )
00269 {
00270   Widget widget = XmCreatePushButton(aMenu,(char*)aLabel.c_str(),NULL,0);
00271   XtManageChild(widget);
00272   XtAddCallback(widget,XmNactivateCallback,aCallback,(XtPointer)this);
00273 }
00274 
00275 void G4OpenInventorXtViewer::HelpCancelCbk(
00276   Widget,XtPointer aData,XtPointer) {
00277   G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
00278   XtUnmanageChild(This->fHelpForm);
00279 }
00280 
00281 
00285 
00286 void G4OpenInventorXtViewer::EscapeCbk(
00287   Widget,XtPointer aData,XtPointer) {
00288   G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
00289   This->Escape();
00290 }
00291 
00292 void G4OpenInventorXtViewer::PostScriptCbk(
00293   Widget,XtPointer aData,XtPointer) {
00294   G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
00295   This->WritePostScript();
00296 }
00297 
00298 void G4OpenInventorXtViewer::PixmapPostScriptCbk(
00299   Widget,XtPointer aData,XtPointer) {
00300   G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
00301   This->WritePixmapPostScript();
00302 }
00303 
00304 void G4OpenInventorXtViewer::SceneGraphStatisticsCbk(
00305   Widget,XtPointer aData,XtPointer) {
00306   G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
00307   This->SceneGraphStatistics();
00308 }
00309 
00310 void G4OpenInventorXtViewer::WriteInventorCbk(
00311   Widget,XtPointer aData,XtPointer) {
00312   G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
00313   This->WriteInventor();
00314 }
00315 
00316 void G4OpenInventorXtViewer::EraseDetectorCbk(
00317   Widget,XtPointer aData,XtPointer) {
00318   G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
00319   This->EraseDetector();
00320 }
00321 
00322 void G4OpenInventorXtViewer::EraseEventCbk(
00323   Widget,XtPointer aData,XtPointer) {
00324   G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
00325   This->EraseEvent();
00326 }
00327 
00328 void G4OpenInventorXtViewer::SetSolidCbk(
00329   Widget,XtPointer aData,XtPointer) {
00330   G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
00331   This->SetSolid();
00332 }
00333 
00334 void G4OpenInventorXtViewer::SetWireFrameCbk(
00335   Widget,XtPointer aData,XtPointer) {
00336   G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
00337   This->SetWireFrame();
00338 }
00339 
00340 void G4OpenInventorXtViewer::SetReducedWireFrameCbk(
00341   Widget,XtPointer aData,XtPointer) {
00342   G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
00343   This->SetReducedWireFrame(true);
00344 }
00345 
00346 void G4OpenInventorXtViewer::SetFullWireFrameCbk(
00347   Widget,XtPointer aData,XtPointer) {
00348   G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
00349   This->SetReducedWireFrame(false);
00350 }
00351 
00352 void G4OpenInventorXtViewer::UpdateSceneCbk(
00353   Widget,XtPointer aData,XtPointer) {
00354   G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
00355   This->UpdateScene();
00356 }
00357 
00358 void G4OpenInventorXtViewer::SetPreviewCbk(
00359   Widget,XtPointer aData,XtPointer) {
00360   G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
00361   This->SetPreview();
00362 }
00363 
00364 void G4OpenInventorXtViewer::SetPreviewAndFullCbk(
00365   Widget,XtPointer aData,XtPointer) {
00366   G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
00367   This->SetPreviewAndFull();
00368 }
00369 
00370 void G4OpenInventorXtViewer::HelpCbk(
00371   Widget,XtPointer aData,XtPointer) {
00372   G4OpenInventorXtViewer* This = (G4OpenInventorXtViewer*)aData;
00373   XtManageChild(This->fHelpForm);
00374   XmTextSetString(This->fHelpText,(char*)This->Help().c_str());
00375 }
00376 
00377 #endif

Generated on Mon May 27 17:49:13 2013 for Geant4 by  doxygen 1.4.7