3 //==========================================================================
5 //==========================================================================
6 #include "bbtkObject.h"
7 #include "bbtkInterpreter.h"
8 #include "bbtkWxBlackBox.h"
9 #include "bbtkWxGUIConsole.h"
11 #include <wx/cmdline.h>
15 //==========================================================================
16 // Command line options definition
17 static const wxCmdLineEntryDesc cmdLineDesc[] =
19 { wxCMD_LINE_SWITCH, _T("h"), _T("help"), _T("print this help or help on the application defined in input bbs file if any") },
20 { wxCMD_LINE_SWITCH, _T("g"), _T("graphical-dialog"), _T("prompt the input parameter values using graphical dialog") },
21 { wxCMD_LINE_SWITCH, _T("t"), _T("text-dialog"), _T("prompt the input parameter values in text mode") },
22 { wxCMD_LINE_SWITCH, _T("c"), _T("console"), _T("open bbi console") },
23 { wxCMD_LINE_SWITCH, _T("N"), _T("no-console"), _T("never open bbi console even on error") },
24 { wxCMD_LINE_SWITCH, _T("q"), _T("quiet"), _T("be quiet (='message max 0')") },
25 { wxCMD_LINE_SWITCH, _T("d"), _T("debug"), _T("turn all messages on (='message all 9')") },
26 { wxCMD_LINE_SWITCH, _T("D"), _T("Debug"), _T("memory debug on exit (='debug -D')") },
27 { wxCMD_LINE_PARAM, NULL, NULL, _T("file [file [...]]"), wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE },
30 //==========================================================================
32 //==========================================================================
33 // Processes the command line parsing result
34 struct WxProcessCmdLine
37 void Process(wxCmdLineParser& parser);
40 std::vector<std::string> argv;
45 bool graphical_dialog;
49 std::map<std::string,std::string> param_map;
50 std::vector<std::string> input_file;
53 //==========================================================================
55 //==========================================================================
56 void WxProcessCmdLine::Process(wxCmdLineParser& parser)
59 if (parser.Found(_T("D")))
61 bbtk::StaticInitTime::PrintObjectListInfo = true;
64 debug = ( parser.Found(_T("d")) );
65 quiet = ( parser.Found(_T("q")) );
66 help = ( parser.Found(_T("h")) );
67 graphical_dialog = ( parser.Found(_T("g")) );
68 text_dialog = ( parser.Found(_T("t")) );
69 no_console = ( parser.Found(_T("N")) );
71 if (quiet) bbtk::MessageManager::SetMessageLevel("max",0);
72 if (debug) bbtk::MessageManager::SetMessageLevel("all",9);
74 // parse the arguments and consider those which contain a "="
75 // as set input commands, other as files
76 int pargc = parser.GetParamCount();
77 for (int i=0; i<pargc; ++i)
79 std::string s = bbtk::wx2std(parser.GetParam(i));
80 std::string::size_type pos = s.find_first_of("=");
81 if (std::string::npos != pos)
83 std::string left = s.substr(0,pos);
84 std::string right = s.substr(pos+1,s.size());
85 param_map[left]=right;
89 input_file.push_back(s);
93 bool usage = (help && (input_file.size()==0));
95 std::cout << "BBI (The Black Box Interpreter) - bbtk "
96 << bbtk::GetVersion() << " - (c) Creatis 2007-2008"
102 console = ( parser.Found(_T("c")) ||
103 ((input_file.size() == 0) &&
108 //==========================================================================
111 //==========================================================================
112 class wxBBIApp : public wxApp
117 // std::cout << "wxBBIApp::OnExit()"<<std::endl;
118 // bbtk::Object::PrintObjectListInfo();
120 void OnInitCmdLine(wxCmdLineParser& parser);
121 bool OnCmdLineParsed(wxCmdLineParser& parser);
123 WxProcessCmdLine cmd;
125 //==========================================================================
128 //==========================================================================
129 void wxBBIApp::OnInitCmdLine(wxCmdLineParser& parser)
131 parser.SetDesc(cmdLineDesc);
133 //==========================================================================
135 //==========================================================================
136 bool wxBBIApp::OnCmdLineParsed(wxCmdLineParser& parser)
139 // if (!cmd.proceed) return false;
142 //==========================================================================
149 //==========================================================================
150 // The `main program' equivalent, creating the windows and returning the
152 bool wxBBIApp::OnInit( )
154 // std::cout << "OnInit"<<std::endl;
157 //See http://www.wxwindows.org/faqgtk.htm#locale
158 setlocale(LC_NUMERIC, "C");
162 if (cmd.quiet) bbtk::MessageManager::SetMessageLevel("max",0);
163 if (cmd.debug) bbtk::MessageManager::SetMessageLevel("all",9);
166 bbtk::WxGUIConsole *I = new bbtk::WxGUIConsole(0,_T("bbi"),wxSize(800,600));
168 if (cmd.console) I->Show(true);
171 I->SetInputs(cmd.param_map);
173 bool help_on_script = cmd.help && (cmd.input_file.size() > 0);
174 if (help_on_script) I->SetNoExecMode(true);
175 if (cmd.graphical_dialog) I->SetDialogMode(bbtk::VirtualExec::GraphicalDialog);
176 if (cmd.text_dialog) I->SetDialogMode(bbtk::VirtualExec::TextDialog);
178 std::vector<std::string>::const_iterator i;
181 for (i=cmd.input_file.begin(); i!=cmd.input_file.end(); ++i)
183 error = ! I->InterpretFile(*i);
186 bool show_on_error = error && ! cmd.no_console;
187 if (show_on_error) I->Show();
189 I->SetNoExecMode(false);
194 I->GetInterpreter()->GetExecuter()->GetFactory()->PrintHelpDescriptor("workspace",package,false);
198 std::cout << "soe="<<show_on_error <<std::endl;
199 std::cout << "con="<<console<<std::endl;
200 std::cout << "iws="<<bbtk::Wx::IsSomeWindowShown()<<std::endl;
202 if (!(show_on_error || cmd.console || bbtk::Wx::IsSomeWindowAlive() ))
205 // std::cout << "I->Close"<<std::endl;
209 // std::cout << "!I->Close"<<std::endl;
212 // std::cout << "EO OnInit"<<std::endl;
217 //==========================================================================
221 //==========================================================================
223 //==========================================================================
224 IMPLEMENT_APP(wxBBIApp);
226 // How to have a Console and wxWidgets
227 // http://www.wxwidgets.org/wiki/index.php/MSVC_Setup_Guide
228 // In Visual C++ 6 (7 should be similar), to create an application that is both a console application
229 // (cout's to the console are visible) and has a wxWidgets GUI,
230 // you need to use the linker option "/subsystem:console" and the following code:
231 int main(int argc, char* argv[])
233 return WinMain(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), SW_SHOWNORMAL);
237 //==========================================================================
238 // OTHER ( Linux... )
239 //==========================================================================
241 IMPLEMENT_APP_NO_MAIN(wxBBIApp);
244 int main(int argc, char* argv[])
246 wxMessageOutput::Set( new wxMessageOutputBest );
248 wxCmdLineParser parser(cmdLineDesc,argc,argv);
249 int val = parser.Parse(false);
255 WxProcessCmdLine cmdline;
256 cmdline.Process(parser);
258 if (!cmdline.proceed) return 0;
260 if (cmdline.no_console)
262 // std::cout << "main NC"<<std::endl;
264 bbtk::Interpreter::Pointer I = bbtk::Interpreter::New();
265 I->SetInputs(cmdline.param_map);
266 bool help_on_script = cmdline.help && (cmdline.input_file.size() > 0);
267 if (help_on_script) I->SetNoExecMode(true);
268 if (cmdline.text_dialog) I->SetDialogMode(bbtk::VirtualExec::TextDialog);
269 std::vector<std::string>::const_iterator i;
271 for (i=cmdline.input_file.begin();
272 i!=cmdline.input_file.end(); ++i)
274 error = ! I->InterpretFile(*i);
279 I->SetNoExecMode(false);
281 I->GetExecuter()->GetFactory()->PrintHelpDescriptor("workspace",
285 if (cmdline.input_file.size()==0)
286 I->CommandLineInterpreter();
298 #endif // defined(_WIN32)
299 //==========================================================================
309 //==========================================================================
311 //==========================================================================
313 #include "bbtkInterpreter.h"
315 // Processes the command line parsing result
316 struct ProcessCmdLine
319 void Process(int argc_, char *argv_[]);
320 bool Found(std::string value);
323 std::vector<std::string> argv;
328 bool graphical_dialog;
332 std::map<std::string,std::string> param_map;
333 std::vector<std::string> input_file;
337 bool ProcessCmdLine::Found(std::string value)
340 for (int i=1; i<argc; ++i)
350 void ProcessCmdLine::Process(int argc_, char *argv_[])
353 for (int i=0; i<argc; ++i)
355 argv.push_back(argv_[i]);
358 //EED proceed = true;
361 bbtk::StaticInitTime::PrintObjectListInfo = true;
364 debug = ( Found("d") );
365 quiet = ( Found("q") );
366 help = ( Found("h") );
367 graphical_dialog = ( Found("g") );
368 text_dialog = ( Found("t") );
369 no_console = ( Found("N") );
371 if (quiet) bbtk::MessageManager::SetMessageLevel("max",0);
372 if (debug) bbtk::MessageManager::SetMessageLevel("all",9);
374 // parse the arguments and consider those which contain a "="
375 // as set input commands, other as files
376 //EED int pargc = parser.GetParamCount();
377 for (int i=1; i<argc; ++i)
379 std::string s = argv[i];
380 std::string::size_type pos = s.find_first_of("=");
381 if (std::string::npos != pos)
383 std::string left = s.substr(0,pos);
384 std::string right = s.substr(pos+1,s.size());
385 param_map[left]=right;
389 input_file.push_back(s);
393 bool usage = (help && (input_file.size()==0));
395 std::cout << "BBI (The Black Box Interpreter) - bbtk "
396 << bbtk::GetVersion() << " - (c) Creatis 2007-2008"
398 //EED parser.Usage();
402 console = ( Found("c") ||
403 ((input_file.size() == 0) &&
408 //==========================================================================
412 int main(int argc, char* argv[])
417 // std::cout << "usage : "<<argv[0]<<" [filename]"<<std::endl;
421 std::cout << "BBI (Black Box Interpreter) - bbtk "
422 << bbtk::GetVersion()<< " - (c) Creatis 2007"
425 bbtk::Interpreter::Pointer I = bbtk::Interpreter::New();
428 I->CommandLineInterpreter();
432 cmd.Process(argc,argv);
433 I->SetInputs(cmd.param_map);
435 std::string f(argv[1]);
439 // bbtk::Wx::LoopUntilAllWindowsClose();
446 #endif //#ifdef _USE_WXWIDGETS_