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