]> Creatis software - bbtk.git/blob - kernel/src/bbtkWxGUIHtmlBrowser.cxx
8fd20ab8aabc346ed71a6460097e2a09ad93a926
[bbtk.git] / kernel / src / bbtkWxGUIHtmlBrowser.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   bbtk
4   Module:    $RCSfile: bbtkWxGUIHtmlBrowser.cxx,v $
5   Language:  C++
6   Date:      $Date: 2008/03/25 06:22:53 $
7   Version:   $Revision: 1.4 $
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     //  std::cout << "goto"<<std::endl;
184
185     bool r = mwxHtmlWindow->LoadPage(std2wx(file));
186     UpdateURL();
187     return r;
188   }
189   //========================================================================
190   
191   //========================================================================
192   void WxGUIHtmlBrowser::OnBackButton(wxCommandEvent& )
193   {
194     //    std::cout << "back"<<std::endl;
195     mwxHtmlWindow->HistoryBack();
196     UpdateURL();
197   }
198   //========================================================================
199
200   //========================================================================
201   void WxGUIHtmlBrowser::OnForwardButton(wxCommandEvent& )
202   {
203     //    std::cout << "forward"<<std::endl;
204     mwxHtmlWindow->HistoryForward();
205     UpdateURL();
206   }
207   //========================================================================
208
209   //========================================================================
210   void WxGUIHtmlBrowser::GoHome()
211   {
212     std::string url = ConfigurationFile::GetInstance().Get_doc_path();
213     url += "/bbtkWebSite/menu.html";
214         GoTo(url);
215
216     mwxURL->Clear();
217     mwxURL->AppendText(std2wx(url));
218
219
220 /*EED
221     if (Utilities::FileExists(url)) 
222       {
223         GoTo(url);
224       }
225     else 
226       {
227         // what ?
228       }
229 */
230   }
231   //========================================================================
232
233   //========================================================================
234   void WxGUIHtmlBrowser::OnHomeButton(wxCommandEvent& )
235   {
236     GoHome();
237   }
238   //========================================================================
239
240   //========================================================================
241   void WxGUIHtmlBrowser::OnReloadButton(wxCommandEvent& e)
242   {
243     OnURLEnter(e);
244   }
245   //========================================================================
246   //========================================================================
247   std::string WxGUIHtmlBrowser::GetCurrentPage()
248   {
249     return wx2std(mwxURL->GetValue());
250   }
251   //========================================================================
252
253   /*
254   //========================================================================
255   void WxGUIHtmlBrowser::OnIncludeFileButton(wxCommandEvent& )
256   {
257     std::string filename = wx2std(mwxURL->GetValue());
258     size_t s = filename.length();
259
260     WxGUIConsole* C = mWxGUIConsole; //::GetInstance();
261     //    MessageManager::SetMessageLevel("All",9);
262     Interpreter* I = new Interpreter;
263     
264     if ((s>3) && (filename[s-1]=='s')
265         && (filename[s-2]=='b')
266         && (filename[s-3]=='b')
267         && (filename[s-4]=='.'))
268       {
269         std::cout << "stat"<<std::endl;
270         if (C!=0) C->SetStatusText(_T("Executing ")+mwxURL->GetValue());
271         std::cout << "int"<<std::endl;
272         I->InterpretFile(filename);
273         std::cout << "eoint"<<std::endl;
274       }
275     else
276       {
277         if (C!=0) C->SetStatusText(_T("The current page is not a bbs file : cannot execute it"));
278       }
279     
280     delete I;
281    }
282   //========================================================================
283   */
284
285
286   //======================================================================== 
287   void WxGUIHtmlBrowser::OnURLEnter( wxCommandEvent&)
288   { 
289     mwxHtmlWindow->LoadPage(mwxURL->GetValue());
290   }
291   //========================================================================
292   
293
294   //========================================================================
295   void WxGUIHtmlBrowser::OnLinkClicked(wxHtmlLinkEvent& e)
296   { 
297   
298     bool go = true;
299     if (mUser) 
300       {
301         wxString file = wxPathOnly(mwxURL->GetValue());
302         file += std2wx(ConfigurationFile::GetInstance().Get_file_separator());
303         file += e.GetLinkInfo().GetHref();
304         go = mUser->WxGUIHtmlBrowserUserOnLinkClicked( wx2std( file ) );
305       }
306     if (go) 
307       {
308         mwxHtmlWindow->LoadPage( e.GetLinkInfo().GetHref() );
309         UpdateURL();
310       }
311     //    mwxHtmlWindow->LoadPage( mwxURL->GetValue() );
312    }
313   //========================================================================
314
315
316   /*
317   void WxGUIHtmlBrowser::OnCell(wxHtmlCellEvent& )
318   {
319     std::cout  << "OnCell"<<std::endl;
320   }
321   */
322
323   //========================================================================
324   void WxGUIHtmlBrowser::UpdateURL()
325   {
326
327     wxString s = mwxHtmlWindow->GetOpenedPage();
328     if (!mwxHtmlWindow->GetOpenedAnchor().IsEmpty())
329     {
330             s += _T("#") + mwxHtmlWindow->GetOpenedAnchor();
331     }
332     mwxURL->Clear();
333     mwxURL->AppendText(s);
334   }
335   //========================================================================
336
337   //========================================================================
338   void WxGUIHtmlBrowser::SetSize( wxSize s)
339   {
340     //    wxPanel::SetSize(s);
341     mwxHtmlWindow->SetSize(s);
342     Fit();
343   }
344   //========================================================================
345 }
346
347 #endif