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