G4OpenInventorWinViewer.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 : Mods for SoXtHepViewer
00032  * gb : on Win32 use an SoXtExaminerViewer.
00033  * gb : 05 April 2004 : creation.
00034  * gb : 09 November 2004 : Pulldown menu with the escape menu item.
00035  * gb 14 November 2004 : inherit G4OpenInventorViewer.
00036  */
00037 
00038 #ifdef G4VIS_BUILD_OIWIN32_DRIVER
00039 
00040 // this :
00041 #include "G4OpenInventorWinViewer.hh"
00042 
00043 #include <Inventor/nodes/SoSelection.h>
00044 
00045 #include <Inventor/Win/SoWin.h>
00046 #include <Inventor/Win/viewers/SoWinExaminerViewer.h>
00047 
00048 #include "HEPVis/actions/SoGL2PSAction.h"
00049 
00050 #include "G4OpenInventor.hh"
00051 #include "G4OpenInventorSceneHandler.hh"
00052 #include "G4VInteractorManager.hh"
00053 #include "G4VisManager.hh"
00054 
00055 #include <windowsx.h>
00056 
00057 // To have sizeChanged public :
00058 class Geant4_SoWinExaminerViewer : public SoWinExaminerViewer {
00059 public:
00060   Geant4_SoWinExaminerViewer(HWND parent,const char* name,SbBool embed)
00061   :SoWinExaminerViewer(parent,name,embed){}
00062   virtual void sizeChanged(const SbVec2s & size){
00063     SoWinExaminerViewer::sizeChanged(size);
00064   }
00065 };
00066 
00067 // File : 
00068 #define ID_FILE_POSTSCRIPT 1
00069 #define ID_FILE_PIXMAP_POSTSCRIPT 2
00070 #define ID_FILE_INVENTOR 3
00071 #define ID_FILE_ESCAPE 4
00072 // Etc : 
00073 #define ID_ETC_ERASE_DETECTOR 101
00074 #define ID_ETC_ERASE_EVENT 102
00075 #define ID_ETC_SET_SOLID 103
00076 #define ID_ETC_SET_WIRE_FRAME 104
00077 #define ID_ETC_SET_REDUCED_WIRE_FRAME 105
00078 #define ID_ETC_SET_FULL_WIRE_FRAME 106
00079 #define ID_ETC_SET_PREVIEW 107
00080 #define ID_ETC_SET_PREVIEW_AND_FULL 108
00081 #define ID_ETC_UPDATE_SCENE 109
00082 #define ID_ETC_STATS 110
00083 // Help :
00084 #define ID_HELP_CONTROLS 201
00085 
00086 //static void SecondaryLoopPostAction ();
00087 
00088 static const char className[] = "G4OpenInventorShellWindow";
00089 
00090 G4OpenInventorWinViewer::G4OpenInventorWinViewer(
00091  G4OpenInventorSceneHandler& sceneHandler
00092 ,const G4String& name)
00093 :G4OpenInventorViewer (sceneHandler, name)
00094 ,fShell(0)
00095 ,fViewer(0)
00096 {
00097   if (G4VisManager::GetVerbosity() >= G4VisManager::confirmations)
00098     G4cout << "Window name: " << fName << G4endl;
00099 }
00100 
00101 
00102 void G4OpenInventorWinViewer::Initialise() {
00103 
00104   G4String wName = fName;
00105 
00106   int width = 600;
00107   int height = 600;
00108 
00109   HWND parent = (HWND)fInteractorManager->GetParentInteractor ();
00110   if(!parent) {
00111     //Create a shell window :
00112     G4String shellName = wName;
00113     shellName += "_shell"; 
00114     static SbBool done = FALSE;
00115     if(done==FALSE) {
00116       HBRUSH brush = (HBRUSH) GetSysColorBrush(COLOR_BTNFACE);
00117       WNDCLASS wc;
00118       wc.style = CS_HREDRAW | CS_VREDRAW;
00119       wc.lpfnWndProc = (WNDPROC)WindowProc;
00120       wc.cbClsExtra = 0;
00121       wc.cbWndExtra = 0;
00122       wc.hInstance = ::GetModuleHandle(0);
00123       wc.hIcon = ::LoadIcon(0, IDI_APPLICATION);
00124       wc.hCursor = ::LoadCursor(0, IDC_ARROW);
00125       wc.hbrBackground = brush;
00126       wc.lpszMenuName = className;
00127       wc.lpszClassName = className;
00128       ::RegisterClass(&wc);
00129       done = TRUE;
00130     }
00131     
00132     width = fVP.GetWindowSizeHintX();
00133     height = fVP.GetWindowSizeHintX();
00134     
00135     HMENU menuBar = CreateMenu();
00136 
00137    {HMENU casc = CreatePopupMenu();
00138     ::AppendMenu(menuBar,MF_POPUP,(UINT)casc,"File");
00139     ::AppendMenu(casc,MF_STRING,ID_FILE_POSTSCRIPT,"PS (gl2ps)");
00140     ::AppendMenu(casc,MF_STRING,ID_FILE_PIXMAP_POSTSCRIPT,"PS (pixmap)");
00141     ::AppendMenu(casc,MF_STRING,ID_FILE_INVENTOR,"IV");
00142     ::AppendMenu(casc,MF_STRING,ID_FILE_ESCAPE,"Escape");}
00143 
00144    {HMENU casc = CreatePopupMenu();
00145     ::AppendMenu(menuBar,MF_POPUP,(UINT)casc,"Etc");
00146     ::AppendMenu(casc,MF_STRING,ID_ETC_ERASE_DETECTOR,"Erase detector");
00147     ::AppendMenu(casc,MF_STRING,ID_ETC_ERASE_EVENT,"Erase event");
00148     ::AppendMenu(casc,MF_STRING,ID_ETC_SET_SOLID,"Set solid");
00149     //::AppendMenu(casc,MF_STRING,ID_ETC_SET_WIRE_FRAME,"Set (G4) wire frame");
00150     ::AppendMenu(casc,MF_STRING,ID_ETC_SET_REDUCED_WIRE_FRAME,
00151                       "Set (G4) reduced wire frame");
00152     ::AppendMenu(casc,MF_STRING,ID_ETC_SET_FULL_WIRE_FRAME,
00153                       "Set (G4) full wire frame");
00154     ::AppendMenu(casc,MF_STRING,ID_ETC_SET_PREVIEW,
00155                       "Visible mothers + invisible daughters");
00156     ::AppendMenu(casc,MF_STRING,ID_ETC_SET_PREVIEW_AND_FULL,
00157                       "Visible mothers + visible daughters");
00158     ::AppendMenu(casc,MF_STRING,ID_ETC_UPDATE_SCENE,"Update scene");
00159     ::AppendMenu(casc,MF_STRING,ID_ETC_STATS,"Scene graph stats");}
00160 
00161    {HMENU casc = CreatePopupMenu();
00162     ::AppendMenu(menuBar,MF_POPUP,(UINT)casc,"Help");
00163     ::AppendMenu(casc,MF_STRING,ID_HELP_CONTROLS,"Controls");}
00164 
00165     fShell = ::CreateWindow(className, shellName.c_str(), 
00166                             WS_OVERLAPPEDWINDOW |
00167                             WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
00168                             CW_USEDEFAULT, CW_USEDEFAULT, 
00169                             width,height,
00170                             0,menuBar,::GetModuleHandle(0),0);
00171     // Retreive window and client sizez :
00172     RECT wrect,crect;
00173     GetWindowRect((HWND)fShell,&wrect);
00174     GetClientRect((HWND)fShell,&crect);
00175     int ww = wrect.right-wrect.left;
00176     int wh = wrect.bottom-wrect.top;
00177     int cw = crect.right-crect.left;
00178     int ch = crect.bottom-crect.top;
00179     // Compell client rect to be width height :
00180     MoveWindow((HWND)fShell,wrect.left,wrect.top,width+ww-cw,height+wh-ch,TRUE);
00181     ::SetWindowLongPtr((HWND)fShell,GWLP_USERDATA,LONG(this));
00182     ::SetWindowText((HWND)fShell,shellName.c_str());
00183     parent = fShell;
00184     fInteractorManager->AddShell(fShell);
00185   } else {
00186     char* str = fInteractorManager->GetCreationString();
00187     if(str!=0) wName = str;
00188   }
00189   fViewer = new Geant4_SoWinExaminerViewer(parent,wName.c_str(),TRUE);
00190 
00191   // Have a GL2PS render action :
00192   const SbViewportRegion& vpRegion = fViewer->getViewportRegion();
00193   fGL2PSAction = new SoGL2PSAction(vpRegion);
00194   fViewer->setGLRenderAction(fGL2PSAction);
00195 
00196   fViewer->setSize(SbVec2s(width,height));
00197   fViewer->setSceneGraph(fSoSelection);
00198   fViewer->viewAll();
00199   fViewer->saveHomePosition();
00200   fViewer->setTitle(fName);
00201   fViewer->show();
00202   if(fShell) {
00203     SoWin::show(fShell);
00204     fInteractorManager->FlushAndWaitExecution ();
00205   }
00206   fInteractorManager->SetCreatedInteractor (fViewer -> getWidget());
00207 }
00208 
00209 G4OpenInventorWinViewer::~G4OpenInventorWinViewer () {
00210   if(fShell) fInteractorManager->RemoveShell(fShell);
00211   if(fViewer) {
00212     fViewer->setSceneGraph(0);
00213     delete fViewer;
00214   }
00215   if(fShell) {
00216     ::SetWindowLongPtr((HWND)fShell,GWLP_USERDATA,LONG(0));
00217     ::DestroyWindow((HWND)fShell);
00218   }
00219 }
00220 
00221 void G4OpenInventorWinViewer::FinishView () {
00222   if(!fViewer) return;
00223   fViewer->viewAll();
00224   fViewer->saveHomePosition();
00225 }
00226 
00227 void G4OpenInventorWinViewer::SetView () {
00228   G4OpenInventorViewer::SetView ();
00229   if(!fViewer) return;
00230   // Background.
00231   G4Colour b = fVP.GetBackgroundColour ();
00232   fViewer->setBackgroundColor
00233     (SbColor((float)b.GetRed(),(float)b.GetGreen(),(float)b.GetBlue()));
00234 }
00235 void G4OpenInventorWinViewer::ViewerRender () {
00236   if(!fViewer) return;
00237   fViewer->render();
00238 }
00239 
00240 SoCamera* G4OpenInventorWinViewer::GetCamera () {
00241   if(!fViewer) return 0;
00242   return fViewer->getCamera();
00243 }
00244 
00245 
00247 LRESULT CALLBACK G4OpenInventorWinViewer::WindowProc ( 
00248  HWND   aWindow
00249 ,UINT   aMessage
00250 ,WPARAM aWParam
00251 ,LPARAM aLParam
00252 )
00254 //  Below treatment of WM_SIZE, WM_SETFOCUS not necessary 
00255 // with TGS, but needed with SoFree. WM_DESTROY needed for 
00256 // 'main top level window' so that 'Close window' induces
00257 // the end of the task.
00259 {
00260   switch (aMessage) { 
00261   case WM_SIZE:{ // Assume one child window !
00262     int width = LOWORD(aLParam);
00263     int height = HIWORD(aLParam);
00264     //printf("debug : G4SoWindow : WMS_SIZE : %d %d\n",width,height);
00265     G4OpenInventorWinViewer* This = 
00266       (G4OpenInventorWinViewer*)::GetWindowLongPtr(aWindow,GWLP_USERDATA);
00267     if(This && This->fViewer) {
00268       This->fViewer->sizeChanged(SbVec2s(width,height));
00269     }
00270   }return 0;
00271   case WM_SETFOCUS:{ // Assume one child window !
00272     HWND hwnd = ::GetFirstChild(aWindow);
00273     if(hwnd!=0) ::SetFocus(hwnd);
00274   }return 0;
00275   case WM_DESTROY:{
00276     //G4OpenInventorWinViewer* This = 
00277     //  (G4OpenInventorWinViewer*)::GetWindowLongPtr(aWindow,GWLP_USERDATA);
00278     //::PostQuitMessage(0);
00279   }return 0;
00280   case WM_COMMAND:{
00281     G4OpenInventorWinViewer* This = 
00282       (G4OpenInventorWinViewer*)::GetWindowLongPtr(aWindow,GWLP_USERDATA);
00283     if(This) {
00284       if(aLParam==0) { //From menu.
00285         // File :
00286         if(aWParam==ID_FILE_POSTSCRIPT) {
00287           This->WritePostScript();
00288         } else if(aWParam==ID_FILE_PIXMAP_POSTSCRIPT) {
00289           This->WritePixmapPostScript();
00290         } else if(aWParam==ID_FILE_INVENTOR) {
00291           This->WriteInventor();
00292         } else if(aWParam==ID_FILE_ESCAPE) {
00293           This->Escape();
00294         // Etc :
00295         } else if(aWParam==ID_ETC_ERASE_DETECTOR) {
00296           This->EraseDetector();
00297         } else if(aWParam==ID_ETC_ERASE_EVENT) {
00298           This->EraseEvent();
00299         } else if(aWParam==ID_ETC_SET_SOLID) {
00300           This->SetSolid();
00301         } else if(aWParam==ID_ETC_SET_WIRE_FRAME) {
00302           This->SetWireFrame();
00303         } else if(aWParam==ID_ETC_SET_REDUCED_WIRE_FRAME) {
00304           This->SetReducedWireFrame(true);
00305         } else if(aWParam==ID_ETC_SET_FULL_WIRE_FRAME) {
00306           This->SetReducedWireFrame(false);
00307         } else if(aWParam==ID_ETC_SET_PREVIEW) {
00308           This->SetPreview();
00309         } else if(aWParam==ID_ETC_SET_PREVIEW_AND_FULL) {
00310           This->SetPreviewAndFull();
00311         } else if(aWParam==ID_ETC_UPDATE_SCENE) {
00312           This->UpdateScene();
00313         } else if(aWParam==ID_ETC_STATS) {
00314           This->SceneGraphStatistics();
00315         // Help :
00316         } else if(aWParam==ID_HELP_CONTROLS) {
00317           G4cout << This->Help() << G4endl;
00318         }
00319       }
00320     }
00321   }return 0;
00322   default:
00323     return (::DefWindowProc(aWindow,aMessage,aWParam,aLParam));
00324   }
00325 }
00326 
00327 #endif

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