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 //==========================================================================
43 // Command line options definition
44 static const wxCmdLineEntryDesc cmdLineDesc[] =
46 { wxCMD_LINE_SWITCH, _T("h"), _T("help"), _T("print this help or help on the application defined in input bbs file if any") },
47 { wxCMD_LINE_SWITCH, _T("g"), _T("graphical-dialog"), _T("prompt the input parameter values using graphical dialog") },
48 { wxCMD_LINE_SWITCH, _T("t"), _T("text-dialog"), _T("prompt the input parameter values in text mode") },
49 { wxCMD_LINE_SWITCH, _T("c"), _T("console"), _T("open bbi console") },
50 { wxCMD_LINE_SWITCH, _T("N"), _T("no-console"), _T("never open bbi console even on error") },
51 { wxCMD_LINE_SWITCH, _T("q"), _T("quiet"), _T("be quiet (='message max 0')") },
52 { wxCMD_LINE_SWITCH, _T("d"), _T("debug"), _T("turn all messages on (='message all 9')") },
53 { wxCMD_LINE_SWITCH, _T("D"), _T("Debug"), _T("memory debug on exit (='debug -D')") },
54 { wxCMD_LINE_PARAM, NULL, NULL, _T("file [file [...]]"), wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE },
57 //==========================================================================
59 //==========================================================================
60 // Processes the command line parsing result
61 struct WxProcessCmdLine
64 void Process(wxCmdLineParser& parser);
67 std::vector<std::string> argv;
72 bool graphical_dialog;
76 std::map<std::string,std::string> param_map;
77 std::vector<std::string> input_file;
80 //==========================================================================
82 //==========================================================================
83 void WxProcessCmdLine::Process(wxCmdLineParser& parser)
86 if (parser.Found(_T("D")))
88 bbtk::StaticInitTime::PrintObjectListInfo = true;
91 debug = ( parser.Found(_T("d")) );
92 quiet = ( parser.Found(_T("q")) );
93 help = ( parser.Found(_T("h")) );
94 graphical_dialog = ( parser.Found(_T("wxcommandlineg")) );
95 text_dialog = ( parser.Found(_T("t")) );
96 no_console = ( parser.Found(_T("N")) );
98 if (quiet) bbtk::MessageManager::SetMessageLevel("max",0);
99 if (debug) bbtk::MessageManager::SetMessageLevel("all",9);
101 // parse the arguments and consider those which contain a "="
102 // as set input commands, other as files
103 int pargc = parser.GetParamCount();
104 for (int i=0; i<pargc; ++i)
106 std::string s = bbtk::wx2std(parser.GetParam(i));
107 std::string::size_type pos = s.find_first_of("=");
108 if (std::string::npos != pos)
110 std::string left = s.substr(0,pos);
111 std::string right = s.substr(pos+1,s.size());
112 param_map[left]=right;
116 input_file.push_back(s);
120 bool usage = (help && (input_file.size()==0));
122 std::cout << "BBI (The Black Box Interpreter) - bbtk "
123 << bbtk::GetVersion() << " - (c) Creatis 2007-2008"
129 console = ( parser.Found(_T("c")) ||
130 ((input_file.size() == 0) &&
135 //==========================================================================
138 //==========================================================================
139 class wxBBIApp : public wxApp
144 // std::cout << "wxBBIApp::OnExit()"<<std::endl;
145 // bbtk::Object::PrintObjectListInfo();
147 void OnInitCmdLine(wxCmdLineParser& parser);
148 bool OnCmdLineParsed(wxCmdLineParser& parser);
150 WxProcessCmdLine cmd;
152 //==========================================================================
155 //==========================================================================
156 void wxBBIApp::OnInitCmdLine(wxCmdLineParser& parser)
158 parser.SetDesc(cmdLineDesc);
160 //==========================================================================
162 //==========================================================================
163 bool wxBBIApp::OnCmdLineParsed(wxCmdLineParser& parser)
166 // if (!cmd.proceed) return false;
169 //==========================================================================
176 //==========================================================================
177 // The `main program' equivalent, creating the windows and returning the
179 bool wxBBIApp::OnInit( )
182 //FILE *ff; ff = fopen ("/tmp/wt.log","a+"); fprintf(ff,"EED wxBBIApp::OnInit\n"); fclose(ff);
184 // std::cout << "OnInit"<<std::endl;
187 //See http://www.wxwindows.org/faqgtk.htm#locale
188 setlocale(LC_NUMERIC, "C");
192 if (cmd.quiet) bbtk::MessageManager::SetMessageLevel("max",0);
193 if (cmd.debug) bbtk::MessageManager::SetMessageLevel("all",9);
197 //printf ("EED bbi wxBBIApp::OnInit .....................\n");
198 //cmd.input_file.push_back("/home/davila/Borrame/testwt.bbs");
201 bbtk::WxGUIConsole *I = new bbtk::WxGUIConsole(0,_T("bbi"),wxSize(800,600));
203 if (cmd.console) I->Show(true);
206 I->SetInputs(cmd.param_map);
208 bool help_on_script = cmd.help && (cmd.input_file.size() > 0);
209 if (help_on_script) I->SetNoExecMode(true);
210 if (cmd.graphical_dialog) I->SetDialogMode(bbtk::VirtualExec::GraphicalDialog);
211 if (cmd.text_dialog) I->SetDialogMode(bbtk::VirtualExec::TextDialog);
213 std::vector<std::string>::const_iterator i;
216 for (i=cmd.input_file.begin(); i!=cmd.input_file.end(); ++i)
218 error = ! I->InterpretFile(*i);
221 bool show_on_error = error && ! cmd.no_console;
222 if (show_on_error) I->Show();
224 I->SetNoExecMode(false);
229 I->GetInterpreter()->GetExecuter()->GetFactory()->PrintHelpDescriptor("workspace",package,false);
233 std::cout << "soe="<<show_on_error <<std::endl;
234 std::cout << "con="<<console<<std::endl;
235 std::cout << "iws="<<bbtk::Wx::IsSomeWindowShown()<<std::endl;
237 if (!(show_on_error || cmd.console || bbtk::Wx::IsSomeWindowAlive() ))
240 // std::cout << "I->Close"<<std::endl;
244 // std::cout << "!I->Close"<<std::endl;
247 // std::cout << "EO OnInit"<<std::endl;
252 //==========================================================================
256 //==========================================================================
258 //==========================================================================
259 IMPLEMENT_APP(wxBBIApp);
261 // How to have a Console and wxWidgets
262 // http://www.wxwidgets.org/wiki/index.php/MSVC_Setup_Guide
263 // In Visual C++ 6 (7 should be similar), to create an application that is both a console application
264 // (cout's to the console are visible) and has a wxWidgets GUI,
265 // you need to use the linker option "/subsystem:console" and the following code:
266 int main(int argc, char* argv[])
270 //FILE *ff; ff = fopen ("/tmp/wt.log","a+"); fprintf(ff,"EED main C\n"); fclose(ff);
272 return WinMain(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), SW_SHOWNORMAL);
276 //==========================================================================
277 // OTHER ( Linux... )
278 //==========================================================================
280 IMPLEMENT_APP_NO_MAIN(wxBBIApp);
283 int main(int argc, char* argv[])
287 //FILE *ff; ff = fopen ("/tmp/wt.log","a+"); fprintf(ff,"EED main A\n"); fclose(ff);
289 wxMessageOutput::Set( new wxMessageOutputBest );
291 wxCmdLineParser parser(cmdLineDesc,argc,argv);
292 int val = parser.Parse(false);
298 WxProcessCmdLine cmdline;
299 cmdline.Process(parser);
301 if (!cmdline.proceed) return 0;
303 if (cmdline.no_console)
305 // std::cout << "main NC"<<std::endl;
307 bbtk::Interpreter::Pointer I = bbtk::Interpreter::New();
308 I->SetInputs(cmdline.param_map);
309 bool help_on_script = cmdline.help && (cmdline.input_file.size() > 0);
310 if (help_on_script) I->SetNoExecMode(true);
311 if (cmdline.text_dialog) I->SetDialogMode(bbtk::VirtualExec::TextDialog);
312 std::vector<std::string>::const_iterator i;
314 for (i=cmdline.input_file.begin();
315 i!=cmdline.input_file.end(); ++i)
317 error = ! I->InterpretFile(*i);
322 I->SetNoExecMode(false);
324 I->GetExecuter()->GetFactory()->PrintHelpDescriptor("workspace",
328 if (cmdline.input_file.size()==0)
329 I->CommandLineInterpreter();
341 #endif // defined(_WIN32)
342 //==========================================================================
352 //==========================================================================
354 //==========================================================================
356 #include "bbtkInterpreter.h"
358 // Processes the command line parsing result
359 struct ProcessCmdLine
362 void Process(int argc_, char *argv_[]);
363 bool Found(std::string value);
366 std::vector<std::string> argv;
371 bool graphical_dialog;
375 std::map<std::string,std::string> param_map;
376 std::vector<std::string> input_file;
380 bool ProcessCmdLine::Found(std::string value)
383 for (int i=1; i<argc; ++i)
393 void ProcessCmdLine::Process(int argc_, char *argv_[])
396 for (int i=0; i<argc; ++i)
398 argv.push_back(argv_[i]);
401 //EED proceed = true;
404 bbtk::StaticInitTime::PrintObjectListInfo = true;
407 debug = ( Found("d") );
408 quiet = ( Found("q") );
409 help = ( Found("h") );
410 graphical_dialog = ( Found("g") );
411 text_dialog = ( Found("t") );
412 no_console = ( Found("N") );
414 if (quiet) bbtk::MessageManager::SetMessageLevel("max",0);
415 if (debug) bbtk::MessageManager::SetMessageLevel("all",9);
417 // parse the arguments and consider those which contain a "="
418 // as set input commands, other as files
419 //EED int pargc = parser.GetParamCount();
420 for (int i=1; i<argc; ++i)
422 std::string s = argv[i];
423 std::string::size_type pos = s.find_first_of("=");
424 if (std::string::npos != pos)
426 std::string left = s.substr(0,pos);
427 std::string right = s.substr(pos+1,s.size());
428 param_map[left]=right;
432 input_file.push_back(s);
436 bool usage = (help && (input_file.size()==0));
438 std::cout << "BBI (The Black Box Interpreter) - bbtk "
439 << bbtk::GetVersion() << " - (c) Creatis 2007-2008"
441 //EED parser.Usage();
445 console = ( Found("c") ||
446 ((input_file.size() == 0) &&
451 //==========================================================================
455 int main(int argc, char* argv[])
460 // std::cout << "usage : "<<argv[0]<<" [filename]"<<std::endl;
464 std::cout << "BBI (Black Box Interpreter) - bbtk "
465 << bbtk::GetVersion()<< " - (c) Creatis 2007"
468 //FILE *ff; ff = fopen ("/tmp/wt.log","a+"); fprintf(ff,"EED main B\n"); fclose(ff);
470 bbtk::Interpreter::Pointer I = bbtk::Interpreter::New();
473 I->CommandLineInterpreter();
477 cmd.Process(argc,argv);
478 I->SetInputs(cmd.param_map);
480 std::string f(argv[1]);
484 // bbtk::Wx::LoopUntilAllWindowsClose();
491 #endif //#ifdef _USE_WXWIDGETS_