FaceTracker2
MainFrm.cpp
1 // MainFrm.cpp : implementation of the CMainFrame class
2 //
3 
4 #include "stdafx.h"
5 #include "VisionExample.h"
6 #include "VisionExampleDoc.h"
7 #include "MainFrm.h"
8 
9 #include "videoInput.h"
10 
11 #ifdef _DEBUG
12 #define new DEBUG_NEW
13 #undef THIS_FILE
14 static char THIS_FILE[] = __FILE__;
15 #endif
16 
17 /////////////////////////////////////////////////////////////////////////////
18 // CMainFrame
19 
20 IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
21 
22 BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
23  //{{AFX_MSG_MAP(CMainFrame)
24  // NOTE - the ClassWizard will add and remove mapping macros here.
25  // DO NOT EDIT what you see in these blocks of generated code !
26  ON_WM_CREATE()
27  ON_MESSAGE(WM_MYMESSAGE , OnChangeStatusBar) //message for displaying the status bar text
28  //}}AFX_MSG_MAP
29 
30  ON_NOTIFY_EX_RANGE(TTN_NEEDTEXT, 0, 0xFFFF, OnToolTipText)
31 END_MESSAGE_MAP()
32 
33 static UINT indicators[] =
34 {
35  ID_SEPARATOR, // status line indicator
36 };
37 
38 /////////////////////////////////////////////////////////////////////////////
39 // CMainFrame construction/destruction
40 
41 CMainFrame::CMainFrame()
42 {
43  // TODO: add member initialization code here
44  m_comboBoxConfigFile = NULL;
45  m_comboBoxCamResolution = NULL;
46  m_comboBoxCamDevice = NULL;
47  m_comboBoxMaxFaces = NULL;
48 }
49 
50 CMainFrame::~CMainFrame()
51 {
52 
53 }
54 
55 // Returns pointer to the document object.
56 // To know more about document object in MFC (Microsoft Foundation Classess)
57 // refer to the MSDN documentation.
58 CVisionExampleDoc* CMainFrame::GetDocument()
59 {
60  POSITION pos = AfxGetApp()->GetFirstDocTemplatePosition();
61  CDocTemplate* pDocTemplate = AfxGetApp()->GetNextDocTemplate(pos);
62  pos = pDocTemplate->GetFirstDocPosition();
63  return (CVisionExampleDoc*)pDocTemplate->GetNextDoc(pos);
64 }
65 
66 /**
67 * Returns pointer to OpenGL rendering window.
68 */
69 
70 COpenGLWnd* CMainFrame::GetOpenGLWnd()
71 {
72  CVisionExampleDoc *pDoc = (CVisionExampleDoc*)GetDocument();
73  POSITION pos = pDoc->GetFirstViewPosition();
74  return (COpenGLWnd*)pDoc->GetNextView(pos);
75 }
76 
77 
78 int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
79 {
80  if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
81  return -1;
82 
83  if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT | TBSTYLE_TOOLTIPS | TBSTYLE_AUTOSIZE,
84  WS_CHILD | WS_VISIBLE | CBRS_ALIGN_TOP /*| CBRS_TOOLTIPS*/ | CBRS_FLYBY) ||
85  !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
86  {
87  TRACE0("Failed to create toolbar\n");
88  return -1; // fail to create
89  }
90 
91  if (!m_wndReBar.Create(this) ||
92  !m_wndDlgBar.Create(this, IDD_DIALOGBAR, CBRS_ALIGN_TOP | CBRS_TOOLTIPS | CBRS_FLYBY, IDD_DIALOGBAR) ||
93  !m_wndReBar.AddBar(&m_wndDlgBar) ||
94  !m_wndReBar.AddBar(&m_wndToolBar))
95  {
96  TRACE0("Failed to create rebar\n");
97  return -1; // fail to create
98  }
99 
100  if (!m_wndStatusBar.Create(this) ||
101  !m_wndStatusBar.SetIndicators(indicators,
102  sizeof(indicators)/sizeof(UINT)))
103  {
104  TRACE0("Failed to create status bar\n");
105  return -1; // fail to create
106  }
107 
108  m_comboBoxConfigFile = (CComboBox *)m_wndDlgBar.GetDlgItem(ID_COMBO_CONFIG_FILE);
109  m_comboBoxCamResolution = (CComboBox *)m_wndDlgBar.GetDlgItem(ID_COMBO_CAM_RESOLUTION);
110  m_comboBoxCamDevice = (CComboBox *)m_wndDlgBar.GetDlgItem(ID_COMBO_CAM_DEVICE);
111  m_comboBoxMaxFaces = (CComboBox *)m_wndDlgBar.GetDlgItem(ID_COMBO_MAX_FACES);
112 
113 
114  return 0;
115 }
116 
117 BOOL CMainFrame::OnToolTipText(UINT, NMHDR* pNMHDR, LRESULT* pResult)
118 {
119  ASSERT(pNMHDR->code == TTN_NEEDTEXT);
120 
121  TOOLTIPTEXT* pTTT = (TOOLTIPTEXT*)pNMHDR;
122  TCHAR szFullText[512];
123  CString strTipText;
124  UINT nID = pNMHDR->idFrom;
125 
126  if (pTTT->uFlags & TTF_IDISHWND)
127  {
128  // idFrom is actually the HWND of the tool
129  nID = ::GetDlgCtrlID((HWND)nID);
130  }
131 
132  if (nID != 0) // will be zero on a separator
133  {
134  strTipText.LoadString(nID);
135  lstrcpyn(pTTT->szText, strTipText, sizeof(pTTT->szText));
136 
137  *pResult = 0;
138 
139  // bring the tooltip window above other popup windows
140  ::SetWindowPos(pNMHDR->hwndFrom, HWND_TOP, 0, 0, 0, 0,
141  SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOMOVE | SWP_NOOWNERZORDER);
142 
143  return TRUE;
144  }
145 
146  return FALSE;
147 }
148 
149 //window size x+20 y+110
150 #define xsize 1100
151 #define ysize 720
152 
153 BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
154 {
155  cs.cx=xsize;
156  cs.cy=ysize;
157  /*
158  cs.y = ::GetSystemMetrics(SM_CYSCREEN) - ysize - (::GetSystemMetrics(SM_CYSCREEN) / 30);
159  if(cs.y < 0 ) cs.y = 0;
160  cs.x = 0;
161  */
162  cs.x = (::GetSystemMetrics(SM_CXSCREEN) - xsize) / 2;
163  cs.y = (::GetSystemMetrics(SM_CYSCREEN) - ysize) / 2;
164 
165  if( !CFrameWnd::PreCreateWindow(cs) )
166  return FALSE;
167  // TODO: Modify the Window class or styles here by modifying
168  // the CREATESTRUCT cs
169 
170  return TRUE;
171 }
172 LRESULT CMainFrame::OnChangeStatusBar(WPARAM wParam, LPARAM lParam)
173 {
174  if(wParam != NULL)
175  {
176  CString* strStatusText = (CString*) wParam;
177  //Update the statusbar
178  if (m_wndStatusBar) m_wndStatusBar.SetPaneText( 0, *strStatusText, TRUE );
179  //free memory
180  delete strStatusText;
181  }
182  return 0;
183 }
184 
185 
186 /////////////////////////////////////////////////////////////////////////////
187 // CMainFrame diagnostics
188 
189 #ifdef _DEBUG
190 void CMainFrame::AssertValid() const
191 {
192  CFrameWnd::AssertValid();
193 }
194 
195 void CMainFrame::Dump(CDumpContext& dc) const
196 {
197  CFrameWnd::Dump(dc);
198 }
199 
200 #endif //_DEBUG
201 
202 void CMainFrame::ShowAvailConfigFiles(char *path,char *currentChoice)
203 {
204  HANDLE hSearch;
205  WIN32_FIND_DATA FileData;
206  CString dupa;
207  CString temp;
208 
209  temp = currentChoice;
210 
211  while (m_comboBoxConfigFile->GetCount() >0) m_comboBoxConfigFile->DeleteString(0);
212 
213  dupa.Format("%s", path);
214  if (dupa == "") return;
215  dupa += "\\*.cfg";
216  hSearch = FindFirstFile(dupa, &FileData);
217 
218  if (hSearch == INVALID_HANDLE_VALUE){
219  LPVOID lpMsgBuf;
220  FormatMessage(
221  FORMAT_MESSAGE_ALLOCATE_BUFFER |
222  FORMAT_MESSAGE_FROM_SYSTEM |
223  FORMAT_MESSAGE_IGNORE_INSERTS,
224  NULL,
225  GetLastError(),
226  0, // Default language
227  (LPTSTR) &lpMsgBuf,
228  0,
229  NULL
230  );
231  // Free the buffer.
232  LocalFree( lpMsgBuf );
233  }
234  int iIndex = 0;
235 
236  do {
237  if (!(FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)){
238  std::string cfgFileName = FileData.cFileName;
239  if(cfgFileName.find("NeuralNet.cfg") == std::string::npos)
240  m_comboBoxConfigFile->InsertString(iIndex, FileData.cFileName);
241  if (!temp.CompareNoCase(FileData.cFileName))
242  m_comboBoxConfigFile->SetCurSel(iIndex);
243  iIndex++;
244  }
245  }while (FindNextFile(hSearch, &FileData));
246  m_comboBoxConfigFile->EnableWindow(m_comboBoxConfigFile->GetCount());
247 }
248 
249 void CMainFrame::ShowAvailCamResolutions()
250 {
251  while (m_comboBoxCamResolution->GetCount() > 0) m_comboBoxCamResolution->DeleteString(0);
252 
253  int iIndex = 0;
254 
255  m_comboBoxCamResolution->InsertString(iIndex++, "320x240");
256  m_comboBoxCamResolution->InsertString(iIndex++, "640x480");
257  m_comboBoxCamResolution->InsertString(iIndex++, "800x600");
258  m_comboBoxCamResolution->InsertString(iIndex++, "1280x720");
259  m_comboBoxCamResolution->InsertString(iIndex++, "1920x1080");
260  m_comboBoxCamResolution->InsertString(iIndex++, "2560x1440");
261  m_comboBoxCamResolution->InsertString(iIndex++, "4096x2160");
262 
263  m_comboBoxCamResolution->SetCurSel(2);
264 
265  m_comboBoxCamResolution->EnableWindow(m_comboBoxCamResolution->GetCount());
266 }
267 
268 void CMainFrame::ShowAvailCamDevices()
269 {
270  while (m_comboBoxCamDevice->GetCount() > 0) m_comboBoxCamDevice->DeleteString(0);
271 
272  int n = videoInput::listDevices();
273 
274  for (int i = 0; i < n; ++i) {
275  m_comboBoxCamDevice->InsertString(i, videoInput::getDeviceName(i));
276  }
277 
278  m_comboBoxCamDevice->SetCurSel(0);
279 
280  m_comboBoxCamDevice->EnableWindow(m_comboBoxCamDevice->GetCount());
281 }
282 
283 void CMainFrame::ShowAvailMaxFaces()
284 {
285  while (m_comboBoxMaxFaces->GetCount() > 0) m_comboBoxMaxFaces->DeleteString(0);
286 
287  for (int i = 0; i < 10; i++)
288  m_comboBoxMaxFaces->InsertString(i, std::to_string(i+1).c_str());
289 
290  m_comboBoxMaxFaces->SetCurSel(2);
291 
292  m_comboBoxMaxFaces->EnableWindow(m_comboBoxMaxFaces->GetCount());
293 }
294 
295 void CMainFrame::GetConfigFilename(CString& strName)
296 {
297  m_comboBoxConfigFile = (CComboBox *)m_wndDlgBar.GetDlgItem(ID_COMBO_CONFIG_FILE);
298  m_comboBoxConfigFile->GetLBText(m_comboBoxConfigFile->GetCurSel(), strName);
299 }
300 
301 void CMainFrame::GetCameraResolution(CString& strName)
302 {
303  m_comboBoxCamResolution = (CComboBox *)m_wndDlgBar.GetDlgItem(ID_COMBO_CAM_RESOLUTION);
304  m_comboBoxCamResolution->GetLBText(m_comboBoxCamResolution->GetCurSel(), strName);
305 }
306 
307 int CMainFrame::GetCameraDevice()
308 {
309  m_comboBoxCamDevice = (CComboBox *)m_wndDlgBar.GetDlgItem(ID_COMBO_CAM_DEVICE);
310  return m_comboBoxCamDevice ? m_comboBoxCamDevice->GetCurSel() : 0;
311 }
312 
313 int CMainFrame::GetMaxFaces()
314 {
315  m_comboBoxMaxFaces = (CComboBox *)m_wndDlgBar.GetDlgItem(ID_COMBO_MAX_FACES);
316  return m_comboBoxMaxFaces ? m_comboBoxMaxFaces->GetCurSel() + 1 : 1;
317 }
318 
319 /////////////////////////////////////////////////////////////////////////////
320 // CMainFrame message handlers
321 
The document.
The OpenGL-enabled window with rendering and basic interaction.