0031521: Samples - update MFC ImportExport sample to use AIS_ViewController
[occt.git] / samples / mfc / standard / Common / OCC_2dView.cpp
1 // OCC_2dView.cpp: implementation of the OCC_2dView class.
2 //
3 //////////////////////////////////////////////////////////////////////
4 #include "stdafx.h"
5
6 #include "OCC_2dView.h"
7
8 #include "OCC_App.h"
9 #include "OCC_2dDoc.h"
10 #include "resource2d\RectangularGrid.h"
11 #include "resource2d\CircularGrid.h"
12
13 #include "Quantity_Color.hxx"
14 #include "Quantity_NameOfColor.hxx"
15
16 /////////////////////////////////////////////////////////////////////////////
17 // OCC_2dView
18
19 IMPLEMENT_DYNCREATE(OCC_2dView, OCC_BaseView)
20
21 BEGIN_MESSAGE_MAP(OCC_2dView, OCC_BaseView)
22         //{{AFX_MSG_MAP(OCC_2dView)
23                 // NOTE - the ClassWizard will add and remove mapping macros here.
24                 //    DO NOT EDIT what you see in these blocks of generated code!
25   ON_COMMAND(ID_FILE_EXPORT_IMAGE, OnFileExportImage)
26   ON_COMMAND(ID_BUTTON2DGridRectLines, OnBUTTONGridRectLines)
27   ON_COMMAND(ID_BUTTON2DGridRectPoints, OnBUTTONGridRectPoints)
28   ON_COMMAND(ID_BUTTON2DGridCircLines, OnBUTTONGridCircLines)
29   ON_COMMAND(ID_BUTTON2DGridCircPoints, OnBUTTONGridCircPoints)
30   ON_COMMAND(ID_BUTTON2DGridValues, OnBUTTONGridValues)
31   ON_UPDATE_COMMAND_UI(ID_BUTTON2DGridValues, OnUpdateBUTTONGridValues)
32   ON_COMMAND(ID_BUTTON2DGridCancel, OnBUTTONGridCancel)
33   ON_UPDATE_COMMAND_UI(ID_BUTTON2DGridCancel, OnUpdateBUTTONGridCancel)
34   ON_WM_SIZE()
35   ON_COMMAND(ID_BUTTON2DFitAll, OnBUTTONFitAll)
36   ON_COMMAND(ID_BUTTON2DGlobPanning, OnBUTTONGlobPanning)
37   ON_COMMAND(ID_BUTTON2DPanning, OnBUTTONPanning)
38   ON_COMMAND(ID_BUTTON2DZoomProg, OnBUTTONZoomProg)
39   ON_COMMAND(ID_BUTTON2DZoomWin, OnBUTTONZoomWin)
40   ON_UPDATE_COMMAND_UI(ID_BUTTON2DGlobPanning, OnUpdateBUTTON2DGlobPanning)
41   ON_UPDATE_COMMAND_UI(ID_BUTTON2DPanning, OnUpdateBUTTON2DPanning)
42   ON_UPDATE_COMMAND_UI(ID_BUTTON2DZoomProg, OnUpdateBUTTON2DZoomProg)
43   ON_UPDATE_COMMAND_UI(ID_BUTTON2DZoomWin, OnUpdateBUTTON2DZoomWin)
44   ON_COMMAND(ID_Modify_ChangeBackground ,OnChangeBackground)
45         //}}AFX_MSG_MAP
46 END_MESSAGE_MAP()
47
48 /////////////////////////////////////////////////////////////////////////////
49 // OCC_2dView construction/destruction
50
51 OCC_2dView::OCC_2dView()
52 {
53   myToAllowRotation = false;
54   myDefaultGestures.Bind (Aspect_VKeyMouse_LeftButton, AIS_MouseGesture_SelectRectangle);
55   myMouseGestureMap = myDefaultGestures;
56 }
57
58 OCC_2dView::~OCC_2dView()
59 {
60   //
61 }
62
63 /////////////////////////////////////////////////////////////////////////////
64 // OCC_2dView drawing
65
66 void OCC_2dView::OnInitialUpdate()
67 {
68   OCC_BaseView::OnInitialUpdate();
69
70   // initialize the grids dialogs
71   TheRectangularGridDialog.Create(CRectangularGrid::IDD, NULL);
72   TheCircularGridDialog.Create(CCircularGrid::IDD, NULL);
73   TheRectangularGridDialog.SetViewer (myView->Viewer());
74   TheCircularGridDialog.SetViewer (myView->Viewer());
75 }
76
77 void OCC_2dView::OnFileExportImage()
78 {
79   GetDocument()->ExportView (myView);
80 }
81
82 /////////////////////////////////////////////////////////////////////////////
83 // OCC_2dView diagnostics
84
85 #ifdef _DEBUG
86 void OCC_2dView::AssertValid() const
87 {
88   CView::AssertValid();
89 }
90
91 void OCC_2dView::Dump(CDumpContext& dc) const
92 {
93   CView::Dump(dc);
94 }
95
96 OCC_2dDoc* OCC_2dView::GetDocument() // non-debug version is inline
97 {
98   //ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(OCC_2dDoc)));
99   return (OCC_2dDoc*)m_pDocument;
100 }
101 #endif //_DEBUG
102 void OCC_2dView::OnBUTTONGridRectLines() 
103 {
104   Handle(V3d_Viewer) aViewer = myView->Viewer();
105   Handle(Graphic3d_AspectMarker3d) aGridAspect = new Graphic3d_AspectMarker3d(Aspect_TOM_RING1,Quantity_NOC_WHITE,2);
106   aViewer->SetGridEcho(aGridAspect);
107   Standard_Integer aWidth=0, aHeight=0, anOffset=0;
108   myView->Window()->Size(aWidth,aHeight);
109   aViewer->SetRectangularGridGraphicValues(aWidth,aHeight,anOffset);
110   aViewer->ActivateGrid(Aspect_GT_Rectangular, Aspect_GDM_Lines);
111   FitAll();
112
113   if (TheCircularGridDialog.IsWindowVisible())
114   {
115     TheCircularGridDialog.ShowWindow(SW_HIDE);
116     TheRectangularGridDialog.UpdateValues();
117     TheRectangularGridDialog.ShowWindow(SW_SHOW);
118   }
119 }
120
121 void OCC_2dView::OnBUTTONGridRectPoints() 
122 {
123   Handle(V3d_Viewer) aViewer = myView->Viewer();
124   Handle(Graphic3d_AspectMarker3d) aGridAspect = new Graphic3d_AspectMarker3d(Aspect_TOM_RING1,Quantity_NOC_WHITE,2);
125   aViewer->SetGridEcho(aGridAspect);
126   Standard_Integer aWidth=0, aHeight=0, anOffset=0;
127   myView->Window()->Size(aWidth,aHeight);
128   aViewer->SetRectangularGridGraphicValues(aWidth,aHeight,anOffset);
129   aViewer->ActivateGrid(Aspect_GT_Rectangular, Aspect_GDM_Points);
130   FitAll();
131
132   if (TheCircularGridDialog.IsWindowVisible())
133   {
134     TheCircularGridDialog.ShowWindow(SW_HIDE);
135     TheRectangularGridDialog.UpdateValues();
136     TheRectangularGridDialog.ShowWindow(SW_SHOW);
137   }
138 }
139
140 void OCC_2dView::OnBUTTONGridCircLines() 
141 {
142   Handle(V3d_Viewer) aViewer = myView->Viewer();
143   Handle(Graphic3d_AspectMarker3d) aGridAspect = new Graphic3d_AspectMarker3d(Aspect_TOM_RING1,Quantity_NOC_WHITE,2);
144   aViewer->SetGridEcho(aGridAspect);
145   Standard_Integer aWidth=0, aHeight=0, anOffset=0;
146   myView->Window()->Size(aWidth,aHeight);
147   aViewer->SetCircularGridGraphicValues(aWidth>aHeight?aWidth:aHeight,anOffset);
148   aViewer->ActivateGrid(Aspect_GT_Circular, Aspect_GDM_Lines);
149   FitAll();
150  
151
152   if (TheRectangularGridDialog.IsWindowVisible())
153   {
154     TheRectangularGridDialog.ShowWindow(SW_HIDE);
155     TheCircularGridDialog.UpdateValues();
156     TheCircularGridDialog.ShowWindow(SW_SHOW);
157   }
158 }
159
160 void OCC_2dView::OnBUTTONGridCircPoints() 
161 {
162   Handle(V3d_Viewer) aViewer = myView->Viewer();
163   Handle(Graphic3d_AspectMarker3d) aGridAspect = new Graphic3d_AspectMarker3d(Aspect_TOM_RING1,Quantity_NOC_WHITE,2);
164   aViewer->SetGridEcho(aGridAspect);
165   Standard_Integer aWidth=0, aHeight=0, anOffset=0;
166   myView->Window()->Size(aWidth,aHeight);
167   aViewer->SetCircularGridGraphicValues(aWidth>aHeight?aWidth:aHeight,anOffset);
168   aViewer->ActivateGrid(Aspect_GT_Circular, Aspect_GDM_Points);
169   FitAll();
170   if (TheRectangularGridDialog.IsWindowVisible())
171   {
172     TheRectangularGridDialog.ShowWindow(SW_HIDE);
173     TheCircularGridDialog.UpdateValues();
174     TheCircularGridDialog.ShowWindow(SW_SHOW);
175   }
176 }
177
178 void OCC_2dView::OnBUTTONGridValues() 
179 {
180   Handle(V3d_Viewer) aViewer = myView->Viewer();
181   Aspect_GridType  TheGridtype = aViewer->GridType();
182
183   switch( TheGridtype ) 
184   {
185   case  Aspect_GT_Rectangular:
186     TheRectangularGridDialog.UpdateValues();
187     TheRectangularGridDialog.ShowWindow(SW_SHOW);
188     break;
189   case  Aspect_GT_Circular:
190     TheCircularGridDialog.UpdateValues();
191     TheCircularGridDialog.ShowWindow(SW_SHOW);
192     break;
193   default :
194     throw Standard_Failure("invalid Aspect_GridType");
195   }
196 }
197 void OCC_2dView::OnUpdateBUTTONGridValues(CCmdUI* pCmdUI) 
198 {
199   Handle(V3d_Viewer) aViewer = myView->Viewer();
200   pCmdUI-> Enable( aViewer->IsActive() );
201 }
202
203 void OCC_2dView::OnBUTTONGridCancel() 
204 {
205   Handle(V3d_Viewer) aViewer = myView->Viewer();
206   aViewer->DeactivateGrid();
207   TheRectangularGridDialog.ShowWindow(SW_HIDE);
208   TheCircularGridDialog.ShowWindow(SW_HIDE);
209   aViewer->Update();
210 }
211 void OCC_2dView::OnUpdateBUTTONGridCancel(CCmdUI* pCmdUI) 
212 {
213   Handle(V3d_Viewer) aViewer = myView->Viewer();
214   pCmdUI-> Enable( aViewer->IsActive() );       
215 }
216
217 void OCC_2dView::OnSize(UINT nType, int cx, int cy)
218 {
219   OCC_BaseView::OnSize (nType, cx, cy);
220   // Take care : This fonction is call before OnInitialUpdate
221   if (!myView.IsNull())
222     myView->MustBeResized(); 
223 }
224
225 void OCC_2dView::OnBUTTONFitAll() 
226 {
227   myView->FitAll();
228 }
229
230 void OCC_2dView::OnBUTTONGlobPanning() 
231 {
232   //save the current zoom value
233   myCurZoom = myView->Scale();  
234
235   // Do a Global Zoom 
236   myView->FitAll();
237
238   // Set the mode
239   setCurrentAction (CurAction3d_GlobalPanning);
240 }
241 void OCC_2dView::OnBUTTONPanning() 
242 {
243   setCurrentAction (CurAction3d_DynamicPanning);
244 }
245 void OCC_2dView::OnBUTTONZoomProg() 
246 {
247   setCurrentAction (CurAction3d_DynamicZooming);
248 }
249 void OCC_2dView::OnBUTTONZoomWin() 
250 {
251   setCurrentAction (CurAction3d_WindowZooming);
252 }
253 void OCC_2dView::OnChangeBackground() 
254 {
255   Standard_Real R1, G1, B1;
256   Handle(Aspect_Window) aWindow = myView->Window();
257   Aspect_Background ABack = aWindow->Background();
258   Quantity_Color aColor = ABack.Color();
259   aColor.Values(R1,G1,B1,Quantity_TOC_RGB);
260   COLORREF m_clr ;
261   m_clr = RGB(R1*255,G1*255,B1*255);
262
263   CColorDialog dlgColor(m_clr);
264   if (dlgColor.DoModal() == IDOK)
265   {
266     m_clr = dlgColor.GetColor();
267     R1 = GetRValue(m_clr)/255.;
268     G1 = GetGValue(m_clr)/255.;
269     B1 = GetBValue(m_clr)/255.;
270     aColor.SetValues(R1,G1,B1,Quantity_TOC_RGB);
271     myView->SetBackgroundColor(aColor);
272     myView->Update();
273   }     
274 }
275
276
277 void OCC_2dView::OnUpdateBUTTON2DGlobPanning(CCmdUI* pCmdUI) 
278 {
279   pCmdUI->SetCheck (getCurrentAction() == CurAction3d_GlobalPanning);
280   pCmdUI->Enable   (getCurrentAction() != CurAction3d_GlobalPanning);
281 }
282
283 void OCC_2dView::OnUpdateBUTTON2DPanning(CCmdUI* pCmdUI) 
284 {
285   pCmdUI->SetCheck (getCurrentAction() == CurAction3d_DynamicPanning);
286   pCmdUI->Enable   (getCurrentAction() != CurAction3d_DynamicPanning);
287 }
288
289 void OCC_2dView::OnUpdateBUTTON2DZoomProg(CCmdUI* pCmdUI) 
290 {
291   pCmdUI->SetCheck (getCurrentAction() == CurAction3d_DynamicZooming);
292   pCmdUI->Enable   (getCurrentAction() != CurAction3d_DynamicZooming);
293 }
294
295 void OCC_2dView::OnUpdateBUTTON2DZoomWin(CCmdUI* pCmdUI) 
296 {
297   pCmdUI->SetCheck (getCurrentAction() == CurAction3d_WindowZooming);
298   pCmdUI->Enable   (getCurrentAction() != CurAction3d_WindowZooming);           
299 }