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