Integration of OCCT 6.5.0 from SVN
[occt.git] / samples / mfc / standard / 03_Viewer2d / src / Properties / ColorPropertyPage.cpp
1 // ColorPropertyPage.cpp : implementation file
2 //
3
4 #include "stdafx.h"
5
6 #include "ColorPropertyPage.h"
7 #include "ColorNewColorCubeColorMapDialog.h"
8 #include "ColorNewColorRampColorMapDialog.h"
9 #include "Aspect_ColorMapEntry.hxx"
10 #include "V2d_DefaultMap.hxx"
11 #include "Aspect_GenericColorMap.hxx"
12
13 /////////////////////////////////////////////////////////////////////////////
14 // CColorPropertyPage property page
15
16 IMPLEMENT_DYNCREATE(CColorPropertyPage, CPropertyPage)
17
18 CColorPropertyPage::CColorPropertyPage()
19  : CPropertyPage(CColorPropertyPage::IDD)
20 {
21     //{{AFX_DATA_INIT(CColorPropertyPage)
22         m_ColorMapSize        = _T("Computing...");
23         m_ColorMapType        = _T("Computing...");
24         m_CurrentEntryRed     = _T("9.999");
25         m_CurrentEntryGreen   = _T("9.999");
26         m_CurrentEntryBlue    = _T("9.999");
27         m_NearsetColorName    = _T("Computing...");
28         m_NewEntryRed         = 9.999;
29         m_NewEntryBlue        = 9.999;
30         m_NewEntryGreen       = 9.999;
31         //}}AFX_DATA_INIT
32 }
33
34 BOOL CColorPropertyPage::OnInitDialog() 
35 {
36         CPropertyPage::OnInitDialog();
37         
38     for (int i = 0 ;i< 517 ;  i++)
39     {
40       Standard_CString TheColorName = Quantity_Color::StringName((Quantity_NameOfColor)i);
41       // update the CComboBox to add the enumeration possibilities.
42       m_NewEntryColorNameCtrl.AddString( TheColorName );
43     }
44     m_NewEntryColorNameCtrl.SetCurSel(1);
45         // TODO: Add extra initialization here
46     UpdateDisplay(1);
47         
48         return TRUE;  // return TRUE unless you set the focus to a control
49                       // EXCEPTION: OCX Property Pages should return FALSE
50 }
51
52 CColorPropertyPage::~CColorPropertyPage()
53 {
54 }
55
56 void CColorPropertyPage::DoDataExchange(CDataExchange* pDX)
57 {
58         CPropertyPage::DoDataExchange(pDX);
59         //{{AFX_DATA_MAP(CColorPropertyPage)
60         DDX_Control(pDX, IDC_ColorMap_COMBO_NewEntryColorName   , m_NewEntryColorNameCtrl );
61         DDX_Control(pDX, IDC_ColorMap_TAB                       , m_TabCtrl               );
62         DDX_Text   (pDX, IDC_ColorMap_STATIC_Size               , m_ColorMapSize          );
63         DDX_Text   (pDX, IDC_ColorMap_STATIC_Type               , m_ColorMapType          );
64         DDX_Text   (pDX, IDC_ColorMap_STATIC_EntryColorRed      , m_CurrentEntryRed       );
65         DDX_Text   (pDX, IDC_ColorMap_STATIC_EntryColorGreen    , m_CurrentEntryGreen     );
66         DDX_Text   (pDX, IDC_ColorMap_STATIC_EntryColorBlue     , m_CurrentEntryBlue      );
67         DDX_Text   (pDX, IDC_ColorMap_STATIC_EntryColorName     , m_NearsetColorName      );
68         DDX_Text   (pDX, IDC_ColorMap_EDIT_NewEntryColorRed     , m_NewEntryRed           );
69         DDX_Text   (pDX, IDC_ColorMap_EDIT_NewEntryColorBlue    , m_NewEntryBlue          );
70         DDX_Text   (pDX, IDC_ColorMap_EDIT_NewEntryColorGreen   , m_NewEntryGreen         );
71         //}}AFX_DATA_MAP
72 }
73
74 BEGIN_MESSAGE_MAP(CColorPropertyPage, CPropertyPage)
75         //{{AFX_MSG_MAP(CColorPropertyPage)
76         ON_NOTIFY(TCN_SELCHANGE, IDC_ColorMap_TAB, OnSelchangeColorMapTAB)
77         ON_CBN_SELCHANGE(IDC_ColorMap_COMBO_NewEntryColorName, OnSelchangeColorMapCOMBONewEntryColorName)
78         ON_BN_CLICKED(IDC_ColorMap_BUTTON_UpdateCurrentEntry, OnColorMapBUTTONUpdateCurrentEntry)
79         ON_BN_CLICKED(IDC_ColorMap_BUTTON_NewColorCubeColorMap, OnColorMapBUTTONNewColorCubeColorMap)
80         ON_BN_CLICKED(IDC_ColorMap_BUTTON_NewGenericColorMap, OnColorMapBUTTONNewGenericColorMap)
81         ON_BN_CLICKED(IDC_ColorMap_BUTTON_NewColorRampColorMap, OnColorMapBUTTONNewColorRampColorMap)
82         ON_BN_CLICKED(IDC_ColorMap_BUTTON_NewEntry_EditColor, OnColorMapBUTTONNewEntryEditColor)
83         ON_BN_CLICKED(IDC_ColorMap_BUTTON_AddNewEntry, OnColorMapBUTTONAddNewEntry)
84         //}}AFX_MSG_MAP
85 END_MESSAGE_MAP()
86
87 void CColorPropertyPage::UpdateDisplay(int CurrentSelectionIndex)
88 {
89     Aspect_TypeOfColorMap aColorMapType = myColorMap->Type();
90     switch (aColorMapType) {
91       case Aspect_TOC_Generic   : m_ColorMapType = _T("Generic");  ; break;
92       case Aspect_TOC_ColorCube : m_ColorMapType = _T("ColorCube"); break;
93       case Aspect_TOC_ColorRamp : m_ColorMapType = _T("ColorRamp"); break;
94       default : cout<<" Underknown";
95     }
96
97     int ShowOrHide;
98     if (aColorMapType == Aspect_TOC_Generic)
99     {
100       ShowOrHide = SW_SHOW;
101       GetDlgItem(IDC_ColorMap_STATIC_NewEntryNotavailable)->ShowWindow(SW_HIDE);
102     }
103     else
104     {  
105       ShowOrHide = SW_HIDE;
106       GetDlgItem(IDC_ColorMap_STATIC_NewEntryNotavailable)->ShowWindow(SW_SHOW);
107     }
108
109     GetDlgItem(IDC_ColorMap_EDIT_NewEntryColorRed)->ShowWindow(ShowOrHide);
110     GetDlgItem(IDC_ColorMap_EDIT_NewEntryColorGreen)->ShowWindow(ShowOrHide);
111     GetDlgItem(IDC_ColorMap_EDIT_NewEntryColorBlue)->ShowWindow(ShowOrHide);
112     GetDlgItem(IDC_ColorMap_COMBO_NewEntryColorName)->ShowWindow(ShowOrHide);
113     GetDlgItem(IDC_ColorMap_BUTTON_NewEntry_EditColor)->ShowWindow(ShowOrHide);
114     GetDlgItem(IDC_ColorMap_BUTTON_UpdateCurrentEntry)->ShowWindow(ShowOrHide);
115     GetDlgItem(IDC_ColorMap_BUTTON_AddNewEntry)->ShowWindow(ShowOrHide);
116     GetDlgItem(IDC_ColorMap_STATIC_NewEntryColorNameStatic)->ShowWindow(ShowOrHide);
117
118     TCollection_AsciiString SizeMessage(myColorMap->Size());
119     m_ColorMapSize = _T(SizeMessage.ToCString());
120     // clear the Tab Ctrl
121     m_TabCtrl.DeleteAllItems();
122
123         TC_ITEM TabCtrlItem;
124         TabCtrlItem.mask = TCIF_TEXT;
125
126     for(int i =0;i<myColorMap->Size();i++) // not <=ColorMapSize, I've enter a CSR 
127     {
128       Aspect_ColorMapEntry aColorMapEntry = myColorMap->FindEntry(i);
129       TCollection_AsciiString EntryNumerMessage(aColorMapEntry.Index());
130       TabCtrlItem.pszText = (LPSTR)EntryNumerMessage.ToCString();
131           m_TabCtrl.InsertItem( aColorMapEntry.Index(), &TabCtrlItem );
132     }
133     m_TabCtrl.SetCurSel(CurrentSelectionIndex);
134
135     // update The Current Selected entry
136       Aspect_ColorMapEntry aColorMapEntry = myColorMap->FindEntry(CurrentSelectionIndex);
137       if (aColorMapEntry. IsAllocated () ) 
138       {
139         Quantity_Color TheColor =  aColorMapEntry.Color();
140         Quantity_Parameter Red,Green,Blue;
141         TheColor.Values(Red,Green,Blue,Quantity_TOC_RGB);
142         TCollection_AsciiString Message;
143         Message = Red;  
144         m_CurrentEntryRed     = _T(Message.ToCString());
145         m_NewEntryRed         = Red;
146         Message = Green; 
147         m_CurrentEntryGreen   = _T(Message.ToCString());
148         m_NewEntryGreen       = Green;
149         Message = Blue;  
150         m_CurrentEntryBlue    = _T(Message.ToCString());
151         m_NewEntryBlue        = Blue;
152
153         Standard_CString StringName = Quantity_Color::StringName(TheColor.Name());
154         m_NearsetColorName = StringName;
155         m_NewEntryColorNameCtrl.SetCurSel(TheColor.Name());
156        if (aColorMapType == Aspect_TOC_Generic) 
157          if (m_TabCtrl.GetCurSel() == 0)
158           GetDlgItem(IDC_ColorMap_BUTTON_UpdateCurrentEntry)->ShowWindow(SW_HIDE);
159          else
160            GetDlgItem(IDC_ColorMap_BUTTON_UpdateCurrentEntry)->ShowWindow(SW_SHOW);
161       }
162       else 
163        {
164               m_NearsetColorName = _T("Not Allocated");       
165           m_CurrentEntryRed     = _T("9.999");
166           m_NewEntryRed         = 9.999;
167           m_CurrentEntryGreen   =  _T("9.999");
168           m_NewEntryGreen       = 9.999;
169           m_CurrentEntryBlue    =  _T("9.999");
170           m_NewEntryBlue        = 9.999;
171           GetDlgItem(IDC_ColorMap_BUTTON_UpdateCurrentEntry)->ShowWindow(SW_HIDE);
172        }
173
174     UpdateData(false);
175 }
176 void CColorPropertyPage::OnColorMapBUTTONNewGenericColorMap() 
177 {
178         // TODO: Add your control notification handler code here
179         myColorMap = V2d_DefaultMap::ColorMap();
180     SetModified(true);
181     UpdateDisplay(1);   
182 }
183
184 void CColorPropertyPage::OnColorMapBUTTONNewColorCubeColorMap() 
185 {
186   CColorNewColorCubeColorMapDialog aDlg(NULL);
187   if (aDlg.DoModal() == IDOK)
188   {
189    myColorMap = aDlg.ColorMap(); 
190    SetModified(true);
191    UpdateDisplay(1);    
192   }
193 }
194
195 void CColorPropertyPage::OnColorMapBUTTONNewColorRampColorMap() 
196 {
197   CColorNewColorRampColorMapDialog aDlg(NULL);
198   if (aDlg.DoModal() == IDOK)
199   {
200    myColorMap = aDlg.ColorMap(); 
201    SetModified(true);
202    UpdateDisplay(1);    
203   }
204 }
205
206 /////////////////////////////////////////////////////////////////////////////
207 // CColorPropertyPage message handlers
208
209 void CColorPropertyPage::OnSelchangeColorMapTAB(NMHDR* pNMHDR, LRESULT* pResult) 
210 {
211     UpdateDisplay(m_TabCtrl.GetCurSel());       
212         *pResult = 0;
213 }
214
215 void CColorPropertyPage::OnSelchangeColorMapCOMBONewEntryColorName() 
216 {
217     UpdateData(true);   
218     int CurSel = m_NewEntryColorNameCtrl.GetCurSel();
219     Quantity_NameOfColor SelectedNameOfColor = (Quantity_NameOfColor)CurSel;
220     Quantity_Color TheSelectedEquivalentColor(SelectedNameOfColor);
221
222     Quantity_Parameter Red,Green,Blue;
223     TheSelectedEquivalentColor.Values(Red,Green,Blue,Quantity_TOC_RGB);
224     m_NewEntryRed         = Red;
225     m_NewEntryGreen       = Green;
226     m_NewEntryBlue        = Blue; 
227     UpdateData(false);  
228 }
229
230 void CColorPropertyPage::OnColorMapBUTTONNewEntryEditColor() 
231 {
232         int CurrentSelectedItem = m_TabCtrl.GetCurSel();
233
234     COLORREF m_clr ;
235         m_clr = RGB(m_NewEntryRed*255,m_NewEntryGreen*255,m_NewEntryBlue*255);
236         CColorDialog dlgColor(m_clr);
237         if (dlgColor.DoModal() == IDOK)
238         {
239         SetModified(TRUE);
240                 m_clr = dlgColor.GetColor();
241                 m_NewEntryRed    = GetRValue(m_clr)/255.;
242                 m_NewEntryGreen  = GetGValue(m_clr)/255.;
243                 m_NewEntryBlue   = GetBValue(m_clr)/255.;
244     }
245     Quantity_Color aColor(m_NewEntryRed,m_NewEntryGreen,m_NewEntryBlue,Quantity_TOC_RGB);
246     m_NewEntryColorNameCtrl.SetCurSel(aColor.Name());
247     UpdateData(false);
248 }
249
250 void CColorPropertyPage::OnColorMapBUTTONUpdateCurrentEntry() 
251 {
252     UpdateData(true);
253     Handle(Aspect_GenericColorMap) aGenericColorMap = Handle(Aspect_GenericColorMap)::DownCast(myColorMap);
254     if (aGenericColorMap.IsNull()) 
255        Standard_Failure::Raise(" couldn't update a none Generic Color Map");
256
257     Aspect_ColorMapEntry aColorMapEntry = myColorMap->FindEntry( m_TabCtrl.GetCurSel());
258     Quantity_Color aColor(m_NewEntryRed,m_NewEntryGreen,m_NewEntryBlue,Quantity_TOC_RGB);
259     aColorMapEntry.SetColor(aColor);
260     aGenericColorMap->AddEntry(aColorMapEntry); // in fact just update
261     SetModified(true);
262     UpdateData(true);
263     UpdateDisplay(m_TabCtrl.GetCurSel());       
264
265 }
266
267 void CColorPropertyPage::OnColorMapBUTTONAddNewEntry() 
268 {
269    UpdateData(true);
270    Quantity_Color aColor(m_NewEntryRed,m_NewEntryGreen,m_NewEntryBlue,Quantity_TOC_RGB);
271     Handle(Aspect_GenericColorMap) aGenericColorMap = Handle(Aspect_GenericColorMap)::DownCast(myColorMap);
272     if (aGenericColorMap.IsNull()) 
273        Standard_Failure::Raise(" couldn't update a none Generic Color Map");
274
275    int NewEntry = aGenericColorMap->AddEntry(aColor);
276    SetModified(true);
277    UpdateDisplay(NewEntry);
278    UpdateData(false);
279 }
280
281 BOOL CColorPropertyPage::OnApply() 
282 {
283     myViewer->SetColorMap(myColorMap);
284     myViewer->Update();
285         return CPropertyPage::OnApply();
286 }