Update the changelog
[opencv] / apps / Hawk / LogView.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 //
10 //                        Intel License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 //   * Redistribution's of source code must retain the above copyright notice,
20 //     this list of conditions and the following disclaimer.
21 //
22 //   * Redistribution's in binary form must reproduce the above copyright notice,
23 //     this list of conditions and the following disclaimer in the documentation
24 //     and/or other materials provided with the distribution.
25 //
26 //   * The name of Intel Corporation may not be used to endorse or promote products
27 //     derived from this software without specific prior written permission.
28 //
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
39 //
40 //M*/// LogView.cpp : implementation file
41 //
42
43 #include "stdafx.h"
44 #include "Hawk.h"
45 #include "LogView.h"
46
47 #ifdef _DEBUG
48 #define new DEBUG_NEW
49 #undef THIS_FILE
50 static char THIS_FILE[] = __FILE__;
51 #endif
52
53
54 /////////////////////////////////////////////////////////////////////////////
55 // CLogView
56
57 IMPLEMENT_DYNCREATE(CLogView, CEditView)
58
59 CLogView::CLogView() : m_edit(GetEditCtrl()), m_logLength(30000)
60 {
61 }
62
63 CLogView::~CLogView()
64 {
65 }
66
67
68 BEGIN_MESSAGE_MAP(CLogView, CEditView)
69         //{{AFX_MSG_MAP(CLogView)
70         ON_WM_CREATE()
71         ON_CONTROL_REFLECT(EN_CHANGE, OnChange)
72         //}}AFX_MSG_MAP
73 END_MESSAGE_MAP()
74
75 /////////////////////////////////////////////////////////////////////////////
76 // CLogView drawing
77
78 void CLogView::OnDraw(CDC* pDC)
79 {
80         CDocument* pDoc = GetDocument();
81         // TODO: add draw code here
82 }
83
84 /////////////////////////////////////////////////////////////////////////////
85 // CLogView diagnostics
86
87 #ifdef _DEBUG
88 void CLogView::AssertValid() const
89 {
90         CView::AssertValid();
91 }
92
93 void CLogView::Dump(CDumpContext& dc) const
94 {
95         CView::Dump(dc);
96 }
97 #endif //_DEBUG
98
99 /////////////////////////////////////////////////////////////////////////////
100 // CLogView message handlers
101
102 int CLogView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
103 {
104         if (CView::OnCreate(lpCreateStruct) == -1)
105                 return -1;
106         
107     m_edit.SetReadOnly();       
108         return 0;
109 }
110
111 int CLogView::AddLine(LPCTSTR strings)
112 {
113         char    newstring[255];
114
115         strcpy(newstring, "\r\n");
116         if (strlen(strings)<252)
117         strcat(newstring, strings);
118     else 
119         strncat(newstring, strings, 252);        
120
121
122         int end = m_length;//m_edit.GetWindowTextLength();
123     if(end + (int)strlen(strings) > m_logLength)
124     {
125         /* Delete the beginning to free the space */
126         m_edit.SetSel(0, end + strlen(strings) - m_logLength);
127         m_edit.ReplaceSel("");
128     }
129
130         if (end == 0) {
131                 m_edit.SetWindowText(strings);
132                 return 0;
133         }
134
135         int number_lines = m_edit.GetLineCount();
136         int start = m_edit.LineIndex(number_lines-1);
137
138         int len = m_edit.LineLength(end);
139         if (len==0) {
140                 m_edit.SetSel(end, end+1);
141                 m_edit.ReplaceSel(strings);
142                 number_lines--;
143         } else {
144                 m_edit.SetSel(end, end+1);
145                 m_edit.ReplaceSel(newstring);
146         }
147
148         return number_lines;
149 }
150
151 void CLogView::AddString(LPCTSTR _text)
152 {
153     char* text = _strdup(_text);
154     int index = 0, i = 0;
155 //    int length = m_edit.GetWindowTextLength();
156     int length = m_length;
157     int l = strlen(text);
158     char* buffer = new char[strlen(text) + 2];
159     char c;
160
161     while(1)
162     {
163         while(text[i] != 0 && text[i] != 10 && text[i] != 13) i++;
164         if(text[i] == 0)
165         {
166             length = ClearHead(strlen(&text[index]));
167             m_edit.SetSel(length, length + 1);
168             m_edit.ReplaceSel(&text[index]);
169             break;
170         }
171
172         if(text[i] == 10)
173         {
174             if(text[i + 1] == 13)
175             {
176                 text[i] = 13;
177                 text[i + 1] = 10;
178                 c = text[i + 2];
179                 text[i + 2] = 0;
180                 length = ClearHead(strlen(&text[index]));
181                 m_edit.SetSel(length, length + 1);
182                 m_edit.ReplaceSel(&text[index]);
183                 text[i + 2] = c;
184                 
185                 index = i + 2;
186                 i = index;
187             }
188             else
189             {
190                 memcpy(buffer, &text[index], i - index);
191                 buffer[i - index] = 13;
192                 buffer[i - index + 1] = 10;
193                 buffer[i - index + 2] = 0;
194                 length = ClearHead(strlen(buffer));;
195                 m_edit.SetSel(length, length + 1);
196                 m_edit.ReplaceSel(buffer);
197
198                 index = i + 1;
199                 i = index;
200             }
201         }
202         else
203         {
204             // text[i] == 13
205             if(text[i + 1] == 10)
206             {
207                 i = i + 2;
208             }
209             else
210             {
211                 memcpy(buffer, &text[index], i - index);
212                 buffer[i - index] = 13;
213                 buffer[i - index + 1] = 10;
214                 buffer[i - index + 2] = 0;
215
216                 length = ClearHead(strlen(buffer));
217                 m_edit.SetSel(length, length + 1);
218                 m_edit.ReplaceSel(buffer);
219
220                 index = i + 1;
221                 i = index;
222             }
223         }
224     }
225
226     delete buffer;
227     delete text;
228 }
229
230 int CLogView::ClearHead(int textLength)
231 {
232     int length = m_length;//m_edit.GetWindowTextLength();
233     if(length + textLength > m_logLength)
234     {
235         /* Delete the beginning to free the space */
236         m_edit.SetSel(0, length + textLength - m_logLength);
237 //        m_edit.SetSel(0, -1);
238         m_edit.ReplaceSel("");
239     }
240
241     return m_length;//m_edit.GetWindowTextLength();    
242 }
243
244 void CLogView::OnChange() 
245 {
246     m_length = m_edit.GetWindowTextLength();
247 }