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