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