]> 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
71   if (parser.Found(_T("M"))) 
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   if (!(show_on_error || console || bbtk::Wx::IsSomeWindowShown() ))
178     {
179       I->Close();
180     }
181   return true;
182
183 }
184
185
186 #if defined(_WIN32) 
187
188 //  How to have a Console and wxWidgets
189 //  http://www.wxwidgets.org/wiki/index.php/MSVC_Setup_Guide
190 //   In Visual C++ 6 (7 should be similar), to create an application that is both a console application 
191 //  (cout's to the console are visible) and has a wxWidgets GUI, 
192 //  you need to use the linker option "/subsystem:console" and the following code:
193 int main(int argc, char* argv[])
194 {
195         return WinMain(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), SW_SHOWNORMAL);
196 }
197
198 #endif // defined(_WIN32) 
199
200
201 #else
202 //==========================================================================
203 // WITHOUT WX
204 //==========================================================================
205
206 #include "bbtkInterpreter.h"
207
208 int main(int argc, char* argv[])
209 {  
210
211   if (argc>2) return 0;
212
213   std::cout << "BBI (Black Box Interpreter) - bbtk "
214             << bbtk::GetVersion()<< " - (c) Creatis 2007"
215             <<std::endl;
216
217   bbtk::Interpreter I;
218   if (argc==1) 
219     {
220       I.CommandLineInterpreter();
221     }
222   else 
223     {
224       std::string f(argv[1]);
225       I.InterpretFile(f);
226     }
227     
228   return 0;
229
230 }
231
232 // EOF
233 #endif //#ifdef _USE_WXWIDGETS_
234
235
236