]> Creatis software - bbtk.git/blob - kernel/appli/bbc/main.cxx.in
Feature #1774
[bbtk.git] / kernel / appli / bbc / main.cxx.in
1 /*
2  # ---------------------------------------------------------------------
3  #
4  # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5  #                        pour la SantÈ)
6  # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7  # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8  # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9  #
10  #  This software is governed by the CeCILL-B license under French law and
11  #  abiding by the rules of distribution of free software. You can  use,
12  #  modify and/ or redistribute the software under the terms of the CeCILL-B
13  #  license as circulated by CEA, CNRS and INRIA at the following URL
14  #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15  #  or in the file LICENSE.txt.
16  #
17  #  As a counterpart to the access to the source code and  rights to copy,
18  #  modify and redistribute granted by the license, users are provided only
19  #  with a limited warranty  and the software's author,  the holder of the
20  #  economic rights,  and the successive licensors  have only  limited
21  #  liability.
22  #
23  #  The fact that you are presently reading this means that you have had
24  #  knowledge of the CeCILL-B license and that you accept its terms.
25  # ------------------------------------------------------------------------ */
26
27 #ifdef _USE_WXWIDGETS_
28
29 //==========================================================================
30 // WITH WX
31 //==========================================================================
32 #include "bbtkInterpreter.h"
33 #include "bbtkWxBlackBox.h"
34 #include "bbtkWxGUIConsole.h"
35
36 #include <wx/cmdline.h>
37 #include <vector>
38 #include <map>
39
40 #include "EXEC_FUNCTION.h"
41
42 static const wxCmdLineEntryDesc cmdLineDesc[] =
43 {
44   { wxCMD_LINE_SWITCH, _T("d"), _T("debug"), _T("debug messages on (message all 9)") },
45   { wxCMD_LINE_SWITCH, _T("q"), _T("quiet"),   _T("be quiet (message mac 0)") },
46   { wxCMD_LINE_SWITCH, _T("h"), _T("help"),   _T("print help") },
47   { wxCMD_LINE_SWITCH, _T("g"), _T("graphical-dialog"),   _T("prompts the user for the parameters values using dialog boxes") },
48   { wxCMD_LINE_SWITCH, _T("t"), _T("text-dialog"),   _T("prompts the user for the parameters values in text mode") },
49   { wxCMD_LINE_PARAM,  NULL, NULL, _T("Input=Value"), wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE },
50   { wxCMD_LINE_NONE }
51 };
52
53
54
55 class wxBBIApp : public wxApp
56 {
57 public:
58   bool OnInit( );
59   int  OnExit() { return true; }
60   void OnInitCmdLine(wxCmdLineParser& parser);
61   bool OnCmdLineParsed(wxCmdLineParser& parser);
62   void Run(bbtk::Interpreter*);
63
64   bbtk::Executer::Pointer mExecuter;
65   //  int argc;
66   //  std::vector<std::string> argv;
67   bool command;
68   bool debug;
69   bool quiet;
70   bool help;
71   bool graphical_dialog;
72   bool text_dialog;
73
74   std::map<std::string,std::string> param_map;
75
76 };
77
78 IMPLEMENT_APP(wxBBIApp);
79
80 void wxBBIApp::OnInitCmdLine(wxCmdLineParser& parser)
81 {
82   parser.SetDesc(cmdLineDesc);
83 }
84
85 bool wxBBIApp::OnCmdLineParsed(wxCmdLineParser& parser)
86 {
87   debug = ( parser.Found(_T("d")) );
88   quiet = ( parser.Found(_T("q")) );
89   help = ( parser.Found(_T("h")) );
90   graphical_dialog = ( parser.Found(_T("g")) );
91   text_dialog = ( parser.Found(_T("t")) );
92
93   // parse the arguments and consider those which contain a "=" 
94   // as set input commands
95   int argc = parser.GetParamCount();
96   for (int i=0; i<argc; ++i) 
97     {
98       std::string s = bbtk::wx2std(parser.GetParam(i));
99       std::string::size_type pos = s.find_first_of("=");
100       if (std::string::npos != pos) 
101         {
102           std::string left = s.substr(0,pos);
103           std::string right = s.substr(pos+1,s.size());
104           param_map[left]=right;
105         }
106       else
107         {
108           std::cout << "'" << s << "' option unrecognized : ignored"<<std::endl;
109         }
110     }
111
112   if (help) {
113     std::cout << bbtk::wx2std(parser.GetParam(0)) << std::endl;
114     parser.Usage();
115   }
116
117   return true;
118 }
119
120
121
122 // ----------------------------------------------------------------------------
123 // The `main program' equivalent, creating the windows and returning the
124 // main frame
125 bool wxBBIApp::OnInit( )
126 {
127   //    std::cout << "OnInit"<<std::endl;
128   wxApp::OnInit();
129 #ifdef __WXGTK__
130   //See http://www.wxwindows.org/faqgtk.htm#locale
131   setlocale(LC_NUMERIC, "C");
132 #endif
133   
134   if (quiet) bbtk::MessageManager::SetMessageLevel("max",0);
135   if (debug) bbtk::MessageManager::SetMessageLevel("all",9);
136  
137   // Creates the parent window of all bbtk windows 
138   bbtk::Wx::CreateTopWindow();
139
140   try {
141     mExecuter = bbtk::Executer::New();
142     mExecuter->SetInputs(param_map);
143
144     if (help) mExecuter->SetNoExecMode(true);
145
146     if (graphical_dialog) mExecuter->SetDialogMode(bbtk::VirtualExec::GraphicalDialog);
147     if (text_dialog) mExecuter->SetDialogMode(bbtk::VirtualExec::TextDialog);
148
149     EXEC_FUNCTION(mExecuter);
150
151     mExecuter->SetNoExecMode(false);
152
153     if (help)
154       {
155         std::string package;
156         mExecuter->GetFactory()->HelpBlackBox("workspace",package,false);
157       }
158   }
159   catch (bbtk::Exception e)
160     {
161       wxString mess;
162       mess += bbtk::std2wx ( e.GetMessage() );
163       wxMessageBox(mess,_T("Error"),wxOK | wxICON_ERROR);
164       bbtk::Wx::GetTopWindow()->Close();
165       return false;
166     }
167   if (help || !bbtk::Wx::IsSomeWindowAlive())
168     {
169       return false;
170     }
171   return true;
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(),
185                  SW_SHOWNORMAL);
186 }
187
188 #endif // defined(_WIN32)
189
190
191 #else
192 //==========================================================================
193 // WITHOUT WX
194 //==========================================================================
195
196 #include "bbtkInterpreter.h"
197
198 int main(int argc, char* argv[])
199 {  
200
201   if (argc>2) return 0;
202
203   std::cout << "BBI (Black Box Interpreter) - bbtk "
204             << bbtk::GetVersion()<< " - (c) Creatis 2007"
205             <<std::endl;
206
207   bbtk::Interpreter I;
208   if (argc==1) 
209     {
210       I.CommandLineInterpreter();
211     }
212   else
213     {
214       std::string f(argv[1]);
215       I.InterpretFile(f);
216     }
217     
218   return 0;
219
220 }
221
222 // EOF
223 #endif //#ifdef _USE_WXWIDGETS_
224
225
226