Move the sources to trunk
[opencv] / apps / LkDemo / MainFrm.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*/// MainFrm.cpp : implementation of the CMainFrame class
41 //
42
43 #include "stdafx.h"
44 #include "LkDemo.h"
45
46 #include "MainFrm.h"
47
48 #ifdef _DEBUG
49 #define new DEBUG_NEW
50 #undef THIS_FILE
51 static char THIS_FILE[] = __FILE__;
52 #endif
53
54 /////////////////////////////////////////////////////////////////////////////
55 // CMainFrame
56
57 IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
58
59 BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
60         //{{AFX_MSG_MAP(CMainFrame)
61         ON_WM_CREATE()
62         ON_UPDATE_COMMAND_UI(ID_CAPTURE, OnUpdateCapture)
63     ON_COMMAND(ID_CAPTURE, OnCapture)
64         ON_COMMAND(ID_TRACK, OnTrack)
65         ON_UPDATE_COMMAND_UI(ID_TRACK, OnUpdateTrack)
66         ON_COMMAND(ID_NIGHTMODE, OnNightmode)
67         ON_UPDATE_COMMAND_UI(ID_NIGHTMODE, OnUpdateNightmode)
68         ON_COMMAND(ID_CAPOPTIONS, OnCapOptions)
69     ON_COMMAND(ID_CAPFORMAT, OnVideoFormat)
70         //}}AFX_MSG_MAP
71 END_MESSAGE_MAP()
72
73 static UINT indicators[] =
74 {
75         ID_SEPARATOR,           // status line indicator
76         ID_INDICATOR_CAPS,
77         ID_INDICATOR_NUM,
78         ID_INDICATOR_SCRL,
79 };
80
81 /////////////////////////////////////////////////////////////////////////////
82 // CMainFrame construction/destruction
83
84 CMainFrame::CMainFrame()
85 {
86         
87 }
88
89 CMainFrame::~CMainFrame()
90 {
91 }
92
93 int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
94 {
95         if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
96                 return -1;
97         
98         if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
99                 | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
100                 !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
101         {
102                 TRACE0("Failed to create toolbar\n");
103                 return -1;      // fail to create
104         }
105
106         if (!m_wndStatusBar.Create(this) ||
107                 !m_wndStatusBar.SetIndicators(indicators,
108                   sizeof(indicators)/sizeof(UINT)))
109         {
110                 TRACE0("Failed to create status bar\n");
111                 return -1;      // fail to create
112         }
113
114         // TODO: Delete these three lines if you don't want the toolbar to
115         //  be dockable
116         m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
117         EnableDocking(CBRS_ALIGN_ANY);
118         DockControlBar(&m_wndToolBar);
119
120         return 0;
121 }
122
123 BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
124 {
125         if( !CFrameWnd::PreCreateWindow(cs) )
126                 return FALSE;
127         return TRUE;
128 }
129
130 CLkDemoView* CMainFrame::GetCameraView()
131 {
132     return (CLkDemoView*)GetActiveView();
133 }
134
135
136 /////////////////////////////////////////////////////////////////////////////
137 // CMainFrame message handlers
138
139 void CMainFrame::OnUpdateCapture(CCmdUI* pCmdUI) 
140 {
141     CLkDemoView* view = GetCameraView();
142     bool enable = false;
143     bool press = false;
144
145     if( view )
146     {
147         enable = view->Camera().IsInitialized();
148         press = view->Camera().IsRunning();
149     }
150
151     pCmdUI->Enable( enable );
152     pCmdUI->SetCheck( press );
153 }
154
155 void CMainFrame::OnCapture() 
156 {
157     CLkDemoView* view = GetCameraView();
158
159     if( view && view->Camera().IsInitialized())
160     {
161         if( view->Camera().IsRunning())
162         {
163             view->Camera().Stop();
164         }
165         else
166         {
167             view->Camera().Start();
168         }
169     }    
170 }
171
172 void CMainFrame::OnTrack() 
173 {
174     CLkDemoView* view = GetCameraView();
175
176     if( view && view->Camera().IsInitialized())
177     {
178         if( view->IsTracked())
179         {
180             view->StopTracking();
181         }
182         else
183         {
184             view->StartTracking();
185         }
186     }
187 }
188
189 void CMainFrame::OnUpdateTrack(CCmdUI* pCmdUI) 
190 {
191     bool enable = false;
192     bool press = false;
193     CLkDemoView* view = GetCameraView();
194
195     if( view && view->Camera().IsInitialized())
196     {
197         enable = true;
198         press = view->IsTracked();
199     }
200
201     pCmdUI->Enable( enable );
202     pCmdUI->SetCheck( press );
203 }
204
205 void CMainFrame::OnNightmode() 
206 {
207     CLkDemoView* view = GetCameraView();
208
209     if( view )
210     {
211         view->SetNightMode( !view->IsNightMode() );
212     }
213 }
214
215 void CMainFrame::OnUpdateNightmode(CCmdUI* pCmdUI) 
216 {
217     bool enable = false;
218     bool press = false;
219     CLkDemoView* view = GetCameraView();
220
221     if( view )
222     {
223         enable = view->IsTracked();
224         press = enable && view->IsNightMode();
225     }
226
227     pCmdUI->Enable( enable );
228     pCmdUI->SetCheck( press );
229 }
230
231
232 void CMainFrame::OnCapOptions() 
233 {
234     CLkDemoView* view = GetCameraView();
235
236     if( view )
237     {
238         view->Camera().VideoSourceDlg();
239         view->InvalidateRect(0);
240     }
241 }
242
243
244 void CMainFrame::OnVideoFormat() 
245 {
246     CLkDemoView* view = GetCameraView();
247
248     if( view )
249     {
250         view->Camera().VideoFormatDlg();
251         view->InvalidateRect(0);
252     }
253 }