Update the changelog
[opencv] / apps / StereoGR / MainFrm.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////\r
2 //\r
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.\r
4 //\r
5 //  By downloading, copying, installing or using the software you agree to this license.\r
6 //  If you do not agree to this license, do not download, install,\r
7 //  copy or use the software.\r
8 //\r
9 //\r
10 //                        Intel License Agreement\r
11 //                For Open Source Computer Vision Library\r
12 //\r
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.\r
14 // Third party copyrights are property of their respective owners.\r
15 //\r
16 // Redistribution and use in source and binary forms, with or without modification,\r
17 // are permitted provided that the following conditions are met:\r
18 //\r
19 //   * Redistribution's of source code must retain the above copyright notice,\r
20 //     this list of conditions and the following disclaimer.\r
21 //\r
22 //   * Redistribution's in binary form must reproduce the above copyright notice,\r
23 //     this list of conditions and the following disclaimer in the documentation\r
24 //     and/or other materials provided with the distribution.\r
25 //\r
26 //   * The name of Intel Corporation may not be used to endorse or promote products\r
27 //     derived from this software without specific prior written permission.\r
28 //\r
29 // This software is provided by the copyright holders and contributors "as is" and\r
30 // any express or implied warranties, including, but not limited to, the implied\r
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.\r
32 // In no event shall the Intel Corporation or contributors be liable for any direct,\r
33 // indirect, incidental, special, exemplary, or consequential damages\r
34 // (including, but not limited to, procurement of substitute goods or services;\r
35 // loss of use, data, or profits; or business interruption) however caused\r
36 // and on any theory of liability, whether in contract, strict liability,\r
37 // or tort (including negligence or otherwise) arising in any way out of\r
38 // the use of this software, even if advised of the possibility of such damage.\r
39 //\r
40 //M*/// MainFrm.cpp : implementation of the CMainFrame class\r
41 //\r
42 \r
43 #include "stdafx.h"\r
44 #include "StereoGR.h"\r
45 \r
46 #include "MainFrm.h"\r
47 #include "StereoGRDoc.h"\r
48 #include "StereoGRView.h"\r
49 \r
50 #ifdef _DEBUG\r
51 #define new DEBUG_NEW\r
52 #undef THIS_FILE\r
53 static char THIS_FILE[] = __FILE__;\r
54 #endif\r
55 \r
56 /////////////////////////////////////////////////////////////////////////////\r
57 // CMainFrame\r
58 \r
59 IMPLEMENT_DYNAMIC(CMainFrame, CMDIFrameWnd)\r
60 \r
61 BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)\r
62         //{{AFX_MSG_MAP(CMainFrame)\r
63         ON_WM_CREATE()\r
64         ON_WM_TIMER()\r
65         ON_COMMAND(ID_DGRECOG, OnDgRecog)\r
66         ON_UPDATE_COMMAND_UI(ID_DGRECOG, OnUpdateDgrecog)\r
67         ON_WM_CLOSE()\r
68         //}}AFX_MSG_MAP\r
69 END_MESSAGE_MAP()\r
70 \r
71 static UINT indicators[] =\r
72 {\r
73         ID_SEPARATOR,           // status line indicator\r
74         ID_SEPARATOR,\r
75         ID_SEPARATOR,\r
76         ID_INDICATOR_NUM,\r
77         ID_INDICATOR_SCRL,\r
78 };\r
79 \r
80 /////////////////////////////////////////////////////////////////////////////\r
81 // CMainFrame construction/destruction\r
82 \r
83 CMainFrame::CMainFrame()\r
84 {\r
85     m_isTimerStarted = FALSE;\r
86     m_drawCount = 0;\r
87 }\r
88 \r
89 CMainFrame::~CMainFrame()\r
90 {\r
91 }\r
92 \r
93 int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)\r
94 {\r
95         if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)\r
96                 return -1;\r
97         \r
98         if (!m_wndToolBar.CreateEx(this) ||\r
99                 !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))\r
100         {\r
101                 TRACE0("Failed to create toolbar\n");\r
102                 return -1;      // fail to create\r
103         }\r
104         if (!m_wndDlgBar.Create(this, IDR_MAINFRAME, \r
105                 CBRS_ALIGN_TOP, AFX_IDW_DIALOGBAR))\r
106         {\r
107                 TRACE0("Failed to create dialogbar\n");\r
108                 return -1;              // fail to create\r
109         }\r
110 \r
111         if (!m_wndReBar.Create(this) ||\r
112                 !m_wndReBar.AddBar(&m_wndToolBar) ||\r
113                 !m_wndReBar.AddBar(&m_wndDlgBar))\r
114         {\r
115                 TRACE0("Failed to create rebar\n");\r
116                 return -1;      // fail to create\r
117         }\r
118 \r
119         if (!m_wndStatusBar.Create(this) ||\r
120                 !m_wndStatusBar.SetIndicators(indicators,\r
121                   sizeof(indicators)/sizeof(UINT)))\r
122         {\r
123                 TRACE0("Failed to create status bar\n");\r
124                 return -1;      // fail to create\r
125         }\r
126 \r
127         // TODO: Remove this if you don't want tool tips\r
128         m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() |\r
129                 CBRS_TOOLTIPS | CBRS_FLYBY);\r
130 \r
131     ShowControlBar(&m_wndToolBar, SW_SHOW, FALSE);\r
132     ShowControlBar(&m_wndDlgBar, SW_HIDE, FALSE);\r
133 \r
134     m_menu.LoadMenu(IDR_STEREOTYPE);\r
135     SetMenu(&m_menu);\r
136         return 0;\r
137 }\r
138 \r
139 BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)\r
140 {\r
141         if( !CMDIFrameWnd::PreCreateWindow(cs) )\r
142                 return FALSE;\r
143         // TODO: Modify the Window class or styles here by modifying\r
144         //  the CREATESTRUCT cs\r
145 \r
146         return TRUE;\r
147 }\r
148 \r
149 /////////////////////////////////////////////////////////////////////////////\r
150 // CMainFrame diagnostics\r
151 \r
152 #ifdef _DEBUG\r
153 void CMainFrame::AssertValid() const\r
154 {\r
155         CMDIFrameWnd::AssertValid();\r
156 }\r
157 \r
158 void CMainFrame::Dump(CDumpContext& dc) const\r
159 {\r
160         CMDIFrameWnd::Dump(dc);\r
161 }\r
162 \r
163 #endif //_DEBUG\r
164 \r
165 /////////////////////////////////////////////////////////////////////////////\r
166 // CMainFrame message handlers\r
167 \r
168 \r
169 CStatusBar* CMainFrame::GetStatusBar()\r
170 {\r
171     return &m_wndStatusBar;\r
172 }\r
173 \r
174 void CMainFrame::AddView(CStereoGRView *pView)\r
175 {\r
176     int id = pView->GetDocument()->GetDocType();\r
177     STEREOGRAPP(app)\r
178     app->SetView(id, pView);\r
179     if(!m_isTimerStarted)\r
180     {\r
181         SetTimer(1, 1, 0);\r
182         m_isTimerStarted = TRUE;\r
183     }\r
184 \r
185     m_drawCount = 0;\r
186 }\r
187 \r
188 void CMainFrame::OnTimer(UINT nIDEvent) \r
189 {\r
190     Update();\r
191         CMDIFrameWnd::OnTimer(nIDEvent);\r
192 }\r
193 \r
194 void CMainFrame::Update()\r
195 {\r
196     /* Count the frame rate */\r
197     float fps = CountRate();\r
198     CString str;\r
199     str.Format("%.1f frames per second", fps);\r
200         // This feature is disabled in the current release\r
201 //    m_wndStatusBar.SetPaneText(0, LPCTSTR(str));\r
202 \r
203     STEREOGRAPP(app)\r
204     app->Update();\r
205 }\r
206 \r
207 void CMainFrame::RemoveView(CStereoGRView *pView)\r
208 {\r
209     int id = pView->GetDocument()->GetDocType();\r
210     STEREOGRAPP(app);\r
211     app->SetView(id, 0);\r
212     int* viewIds = app->GetViewIDs();\r
213     viewIds[id] = 0;\r
214 \r
215     m_drawCount = 0;\r
216 }\r
217 \r
218 void CMainFrame::InitializeMenu()\r
219 {\r
220     CMenu* pMenu = GetMenu();\r
221     pMenu = pMenu->GetSubMenu(1);\r
222 //    pMenu->InsertMenu(0, MF_BYPOSITION | MF_STRING, 1100, "Test");\r
223 }\r
224 \r
225 \r
226 BOOL CMainFrame::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo) \r
227 {\r
228     CStereoGRApp* app = (CStereoGRApp*)AfxGetApp();\r
229     int* viewIds = app->GetViewIDs();\r
230     if(pHandlerInfo == 0)\r
231     {\r
232         if(nID >= ID_VIEW1 && nID <= ID_VIEW12)\r
233         {\r
234             int id = nID - ID_VIEW1;\r
235             if(nCode == CN_COMMAND)\r
236             {\r
237                 viewIds[id] = !viewIds[id];\r
238                 CStereoGRApp* app = (CStereoGRApp*)AfxGetApp();\r
239                 if(viewIds[id])\r
240                 {\r
241                     app->OpenView(id);\r
242                 }\r
243                 else\r
244                 {\r
245                     app->CloseView(id);\r
246                 }\r
247                 return TRUE;\r
248             }\r
249             else if(nCode == CN_UPDATE_COMMAND_UI)\r
250             {\r
251                 CCmdUI* pCmdUI = (CCmdUI*)pExtra;\r
252                 if((nID >= ID_VIEW1 && nID <= ID_VIEW6) || nID == ID_VIEW8 || (nID >= ID_VIEW10 && nID <= ID_VIEW12))\r
253                 {\r
254                     pCmdUI->Enable();\r
255                     pCmdUI->SetCheck(viewIds[id]);\r
256                 }\r
257                 else\r
258                 {\r
259                     pCmdUI->Enable(FALSE);\r
260                 }\r
261                 return TRUE;\r
262             }\r
263 \r
264         }\r
265     }\r
266 \r
267         return CMDIFrameWnd::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);\r
268 }\r
269 \r
270 \r
271 float CMainFrame::CountRate()\r
272 {\r
273     if(!m_drawCount)\r
274     {\r
275         m_time = GetTickCount();\r
276         m_drawCount++;\r
277         return 0;\r
278     }\r
279     \r
280     long time = GetTickCount();\r
281     if(time == m_time)\r
282     {\r
283         return 0;\r
284     }\r
285 \r
286     float fps = 1000.0f*m_drawCount/(GetTickCount() - m_time);\r
287     m_drawCount++;\r
288 \r
289     return fps;\r
290 }\r
291 \r
292 void CMainFrame::OnDgRecog() \r
293 {\r
294      CStereoGRApp* app = ((CStereoGRApp*)AfxGetApp());\r
295 \r
296      app->m_doRecog = !app->m_doRecog;\r
297 \r
298      //CButton* pbut = (CButton*)GetDlgItem( ID_DGRECOG );\r
299      //pbut->SetCheck( app->m_doRecog );  \r
300 \r
301         // TODO: Add your command handler code here                             \r
302 }\r
303 \r
304 void CMainFrame::OnUpdateDgrecog(CCmdUI* pCmdUI) \r
305 {\r
306     CStereoGRApp* app = ((CStereoGRApp*)AfxGetApp());\r
307 \r
308     //CButton* pbut = (CButton*)pCmdUI;\r
309     pCmdUI->SetCheck( app->m_doRecog );  \r
310     \r
311         // TODO: Add your command update UI handler code here\r
312         \r
313 }\r
314 \r
315 void CMainFrame::OnClose() \r
316 {\r
317         STEREOGRAPP(app)\r
318 \r
319         for(int i = 0; i < VIEWS_COUNT; i++)\r
320         {\r
321                 if(app->GetView(i) == 0)\r
322                         continue;\r
323 \r
324                 CString str;\r
325                 str.Format("Settings\\StereoGR\\View%d", i);\r
326                 int x = 0, y = 0;\r
327                 RECT viewRect;\r
328                 RECT clientRect;\r
329                 GetClRect(&clientRect);\r
330                 ClientToScreen(&clientRect);\r
331                 app->GetView(i)->GetParent()->GetWindowRect(&viewRect);\r
332                 x = viewRect.left - clientRect.left;\r
333                 y = viewRect.top - clientRect.top;\r
334                 app->WriteProfileInt(LPCTSTR(str), "left", x);\r
335                 app->WriteProfileInt(LPCTSTR(str), "top", y);\r
336         }\r
337                 \r
338         app->SaveSettings();\r
339         CMDIFrameWnd::OnClose();\r
340 }\r
341 \r
342 BOOL CMainFrame::DestroyWindow() \r
343 {\r
344         \r
345         \r
346         return CMDIFrameWnd::DestroyWindow();\r
347 }\r
348 \r
349 void CMainFrame::GetClRect( LPRECT lpRect ) const\r
350 {\r
351         CWnd::GetWindowRect(lpRect);\r
352         CRect toolbarRect;\r
353         m_wndToolBar.GetWindowRect(&toolbarRect);\r
354         lpRect->top += toolbarRect.Size().cy;\r
355 }\r