source: tags/Visualization_after-vis09-02-01-tag/OpenInventor/src/G4OpenInventorWinViewer.cc @ 958

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

tag

  • Property svn:mime-type set to text/cpp
File size: 11.1 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: G4OpenInventorWinViewer.cc,v 1.25 2006/06/29 21:22:22 gunter Exp $
28// GEANT4 tag $Name:  $
29//
30/*
31 * jck : 05 Feb 1997 : Initial Implementation
32 * jck : 21 Apr 1997 : Mods for SoXtHepViewer
33 * gb : on Win32 use an SoXtExaminerViewer.
34 * gb : 05 April 2004 : creation.
35 * gb : 09 November 2004 : Pulldown menu with the escape menu item.
36 * gb 14 November 2004 : inherit G4OpenInventorViewer.
37 */
38
39#ifdef G4VIS_BUILD_OIWIN32_DRIVER
40
41// this :
42#include "G4OpenInventorWinViewer.hh"
43
44#include <Inventor/nodes/SoSelection.h>
45
46#include <Inventor/Win/SoWin.h>
47#include <Inventor/Win/viewers/SoWinExaminerViewer.h>
48
49#include "HEPVis/actions/SoGL2PSAction.h"
50
51#include "G4OpenInventor.hh"
52#include "G4OpenInventorSceneHandler.hh"
53#include "G4VInteractorManager.hh"
54
55#include <windowsx.h>
56
57// To have sizeChanged public :
58class Geant4_SoWinExaminerViewer : public SoWinExaminerViewer {
59public:
60  Geant4_SoWinExaminerViewer(HWND parent,const char* name,SbBool embed)
61  :SoWinExaminerViewer(parent,name,embed){}
62  virtual void sizeChanged(const SbVec2s & size){
63    SoWinExaminerViewer::sizeChanged(size);
64  }
65};
66
67#define SIZE 600
68// File :
69#define ID_FILE_POSTSCRIPT 1
70#define ID_FILE_PIXMAP_POSTSCRIPT 2
71#define ID_FILE_INVENTOR 3
72#define ID_FILE_ESCAPE 4
73// Etc :
74#define ID_ETC_ERASE_DETECTOR 101
75#define ID_ETC_ERASE_EVENT 102
76#define ID_ETC_SET_SOLID 103
77#define ID_ETC_SET_WIRE_FRAME 104
78#define ID_ETC_SET_REDUCED_WIRE_FRAME 105
79#define ID_ETC_SET_FULL_WIRE_FRAME 106
80#define ID_ETC_SET_PREVIEW 107
81#define ID_ETC_SET_PREVIEW_AND_FULL 108
82#define ID_ETC_UPDATE_SCENE 109
83#define ID_ETC_STATS 110
84// Help :
85#define ID_HELP_CONTROLS 201
86
87//static void SecondaryLoopPostAction ();
88
89static const char className[] = "G4OpenInventorShellWindow";
90
91G4OpenInventorWinViewer::G4OpenInventorWinViewer(
92 G4OpenInventorSceneHandler& sceneHandler
93,const G4String& name)
94:G4OpenInventorViewer (sceneHandler, name)
95,fShell(0)
96,fViewer(0)
97{
98  G4cout << "Window name: " << fName << G4endl;
99
100  G4String wName = fName;
101
102  HWND parent = (HWND)fInteractorManager->GetParentInteractor ();
103  if(!parent) {
104    //Create a shell window :
105    G4String shellName = wName;
106    shellName += "_shell";
107    static SbBool done = FALSE;
108    if(done==FALSE) {
109      HBRUSH brush = (HBRUSH) GetSysColorBrush(COLOR_BTNFACE);
110      WNDCLASS wc;
111      wc.style = CS_HREDRAW | CS_VREDRAW;
112      wc.lpfnWndProc = (WNDPROC)WindowProc;
113      wc.cbClsExtra = 0;
114      wc.cbWndExtra = 0;
115      wc.hInstance = ::GetModuleHandle(0);
116      wc.hIcon = ::LoadIcon(0, IDI_APPLICATION);
117      wc.hCursor = ::LoadCursor(0, IDC_ARROW);
118      wc.hbrBackground = brush;
119      wc.lpszMenuName = className;
120      wc.lpszClassName = className;
121      ::RegisterClass(&wc);
122      done = TRUE;
123    }
124
125    HMENU menuBar = CreateMenu();
126
127   {HMENU casc = CreatePopupMenu();
128    ::AppendMenu(menuBar,MF_POPUP,(UINT)casc,"File");
129    ::AppendMenu(casc,MF_STRING,ID_FILE_POSTSCRIPT,"PS (gl2ps)");
130    ::AppendMenu(casc,MF_STRING,ID_FILE_PIXMAP_POSTSCRIPT,"PS (pixmap)");
131    ::AppendMenu(casc,MF_STRING,ID_FILE_INVENTOR,"IV");
132    ::AppendMenu(casc,MF_STRING,ID_FILE_ESCAPE,"Escape");}
133
134   {HMENU casc = CreatePopupMenu();
135    ::AppendMenu(menuBar,MF_POPUP,(UINT)casc,"Etc");
136    ::AppendMenu(casc,MF_STRING,ID_ETC_ERASE_DETECTOR,"Erase detector");
137    ::AppendMenu(casc,MF_STRING,ID_ETC_ERASE_EVENT,"Erase event");
138    ::AppendMenu(casc,MF_STRING,ID_ETC_SET_SOLID,"Set solid");
139    //::AppendMenu(casc,MF_STRING,ID_ETC_SET_WIRE_FRAME,"Set (G4) wire frame");
140    ::AppendMenu(casc,MF_STRING,ID_ETC_SET_REDUCED_WIRE_FRAME,
141                      "Set (G4) reduced wire frame");
142    ::AppendMenu(casc,MF_STRING,ID_ETC_SET_FULL_WIRE_FRAME,
143                      "Set (G4) full wire frame");
144    ::AppendMenu(casc,MF_STRING,ID_ETC_SET_PREVIEW,
145                      "Visible mothers + invisible daughters");
146    ::AppendMenu(casc,MF_STRING,ID_ETC_SET_PREVIEW_AND_FULL,
147                      "Visible mothers + visible daughters");
148    ::AppendMenu(casc,MF_STRING,ID_ETC_UPDATE_SCENE,"Update scene");
149    ::AppendMenu(casc,MF_STRING,ID_ETC_STATS,"Scene graph stats");}
150
151   {HMENU casc = CreatePopupMenu();
152    ::AppendMenu(menuBar,MF_POPUP,(UINT)casc,"Help");
153    ::AppendMenu(casc,MF_STRING,ID_HELP_CONTROLS,"Controls");}
154
155    fShell = ::CreateWindow(className, shellName.c_str(),
156                            WS_OVERLAPPEDWINDOW |
157                            WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
158                            CW_USEDEFAULT, CW_USEDEFAULT,
159                            SIZE,SIZE,
160                            0,menuBar,::GetModuleHandle(0),0);
161    // Retreive window and client sizez :
162    RECT wrect,crect;
163    GetWindowRect((HWND)fShell,&wrect);
164    GetClientRect((HWND)fShell,&crect);
165    int ww = wrect.right-wrect.left;
166    int wh = wrect.bottom-wrect.top;
167    int cw = crect.right-crect.left;
168    int ch = crect.bottom-crect.top;
169    // Compell client rect to be SIZE SIZE :
170    MoveWindow((HWND)fShell,wrect.left,wrect.top,SIZE+ww-cw,SIZE+wh-ch,TRUE);
171    ::SetWindowLong((HWND)fShell,GWL_USERDATA,LONG(this));
172    ::SetWindowText((HWND)fShell,shellName.c_str());
173    parent = fShell;
174    fInteractorManager->AddShell(fShell);
175  } else {
176    char* str = fInteractorManager->GetCreationString();
177    if(str!=0) wName = str;
178  }
179  fViewer = new Geant4_SoWinExaminerViewer(parent,wName.c_str(),TRUE);
180
181  // Have a GL2PS render action :
182  const SbViewportRegion& vpRegion = fViewer->getViewportRegion();
183  fGL2PSAction = new SoGL2PSAction(vpRegion);
184  fViewer->setGLRenderAction(fGL2PSAction);
185
186  fViewer->setSize(SbVec2s(SIZE,SIZE));
187  fViewer->setSceneGraph(fSoSelection);
188  fViewer->viewAll();
189  fViewer->saveHomePosition();
190  fViewer->setTitle(fName);
191  fViewer->show();
192  if(fShell) {
193    SoWin::show(fShell);
194    fInteractorManager->FlushAndWaitExecution ();
195  }
196  fInteractorManager->SetCreatedInteractor (fViewer -> getWidget());
197}
198
199G4OpenInventorWinViewer::~G4OpenInventorWinViewer () {
200  if(fShell) fInteractorManager->RemoveShell(fShell);
201  if(fViewer) {
202    fViewer->setSceneGraph(0);
203    delete fViewer;
204  }
205  if(fShell) {
206    ::SetWindowLong((HWND)fShell,GWL_USERDATA,LONG(0));
207    ::DestroyWindow((HWND)fShell);
208  }
209}
210
211void G4OpenInventorWinViewer::FinishView () {
212  if(!fViewer) return;
213  fViewer->viewAll();
214  fViewer->saveHomePosition();
215}
216
217void G4OpenInventorWinViewer::SetView () {
218  G4OpenInventorViewer::SetView ();
219  if(!fViewer) return;
220  // Background.
221  G4Colour b = fVP.GetBackgroundColour ();
222  fViewer->setBackgroundColor
223    (SbColor((float)b.GetRed(),(float)b.GetGreen(),(float)b.GetBlue()));
224}
225void G4OpenInventorWinViewer::ViewerRender () {
226  if(!fViewer) return;
227  fViewer->render();
228}
229
230SoCamera* G4OpenInventorWinViewer::GetCamera () {
231  if(!fViewer) return 0;
232  return fViewer->getCamera();
233}
234
235
236//////////////////////////////////////////////////////////////////////////////
237LRESULT CALLBACK G4OpenInventorWinViewer::WindowProc (
238 HWND   aWindow
239,UINT   aMessage
240,WPARAM aWParam
241,LPARAM aLParam
242)
243//////////////////////////////////////////////////////////////////////////////
244//  Below treatment of WM_SIZE, WM_SETFOCUS not necessary
245// with TGS, but needed with SoFree. WM_DESTROY needed for
246// 'main top level window' so that 'Close window' induces
247// the end of the task.
248//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
249{
250  switch (aMessage) {
251  case WM_SIZE:{ // Assume one child window !
252    int width = LOWORD(aLParam);
253    int height = HIWORD(aLParam);
254    //printf("debug : G4SoWindow : WMS_SIZE : %d %d\n",width,height);
255    G4OpenInventorWinViewer* This =
256      (G4OpenInventorWinViewer*)::GetWindowLong(aWindow,GWL_USERDATA);
257    if(This && This->fViewer) {
258      This->fViewer->sizeChanged(SbVec2s(width,height));
259    }
260  }return 0;
261  case WM_SETFOCUS:{ // Assume one child window !
262    HWND hwnd = ::GetFirstChild(aWindow);
263    if(hwnd!=0) ::SetFocus(hwnd);
264  }return 0;
265  case WM_DESTROY:{
266    //G4OpenInventorWinViewer* This =
267    //  (G4OpenInventorWinViewer*)::GetWindowLong(aWindow,GWL_USERDATA);
268    //::PostQuitMessage(0);
269  }return 0;
270  case WM_COMMAND:{
271    G4OpenInventorWinViewer* This =
272      (G4OpenInventorWinViewer*)::GetWindowLong(aWindow,GWL_USERDATA);
273    if(This) {
274      if(aLParam==0) { //From menu.
275        // File :
276        if(aWParam==ID_FILE_POSTSCRIPT) {
277          This->WritePostScript();
278        } else if(aWParam==ID_FILE_PIXMAP_POSTSCRIPT) {
279          This->WritePixmapPostScript();
280        } else if(aWParam==ID_FILE_INVENTOR) {
281          This->WriteInventor();
282        } else if(aWParam==ID_FILE_ESCAPE) {
283          This->Escape();
284        // Etc :
285        } else if(aWParam==ID_ETC_ERASE_DETECTOR) {
286          This->EraseDetector();
287        } else if(aWParam==ID_ETC_ERASE_EVENT) {
288          This->EraseEvent();
289        } else if(aWParam==ID_ETC_SET_SOLID) {
290          This->SetSolid();
291        } else if(aWParam==ID_ETC_SET_WIRE_FRAME) {
292          This->SetWireFrame();
293        } else if(aWParam==ID_ETC_SET_REDUCED_WIRE_FRAME) {
294          This->SetReducedWireFrame(true);
295        } else if(aWParam==ID_ETC_SET_FULL_WIRE_FRAME) {
296          This->SetReducedWireFrame(false);
297        } else if(aWParam==ID_ETC_SET_PREVIEW) {
298          This->SetPreview();
299        } else if(aWParam==ID_ETC_SET_PREVIEW_AND_FULL) {
300          This->SetPreviewAndFull();
301        } else if(aWParam==ID_ETC_UPDATE_SCENE) {
302          This->UpdateScene();
303        } else if(aWParam==ID_ETC_STATS) {
304          This->SceneGraphStatistics();
305        // Help :
306        } else if(aWParam==ID_HELP_CONTROLS) {
307          G4cout << This->Help() << G4endl;
308        }
309      }
310    }
311  }return 0;
312  default:
313    return (::DefWindowProc(aWindow,aMessage,aWParam,aLParam));
314  }
315}
316
317#endif
Note: See TracBrowser for help on using the repository browser.