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