]> Creatis software - bbtk.git/blob - kernel/appli/bbi/bbi.cxx
*** empty log message ***
[bbtk.git] / kernel / appli / bbi / bbi.cxx
1 #ifdef _USE_WXWIDGETS_
2
3 //==========================================================================
4 // WITH WX
5 //==========================================================================
6 #include "bbtkObject.h"
7 #include "bbtkInterpreter.h"
8 #include "bbtkWxBlackBox.h"
9 #include "bbtkWxGUIConsole.h"
10
11 #include <wx/cmdline.h>
12 #include <vector>
13 #include <map>
14
15
16 static const wxCmdLineEntryDesc cmdLineDesc[] =
17 {
18   { wxCMD_LINE_SWITCH, _T("h"), _T("help"),   _T("print this help or help on the application defined in input bbs file if any") },
19   { wxCMD_LINE_SWITCH, _T("g"), _T("graphical-dialog"),   _T("prompt the input parameter values using graphical dialog") },
20   { wxCMD_LINE_SWITCH, _T("t"), _T("text-dialog"),   _T("prompt the input parameter values in text mode") },
21   { wxCMD_LINE_SWITCH, _T("c"), _T("console"), _T("open bbi console") },
22   { wxCMD_LINE_SWITCH, _T("N"), _T("no-console"),   _T("never open bbi console even on error") },
23   { wxCMD_LINE_SWITCH, _T("q"), _T("quiet"),   _T("be quiet (='message max 0')") },
24   { wxCMD_LINE_SWITCH, _T("d"), _T("debug"), _T("turn all messages on (='message all 9')") },
25   { wxCMD_LINE_SWITCH, _T("D"), _T("Debug"), _T("memory debug on exit (='debug -D')") },
26   { wxCMD_LINE_PARAM,  NULL, NULL, _T("file [file [...]]"), wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE },
27   { wxCMD_LINE_NONE }
28 };
29
30
31
32 class wxBBIApp : public wxApp
33 {
34 public:
35   bool OnInit( );
36   int  OnExit() { 
37     //    std::cout << "wxBBIApp::OnExit()"<<std::endl;
38     // bbtk::Object::PrintObjectListInfo();
39     return true; }
40   void OnInitCmdLine(wxCmdLineParser& parser);
41   bool OnCmdLineParsed(wxCmdLineParser& parser);
42   void Run(bbtk::Interpreter*);
43
44   bbtk::Interpreter* I;
45   int argc;
46   std::vector<std::string> argv;
47   bool console;
48   bool debug;
49   bool quiet;
50   bool help;
51   bool graphical_dialog;
52   bool text_dialog;
53   bool no_console;
54
55   std::map<std::string,std::string> param_map;
56   std::vector<std::string> input_file;
57   
58 };
59
60 IMPLEMENT_APP(wxBBIApp);
61
62 void wxBBIApp::OnInitCmdLine(wxCmdLineParser& parser)
63 {
64   //    std::cout << "OnInitCmdLine"<<std::endl;
65   parser.SetDesc(cmdLineDesc);
66 }
67
68 bool wxBBIApp::OnCmdLineParsed(wxCmdLineParser& parser)
69 {
70
71   if (parser.Found(_T("D"))) 
72     {
73       bbtk::StaticInitTime::PrintObjectListInfo = true;
74     }
75   
76   debug = ( parser.Found(_T("d")) );
77
78   quiet = ( parser.Found(_T("q")) );
79   help = ( parser.Found(_T("h")) );
80   graphical_dialog = ( parser.Found(_T("g")) );
81   text_dialog = ( parser.Found(_T("t")) );
82   no_console = ( parser.Found(_T("N")) );
83
84   // parse the arguments and consider those which contain a "=" 
85   // as set input commands, other as files
86   int argc = parser.GetParamCount();
87   for (int i=0; i<argc; ++i) 
88     {
89       std::string s = bbtk::wx2std(parser.GetParam(i));
90       std::string::size_type pos = s.find_first_of("=");
91       if (std::string::npos != pos) 
92         {
93           std::string left = s.substr(0,pos);
94           std::string right = s.substr(pos+1,s.size());
95           param_map[left]=right;
96           //      std::cout << "'"<<left << "' = '"<<right<<"'"<<std::endl;
97         }
98       else 
99         {
100           //      std::cout << "input file = ["<<s<<"]"<<std::endl;
101           input_file.push_back(s);
102         }
103     }
104
105   bool usage = (help && (input_file.size()==0));
106   if (usage) {
107     std::cout << "BBI (The Black Box Interpreter) - bbtk "
108               << bbtk::GetVersion() << " - (c) Creatis 2007-2008"
109               << std::endl;
110     parser.Usage();
111   }
112
113   console = ( parser.Found(_T("c")) || 
114               ((input_file.size() == 0) && 
115                (!no_console) &&
116                (!usage) ) );
117
118
119
120   return true;
121 }
122
123
124
125
126
127
128 // ----------------------------------------------------------------------------
129 // The `main program' equivalent, creating the windows and returning the
130 // main frame
131 bool wxBBIApp::OnInit( )
132 {
133   //    std::cout << "OnInit"<<std::endl;
134   wxApp::OnInit();
135 #ifdef __WXGTK__
136   //See http://www.wxwindows.org/faqgtk.htm#locale
137   setlocale(LC_NUMERIC, "C");
138 #endif
139   
140
141   if (quiet) bbtk::MessageManager::SetMessageLevel("max",0);
142   if (debug) bbtk::MessageManager::SetMessageLevel("all",9);
143   
144
145   bbtk::WxGUIConsole *I = new bbtk::WxGUIConsole(0,_T("bbi"),wxSize(800,600));
146   SetTopWindow(I);  
147   if (console) I->Show(true);
148
149
150   I->SetInputs(param_map);
151
152   bool help_on_script = help && (input_file.size() > 0);
153   if (help_on_script) I->SetNoExecMode(true);
154
155   if (graphical_dialog) I->SetDialogMode(bbtk::VirtualExec::GraphicalDialog);
156   if (text_dialog) I->SetDialogMode(bbtk::VirtualExec::TextDialog);
157
158   std::vector<std::string>::const_iterator i;
159   bool error = false;
160
161   for (i=input_file.begin(); i!=input_file.end(); ++i) 
162     {
163       error = ! I->InterpretFile(*i);
164       if (error) break;
165     }
166   bool show_on_error = error && ! no_console;
167   if (show_on_error) I->Show();
168
169   I->SetNoExecMode(false);
170
171   if (help_on_script) 
172     {
173       std::string package; 
174       I->GetInterpreter()->GetExecuter()->GetFactory()->HelpBlackBox("workspace",package,false);
175     }
176
177   /*
178   std::cout << "soe="<<show_on_error <<std::endl;
179   std::cout << "con="<<console<<std::endl;
180   std::cout << "iws="<<bbtk::Wx::IsSomeWindowShown()<<std::endl;
181   */
182   if (!(show_on_error || console || bbtk::Wx::IsSomeWindowShown() ))
183     {
184       I->Close();
185       //      std::cout << "I->Close"<<std::endl;
186     }
187   else 
188     {
189       //      std::cout << "!I->Close"<<std::endl;
190     }
191   return true;
192
193 }
194
195
196 #if defined(_WIN32) 
197
198 //  How to have a Console and wxWidgets
199 //  http://www.wxwidgets.org/wiki/index.php/MSVC_Setup_Guide
200 //   In Visual C++ 6 (7 should be similar), to create an application that is both a console application 
201 //  (cout's to the console are visible) and has a wxWidgets GUI, 
202 //  you need to use the linker option "/subsystem:console" and the following code:
203 int main(int argc, char* argv[])
204 {
205         return WinMain(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), SW_SHOWNORMAL);
206 }
207
208 #endif // defined(_WIN32) 
209
210
211 #else
212 //==========================================================================
213 // WITHOUT WX
214 //==========================================================================
215
216 #include "bbtkInterpreter.h"
217
218 int main(int argc, char* argv[])
219 {  
220
221   if (argc>2) return 0;
222
223   std::cout << "BBI (Black Box Interpreter) - bbtk "
224             << bbtk::GetVersion()<< " - (c) Creatis 2007"
225             <<std::endl;
226
227   bbtk::Interpreter I;
228   if (argc==1) 
229     {
230       I.CommandLineInterpreter();
231     }
232   else 
233     {
234       std::string f(argv[1]);
235       I.InterpretFile(f);
236     }
237     
238   return 0;
239
240 }
241
242 // EOF
243 #endif //#ifdef _USE_WXWIDGETS_
244
245
246