175cfcc9a52f256a46d5bac9379e0a9e57f50080
[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 #define ValZWMin 1
17
18 // the key for multi selection :
19 #define MULTISELECTIONKEY MK_SHIFT
20
21 // the key for shortcut ( use to activate dynamic rotation, panning )
22 #define CASCADESHORTCUTKEY MK_CONTROL
23
24 // define in witch case you want to display the popup
25 #define POPUPONBUTTONDOWN
26
27 /////////////////////////////////////////////////////////////////////////////
28 // OCC_2dView
29
30 IMPLEMENT_DYNCREATE(OCC_2dView, CView)
31
32 BEGIN_MESSAGE_MAP(OCC_2dView, CView)
33         //{{AFX_MSG_MAP(OCC_2dView)
34                 // NOTE - the ClassWizard will add and remove mapping macros here.
35                 //    DO NOT EDIT what you see in these blocks of generated code!
36         ON_COMMAND(ID_FILE_EXPORT_IMAGE, OnFileExportImage)
37     ON_COMMAND(ID_BUTTON2DGridRectLines, OnBUTTONGridRectLines)
38         ON_COMMAND(ID_BUTTON2DGridRectPoints, OnBUTTONGridRectPoints)
39         ON_COMMAND(ID_BUTTON2DGridCircLines, OnBUTTONGridCircLines)
40         ON_COMMAND(ID_BUTTON2DGridCircPoints, OnBUTTONGridCircPoints)
41         ON_COMMAND(ID_BUTTON2DGridValues, OnBUTTONGridValues)
42     ON_UPDATE_COMMAND_UI(ID_BUTTON2DGridValues, OnUpdateBUTTONGridValues)
43         ON_COMMAND(ID_BUTTON2DGridCancel, OnBUTTONGridCancel)
44         ON_UPDATE_COMMAND_UI(ID_BUTTON2DGridCancel, OnUpdateBUTTONGridCancel)
45     ON_WM_LBUTTONDOWN()
46         ON_WM_LBUTTONUP()
47         ON_WM_MBUTTONDOWN()
48         ON_WM_MBUTTONUP()
49         ON_WM_RBUTTONDOWN()
50         ON_WM_RBUTTONUP()
51         ON_WM_MOUSEMOVE()
52         ON_WM_SIZE()
53         ON_COMMAND(ID_BUTTON2DFitAll, OnBUTTONFitAll)
54         ON_COMMAND(ID_BUTTON2DGlobPanning, OnBUTTONGlobPanning)
55         ON_COMMAND(ID_BUTTON2DPanning, OnBUTTONPanning)
56         ON_COMMAND(ID_BUTTON2DZoomProg, OnBUTTONZoomProg)
57         ON_COMMAND(ID_BUTTON2DZoomWin, OnBUTTONZoomWin)
58         ON_UPDATE_COMMAND_UI(ID_BUTTON2DGlobPanning, OnUpdateBUTTON2DGlobPanning)
59         ON_UPDATE_COMMAND_UI(ID_BUTTON2DPanning, OnUpdateBUTTON2DPanning)
60         ON_UPDATE_COMMAND_UI(ID_BUTTON2DZoomProg, OnUpdateBUTTON2DZoomProg)
61         ON_UPDATE_COMMAND_UI(ID_BUTTON2DZoomWin, OnUpdateBUTTON2DZoomWin)
62         ON_COMMAND(ID_Modify_ChangeBackground ,OnChangeBackground)
63         //}}AFX_MSG_MAP
64 END_MESSAGE_MAP()
65
66 /////////////////////////////////////////////////////////////////////////////
67 // OCC_2dView construction/destruction
68
69 OCC_2dView::OCC_2dView()
70 {
71   // TODO: add construction code here
72   myCurrentMode = CurAction2d_Nothing;
73   m_Pen = NULL;
74 }
75
76 OCC_2dView::~OCC_2dView()
77 {
78   myV2dView->Remove();
79   if (m_Pen) delete m_Pen;
80 }
81
82 BOOL OCC_2dView::PreCreateWindow(CREATESTRUCT& cs)
83 {
84   // TODO: Modify the Window class or styles here by modifying
85   //  the CREATESTRUCT cs
86   return CView::PreCreateWindow(cs);
87 }
88
89 /////////////////////////////////////////////////////////////////////////////
90 // OCC_2dView drawing
91
92 void OCC_2dView::OnDraw(CDC* pDC)
93 {
94   if (!myV2dView.IsNull())
95     myV2dView->Update();
96 }
97
98 void OCC_2dView::OnInitialUpdate()
99 {
100   CView::OnInitialUpdate();
101
102   Handle(WNT_Window) aWNTWindow = new WNT_Window(GetSafeHwnd(),Quantity_NOC_MATRAGRAY);   
103   myV2dView =((OCC_2dDoc*)GetDocument())->GetViewer2D()->CreateView();
104   myV2dView->SetWindow(aWNTWindow);
105   myV2dView->SetZClippingType(V3d_OFF);
106   myV2dView->SetSurfaceDetail(V3d_TEX_ALL);
107   // initialize the grids dialogs
108   TheRectangularGridDialog.Create(CRectangularGrid::IDD, NULL);
109   TheCircularGridDialog.Create(CCircularGrid::IDD, NULL);
110   TheRectangularGridDialog.SetViewer (((OCC_2dDoc*)GetDocument())->GetViewer2D());
111   TheCircularGridDialog.SetViewer (((OCC_2dDoc*)GetDocument())->GetViewer2D());
112
113   Standard_Integer w=100 , h=100 ;   /* Debug Matrox                         */
114   aWNTWindow->Size (w,h) ;           /* Keeps me unsatisfied (rlb).....      */
115   /* Resize is not supposed to be done on */
116   /* Matrox                               */
117   /* I suspect another problem elsewhere  */
118   ::PostMessage ( GetSafeHwnd () , WM_SIZE , SIZE_RESTORED , w + h*65536 ) ;
119
120 }
121
122 void OCC_2dView::OnFileExportImage()
123 {
124   LPCTSTR filter;
125   filter = _T("EXR Files (*.EXR)|*.exr|TGA Files (*.TGA)|*.tga|TIFF Files (*.TIFF)|*.tiff|"
126               "PPM Files (*.PPM)|*.ppm|JPEG Files(*.JPEG)|*.jpeg|PNG Files (*.PNG)|*.png|"
127               "GIF Files (*.GIF)|*.gif|BMP Files (*.BMP)|*.bmp|PS Files (*.PS)|*.ps|"
128               "EPS Files (*.EPS)|*.eps|TEX Files (*.TEX)|*.tex|PDF Files (*.PDF)|*.pdf"
129               "|SVG Files (*.SVG)|*.svg|PGF Files (*.PGF)|*.pgf|EMF Files (*.EMF)|*.emf||");
130   CFileDialog dlg(FALSE,_T("*.BMP"),NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
131                   filter, 
132                   NULL );
133
134
135 if (dlg.DoModal() == IDOK) 
136 {
137   SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
138   CString aFileName = dlg.GetPathName();
139   CString ext = dlg.GetFileExt();
140   if (!(ext.CompareNoCase("ps")) || !(ext.CompareNoCase("emf"))
141     || !(ext.CompareNoCase("pdf")) || !(ext.CompareNoCase("eps"))
142     || !(ext.CompareNoCase("tex")) || !(ext.CompareNoCase("svg"))
143     || !(ext.CompareNoCase("pgf")))
144   {
145     Graphic3d_ExportFormat exFormat;
146     if (!(ext.CompareNoCase("ps"))) exFormat = Graphic3d_EF_PostScript;
147     else             exFormat = Graphic3d_EF_EnhPostScript;
148     myV2dView->View()->Export( aFileName, exFormat );
149     return;
150   }
151   myV2dView->Dump(aFileName);
152  SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
153 }
154 }
155
156 /////////////////////////////////////////////////////////////////////////////
157 // OCC_2dView diagnostics
158
159 #ifdef _DEBUG
160 void OCC_2dView::AssertValid() const
161 {
162   CView::AssertValid();
163 }
164
165 void OCC_2dView::Dump(CDumpContext& dc) const
166 {
167   CView::Dump(dc);
168 }
169
170 OCC_2dDoc* OCC_2dView::GetDocument() // non-debug version is inline
171 {
172   //ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(OCC_2dDoc)));
173   return (OCC_2dDoc*)m_pDocument;
174 }
175 #endif //_DEBUG
176 void OCC_2dView::OnBUTTONGridRectLines() 
177 {
178   Handle(V3d_Viewer) aViewer = myV2dView->Viewer();
179   Handle(Graphic3d_AspectMarker3d) aGridAspect = new Graphic3d_AspectMarker3d(Aspect_TOM_BALL,Quantity_NOC_WHITE,2);
180   aViewer->SetGridEcho(aGridAspect);
181   Standard_Integer aWidth=0, aHeight=0, anOffset=0;
182   myV2dView->Window()->Size(aWidth,aHeight);
183   aViewer->SetRectangularGridGraphicValues(aWidth,aHeight,anOffset);
184   aViewer->ActivateGrid(Aspect_GT_Rectangular, Aspect_GDM_Lines);
185   FitAll();
186   
187   if (TheCircularGridDialog.IsWindowVisible())
188   {
189     TheCircularGridDialog.ShowWindow(SW_HIDE);
190     TheRectangularGridDialog.UpdateValues();
191     TheRectangularGridDialog.ShowWindow(SW_SHOW);
192   }
193 }
194
195 void OCC_2dView::OnBUTTONGridRectPoints() 
196 {
197   Handle(V3d_Viewer) aViewer = myV2dView->Viewer();
198   Handle(Graphic3d_AspectMarker3d) aGridAspect = new Graphic3d_AspectMarker3d(Aspect_TOM_BALL,Quantity_NOC_WHITE,2);
199   aViewer->SetGridEcho(aGridAspect);
200   Standard_Integer aWidth=0, aHeight=0, anOffset=0;
201   myV2dView->Window()->Size(aWidth,aHeight);
202   aViewer->SetRectangularGridGraphicValues(aWidth,aHeight,anOffset);
203   aViewer->ActivateGrid(Aspect_GT_Rectangular, Aspect_GDM_Points);
204   FitAll();
205
206   if (TheCircularGridDialog.IsWindowVisible())
207   {
208     TheCircularGridDialog.ShowWindow(SW_HIDE);
209     TheRectangularGridDialog.UpdateValues();
210     TheRectangularGridDialog.ShowWindow(SW_SHOW);
211   }
212 }
213
214 void OCC_2dView::OnBUTTONGridCircLines() 
215 {
216   Handle(V3d_Viewer) aViewer = myV2dView->Viewer();
217   Handle(Graphic3d_AspectMarker3d) aGridAspect = new Graphic3d_AspectMarker3d(Aspect_TOM_BALL,Quantity_NOC_WHITE,2);
218   aViewer->SetGridEcho(aGridAspect);
219   Standard_Integer aWidth=0, aHeight=0, anOffset=0;
220   myV2dView->Window()->Size(aWidth,aHeight);
221   aViewer->SetCircularGridGraphicValues(aWidth>aHeight?aWidth:aHeight,anOffset);
222   aViewer->ActivateGrid(Aspect_GT_Circular, Aspect_GDM_Lines);
223   FitAll();
224  
225
226   if (TheRectangularGridDialog.IsWindowVisible())
227   {
228     TheRectangularGridDialog.ShowWindow(SW_HIDE);
229     TheCircularGridDialog.UpdateValues();
230     TheCircularGridDialog.ShowWindow(SW_SHOW);
231   }
232 }
233
234 void OCC_2dView::OnBUTTONGridCircPoints() 
235 {
236   Handle(V3d_Viewer) aViewer = myV2dView->Viewer();
237   Handle(Graphic3d_AspectMarker3d) aGridAspect = new Graphic3d_AspectMarker3d(Aspect_TOM_BALL,Quantity_NOC_WHITE,2);
238   aViewer->SetGridEcho(aGridAspect);
239   Standard_Integer aWidth=0, aHeight=0, anOffset=0;
240   myV2dView->Window()->Size(aWidth,aHeight);
241   aViewer->SetCircularGridGraphicValues(aWidth>aHeight?aWidth:aHeight,anOffset);
242   aViewer->ActivateGrid(Aspect_GT_Circular, Aspect_GDM_Points);
243   FitAll();
244   if (TheRectangularGridDialog.IsWindowVisible())
245   {
246     TheRectangularGridDialog.ShowWindow(SW_HIDE);
247     TheCircularGridDialog.UpdateValues();
248     TheCircularGridDialog.ShowWindow(SW_SHOW);
249   }
250 }
251
252 void OCC_2dView::OnBUTTONGridValues() 
253 {
254   Handle(V3d_Viewer) aViewer = myV2dView->Viewer();
255   Aspect_GridType  TheGridtype = aViewer->GridType();
256
257   switch( TheGridtype ) 
258   {
259   case  Aspect_GT_Rectangular:
260     TheRectangularGridDialog.UpdateValues();
261     TheRectangularGridDialog.ShowWindow(SW_SHOW);
262     break;
263   case  Aspect_GT_Circular:
264     TheCircularGridDialog.UpdateValues();
265     TheCircularGridDialog.ShowWindow(SW_SHOW);
266     break;
267   default :
268     Standard_Failure::Raise("invalid Aspect_GridType");
269   }
270 }
271 void OCC_2dView::OnUpdateBUTTONGridValues(CCmdUI* pCmdUI) 
272 {
273   Handle(V3d_Viewer) aViewer = myV2dView->Viewer();
274   pCmdUI-> Enable( aViewer->IsActive() );
275 }
276
277 void OCC_2dView::OnBUTTONGridCancel() 
278 {
279   Handle(V3d_Viewer) aViewer = myV2dView->Viewer();
280   aViewer->DeactivateGrid();
281   TheRectangularGridDialog.ShowWindow(SW_HIDE);
282   TheCircularGridDialog.ShowWindow(SW_HIDE);
283   aViewer->Update();
284 }
285 void OCC_2dView::OnUpdateBUTTONGridCancel(CCmdUI* pCmdUI) 
286 {
287   Handle(V3d_Viewer) aViewer = myV2dView->Viewer();
288   pCmdUI-> Enable( aViewer->IsActive() );       
289 }
290
291 void OCC_2dView::OnLButtonDown(UINT nFlags, CPoint point) 
292 {
293   //  save the current mouse coordinate in min 
294   myXmin=point.x;  myYmin=point.y;
295   myXmax=point.x;  myYmax=point.y;
296
297   if ( nFlags & CASCADESHORTCUTKEY ) 
298   {
299     // Button MB1 down Control :start zomming 
300     // 
301   }
302   else // if ( MULTISELECTIONKEY )
303   {
304     switch (myCurrentMode)
305     {
306     case CurAction2d_Nothing : // start a drag
307       DragEvent2D(point.x,point.y,-1);
308       break;
309     case CurAction2d_DynamicZooming : // nothing
310       break;
311     case CurAction2d_WindowZooming : // nothing
312       break;
313     case CurAction2d_DynamicPanning :// nothing
314       break;
315     case CurAction2d_GlobalPanning :// nothing
316       break;
317     default :
318       Standard_Failure::Raise(" incompatible Current Mode ");
319       break;
320     }
321   }
322 }
323
324
325 void OCC_2dView::OnLButtonUp(UINT nFlags, CPoint point) 
326 {
327   // TODO: Add your message handler code here and/or call default
328   if ( nFlags & CASCADESHORTCUTKEY ) 
329   {
330     return;
331   }
332   else // if ( Ctrl )
333   {
334     switch (myCurrentMode)
335     {
336     case CurAction2d_Nothing :
337       if (point.x == myXmin && point.y == myYmin)
338       { // no offset between down and up --> selectEvent
339         myXmax=point.x;  
340         myYmax=point.y;
341         if (nFlags & MULTISELECTIONKEY )
342           MultiInputEvent2D(point.x,point.y);
343         else
344           InputEvent2D     (point.x,point.y);
345       } else
346       {
347         DrawRectangle2D(myXmin,myYmin,myXmax,myYmax,Standard_False);
348         myXmax=point.x;  
349         myYmax=point.y;
350         if (nFlags & MULTISELECTIONKEY)
351           MultiDragEvent2D(point.x,point.y,1);
352         else
353           DragEvent2D(point.x,point.y,1);
354       }
355       break;
356     case CurAction2d_DynamicZooming :
357       // 
358       myCurrentMode = CurAction2d_Nothing;
359       break;
360     case CurAction2d_WindowZooming :
361       myXmax=point.x;     myYmax=point.y;
362       DrawRectangle2D(myXmin,myYmin,myXmax,myYmax,Standard_False,LongDash);
363       if ((abs(myXmin-myXmax)>ValZWMin) || (abs(myYmin-myYmax)>ValZWMin))
364         // Test if the zoom window is greater than a minimale window.
365       {
366         // Do the zoom window between Pmin and Pmax
367         myV2dView->WindowFit(myXmin,myYmin,myXmax,myYmax);  
368       }  
369       myCurrentMode = CurAction2d_Nothing;
370       break;
371     case CurAction2d_DynamicPanning :
372       myCurrentMode = CurAction2d_Nothing;
373       break;
374     case CurAction2d_GlobalPanning :
375       myV2dView->Place(point.x,point.y,myCurZoom); 
376       myCurrentMode = CurAction2d_Nothing;
377       break;
378     default :
379       Standard_Failure::Raise(" incompatible Current Mode ");
380       break;
381     } //switch (myCurrentMode)
382   } //  else // if ( CASCADESHORTCUTKEY )       
383 }
384
385 void OCC_2dView::OnMButtonDown(UINT nFlags, CPoint point) 
386 {
387   if ( nFlags & CASCADESHORTCUTKEY ) 
388   {
389     // Button MB2 down + CASCADESHORTCUTKEY : panning init  
390     // 
391   }
392 }
393
394 void OCC_2dView::OnMButtonUp(UINT nFlags, CPoint point) 
395 {
396   if ( nFlags & CASCADESHORTCUTKEY ) 
397   {
398     // Button MB2 up + CASCADESHORTCUTKEY : panning stop 
399   }
400 }
401
402 void OCC_2dView::OnRButtonDown(UINT nFlags, CPoint point) 
403 {
404 #ifdef POPUPONBUTTONDOWN
405    if ( !(nFlags & CASCADESHORTCUTKEY) ) 
406      Popup2D(point.x,point.y);
407 #endif
408 }
409
410 void OCC_2dView::OnRButtonUp(UINT nFlags, CPoint point) 
411 {
412 #ifndef POPUPONBUTTONDOWN
413    if ( !(nFlags & CASCADESHORTCUTKEY) ) 
414     Popup2D(point.x,point.y);
415 #endif
416 }
417
418 void OCC_2dView::OnMouseMove(UINT nFlags, CPoint point) 
419 {
420   //   ============================  LEFT BUTTON =======================
421   if ( (nFlags & MK_LBUTTON) &! (nFlags & MK_RBUTTON) ) // Left + Right is specific
422   {
423     if ( nFlags & CASCADESHORTCUTKEY ) 
424     {
425       // move with MB1 and CASCADESHORTCUTKEY : on the dynamic zooming  
426       // Do the zoom in function of mouse's coordinates  
427       myV2dView->Zoom(myXmax,myYmax,point.x,point.y);
428       // save the current mouse coordinate in min 
429       myXmax = point.x; 
430       myYmax = point.y; 
431     }
432     else // if ( CASCADESHORTCUTKEY )
433     {
434       switch (myCurrentMode)
435       {
436       case CurAction2d_Nothing :
437         myXmax = point.x;     myYmax = point.y; 
438         DrawRectangle2D(myXmin,myYmin,myXmax,myYmax,Standard_False);
439         DragEvent2D(myXmax,myYmax,0);  
440         DrawRectangle2D(myXmin,myYmin,myXmax,myYmax,Standard_True);
441         break;
442       case CurAction2d_DynamicZooming :
443         myV2dView->Zoom(myXmax,myYmax,point.x,point.y);
444         // save the current mouse coordinate in min \n";
445         myXmax=point.x;  myYmax=point.y;
446         break;
447       case CurAction2d_WindowZooming :
448         myXmax = point.x; myYmax = point.y;     
449         DrawRectangle2D(myXmin,myYmin,myXmax,myYmax,Standard_False,LongDash);
450         DrawRectangle2D(myXmin,myYmin,myXmax,myYmax,Standard_True,LongDash);
451         break;
452       case CurAction2d_DynamicPanning :
453         myV2dView->Pan(point.x-myXmax,myYmax-point.y); // Realize the panning
454         myXmax = point.x; myYmax = point.y;     
455         break;
456       case CurAction2d_GlobalPanning : // nothing           
457         break;
458       default :
459         Standard_Failure::Raise(" incompatible Current Mode ");
460         break;
461       }//  switch (myCurrentMode)
462     }// if ( nFlags & CASCADESHORTCUTKEY )  else 
463   } else //   if ( nFlags & MK_LBUTTON) 
464     //   ============================  MIDDLE BUTTON =======================
465     if ( nFlags & MK_MBUTTON)
466     {
467       if ( nFlags & CASCADESHORTCUTKEY ) 
468       {
469         myV2dView->Pan(point.x-myXmax,myYmax-point.y); // Realize the panning
470         myXmax = point.x; myYmax = point.y;     
471
472       }
473     } else //  if ( nFlags & MK_MBUTTON)
474       //   ============================  RIGHT BUTTON =======================
475       if ( (nFlags & MK_RBUTTON) &! (nFlags & MK_LBUTTON) ) // Left + Right is specific
476       {
477       }else //if ( nFlags & MK_RBUTTON)
478         if ( (nFlags & MK_RBUTTON) && (nFlags & MK_LBUTTON) )
479         {
480           // in case of Left + Right : same as Middle
481           if ( nFlags & CASCADESHORTCUTKEY ) 
482           {
483             myV2dView->Pan(point.x-myXmax,myYmax-point.y); // Realize the panning
484             myXmax = point.x; myYmax = point.y; 
485           }
486         }else //if ( nFlags & MK_RBUTTON)&& (nFlags & MK_LBUTTON) 
487           //   ============================  NO BUTTON =======================
488         {  // No buttons 
489           myXmax = point.x; myYmax = point.y;   
490           if (nFlags & MULTISELECTIONKEY)
491             MultiMoveEvent2D(point.x,point.y);
492           else
493             MoveEvent2D(point.x,point.y);
494         }
495 }
496
497
498 void OCC_2dView::OnSize(UINT nType, int cx, int cy) 
499 {
500   // Take care : This fonction is call before OnInitialUpdate
501   if (!myV2dView.IsNull())
502     myV2dView->MustBeResized(); 
503
504 }
505
506 void OCC_2dView::OnBUTTONFitAll() 
507 {
508   myV2dView->FitAll();
509 }
510
511 void OCC_2dView::OnBUTTONGlobPanning() 
512 {
513   //save the current zoom value
514   myCurZoom = myV2dView->Scale();  
515
516   // Do a Global Zoom 
517   myV2dView->FitAll();
518
519   // Set the mode
520   myCurrentMode = CurAction2d_GlobalPanning;
521 }
522 void OCC_2dView::OnBUTTONPanning() 
523 {
524   myCurrentMode = CurAction2d_DynamicPanning;
525 }
526 void OCC_2dView::OnBUTTONZoomProg() 
527 {
528   myCurrentMode = CurAction2d_DynamicZooming;
529 }
530 void OCC_2dView::OnBUTTONZoomWin() 
531 {
532   myCurrentMode = CurAction2d_WindowZooming;
533 }
534 void OCC_2dView::OnChangeBackground() 
535 {
536   Standard_Real R1, G1, B1;
537   Handle(Aspect_Window) aWindow = myV2dView->Window();
538   Aspect_Background ABack = aWindow->Background();
539   Quantity_Color aColor = ABack.Color();
540   aColor.Values(R1,G1,B1,Quantity_TOC_RGB);
541   COLORREF m_clr ;
542   m_clr = RGB(R1*255,G1*255,B1*255);
543
544   CColorDialog dlgColor(m_clr);
545   if (dlgColor.DoModal() == IDOK)
546   {
547     m_clr = dlgColor.GetColor();
548     R1 = GetRValue(m_clr)/255.;
549     G1 = GetGValue(m_clr)/255.;
550     B1 = GetBValue(m_clr)/255.;
551     aColor.SetValues(R1,G1,B1,Quantity_TOC_RGB);
552     myV2dView->SetBackgroundColor(aColor);
553     myV2dView->Update();
554   }     
555 }
556
557
558 void OCC_2dView::OnUpdateBUTTON2DGlobPanning(CCmdUI* pCmdUI) 
559 {
560   pCmdUI->SetCheck (myCurrentMode == CurAction2d_GlobalPanning);
561   pCmdUI->Enable   (myCurrentMode != CurAction2d_GlobalPanning);
562 }
563
564 void OCC_2dView::OnUpdateBUTTON2DPanning(CCmdUI* pCmdUI) 
565 {
566   pCmdUI->SetCheck (myCurrentMode == CurAction2d_DynamicPanning);
567   pCmdUI->Enable   (myCurrentMode != CurAction2d_DynamicPanning);
568 }
569
570 void OCC_2dView::OnUpdateBUTTON2DZoomProg(CCmdUI* pCmdUI) 
571 {
572   pCmdUI->SetCheck (myCurrentMode == CurAction2d_DynamicZooming);
573   pCmdUI->Enable   (myCurrentMode != CurAction2d_DynamicZooming);
574 }
575
576 void OCC_2dView::OnUpdateBUTTON2DZoomWin(CCmdUI* pCmdUI) 
577 {
578   pCmdUI->SetCheck (myCurrentMode == CurAction2d_WindowZooming);
579   pCmdUI->Enable   (myCurrentMode != CurAction2d_WindowZooming);                
580 }
581
582
583 void OCC_2dView::DrawRectangle2D(const Standard_Integer  MinX,
584                               const Standard_Integer  MinY,
585                               const Standard_Integer  MaxX,
586                               const Standard_Integer  MaxY,
587                               const Standard_Boolean  Draw, 
588                               const LineStyle aLineStyle)
589 {
590   static int m_DrawMode;
591   if  (!m_Pen && aLineStyle ==Solid )
592   {m_Pen = new CPen(PS_SOLID, 1, RGB(0,0,0)); m_DrawMode = R2_MERGEPENNOT;}
593   else if (!m_Pen && aLineStyle ==Dot )
594   {m_Pen = new CPen(PS_DOT, 1, RGB(0,0,0));   m_DrawMode = R2_XORPEN;}
595   else if (!m_Pen && aLineStyle == ShortDash)
596   {m_Pen = new CPen(PS_DASH, 1, RGB(255,0,0));  m_DrawMode = R2_XORPEN;}
597   else if (!m_Pen && aLineStyle == LongDash)
598   {m_Pen = new CPen(PS_DASH, 1, RGB(0,0,0));    m_DrawMode = R2_NOTXORPEN;}
599   else if (aLineStyle == Default) 
600   { m_Pen = NULL;       m_DrawMode = R2_MERGEPENNOT;}
601
602   CPen* aOldPen;
603   CClientDC clientDC(this);
604   if (m_Pen) aOldPen = clientDC.SelectObject(m_Pen);
605   clientDC.SetROP2(m_DrawMode);
606
607   static                Standard_Integer StoredMinX, StoredMaxX, StoredMinY, StoredMaxY;
608   static                Standard_Boolean m_IsVisible;
609
610   if ( m_IsVisible && !Draw) // move or up  : erase at the old position 
611   {
612     clientDC.MoveTo(StoredMinX,StoredMinY); clientDC.LineTo(StoredMinX,StoredMaxY); 
613     clientDC.LineTo(StoredMaxX,StoredMaxY); 
614     clientDC.LineTo(StoredMaxX,StoredMinY); clientDC.LineTo(StoredMinX,StoredMinY);
615     m_IsVisible = false;
616   }
617
618   StoredMinX = min ( MinX, MaxX );
619   StoredMinY = min ( MinY, MaxY );
620   StoredMaxX = max ( MinX, MaxX );
621   StoredMaxY = max ( MinY, MaxY);
622
623   if (Draw) // move : draw
624   {
625     clientDC.MoveTo(StoredMinX,StoredMinY); clientDC.LineTo(StoredMinX,StoredMaxY); 
626     clientDC.LineTo(StoredMaxX,StoredMaxY); 
627     clientDC.LineTo(StoredMaxX,StoredMinY); clientDC.LineTo(StoredMinX,StoredMinY);
628     m_IsVisible = true;
629   }
630
631   if (m_Pen) clientDC.SelectObject(aOldPen);
632 }
633
634
635
636 // =====================================================================
637
638 //-----------------------------------------------------------------------------------------
639 //
640 //-----------------------------------------------------------------------------------------
641 void OCC_2dView::DragEvent2D(const Standard_Integer  x,
642                             const Standard_Integer  y,
643                             const Standard_Integer  TheState)
644 {
645   // TheState == -1  button down
646   // TheState ==  0  move
647   // TheState ==  1  button up
648
649   static Standard_Integer theButtonDownX=0;
650   static Standard_Integer theButtonDownY=0;
651
652   if (TheState == -1)
653   {
654     theButtonDownX=x;
655     theButtonDownY=y;
656   }
657
658   if (TheState == 0)
659   {
660     ((OCC_2dDoc*)GetDocument())->GetInteractiveContext()->MoveTo(x,y,myV2dView);
661     ((OCC_2dDoc*)GetDocument())->GetInteractiveContext()->Select(theButtonDownX,theButtonDownY,x,y,myV2dView);
662   }
663
664   if (TheState == 1)
665   {
666     ((OCC_2dDoc*)GetDocument())->GetInteractiveContext()->Select(true);
667   }
668 }
669
670
671 //-----------------------------------------------------------------------------------------
672 //
673 //-----------------------------------------------------------------------------------------
674 void OCC_2dView::InputEvent2D(const Standard_Integer  x     ,
675                               const Standard_Integer  y     ) 
676 {
677   ((OCC_2dDoc*)GetDocument())->GetInteractiveContext()->Select(true);
678 }
679
680 //-----------------------------------------------------------------------------------------
681 //
682 //-----------------------------------------------------------------------------------------
683 void OCC_2dView::MoveEvent2D(const Standard_Integer  x,
684                             const Standard_Integer  y) 
685 {
686   if(myV2dView->Viewer()->Grid()->IsActive())
687   {
688     Quantity_Length aGridX=0,aGridY=0,aGridZ=0;
689     myV2dView->ConvertToGrid(x,y,aGridX,aGridY,aGridZ);
690     //View is not updated automatically in ConvertToGrid
691     myV2dView->Update();
692   }
693   ((OCC_2dDoc*)GetDocument())->GetInteractiveContext()->MoveTo(x,y,myV2dView);
694 }
695
696 //-----------------------------------------------------------------------------------------
697 //
698 //-----------------------------------------------------------------------------------------
699 void OCC_2dView::MultiMoveEvent2D(const Standard_Integer  x,
700                                   const Standard_Integer  y) 
701 {
702 // MultiMoveEvent2D means we move the mouse in a multi selection mode
703 ((OCC_2dDoc*)GetDocument())->GetInteractiveContext()->MoveTo(x,y,myV2dView);
704 }
705
706 //-----------------------------------------------------------------------------------------
707 //
708 //-----------------------------------------------------------------------------------------
709 void OCC_2dView::MultiDragEvent2D(const Standard_Integer  x        ,
710                                   const Standard_Integer  y        ,
711                                   const Standard_Integer  TheState) 
712 {
713   static Standard_Integer theButtonDownX=0;
714   static Standard_Integer theButtonDownY=0;
715
716   if (TheState == -1)
717   {
718     theButtonDownX=x;
719     theButtonDownY=y;
720   }
721
722   if (TheState == 0)
723   {
724     ((OCC_2dDoc*)GetDocument())->GetInteractiveContext()->MoveTo(x,y,myV2dView);
725     ((OCC_2dDoc*)GetDocument())->GetInteractiveContext()->ShiftSelect(theButtonDownX,theButtonDownY,x,y,myV2dView);;
726   }
727
728   if (TheState == 1)
729   {
730     ((OCC_2dDoc*)GetDocument())->GetInteractiveContext()->ShiftSelect(true);
731   }
732 }
733
734
735 //-----------------------------------------------------------------------------------------
736 //
737 //-----------------------------------------------------------------------------------------
738 void OCC_2dView::MultiInputEvent2D(const Standard_Integer  x       ,
739                                   const Standard_Integer  y       ) 
740 {
741   ((OCC_2dDoc*)GetDocument())->GetInteractiveContext()->ShiftSelect(true);
742 }
743
744 //-----------------------------------------------------------------------------------------
745 //
746 //-----------------------------------------------------------------------------------------
747 void  OCC_2dView::Popup2D(const Standard_Integer  x,
748                          const Standard_Integer  y ) 
749 {
750   CMenu menu;
751   CMenu* pPopup ;
752
753   // load the 'normal' popup     
754   VERIFY(menu.LoadMenu(IDR_Popup2D));
755   // activate the sub menu '0'
756   pPopup= menu.GetSubMenu(0);
757   ASSERT(pPopup != NULL);
758
759   // display the popup
760   POINT winCoord = { x , y };
761   ClientToScreen ( &winCoord);
762   pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON , winCoord.x, winCoord.y , AfxGetMainWnd());
763 }
764
765 void OCC_2dView::FitAll()
766 {
767   myV2dView->FitAll();
768 }