2
2
#define BLIT_IT_NATIVES_DLL_COMPILE
3
3
4
4
#include " natives.h"
5
+
5
6
#include " threadutils.h"
6
7
8
+ #include < list>
9
+ #include < string>
10
+
7
11
#include < Windows.h>
8
12
9
13
using namespace std ;
10
14
11
- ChooseFileSuccessCallback _chooseFileSuccessCallback = NULL ;
12
- ChooseFileCancelledCallback _chooseFileCancelledCallback = NULL ;
15
+ struct ChooseFileContext
16
+ {
17
+ ChooseFileSuccessCallback _chooseFileSuccessCallback;
18
+ ChooseFileCancelledCallback _chooseFileCancelledCallback;
19
+ pthread_t file_picker_thread;
20
+ list<wstring> filetype_names;
21
+ list<wstring> filetype_extensions;
22
+ };
13
23
14
- pthread_mutex_t file_picker_mutex = PTHREAD_MUTEX_INITIALIZER;
15
- pthread_t file_picker_thread;
16
- bool file_picker_running = false ;
17
24
18
- void * LoadGcodeAsync (void * arg)
25
+ void * FilePickerThread (void * arg)
19
26
{
27
+ ChooseFileContext* cfc = (ChooseFileContext*)arg;
28
+
20
29
const int buf_len = 260 ;
21
- TCHAR cwd[buf_len] = { 0 };
30
+ // TCHAR cwd[buf_len] = { 0 };
22
31
23
- GetCurrentDirectory (buf_len, cwd);
32
+ // GetCurrentDirectory(buf_len, cwd);
24
33
25
34
OPENFILENAME ofn; // common dialog box structure
26
35
TCHAR szFile[buf_len]; // buffer for file name
27
36
// HWND hwnd; // owner window
28
37
// HANDLE hf; // file handle
29
- TCHAR filter[64 ] = TEXT (" G-code\0 *.gcode\0 All\0 *.*\0 " );
38
+
39
+
40
+
41
+ // string test("G-code\0*.gcode\0All\0*.*\0");
42
+ wstring filetype_filters;
43
+ list<wstring>::const_iterator ext_itr = cfc->filetype_extensions .begin ();
44
+ for (list<wstring>::const_iterator name_itr = cfc->filetype_names .begin (); name_itr != cfc->filetype_names .end (); ++name_itr)
45
+ {
46
+ filetype_filters += *name_itr;
47
+ filetype_filters.push_back (' \0 ' );
48
+ filetype_filters += L" *." ;
49
+ filetype_filters += *ext_itr;
50
+ filetype_filters.push_back (' \0 ' );
51
+ ++ext_itr;
52
+ }
53
+ filetype_filters += L" All" ;
54
+ filetype_filters.push_back (' \0 ' );
55
+ filetype_filters += L" *.*" ;
56
+ filetype_filters.push_back (' \0 ' );
57
+
58
+
59
+ // TCHAR filter[64] = TEXT(((const char*)test.c_str()));
60
+
61
+ TCHAR* filter = (TCHAR*)filetype_filters.c_str ();
62
+
63
+
64
+
30
65
31
66
// Initialize OPENFILENAME
32
67
ZeroMemory (&ofn, sizeof (ofn));
@@ -44,46 +79,55 @@ void* LoadGcodeAsync(void* arg)
44
79
ofn.lpstrInitialDir = NULL ;
45
80
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR | OFN_READONLY;
46
81
47
- SetCurrentDirectory (cwd);
82
+ // SetCurrentDirectory(cwd);
48
83
49
84
if (GetOpenFileName (&ofn))
50
85
{
51
- if (_chooseFileSuccessCallback)
52
- _chooseFileSuccessCallback ();
86
+ pthread_testcancel ();
87
+ if (cfc->_chooseFileSuccessCallback )
88
+ cfc->_chooseFileSuccessCallback (ofn.lpstrFile );
53
89
}
54
90
else
55
91
{
56
- if (_chooseFileCancelledCallback)
57
- _chooseFileCancelledCallback ();
92
+ pthread_testcancel ();
93
+ if (cfc->_chooseFileCancelledCallback )
94
+ cfc->_chooseFileCancelledCallback ();
58
95
}
59
96
60
97
return NULL ;
61
98
}
62
99
63
100
64
- void __stdcall RegisterChooseFileSuccessCallback (ChooseFileSuccessCallback callback )
101
+ void * __stdcall CreateChooseFileContext ( )
65
102
{
66
- _chooseFileSuccessCallback = callback;
103
+ ChooseFileContext* cfc = new ChooseFileContext ();
104
+ cfc->_chooseFileCancelledCallback = NULL ;
105
+ cfc->_chooseFileSuccessCallback = NULL ;
106
+ return cfc;
67
107
}
68
108
69
109
70
- void __stdcall RegisterChooseFileCancelledCallback (ChooseFileCancelledCallback callback )
110
+ void __stdcall AddChooseFileType ( void * context, const wchar_t * name, const wchar_t * extension )
71
111
{
72
- _chooseFileCancelledCallback = callback;
112
+ ChooseFileContext* cfc = (ChooseFileContext*)context;
113
+ cfc->filetype_names .push_back (wstring (name));
114
+ cfc->filetype_extensions .push_back (wstring (extension));
73
115
}
74
116
75
117
76
- void __stdcall ChooseFile ()
118
+ void __stdcall ChooseFile (void * context, ChooseFileSuccessCallback successCallback, ChooseFileCancelledCallback cancelledCallback )
77
119
{
78
- pthread_mutex_lock (&shared_machine_mutex);
79
- if (shared_machine == NULL )
80
- {
81
- shared_machine = new Machine ();
82
- loading_thread_started = true ;
83
- pthread_create (&loading_thread, NULL , LoadGcodeAsync, NULL );
84
- }
85
- pthread_mutex_unlock (&shared_machine_mutex);
120
+ ChooseFileContext* cfc = (ChooseFileContext*)context;
121
+ cfc->_chooseFileSuccessCallback = successCallback;
122
+ cfc->_chooseFileCancelledCallback = cancelledCallback;
123
+ pthread_create (&cfc->file_picker_thread , NULL , FilePickerThread, cfc);
86
124
}
87
125
88
126
127
+ void __stdcall DestroyChooseFileContext (void * context)
128
+ {
129
+ ChooseFileContext* cfc = (ChooseFileContext*)context;
130
+ pthread_cancel (cfc->file_picker_thread );
131
+ delete cfc;
132
+ }
89
133
0 commit comments