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_
32 //==========================================================================
34 //==========================================================================
35 #include "bbtkObject.h"
36 #include "bbtkInterpreter.h"
37 #include "bbtkWxBlackBox.h"
38 #include "bbtkWxGUIConsole.h"
40 #include <wx/cmdline.h>
44 //==========================================================================
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[] =
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 },
63 // Command line options definition
64 static const wxCmdLineEntryDesc cmdLineDesc[] =
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 },
82 //==========================================================================
84 //==========================================================================
85 // Processes the command line parsing result
86 struct WxProcessCmdLine
89 void Process(wxCmdLineParser& parser);
92 std::vector<std::string> argv;
97 bool graphical_dialog;
101 std::map<std::string,std::string> param_map;
102 std::vector<std::string> input_file;
105 //==========================================================================
107 //==========================================================================
108 void WxProcessCmdLine::Process(wxCmdLineParser& parser)
111 if (parser.Found(_T("D")))
113 bbtk::StaticInitTime::PrintObjectListInfo = true;
116 debug = ( parser.Found(_T("d")) );
117 quiet = ( parser.Found(_T("q")) );
118 help = ( parser.Found(_T("h")) );
120 //EED 2017-09-16 Migration wxWidgets 2.8 to 3.0
121 #if wxMAJOR_VERSION <= 2
122 graphical_dialog = ( parser.Found(_T("wxcommandlineg")) );
124 printf("EED Warnning. WxProcessCmdLine::Process g wxcommandlineg ");
125 graphical_dialog = ( parser.Found(_T("g")) );
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);
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)
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)
142 std::string left = s.substr(0,pos);
143 std::string right = s.substr(pos+1,s.size());
144 param_map[left]=right;
148 input_file.push_back(s);
152 bool usage = (help && (input_file.size()==0));
154 std::cout << "BBI (The Black Box Interpreter) - bbtk "
155 << bbtk::GetVersion() << " - (c) Creatis 2007-2008"
161 console = ( parser.Found(_T("c")) ||
162 ((input_file.size() == 0) &&
167 //==========================================================================
170 //==========================================================================
171 class wxBBIApp : public wxApp
176 // std::cout << "wxBBIApp::OnExit()"<<std::endl;
177 // bbtk::Object::PrintObjectListInfo();
179 void OnInitCmdLine(wxCmdLineParser& parser);
180 bool OnCmdLineParsed(wxCmdLineParser& parser);
182 WxProcessCmdLine cmd;
184 //==========================================================================
187 //==========================================================================
188 void wxBBIApp::OnInitCmdLine(wxCmdLineParser& parser)
190 parser.SetDesc(cmdLineDesc);
192 //==========================================================================
194 //==========================================================================
195 bool wxBBIApp::OnCmdLineParsed(wxCmdLineParser& parser)
198 // if (!cmd.proceed) return false;
201 //==========================================================================
208 //==========================================================================
209 // The `main program' equivalent, creating the windows and returning the
211 bool wxBBIApp::OnInit( )
214 //FILE *ff; ff = fopen ("/tmp/wt.log","a+"); fprintf(ff,"EED wxBBIApp::OnInit\n"); fclose(ff);
216 // std::cout << "OnInit"<<std::endl;
219 //See http://www.wxwindows.org/faqgtk.htm#locale
220 setlocale(LC_NUMERIC, "C");
224 if (cmd.quiet) bbtk::MessageManager::SetMessageLevel("max",0);
225 if (cmd.debug) bbtk::MessageManager::SetMessageLevel("all",9);
229 //printf ("EED bbi wxBBIApp::OnInit .....................\n");
230 //cmd.input_file.push_back("/home/davila/Borrame/testwt.bbs");
233 bbtk::WxGUIConsole *I = new bbtk::WxGUIConsole(0,_T("bbi"),wxSize(800,600));
235 if (cmd.console) I->Show(true);
238 I->SetInputs(cmd.param_map);
240 bool help_on_script = cmd.help && (cmd.input_file.size() > 0);
241 if (help_on_script) I->SetNoExecMode(true);
242 if (cmd.graphical_dialog) I->SetDialogMode(bbtk::VirtualExec::GraphicalDialog);
243 if (cmd.text_dialog) I->SetDialogMode(bbtk::VirtualExec::TextDialog);
245 std::vector<std::string>::const_iterator i;
248 for (i=cmd.input_file.begin(); i!=cmd.input_file.end(); ++i)
250 error = ! I->InterpretFile(*i);
253 bool show_on_error = error && ! cmd.no_console;
254 if (show_on_error) I->Show();
256 I->SetNoExecMode(false);
261 I->GetInterpreter()->GetExecuter()->GetFactory()->PrintHelpDescriptor("workspace",package,false);
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;
269 if (!(show_on_error || cmd.console || bbtk::Wx::IsSomeWindowAlive() ))
272 // std::cout << "I->Close"<<std::endl;
276 // std::cout << "!I->Close"<<std::endl;
279 // std::cout << "EO OnInit"<<std::endl;
284 //==========================================================================
288 //==========================================================================
290 //==========================================================================
291 IMPLEMENT_APP(wxBBIApp);
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[])
302 //FILE *ff; ff = fopen ("/tmp/wt.log","a+"); fprintf(ff,"EED main C\n"); fclose(ff);
304 return WinMain(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), SW_SHOWNORMAL);
308 //==========================================================================
309 // OTHER ( Linux... )
310 //==========================================================================
312 IMPLEMENT_APP_NO_MAIN(wxBBIApp);
315 int main(int argc, char* argv[])
319 //FILE *ff; ff = fopen ("/tmp/wt.log","a+"); fprintf(ff,"EED main A\n"); fclose(ff);
321 wxMessageOutput::Set( new wxMessageOutputBest );
323 wxCmdLineParser parser(cmdLineDesc,argc,argv);
324 int val = parser.Parse(false);
330 WxProcessCmdLine cmdline;
331 cmdline.Process(parser);
333 if (!cmdline.proceed) return 0;
335 if (cmdline.no_console)
337 // std::cout << "main NC"<<std::endl;
339 bbtk::Interpreter::Pointer I = bbtk::Interpreter::New();
340 I->SetInputs(cmdline.param_map);
341 bool help_on_script = cmdline.help && (cmdline.input_file.size() > 0);
342 if (help_on_script) I->SetNoExecMode(true);
343 if (cmdline.text_dialog) I->SetDialogMode(bbtk::VirtualExec::TextDialog);
344 std::vector<std::string>::const_iterator i;
346 for (i=cmdline.input_file.begin();
347 i!=cmdline.input_file.end(); ++i)
349 error = ! I->InterpretFile(*i);
354 I->SetNoExecMode(false);
356 I->GetExecuter()->GetFactory()->PrintHelpDescriptor("workspace",
360 if (cmdline.input_file.size()==0)
362 I->CommandLineInterpreter();
363 } // if cmdline.input_file.size
367 } // if cmdline.no_console
372 #endif // defined(_WIN32)
373 //==========================================================================
383 //==========================================================================
385 //==========================================================================
387 #include "bbtkInterpreter.h"
389 // Processes the command line parsing result
390 struct ProcessCmdLine
393 void Process(int argc_, char *argv_[]);
394 bool Found(std::string value);
397 std::vector<std::string> argv;
402 bool graphical_dialog;
406 std::map<std::string,std::string> param_map;
407 std::vector<std::string> input_file;
411 bool ProcessCmdLine::Found(std::string value)
414 for (int i=1; i<argc; ++i)
424 void ProcessCmdLine::Process(int argc_, char *argv_[])
427 for (int i=0; i<argc; ++i)
429 argv.push_back(argv_[i]);
432 //EED proceed = true;
435 bbtk::StaticInitTime::PrintObjectListInfo = true;
438 debug = ( Found("d") );
439 quiet = ( Found("q") );
440 help = ( Found("h") );
441 graphical_dialog = ( Found("g") );
442 text_dialog = ( Found("t") );
443 no_console = ( Found("N") );
445 if (quiet) bbtk::MessageManager::SetMessageLevel("max",0);
446 if (debug) bbtk::MessageManager::SetMessageLevel("all",9);
448 // parse the arguments and consider those which contain a "="
449 // as set input commands, other as files
450 //EED int pargc = parser.GetParamCount();
451 for (int i=1; i<argc; ++i)
453 std::string s = argv[i];
454 std::string::size_type pos = s.find_first_of("=");
455 if (std::string::npos != pos)
457 std::string left = s.substr(0,pos);
458 std::string right = s.substr(pos+1,s.size());
459 param_map[left]=right;
463 input_file.push_back(s);
467 bool usage = (help && (input_file.size()==0));
469 std::cout << "BBI (The Black Box Interpreter) - bbtk "
470 << bbtk::GetVersion() << " - (c) Creatis 2007-2008"
472 //EED parser.Usage();
476 console = ( Found("c") ||
477 ((input_file.size() == 0) &&
482 //==========================================================================
486 int main(int argc, char* argv[])
491 // std::cout << "usage : "<<argv[0]<<" [filename]"<<std::endl;
495 std::cout << "BBI (Black Box Interpreter) - bbtk "
496 << bbtk::GetVersion()<< " - (c) Creatis 2007"
499 //FILE *ff; ff = fopen ("/tmp/wt.log","a+"); fprintf(ff,"EED main B\n"); fclose(ff);
501 bbtk::Interpreter::Pointer I = bbtk::Interpreter::New();
504 I->CommandLineInterpreter();
508 cmd.Process(argc,argv);
509 I->SetInputs(cmd.param_map);
511 std::string f(argv[1]);
515 // bbtk::Wx::LoopUntilAllWindowsClose();
522 #endif //#ifdef _USE_WXWIDGETS_