]> Creatis software - bbtk.git/blob - kernel/appli/bbi/bbi.cxx
#3203 BBTK Feature New Normal vtk7itk4wx3-mingw64
[bbtk.git] / kernel / appli / bbi / bbi.cxx
1 /*
2  # ---------------------------------------------------------------------
3  #
4  # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5  #                        pour la SantÈ)
6  # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7  # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8  # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9  #
10  #  This software is governed by the CeCILL-B license under French law and
11  #  abiding by the rules of distribution of free software. You can  use,
12  #  modify and/ or redistribute the software under the terms of the CeCILL-B
13  #  license as circulated by CEA, CNRS and INRIA at the following URL
14  #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15  #  or in the file LICENSE.txt.
16  #
17  #  As a counterpart to the access to the source code and  rights to copy,
18  #  modify and redistribute granted by the license, users are provided only
19  #  with a limited warranty  and the software's author,  the holder of the
20  #  economic rights,  and the successive licensors  have only  limited
21  #  liability.
22  #
23  #  The fact that you are presently reading this means that you have had
24  #  knowledge of the CeCILL-B license and that you accept its terms.
25  # ------------------------------------------------------------------------ */
26
27
28 #ifdef _USE_WXWIDGETS_
29
30 #include <exception>
31
32 //==========================================================================
33 // WITH WX
34 //==========================================================================
35 #include "bbtkObject.h"
36 #include "bbtkInterpreter.h"
37 #include "bbtkWxBlackBox.h"
38 #include "bbtkWxGUIConsole.h"
39
40 #include <wx/cmdline.h>
41 #include <vector>
42 #include <map>
43
44 //==========================================================================
45
46 //EED 2017-09-16 Migration wxWidgets 2.8 to 3.0
47 #if wxMAJOR_VERSION <= 2
48         // Command line options definition
49         static const wxCmdLineEntryDesc cmdLineDesc[] =
50         {
51           { wxCMD_LINE_SWITCH, _T("h"), _T("help"),   _T("print this help or help on the application defined in input bbs file if any") },
52           { wxCMD_LINE_SWITCH, _T("g"), _T("graphical-dialog"),   _T("prompt the input parameter values using graphical dialog") },
53           { wxCMD_LINE_SWITCH, _T("t"), _T("text-dialog"),   _T("prompt the input parameter values in text mode") },
54           { wxCMD_LINE_SWITCH, _T("c"), _T("console"), _T("open bbi console") },
55           { wxCMD_LINE_SWITCH, _T("N"), _T("no-console"),   _T("never open bbi console even on error") },
56           { wxCMD_LINE_SWITCH, _T("q"), _T("quiet"),   _T("be quiet (='message max 0')") },
57           { wxCMD_LINE_SWITCH, _T("d"), _T("debug"), _T("turn all messages on (='message all 9')") },
58           { wxCMD_LINE_SWITCH, _T("D"), _T("Debug"), _T("memory debug on exit (='debug -D')") },
59           { wxCMD_LINE_PARAM,  NULL, NULL, _T("file [file [...]]"), wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE },
60           { wxCMD_LINE_NONE }
61         };
62 #else
63         // Command line options definition
64         static const wxCmdLineEntryDesc cmdLineDesc[] =
65         {
66           { wxCMD_LINE_SWITCH, "h", "help",   "print this help or help on the application defined in input bbs file if any" },
67           { wxCMD_LINE_SWITCH, "g", "graphical-dialog",   "prompt the input parameter values using graphical dialog" },
68           { wxCMD_LINE_SWITCH, "t", "text-dialog",   "prompt the input parameter values in text mode" },
69           { wxCMD_LINE_SWITCH, "c", "console", "open bbi console" },
70           { wxCMD_LINE_SWITCH, "N", "no-console",   "never open bbi console even on error" },
71           { wxCMD_LINE_SWITCH, "q", "quiet",   "be quiet (='message max 0')" },
72           { wxCMD_LINE_SWITCH, "d", "debug", "turn all messages on (='message all 9')" },
73           { wxCMD_LINE_SWITCH, "D", "Debug", "memory debug on exit (='debug -D')" },
74           { wxCMD_LINE_PARAM,  NULL, NULL, "file [file [...]]", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE },
75           { wxCMD_LINE_NONE }
76         };
77 #endif
78
79
80
81
82 //==========================================================================
83
84 //==========================================================================
85 // Processes the command line parsing result
86 struct WxProcessCmdLine
87 {
88   WxProcessCmdLine() {}
89   void Process(wxCmdLineParser& parser);
90
91   //  int argc;
92   std::vector<std::string> argv;
93   bool console;
94   bool debug;
95   bool quiet;
96   bool help;
97   bool graphical_dialog;
98   bool text_dialog;
99   bool no_console;
100   bool proceed;
101   std::map<std::string,std::string> param_map;
102   std::vector<std::string> input_file;
103   
104 };
105 //==========================================================================
106
107 //==========================================================================
108 void WxProcessCmdLine::Process(wxCmdLineParser& parser)
109 {
110   proceed = true;
111   if (parser.Found(_T("D"))) 
112     {
113       bbtk::StaticInitTime::PrintObjectListInfo = true;
114     }
115   
116   debug                         = ( parser.Found(_T("d")) );  
117   quiet                         = ( parser.Found(_T("q")) );
118   help                          = ( parser.Found(_T("h")) );
119
120 //EED 2017-09-16 Migration wxWidgets 2.8 to 3.0
121 #if wxMAJOR_VERSION <= 2
122   graphical_dialog      = ( parser.Found(_T("wxcommandlineg")) );
123 #else
124   printf("EED Warnning. WxProcessCmdLine::Process  g  wxcommandlineg \n");
125   graphical_dialog      = ( parser.Found(_T("g")) );
126 #endif
127
128   text_dialog           = ( parser.Found(_T("t")) );
129   no_console            = ( parser.Found(_T("N")) );
130   if (quiet) bbtk::MessageManager::SetMessageLevel("max",0);
131   if (debug) bbtk::MessageManager::SetMessageLevel("all",9);
132
133   // parse the arguments and consider those which contain a "=" 
134   // as set input commands, other as files
135   int pargc = parser.GetParamCount();
136   for (int i=0; i<pargc; ++i) 
137     {
138       std::string s = bbtk::wx2std(parser.GetParam(i));
139       std::string::size_type pos = s.find_first_of("=");
140       if (std::string::npos != pos) 
141         {
142           std::string left = s.substr(0,pos);
143           std::string right = s.substr(pos+1,s.size());
144           param_map[left]=right;
145         }
146       else 
147         {
148           input_file.push_back(s);
149         }
150     }
151
152   bool usage = (help && (input_file.size()==0));
153   if (usage) {
154     std::cout << "BBI (The Black Box Interpreter) - bbtk "
155               << bbtk::GetVersion() << " - (c) Creatis 2007-2008"
156               << std::endl;
157     parser.Usage();
158     proceed = false;
159   }
160
161   console = ( parser.Found(_T("c")) || 
162               ((input_file.size() == 0) && 
163                (!no_console) &&
164                (!usage) ) );
165
166 }
167 //==========================================================================
168
169
170 //==========================================================================
171 class wxBBIApp : public wxApp
172 {
173 public:
174   bool OnInit( );
175   int  OnExit() { 
176     //    std::cout << "wxBBIApp::OnExit()"<<std::endl;
177     // bbtk::Object::PrintObjectListInfo();
178     return true; }
179   void OnInitCmdLine(wxCmdLineParser& parser);
180   bool OnCmdLineParsed(wxCmdLineParser& parser);
181
182   WxProcessCmdLine cmd;
183 };
184 //==========================================================================
185
186
187 //==========================================================================
188 void wxBBIApp::OnInitCmdLine(wxCmdLineParser& parser)
189 {
190   parser.SetDesc(cmdLineDesc);
191 }
192 //==========================================================================
193
194 //==========================================================================
195 bool wxBBIApp::OnCmdLineParsed(wxCmdLineParser& parser)
196 {
197   cmd.Process(parser);
198   // if (!cmd.proceed) return false;
199   return true;
200 }
201 //==========================================================================
202
203
204
205
206
207
208 //==========================================================================
209 // The `main program' equivalent, creating the windows and returning the
210 // main frame
211 bool wxBBIApp::OnInit( )
212 {
213 //Borrame
214 //FILE *ff; ff = fopen ("/tmp/wt.log","a+"); fprintf(ff,"EED wxBBIApp::OnInit\n"); fclose(ff);
215
216   //      std::cout << "OnInit"<<std::endl;
217   wxApp::OnInit();
218 #ifdef __WXGTK__
219   //See http://www.wxwindows.org/faqgtk.htm#locale
220   setlocale(LC_NUMERIC, "C");
221 #endif
222   
223   if (cmd.quiet) bbtk::MessageManager::SetMessageLevel("max",0);
224   if (cmd.debug) bbtk::MessageManager::SetMessageLevel("all",9);
225   
226
227 //Borrame
228 //printf ("EED bbi wxBBIApp::OnInit .....................\n");
229 //cmd.input_file.push_back("/home/davila/Borrame/testwt.bbs");
230
231   bbtk::WxGUIConsole *I = new bbtk::WxGUIConsole(0,_T("bbi"),wxSize(800,600));
232   SetTopWindow(I);  
233   if (cmd.console) I->Show(true);
234
235
236   I->SetInputs(cmd.param_map);
237
238   bool help_on_script = cmd.help && (cmd.input_file.size() > 0);
239   if (help_on_script)         I->SetNoExecMode(true);
240   if (cmd.graphical_dialog)   I->SetDialogMode(bbtk::VirtualExec::GraphicalDialog);
241   if (cmd.text_dialog)        I->SetDialogMode(bbtk::VirtualExec::TextDialog);
242
243   std::vector<std::string>::const_iterator i;
244   bool error = false;
245
246
247   for (i=cmd.input_file.begin(); i!=cmd.input_file.end(); ++i) 
248     {
249       error = ! I->InterpretFile(*i);
250       if (error) break;
251     }
252   bool show_on_error = error && ! cmd.no_console;
253   if (show_on_error) I->Show();
254
255   I->SetNoExecMode(false);
256
257   if (help_on_script) 
258     {
259       std::string package; 
260       I->GetInterpreter()->GetExecuter()->GetFactory()->PrintHelpDescriptor("workspace",package,false);
261     }
262
263
264   /*
265   std::cout << "soe="<<show_on_error <<std::endl;
266   std::cout << "con="<<console<<std::endl;
267   std::cout << "iws="<<bbtk::Wx::IsSomeWindowShown()<<std::endl;
268   */
269   if (!(show_on_error || cmd.console || bbtk::Wx::IsSomeWindowAlive() ))
270     {
271       I->Close();
272       //      std::cout << "I->Close"<<std::endl;
273     }
274   else 
275     {
276       //      std::cout << "!I->Close"<<std::endl;
277     }
278         
279   //    std::cout << "EO OnInit"<<std::endl;
280
281   return true;
282
283 }
284 //==========================================================================
285
286
287 #if defined(_WIN32) 
288 //==========================================================================
289 // WINDOWS
290 //==========================================================================
291 IMPLEMENT_APP(wxBBIApp);
292
293 //  How to have a Console and wxWidgets
294 //  http://www.wxwidgets.org/wiki/index.php/MSVC_Setup_Guide
295 //   In Visual C++ 6 (7 should be similar), to create an application that is both a console application 
296 //  (cout's to the console are visible) and has a wxWidgets GUI, 
297 //  you need to use the linker option "/subsystem:console" and the following code:
298 int main(int argc, char* argv[])
299 {
300 //Borrame
301 //FILE *ff; ff = fopen ("/tmp/wt.log","a+"); fprintf(ff,"EED main C\n"); fclose(ff);
302
303         // EED 2018-07-16
304         char buffer[1500];
305         wcstombs(buffer , ::GetCommandLine() , 1500 );
306     return WinMain(::GetModuleHandle(NULL), NULL, buffer , SW_SHOWNORMAL);
307         //    return WinMain(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), SW_SHOWNORMAL);
308 }
309
310
311 #else 
312 //==========================================================================
313 // OTHER ( Linux... )
314 //==========================================================================
315
316 IMPLEMENT_APP_NO_MAIN(wxBBIApp);
317
318
319 int main(int argc, char* argv[])
320 {       
321 //Borrame
322 //FILE *ff; ff = fopen ("/tmp/wt.log","a+"); fprintf(ff,"EED main A\n"); fclose(ff);
323
324   wxMessageOutput::Set( new wxMessageOutputBest );
325
326   wxCmdLineParser parser(cmdLineDesc,argc,argv);
327   int val = parser.Parse(false);
328   if (val>0) 
329     {  
330       parser.Usage();
331       return 0;
332     }
333   WxProcessCmdLine cmdline;
334   cmdline.Process(parser);
335
336   if (!cmdline.proceed) return 0;
337
338         if (cmdline.no_console) 
339     {
340       //      std::cout << "main NC"<<std::endl;
341       // Interpreter 
342       bbtk::Interpreter::Pointer I = bbtk::Interpreter::New();
343       I->SetInputs(cmdline.param_map);
344       bool help_on_script = cmdline.help && (cmdline.input_file.size() > 0);
345       if (help_on_script) I->SetNoExecMode(true);
346       if (cmdline.text_dialog) I->SetDialogMode(bbtk::VirtualExec::TextDialog);
347       std::vector<std::string>::const_iterator i;
348       bool error = false;
349       for (i=cmdline.input_file.begin(); 
350            i!=cmdline.input_file.end(); ++i) 
351                 {
352                         error = ! I->InterpretFile(*i);
353                         if (error) break;
354                 }
355         if (help_on_script) 
356                 {
357                         I->SetNoExecMode(false);
358                         std::string package; 
359                         I->GetExecuter()->GetFactory()->PrintHelpDescriptor("workspace",
360                                                               package,
361                                                               false);
362                 }
363         if (cmdline.input_file.size()==0)
364                 {
365                         I->CommandLineInterpreter();
366                 } // if cmdline.input_file.size
367       //
368     } else {
369       wxEntry(argc, argv);
370     } // if cmdline.no_console
371
372 }
373
374
375 #endif // defined(_WIN32) 
376 //==========================================================================
377
378
379
380
381
382
383
384
385 #else
386 //==========================================================================
387 // WITHOUT WX
388 //==========================================================================
389
390 #include "bbtkInterpreter.h"
391
392 // Processes the command line parsing result
393 struct ProcessCmdLine
394 {
395   ProcessCmdLine() {}
396   void Process(int argc_, char *argv_[]);
397   bool Found(std::string value);
398
399   int  argc;
400   std::vector<std::string> argv;
401   bool console;
402   bool debug;
403   bool quiet;
404   bool help;
405   bool graphical_dialog;
406   bool text_dialog;
407   bool no_console;
408   bool proceed;
409   std::map<std::string,std::string> param_map;
410   std::vector<std::string> input_file;
411   
412 };
413
414 bool ProcessCmdLine::Found(std::string value)
415 {
416         bool result=false;
417         for (int i=1; i<argc; ++i) 
418         {
419                 if (value==argv[i]) 
420                 {
421                         result = true;
422                 } 
423         } // for
424         return result;
425 }
426
427 void ProcessCmdLine::Process(int argc_, char *argv_[])
428 {
429    argc = argc_;
430    for (int i=0; i<argc; ++i) 
431     {
432         argv.push_back(argv_[i]);
433     } // for
434
435 //EED  proceed = true;
436   if (Found("D")) 
437     {
438       bbtk::StaticInitTime::PrintObjectListInfo = true;
439     }
440   
441   debug                 = ( Found("d") );  
442   quiet                 = ( Found("q") );
443   help                  = ( Found("h") );
444   graphical_dialog      = ( Found("g") );
445   text_dialog           = ( Found("t") );
446   no_console            = ( Found("N") );
447   
448   if (quiet) bbtk::MessageManager::SetMessageLevel("max",0);
449   if (debug) bbtk::MessageManager::SetMessageLevel("all",9);
450
451   // parse the arguments and consider those which contain a "=" 
452   // as set input commands, other as files
453 //EED  int pargc = parser.GetParamCount();
454   for (int i=1; i<argc; ++i) 
455     {
456       std::string s = argv[i];
457       std::string::size_type pos = s.find_first_of("=");
458       if (std::string::npos != pos) 
459         {
460           std::string left = s.substr(0,pos);
461           std::string right = s.substr(pos+1,s.size());
462           param_map[left]=right;
463         }
464       else 
465         {
466           input_file.push_back(s);
467         }
468     }// for
469
470   bool usage = (help && (input_file.size()==0));
471   if (usage) {
472     std::cout << "BBI (The Black Box Interpreter) - bbtk "
473               << bbtk::GetVersion() << " - (c) Creatis 2007-2008"
474               << std::endl;
475 //EED    parser.Usage();
476     proceed = false;
477   }
478
479   console = ( Found("c") || 
480               ((input_file.size() == 0) && 
481                (!no_console) &&
482                (!usage) ) );
483   
484 }
485 //==========================================================================
486
487
488
489 int main(int argc, char* argv[])
490 {  
491 //EED
492 //  if (argc>2) 
493 //    {
494 //      std::cout << "usage : "<<argv[0]<<" [filename]"<<std::endl;
495 //      return 0;
496 //    }
497
498   std::cout << "BBI (Black Box Interpreter) - bbtk "
499             << bbtk::GetVersion()<< " - (c) Creatis 2007"
500             << std::endl;
501 //Borrame
502 //FILE *ff; ff = fopen ("/tmp/wt.log","a+"); fprintf(ff,"EED main B\n"); fclose(ff);
503
504   bbtk::Interpreter::Pointer I = bbtk::Interpreter::New();
505   if (argc==1) 
506   {
507         I->CommandLineInterpreter();
508   }  else     {
509          
510         ProcessCmdLine cmd;
511         cmd.Process(argc,argv);
512         I->SetInputs(cmd.param_map);
513
514         std::string f(argv[1]);
515         I->InterpretFile(f);
516   }
517   
518   //  bbtk::Wx::LoopUntilAllWindowsClose(); 
519      
520   return 0;
521
522 }
523
524 // EOF
525 #endif //#ifdef _USE_WXWIDGETS_
526
527
528