]> Creatis software - bbtk.git/blob - kernel/src/bbtkWxGUIHtmlBrowser.cxx
4994d18c40eaec19814fe7e081df3e06e7386082
[bbtk.git] / kernel / src / bbtkWxGUIHtmlBrowser.cxx
1 /*=========================================================================                                                                               
2   Program:   bbtk
3   Module:    $RCSfile: bbtkWxGUIHtmlBrowser.cxx,v $
4   Language:  C++
5   Date:      $Date: 2009/03/23 13:06:41 $
6   Version:   $Revision: 1.13 $
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     panel       -> SetAutoLayout(true);
190     panel       -> Layout();
191
192 //    GoHome();
193
194    }
195   //========================================================================
196  
197
198   //========================================================================
199   bool WxGUIHtmlBrowser::GoTo(std::string& file)
200   { 
201     bool r = mwxHtmlWindow->LoadPage(std2wx(file));
202     UpdateURL();
203     return r;
204   }
205   //========================================================================
206   
207   //========================================================================
208   void WxGUIHtmlBrowser::OnBackButton(wxCommandEvent& )
209   {
210     mwxHtmlWindow->HistoryBack();
211     UpdateURL();
212   }
213   //========================================================================
214
215   //========================================================================
216   void WxGUIHtmlBrowser::OnForwardButton(wxCommandEvent& )
217   {
218     mwxHtmlWindow->HistoryForward();
219     UpdateURL();
220   }
221   //========================================================================
222
223   //========================================================================
224   void WxGUIHtmlBrowser::GoHome()
225   {
226     std::string url = ConfigurationFile::GetInstance().Get_doc_path();
227     url += "/help_contents.html";
228     GoTo(url);
229   }
230   //========================================================================
231
232   //========================================================================
233   void WxGUIHtmlBrowser::OnHomeButton(wxCommandEvent& )
234   {
235     GoHome();
236   }
237   //========================================================================
238
239   //========================================================================
240   void WxGUIHtmlBrowser::OnReloadButton(wxCommandEvent& e)
241   {
242     OnURLEnter(e);
243   }
244   //========================================================================
245   //========================================================================
246   std::string WxGUIHtmlBrowser::GetCurrentPage()
247   {
248     return wx2std(mwxURL->GetValue());
249   }
250   //========================================================================
251
252   /*
253   //========================================================================
254   void WxGUIHtmlBrowser::OnIncludeFileButton(wxCommandEvent& )
255   {
256     std::string filename = wx2std(mwxURL->GetValue());
257     size_t s = filename.length();
258
259     WxGUIConsole* C = mWxGUIConsole; //::GetInstance();
260     //    MessageManager::SetMessageLevel("All",9);
261     Interpreter* I = new Interpreter;
262     
263     if ((s>3) && (filename[s-1]=='s')
264         && (filename[s-2]=='b')
265         && (filename[s-3]=='b')
266         && (filename[s-4]=='.'))
267       {
268         std::cout << "stat"<<std::endl;
269         if (C!=0) C->SetStatusText(_T("Executing ")+mwxURL->GetValue());
270         std::cout << "int"<<std::endl;
271         I->InterpretFile(filename);
272         std::cout << "eoint"<<std::endl;
273       }
274     else
275       {
276         if (C!=0) C->SetStatusText(_T("The current page is not a bbs file : cannot execute it"));
277       }
278     
279     delete I;
280    }
281   //========================================================================
282   */
283
284
285   //======================================================================== 
286   void WxGUIHtmlBrowser::OnURLEnter( wxCommandEvent&)
287   { 
288     mwxHtmlWindow->LoadPage(mwxURL->GetValue());
289   }
290   //========================================================================
291   
292         //========================================================================
293         void WxGUIHtmlBrowser::OnLinkClicked2(const wxHtmlLinkInfo& info)
294         {
295           //            std::cout << "OLK2"<<std::endl;
296                 bool go = true;
297                 if (mUser) 
298                 {
299                         wxString file = info.GetHref();
300                         go = mUser->WxGUIHtmlBrowserUserOnLinkClicked( wx2std( file ) );
301                 }
302                 if (go) 
303                 {
304                         mwxHtmlWindow->LoadPage( info.GetHref() );
305                         UpdateURL();
306                 }
307                 
308                 
309         }
310                 
311   //========================================================================
312   void WxGUIHtmlBrowser::OnLinkClicked(wxHtmlLinkEvent& e)
313   { 
314     //    std::cout << "OLK"<<std::endl;
315     bool go = true;
316     if (mUser) 
317       {
318                   /*
319         wxString file = wxPathOnly(mwxURL->GetValue());
320         file += std2wx(ConfigurationFile::GetInstance().Get_file_separator());
321         file += e.GetLinkInfo().GetHref();
322         */
323                 wxString file = e.GetLinkInfo().GetHref();
324                 go = mUser->WxGUIHtmlBrowserUserOnLinkClicked( wx2std( file ) );
325       }
326     if (go) 
327       {
328         mwxHtmlWindow->LoadPage( e.GetLinkInfo().GetHref() );
329         UpdateURL();
330         //      OnURLEnter(e);
331         //      mwxHtmlWindow->LoadPage(mwxURL->GetValue());
332      }
333
334     //    mwxHtmlWindow->LoadPage( mwxURL->GetValue() );
335    }
336   //========================================================================
337
338
339   //========================================================================
340   void WxGUIHtmlBrowser::OnSize(wxSizeEvent& e)
341   { 
342           mwxHtmlWindow->EnableScrolling(true,true);
343           if   (  mwxURL->GetValue()!=wxString(_T(""))  )
344           {
345 // ??????????   No funciona ....?????
346 //EED2          mwxHtmlWindow->LoadPage(mwxURL->GetValue());
347 //              printf("EED WxGUIHtmlBrowser::OnSize %s \n", mwxURL->GetValue().c_str() );
348 //EED2          mwxHtmlWindow->Scroll( 10,  500);
349           } else {
350 //              GoHome();
351           }
352           e.Skip(true);
353   }
354
355   /*
356   void WxGUIHtmlBrowser::OnCell(wxHtmlCellEvent& )
357   {
358     std::cout  << "OnCell"<<std::endl;
359   }
360   */
361
362   //========================================================================
363   void WxGUIHtmlBrowser::UpdateURL()
364   {
365     wxString s = mwxHtmlWindow->GetOpenedPage();
366     if (!mwxHtmlWindow->GetOpenedAnchor().IsEmpty())
367     {
368             s += _T("#") + mwxHtmlWindow->GetOpenedAnchor();
369     }
370     mwxURL->Clear();
371     mwxURL->AppendText(s);
372   }
373   //========================================================================
374
375   //========================================================================
376   void WxGUIHtmlBrowser::SetSize( wxSize s)
377   {
378     //    wxPanel::SetSize(s);
379     mwxHtmlWindow->SetSize(s);
380     Fit();
381   }
382   //========================================================================
383 }
384
385 #endif