]> Creatis software - bbtk.git/blob - kernel/appli/bbi/bbi.cxx
*** empty log message ***
[bbtk.git] / kernel / appli / bbi / bbi.cxx
1 #ifdef _USE_WXWIDGETS_
2
3 //==========================================================================
4 // WITH WX
5 //==========================================================================
6 #include "bbtkObject.h"
7 #include "bbtkInterpreter.h"
8 #include "bbtkWxBlackBox.h"
9 #include "bbtkWxGUIConsole.h"
10
11 #include <wx/cmdline.h>
12 #include <vector>
13 #include <map>
14
15 //==========================================================================
16 // Command line options definition
17 static const wxCmdLineEntryDesc cmdLineDesc[] =
18 {
19   { wxCMD_LINE_SWITCH, _T("h"), _T("help"),   _T("print this help or help on the application defined in input bbs file if any") },
20   { wxCMD_LINE_SWITCH, _T("g"), _T("graphical-dialog"),   _T("prompt the input parameter values using graphical dialog") },
21   { wxCMD_LINE_SWITCH, _T("t"), _T("text-dialog"),   _T("prompt the input parameter values in text mode") },
22   { wxCMD_LINE_SWITCH, _T("c"), _T("console"), _T("open bbi console") },
23   { wxCMD_LINE_SWITCH, _T("N"), _T("no-console"),   _T("never open bbi console even on error") },
24   { wxCMD_LINE_SWITCH, _T("q"), _T("quiet"),   _T("be quiet (='message max 0')") },
25   { wxCMD_LINE_SWITCH, _T("d"), _T("debug"), _T("turn all messages on (='message all 9')") },
26   { wxCMD_LINE_SWITCH, _T("D"), _T("Debug"), _T("memory debug on exit (='debug -D')") },
27   { wxCMD_LINE_PARAM,  NULL, NULL, _T("file [file [...]]"), wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE },
28   { wxCMD_LINE_NONE }
29 };
30 //==========================================================================
31
32 //==========================================================================
33 // Processes the command line parsing result
34 struct WxProcessCmdLine
35 {
36   WxProcessCmdLine() {}
37   void Process(wxCmdLineParser& parser);
38
39   //  int argc;
40   std::vector<std::string> argv;
41   bool console;
42   bool debug;
43   bool quiet;
44   bool help;
45   bool graphical_dialog;
46   bool text_dialog;
47   bool no_console;
48   bool proceed;
49   std::map<std::string,std::string> param_map;
50   std::vector<std::string> input_file;
51   
52 };
53 //==========================================================================
54
55 //==========================================================================
56 void WxProcessCmdLine::Process(wxCmdLineParser& parser)
57 {
58   proceed = true;
59   if (parser.Found(_T("D"))) 
60     {
61       bbtk::StaticInitTime::PrintObjectListInfo = true;
62     }
63   
64   debug = ( parser.Found(_T("d")) );  
65   quiet = ( parser.Found(_T("q")) );
66   help = ( parser.Found(_T("h")) );
67   graphical_dialog = ( parser.Found(_T("g")) );
68   text_dialog = ( parser.Found(_T("t")) );
69   no_console = ( parser.Found(_T("N")) );
70   
71   if (quiet) bbtk::MessageManager::SetMessageLevel("max",0);
72   if (debug) bbtk::MessageManager::SetMessageLevel("all",9);
73
74   // parse the arguments and consider those which contain a "=" 
75   // as set input commands, other as files
76   int pargc = parser.GetParamCount();
77   for (int i=0; i<pargc; ++i) 
78     {
79       std::string s = bbtk::wx2std(parser.GetParam(i));
80       std::string::size_type pos = s.find_first_of("=");
81       if (std::string::npos != pos) 
82         {
83           std::string left = s.substr(0,pos);
84           std::string right = s.substr(pos+1,s.size());
85           param_map[left]=right;
86         }
87       else 
88         {
89           input_file.push_back(s);
90         }
91     }
92
93   bool usage = (help && (input_file.size()==0));
94   if (usage) {
95     std::cout << "BBI (The Black Box Interpreter) - bbtk "
96               << bbtk::GetVersion() << " - (c) Creatis 2007-2008"
97               << std::endl;
98     parser.Usage();
99     proceed = false;
100   }
101
102   console = ( parser.Found(_T("c")) || 
103               ((input_file.size() == 0) && 
104                (!no_console) &&
105                (!usage) ) );
106   
107 }
108 //==========================================================================
109
110
111 //==========================================================================
112 class wxBBIApp : public wxApp
113 {
114 public:
115   bool OnInit( );
116   int  OnExit() { 
117     //    std::cout << "wxBBIApp::OnExit()"<<std::endl;
118     // bbtk::Object::PrintObjectListInfo();
119     return true; }
120   void OnInitCmdLine(wxCmdLineParser& parser);
121   bool OnCmdLineParsed(wxCmdLineParser& parser);
122
123   WxProcessCmdLine cmd;
124 };
125 //==========================================================================
126
127
128 //==========================================================================
129 void wxBBIApp::OnInitCmdLine(wxCmdLineParser& parser)
130 {
131   parser.SetDesc(cmdLineDesc);
132 }
133 //==========================================================================
134
135 //==========================================================================
136 bool wxBBIApp::OnCmdLineParsed(wxCmdLineParser& parser)
137 {
138   cmd.Process(parser);
139   // if (!cmd.proceed) return false;
140   return true;
141 }
142 //==========================================================================
143
144
145
146
147
148
149 //==========================================================================
150 // The `main program' equivalent, creating the windows and returning the
151 // main frame
152 bool wxBBIApp::OnInit( )
153 {
154   //      std::cout << "OnInit"<<std::endl;
155   wxApp::OnInit();
156 #ifdef __WXGTK__
157   //See http://www.wxwindows.org/faqgtk.htm#locale
158   setlocale(LC_NUMERIC, "C");
159 #endif
160   
161
162   if (cmd.quiet) bbtk::MessageManager::SetMessageLevel("max",0);
163   if (cmd.debug) bbtk::MessageManager::SetMessageLevel("all",9);
164   
165
166   bbtk::WxGUIConsole *I = new bbtk::WxGUIConsole(0,_T("bbi"),wxSize(800,600));
167   SetTopWindow(I);  
168   if (cmd.console) I->Show(true);
169
170
171   I->SetInputs(cmd.param_map);
172
173   bool help_on_script = cmd.help && (cmd.input_file.size() > 0);
174   if (help_on_script) 
175     I->SetNoExecMode(true);
176   if (cmd.graphical_dialog) 
177     I->SetDialogMode(bbtk::VirtualExec::GraphicalDialog);
178   if (cmd.text_dialog) 
179     I->SetDialogMode(bbtk::VirtualExec::TextDialog);
180
181   std::vector<std::string>::const_iterator i;
182   bool error = false;
183
184   for (i=cmd.input_file.begin(); i!=cmd.input_file.end(); ++i) 
185     {
186       error = ! I->InterpretFile(*i);
187       if (error) break;
188     }
189   bool show_on_error = error && ! cmd.no_console;
190   if (show_on_error) I->Show();
191
192   I->SetNoExecMode(false);
193
194   if (help_on_script) 
195     {
196       std::string package; 
197       I->GetInterpreter()->GetExecuter()->GetFactory()->PrintHelpDescriptor("workspace",package,false);
198     }
199
200   /*
201   std::cout << "soe="<<show_on_error <<std::endl;
202   std::cout << "con="<<console<<std::endl;
203   std::cout << "iws="<<bbtk::Wx::IsSomeWindowShown()<<std::endl;
204   */
205   if (!(show_on_error || cmd.console || bbtk::Wx::IsSomeWindowAlive() ))
206     {
207       I->Close();
208       //      std::cout << "I->Close"<<std::endl;
209     }
210   else 
211     {
212       //      std::cout << "!I->Close"<<std::endl;
213     }
214         
215   //    std::cout << "EO OnInit"<<std::endl;
216
217   return true;
218
219 }
220 //==========================================================================
221
222
223 #if defined(_WIN32) 
224 //==========================================================================
225 // WINDOWS
226 //==========================================================================
227 IMPLEMENT_APP(wxBBIApp);
228
229 //  How to have a Console and wxWidgets
230 //  http://www.wxwidgets.org/wiki/index.php/MSVC_Setup_Guide
231 //   In Visual C++ 6 (7 should be similar), to create an application that is both a console application 
232 //  (cout's to the console are visible) and has a wxWidgets GUI, 
233 //  you need to use the linker option "/subsystem:console" and the following code:
234 int main(int argc, char* argv[])
235 {
236     return WinMain(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), SW_SHOWNORMAL);
237 }
238
239 #else 
240 //==========================================================================
241 // OTHER ( Linux... )
242 //==========================================================================
243
244 IMPLEMENT_APP_NO_MAIN(wxBBIApp);
245
246
247 int main(int argc, char* argv[])
248 {       
249   wxMessageOutput::Set( new wxMessageOutputBest );
250
251   wxCmdLineParser parser(cmdLineDesc,argc,argv);
252   int val = parser.Parse(false);
253   if (val>0) 
254     {  
255       parser.Usage();
256       return 0;
257     }
258   WxProcessCmdLine cmdline;
259   cmdline.Process(parser);
260
261   if (!cmdline.proceed) return 0;
262
263   if (cmdline.no_console) 
264     {
265       //      std::cout << "main NC"<<std::endl;
266       // Interpreter 
267       bbtk::Interpreter::Pointer I = bbtk::Interpreter::New();
268       I->SetInputs(cmdline.param_map);
269       bool help_on_script = cmdline.help && (cmdline.input_file.size() > 0);
270       if (help_on_script) I->SetNoExecMode(true);
271       if (cmdline.text_dialog) I->SetDialogMode(bbtk::VirtualExec::TextDialog);
272       std::vector<std::string>::const_iterator i;
273       bool error = false;
274       for (i=cmdline.input_file.begin(); 
275            i!=cmdline.input_file.end(); ++i) 
276         {
277           error = ! I->InterpretFile(*i);
278           if (error) break;
279         }
280       if (help_on_script) 
281         {
282           I->SetNoExecMode(false);
283           std::string package; 
284           I->GetExecuter()->GetFactory()->PrintHelpDescriptor("workspace",
285                                                               package,
286                                                               false);
287         }
288       if (cmdline.input_file.size()==0)
289         I->CommandLineInterpreter();
290
291       //
292     }
293   else 
294     {
295       wxEntry(argc, argv);
296     }
297
298 }
299
300
301 #endif // defined(_WIN32) 
302 //==========================================================================
303
304
305
306
307
308
309
310
311 #else
312 //==========================================================================
313 // WITHOUT WX
314 //==========================================================================
315
316 #include "bbtkInterpreter.h"
317
318 // Processes the command line parsing result
319 struct ProcessCmdLine
320 {
321   ProcessCmdLine() {}
322   void Process(int argc_, char *argv_[]);
323   bool Found(std::string value);
324
325   int  argc;
326   std::vector<std::string> argv;
327   bool console;
328   bool debug;
329   bool quiet;
330   bool help;
331   bool graphical_dialog;
332   bool text_dialog;
333   bool no_console;
334   bool proceed;
335   std::map<std::string,std::string> param_map;
336   std::vector<std::string> input_file;
337   
338 };
339
340 bool ProcessCmdLine::Found(std::string value)
341 {
342         bool result=false;
343         for (int i=1; i<argc; ++i) 
344         {
345                 if (value==argv[i]) 
346                 {
347                         result = true;
348                 } 
349         } // for
350         return result;
351 }
352
353 void ProcessCmdLine::Process(int argc_, char *argv_[])
354 {
355    argc = argc_;
356    for (int i=0; i<argc; ++i) 
357     {
358         argv.push_back(argv_[i]);
359     } // for
360
361 //EED  proceed = true;
362   if (Found("D")) 
363     {
364       bbtk::StaticInitTime::PrintObjectListInfo = true;
365     }
366   
367   debug                 = ( Found("d") );  
368   quiet                 = ( Found("q") );
369   help                  = ( Found("h") );
370   graphical_dialog      = ( Found("g") );
371   text_dialog           = ( Found("t") );
372   no_console            = ( Found("N") );
373   
374   if (quiet) bbtk::MessageManager::SetMessageLevel("max",0);
375   if (debug) bbtk::MessageManager::SetMessageLevel("all",9);
376
377   // parse the arguments and consider those which contain a "=" 
378   // as set input commands, other as files
379 //EED  int pargc = parser.GetParamCount();
380   for (int i=1; i<argc; ++i) 
381     {
382       std::string s = argv[i];
383       std::string::size_type pos = s.find_first_of("=");
384       if (std::string::npos != pos) 
385         {
386           std::string left = s.substr(0,pos);
387           std::string right = s.substr(pos+1,s.size());
388           param_map[left]=right;
389         }
390       else 
391         {
392           input_file.push_back(s);
393         }
394     }// for
395
396   bool usage = (help && (input_file.size()==0));
397   if (usage) {
398     std::cout << "BBI (The Black Box Interpreter) - bbtk "
399               << bbtk::GetVersion() << " - (c) Creatis 2007-2008"
400               << std::endl;
401 //EED    parser.Usage();
402     proceed = false;
403   }
404
405   console = ( Found("c") || 
406               ((input_file.size() == 0) && 
407                (!no_console) &&
408                (!usage) ) );
409   
410 }
411 //==========================================================================
412
413
414
415 int main(int argc, char* argv[])
416 {  
417 //EED
418 //  if (argc>2) 
419 //    {
420 //      std::cout << "usage : "<<argv[0]<<" [filename]"<<std::endl;
421 //      return 0;
422 //    }
423
424   std::cout << "BBI (Black Box Interpreter) - bbtk "
425             << bbtk::GetVersion()<< " - (c) Creatis 2007"
426             << std::endl;
427
428   bbtk::Interpreter::Pointer I = bbtk::Interpreter::New();
429   if (argc==1) 
430   {
431         I->CommandLineInterpreter();
432   }  else     {
433          
434         ProcessCmdLine cmd;
435         cmd.Process(argc,argv);
436         I->SetInputs(cmd.param_map);
437
438         std::string f(argv[1]);
439         I->InterpretFile(f);
440   }
441   
442   //  bbtk::Wx::LoopUntilAllWindowsClose(); 
443      
444   return 0;
445
446 }
447
448 // EOF
449 #endif //#ifdef _USE_WXWIDGETS_
450
451
452