Move the sources to trunk
[opencv] / filters / Tracker3dFilter / src / FilenamesDialog.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) 2002, 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 //   * Redistributions of source code must retain the above copyright notice,
20 //     this list of conditions and the following disclaimer.
21 //
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.
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*/
41
42 #pragma warning(disable: 4786)
43 #include "FilenamesDialog.h"
44
45 template<class T> static inline void AddTemplate(char *&p, const T &item)
46 {
47     *(T *)p = item;
48     p += sizeof(T);
49 }
50
51 static void AddTemplate(char *&p, const wchar_t *item)
52 {
53     size_t sz = (wcslen(item) + 1) * 2;
54     memcpy(p, item, sz);
55     p += sz;
56 }
57
58 void AddItem(char *&p, const DLGITEMTEMPLATE &item_tmpl, WORD item_class, const wchar_t *title)
59 {
60     // make sure the beginning of each DLGITEMTEMPLATE is DWORD aligned.
61     if ((int)p & 2)
62         p += 2;
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
68 }
69
70 #define ID_LABEL  20
71 #define ID_EDIT   40
72 #define ID_BROWSE 60
73
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);
77
78 bool FilenamesDialog(HWND parent, int num_filenames, std::vector<std::string> &filenames)
79 {
80     int i;
81
82     if (num_filenames > 20)
83         return false;
84
85     if (filenames.size() == 0)
86         LoadCameraIntrinsicsFilenames(filenames);
87
88     filenames.resize(num_filenames);
89
90     int name_size = 0;
91     for (i = 0; i < num_filenames; i++)
92         name_size += filenames[i].length() + 1;
93
94     int num_items = num_filenames*3 + 2;
95     int width = 200;
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);
99     char *p = dlgtmpl;
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
107
108 #define BUTTON 0x80
109 #define EDIT   0x81
110 #define STATIC 0x82
111     for (i = 0; i < num_filenames; i++)
112     {
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"...");
122     }
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");
127
128     int r = DialogBoxIndirectParam(0, (DLGTEMPLATE *)dlgtmpl, parent, DlgProc, (LPARAM)&filenames);
129     free(dlgtmpl);
130
131     if (r == IDOK)
132         SaveCameraIntrinsicsFilenames(filenames);
133
134     return r == IDOK;
135 }
136
137 static BOOL CALLBACK DlgProc(HWND dlg, UINT msg, WPARAM w, LPARAM l)
138 {
139     char filename[MAX_PATH];
140     switch (msg)
141     {
142         case WM_INITDIALOG:
143             SetWindowLong(dlg, GWL_USERDATA, l);
144             return true;
145
146         case WM_COMMAND:
147             int control = LOWORD(w);
148             if (control == IDOK)
149             {
150                 std::vector<std::string> *filenames = (std::vector<std::string> *)GetWindowLong(dlg, GWL_USERDATA);
151                 for (int i = 0; i < filenames->size(); i++)
152                 {
153                     GetWindowText(GetDlgItem(dlg, ID_EDIT+i), filename, sizeof(filename));
154                     (*filenames)[i] = filename;
155                 }
156
157                 EndDialog(dlg, IDOK);
158                 return true;
159             }
160             else if (control == IDCANCEL)
161             {
162                 EndDialog(dlg, IDCANCEL);
163                 return true;
164             }
165             else if (control >= ID_BROWSE && control < ID_BROWSE+20)
166             {
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);
176                 return true;
177             }
178     }
179     return false;
180 }
181
182
183 class AutoCloseKey
184 {
185     HKEY *m_pkey;
186
187 public:
188     AutoCloseKey(HKEY *pkey) : m_pkey(pkey) { };
189     ~AutoCloseKey()
190     {
191         if (*m_pkey != 0)
192         {
193             RegCloseKey(*m_pkey);
194             *m_pkey = 0;
195         }
196     };
197 };
198
199 #define AUTO_CLOSE_KEY(f) AutoCloseKey AutoCloseKey##f(&f)
200
201 static const char key_name[] = "Software\\Intel\\VAI\\3d Tracker\\";
202
203
204 static void LoadCameraIntrinsicsFilenames(std::vector<std::string> &filenames)
205 {
206     LONG r;
207     
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)
211         return;
212
213     char names[2000];
214     DWORD size = sizeof(names);
215     r = RegQueryValueEx(key, "CameraIntrinsicsFilenames", 0, NULL, (BYTE *)names, &size);
216     if (r != ERROR_SUCCESS)
217         return;
218
219     filenames.resize(0);
220     const char *p = names;
221     while (*p != '\0')
222     {
223         filenames.push_back(p);
224         p += strlen(p) + 1;
225     }
226 }
227
228 static void SaveCameraIntrinsicsFilenames(const std::vector<std::string> &filenames)
229 {
230     LONG r;
231     
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)
235         return;
236
237     char names[2000];
238     // Warning: no overflow protection
239     char *p = names;
240     for (int i = 0; i < filenames.size(); i++)
241     {
242         strcpy(p, filenames[i].c_str());
243         p += filenames[i].length() + 1;
244     }
245     *p = '\0';
246
247     DWORD size = p - names + 1;
248
249     RegSetValueEx(key, "CameraIntrinsicsFilenames", 0, REG_MULTI_SZ, (BYTE *)names, size);
250 }