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