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