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