Integration of OCCT 6.5.0 from SVN
[occt.git] / samples / mfc / standard / 08_HLR / src / Properties / TypePropertyPage.cpp
1 // TypePropertyPage.cpp : implementation file
2 //
3
4 #include "stdafx.h"
5
6 #include "TypePropertyPage.h"
7
8 #include "HLRApp.h"
9
10 #ifdef _DEBUG
11 //#define new DEBUG_NEW
12 #undef THIS_FILE
13 static char THIS_FILE[] = __FILE__;
14 #endif
15
16 #include "Aspect_TypeMap.hxx"
17 #include "Aspect_TypeMapEntry.hxx"
18 #include "TColQuantity_Array1OfLength.hxx"
19 #include "TColStd_SequenceOfReal.hxx"
20
21 /////////////////////////////////////////////////////////////////////////////
22 // CTypePropertyPage property page
23
24 IMPLEMENT_DYNCREATE(CTypePropertyPage, CPropertyPage)
25
26 CTypePropertyPage::CTypePropertyPage() : CPropertyPage(CTypePropertyPage::IDD)
27 {
28         //{{AFX_DATA_INIT(CTypePropertyPage)
29         m_TypeMapSize       = _T("Computing...");
30         m_CurrentEntryStyle = _T("Computing...");
31         m_CurrentEntryValue = _T("Computing...");
32     m_NewEntryValue     = _T("");
33         //}}AFX_DATA_INIT
34 }
35
36 BOOL CTypePropertyPage::OnInitDialog() 
37 {
38         CPropertyPage::OnInitDialog();
39         
40         // TODO: Add extra initialization here
41
42     m_NewEntryControl.AddString("SOLID");
43     m_NewEntryControl.AddString("DASH");
44     m_NewEntryControl.AddString("DOT");
45     m_NewEntryControl.AddString("DOTDASH");
46     m_NewEntryControl.AddString("USERDEFINED");
47     m_NewEntryControl.SetCurSel(1);
48
49     UpdateDisplay(1);
50
51     UpdateData(false);
52
53         return TRUE;  // return TRUE unless you set the focus to a control
54                       // EXCEPTION: OCX Property Pages should return FALSE
55 }
56
57 CTypePropertyPage::~CTypePropertyPage()
58 {
59 }
60
61 void CTypePropertyPage::DoDataExchange(CDataExchange* pDX)
62 {
63         CPropertyPage::DoDataExchange(pDX);
64         //{{AFX_DATA_MAP(CTypePropertyPage)
65         DDX_Control(pDX, IDC_TypeMap_TAB, m_TabCtrl);
66         DDX_Control(pDX, IDC_TypeMap_COMBO_NewEntryStyle, m_NewEntryControl);
67         DDX_Control(pDX, IDC_TypeMap_EDIT_NewEntryValue, m_NewEntryValueControl);
68         DDX_Text(pDX, IDC_TypeMap_STATIC_Size, m_TypeMapSize);
69         DDX_Text(pDX, IDC_TypeMap_STATIC_CurrentEntryStyle, m_CurrentEntryStyle);
70         DDX_Text(pDX, IDC_TypeMap_STATIC_CurrentEntryValue, m_CurrentEntryValue);
71         DDX_Text(pDX, IDC_TypeMap_EDIT_NewEntryValue, m_NewEntryValue);
72
73         //}}AFX_DATA_MAP
74 }
75
76
77 BEGIN_MESSAGE_MAP(CTypePropertyPage, CPropertyPage)
78         //{{AFX_MSG_MAP(CTypePropertyPage)
79         ON_BN_CLICKED(IDC_FontMap_BUTTON_NewEntry, OnFontMapBUTTONNewEntry)
80         ON_BN_CLICKED(IDC_FontMap_BUTTON_UpdateCurrent, OnFontMapBUTTONUpdateCurrent)
81         ON_EN_CHANGE(IDC_TypeMap_EDIT_NewEntryValue, OnChangeTypeMapEDITNewEntryValue)
82         ON_NOTIFY(TCN_SELCHANGE, IDC_TypeMap_TAB, OnSelchangeTypeMapTAB)
83         ON_CBN_SELCHANGE(IDC_TypeMap_COMBO_NewEntryStyle, OnSelchangeTypeMapCOMBONewEntryStyle)
84         //}}AFX_MSG_MAP
85 END_MESSAGE_MAP()
86
87 /////////////////////////////////////////////////////////////////////////////
88 // CTypePropertyPage message handlers
89 void CTypePropertyPage::UpdateDisplay(int CurrentSelectionIndex)
90 {
91     TCollection_AsciiString SizeMessage(myTypeMap->Size());
92     m_TypeMapSize = _T(SizeMessage.ToCString());
93         
94     m_TabCtrl.DeleteAllItems();
95
96         TC_ITEM TabCtrlItem;
97         TabCtrlItem.mask = TCIF_TEXT;
98
99     for(int i =1;i<=myTypeMap->Size();i++) 
100     {
101       Aspect_TypeMapEntry aTypeMapEntry = myTypeMap->Entry(i);
102       TCollection_AsciiString EntryNumerMessage(aTypeMapEntry.Index());
103       TabCtrlItem.pszText = (LPSTR) EntryNumerMessage.ToCString();
104           m_TabCtrl.InsertItem( aTypeMapEntry.Index(), &TabCtrlItem );
105     }
106     m_TabCtrl.SetCurSel(CurrentSelectionIndex-1);
107
108     // update The Current Selected entry
109     Aspect_TypeMapEntry aTypeMapEntry = myTypeMap->Entry(CurrentSelectionIndex);
110     if (aTypeMapEntry. IsAllocated () ) 
111       {
112         Aspect_LineStyle TypeMapEntryType = aTypeMapEntry.Type() ;
113
114         switch (TypeMapEntryType.Style()) {
115           case Aspect_TOL_SOLID       :  m_CurrentEntryStyle = _T("SOLID");       break;
116           case Aspect_TOL_DASH        :  m_CurrentEntryStyle = _T("DASH");        break;
117           case Aspect_TOL_DOT         :  m_CurrentEntryStyle = _T("DOT");         break;
118           case Aspect_TOL_DOTDASH     :  m_CurrentEntryStyle = _T("DOTDASH");     break;
119           case Aspect_TOL_USERDEFINED :  m_CurrentEntryStyle = _T("USERDEFINED"); break;
120           default : m_CurrentEntryStyle = _T("Underknown");
121         }
122         int NbValue = TypeMapEntryType.Length();
123         TCollection_AsciiString Message = BuildValuesAscii(TypeMapEntryType.Values());
124
125             m_CurrentEntryValue = _T(Message.ToCString());
126
127         m_NewEntryControl.SetCurSel(TypeMapEntryType.Style());
128         m_NewEntryValue = _T(Message.ToCString());  
129         if (TypeMapEntryType.Style() == Aspect_TOL_USERDEFINED)
130           m_NewEntryValueControl.SetReadOnly( false );
131         else
132           m_NewEntryValueControl.SetReadOnly( true );
133       }
134       else 
135       {
136             m_CurrentEntryStyle = _T("Not Allocated");
137             m_CurrentEntryValue = _T("");
138         m_NewEntryValue     = _T("No value");  
139         GetDlgItem(IDC_FontMap_BUTTON_UpdateCurrent)->ShowWindow(SW_HIDE);
140       }
141 }
142
143 void CTypePropertyPage::OnSelchangeTypeMapTAB(NMHDR* pNMHDR, LRESULT* pResult) 
144 {
145     UpdateDisplay(m_TabCtrl.GetCurSel()+1);     
146     UpdateData(false);
147         *pResult = 0;
148 }
149 void CTypePropertyPage::OnSelchangeTypeMapCOMBONewEntryStyle() 
150 {
151    UpdateData(true);
152    // the Type Map entry change in the edit part
153    if (m_NewEntryControl.GetCurSel() == Aspect_TOL_USERDEFINED)
154      m_NewEntryValueControl.SetReadOnly( false );
155    else
156    {
157       m_NewEntryValueControl.SetReadOnly( true );
158     
159     // create a dummy map to extract the default values
160       Aspect_TypeMapEntry aTypeMapEntry(99,(Aspect_TypeOfLine)m_NewEntryControl.GetCurSel());
161       Aspect_LineStyle TypeMapEntryType = aTypeMapEntry.Type() ;
162       TCollection_AsciiString Message = BuildValuesAscii(TypeMapEntryType.Values());
163
164       m_NewEntryValue = _T(Message.ToCString());
165    }
166    UpdateData(false);
167 }
168
169 void CTypePropertyPage::OnChangeTypeMapEDITNewEntryValue() 
170 {
171    UpdateData(true);
172    Handle(TColQuantity_HArray1OfLength) anArray;
173    Standard_Boolean IsDone = ExtractValues(TCollection_AsciiString((char *)(LPCSTR)m_NewEntryValue),anArray);
174    if (IsDone)
175    {
176         GetDlgItem(IDC_FontMap_BUTTON_UpdateCurrent)->ShowWindow(SW_SHOW);
177         GetDlgItem(IDC_FontMap_BUTTON_NewEntry)->ShowWindow(SW_SHOW);
178    }
179    else
180    {
181         GetDlgItem(IDC_FontMap_BUTTON_UpdateCurrent)->ShowWindow(SW_HIDE);
182         GetDlgItem(IDC_FontMap_BUTTON_NewEntry)->ShowWindow(SW_HIDE);
183     }
184 }
185
186 void CTypePropertyPage::OnFontMapBUTTONNewEntry() 
187 {
188    UpdateData(true);
189    int NewEntry;
190
191    if (m_NewEntryControl.GetCurSel() == Aspect_TOL_USERDEFINED)
192    {
193      Handle(TColQuantity_HArray1OfLength) anArray;
194      if (ExtractValues(TCollection_AsciiString((char *)(LPCSTR)m_NewEntryValue),anArray))
195      {
196        Aspect_LineStyle aLineStyle(anArray->Array1());
197       NewEntry = myTypeMap->AddEntry( aLineStyle );
198      }
199      else Standard_Failure::Raise(" The String is not Valid ");
200    }
201    else
202    {
203       Aspect_LineStyle aLineStyle((Aspect_TypeOfLine)m_NewEntryControl.GetCurSel());
204       NewEntry = myTypeMap->AddEntry( aLineStyle );
205    }
206
207    SetModified(true);
208    UpdateDisplay(NewEntry);
209    UpdateData(false);
210 }
211
212 void CTypePropertyPage::OnFontMapBUTTONUpdateCurrent() 
213 {
214    UpdateData(true);
215    Aspect_TypeMapEntry aTypeMapEntry = myTypeMap->Entry( m_TabCtrl.GetCurSel()+1);
216
217    if (m_NewEntryControl.GetCurSel() == Aspect_TOL_USERDEFINED)
218    {
219      Handle(TColQuantity_HArray1OfLength) anArray;
220      if (ExtractValues(TCollection_AsciiString((char *)(LPCSTR)m_NewEntryValue),anArray))
221      {
222        Aspect_LineStyle aLineStyle(anArray->Array1());
223        aTypeMapEntry.SetType( aLineStyle );
224      }
225      else Standard_Failure::Raise(" The String is not Valid ");
226    }
227    else
228    {
229      Aspect_LineStyle aLineStyle((Aspect_TypeOfLine)m_NewEntryControl.GetCurSel());
230      aTypeMapEntry.SetType( aLineStyle );
231    }
232
233    myTypeMap->AddEntry(aTypeMapEntry); // in fact just update
234    
235    SetModified(true);
236    UpdateDisplay(m_TabCtrl.GetCurSel()+1);      
237    UpdateData(false);
238 }
239
240 BOOL CTypePropertyPage::OnApply() 
241 {
242     myViewer->SetTypeMap(myTypeMap);
243     myViewer->Update();
244         return CPropertyPage::OnApply();
245 }
246
247 Standard_Boolean  CTypePropertyPage::ExtractValues(TCollection_AsciiString aMessage,
248                                                    Handle(TColQuantity_HArray1OfLength)& anArray) // out 
249 {
250   TColStd_SequenceOfReal aSequenceOfReal;
251   Standard_Integer CurrentStartValue=1;
252   bool NotFinish = true;
253   while (NotFinish)
254   {
255     CurrentStartValue = aMessage.SearchFromEnd(";");
256     if ( CurrentStartValue == aMessage.Length()) return Standard_False;
257     if (CurrentStartValue != -1)
258     { 
259       TCollection_AsciiString aNewMessage = aMessage.Split(CurrentStartValue);
260       aMessage.Remove(aMessage.Length());
261       if (aNewMessage.IsRealValue())
262         aSequenceOfReal.Append(aNewMessage.RealValue());
263       else  return Standard_False;
264     }
265     else
266     {
267       if (aMessage.IsRealValue())
268         aSequenceOfReal.Append(aMessage.RealValue());
269       else  return Standard_False;
270       NotFinish = false;
271     }
272   }
273
274   anArray = new TColQuantity_HArray1OfLength(1,aSequenceOfReal.Length());
275   for (int i=1;i<=aSequenceOfReal.Length();i++)
276     anArray->SetValue(i,aSequenceOfReal(aSequenceOfReal.Length()-i+1));
277
278   return Standard_True;
279 }
280
281 TCollection_AsciiString CTypePropertyPage::BuildValuesAscii(const TColQuantity_Array1OfLength& anArray)
282 {
283   TCollection_AsciiString Message;
284   int NbValue = anArray.Length();
285   for (int j=1;j<NbValue;j++)
286    {
287      Message += anArray(j);
288      Message += " ; ";
289    }
290   Quantity_Length Length =  anArray(NbValue);
291   if (Length > Precision::Confusion()) Message += Length;
292   else Message += "No value";
293   return Message;
294 }