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