1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2002, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistributions of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
22 // * Redistributions 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.
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.
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.
42 #pragma warning(disable: 4786)
43 #include "FilenamesDialog.h"
45 template<class T> static inline void AddTemplate(char *&p, const T &item)
51 static void AddTemplate(char *&p, const wchar_t *item)
53 size_t sz = (wcslen(item) + 1) * 2;
58 void AddItem(char *&p, const DLGITEMTEMPLATE &item_tmpl, WORD item_class, const wchar_t *title)
60 // make sure the beginning of each DLGITEMTEMPLATE is DWORD aligned.
63 AddTemplate(p, item_tmpl);
64 AddTemplate(p, (WORD)0xffff); // item class
65 AddTemplate(p, item_class);
66 AddTemplate(p, title);
67 AddTemplate(p, (WORD)0); // creation data
74 static BOOL CALLBACK DlgProc(HWND dlg, UINT msg, WPARAM w, LPARAM l);
75 static void LoadCameraIntrinsicsFilenames(std::vector<std::string> &filenames);
76 static void SaveCameraIntrinsicsFilenames(const std::vector<std::string> &filenames);
78 bool FilenamesDialog(HWND parent, int num_filenames, std::vector<std::string> &filenames)
82 if (num_filenames > 20)
85 if (filenames.size() == 0)
86 LoadCameraIntrinsicsFilenames(filenames);
88 filenames.resize(num_filenames);
91 for (i = 0; i < num_filenames; i++)
92 name_size += filenames[i].length() + 1;
94 int num_items = num_filenames*3 + 2;
96 int height = 35 + num_filenames * 20;
97 size_t sz = sizeof(DLGTEMPLATE)+90 + num_items * (sizeof(DLGITEMTEMPLATE)+18) + name_size*2;
98 char *dlgtmpl = (char *)malloc(sz);
100 DLGTEMPLATE dlgtemplate = { WS_POPUP|WS_CAPTION|DS_MODALFRAME|DS_SETFONT|DS_SETFOREGROUND, 0, num_items, 0, 0, width, height };
101 AddTemplate(p, dlgtemplate);
102 AddTemplate(p, (WORD)0); // menu
103 AddTemplate(p, (WORD)0); // window class
104 AddTemplate(p, L"Camera Intrinsics Filenames"); // title
105 AddTemplate(p, (WORD)8); // font size
106 AddTemplate(p, L"MS Sans Serif"); // font
111 for (i = 0; i < num_filenames; i++)
113 DLGITEMTEMPLATE label = { SS_LEFT|WS_CHILD|WS_VISIBLE, 0, 10, 12+i*20, 40, 13, ID_LABEL+i };
114 DLGITEMTEMPLATE edit = { ES_LEFT|WS_BORDER|WS_TABSTOP|WS_CHILD|WS_VISIBLE, 0, 50, 10+i*20, width-75, 13, ID_EDIT+i };
115 DLGITEMTEMPLATE browse = { BS_PUSHBUTTON|WS_TABSTOP|WS_CHILD|WS_VISIBLE, 0, width-25, 10+i*20, 13, 13, ID_BROWSE+i };
116 wchar_t text[MAX_PATH];
117 swprintf(text, L"Camera %d", i+1);
118 AddItem(p, label, STATIC, text);
119 mbstowcs(text, filenames[i].c_str(), sizeof(text));
120 AddItem(p, edit, EDIT, text);
121 AddItem(p, browse, BUTTON, L"...");
123 DLGITEMTEMPLATE ok = { BS_DEFPUSHBUTTON|WS_TABSTOP|WS_CHILD|WS_VISIBLE, 0, 40, height - 20, 50, 14, IDOK };
124 DLGITEMTEMPLATE cancel = { BS_PUSHBUTTON|WS_TABSTOP|WS_CHILD|WS_VISIBLE, 0, width-90, height - 20, 50, 14, IDCANCEL };
125 AddItem(p, ok, BUTTON, L"OK");
126 AddItem(p, cancel, BUTTON, L"Cancel");
128 int r = DialogBoxIndirectParam(0, (DLGTEMPLATE *)dlgtmpl, parent, DlgProc, (LPARAM)&filenames);
132 SaveCameraIntrinsicsFilenames(filenames);
137 static BOOL CALLBACK DlgProc(HWND dlg, UINT msg, WPARAM w, LPARAM l)
139 char filename[MAX_PATH];
143 SetWindowLong(dlg, GWL_USERDATA, l);
147 int control = LOWORD(w);
150 std::vector<std::string> *filenames = (std::vector<std::string> *)GetWindowLong(dlg, GWL_USERDATA);
151 for (int i = 0; i < filenames->size(); i++)
153 GetWindowText(GetDlgItem(dlg, ID_EDIT+i), filename, sizeof(filename));
154 (*filenames)[i] = filename;
157 EndDialog(dlg, IDOK);
160 else if (control == IDCANCEL)
162 EndDialog(dlg, IDCANCEL);
165 else if (control >= ID_BROWSE && control < ID_BROWSE+20)
167 const char filter[] = "*.txt\0*.txt\0All files\0*.*\0";
168 int index = control-ID_BROWSE;
169 HWND edit = GetDlgItem(dlg, ID_EDIT+index);
170 GetWindowText(edit, filename, sizeof(filename));
171 OPENFILENAME ofn = { sizeof(OPENFILENAME), dlg, 0, filter, NULL, 0, 1,
172 filename, sizeof(filename), NULL, 0, NULL, "Camera Intrinsics File",
173 OFN_PATHMUSTEXIST|OFN_FILEMUSTEXIST|OFN_NOCHANGEDIR|OFN_HIDEREADONLY };
174 if (GetOpenFileName(&ofn))
175 SetWindowText(edit, filename);
188 AutoCloseKey(HKEY *pkey) : m_pkey(pkey) { };
193 RegCloseKey(*m_pkey);
199 #define AUTO_CLOSE_KEY(f) AutoCloseKey AutoCloseKey##f(&f)
201 static const char key_name[] = "Software\\Intel\\VAI\\3d Tracker\\";
204 static void LoadCameraIntrinsicsFilenames(std::vector<std::string> &filenames)
208 HKEY key = 0; AUTO_CLOSE_KEY(key);
209 r = RegOpenKeyEx(HKEY_LOCAL_MACHINE, key_name, 0, KEY_READ, &key);
210 if (r != ERROR_SUCCESS)
214 DWORD size = sizeof(names);
215 r = RegQueryValueEx(key, "CameraIntrinsicsFilenames", 0, NULL, (BYTE *)names, &size);
216 if (r != ERROR_SUCCESS)
220 const char *p = names;
223 filenames.push_back(p);
228 static void SaveCameraIntrinsicsFilenames(const std::vector<std::string> &filenames)
232 HKEY key = 0; AUTO_CLOSE_KEY(key);
233 r = RegCreateKeyEx(HKEY_LOCAL_MACHINE, key_name, 0, NULL, 0, KEY_WRITE, NULL, &key, NULL);
234 if (r != ERROR_SUCCESS)
238 // Warning: no overflow protection
240 for (int i = 0; i < filenames.size(); i++)
242 strcpy(p, filenames[i].c_str());
243 p += filenames[i].length() + 1;
247 DWORD size = p - names + 1;
249 RegSetValueEx(key, "CameraIntrinsicsFilenames", 0, REG_MULTI_SZ, (BYTE *)names, size);