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