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("g")) );
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( )
181 // std::cout << "OnInit"<<std::endl;
184 //See http://www.wxwindows.org/faqgtk.htm#locale
185 setlocale(LC_NUMERIC, "C");
189 if (cmd.quiet) bbtk::MessageManager::SetMessageLevel("max",0);
190 if (cmd.debug) bbtk::MessageManager::SetMessageLevel("all",9);
193 bbtk::WxGUIConsole *I = new bbtk::WxGUIConsole(0,_T("bbi"),wxSize(800,600));
195 if (cmd.console) I->Show(true);
198 I->SetInputs(cmd.param_map);
200 bool help_on_script = cmd.help && (cmd.input_file.size() > 0);
201 if (help_on_script) I->SetNoExecMode(true);
202 if (cmd.graphical_dialog) I->SetDialogMode(bbtk::VirtualExec::GraphicalDialog);
203 if (cmd.text_dialog) I->SetDialogMode(bbtk::VirtualExec::TextDialog);
205 std::vector<std::string>::const_iterator i;
208 for (i=cmd.input_file.begin(); i!=cmd.input_file.end(); ++i)
210 error = ! I->InterpretFile(*i);
213 bool show_on_error = error && ! cmd.no_console;
214 if (show_on_error) I->Show();
216 I->SetNoExecMode(false);
221 I->GetInterpreter()->GetExecuter()->GetFactory()->PrintHelpDescriptor("workspace",package,false);
225 std::cout << "soe="<<show_on_error <<std::endl;
226 std::cout << "con="<<console<<std::endl;
227 std::cout << "iws="<<bbtk::Wx::IsSomeWindowShown()<<std::endl;
229 if (!(show_on_error || cmd.console || bbtk::Wx::IsSomeWindowAlive() ))
232 // std::cout << "I->Close"<<std::endl;
236 // std::cout << "!I->Close"<<std::endl;
239 // std::cout << "EO OnInit"<<std::endl;
244 //==========================================================================
248 //==========================================================================
250 //==========================================================================
251 IMPLEMENT_APP(wxBBIApp);
253 // How to have a Console and wxWidgets
254 // http://www.wxwidgets.org/wiki/index.php/MSVC_Setup_Guide
255 // In Visual C++ 6 (7 should be similar), to create an application that is both a console application
256 // (cout's to the console are visible) and has a wxWidgets GUI,
257 // you need to use the linker option "/subsystem:console" and the following code:
258 int main(int argc, char* argv[])
260 return WinMain(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), SW_SHOWNORMAL);
264 //==========================================================================
265 // OTHER ( Linux... )
266 //==========================================================================
268 IMPLEMENT_APP_NO_MAIN(wxBBIApp);
271 int main(int argc, char* argv[])
273 wxMessageOutput::Set( new wxMessageOutputBest );
275 wxCmdLineParser parser(cmdLineDesc,argc,argv);
276 int val = parser.Parse(false);
282 WxProcessCmdLine cmdline;
283 cmdline.Process(parser);
285 if (!cmdline.proceed) return 0;
287 if (cmdline.no_console)
289 // std::cout << "main NC"<<std::endl;
291 bbtk::Interpreter::Pointer I = bbtk::Interpreter::New();
292 I->SetInputs(cmdline.param_map);
293 bool help_on_script = cmdline.help && (cmdline.input_file.size() > 0);
294 if (help_on_script) I->SetNoExecMode(true);
295 if (cmdline.text_dialog) I->SetDialogMode(bbtk::VirtualExec::TextDialog);
296 std::vector<std::string>::const_iterator i;
298 for (i=cmdline.input_file.begin();
299 i!=cmdline.input_file.end(); ++i)
301 error = ! I->InterpretFile(*i);
306 I->SetNoExecMode(false);
308 I->GetExecuter()->GetFactory()->PrintHelpDescriptor("workspace",
312 if (cmdline.input_file.size()==0)
313 I->CommandLineInterpreter();
325 #endif // defined(_WIN32)
326 //==========================================================================
336 //==========================================================================
338 //==========================================================================
340 #include "bbtkInterpreter.h"
342 // Processes the command line parsing result
343 struct ProcessCmdLine
346 void Process(int argc_, char *argv_[]);
347 bool Found(std::string value);
350 std::vector<std::string> argv;
355 bool graphical_dialog;
359 std::map<std::string,std::string> param_map;
360 std::vector<std::string> input_file;
364 bool ProcessCmdLine::Found(std::string value)
367 for (int i=1; i<argc; ++i)
377 void ProcessCmdLine::Process(int argc_, char *argv_[])
380 for (int i=0; i<argc; ++i)
382 argv.push_back(argv_[i]);
385 //EED proceed = true;
388 bbtk::StaticInitTime::PrintObjectListInfo = true;
391 debug = ( Found("d") );
392 quiet = ( Found("q") );
393 help = ( Found("h") );
394 graphical_dialog = ( Found("g") );
395 text_dialog = ( Found("t") );
396 no_console = ( Found("N") );
398 if (quiet) bbtk::MessageManager::SetMessageLevel("max",0);
399 if (debug) bbtk::MessageManager::SetMessageLevel("all",9);
401 // parse the arguments and consider those which contain a "="
402 // as set input commands, other as files
403 //EED int pargc = parser.GetParamCount();
404 for (int i=1; i<argc; ++i)
406 std::string s = argv[i];
407 std::string::size_type pos = s.find_first_of("=");
408 if (std::string::npos != pos)
410 std::string left = s.substr(0,pos);
411 std::string right = s.substr(pos+1,s.size());
412 param_map[left]=right;
416 input_file.push_back(s);
420 bool usage = (help && (input_file.size()==0));
422 std::cout << "BBI (The Black Box Interpreter) - bbtk "
423 << bbtk::GetVersion() << " - (c) Creatis 2007-2008"
425 //EED parser.Usage();
429 console = ( Found("c") ||
430 ((input_file.size() == 0) &&
435 //==========================================================================
439 int main(int argc, char* argv[])
444 // std::cout << "usage : "<<argv[0]<<" [filename]"<<std::endl;
448 std::cout << "BBI (Black Box Interpreter) - bbtk "
449 << bbtk::GetVersion()<< " - (c) Creatis 2007"
452 bbtk::Interpreter::Pointer I = bbtk::Interpreter::New();
455 I->CommandLineInterpreter();
459 cmd.Process(argc,argv);
460 I->SetInputs(cmd.param_map);
462 std::string f(argv[1]);
466 // bbtk::Wx::LoopUntilAllWindowsClose();
473 #endif //#ifdef _USE_WXWIDGETS_