]> 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("M"), _T("Memory debug"), _T("Display memory debug message on exit ('object -S')") },
19   { wxCMD_LINE_SWITCH, _T("d"), _T("debug"), _T("all messages on ('message all 9')") },
20   { wxCMD_LINE_SWITCH, _T("c"), _T("console"), _T("open a console") },
21   { wxCMD_LINE_SWITCH, _T("q"), _T("quiet"),   _T("be quiet ('message max 0')") },
22   { wxCMD_LINE_SWITCH, _T("h"), _T("help"),   _T("print help on bbi or on workspace defined if any") },
23   { wxCMD_LINE_SWITCH, _T("g"), _T("graphical-dialog"),   _T("prompts the user for the parameter values using graphical dialog") },
24   { wxCMD_LINE_SWITCH, _T("t"), _T("text-dialog"),   _T("prompts the user for the parameter values in text mode") },
25   { wxCMD_LINE_SWITCH, _T("N"), _T("no-console"),   _T("do not show console (even on error)") },
26   { wxCMD_LINE_PARAM,  NULL, NULL, _T("input_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   if (parser.Found(_T("M"))) 
71     {
72       bbtk::StaticInitTime::PrintObjectListInfo = true;
73     }
74   
75   debug = ( parser.Found(_T("d")) );
76   quiet = ( parser.Found(_T("q")) );
77   help = ( parser.Found(_T("h")) );
78   graphical_dialog = ( parser.Found(_T("g")) );
79   text_dialog = ( parser.Found(_T("t")) );
80   no_console = ( parser.Found(_T("N")) );
81
82   // parse the arguments and consider those which contain a "=" 
83   // as set input commands, other as files
84   int argc = parser.GetParamCount();
85   for (int i=0; i<argc; ++i) 
86     {
87       std::string s = bbtk::wx2std(parser.GetParam(i));
88       std::string::size_type pos = s.find_first_of("=");
89       if (std::string::npos != pos) 
90         {
91           std::string left = s.substr(0,pos);
92           std::string right = s.substr(pos+1,s.size());
93           param_map[left]=right;
94           //      std::cout << "'"<<left << "' = '"<<right<<"'"<<std::endl;
95         }
96       else 
97         {
98           //      std::cout << "input file = ["<<s<<"]"<<std::endl;
99           input_file.push_back(s);
100         }
101     }
102
103   bool usage = (help && (input_file.size()==0));
104   if (usage) {
105     std::cout << "BBI (The Black Box Interpreter) - bbtk "
106               << bbtk::GetVersion() << " - (c) Creatis 2007-2008"
107               << std::endl;
108     parser.Usage();
109   }
110
111   console = ( parser.Found(_T("c")) || 
112               ((input_file.size() == 0) && 
113                (!no_console) &&
114                (!usage) ) );
115
116
117
118   return true;
119 }
120
121
122
123
124
125
126 // ----------------------------------------------------------------------------
127 // The `main program' equivalent, creating the windows and returning the
128 // main frame
129 bool wxBBIApp::OnInit( )
130 {
131   //    std::cout << "OnInit"<<std::endl;
132   wxApp::OnInit();
133 #ifdef __WXGTK__
134   //See http://www.wxwindows.org/faqgtk.htm#locale
135   setlocale(LC_NUMERIC, "C");
136 #endif
137   
138
139   if (quiet) bbtk::MessageManager::SetMessageLevel("max",0);
140   if (debug) bbtk::MessageManager::SetMessageLevel("all",9);
141   
142
143   bbtk::WxGUIConsole *I = new bbtk::WxGUIConsole(0,_T("bbi"),wxSize(800,600));
144   SetTopWindow(I);  
145   if (console) I->Show(true);
146
147
148   I->SetInputs(param_map);
149
150   bool help_on_script = help && (input_file.size() > 0);
151   if (help_on_script) I->SetNoExecMode(true);
152
153   if (graphical_dialog) I->SetDialogMode(bbtk::VirtualExec::GraphicalDialog);
154   if (text_dialog) I->SetDialogMode(bbtk::VirtualExec::TextDialog);
155
156   std::vector<std::string>::const_iterator i;
157   bool error = false;
158
159   for (i=input_file.begin(); i!=input_file.end(); ++i) 
160     {
161       error = ! I->InterpretFile(*i);
162       if (error) break;
163     }
164   bool show_on_error = error && ! no_console;
165   if (show_on_error) I->Show();
166
167   I->SetNoExecMode(false);
168
169   if (help_on_script) 
170     {
171       std::string package; 
172       I->GetInterpreter()->GetExecuter()->GetFactory()->HelpBlackBox("workspace",package,false);
173     }
174
175   if (!(show_on_error || console || bbtk::Wx::IsSomeWindowShown() ))
176     {
177       I->Close();
178     }
179   return true;
180
181 }
182
183
184 #if defined(_WIN32) 
185
186 //  How to have a Console and wxWidgets
187 //  http://www.wxwidgets.org/wiki/index.php/MSVC_Setup_Guide
188 //   In Visual C++ 6 (7 should be similar), to create an application that is both a console application 
189 //  (cout's to the console are visible) and has a wxWidgets GUI, 
190 //  you need to use the linker option "/subsystem:console" and the following code:
191 int main(int argc, char* argv[])
192 {
193         return WinMain(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), SW_SHOWNORMAL);
194 }
195
196 #endif // defined(_WIN32) 
197
198
199 #else
200 //==========================================================================
201 // WITHOUT WX
202 //==========================================================================
203
204 #include "bbtkInterpreter.h"
205
206 int main(int argc, char* argv[])
207 {  
208
209   if (argc>2) return 0;
210
211   std::cout << "BBI (Black Box Interpreter) - bbtk "
212             << bbtk::GetVersion()<< " - (c) Creatis 2007"
213             <<std::endl;
214
215   bbtk::Interpreter I;
216   if (argc==1) 
217     {
218       I.CommandLineInterpreter();
219     }
220   else 
221     {
222       std::string f(argv[1]);
223       I.InterpretFile(f);
224     }
225     
226   return 0;
227
228 }
229
230 // EOF
231 #endif //#ifdef _USE_WXWIDGETS_
232
233
234