]> Creatis software - bbtk.git/blob - kernel/src/bbtkWxGUIHtmlBrowser.cxx
*** empty log message ***
[bbtk.git] / kernel / src / bbtkWxGUIHtmlBrowser.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   bbtk
4   Module:    $RCSfile: bbtkWxGUIHtmlBrowser.cxx,v $
5   Language:  C++
6   Date:      $Date: 2008/06/10 19:19:42 $
7   Version:   $Revision: 1.6 $
8                                                                                 
9   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10   l'Image). All rights reserved. See Doc/License.txt or
11   http://www.creatis.insa-lyon.fr/Public/bbtk/License.html for details.
12                                                                                 
13      This software is distributed WITHOUT ANY WARRANTY; without even
14      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15      PURPOSE.  See the above copyright notices for more information.
16                                                                                 
17 =========================================================================*//**
18  * \brief Short description in one line
19  * 
20  * Long description which 
21  * can span multiple lines
22  */
23 /**
24  * \file 
25  * \brief 
26  */
27 /**
28  * \class bbtk::
29  * \brief 
30  */
31
32 #ifdef _USE_WXWIDGETS_
33
34
35 #include "bbtkWxGUIHtmlBrowser.h"
36 //#include "bbtkWxBlackBox.h"
37 //#include "bbtkWxGUIConsole.h"
38
39 #include "bbtkConfigurationFile.h"
40 #include "bbtkUtilities.h"
41
42 namespace bbtk
43 {  
44
45   //========================================================================
46   enum 
47     {
48       bwd_id,
49       fwd_id,
50       home_id,
51       reload_id,
52       include_id,
53       url_id  ,
54       html_id
55     };
56
57
58 /*EED
59   //========================================================================
60   void WxHtmlWindow::OnLinkClicked(const wxHtmlLinkInfo& e)
61   {
62         std::cout  << "WxHtmlWindow::OnLink"<<std::endl;
63     //    std::cout  << e.GetHref()<<std::endl;
64     LoadPage(e.GetHref());
65     if ( ! GetOpenedAnchor().IsEmpty() )
66       { 
67         //      std::cout << "#" << GetOpenedAnchor() << std::endl;
68         LoadPage(_T("#"+GetOpenedAnchor()));
69       }
70     mBrowser->UpdateURL();
71   }
72   //========================================================================
73 */
74
75
76 //EED2  BEGIN_EVENT_TABLE(WxHtmlWindow, wxPanel)
77 //EED2    EVT_SIZE(WxHtmlWindow::OnSize)
78 //EED2  END_EVENT_TABLE()
79
80 //EED2  void WxHtmlWindow::OnSize( wxSizeEvent& )
81 //EED2  {
82 //EED2          printf("EED WxHtmlWindow::OnSize  \n" );
83 //EED2          Scroll(10,500);
84 //EED2  }
85
86
87   //========================================================================
88   
89   BEGIN_EVENT_TABLE(WxGUIHtmlBrowser, wxPanel)
90     EVT_BUTTON(bwd_id, WxGUIHtmlBrowser::OnBackButton )
91     EVT_BUTTON(fwd_id, WxGUIHtmlBrowser::OnForwardButton )
92     EVT_BUTTON(home_id, WxGUIHtmlBrowser::OnHomeButton )
93     EVT_BUTTON(reload_id, WxGUIHtmlBrowser::OnReloadButton )
94   //    EVT_BUTTON(include_id, WxGUIHtmlBrowser::OnIncludeFileButton )
95     EVT_TEXT_ENTER(url_id, WxGUIHtmlBrowser::OnURLEnter )
96     EVT_HTML_LINK_CLICKED(html_id, WxGUIHtmlBrowser::OnLinkClicked)
97     EVT_SIZE(WxGUIHtmlBrowser::OnSize)
98
99   END_EVENT_TABLE()
100   //========================================================================
101
102
103   //========================================================================
104     WxGUIHtmlBrowser::WxGUIHtmlBrowser ( wxWindow *parent, wxSize size,
105                                          WxGUIHtmlBrowserUser* user)
106       : 
107       wxPanel ( parent, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL),
108       mUser(user)
109
110   {
111     wxPanel* panel = this;
112     
113     wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
114     
115     
116     wxBoxSizer *bsizer = new wxBoxSizer(wxHORIZONTAL);
117     
118     mwxBackButton = new wxButton( panel, bwd_id,_T("<"),wxDefaultPosition,
119                                   wxDefaultSize,wxBU_EXACTFIT);
120     bsizer->Add ( mwxBackButton , 0, wxALIGN_CENTRE );
121     
122     mwxForwardButton = new wxButton( panel, fwd_id,_T(">"),wxDefaultPosition,
123                                      wxDefaultSize,wxBU_EXACTFIT);
124     bsizer->Add ( mwxForwardButton  , 0, wxALIGN_CENTRE );
125     
126     mwxHomeButton = new wxButton( panel, home_id,_T("Home"),wxDefaultPosition,
127                                   wxDefaultSize,wxBU_EXACTFIT);
128     bsizer->Add ( mwxHomeButton , 0, wxALIGN_CENTRE );
129     
130     mwxReloadButton = new wxButton( panel, reload_id,_T("Reload"),wxDefaultPosition,
131                                     wxDefaultSize,wxBU_EXACTFIT);
132     bsizer->Add ( mwxReloadButton  , 0, wxALIGN_CENTRE );
133     
134     /*
135     mwxIncludeFileButton = new wxButton( panel, include_id,
136                                        _T("RUN"),wxDefaultPosition,
137                                        wxDefaultSize,wxBU_EXACTFIT);
138     bsizer->Add ( mwxIncludeFileButton , 0, wxALIGN_CENTRE |  wxLEFT | wxTOP | wxBOTTOM , 10 );
139     */
140
141     mwxURL = new wxTextCtrl(panel,url_id,_T(""),
142                             wxDefaultPosition,
143                             wxDefaultSize,
144                             wxTE_PROCESS_ENTER);
145     mwxURL->SetLabel(_T("URL"));
146     bsizer->Add(mwxURL, 1, wxEXPAND);
147
148
149 //    mwxHtmlWindow = new WxHtmlWindow(parent,html_id,this,size);
150     mwxHtmlWindow = new WxHtmlWindow(this,html_id,this,size);
151
152     /*
153
154 -1, 
155                                      wxDefaultPosition, 
156                                      size,
157                                      wxHW_SCROLLBAR_AUTO, 
158                                      "bbtk::WxGUIHtmlBrowser");
159     */
160     mwxHtmlWindow->SetBorders(5);
161     //  mwxHtmlWindow->FitInside();
162
163     wxStaticBoxSizer* hw = 
164       new wxStaticBoxSizer( new wxStaticBox( this, -1, _T(""),
165                                              wxDefaultPosition, 
166                                              size ),
167                             wxVERTICAL );
168 //EED    hw->Add ( mwxHtmlWindow, 1, wxGROW  );
169     hw->Add ( mwxHtmlWindow, 1, wxEXPAND  );
170
171
172
173
174
175     //sizer->Add ( mwxHtmlWindow, 1, wxGROW  );
176 //EED    sizer->Add ( hw, 1, wxGROW ); // | wxLEFT | wxRIGHT | wxBOTTOM, 10 );
177
178     sizer->Add ( bsizer , 0, wxEXPAND );
179     sizer->Add ( hw, 1, wxGROW ); // | wxLEFT | wxRIGHT | wxBOTTOM, 10 );
180
181
182
183     panel       -> SetSizer(sizer);
184     panel       -> SetAutoLayout(true);
185     panel       -> Layout();
186
187 //    GoHome();
188
189    }
190   //========================================================================
191  
192
193   //========================================================================
194   bool WxGUIHtmlBrowser::GoTo(std::string& file)
195   { 
196     bool r = mwxHtmlWindow->LoadPage(std2wx(file));
197     UpdateURL();
198     return r;
199   }
200   //========================================================================
201   
202   //========================================================================
203   void WxGUIHtmlBrowser::OnBackButton(wxCommandEvent& )
204   {
205     mwxHtmlWindow->HistoryBack();
206     UpdateURL();
207   }
208   //========================================================================
209
210   //========================================================================
211   void WxGUIHtmlBrowser::OnForwardButton(wxCommandEvent& )
212   {
213     mwxHtmlWindow->HistoryForward();
214     UpdateURL();
215   }
216   //========================================================================
217
218   //========================================================================
219   void WxGUIHtmlBrowser::GoHome()
220   {
221     std::string url = ConfigurationFile::GetInstance().Get_doc_path();
222     url += "/bbtkWebSite/menu.html";
223         GoTo(url);
224
225     mwxURL->Clear();
226     mwxURL->AppendText(std2wx(url));
227
228
229 /*EED
230     if (Utilities::FileExists(url)) 
231       {
232         GoTo(url);
233       }
234     else 
235       {
236         // what ?
237       }
238 */
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         wxString file = wxPathOnly(mwxURL->GetValue());
311         file += std2wx(ConfigurationFile::GetInstance().Get_file_separator());
312         file += e.GetLinkInfo().GetHref();
313         go = mUser->WxGUIHtmlBrowserUserOnLinkClicked( wx2std( file ) );
314       }
315     if (go) 
316       {
317         mwxHtmlWindow->LoadPage( e.GetLinkInfo().GetHref() );
318         UpdateURL();
319       }
320     //    mwxHtmlWindow->LoadPage( mwxURL->GetValue() );
321    }
322   //========================================================================
323
324
325   //========================================================================
326   void WxGUIHtmlBrowser::OnSize(wxSizeEvent& e)
327   { 
328           mwxHtmlWindow->EnableScrolling(true,true);
329           if   (  mwxURL->GetValue()!=wxString(_T(""))  )
330           {
331 // ??????????   No funciona ....?????
332 //EED2          mwxHtmlWindow->LoadPage(mwxURL->GetValue());
333 //              printf("EED WxGUIHtmlBrowser::OnSize %s \n", mwxURL->GetValue().c_str() );
334 //EED2          mwxHtmlWindow->Scroll( 10,  500);
335           } else {
336 //              GoHome();
337           }
338           e.Skip(true);
339   }
340
341   /*
342   void WxGUIHtmlBrowser::OnCell(wxHtmlCellEvent& )
343   {
344     std::cout  << "OnCell"<<std::endl;
345   }
346   */
347
348   //========================================================================
349   void WxGUIHtmlBrowser::UpdateURL()
350   {
351
352     wxString s = mwxHtmlWindow->GetOpenedPage();
353     if (!mwxHtmlWindow->GetOpenedAnchor().IsEmpty())
354     {
355             s += _T("#") + mwxHtmlWindow->GetOpenedAnchor();
356     }
357     mwxURL->Clear();
358     mwxURL->AppendText(s);
359   }
360   //========================================================================
361
362   //========================================================================
363   void WxGUIHtmlBrowser::SetSize( wxSize s)
364   {
365     //    wxPanel::SetSize(s);
366     mwxHtmlWindow->SetSize(s);
367     Fit();
368   }
369   //========================================================================
370 }
371
372 #endif