]> Creatis software - bbtk.git/blob - kernel/src/bbtkWxGUIHtmlBrowser.cxx
Feature #1774
[bbtk.git] / kernel / src / bbtkWxGUIHtmlBrowser.cxx
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
28 /*=========================================================================
29   Program:   bbtk
30   Module:    $RCSfile: bbtkWxGUIHtmlBrowser.cxx,v $
31   Language:  C++
32   Date:      $Date: 2012/11/16 08:49:01 $
33   Version:   $Revision: 1.15 $
34 =========================================================================*/
35
36
37
38
39 /**
40  * \brief Short description in one line
41  * 
42  * Long description which 
43  * can span multiple lines
44  */
45 /**
46  * \file 
47  * \brief 
48  */
49 /**
50  * \class bbtk::
51  * \brief 
52  */
53
54 #ifdef _USE_WXWIDGETS_
55
56
57 #include "bbtkWxGUIHtmlBrowser.h"
58 //#include "bbtkWxBlackBox.h"
59 //#include "bbtkWxGUIConsole.h"
60
61 #include "bbtkConfigurationFile.h"
62 #include "bbtkUtilities.h"
63
64 namespace bbtk
65 {  
66
67   //========================================================================
68   enum 
69     {
70       bwd_id,
71       fwd_id,
72       home_id,
73       reload_id,
74       include_id,
75       url_id  ,
76       html_id = 10100
77     };
78
79
80 /*
81   //========================================================================
82   void WxHtmlWindow::OnLinkClicked(const wxHtmlLinkInfo& e)
83   {
84         std::cout  << "WxHtmlWindow::OnLink"<<std::endl;
85     //    std::cout  << e.GetHref()<<std::endl;
86     mBrowser->OnLinkClicked2(e);
87   }
88 */
89
90 //EED2  BEGIN_EVENT_TABLE(WxHtmlWindow, wxPanel)
91 //EED2    EVT_SIZE(WxHtmlWindow::OnSize)
92 //EED2  END_EVENT_TABLE()
93
94 //EED2  void WxHtmlWindow::OnSize( wxSizeEvent& )
95 //EED2  {
96 //EED2          printf("EED WxHtmlWindow::OnSize  \n" );
97 //EED2          Scroll(10,500);
98 //EED2  }
99
100
101   //========================================================================
102   
103   BEGIN_EVENT_TABLE(WxGUIHtmlBrowser, wxPanel)
104     EVT_BUTTON(bwd_id, WxGUIHtmlBrowser::OnBackButton )
105     EVT_BUTTON(fwd_id, WxGUIHtmlBrowser::OnForwardButton )
106     EVT_BUTTON(home_id, WxGUIHtmlBrowser::OnHomeButton )
107     EVT_BUTTON(reload_id, WxGUIHtmlBrowser::OnReloadButton )
108   //    EVT_BUTTON(include_id, WxGUIHtmlBrowser::OnIncludeFileButton )
109     EVT_TEXT_ENTER(url_id, WxGUIHtmlBrowser::OnURLEnter )
110    EVT_HTML_LINK_CLICKED(html_id, WxGUIHtmlBrowser::OnLinkClicked)
111     EVT_SIZE(WxGUIHtmlBrowser::OnSize)
112
113   END_EVENT_TABLE()
114   //========================================================================
115
116
117   //========================================================================
118     WxGUIHtmlBrowser::WxGUIHtmlBrowser ( wxWindow *parent, wxSize size,
119                                          WxGUIHtmlBrowserUser* user)
120       : 
121       wxPanel ( parent, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL),
122       mUser(user)
123
124   {
125     wxPanel* panel = this;
126
127     wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
128     
129     
130     wxBoxSizer *bsizer = new wxBoxSizer(wxHORIZONTAL);
131     
132     mwxBackButton = new wxButton( panel, bwd_id,_T("<"),wxDefaultPosition,
133                                   wxDefaultSize,wxBU_EXACTFIT);
134     bsizer->Add ( mwxBackButton , 0, wxALIGN_CENTRE );
135     
136     mwxForwardButton = new wxButton( panel, fwd_id,_T(">"),wxDefaultPosition,
137                                      wxDefaultSize,wxBU_EXACTFIT);
138     bsizer->Add ( mwxForwardButton  , 0, wxALIGN_CENTRE );
139     
140     mwxHomeButton = new wxButton( panel, home_id,_T("Home"),wxDefaultPosition,
141                                   wxDefaultSize,wxBU_EXACTFIT);
142     bsizer->Add ( mwxHomeButton , 0, wxALIGN_CENTRE );
143     
144     mwxReloadButton = new wxButton( panel, reload_id,_T("Reload"),wxDefaultPosition,
145                                     wxDefaultSize,wxBU_EXACTFIT);
146     bsizer->Add ( mwxReloadButton  , 0, wxALIGN_CENTRE );
147     
148     /*
149     mwxIncludeFileButton = new wxButton( panel, include_id,
150                                        _T("RUN"),wxDefaultPosition,
151                                        wxDefaultSize,wxBU_EXACTFIT);
152     bsizer->Add ( mwxIncludeFileButton , 0, wxALIGN_CENTRE |  wxLEFT | wxTOP | wxBOTTOM , 10 );
153     */
154
155     mwxURL = new wxTextCtrl(panel,url_id,_T(""),
156                             wxDefaultPosition,
157                             wxDefaultSize,
158                             wxTE_PROCESS_ENTER);
159     mwxURL->SetLabel(_T("URL"));
160     bsizer->Add(mwxURL, 1, wxEXPAND);
161
162
163 //    mwxHtmlWindow = new WxHtmlWindow(this,html_id,this,size);
164
165           mwxHtmlWindow = new wxHtmlWindow(this,html_id,
166                                                                          wxDefaultPosition, 
167                                                                          size,
168                                                                          wxHW_SCROLLBAR_AUTO, 
169                                                                          _T("bbtk::WxGUIHtmlBrowser"));
170
171     /*
172
173 -1, 
174                                      wxDefaultPosition, 
175                                      size,
176                                      wxHW_SCROLLBAR_AUTO, 
177                                      "bbtk::WxGUIHtmlBrowser");
178     */
179     mwxHtmlWindow->SetBorders(5);
180     //  mwxHtmlWindow->FitInside();
181
182         sizer->Add ( bsizer , 0, wxEXPAND );
183           
184 // EED: This have a problem in macOS
185 //    wxStaticBoxSizer* hw = 
186 //          new wxStaticBoxSizer( 
187 //                                                         new wxStaticBox( this, -1, _T(""),  wxDefaultPosition,  size ), 
188 //                                                        wxVERTICAL 
189 //                               );
190 //        hw->Add ( mwxHtmlWindow, 1, wxEXPAND  );        
191 //EED    sizer->Add ( hw, 1, wxGROW ); // | wxLEFT | wxRIGHT | wxBOTTOM, 10 );
192           
193     sizer->Add ( mwxHtmlWindow, 1, wxGROW ); // | wxLEFT | wxRIGHT | wxBOTTOM, 10 );
194
195     panel       -> SetSizer(sizer);
196
197     panel       -> SetAutoLayout(true);
198     panel       -> Layout();
199
200 //    GoHome();
201
202    }
203   //========================================================================
204  
205
206   //========================================================================
207   bool WxGUIHtmlBrowser::GoTo(std::string& file)
208   { 
209     bool r = mwxHtmlWindow->LoadPage(std2wx(file));
210     UpdateURL();
211     return r;
212   }
213   //========================================================================
214   
215   //========================================================================
216   void WxGUIHtmlBrowser::OnBackButton(wxCommandEvent& )
217   {
218     mwxHtmlWindow->HistoryBack();
219     UpdateURL();
220   }
221   //========================================================================
222
223   //========================================================================
224   void WxGUIHtmlBrowser::OnForwardButton(wxCommandEvent& )
225   {
226     mwxHtmlWindow->HistoryForward();
227     UpdateURL();
228   }
229   //========================================================================
230
231   //========================================================================
232   void WxGUIHtmlBrowser::GoHome()
233   {
234     std::string url = ConfigurationFile::GetInstance().Get_doc_path();
235     url += "/help_contents.html";
236     GoTo(url);
237   }
238   //========================================================================
239
240   //========================================================================
241   void WxGUIHtmlBrowser::OnHomeButton(wxCommandEvent& )
242   {
243     GoHome();
244   }
245   //========================================================================
246
247   //========================================================================
248   void WxGUIHtmlBrowser::OnReloadButton(wxCommandEvent& e)
249   {
250     OnURLEnter(e);
251   }
252   //========================================================================
253   //========================================================================
254   std::string WxGUIHtmlBrowser::GetCurrentPage()
255   {
256     return wx2std(mwxURL->GetValue());
257   }
258   //========================================================================
259
260   /*
261   //========================================================================
262   void WxGUIHtmlBrowser::OnIncludeFileButton(wxCommandEvent& )
263   {
264     std::string filename = wx2std(mwxURL->GetValue());
265     size_t s = filename.length();
266
267     WxGUIConsole* C = mWxGUIConsole; //::GetInstance();
268     //    MessageManager::SetMessageLevel("All",9);
269     Interpreter* I = new Interpreter;
270     
271     if ((s>3) && (filename[s-1]=='s')
272         && (filename[s-2]=='b')
273         && (filename[s-3]=='b')
274         && (filename[s-4]=='.'))
275       {
276         std::cout << "stat"<<std::endl;
277         if (C!=0) C->SetStatusText(_T("Executing ")+mwxURL->GetValue());
278         std::cout << "int"<<std::endl;
279         I->InterpretFile(filename);
280         std::cout << "eoint"<<std::endl;
281       }
282     else
283       {
284         if (C!=0) C->SetStatusText(_T("The current page is not a bbs file : cannot execute it"));
285       }
286     
287     delete I;
288    }
289   //========================================================================
290   */
291
292
293   //======================================================================== 
294   void WxGUIHtmlBrowser::OnURLEnter( wxCommandEvent&)
295   { 
296     mwxHtmlWindow->LoadPage(mwxURL->GetValue());
297   }
298   //========================================================================
299   
300         //========================================================================
301         void WxGUIHtmlBrowser::OnLinkClicked2(const wxHtmlLinkInfo& info)
302         {
303           //            std::cout << "OLK2"<<std::endl;
304                 bool go = true;
305                 if (mUser) 
306                 {
307                         wxString file = info.GetHref();
308                         go = mUser->WxGUIHtmlBrowserUserOnLinkClicked( wx2std( file ) );
309                 }
310                 if (go) 
311                 {
312                         mwxHtmlWindow->LoadPage( info.GetHref() );
313                         UpdateURL();
314                 }
315                 
316                 
317         }
318                 
319   //========================================================================
320   void WxGUIHtmlBrowser::OnLinkClicked(wxHtmlLinkEvent& e)
321   { 
322     //    std::cout << "OLK"<<std::endl;
323     bool go = true;
324     if (mUser) 
325       {
326                   /*
327         wxString file = wxPathOnly(mwxURL->GetValue());
328         file += std2wx(ConfigurationFile::GetInstance().Get_file_separator());
329         file += e.GetLinkInfo().GetHref();
330         */
331                 wxString file = e.GetLinkInfo().GetHref();
332                 go = mUser->WxGUIHtmlBrowserUserOnLinkClicked( wx2std( file ) );
333       }
334     if (go) 
335       {
336         mwxHtmlWindow->LoadPage( e.GetLinkInfo().GetHref() );
337         UpdateURL();
338         //      OnURLEnter(e);
339         //      mwxHtmlWindow->LoadPage(mwxURL->GetValue());
340      }
341
342     //    mwxHtmlWindow->LoadPage( mwxURL->GetValue() );
343    }
344   //========================================================================
345
346
347   //========================================================================
348   void WxGUIHtmlBrowser::OnSize(wxSizeEvent& e)
349   { 
350           mwxHtmlWindow->EnableScrolling(true,true);
351           if   (  mwxURL->GetValue()!=wxString(_T(""))  )
352           {
353 // ??????????   No funciona ....?????
354 //EED2          mwxHtmlWindow->LoadPage(mwxURL->GetValue());
355 //              printf("EED WxGUIHtmlBrowser::OnSize %s \n", mwxURL->GetValue().c_str() );
356 //EED2          mwxHtmlWindow->Scroll( 10,  500);
357           } else {
358 //              GoHome();
359           }
360           e.Skip(true);
361   }
362
363   /*
364   void WxGUIHtmlBrowser::OnCell(wxHtmlCellEvent& )
365   {
366     std::cout  << "OnCell"<<std::endl;
367   }
368   */
369
370   //========================================================================
371   void WxGUIHtmlBrowser::UpdateURL()
372   {
373     wxString s = mwxHtmlWindow->GetOpenedPage();
374     if (!mwxHtmlWindow->GetOpenedAnchor().IsEmpty())
375     {
376             s += _T("#") + mwxHtmlWindow->GetOpenedAnchor();
377     }
378     mwxURL->Clear();
379     mwxURL->AppendText(s);
380   }
381   //========================================================================
382
383   //========================================================================
384   void WxGUIHtmlBrowser::SetSize( wxSize s)
385   {
386     //    wxPanel::SetSize(s);
387     mwxHtmlWindow->SetSize(s);
388     Fit();
389   }
390   //========================================================================
391 }
392
393 #endif