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