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 # ------------------------------------------------------------------------ */
27 //__asm__(".symver fcntl64,fcntl64@GLIBC_2.27");
29 #ifdef _USE_WXWIDGETS_
34 //==========================================================================
36 //==========================================================================
37 #include "bbtkObject.h"
38 #include "bbtkInterpreter.h"
39 #include "bbtkWxBlackBox.h"
40 #include "bbtkWxGUIConsole.h"
42 #include <wx/cmdline.h>
46 //==========================================================================
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[] =
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 },
65 // Command line options definition
66 static const wxCmdLineEntryDesc cmdLineDesc[] =
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 },
84 //==========================================================================
86 //==========================================================================
87 // Processes the command line parsing result
88 struct WxProcessCmdLine
91 void Process(wxCmdLineParser& parser);
94 std::vector<std::string> argv;
99 bool graphical_dialog;
103 std::map<std::string,std::string> param_map;
104 std::vector<std::string> input_file;
107 //==========================================================================
109 //==========================================================================
110 void WxProcessCmdLine::Process(wxCmdLineParser& parser)
113 if (parser.Found(_T("D")))
115 bbtk::StaticInitTime::PrintObjectListInfo = true;
118 debug = ( parser.Found(_T("d")) );
119 quiet = ( parser.Found(_T("q")) );
120 help = ( parser.Found(_T("h")) );
122 //EED 2017-09-16 Migration wxWidgets 2.8 to 3.0
123 #if wxMAJOR_VERSION <= 2
124 graphical_dialog = ( parser.Found(_T("wxcommandlineg")) );
126 printf("EED Warnning. WxProcessCmdLine::Process g wxcommandlineg \n");
127 graphical_dialog = ( parser.Found(_T("g")) );
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);
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)
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)
144 std::string left = s.substr(0,pos);
145 std::string right = s.substr(pos+1,s.size());
146 param_map[left]=right;
150 input_file.push_back(s);
154 bool usage = (help && (input_file.size()==0));
156 std::cout << "BBI (The Black Box Interpreter) - bbtk "
157 << bbtk::GetVersion() << " - (c) Creatis 2007-2008"
163 console = ( parser.Found(_T("c")) ||
164 ((input_file.size() == 0) &&
169 //==========================================================================
172 //==========================================================================
173 class wxBBIApp : public wxApp
178 // std::cout << "wxBBIApp::OnExit()"<<std::endl;
179 // bbtk::Object::PrintObjectListInfo();
181 void OnInitCmdLine(wxCmdLineParser& parser);
182 bool OnCmdLineParsed(wxCmdLineParser& parser);
184 WxProcessCmdLine cmd;
186 //==========================================================================
189 //==========================================================================
190 void wxBBIApp::OnInitCmdLine(wxCmdLineParser& parser)
192 parser.SetDesc(cmdLineDesc);
194 //==========================================================================
196 //==========================================================================
197 bool wxBBIApp::OnCmdLineParsed(wxCmdLineParser& parser)
200 // if (!cmd.proceed) return false;
203 //==========================================================================
207 //==========================================================================
208 // The `main program' equivalent, creating the windows and returning the
210 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");
223 if (cmd.quiet) bbtk::MessageManager::SetMessageLevel("max",0);
224 if (cmd.debug) bbtk::MessageManager::SetMessageLevel("all",9);
228 //printf ("EED bbi wxBBIApp::OnInit .....................\n");
229 //cmd.input_file.push_back("/home/davila/Borrame/testwt.bbs");
231 bbtk::WxGUIConsole *I = new bbtk::WxGUIConsole(0,_T("bbi"),wxSize(800,600));
233 if (cmd.console) I->Show(true);
236 I->SetInputs(cmd.param_map);
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);
243 std::vector<std::string>::const_iterator i;
247 for (i=cmd.input_file.begin(); i!=cmd.input_file.end(); ++i)
249 error = ! I->InterpretFile(*i);
252 bool show_on_error = error && ! cmd.no_console;
253 if (show_on_error) I->Show();
255 I->SetNoExecMode(false);
260 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[])
301 //FILE *ff; ff = fopen ("/tmp/wt.log","a+"); fprintf(ff,"EED main C\n"); fclose(ff);
305 wcstombs(buffer , ::GetCommandLine() , 1500 );
306 return WinMain(::GetModuleHandle(NULL), NULL, buffer , SW_SHOWNORMAL);
307 // return WinMain(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), SW_SHOWNORMAL);
312 //==========================================================================
313 // OTHER ( Linux... )
314 //==========================================================================
316 IMPLEMENT_APP_NO_MAIN(wxBBIApp);
319 int main(int argc, char* argv[])
322 //FILE *ff; ff = fopen ("/tmp/wt.log","a+"); fprintf(ff,"EED main A\n"); fclose(ff);
324 wxMessageOutput::Set( new wxMessageOutputBest );
326 wxCmdLineParser parser(cmdLineDesc,argc,argv);
327 int val = parser.Parse(false);
333 WxProcessCmdLine cmdline;
334 cmdline.Process(parser);
336 if (!cmdline.proceed) return 0;
338 if (cmdline.no_console)
340 // std::cout << "main NC"<<std::endl;
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;
349 for (i=cmdline.input_file.begin();
350 i!=cmdline.input_file.end(); ++i)
352 error = ! I->InterpretFile(*i);
357 I->SetNoExecMode(false);
359 I->GetExecuter()->GetFactory()->PrintHelpDescriptor("workspace",
363 if (cmdline.input_file.size()==0)
365 I->CommandLineInterpreter();
366 } // if cmdline.input_file.size
370 } // if cmdline.no_console
375 #endif // defined(_WIN32)
376 //==========================================================================
386 //==========================================================================
388 //==========================================================================
390 #include "bbtkInterpreter.h"
392 // Processes the command line parsing result
393 struct ProcessCmdLine
396 void Process(int argc_, char *argv_[]);
397 bool Found(std::string value);
400 std::vector<std::string> argv;
405 bool graphical_dialog;
409 std::map<std::string,std::string> param_map;
410 std::vector<std::string> input_file;
414 bool ProcessCmdLine::Found(std::string value)
417 for (int i=1; i<argc; ++i)
427 void ProcessCmdLine::Process(int argc_, char *argv_[])
430 for (int i=0; i<argc; ++i)
432 argv.push_back(argv_[i]);
435 //EED proceed = true;
438 bbtk::StaticInitTime::PrintObjectListInfo = true;
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") );
448 if (quiet) bbtk::MessageManager::SetMessageLevel("max",0);
449 if (debug) bbtk::MessageManager::SetMessageLevel("all",9);
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)
456 std::string s = argv[i];
457 std::string::size_type pos = s.find_first_of("=");
458 if (std::string::npos != pos)
460 std::string left = s.substr(0,pos);
461 std::string right = s.substr(pos+1,s.size());
462 param_map[left]=right;
466 input_file.push_back(s);
470 bool usage = (help && (input_file.size()==0));
472 std::cout << "BBI (The Black Box Interpreter) - bbtk "
473 << bbtk::GetVersion() << " - (c) Creatis 2007-2008"
475 //EED parser.Usage();
479 console = ( Found("c") ||
480 ((input_file.size() == 0) &&
485 //==========================================================================
489 int main(int argc, char* argv[])
494 // std::cout << "usage : "<<argv[0]<<" [filename]"<<std::endl;
498 std::cout << "BBI (Black Box Interpreter) - bbtk "
499 << bbtk::GetVersion()<< " - (c) Creatis 2007"
502 //FILE *ff; ff = fopen ("/tmp/wt.log","a+"); fprintf(ff,"EED main B\n"); fclose(ff);
504 bbtk::Interpreter::Pointer I = bbtk::Interpreter::New();
507 I->CommandLineInterpreter();
511 cmd.Process(argc,argv);
512 I->SetInputs(cmd.param_map);
514 std::string f(argv[1]);
518 // bbtk::Wx::LoopUntilAllWindowsClose();
525 #endif //#ifdef _USE_WXWIDGETS_