]> Creatis software - creaContours.git/blob - appli/wxContourGUIExample/wxContourGUIExample.cxx
a679b72b006ad4b39d67e0b248995fd494e6a0da
[creaContours.git] / appli / wxContourGUIExample / wxContourGUIExample.cxx
1 //----------------------------------------------------------------------------------------------------------------
2 // Class definition include
3 //----------------------------------------------------------------------------------------------------------------
4
5 #include "wxContourGUIExample.h"
6 #include "wxContourMainFrame.h"
7 //#include "OutlineModelManager.h"
8 //#include "wxContourEventHandler.h"
9
10 //#include <creaImageIOWxGimmickDialog.h>
11 #include <creaImageIOWxGimmickReaderDialog.h>
12 #include <creaImageIOSystem.h>
13
14 #include <creaVtkBasicSlicer.h>
15 //#include "wxContourMainPanel.h"
16
17 #include "vtkMetaImageReader.h"
18 #include <map>
19 #include "vtkImageData.h"
20
21 #include "wx/artprov.h"
22 #include <wx/filedlg.h>
23 //#include "OutlineModelBuilder.h"
24 //#include "ContourThing.h"
25 //#include "AxeThing.h"
26 //#include "ImageSourceThing.h"
27 //#include "ImageSectionThing.h"
28 //#include "SomeEnvironment.h"
29 //#include "ReaderEnvironment.h"
30
31 //#include "interfMainPanel.h"
32
33 #if defined(MACOSX) // assume this is OSX 
34 # include <sys/param.h>
35 # include <mach-o/dyld.h> // _NSGetExecutablePath : must add -framework CoreFoundation to link line 
36 # include <string.h>
37 # ifndef PATH_MAX
38 #  define PATH_MAX MAXPATHLEN
39 # endif
40 #endif // MACOSX
41
42 #ifndef PATH_MAX // If not defined yet : do it 
43 #  define PATH_MAX 2048
44 #endif
45
46 #if defined(WIN32)
47   #include <direct.h>
48 #else
49    #include <dirent.h>  
50 #endif
51
52 #include <stdlib.h>
53
54 wxContourMainFrame* wxTheApplication::frame = 0;
55
56 //----------------------------------------------------------------------------------------------------------------
57 // This macro implements the entry point (main function) for the application
58 //----------------------------------------------------------------------------------------------------------------
59
60 /*
61 //EED
62 // http://lists.wxwidgets.org/archive/wx-dev/msg30449.html
63 wxString GetExecutablePath()
64 {
65     char buf[512];
66     char* slash;
67
68 #if defined(WIN32)
69     GetModuleFileName(NULL, buf, 511);
70     slash = strrchr(buf, '\\');
71     if (slash)
72     {
73         *slash = 0;
74     }
75 #elif defined(__LINUX__)
76     int res;
77     res = readlink("/proc/self/exe", buf, 512);
78     if (res == -1)
79         return _T("");
80     buf[res] = 0;
81     slash = strrchr(buf, '/');
82     if (slash)
83     {
84         *slash = 0;
85     }
86 #else
87     return "";
88 #endif
89
90     return wxString(buf, wxConvUTF8 );
91 }
92 */
93
94 //=========================================================================  
95 // From http://www.fltk.org/newsgroups.php?gfltk.general+v:22083
96 //
97 int get_app_path (char *pname, size_t pathsize)
98 {
99 #ifdef LINUX    
100     /* Oddly, the readlink(2) man page says no NULL is appended. */
101     /* So you have to do it yourself, based on the return value: */
102     pathsize --; /* Preserve a space to add the trailing NULL */
103     long result = readlink("/proc/self/exe", pname, pathsize);
104     if (result > 0)
105         {
106                 pname[result] = 0; /* add the #@!%ing NULL */
107                 
108                 if ((access(pname, 0) == 0))
109                         return 0; /* file exists, return OK */
110                 /*else name doesn't seem to exist, return FAIL (falls
111                  through) */
112         }
113 #endif /* LINUX */
114     
115 #ifdef WIN32
116     long result = GetModuleFileName(NULL, pname, pathsize);
117     if (result > 0)
118         {
119                 /* fix up the dir slashes... */
120                 int len = strlen(pname);
121                 int idx;
122                 for (idx = 0; idx < len; idx++)
123                 {
124                         if (pname[idx] == '\\') pname[idx] = '/';
125                 }
126                 
127                 for (idx = len-1; idx >=0 ; idx--)
128                 {
129                         if (pname[idx] == '/')
130                         { 
131                                 pname[idx+1] = '\0';
132                                 idx = -1;
133                         }
134                 }
135                 
136                 if ((access(pname, 0) == 0))
137                         return 0; /* file exists, return OK */
138                 /*else name doesn't seem to exist, return FAIL (falls
139                  through) */
140         }
141 #endif /* WIN32 */
142     
143 #ifdef SOLARIS
144     char *p = getexecname();
145     if (p)
146         {
147                 /* According to the Sun manpages, getexecname will
148                  "normally" return an */
149                 /* absolute path - BUT might not... AND that IF it is not,
150                  pre-pending */
151                 /* getcwd() will "usually" be the correct thing... Urgh!
152                  */
153                 
154                 /* check pathname is absolute (begins with a / ???) */
155                 if (p[0] == '/') /* assume this means we have an
156                  absolute path */
157                 {
158                         strncpy(pname, p, pathsize);
159                         if ((access(pname, 0) == 0))
160                                 return 0; /* file exists, return OK */
161                 }
162                 else /* if not, prepend getcwd() then check if file
163                  exists */
164                 {
165                         getcwd(pname, pathsize);
166                         long result = strlen(pname);
167                         strncat(pname, "/", (pathsize - result));
168                         result ++;
169                         strncat(pname, p, (pathsize - result));
170                         
171                         if ((access(pname, 0) == 0))
172                                 return 0; /* file exists, return OK */
173                         /*else name doesn't seem to exist, return FAIL
174                          (falls through) */
175                 }
176         }
177 #endif /* SOLARIS */
178     
179 #ifdef MACOSX /* assume this is OSX */
180     /*
181          from http://www.hmug.org/man/3/NSModule.html
182          
183          extern int _NSGetExecutablePath(char *buf, unsigned long
184          *bufsize);
185          
186          _NSGetExecutablePath  copies  the  path  of the executable
187          into the buffer and returns 0 if the path was successfully
188          copied  in the provided buffer. If the buffer is not large
189          enough, -1 is returned and the  expected  buffer  size  is
190          copied  in  *bufsize.  Note that _NSGetExecutablePath will
191          return "a path" to the executable not a "real path" to the
192          executable.  That  is  the path may be a symbolic link and
193          not the real file. And with  deep  directories  the  total
194          bufsize needed could be more than MAXPATHLEN.
195          */
196         
197     int status = -1;
198     char *given_path = (char*)malloc(MAXPATHLEN * 2);
199     if (!given_path) return status;
200     
201     uint32_t npathsize = MAXPATHLEN * 2;
202     long result = _NSGetExecutablePath(given_path, &npathsize);
203     if (result == 0)
204         { /* OK, we got something - now try and resolve the real path...
205          */
206                 if (realpath(given_path, pname) != NULL)
207                 {
208                         if ((access(pname, 0) == 0))
209                                 status = 0; /* file exists, return OK */
210                 }
211         }
212     free (given_path);
213     return status;
214 #endif /* MACOSX */
215     
216     return -1; /* Path Lookup Failed */
217
218 //=========================================================================
219
220 #if defined(_WIN32)
221 #define CREACONTOUR_VALID_FILE_SEPARATOR_CHAR '\\'
222 #else
223 #define CREACONTOUR_VALID_FILE_SEPARATOR_CHAR '/'
224 #endif
225
226 //=========================================================================
227 std::string GetExecutablePath()
228 {
229     char name[PATH_MAX];
230     int err = get_app_path(name, PATH_MAX);
231     if (err) 
232         {
233                 printf("Could not determine current executable path ?  ");  
234         }    
235     // remove the exe name
236     char *slash;                
237     slash = strrchr(name, CREACONTOUR_VALID_FILE_SEPARATOR_CHAR);
238     if (slash)
239         {
240                 *slash = 0;
241         }
242     return name;
243 }
244
245 //=========================================================================
246 //=========================================================================
247 IMPLEMENT_APP( wxTheApplication );
248 /*
249 wxContourGUIExample :: wxContourGUIExample(const wxString& title, const wxPoint& pos, const wxSize& size)
250 : wxFrame((wxFrame *) NULL, -1, title, pos, size)
251 {
252 }
253 */
254
255 #ifdef _DEBUG
256 void wxAppConsole::OnAssert(char const *,int,char const *,char const *)
257 {
258 }
259
260 void wxAppConsole::OnAssertFailure(char const *,int,char const *,char const *,char const *)
261 {
262 }
263 #endif
264
265 bool wxTheApplication :: OnInit()
266 {
267         wxInitAllImageHandlers();
268
269         int min_image_type = GIMMICK_2D_IMAGE_SELECTION;
270         int max_image_type = GIMMICK_3D_IMAGE_SELECTION;
271         int output_dim = NATIVE;
272         int threads = 1;
273
274         std::vector<vtkImageData*> images;
275
276         creaImageIO::WxGimmickReaderDialog w(0,
277                                                                                 -1,
278                                                                                 "creaContours_Descriptor.dscp",
279                                                                                 "creatisContours DB", 
280                                                                                 _T("WxGimmickDialog test"),
281                                                                                 wxDefaultPosition,wxSize(1200,800)
282                                                                                 ,min_image_type,
283                                                                                 max_image_type,
284                                                                                 output_dim,
285                                                                                 threads);
286   
287         w.ShowModal();
288
289
290 //JCP
291         //std::string datadir(  crea::wx2std(GetExecutablePath()) );    
292         std::string datadir(  GetExecutablePath() );
293 //JCP
294         
295                 #ifdef MACOSX /* assume this is OSX */
296                         datadir=datadir+"/../../..";
297                 #endif // MACOSX        
298                 
299                 
300                         if(w.GetReturnCode() == wxID_OK)
301                         {
302                                 std::vector<std::string> s;
303                                 w.GetSelectedFiles(s);
304                                 std::vector<std::string>::iterator i;
305                                 for (i=s.begin();i!=s.end();++i)
306                                 {
307                                         std::cout << *i << std::endl;
308                                 }
309                                 std::cout << "$$$$ "<<std::endl;
310                                 //w.GetSelectedImages(images);
311                                 w.GetSelectedImages(images,output_dim);
312                         }
313                         else if (w.GetReturnCode() == wxID_CANCEL)
314                         {
315                                 vtkMetaImageReader *reader = vtkMetaImageReader::New();
316                                 std::string filename= datadir + "/data/hola.mhd";          
317                                 reader->SetFileName( filename.c_str() );
318                                 reader->Update();
319                                 images.push_back(reader->GetOutput());
320                         }
321                         else
322                         {        
323                                 return -1;
324                         }   
325
326
327         wxFrame* frame1 = new wxFrame(NULL, wxID_ANY, wxT("ROI Application  -    Evaluation version, 09 Feb 2009 "), wxPoint(400,50), wxSize(800, 600) );
328
329         //frame = new wxContourMainFrame( frame1, wxID_ANY, wxString(_T("")), wxPoint(50,50), wxSize(800, 600), images ); 
330         frame = wxContourMainFrame::getInstance(frame1, wxID_ANY, wxString(_T("")), wxPoint(200,50), wxSize(800, 600), images,  wxDEFAULT_FRAME_STYLE | wxSUNKEN_BORDER,datadir );
331         frame1->CreateStatusBar();
332         frame1->Show(TRUE);
333     frame->RefreshInterface();
334         return TRUE;
335 }
336
337 #if(WIN32)
338 int main(int argc, char* argv[])                                        
339   {                                                                     
340     return WinMain(::GetModuleHandle(NULL), NULL,                       
341                    ::GetCommandLine(), SW_SHOWNORMAL);                  
342   }             
343 #else
344 #endif
345