2 # ---------------------------------------------------------------------
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
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
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.
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
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 # ------------------------------------------------------------------------ */
28 #ifdef _USE_WXWIDGETS_
30 //==========================================================================
32 //==========================================================================
33 #include "bbtkObject.h"
34 #include "bbtkInterpreter.h"
35 #include "bbtkWxBlackBox.h"
36 #include "bbtkWxGUIConsole.h"
38 #include <wx/cmdline.h>
42 //==========================================================================
44 //EED 2017-09-16 Migration wxWidgets 2.8 to 3.0
45 #if wxMAJOR_VERSION <= 2
46 // Command line options definition
47 static const wxCmdLineEntryDesc cmdLineDesc[] =
49 { wxCMD_LINE_SWITCH, _T("h"), _T("help"), _T("print this help or help on the application defined in input bbs file if any") },
50 { wxCMD_LINE_SWITCH, _T("g"), _T("graphical-dialog"), _T("prompt the input parameter values using graphical dialog") },
51 { wxCMD_LINE_SWITCH, _T("t"), _T("text-dialog"), _T("prompt the input parameter values in text mode") },
52 { wxCMD_LINE_SWITCH, _T("c"), _T("console"), _T("open bbi console") },
53 { wxCMD_LINE_SWITCH, _T("N"), _T("no-console"), _T("never open bbi console even on error") },
54 { wxCMD_LINE_SWITCH, _T("q"), _T("quiet"), _T("be quiet (='message max 0')") },
55 { wxCMD_LINE_SWITCH, _T("d"), _T("debug"), _T("turn all messages on (='message all 9')") },
56 { wxCMD_LINE_SWITCH, _T("D"), _T("Debug"), _T("memory debug on exit (='debug -D')") },
57 { wxCMD_LINE_PARAM, NULL, NULL, _T("file [file [...]]"), wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE },
61 // Command line options definition
62 static const wxCmdLineEntryDesc cmdLineDesc[] =
64 { wxCMD_LINE_SWITCH, "h", "help", "print this help or help on the application defined in input bbs file if any" },
65 { wxCMD_LINE_SWITCH, "g", "graphical-dialog", "prompt the input parameter values using graphical dialog" },
66 { wxCMD_LINE_SWITCH, "t", "text-dialog", "prompt the input parameter values in text mode" },
67 { wxCMD_LINE_SWITCH, "c", "console", "open bbi console" },
68 { wxCMD_LINE_SWITCH, "N", "no-console", "never open bbi console even on error" },
69 { wxCMD_LINE_SWITCH, "q", "quiet", "be quiet (='message max 0')" },
70 { wxCMD_LINE_SWITCH, "d", "debug", "turn all messages on (='message all 9')" },
71 { wxCMD_LINE_SWITCH, "D", "Debug", "memory debug on exit (='debug -D')" },
72 { wxCMD_LINE_PARAM, NULL, NULL, "file [file [...]]", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE },
80 //==========================================================================
82 //==========================================================================
83 // Processes the command line parsing result
84 struct WxProcessCmdLine
87 void Process(wxCmdLineParser& parser);
90 std::vector<std::string> argv;
95 bool graphical_dialog;
99 std::map<std::string,std::string> param_map;
100 std::vector<std::string> input_file;
103 //==========================================================================
105 //==========================================================================
106 void WxProcessCmdLine::Process(wxCmdLineParser& parser)
109 if (parser.Found(_T("D")))
111 bbtk::StaticInitTime::PrintObjectListInfo = true;
114 debug = ( parser.Found(_T("d")) );
115 quiet = ( parser.Found(_T("q")) );
116 help = ( parser.Found(_T("h")) );
117 graphical_dialog = ( parser.Found(_T("wxcommandlineg")) );
118 text_dialog = ( parser.Found(_T("t")) );
119 no_console = ( parser.Found(_T("N")) );
121 if (quiet) bbtk::MessageManager::SetMessageLevel("max",0);
122 if (debug) bbtk::MessageManager::SetMessageLevel("all",9);
124 // parse the arguments and consider those which contain a "="
125 // as set input commands, other as files
126 int pargc = parser.GetParamCount();
127 for (int i=0; i<pargc; ++i)
129 std::string s = bbtk::wx2std(parser.GetParam(i));
130 std::string::size_type pos = s.find_first_of("=");
131 if (std::string::npos != pos)
133 std::string left = s.substr(0,pos);
134 std::string right = s.substr(pos+1,s.size());
135 param_map[left]=right;
139 input_file.push_back(s);
143 bool usage = (help && (input_file.size()==0));
145 std::cout << "BBI (The Black Box Interpreter) - bbtk "
146 << bbtk::GetVersion() << " - (c) Creatis 2007-2008"
152 console = ( parser.Found(_T("c")) ||
153 ((input_file.size() == 0) &&
158 //==========================================================================
161 //==========================================================================
162 class wxBBIApp : public wxApp
167 // std::cout << "wxBBIApp::OnExit()"<<std::endl;
168 // bbtk::Object::PrintObjectListInfo();
170 void OnInitCmdLine(wxCmdLineParser& parser);
171 bool OnCmdLineParsed(wxCmdLineParser& parser);
173 WxProcessCmdLine cmd;
175 //==========================================================================
178 //==========================================================================
179 void wxBBIApp::OnInitCmdLine(wxCmdLineParser& parser)
181 parser.SetDesc(cmdLineDesc);
183 //==========================================================================
185 //==========================================================================
186 bool wxBBIApp::OnCmdLineParsed(wxCmdLineParser& parser)
189 // if (!cmd.proceed) return false;
192 //==========================================================================
199 //==========================================================================
200 // The `main program' equivalent, creating the windows and returning the
202 bool wxBBIApp::OnInit( )
205 //FILE *ff; ff = fopen ("/tmp/wt.log","a+"); fprintf(ff,"EED wxBBIApp::OnInit\n"); fclose(ff);
207 // std::cout << "OnInit"<<std::endl;
210 //See http://www.wxwindows.org/faqgtk.htm#locale
211 setlocale(LC_NUMERIC, "C");
215 if (cmd.quiet) bbtk::MessageManager::SetMessageLevel("max",0);
216 if (cmd.debug) bbtk::MessageManager::SetMessageLevel("all",9);
220 //printf ("EED bbi wxBBIApp::OnInit .....................\n");
221 //cmd.input_file.push_back("/home/davila/Borrame/testwt.bbs");
224 bbtk::WxGUIConsole *I = new bbtk::WxGUIConsole(0,_T("bbi"),wxSize(800,600));
226 if (cmd.console) I->Show(true);
229 I->SetInputs(cmd.param_map);
231 bool help_on_script = cmd.help && (cmd.input_file.size() > 0);
232 if (help_on_script) I->SetNoExecMode(true);
233 if (cmd.graphical_dialog) I->SetDialogMode(bbtk::VirtualExec::GraphicalDialog);
234 if (cmd.text_dialog) I->SetDialogMode(bbtk::VirtualExec::TextDialog);
236 std::vector<std::string>::const_iterator i;
239 for (i=cmd.input_file.begin(); i!=cmd.input_file.end(); ++i)
241 error = ! I->InterpretFile(*i);
244 bool show_on_error = error && ! cmd.no_console;
245 if (show_on_error) I->Show();
247 I->SetNoExecMode(false);
252 I->GetInterpreter()->GetExecuter()->GetFactory()->PrintHelpDescriptor("workspace",package,false);
256 std::cout << "soe="<<show_on_error <<std::endl;
257 std::cout << "con="<<console<<std::endl;
258 std::cout << "iws="<<bbtk::Wx::IsSomeWindowShown()<<std::endl;
260 if (!(show_on_error || cmd.console || bbtk::Wx::IsSomeWindowAlive() ))
263 // std::cout << "I->Close"<<std::endl;
267 // std::cout << "!I->Close"<<std::endl;
270 // std::cout << "EO OnInit"<<std::endl;
275 //==========================================================================
279 //==========================================================================
281 //==========================================================================
282 IMPLEMENT_APP(wxBBIApp);
284 // How to have a Console and wxWidgets
285 // http://www.wxwidgets.org/wiki/index.php/MSVC_Setup_Guide
286 // In Visual C++ 6 (7 should be similar), to create an application that is both a console application
287 // (cout's to the console are visible) and has a wxWidgets GUI,
288 // you need to use the linker option "/subsystem:console" and the following code:
289 int main(int argc, char* argv[])
293 //FILE *ff; ff = fopen ("/tmp/wt.log","a+"); fprintf(ff,"EED main C\n"); fclose(ff);
295 return WinMain(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), SW_SHOWNORMAL);
299 //==========================================================================
300 // OTHER ( Linux... )
301 //==========================================================================
303 IMPLEMENT_APP_NO_MAIN(wxBBIApp);
306 int main(int argc, char* argv[])
310 //FILE *ff; ff = fopen ("/tmp/wt.log","a+"); fprintf(ff,"EED main A\n"); fclose(ff);
312 wxMessageOutput::Set( new wxMessageOutputBest );
314 wxCmdLineParser parser(cmdLineDesc,argc,argv);
315 int val = parser.Parse(false);
321 WxProcessCmdLine cmdline;
322 cmdline.Process(parser);
324 if (!cmdline.proceed) return 0;
326 if (cmdline.no_console)
328 // std::cout << "main NC"<<std::endl;
330 bbtk::Interpreter::Pointer I = bbtk::Interpreter::New();
331 I->SetInputs(cmdline.param_map);
332 bool help_on_script = cmdline.help && (cmdline.input_file.size() > 0);
333 if (help_on_script) I->SetNoExecMode(true);
334 if (cmdline.text_dialog) I->SetDialogMode(bbtk::VirtualExec::TextDialog);
335 std::vector<std::string>::const_iterator i;
337 for (i=cmdline.input_file.begin();
338 i!=cmdline.input_file.end(); ++i)
340 error = ! I->InterpretFile(*i);
345 I->SetNoExecMode(false);
347 I->GetExecuter()->GetFactory()->PrintHelpDescriptor("workspace",
351 if (cmdline.input_file.size()==0)
352 I->CommandLineInterpreter();
364 #endif // defined(_WIN32)
365 //==========================================================================
375 //==========================================================================
377 //==========================================================================
379 #include "bbtkInterpreter.h"
381 // Processes the command line parsing result
382 struct ProcessCmdLine
385 void Process(int argc_, char *argv_[]);
386 bool Found(std::string value);
389 std::vector<std::string> argv;
394 bool graphical_dialog;
398 std::map<std::string,std::string> param_map;
399 std::vector<std::string> input_file;
403 bool ProcessCmdLine::Found(std::string value)
406 for (int i=1; i<argc; ++i)
416 void ProcessCmdLine::Process(int argc_, char *argv_[])
419 for (int i=0; i<argc; ++i)
421 argv.push_back(argv_[i]);
424 //EED proceed = true;
427 bbtk::StaticInitTime::PrintObjectListInfo = true;
430 debug = ( Found("d") );
431 quiet = ( Found("q") );
432 help = ( Found("h") );
433 graphical_dialog = ( Found("g") );
434 text_dialog = ( Found("t") );
435 no_console = ( Found("N") );
437 if (quiet) bbtk::MessageManager::SetMessageLevel("max",0);
438 if (debug) bbtk::MessageManager::SetMessageLevel("all",9);
440 // parse the arguments and consider those which contain a "="
441 // as set input commands, other as files
442 //EED int pargc = parser.GetParamCount();
443 for (int i=1; i<argc; ++i)
445 std::string s = argv[i];
446 std::string::size_type pos = s.find_first_of("=");
447 if (std::string::npos != pos)
449 std::string left = s.substr(0,pos);
450 std::string right = s.substr(pos+1,s.size());
451 param_map[left]=right;
455 input_file.push_back(s);
459 bool usage = (help && (input_file.size()==0));
461 std::cout << "BBI (The Black Box Interpreter) - bbtk "
462 << bbtk::GetVersion() << " - (c) Creatis 2007-2008"
464 //EED parser.Usage();
468 console = ( Found("c") ||
469 ((input_file.size() == 0) &&
474 //==========================================================================
478 int main(int argc, char* argv[])
483 // std::cout << "usage : "<<argv[0]<<" [filename]"<<std::endl;
487 std::cout << "BBI (Black Box Interpreter) - bbtk "
488 << bbtk::GetVersion()<< " - (c) Creatis 2007"
491 //FILE *ff; ff = fopen ("/tmp/wt.log","a+"); fprintf(ff,"EED main B\n"); fclose(ff);
493 bbtk::Interpreter::Pointer I = bbtk::Interpreter::New();
496 I->CommandLineInterpreter();
500 cmd.Process(argc,argv);
501 I->SetInputs(cmd.param_map);
503 std::string f(argv[1]);
507 // bbtk::Wx::LoopUntilAllWindowsClose();
514 #endif //#ifdef _USE_WXWIDGETS_