0022459: The ImportExport sample crash
[occt.git] / samples / mfc / standard / 09_Animation / src / AnimationView3D.cpp
1 // AnimationView3D.cpp : implementation of the CAnimationView3D class
2 //
3
4 #include "stdafx.h"
5
6 #include "AnimationView3D.h"
7
8 #include "AnimationApp.h"
9 #include "ShadingDialog.h"
10 #include "AnimationDoc.h"
11
12 #include "Sensitivity.h"
13
14 #ifdef _DEBUG
15 //#define new DEBUG_NEW by CasCade
16 #undef THIS_FILE
17 static char THIS_FILE[] = __FILE__;
18 #endif
19 static int rotCount = 0;
20
21 // for elastic bean selection
22 #define ValZWMin 1
23
24
25 /////////////////////////////////////////////////////////////////////////////
26 // CAnimationView3D
27
28 IMPLEMENT_DYNCREATE(CAnimationView3D, CView)
29
30 BEGIN_MESSAGE_MAP(CAnimationView3D, CView)
31         //{{AFX_MSG_MAP(CAnimationView3D)
32         ON_COMMAND(ID_BUTTONAxo, OnBUTTONAxo)
33         ON_COMMAND(ID_BUTTONBack, OnBUTTONBack)
34         ON_COMMAND(ID_BUTTONBottom, OnBUTTONBottom)
35         ON_COMMAND(ID_BUTTONFront, OnBUTTONFront)
36         ON_COMMAND(ID_BUTTONHlrOff, OnBUTTONHlrOff)
37         ON_COMMAND(ID_BUTTONHlrOn, OnBUTTONHlrOn)
38         ON_COMMAND(ID_BUTTONLeft, OnBUTTONLeft)
39         ON_COMMAND(ID_BUTTONPan, OnBUTTONPan)
40         ON_COMMAND(ID_BUTTONPanGlo, OnBUTTONPanGlo)
41         ON_COMMAND(ID_BUTTONReset, OnBUTTONReset)
42         ON_COMMAND(ID_BUTTONRight, OnBUTTONRight)
43         ON_COMMAND(ID_BUTTONRot, OnBUTTONRot)
44         ON_COMMAND(ID_BUTTONTop, OnBUTTONTop)
45         ON_COMMAND(ID_BUTTONZoomAll, OnBUTTONZoomAll)
46     ON_COMMAND(ID_FILE_EXPORT_IMAGE, OnFileExportImage)
47         ON_WM_SIZE()
48         ON_COMMAND(ID_BUTTONZoomProg, OnBUTTONZoomProg)
49         ON_COMMAND(ID_BUTTONZoomWin, OnBUTTONZoomWin)
50         ON_WM_LBUTTONDOWN()
51         ON_WM_LBUTTONUP()
52         ON_WM_MBUTTONDOWN()
53         ON_WM_MBUTTONUP()
54         ON_WM_MOUSEMOVE()
55         ON_WM_RBUTTONDOWN()
56         ON_WM_RBUTTONUP()
57         ON_UPDATE_COMMAND_UI(ID_BUTTONHlrOff, OnUpdateBUTTONHlrOff)
58         ON_UPDATE_COMMAND_UI(ID_BUTTONHlrOn, OnUpdateBUTTONHlrOn)
59         ON_UPDATE_COMMAND_UI(ID_BUTTONPanGlo, OnUpdateBUTTONPanGlo)
60         ON_UPDATE_COMMAND_UI(ID_BUTTONPan, OnUpdateBUTTONPan)
61         ON_UPDATE_COMMAND_UI(ID_BUTTONZoomProg, OnUpdateBUTTONZoomProg)
62         ON_UPDATE_COMMAND_UI(ID_BUTTONZoomWin, OnUpdateBUTTONZoomWin)
63         ON_UPDATE_COMMAND_UI(ID_BUTTONRot, OnUpdateBUTTONRot)
64
65         ON_COMMAND(ID_Modify_ChangeBackground     , OnChangeBackground)
66         ON_WM_TIMER()
67         ON_COMMAND(ID_STOP, OnStop)
68         ON_COMMAND(ID_RESTART, OnRestart)
69
70         ON_COMMAND(ID_SENSITIVITY, OnSensitivity)
71         ON_COMMAND(ID_BUTTONFly, OnBUTTONFly)
72         ON_COMMAND(ID_BUTTONTurn, OnBUTTONTurn)
73         ON_UPDATE_COMMAND_UI(ID_BUTTONFly, OnUpdateBUTTONFly)
74         ON_UPDATE_COMMAND_UI(ID_BUTTONTurn, OnUpdateBUTTONTurn)
75         ON_COMMAND(ID_VIEW_DISPLAYSTATUS, OnViewDisplaystatus)
76         ON_UPDATE_COMMAND_UI(ID_VIEW_DISPLAYSTATUS, OnUpdateViewDisplaystatus)
77         //}}AFX_MSG_MAP
78 // CasCade
79
80 END_MESSAGE_MAP()
81
82 /////////////////////////////////////////////////////////////////////////////
83 // CAnimationView3D construction/destruction
84
85 CAnimationView3D::CAnimationView3D()
86 {
87         // TODO: add construction code here
88
89     myXmin=0;
90     myYmin=0;  
91     myXmax=0;
92     myYmax=0;
93     myCurZoom=0;
94     // will be set in OnInitial update, but, for more security :
95     myCurrentMode = CurrentAction3d_Nothing;
96     myDegenerateModeIsOn=Standard_True;
97     m_Pen = NULL;
98
99         myXmin=0;
100     myYmin=0;  
101     myXmax=0;
102     myYmax=0;
103     myCurZoom=0;
104
105     // will be set in OnInitial update, but, for more security :
106         m_FlySens = 500. ;
107         m_TurnSens = M_PI / 40. ;
108 }
109
110 CAnimationView3D::~CAnimationView3D()
111 {
112     myView->Remove();
113     if (m_Pen) delete m_Pen;
114 }
115
116 BOOL CAnimationView3D::PreCreateWindow(CREATESTRUCT& cs)
117 {
118         // TODO: Modify the Window class or styles here by modifying
119         //  the CREATESTRUCT cs
120
121         return CView::PreCreateWindow(cs);
122 }
123
124 /////////////////////////////////////////////////////////////////////////////
125 // CAnimationView3D drawing
126
127 void CAnimationView3D::OnDraw(CDC* pDC)
128 {
129         CAnimationDoc* pDoc = GetDocument();
130         ASSERT_VALID(pDoc);
131
132         // TODO: add draw code for native data here
133
134           myView->Redraw();
135
136 }
137 void CAnimationView3D::OnInitialUpdate() 
138 {
139         CView::OnInitialUpdate();
140
141         // TODO: Add your specialized code here and/or call the base class
142             // TODO: Add your specialized code here and/or call the base class
143 //      myView = GetDocument()->GetViewer()->CreateView();
144
145         Handle(V3d_Viewer) aViewer ;
146         
147     aViewer = GetDocument()->GetViewer() ;
148         aViewer->DefaultPerspectiveView () ;
149         aViewer->SetDefaultTypeOfView ( V3d_PERSPECTIVE ) ;
150     myView = aViewer->CreateView();
151
152     // store for restore state after rotation (witch is in Degenerated mode)
153     myDegenerateModeIsOn = myView->DegenerateModeIsOn();
154
155         Handle(Graphic3d_WNTGraphicDevice) theGraphicDevice = 
156                 ((CAnimationApp*)AfxGetApp())->GetGraphicDevice();
157     
158     Handle(WNT_Window) aWNTWindow = new WNT_Window(theGraphicDevice,GetSafeHwnd ());
159     myView->SetWindow(aWNTWindow);
160     if (!aWNTWindow->IsMapped()) aWNTWindow->Map();
161
162     // store the mode ( nothing , dynamic zooming, dynamic ... )
163     myCurrentMode = CurrentAction3d_Nothing;
164         CFrameWnd* pParentFrm = GetParentFrame();
165         pParentFrm->ActivateFrame(SW_SHOWMAXIMIZED);
166
167         Standard_Integer w=100 , h=100 ;   /* Debug Matrox                         */
168         aWNTWindow->Size (w,h) ;           /* Keeps me unsatisfied (rlb).....      */
169                                            /* Resize is not supposed to be done on */
170                                            /* Matrox                               */
171                                            /* I suspect another problem elsewhere  */
172         ::PostMessage ( GetSafeHwnd () , WM_SIZE , SIZE_RESTORED , w + h*65536 ) ;
173
174         myPView = Handle(V3d_PerspectiveView)::DownCast (myView);
175
176         m_Tune.Create ( IDD_TUNE , NULL ) ;
177         
178         RECT dlgrect;
179         m_Tune.GetWindowRect(&dlgrect);
180         LONG width = dlgrect.right-dlgrect.left;
181         LONG height = dlgrect.bottom-dlgrect.top;
182         RECT MainWndRect;
183         AfxGetApp()->m_pMainWnd->GetWindowRect(&MainWndRect);
184         LONG left = MainWndRect.left+3;
185         LONG top = MainWndRect.top + 112;
186         m_Tune.MoveWindow(left,top,width,height);
187         
188         m_Tune.m_pView = this ;
189
190         m_Tune.ShowWindow ( SW_HIDE );
191
192     // store the mode ( nothing , dynamic zooming, dynamic ... )
193
194     myCurrentMode = CurrentAction3d_Nothing;
195
196         ReloadData () ;
197
198 }
199
200 void CAnimationView3D::DisplayTuneDialog()
201 {
202         m_Tune.Create ( IDD_TUNE , NULL ) ;
203         
204         RECT dlgrect;
205         m_Tune.GetWindowRect(&dlgrect);
206         LONG width = dlgrect.right-dlgrect.left;
207         LONG height = dlgrect.bottom-dlgrect.top;
208         RECT MainWndRect;
209         AfxGetApp()->m_pMainWnd->GetWindowRect(&MainWndRect);
210         LONG left = MainWndRect.left+3;
211         LONG top = MainWndRect.top + 112;
212         m_Tune.MoveWindow(left,top,width,height);
213         
214         m_Tune.m_pView = this ;
215 }
216
217
218 /////////////////////////////////////////////////////////////////////////////
219 // CAnimationView3D diagnostics
220
221 #ifdef _DEBUG
222 void CAnimationView3D::AssertValid() const
223 {
224         CView::AssertValid();
225 }
226
227 void CAnimationView3D::Dump(CDumpContext& dc) const
228 {
229         CView::Dump(dc);
230 }
231
232 CAnimationDoc* CAnimationView3D::GetDocument() // non-debug version is inline
233 {
234         ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CAnimationDoc)));
235         return (CAnimationDoc*)m_pDocument;
236 }
237 #endif //_DEBUG
238
239 /////////////////////////////////////////////////////////////////////////////
240 // CAnimationView3D message handlers
241 void CAnimationView3D::OnFileExportImage()
242 {
243   CFileDialog dlg(FALSE,_T("*.BMP"),NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
244                   _T("BMP Files (*.BMP)|*.bmp |GIF Files (*.GIF)|*.gif | XWD Files (*.XWD)|*.xwd||"), 
245                   NULL );
246
247   if (dlg.DoModal() == IDOK) 
248   {
249     SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
250     CString filename = dlg.GetPathName();
251     Handle(Aspect_Window) anAspectWindow = myView->Window();
252     Handle(WNT_Window) aWNTWindow = Handle(WNT_Window)::DownCast(anAspectWindow);
253     CString ext = dlg.GetFileExt();
254     if (ext == "bmp")     aWNTWindow->SetOutputFormat ( WNT_TOI_BMP );
255     if (ext == "gif")     aWNTWindow->SetOutputFormat ( WNT_TOI_GIF );
256     if (ext == "xwd")     aWNTWindow->SetOutputFormat ( WNT_TOI_XWD );
257     aWNTWindow->Dump ((Standard_CString)(LPCTSTR)filename);
258     SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
259   }
260 }
261
262 void CAnimationView3D::OnSize(UINT nType, int cx, int cy) 
263 {
264         m_cx = cx ;
265         m_cy = cy ;
266         if (!myView.IsNull())
267                 myView->MustBeResized();
268 }
269
270 void CAnimationView3D::OnBUTTONBack() 
271 { myView->SetProj(V3d_Xneg); } // See the back View
272 void CAnimationView3D::OnBUTTONFront() 
273 { myView->SetProj(V3d_Xpos); } // See the front View
274
275 void CAnimationView3D::OnBUTTONBottom() 
276 { myView->SetProj(V3d_Zneg); } // See the bottom View
277 void CAnimationView3D::OnBUTTONTop() 
278 { myView->SetProj(V3d_Zpos); } // See the top View      
279
280 void CAnimationView3D::OnBUTTONLeft() 
281 { myView->SetProj(V3d_Ypos); } // See the left View     
282 void CAnimationView3D::OnBUTTONRight() 
283 { myView->SetProj(V3d_Yneg); } // See the right View
284
285 void CAnimationView3D::OnBUTTONAxo() 
286 { myView->SetProj(V3d_XposYnegZpos); } // See the axonometric View
287
288 void CAnimationView3D::OnBUTTONHlrOff() 
289 {
290   myView->SetDegenerateModeOn();
291   myDegenerateModeIsOn = Standard_True;
292 }
293
294 void CAnimationView3D::OnBUTTONHlrOn() 
295 {
296   SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
297   myView->SetDegenerateModeOff();
298   myDegenerateModeIsOn = Standard_False;
299   SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
300 }
301
302 void CAnimationView3D::OnBUTTONPan() 
303 {  
304         myCurrentMode = CurrentAction3d_DynamicPanning; 
305 }
306
307 void CAnimationView3D::OnBUTTONPanGlo() 
308 {
309   // save the current zoom value 
310   myCurZoom = myView->Scale();
311   // Do a Global Zoom 
312   myView->FitAll();
313   // Set the mode 
314   myCurrentMode = CurrentAction3d_GlobalPanning;
315 }
316
317 void CAnimationView3D::OnBUTTONReset() 
318 {   myView->Reset(); 
319         ReloadData();
320 }
321
322 void CAnimationView3D::OnBUTTONRot() 
323 {   myCurrentMode = CurrentAction3d_DynamicRotation; }
324
325
326 void CAnimationView3D::OnBUTTONZoomAll() 
327 {
328         SetDimensions();
329         myView->FitAll();
330         myView->ZFitAll();
331 }
332
333 void CAnimationView3D::OnBUTTONZoomProg() 
334 {  myCurrentMode = CurrentAction3d_DynamicZooming; }
335
336 void CAnimationView3D::OnBUTTONZoomWin() 
337 {  myCurrentMode = CurrentAction3d_WindowZooming; }
338
339 void CAnimationView3D::OnBUTTONFly() 
340 {  myCurrentMode = CurrentAction3d_Fly; }
341
342 void CAnimationView3D::OnBUTTONTurn() 
343 {  myCurrentMode = CurrentAction3d_Turn; }
344
345
346 void CAnimationView3D::OnLButtonDown(UINT nFlags, CPoint point) 
347 {
348   //  save the current mouse coordinate in min 
349   myXmin=point.x;  myYmin=point.y;
350   myXmax=point.x;  myYmax=point.y;
351
352   if ( nFlags & MK_CONTROL ) 
353           {
354             // Button MB1 down Control :start zomming 
355         // SetCursor(AfxGetApp()->LoadStandardCursor());
356           }
357         else // if ( Ctrl )
358           {
359         switch (myCurrentMode)
360         {
361          case CurrentAction3d_Nothing : // start a drag
362            if (nFlags & MK_SHIFT)
363                 GetDocument()->ShiftDragEvent(myXmax,myYmax,-1,myView);
364            else
365                 GetDocument()->DragEvent(myXmax,myYmax,-1,myView);
366         break;
367          break;
368          case CurrentAction3d_DynamicZooming : // noting
369              // SetCursor(AfxGetApp()->LoadStandardCursor());
370          break;
371          case CurrentAction3d_WindowZooming : // noting
372          break;
373          case CurrentAction3d_DynamicPanning :// noting
374          break;
375          case CurrentAction3d_GlobalPanning :// noting
376         break;
377         case  CurrentAction3d_DynamicRotation :
378                         if (!myDegenerateModeIsOn)
379                           myView->SetDegenerateModeOn();
380                         myView->StartRotation(point.x,point.y);  
381         break;
382                 case  CurrentAction3d_Fly :
383                         KillTimer (1) ;
384                         SetTimer ( 1 , 100 , NULL ) ;
385                 break ;
386         case  CurrentAction3d_Turn :
387                         KillTimer (1) ;
388                         SetTimer ( 1 , 100 , NULL ) ;
389                 break ;
390         default :
391            Standard_Failure::Raise(" incompatible Current Mode ");
392         break;
393         }
394     }
395 }
396
397 void CAnimationView3D::OnLButtonUp(UINT nFlags, CPoint point) 
398 {
399    if ( nFlags & MK_CONTROL ) 
400           {
401         return;
402           }
403         else // if ( Ctrl )
404           {
405         switch (myCurrentMode)
406         {
407          case CurrentAction3d_Nothing :
408          if (point.x == myXmin && point.y == myYmin)
409          { // no offset between down and up --> selectEvent
410             myXmax=point.x;  
411             myYmax=point.y;
412             if (nFlags & MK_SHIFT )
413               GetDocument()->ShiftInputEvent(point.x,point.y,myView);
414             else
415               GetDocument()->InputEvent     (point.x,point.y,myView);
416          } else
417          {
418             DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_False);
419             myXmax=point.x;  
420             myYmax=point.y;
421                     if (nFlags & MK_SHIFT)
422                                 GetDocument()->ShiftDragEvent(point.x,point.y,1,myView);
423                         else
424                                 GetDocument()->DragEvent(point.x,point.y,1,myView);
425          }
426          break;
427          case CurrentAction3d_DynamicZooming :
428              // SetCursor(AfxGetApp()->LoadStandardCursor());         
429                myCurrentMode = CurrentAction3d_Nothing;
430          break;
431          case CurrentAction3d_WindowZooming :
432            myXmax=point.x;            myYmax=point.y;
433             DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_False,LongDash);
434                if ((abs(myXmin-myXmax)>ValZWMin) || (abs(myYmin-myYmax)>ValZWMin))
435                                          // Test if the zoom window is greater than a minimale window.
436                         {
437                           // Do the zoom window between Pmin and Pmax
438                           myView->WindowFitAll(myXmin,myYmin,myXmax,myYmax);  
439                         }  
440                myCurrentMode = CurrentAction3d_Nothing;
441          break;
442          case CurrentAction3d_DynamicPanning :
443            myCurrentMode = CurrentAction3d_Nothing;
444          break;
445          case CurrentAction3d_GlobalPanning :
446                myView->Place(point.x,point.y,myCurZoom); 
447                myCurrentMode = CurrentAction3d_Nothing;
448         break;
449         case  CurrentAction3d_DynamicRotation :
450                myCurrentMode = CurrentAction3d_Nothing;
451         break;
452                 case  CurrentAction3d_Fly :
453                         KillTimer ( 1 ) ;
454         case  CurrentAction3d_Turn :
455                         KillTimer ( 1 ) ;
456                 break;
457         default :
458            Standard_Failure::Raise(" incompatible Current Mode ");
459         break;
460         } //switch (myCurrentMode)
461     } //        else // if ( Ctrl )
462 }
463
464 void CAnimationView3D::OnMButtonDown(UINT nFlags, CPoint point) 
465 {
466    if ( nFlags & MK_CONTROL ) 
467           {
468         // Button MB2 down Control : panning init  
469         // SetCursor(AfxGetApp()->LoadStandardCursor());   
470           }
471 }
472
473 void CAnimationView3D::OnMButtonUp(UINT nFlags, CPoint point) 
474 {
475    if ( nFlags & MK_CONTROL ) 
476           {
477         // Button MB2 down Control : panning init  
478         // SetCursor(AfxGetApp()->LoadStandardCursor());   
479           }
480 }
481
482 void CAnimationView3D::OnRButtonDown(UINT nFlags, CPoint point) 
483 {
484    if ( nFlags & MK_CONTROL ) 
485           {
486         // SetCursor(AfxGetApp()->LoadStandardCursor());   
487             if (!myDegenerateModeIsOn)
488               myView->SetDegenerateModeOn();
489               myView->StartRotation(point.x,point.y);  
490           }
491         else // if ( Ctrl )
492           {
493             GetDocument()->Popup(point.x,point.y,myView);
494       } 
495 }
496
497 void CAnimationView3D::OnRButtonUp(UINT nFlags, CPoint point) 
498 {
499     SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
500     if (!myDegenerateModeIsOn)
501     {  
502       myView->SetDegenerateModeOff();
503       myDegenerateModeIsOn = Standard_False;
504     } else
505     {
506       myView->SetDegenerateModeOn();
507       myDegenerateModeIsOn = Standard_True;
508     }
509     SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
510 }
511
512 void CAnimationView3D::OnMouseMove(UINT nFlags, CPoint point) 
513 {
514     //   ============================  LEFT BUTTON =======================
515   m_curx = point.x ;
516   m_cury = point.y ;
517
518   if ( nFlags & MK_LBUTTON)
519     {
520      if ( nFlags & MK_CONTROL ) 
521           {
522             // move with MB1 and Control : on the dynamic zooming  
523             // Do the zoom in function of mouse's coordinates  
524             myView->Zoom(myXmax,myYmax,point.x,point.y); 
525             // save the current mouse coordinate in min 
526                 myXmax = point.x; 
527         myYmax = point.y;       
528           }
529           else // if ( Ctrl )
530           {
531         switch (myCurrentMode)
532         {
533          case CurrentAction3d_Nothing :
534                    myXmax = point.x;            myYmax = point.y;
535             DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_False);
536            if (nFlags & MK_SHIFT)               
537              GetDocument()->ShiftDragEvent(myXmax,myYmax,0,myView);
538            else
539              GetDocument()->DragEvent(myXmax,myYmax,0,myView);
540             DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_True);
541           break;
542          case CurrentAction3d_DynamicZooming :
543                myView->Zoom(myXmax,myYmax,point.x,point.y); 
544                // save the current mouse coordinate in min \n";
545                myXmax=point.x;  myYmax=point.y;
546          break;
547          case CurrentAction3d_WindowZooming :
548                    myXmax = point.x; myYmax = point.y;  
549             DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_False,LongDash);
550             DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_True,LongDash);
551          break;
552          case CurrentAction3d_DynamicPanning :
553                    myView->Pan(point.x-myXmax,myYmax-point.y); // Realize the panning
554                    myXmax = point.x; myYmax = point.y;  
555          break;
556          case CurrentAction3d_GlobalPanning : // nothing           
557         break;
558         case  CurrentAction3d_DynamicRotation :
559           myView->Rotation(point.x,point.y);
560               myView->Redraw();
561         break;
562                 case CurrentAction3d_Fly :
563                         break ;
564                 case CurrentAction3d_Turn :
565                         break ;
566         default :
567            Standard_Failure::Raise(" incompatible Current Mode ");
568         break;
569         }//  switch (myCurrentMode)
570       }// if ( nFlags & MK_CONTROL )  else 
571     } else //   if ( nFlags & MK_LBUTTON) 
572     //   ============================  MIDDLE BUTTON =======================
573     if ( nFlags & MK_MBUTTON)
574     {
575      if ( nFlags & MK_CONTROL ) 
576           {
577                 myView->Pan(point.x-myXmax,myYmax-point.y); // Realize the panning
578                 myXmax = point.x; myYmax = point.y;     
579
580           }
581     } else //  if ( nFlags & MK_MBUTTON)
582     //   ============================  RIGHT BUTTON =======================
583     if ( nFlags & MK_RBUTTON)
584     {
585      if ( nFlags & MK_CONTROL ) 
586           {
587              rotCount++;
588          myView->Rotation(point.x,point.y);
589           }
590     }else //if ( nFlags & MK_RBUTTON)
591     //   ============================  NO BUTTON =======================
592     {  // No buttons 
593           myXmax = point.x; myYmax = point.y;   
594           if (nFlags & MK_SHIFT)
595                 GetDocument()->ShiftMoveEvent(point.x,point.y,myView);
596           else
597                 GetDocument()->MoveEvent(point.x,point.y,myView);
598    }
599 }
600
601 void CAnimationView3D::OnUpdateBUTTONHlrOff(CCmdUI* pCmdUI) 
602 {
603     pCmdUI->SetCheck (myDegenerateModeIsOn);
604         pCmdUI->Enable   (!myDegenerateModeIsOn);       
605 }
606
607 void CAnimationView3D::OnUpdateBUTTONHlrOn(CCmdUI* pCmdUI) 
608 {
609     pCmdUI->SetCheck (!myDegenerateModeIsOn);
610         pCmdUI->Enable   (myDegenerateModeIsOn);        
611 }
612
613 void CAnimationView3D::OnUpdateBUTTONPanGlo(CCmdUI* pCmdUI) 
614 {
615     pCmdUI->SetCheck (myCurrentMode == CurrentAction3d_GlobalPanning);
616         pCmdUI->Enable   (myCurrentMode != CurrentAction3d_GlobalPanning);      
617         
618 }
619
620 void CAnimationView3D::OnUpdateBUTTONPan(CCmdUI* pCmdUI) 
621 {
622     pCmdUI->SetCheck (myCurrentMode == CurrentAction3d_DynamicPanning);
623         pCmdUI->Enable   (myCurrentMode != CurrentAction3d_DynamicPanning );    
624 }
625
626 void CAnimationView3D::OnUpdateBUTTONZoomProg(CCmdUI* pCmdUI) 
627 {
628     pCmdUI->SetCheck (myCurrentMode == CurrentAction3d_DynamicZooming );
629         pCmdUI->Enable   (myCurrentMode != CurrentAction3d_DynamicZooming);     
630 }
631
632 void CAnimationView3D::OnUpdateBUTTONZoomWin(CCmdUI* pCmdUI) 
633 {
634     pCmdUI->SetCheck (myCurrentMode == CurrentAction3d_WindowZooming);
635         pCmdUI->Enable   (myCurrentMode != CurrentAction3d_WindowZooming);      
636 }
637
638 void CAnimationView3D::OnUpdateBUTTONRot(CCmdUI* pCmdUI) 
639 {
640     pCmdUI->SetCheck (myCurrentMode == CurrentAction3d_DynamicRotation);
641         pCmdUI->Enable   (myCurrentMode != CurrentAction3d_DynamicRotation);    
642 }
643
644 void CAnimationView3D::OnUpdateBUTTONFly(CCmdUI* pCmdUI) 
645 {
646         pCmdUI->Enable(GetDocument()->m_bIsGridLoaded);
647     pCmdUI->SetCheck (myCurrentMode == CurrentAction3d_Fly);
648 }
649
650 void CAnimationView3D::OnUpdateBUTTONTurn(CCmdUI* pCmdUI) 
651 {
652         pCmdUI->Enable(GetDocument()->m_bIsGridLoaded);
653     pCmdUI->SetCheck (myCurrentMode == CurrentAction3d_Turn);
654 }
655
656 void CAnimationView3D::OnChangeBackground() 
657 {
658         Standard_Real R1;
659         Standard_Real G1;
660         Standard_Real B1;
661     myView->BackgroundColor(Quantity_TOC_RGB,R1,G1,B1);
662         COLORREF m_clr ;
663         m_clr = RGB(R1*255,G1*255,B1*255);
664
665         CColorDialog dlgColor(m_clr);
666         if (dlgColor.DoModal() == IDOK)
667         {
668                 m_clr = dlgColor.GetColor();
669                 R1 = GetRValue(m_clr)/255.;
670                 G1 = GetGValue(m_clr)/255.;
671                 B1 = GetBValue(m_clr)/255.;
672         myView->SetBackgroundColor(Quantity_TOC_RGB,R1,G1,B1);
673         }
674     myView->Redraw();
675 }
676
677 //==========================================================================================
678 //==========================================================================================
679 //==========================================================================================
680
681 //-----------------------------------------------------------------------------------------
682 //
683 //-----------------------------------------------------------------------------------------
684 void CAnimationView3D::DrawRectangle(const Standard_Integer  MinX    ,
685                                                             const Standard_Integer  MinY    ,
686                                         const Standard_Integer  MaxX ,
687                                                             const Standard_Integer  MaxY ,
688                                                             const Standard_Boolean  Draw , 
689                                         const LineStyle aLineStyle)
690 {
691     static int m_DrawMode;
692     if  (!m_Pen && aLineStyle ==Solid )
693         {m_Pen = new CPen(PS_SOLID, 1, RGB(0,0,0)); m_DrawMode = R2_MERGEPENNOT;}
694     else if (!m_Pen && aLineStyle ==Dot )
695         {m_Pen = new CPen(PS_DOT, 1, RGB(0,0,0));   m_DrawMode = R2_XORPEN;}
696     else if (!m_Pen && aLineStyle == ShortDash)
697         {m_Pen = new CPen(PS_DASH, 1, RGB(255,0,0));    m_DrawMode = R2_XORPEN;}
698     else if (!m_Pen && aLineStyle == LongDash)
699         {m_Pen = new CPen(PS_DASH, 1, RGB(0,0,0));      m_DrawMode = R2_NOTXORPEN;}
700     else if (aLineStyle == Default) 
701         { m_Pen = NULL; m_DrawMode = R2_MERGEPENNOT;}
702
703     CPen* aOldPen;
704     CClientDC clientDC(this);
705     if (m_Pen) aOldPen = clientDC.SelectObject(m_Pen);
706     clientDC.SetROP2(m_DrawMode);
707
708     static              Standard_Integer StoredMinX, StoredMaxX, StoredMinY, StoredMaxY;
709     static              Standard_Boolean m_IsVisible;
710
711     if ( m_IsVisible && !Draw) // move or up  : erase at the old position 
712     {
713      clientDC.MoveTo(StoredMinX,StoredMinY); clientDC.LineTo(StoredMinX,StoredMaxY); 
714      clientDC.LineTo(StoredMaxX,StoredMaxY); 
715          clientDC.LineTo(StoredMaxX,StoredMinY); clientDC.LineTo(StoredMinX,StoredMinY);
716      m_IsVisible = false;
717     }
718
719     StoredMinX = min ( MinX, MaxX );
720     StoredMinY = min ( MinY, MaxY );
721     StoredMaxX = max ( MinX, MaxX );
722     StoredMaxY = max ( MinY, MaxY);
723
724     if (Draw) // move : draw
725     {
726      clientDC.MoveTo(StoredMinX,StoredMinY); clientDC.LineTo(StoredMinX,StoredMaxY); 
727      clientDC.LineTo(StoredMaxX,StoredMaxY); 
728          clientDC.LineTo(StoredMaxX,StoredMinY); clientDC.LineTo(StoredMinX,StoredMinY);
729      m_IsVisible = true;
730    }
731
732    if (m_Pen) 
733        clientDC.SelectObject(aOldPen);
734 }
735 void CAnimationView3D::OnStop() 
736 {
737         KillTimer(GetDocument()->myCount);            
738 }
739
740 void CAnimationView3D::OnRestart() 
741 {
742         KillTimer(GetDocument()->myCount);            
743         SetTimer(GetDocument()->myCount, 1 , NULL); 
744 }
745
746 /*
747 void CAnimationView3D::OnTimer(UINT nIDEvent) 
748 {
749         // TODO: Add your message handler code here and/or call default
750         GetDocument()->OnMyTimer();
751         CView::OnTimer(nIDEvent);
752 }
753 */
754
755
756 /*********************************************************************************
757 **************  W A L K  T H R O U G H  ******************************************
758 /********************************************************************************/
759
760 void CAnimationView3D::OnTimer(UINT_PTR nIDEvent) 
761 {
762         if ( !GetDocument()->m_bIsGridLoaded )
763         {
764                 // TODO: Add your message handler code here and/or call default
765                 GetDocument()->OnMyTimer();
766                 CView::OnTimer(nIDEvent);
767         }
768         else
769         {
770                 CView::OnTimer(nIDEvent);
771                 if ( nIDEvent == 1 ) {
772                   myView->SetImmediateUpdate ( Standard_False ) ;
773                   if ( myCurrentMode == CurrentAction3d_Fly ) {
774
775                          Fly  ( m_curx , m_cury ) ;
776                          if ( m_bShift )
777                            Roll ( m_curx , m_cury ) ;
778                          else
779                            Turn ( m_curx , m_cury ) ;
780
781                         myView->SetAt  ( m_Atx  , m_Aty  , m_Atz  ) ;
782                         myView->SetEye ( m_Eyex , m_Eyey , m_Eyez ) ;
783
784                   }
785                   else if ( myCurrentMode == CurrentAction3d_Turn ) {
786                            Twist ( m_curx , m_cury ) ;
787                   }
788                   else
789                           KillTimer (1) ;
790
791
792                   myView->SetImmediateUpdate ( Standard_True ) ;
793
794                   myView->Update ();
795                 }
796
797                 ReloadData () ;
798         }
799 }
800
801 void CAnimationView3D::OnSensitivity() 
802 {
803         CSensitivity dial ;
804
805         dial.m_SensFly   = m_FlySens  ;
806         dial.m_SensTurn  = m_TurnSens ;
807         if ( dial.DoModal () ) {
808                 m_FlySens  = dial.m_SensFly   ;
809         m_TurnSens = dial.m_SensTurn  ;
810         }
811 }
812
813 void CAnimationView3D::Fly (int x , int y)
814 {
815         double v [3] ;
816         double l ;
817         double sens ;
818         int    i     ;
819
820         sens = (double) myYmin - (double) y ;
821         sens /= (double) m_cy ;
822         sens *= m_FlySens ;
823
824         v [0] = m_Atx - m_Eyex ;
825         v [1] = m_Aty - m_Eyey ;
826         v [2] = m_Atz - m_Eyez ;
827         l = sqrt ( v[0]*v[0] + v[1]*v[1] + v[2]*v[2] ) ;
828         if ( l > 1.e-3 ) {
829                 for ( i=0 ; i<3 ; i++ )
830           v [i] = v [i] / l * sens ;
831
832                 m_Atx += v [0] ;
833                 m_Aty += v [1] ;
834                 m_Atz += v [2] ;
835
836                 m_Eyex += v [0] ;
837                 m_Eyey += v [1] ;
838                 m_Eyez += v [2] ;
839
840         }
841 }
842
843 /* Rotation */
844
845 void CAnimationView3D::Turn (int x , int y)
846 {
847         gp_Vec z (0.,0.,1.) ;
848
849         double v [3] ;
850         double sens ;
851         double aX , aY , aZ ;
852
853         sens = (double) x - (double) myXmin ;
854         sens /= (double) m_cx ;
855         sens *= m_TurnSens ;
856
857         v [0] = m_Atx - m_Eyex ;
858         v [1] = m_Aty - m_Eyey ;
859         v [2] = m_Atz - m_Eyez ;
860
861         gp_Pnt eye ( m_Eyex , m_Eyey , m_Eyez ) ;
862
863         gp_Vec reg (v[0],v[1],v[2] );
864
865         gp_Vec vert = reg ^ z ;
866         gp_Vec haut = vert ^ reg ;
867
868         gp_Dir dh (haut) ;
869         gp_Ax1 rot (eye,dh);
870
871         reg.Rotate (rot,sens) ;
872
873         reg.Coord ( aX , aY , aZ ) ;
874
875         m_Atx = m_Eyex + aX ;
876         m_Aty = m_Eyey + aY ;
877         m_Atz = m_Eyez + aZ ;
878 }
879
880 void CAnimationView3D::Roll (int x , int y)
881 {
882         gp_Vec z (0.,0.,1.) ;
883
884         double v [3] ;
885         double sens ;
886         double aX , aY , aZ ;
887
888         sens = (double) x - (double) myXmin ;
889         sens /= (double) m_cx ;
890         sens *= m_TurnSens ;
891
892         v [0] = m_Atx - m_Eyex ;
893         v [1] = m_Aty - m_Eyey ;
894         v [2] = m_Atz - m_Eyez ;
895
896         gp_Pnt eye ( m_Eyex , m_Eyey , m_Eyez ) ;
897
898         gp_Vec reg (v[0],v[1],v[2] );
899
900         gp_Vec vert = reg ^ z ;
901
902         gp_Dir dh (vert) ;
903         gp_Ax1 rot (eye,dh);
904
905         reg.Rotate (rot,sens) ;
906
907         reg.Coord ( aX , aY , aZ ) ;
908
909         m_Atx = m_Eyex + aX ;
910         m_Aty = m_Eyey + aY ;
911         m_Atz = m_Eyez + aZ ;
912 }
913
914 void CAnimationView3D::Twist (int x , int y)
915 {
916         double sens ;
917         double a ;
918         
919         a = myView->Twist () ;
920
921         sens = (double) x - (double) myXmin ;
922         sens /= (double) m_cx ;
923         sens *= m_TurnSens ;
924
925         a += sens ;
926
927         myView->SetTwist (a) ;
928 }
929
930 void CAnimationView3D::SetFocal(double dFocus,double dAngle)
931 {
932         double v [3] ;
933         double l ;
934         int    i     ;
935
936         v [0] = m_Atx - m_Eyex ;
937         v [1] = m_Aty - m_Eyey ;
938         v [2] = m_Atz - m_Eyez ;
939
940         l = sqrt ( v[0]*v[0] + v[1]*v[1] + v[2]*v[2] ) ;
941         if ( l > 1.e-3 ) {
942                 for ( i=0 ; i<3 ; i++ )
943           v [i] = v [i] / l * dFocus ;
944
945                 m_Focus = dFocus ;
946
947                 m_Atx = v [0] + m_Eyex ;
948                 m_Aty = v [1] + m_Eyey ;
949                 m_Atz = v [2] + m_Eyez ;
950
951         myView->SetImmediateUpdate ( Standard_False ) ;
952                 myView->SetAt  ( m_Atx  , m_Aty  , m_Atz  ) ;
953                 m_dAngle = dAngle ;
954                 dAngle = dAngle * M_PI / 180. ;
955         myPView->SetAngle ( dAngle ) ;
956                 dAngle = myPView->Angle () ;
957
958         myView->SetImmediateUpdate ( Standard_True ) ;
959         myView->Update ();
960         }
961 }
962
963 void CAnimationView3D::ReloadData()
964 {
965         char szMsg [128] ;
966         double dTwist ;
967
968         myView->At  ( m_Atx  , m_Aty  , m_Atz  ) ;
969         myView->Eye ( m_Eyex , m_Eyey , m_Eyez ) ;
970
971         dTwist = myView->Twist () ;
972         dTwist = dTwist * 180. / M_PI ;
973
974         sprintf_s ( szMsg , "%lf" , m_Atx ) ;
975         (m_Tune.GetDlgItem ( IDC_XAT ))->SetWindowText ( szMsg ) ;
976         sprintf_s ( szMsg , "%lf" , m_Aty ) ;
977         (m_Tune.GetDlgItem ( IDC_YAT ))->SetWindowText ( szMsg ) ;
978         sprintf_s ( szMsg , "%lf" , m_Atz ) ;
979         (m_Tune.GetDlgItem ( IDC_ZAT ))->SetWindowText ( szMsg ) ;
980
981         sprintf_s ( szMsg , "%lf" , m_Eyex ) ;
982         (m_Tune.GetDlgItem ( IDC_XEYE ))->SetWindowText ( szMsg ) ;
983         sprintf_s ( szMsg , "%lf" , m_Eyey ) ;
984         (m_Tune.GetDlgItem ( IDC_YEYE ))->SetWindowText ( szMsg ) ;
985         sprintf_s ( szMsg , "%lf" , m_Eyez ) ;
986         (m_Tune.GetDlgItem ( IDC_ZEYE ))->SetWindowText ( szMsg ) ;
987
988         sprintf_s ( szMsg , "%lf" , dTwist ) ;
989         (m_Tune.GetDlgItem ( IDC_TWIST ))->SetWindowText ( szMsg ) ;
990
991         double dx,dy,dz ;
992         dx = m_Atx - m_Eyex ;
993         dy = m_Aty - m_Eyey ;
994         dz = m_Atz - m_Eyez ;
995
996     m_Focus = sqrt ( dx * dx + dy * dy + dz * dz ) ;
997         m_dAngle = myPView->Angle () ;
998         m_dAngle = m_dAngle * 180. / M_PI ;
999
1000         m_Tune.m_dAngle = m_dAngle ;
1001         m_Tune.m_dFocus = m_Focus  ;
1002         m_Tune.UpdateData ( FALSE ) ;
1003 }
1004
1005 void CAnimationView3D::SetDimensions()
1006 {
1007
1008   CAnimationDoc* pDoc = GetDocument();
1009
1010   myView->SetImmediateUpdate ( Standard_False ) ;
1011
1012   m_Atx  = ( pDoc->m_Xmin + pDoc->m_Xmax ) / 2. ;
1013   m_Aty  = ( pDoc->m_Ymin + pDoc->m_Ymax ) / 2. ;
1014   m_Atz  = ( pDoc->m_Zmin + pDoc->m_Zmax ) / 2. ;
1015   m_Eyex = pDoc->m_Xmax ;
1016   m_Eyey = pDoc->m_Ymax ;
1017   m_Eyez = pDoc->m_Zmax ;
1018
1019   myView->SetAt    ( m_Atx  , m_Aty  , m_Atz  ) ;
1020   myView->SetEye   ( m_Eyex , m_Eyey , m_Eyez ) ;
1021   myView->SetTwist (0.) ;
1022
1023   myView->SetImmediateUpdate ( Standard_False ) ;
1024   myView->FitAll();
1025   myView->SetImmediateUpdate ( Standard_False ) ;
1026   myView->ZFitAll();
1027
1028   myView->SetImmediateUpdate ( Standard_True ) ;
1029
1030   ReloadData () ;
1031   myView->Update ();
1032 }
1033
1034 void CAnimationView3D::OnViewDisplaystatus() 
1035 {
1036         // TODO: Add your command handler code here
1037
1038         if ( m_Tune.IsWindowVisible () ) {
1039
1040         }
1041         else {
1042                 m_Tune.ShowWindow ( SW_SHOWNORMAL ) ;
1043         }
1044 }
1045
1046 void CAnimationView3D::OnUpdateViewDisplaystatus(CCmdUI* pCmdUI) 
1047 {
1048         // TODO: Add your command update UI handler code here
1049
1050         if ( m_Tune.IsWindowVisible () ) {
1051                 pCmdUI->SetCheck ( 1 ) ;
1052         }
1053         else {
1054                 pCmdUI->SetCheck ( 0 ) ;
1055         }
1056 }