0031521: Samples - update MFC ImportExport sample to use AIS_ViewController
[occt.git] / samples / mfc / standard / Common / OCC_2dView.cpp
CommitLineData
7fd59977 1// OCC_2dView.cpp: implementation of the OCC_2dView class.
2//
3//////////////////////////////////////////////////////////////////////
4#include "stdafx.h"
5
6#include "OCC_2dView.h"
7
5c1f974e 8#include "OCC_App.h"
7fd59977 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
7fd59977 16/////////////////////////////////////////////////////////////////////////////
17// OCC_2dView
18
7ff18fb9 19IMPLEMENT_DYNCREATE(OCC_2dView, OCC_BaseView)
7fd59977 20
7ff18fb9 21BEGIN_MESSAGE_MAP(OCC_2dView, OCC_BaseView)
7fd59977 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!
7ff18fb9 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)
7fd59977 45 //}}AFX_MSG_MAP
46END_MESSAGE_MAP()
47
48/////////////////////////////////////////////////////////////////////////////
49// OCC_2dView construction/destruction
50
51OCC_2dView::OCC_2dView()
52{
7ff18fb9 53 myToAllowRotation = false;
54 myDefaultGestures.Bind (Aspect_VKeyMouse_LeftButton, AIS_MouseGesture_SelectRectangle);
55 myMouseGestureMap = myDefaultGestures;
7fd59977 56}
57
58OCC_2dView::~OCC_2dView()
59{
7ff18fb9 60 //
7fd59977 61}
62
63/////////////////////////////////////////////////////////////////////////////
64// OCC_2dView drawing
65
7fd59977 66void OCC_2dView::OnInitialUpdate()
67{
7ff18fb9 68 OCC_BaseView::OnInitialUpdate();
7fd59977 69
5c1f974e 70 // initialize the grids dialogs
71 TheRectangularGridDialog.Create(CRectangularGrid::IDD, NULL);
72 TheCircularGridDialog.Create(CCircularGrid::IDD, NULL);
7ff18fb9 73 TheRectangularGridDialog.SetViewer (myView->Viewer());
74 TheCircularGridDialog.SetViewer (myView->Viewer());
7fd59977 75}
76
77void OCC_2dView::OnFileExportImage()
78{
7ff18fb9 79 GetDocument()->ExportView (myView);
7fd59977 80}
81
82/////////////////////////////////////////////////////////////////////////////
83// OCC_2dView diagnostics
84
85#ifdef _DEBUG
86void OCC_2dView::AssertValid() const
87{
5c1f974e 88 CView::AssertValid();
7fd59977 89}
90
91void OCC_2dView::Dump(CDumpContext& dc) const
92{
5c1f974e 93 CView::Dump(dc);
7fd59977 94}
95
5c1f974e 96OCC_2dDoc* OCC_2dView::GetDocument() // non-debug version is inline
7fd59977 97{
5c1f974e 98 //ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(OCC_2dDoc)));
99 return (OCC_2dDoc*)m_pDocument;
7fd59977 100}
101#endif //_DEBUG
7fd59977 102void OCC_2dView::OnBUTTONGridRectLines()
103{
7ff18fb9 104 Handle(V3d_Viewer) aViewer = myView->Viewer();
a577aaab 105 Handle(Graphic3d_AspectMarker3d) aGridAspect = new Graphic3d_AspectMarker3d(Aspect_TOM_RING1,Quantity_NOC_WHITE,2);
5c1f974e 106 aViewer->SetGridEcho(aGridAspect);
107 Standard_Integer aWidth=0, aHeight=0, anOffset=0;
7ff18fb9 108 myView->Window()->Size(aWidth,aHeight);
5c1f974e 109 aViewer->SetRectangularGridGraphicValues(aWidth,aHeight,anOffset);
110 aViewer->ActivateGrid(Aspect_GT_Rectangular, Aspect_GDM_Lines);
111 FitAll();
b12e1c7b 112
7fd59977 113 if (TheCircularGridDialog.IsWindowVisible())
114 {
115 TheCircularGridDialog.ShowWindow(SW_HIDE);
116 TheRectangularGridDialog.UpdateValues();
117 TheRectangularGridDialog.ShowWindow(SW_SHOW);
118 }
119}
120
121void OCC_2dView::OnBUTTONGridRectPoints()
122{
7ff18fb9 123 Handle(V3d_Viewer) aViewer = myView->Viewer();
a577aaab 124 Handle(Graphic3d_AspectMarker3d) aGridAspect = new Graphic3d_AspectMarker3d(Aspect_TOM_RING1,Quantity_NOC_WHITE,2);
5c1f974e 125 aViewer->SetGridEcho(aGridAspect);
126 Standard_Integer aWidth=0, aHeight=0, anOffset=0;
7ff18fb9 127 myView->Window()->Size(aWidth,aHeight);
5c1f974e 128 aViewer->SetRectangularGridGraphicValues(aWidth,aHeight,anOffset);
129 aViewer->ActivateGrid(Aspect_GT_Rectangular, Aspect_GDM_Points);
130 FitAll();
131
7fd59977 132 if (TheCircularGridDialog.IsWindowVisible())
133 {
134 TheCircularGridDialog.ShowWindow(SW_HIDE);
135 TheRectangularGridDialog.UpdateValues();
136 TheRectangularGridDialog.ShowWindow(SW_SHOW);
137 }
138}
139
140void OCC_2dView::OnBUTTONGridCircLines()
141{
7ff18fb9 142 Handle(V3d_Viewer) aViewer = myView->Viewer();
a577aaab 143 Handle(Graphic3d_AspectMarker3d) aGridAspect = new Graphic3d_AspectMarker3d(Aspect_TOM_RING1,Quantity_NOC_WHITE,2);
5c1f974e 144 aViewer->SetGridEcho(aGridAspect);
145 Standard_Integer aWidth=0, aHeight=0, anOffset=0;
7ff18fb9 146 myView->Window()->Size(aWidth,aHeight);
5c1f974e 147 aViewer->SetCircularGridGraphicValues(aWidth>aHeight?aWidth:aHeight,anOffset);
148 aViewer->ActivateGrid(Aspect_GT_Circular, Aspect_GDM_Lines);
149 FitAll();
7fd59977 150
151
152 if (TheRectangularGridDialog.IsWindowVisible())
153 {
154 TheRectangularGridDialog.ShowWindow(SW_HIDE);
155 TheCircularGridDialog.UpdateValues();
156 TheCircularGridDialog.ShowWindow(SW_SHOW);
157 }
158}
159
160void OCC_2dView::OnBUTTONGridCircPoints()
161{
7ff18fb9 162 Handle(V3d_Viewer) aViewer = myView->Viewer();
a577aaab 163 Handle(Graphic3d_AspectMarker3d) aGridAspect = new Graphic3d_AspectMarker3d(Aspect_TOM_RING1,Quantity_NOC_WHITE,2);
5c1f974e 164 aViewer->SetGridEcho(aGridAspect);
165 Standard_Integer aWidth=0, aHeight=0, anOffset=0;
7ff18fb9 166 myView->Window()->Size(aWidth,aHeight);
5c1f974e 167 aViewer->SetCircularGridGraphicValues(aWidth>aHeight?aWidth:aHeight,anOffset);
168 aViewer->ActivateGrid(Aspect_GT_Circular, Aspect_GDM_Points);
169 FitAll();
7fd59977 170 if (TheRectangularGridDialog.IsWindowVisible())
171 {
172 TheRectangularGridDialog.ShowWindow(SW_HIDE);
173 TheCircularGridDialog.UpdateValues();
174 TheCircularGridDialog.ShowWindow(SW_SHOW);
175 }
176}
177
178void OCC_2dView::OnBUTTONGridValues()
179{
7ff18fb9 180 Handle(V3d_Viewer) aViewer = myView->Viewer();
7fd59977 181 Aspect_GridType TheGridtype = aViewer->GridType();
182
183 switch( TheGridtype )
184 {
5c1f974e 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 :
9775fa61 194 throw Standard_Failure("invalid Aspect_GridType");
7fd59977 195 }
196}
197void OCC_2dView::OnUpdateBUTTONGridValues(CCmdUI* pCmdUI)
198{
7ff18fb9 199 Handle(V3d_Viewer) aViewer = myView->Viewer();
7fd59977 200 pCmdUI-> Enable( aViewer->IsActive() );
201}
202
203void OCC_2dView::OnBUTTONGridCancel()
5c1f974e 204{
7ff18fb9 205 Handle(V3d_Viewer) aViewer = myView->Viewer();
7fd59977 206 aViewer->DeactivateGrid();
207 TheRectangularGridDialog.ShowWindow(SW_HIDE);
208 TheCircularGridDialog.ShowWindow(SW_HIDE);
209 aViewer->Update();
210}
211void OCC_2dView::OnUpdateBUTTONGridCancel(CCmdUI* pCmdUI)
212{
7ff18fb9 213 Handle(V3d_Viewer) aViewer = myView->Viewer();
7fd59977 214 pCmdUI-> Enable( aViewer->IsActive() );
215}
216
d01ed5fd 217void OCC_2dView::OnSize(UINT nType, int cx, int cy)
7fd59977 218{
d01ed5fd 219 OCC_BaseView::OnSize (nType, cx, cy);
5c1f974e 220 // Take care : This fonction is call before OnInitialUpdate
7ff18fb9 221 if (!myView.IsNull())
222 myView->MustBeResized();
7fd59977 223}
224
225void OCC_2dView::OnBUTTONFitAll()
226{
7ff18fb9 227 myView->FitAll();
7fd59977 228}
229
230void OCC_2dView::OnBUTTONGlobPanning()
231{
232 //save the current zoom value
7ff18fb9 233 myCurZoom = myView->Scale();
7fd59977 234
235 // Do a Global Zoom
7ff18fb9 236 myView->FitAll();
7fd59977 237
238 // Set the mode
7ff18fb9 239 setCurrentAction (CurAction3d_GlobalPanning);
7fd59977 240}
241void OCC_2dView::OnBUTTONPanning()
242{
7ff18fb9 243 setCurrentAction (CurAction3d_DynamicPanning);
7fd59977 244}
245void OCC_2dView::OnBUTTONZoomProg()
246{
7ff18fb9 247 setCurrentAction (CurAction3d_DynamicZooming);
7fd59977 248}
249void OCC_2dView::OnBUTTONZoomWin()
250{
7ff18fb9 251 setCurrentAction (CurAction3d_WindowZooming);
7fd59977 252}
253void OCC_2dView::OnChangeBackground()
254{
5c1f974e 255 Standard_Real R1, G1, B1;
7ff18fb9 256 Handle(Aspect_Window) aWindow = myView->Window();
5c1f974e 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);
7ff18fb9 271 myView->SetBackgroundColor(aColor);
272 myView->Update();
5c1f974e 273 }
7fd59977 274}
275
276
277void OCC_2dView::OnUpdateBUTTON2DGlobPanning(CCmdUI* pCmdUI)
278{
7ff18fb9 279 pCmdUI->SetCheck (getCurrentAction() == CurAction3d_GlobalPanning);
280 pCmdUI->Enable (getCurrentAction() != CurAction3d_GlobalPanning);
7fd59977 281}
282
283void OCC_2dView::OnUpdateBUTTON2DPanning(CCmdUI* pCmdUI)
284{
7ff18fb9 285 pCmdUI->SetCheck (getCurrentAction() == CurAction3d_DynamicPanning);
286 pCmdUI->Enable (getCurrentAction() != CurAction3d_DynamicPanning);
7fd59977 287}
288
289void OCC_2dView::OnUpdateBUTTON2DZoomProg(CCmdUI* pCmdUI)
290{
7ff18fb9 291 pCmdUI->SetCheck (getCurrentAction() == CurAction3d_DynamicZooming);
292 pCmdUI->Enable (getCurrentAction() != CurAction3d_DynamicZooming);
7fd59977 293}
294
295void OCC_2dView::OnUpdateBUTTON2DZoomWin(CCmdUI* pCmdUI)
296{
7ff18fb9 297 pCmdUI->SetCheck (getCurrentAction() == CurAction3d_WindowZooming);
298 pCmdUI->Enable (getCurrentAction() != CurAction3d_WindowZooming);
7fd59977 299}