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