]> Creatis software - bbtk.git/blob - kernel/appli/bbc/main.cxx.in
Some cleaning
[bbtk.git] / kernel / appli / bbc / main.cxx.in
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 #include "EXEC_FUNCTION.h"
15
16 static const wxCmdLineEntryDesc cmdLineDesc[] =
17 {
18   { wxCMD_LINE_SWITCH, _T("d"), _T("debug"), _T("debug messages on (message all 9)") },
19   { wxCMD_LINE_SWITCH, _T("q"), _T("quiet"),   _T("be quiet (message mac 0)") },
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_PARAM,  NULL, NULL, _T("Input=Value"), 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() { return true; }
34   void OnInitCmdLine(wxCmdLineParser& parser);
35   bool OnCmdLineParsed(wxCmdLineParser& parser);
36   void Run(bbtk::Interpreter*);
37
38   bbtk::Executer::Pointer mExecuter;
39   //  int argc;
40   //  std::vector<std::string> argv;
41   bool command;
42   bool debug;
43   bool quiet;
44   bool help;
45   bool graphical_dialog;
46   bool text_dialog;
47
48   std::map<std::string,std::string> param_map;
49
50 };
51
52 IMPLEMENT_APP(wxBBIApp);
53
54 void wxBBIApp::OnInitCmdLine(wxCmdLineParser& parser)
55 {
56   parser.SetDesc(cmdLineDesc);
57 }
58
59 bool wxBBIApp::OnCmdLineParsed(wxCmdLineParser& parser)
60 {
61   debug = ( parser.Found(_T("d")) );
62   quiet = ( parser.Found(_T("q")) );
63   help = ( parser.Found(_T("h")) );
64   graphical_dialog = ( parser.Found(_T("g")) );
65   text_dialog = ( parser.Found(_T("t")) );
66
67   // parse the arguments and consider those which contain a "=" 
68   // as set input commands
69   int argc = parser.GetParamCount();
70   for (int i=0; i<argc; ++i) 
71     {
72       std::string s = bbtk::wx2std(parser.GetParam(i));
73       std::string::size_type pos = s.find_first_of("=");
74       if (std::string::npos != pos) 
75         {
76           std::string left = s.substr(0,pos);
77           std::string right = s.substr(pos+1,s.size());
78           param_map[left]=right;
79         }
80       else
81         {
82           std::cout << "'" << s << "' option unrecognized : ignored"<<std::endl;
83         }
84     }
85
86   if (help) {
87     std::cout << bbtk::wx2std(parser.GetParam(0)) << std::endl;
88     parser.Usage();
89   }
90
91   return true;
92 }
93
94
95
96 // ----------------------------------------------------------------------------
97 // The `main program' equivalent, creating the windows and returning the
98 // main frame
99 bool wxBBIApp::OnInit( )
100 {
101   //    std::cout << "OnInit"<<std::endl;
102   wxApp::OnInit();
103 #ifdef __WXGTK__
104   //See http://www.wxwindows.org/faqgtk.htm#locale
105   setlocale(LC_NUMERIC, "C");
106 #endif
107   
108   if (quiet) bbtk::MessageManager::SetMessageLevel("max",0);
109   if (debug) bbtk::MessageManager::SetMessageLevel("all",9);
110  
111   // Creates the parent window of all bbtk windows 
112   bbtk::Wx::CreateTopWindow();
113
114   try {
115     mExecuter = bbtk::Executer::New();
116     mExecuter->SetInputs(param_map);
117
118     if (help) mExecuter->SetNoExecMode(true);
119
120     if (graphical_dialog) mExecuter->SetDialogMode(bbtk::VirtualExec::GraphicalDialog);
121     if (text_dialog) mExecuter->SetDialogMode(bbtk::VirtualExec::TextDialog);
122
123     EXEC_FUNCTION(mExecuter);
124
125     mExecuter->SetNoExecMode(false);
126
127     if (help)
128       {
129         std::string package;
130         mExecuter->GetFactory()->HelpBlackBox("workspace",package,false);
131       }
132   }
133   catch (bbtk::Exception e)
134     {
135       wxString mess;
136       mess += bbtk::std2wx ( e.GetMessage() );
137       wxMessageBox(mess,_T("Error"),wxOK | wxICON_ERROR);
138       bbtk::Wx::GetTopWindow()->Close();
139       return false;
140     }
141   if (help || !bbtk::Wx::IsSomeWindowAlive())
142     {
143       return false;
144     }
145   return true;
146 }
147
148
149 #if defined(_WIN32)
150
151 //  How to have a Console and wxWidgets
152 //  http://www.wxwidgets.org/wiki/index.php/MSVC_Setup_Guide
153 //   In Visual C++ 6 (7 should be similar), to create an application that is both a console application 
154 //  (cout's to the console are visible) and has a wxWidgets GUI, 
155 //  you need to use the linker option "/subsystem:console" and the following code:
156 int main(int argc, char* argv[])
157 {
158   return WinMain(::GetModuleHandle(NULL), NULL, ::GetCommandLine(),
159                  SW_SHOWNORMAL);
160 }
161
162 #endif // defined(_WIN32)
163
164
165 #else
166 //==========================================================================
167 // WITHOUT WX
168 //==========================================================================
169
170 #include "bbtkInterpreter.h"
171
172 int main(int argc, char* argv[])
173 {  
174
175   if (argc>2) return 0;
176
177   std::cout << "BBI (Black Box Interpreter) - bbtk "
178             << bbtk::GetVersion()<< " - (c) Creatis 2007"
179             <<std::endl;
180
181   bbtk::Interpreter I;
182   if (argc==1) 
183     {
184       I.CommandLineInterpreter();
185     }
186   else
187     {
188       std::string f(argv[1]);
189       I.InterpretFile(f);
190     }
191     
192   return 0;
193
194 }
195
196 // EOF
197 #endif //#ifdef _USE_WXWIDGETS_
198
199
200