]> Creatis software - bbtk.git/blob - kernel/appli/bbi/bbi.cxx
not center elements in ComboBox
[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 //__asm__(".symver fcntl64,fcntl64@GLIBC_2.27");
28
29 #ifdef _USE_WXWIDGETS_
30
31
32 #include <exception>
33
34 //==========================================================================
35 // WITH WX
36 //==========================================================================
37 #include "bbtkObject.h"
38 #include "bbtkInterpreter.h"
39 #include "bbtkWxBlackBox.h"
40 #include "bbtkWxGUIConsole.h"
41
42 #include <wx/cmdline.h>
43 #include <vector>
44 #include <map>
45
46 //==========================================================================
47
48 //EED 2017-09-16 Migration wxWidgets 2.8 to 3.0
49 #if wxMAJOR_VERSION <= 2
50         // Command line options definition
51         static const wxCmdLineEntryDesc cmdLineDesc[] =
52         {
53           { wxCMD_LINE_SWITCH, _T("h"), _T("help"),   _T("print this help or help on the application defined in input bbs file if any") },
54           { wxCMD_LINE_SWITCH, _T("g"), _T("graphical-dialog"),   _T("prompt the input parameter values using graphical dialog") },
55           { wxCMD_LINE_SWITCH, _T("t"), _T("text-dialog"),   _T("prompt the input parameter values in text mode") },
56           { wxCMD_LINE_SWITCH, _T("c"), _T("console"), _T("open bbi console") },
57           { wxCMD_LINE_SWITCH, _T("N"), _T("no-console"),   _T("never open bbi console even on error") },
58           { wxCMD_LINE_SWITCH, _T("q"), _T("quiet"),   _T("be quiet (='message max 0')") },
59           { wxCMD_LINE_SWITCH, _T("d"), _T("debug"), _T("turn all messages on (='message all 9')") },
60           { wxCMD_LINE_SWITCH, _T("D"), _T("Debug"), _T("memory debug on exit (='debug -D')") },
61           { wxCMD_LINE_PARAM,  NULL, NULL, _T("file [file [...]]"), wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE },
62           { wxCMD_LINE_NONE }
63         };
64 #else
65         // Command line options definition
66         static const wxCmdLineEntryDesc cmdLineDesc[] =
67         {
68           { wxCMD_LINE_SWITCH, "h", "help",   "print this help or help on the application defined in input bbs file if any" },
69           { wxCMD_LINE_SWITCH, "g", "graphical-dialog",   "prompt the input parameter values using graphical dialog" },
70           { wxCMD_LINE_SWITCH, "t", "text-dialog",   "prompt the input parameter values in text mode" },
71           { wxCMD_LINE_SWITCH, "c", "console", "open bbi console" },
72           { wxCMD_LINE_SWITCH, "N", "no-console",   "never open bbi console even on error" },
73           { wxCMD_LINE_SWITCH, "q", "quiet",   "be quiet (='message max 0')" },
74           { wxCMD_LINE_SWITCH, "d", "debug", "turn all messages on (='message all 9')" },
75           { wxCMD_LINE_SWITCH, "D", "Debug", "memory debug on exit (='debug -D')" },
76           { wxCMD_LINE_PARAM,  NULL, NULL, "file [file [...]]", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE },
77           { wxCMD_LINE_NONE }
78         };
79 #endif
80
81
82
83
84 //==========================================================================
85
86 //==========================================================================
87 // Processes the command line parsing result
88 struct WxProcessCmdLine
89 {
90   WxProcessCmdLine() {}
91   void Process(wxCmdLineParser& parser);
92
93   //  int argc;
94   std::vector<std::string> argv;
95   bool console;
96   bool debug;
97   bool quiet;
98   bool help;
99   bool graphical_dialog;
100   bool text_dialog;
101   bool no_console;
102   bool proceed;
103   std::map<std::string,std::string> param_map;
104   std::vector<std::string> input_file;
105   
106 };
107 //==========================================================================
108
109 //==========================================================================
110 void WxProcessCmdLine::Process(wxCmdLineParser& parser)
111 {
112   proceed = true;
113   if (parser.Found(_T("D"))) 
114     {
115       bbtk::StaticInitTime::PrintObjectListInfo = true;
116     }
117   
118   debug                         = ( parser.Found(_T("d")) );  
119   quiet                         = ( parser.Found(_T("q")) );
120   help                          = ( parser.Found(_T("h")) );
121
122 //EED 2017-09-16 Migration wxWidgets 2.8 to 3.0
123 #if wxMAJOR_VERSION <= 2
124   graphical_dialog      = ( parser.Found(_T("wxcommandlineg")) );
125 #else
126   printf("EED Warnning. WxProcessCmdLine::Process  g  wxcommandlineg \n");
127   graphical_dialog      = ( parser.Found(_T("g")) );
128 #endif
129
130   text_dialog           = ( parser.Found(_T("t")) );
131   no_console            = ( parser.Found(_T("N")) );
132   if (quiet) bbtk::MessageManager::SetMessageLevel("max",0);
133   if (debug) bbtk::MessageManager::SetMessageLevel("all",9);
134
135   // parse the arguments and consider those which contain a "=" 
136   // as set input commands, other as files
137   int pargc = parser.GetParamCount();
138   for (int i=0; i<pargc; ++i) 
139     {
140       std::string s = bbtk::wx2std(parser.GetParam(i));
141       std::string::size_type pos = s.find_first_of("=");
142       if (std::string::npos != pos) 
143         {
144           std::string left = s.substr(0,pos);
145           std::string right = s.substr(pos+1,s.size());
146           param_map[left]=right;
147         }
148       else 
149         {
150           input_file.push_back(s);
151         }
152     }
153
154   bool usage = (help && (input_file.size()==0));
155   if (usage) {
156     std::cout << "BBI (The Black Box Interpreter) - bbtk "
157               << bbtk::GetVersion() << " - (c) Creatis 2007-2008"
158               << std::endl;
159     parser.Usage();
160     proceed = false;
161   }
162
163   console = ( parser.Found(_T("c")) || 
164               ((input_file.size() == 0) && 
165                (!no_console) &&
166                (!usage) ) );
167
168 }
169 //==========================================================================
170
171
172 //==========================================================================
173 class wxBBIApp : public wxApp
174 {
175 public:
176   bool OnInit( );
177   int  OnExit() { 
178     //    std::cout << "wxBBIApp::OnExit()"<<std::endl;
179     // bbtk::Object::PrintObjectListInfo();
180     return true; }
181   void OnInitCmdLine(wxCmdLineParser& parser);
182   bool OnCmdLineParsed(wxCmdLineParser& parser);
183
184   WxProcessCmdLine cmd;
185 };
186 //==========================================================================
187
188
189 //==========================================================================
190 void wxBBIApp::OnInitCmdLine(wxCmdLineParser& parser)
191 {
192   parser.SetDesc(cmdLineDesc);
193 }
194 //==========================================================================
195
196 //==========================================================================
197 bool wxBBIApp::OnCmdLineParsed(wxCmdLineParser& parser)
198 {
199   cmd.Process(parser);
200   // if (!cmd.proceed) return false;
201   return true;
202 }
203 //==========================================================================
204
205
206
207 //==========================================================================
208 // The `main program' equivalent, creating the windows and returning the
209 // main frame
210 bool wxBBIApp::OnInit( )
211 {
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