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