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