2ca07f700892e287d59bc1e340c995be6feaeb64
[occt.git] / samples / mfc / standard / 01_Geometry / src / GeometryView.cpp
1 // GeometryView.cpp : implementation of the CGeometryView class
2 //
3
4 #include "stdafx.h"
5 #include "GeometryApp.h"
6
7 #include <GeometryApp.h>
8 #include "GeometryDoc.h"
9 #include "GeometryView.h"
10
11 #define ValZWMin 1
12
13 #ifdef _DEBUG
14 #undef THIS_FILE
15 static char THIS_FILE[] = __FILE__;
16 #endif
17
18 /////////////////////////////////////////////////////////////////////////////
19 // CGeometryView
20
21 IMPLEMENT_DYNCREATE(CGeometryView, OCC_3dView)
22
23 BEGIN_MESSAGE_MAP(CGeometryView, OCC_3dView)
24   //{{AFX_MSG_MAP(CGeometryView)
25   ON_COMMAND(ID_FILE_EXPORT_IMAGE, OnFileExportImage)
26   ON_WM_SIZE()
27   ON_WM_LBUTTONDOWN()
28   ON_WM_LBUTTONUP()
29   ON_WM_MBUTTONDOWN()
30   ON_WM_MBUTTONUP()
31   ON_WM_MOUSEMOVE()
32   ON_WM_RBUTTONDOWN()
33   ON_WM_RBUTTONUP()
34   ON_UPDATE_COMMAND_UI(ID_BUTTONHlrOff, OnUpdateBUTTONHlrOff)
35   ON_UPDATE_COMMAND_UI(ID_BUTTONHlrOn, OnUpdateBUTTONHlrOn)
36   ON_UPDATE_COMMAND_UI(ID_BUTTONPanGlo, OnUpdateBUTTONPanGlo)
37   ON_UPDATE_COMMAND_UI(ID_BUTTONPan, OnUpdateBUTTONPan)
38   ON_UPDATE_COMMAND_UI(ID_BUTTONZoomProg, OnUpdateBUTTONZoomProg)
39   ON_UPDATE_COMMAND_UI(ID_BUTTONZoomWin, OnUpdateBUTTONZoomWin)
40   ON_UPDATE_COMMAND_UI(ID_BUTTONRot, OnUpdateBUTTONRot)
41   //}}AFX_MSG_MAP
42 END_MESSAGE_MAP()
43
44 /////////////////////////////////////////////////////////////////////////////
45 // CGeometryView construction/destruction
46
47 CGeometryView::CGeometryView()
48 {
49 }
50
51 CGeometryView::~CGeometryView()
52 {
53 }
54
55 /////////////////////////////////////////////////////////////////////////////
56 // CGeometryView diagnostics
57
58 #ifdef _DEBUG
59 void CGeometryView::AssertValid() const
60 {
61   CView::AssertValid();
62 }
63
64 void CGeometryView::Dump(CDumpContext& dc) const
65 {
66   CView::Dump(dc);
67 }
68
69 CGeometryDoc* CGeometryView::GetDocument() // non-debug version is inline
70 {
71   ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CGeometryDoc)));
72   return (CGeometryDoc*)m_pDocument;
73 }
74 #endif //_DEBUG
75
76 /////////////////////////////////////////////////////////////////////////////
77 // CGeometryView message handlers
78
79
80 void CGeometryView::OnFileExportImage()
81 {
82   GetDocument()->ExportView (myView);
83 }
84
85 void CGeometryView::OnSize(UINT /*nType*/, int /*cx*/, int /*cy*/)
86 {
87   if (!myView.IsNull())
88     myView->MustBeResized();
89 }
90
91 void CGeometryView::OnLButtonDown(UINT nFlags, CPoint point) 
92 {
93   //  save the current mouse coordinate in min 
94   myXmin=point.x;  myYmin=point.y;
95   myXmax=point.x;  myYmax=point.y;
96
97   if ( nFlags & MK_CONTROL ) 
98   {
99     // Button MB1 down Control :start zomming 
100     // SetCursor(AfxGetApp()->LoadStandardCursor());
101   }
102   else // if ( Ctrl )
103   {
104     switch (myCurrentMode)
105     {
106     case CurAction3d_Nothing : // start a drag
107       if (nFlags & MK_SHIFT)
108         ShiftDragEvent(myXmax,myYmax,-1);
109       else
110         DragEvent(myXmax,myYmax,-1);
111       break;
112       break;
113     case CurAction3d_DynamicZooming : // noting
114       // SetCursor(AfxGetApp()->LoadStandardCursor());
115       break;
116     case CurAction3d_WindowZooming : 
117       break;
118     case CurAction3d_DynamicPanning :// noting
119       // SetCursor(AfxGetApp()->LoadStandardCursor());
120       break;
121     case CurAction3d_GlobalPanning :// noting
122       // SetCursor(AfxGetApp()->LoadStandardCursor());
123       break;
124     case  CurAction3d_DynamicRotation :
125       // SetCursor(AfxGetApp()->LoadStandardCursor());
126       if (myHlrModeIsOn)
127       {
128         myView->SetComputedMode (Standard_False);
129       }
130       myView->StartRotation (point.x, point.y);
131       break;
132     default :
133       Standard_Failure::Raise(" incompatible Current Mode ");
134       break;
135     }
136   }
137 }
138
139 void CGeometryView::OnLButtonUp(UINT nFlags, CPoint point) 
140 {
141   if ( nFlags & MK_CONTROL ) 
142   {
143     return;
144   }
145   else // if ( Ctrl )
146   {
147     switch (myCurrentMode)
148     {
149     case CurAction3d_Nothing :
150       if (point.x == myXmin && point.y == myYmin)
151       { // no offset between down and up --> selectEvent
152         myXmax=point.x;  
153         myYmax=point.y;
154         if (nFlags & MK_SHIFT )
155           ShiftInputEvent(point.x,point.y);
156         else
157           InputEvent     (point.x,point.y);
158       } else
159       {
160         myXmax=point.x;    myYmax=point.y;
161         DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_False);
162         if (nFlags & MK_SHIFT)
163           ShiftDragEvent(point.x,point.y,1);
164         else
165           DragEvent(point.x,point.y,1);
166       }
167       break;
168     case CurAction3d_DynamicZooming :
169       // SetCursor(AfxGetApp()->LoadStandardCursor());         
170       myCurrentMode = CurAction3d_Nothing;
171       break;
172     case CurAction3d_WindowZooming :
173       myXmax=point.x;  myYmax=point.y;
174       DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_False,LongDash);
175       if ((abs(myXmin-myXmax)>ValZWMin) || (abs(myYmin-myYmax)>ValZWMin))
176         // Test if the zoom window is greater than a minimale window.
177       {
178         // Do the zoom window between Pmin and Pmax
179         myView->WindowFitAll(myXmin,myYmin,myXmax,myYmax);  
180       }  
181       myCurrentMode = CurAction3d_Nothing;
182       break;
183     case CurAction3d_DynamicPanning :
184       myCurrentMode = CurAction3d_Nothing;
185       break;
186     case CurAction3d_GlobalPanning :
187       myView->Place(point.x,point.y,myCurZoom); 
188       myCurrentMode = CurAction3d_Nothing;
189       break;
190     case  CurAction3d_DynamicRotation :
191       myCurrentMode = CurAction3d_Nothing;
192       break;
193     default :
194       Standard_Failure::Raise(" incompatible Current Mode ");
195       break;
196     } //switch (myCurrentMode)
197   } //  else // if ( Ctrl )
198 }
199
200 void CGeometryView::OnMButtonDown(UINT nFlags, CPoint /*point*/) 
201 {
202   if ( nFlags & MK_CONTROL ) 
203   {
204     // Button MB2 down Control : panning init  
205     // SetCursor(AfxGetApp()->LoadStandardCursor());   
206   }
207 }
208
209 void CGeometryView::OnMButtonUp(UINT nFlags, CPoint /*point*/)
210 {
211   if ( nFlags & MK_CONTROL ) 
212   {
213     // Button MB2 down Control : panning init  
214     // SetCursor(AfxGetApp()->LoadStandardCursor());   
215   }
216 }
217
218 void CGeometryView::OnRButtonDown(UINT nFlags, CPoint point) 
219 {
220   if ( nFlags & MK_CONTROL )
221   {
222     // SetCursor(AfxGetApp()->LoadStandardCursor());
223     if (myHlrModeIsOn)
224     {
225       myView->SetComputedMode (Standard_False);
226     }
227     myView->StartRotation (point.x, point.y);
228   }
229   else // if ( Ctrl )
230   {
231     Popup (point.x, point.y);
232   }
233 }
234
235 void CGeometryView::OnRButtonUp(UINT /*nFlags*/, CPoint /*point*/) 
236 {
237   SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
238   myView->SetComputedMode (myHlrModeIsOn);
239   SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
240 }
241
242 void CGeometryView::OnMouseMove(UINT nFlags, CPoint point) 
243 {
244   //   ============================  LEFT BUTTON =======================
245   if ( nFlags & MK_LBUTTON)
246   {
247     if ( nFlags & MK_CONTROL ) 
248     {
249       // move with MB1 and Control : on the dynamic zooming  
250       // Do the zoom in function of mouse's coordinates  
251       myView->Zoom(myXmax,myYmax,point.x,point.y); 
252       // save the current mouse coordinate in min 
253       myXmax = point.x; 
254       myYmax = point.y; 
255     }
256     else // if ( Ctrl )
257     {
258       switch (myCurrentMode)
259       {
260       case CurAction3d_Nothing :
261         DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_False);
262         myXmax = point.x; 
263         myYmax = point.y;
264         if (nFlags & MK_SHIFT)          
265           ShiftDragEvent(myXmax,myYmax,0);
266         else
267           DragEvent(myXmax,myYmax,0);
268         DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_True);
269         break;
270       case CurAction3d_DynamicZooming :
271         myView->Zoom(myXmax,myYmax,point.x,point.y); 
272         // save the current mouse coordinate in min \n";
273         myXmax=point.x;  myYmax=point.y;
274         break;
275       case CurAction3d_WindowZooming :
276         myXmax = point.x; myYmax = point.y;     
277         DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_False,LongDash);
278         DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_True,LongDash);
279         break;
280       case CurAction3d_DynamicPanning :
281         myView->Pan(point.x-myXmax,myYmax-point.y); // Realize the panning
282         myXmax = point.x; myYmax = point.y;     
283         break;
284       case CurAction3d_GlobalPanning : // nothing           
285         break;
286       case  CurAction3d_DynamicRotation :
287         myView->Rotation(point.x,point.y);
288         myView->Redraw();
289         break;
290       default :
291         Standard_Failure::Raise(" incompatible Current Mode ");
292         break;
293       }//  switch (myCurrentMode)
294     }// if ( nFlags & MK_CONTROL )  else 
295   } else //   if ( nFlags & MK_LBUTTON) 
296     //   ============================  MIDDLE BUTTON =======================
297     if ( nFlags & MK_MBUTTON)
298     {
299       if ( nFlags & MK_CONTROL ) 
300       {
301         myView->Pan(point.x-myXmax,myYmax-point.y); // Realize the panning
302         myXmax = point.x; myYmax = point.y;     
303
304       }
305     } else //  if ( nFlags & MK_MBUTTON)
306       //   ============================  RIGHT BUTTON =======================
307       if ( nFlags & MK_RBUTTON)
308       {
309         if ( nFlags & MK_CONTROL ) 
310         {
311           myView->Rotation(point.x,point.y);
312         }
313       }else //if ( nFlags & MK_RBUTTON)
314         //   ============================  NO BUTTON =======================
315       {  // No buttons 
316         myXmax = point.x; myYmax = point.y;     
317         if (nFlags & MK_SHIFT)
318           ShiftMoveEvent(point.x,point.y);
319         else
320           MoveEvent(point.x,point.y);
321       }
322 }
323
324 void CGeometryView::OnUpdateBUTTONHlrOff (CCmdUI* pCmdUI)
325 {
326   pCmdUI->SetCheck (!myHlrModeIsOn);
327   pCmdUI->Enable   (myHlrModeIsOn);
328 }
329
330 void CGeometryView::OnUpdateBUTTONHlrOn (CCmdUI* pCmdUI)
331 {
332   pCmdUI->SetCheck (myHlrModeIsOn);
333   pCmdUI->Enable   (!myHlrModeIsOn);
334 }
335
336 void CGeometryView::OnUpdateBUTTONPanGlo(CCmdUI* pCmdUI) 
337 {
338   pCmdUI->SetCheck (myCurrentMode == CurAction3d_GlobalPanning);
339   pCmdUI->Enable   (myCurrentMode != CurAction3d_GlobalPanning);        
340
341 }
342
343 void CGeometryView::OnUpdateBUTTONPan(CCmdUI* pCmdUI) 
344 {
345   pCmdUI->SetCheck (myCurrentMode == CurAction3d_DynamicPanning);
346   pCmdUI->Enable   (myCurrentMode != CurAction3d_DynamicPanning );      
347 }
348
349 void CGeometryView::OnUpdateBUTTONZoomProg(CCmdUI* pCmdUI) 
350 {
351   pCmdUI->SetCheck (myCurrentMode == CurAction3d_DynamicZooming );
352   pCmdUI->Enable   (myCurrentMode != CurAction3d_DynamicZooming);       
353 }
354
355 void CGeometryView::OnUpdateBUTTONZoomWin(CCmdUI* pCmdUI) 
356 {
357   pCmdUI->SetCheck (myCurrentMode == CurAction3d_WindowZooming);
358   pCmdUI->Enable   (myCurrentMode != CurAction3d_WindowZooming);        
359 }
360
361 void CGeometryView::OnUpdateBUTTONRot(CCmdUI* pCmdUI) 
362 {
363   pCmdUI->SetCheck (myCurrentMode == CurAction3d_DynamicRotation);
364   pCmdUI->Enable   (myCurrentMode != CurAction3d_DynamicRotation);      
365 }
366
367 //==========================================================================================
368 //==========================================================================================
369 //==========================================================================================
370
371 //-----------------------------------------------------------------------------------------
372 //
373 //-----------------------------------------------------------------------------------------
374 void CGeometryView::DragEvent(const Standard_Integer  x        ,
375                               const Standard_Integer  y        ,
376                               const Standard_Integer  TheState )
377 {
378
379   // TheState == -1  button down
380   // TheState ==  0  move
381   // TheState ==  1  button up
382
383   static Standard_Integer theButtonDownX=0;
384   static Standard_Integer theButtonDownY=0;
385
386   if (TheState == -1)
387   {
388     theButtonDownX=x;
389     theButtonDownY=y;
390   }
391
392   if (TheState == 0)
393     GetDocument()->GetAISContext()->Select(theButtonDownX,theButtonDownY,x,y,myView);  
394
395
396 //-----------------------------------------------------------------------------------------
397 //
398 //-----------------------------------------------------------------------------------------
399 void CGeometryView::InputEvent(const Standard_Integer /*x*/,
400                                const Standard_Integer /*y*/)
401 {
402   GetDocument()->GetAISContext()->Select(); 
403 }
404
405 //-----------------------------------------------------------------------------------------
406 //
407 //-----------------------------------------------------------------------------------------
408 void CGeometryView::MoveEvent(const Standard_Integer x,
409                               const Standard_Integer y)
410 {
411   GetDocument()->GetAISContext()->MoveTo(x,y,myView);
412 }
413
414 //-----------------------------------------------------------------------------------------
415 //
416 //-----------------------------------------------------------------------------------------
417 void CGeometryView::ShiftMoveEvent(const Standard_Integer  x       ,
418                                    const Standard_Integer  y       ) 
419 {
420   GetDocument()->GetAISContext()->MoveTo(x,y,myView);
421 }
422
423 //-----------------------------------------------------------------------------------------
424 //
425 //-----------------------------------------------------------------------------------------
426 void CGeometryView::ShiftDragEvent(const Standard_Integer  x        ,
427                                    const Standard_Integer  y        ,
428                                    const Standard_Integer  TheState ) 
429 {
430   static Standard_Integer theButtonDownX=0;
431   static Standard_Integer theButtonDownY=0;
432
433   if (TheState == -1)
434   {
435     theButtonDownX=x;
436     theButtonDownY=y;
437   }
438
439   if (TheState == 0)
440     GetDocument()->GetAISContext()->ShiftSelect(theButtonDownX,theButtonDownY,x,y,myView);  
441 }
442
443
444 //-----------------------------------------------------------------------------------------
445 //
446 //-----------------------------------------------------------------------------------------
447 void CGeometryView::ShiftInputEvent(const Standard_Integer /*x*/,
448                                     const Standard_Integer /*y*/)
449 {
450   GetDocument()->GetAISContext()->ShiftSelect(); 
451 }
452
453 //-----------------------------------------------------------------------------------------
454 //
455 //-----------------------------------------------------------------------------------------
456 void  CGeometryView::Popup(const Standard_Integer  x,
457                            const Standard_Integer  y ) 
458 {
459   Standard_Integer PopupMenuNumber=0;
460   GetDocument()->GetAISContext()->InitCurrent();
461   if (GetDocument()->GetAISContext()->MoreCurrent())
462     PopupMenuNumber=1;
463
464   CMenu menu;
465   VERIFY(menu.LoadMenu(IDR_Popup3D));
466   CMenu* pPopup = menu.GetSubMenu(PopupMenuNumber);
467   ASSERT(pPopup != NULL);
468
469   POINT winCoord = { x , y };
470   Handle(WNT_Window) aWNTWindow=
471     Handle(WNT_Window)::DownCast(myView->Window());
472   ClientToScreen ( &winCoord);
473   pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON , winCoord.x, winCoord.y , 
474     AfxGetMainWnd());
475 }
476
477