]> Creatis software - bbtk.git/blob - kernel/src/bbtkWxBrowser.cxx
*** empty log message ***
[bbtk.git] / kernel / src / bbtkWxBrowser.cxx
1 #ifdef _USE_WXWIDGETS_
2
3 #include "bbtkWxBrowser.h"
4 #include "bbtkWxBlackBox.h"
5
6
7 namespace bbtk
8 {  
9
10
11   void WxHtmlWindow::OnLinkClicked(const wxHtmlLinkInfo& e)
12   {
13     //    std::cout  << "WxHtmlWindow::OnLink"<<std::endl;
14     //    std::cout  << e.GetHref()<<std::endl;
15     LoadPage(e.GetHref());
16     if ( ! GetOpenedAnchor().IsEmpty() )
17       { 
18         //      std::cout << "#" << GetOpenedAnchor() << std::endl;
19         LoadPage(_T("#"+GetOpenedAnchor()));
20       }
21     mBrowser->UpdateURL();
22
23
24   }
25
26
27   enum 
28     {
29       bwd_id,
30       fwd_id,
31       url_id,
32       html_id
33     };
34
35   
36   BEGIN_EVENT_TABLE(WxBrowser, wxPanel)
37     EVT_BUTTON(bwd_id, WxBrowser::OnBackButton )
38     EVT_BUTTON(fwd_id, WxBrowser::OnForwardButton )
39     EVT_TEXT_ENTER(url_id, WxBrowser::OnURLEnter )
40      EVT_HTML_LINK_CLICKED(html_id, WxBrowser::OnLinkClicked)
41   END_EVENT_TABLE()
42     
43     WxBrowser::WxBrowser ( wxWindow *parent, wxSize size )
44       : 
45     wxPanel ( parent, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL)
46
47   {
48     wxPanel* panel = this;
49
50     wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
51     
52
53     wxBoxSizer *bsizer = new wxBoxSizer(wxHORIZONTAL);
54     
55     sizer->Add ( bsizer, 0, wxGROW );
56     
57     
58     mwxBackButton = new wxButton( panel, bwd_id,_T("<"),wxDefaultPosition,
59                                   wxDefaultSize,wxBU_EXACTFIT);
60     bsizer->Add ( mwxBackButton, 0, wxALIGN_CENTRE | 
61                   wxLEFT | wxTOP | wxBOTTOM , 10 );
62
63     mwxForwardButton = new wxButton( panel, fwd_id,_T(">"),wxDefaultPosition,
64                                      wxDefaultSize,wxBU_EXACTFIT);
65     bsizer->Add ( mwxForwardButton, 0, wxALIGN_CENTRE | 
66                   wxTOP | wxBOTTOM , 10);
67
68   
69     mwxURL = new wxTextCtrl(panel,url_id,_T(""),
70                             wxDefaultPosition,
71                             wxDefaultSize,
72                             wxTE_PROCESS_ENTER);
73     mwxURL->SetLabel(_T("URL"));
74     bsizer->Add(mwxURL, 1, wxGROW | wxALL, 10);
75
76
77     mwxHtmlWindow = new WxHtmlWindow(parent,html_id,this,size);
78
79     /*
80
81 -1, 
82                                      wxDefaultPosition, 
83                                      size,
84                                      wxHW_SCROLLBAR_AUTO, 
85                                      "bbtk::WxBrowser");
86     */
87     mwxHtmlWindow->SetBorders(5);
88     //  mwxHtmlWindow->FitInside();
89     wxStaticBoxSizer* hw = 
90       new wxStaticBoxSizer( new wxStaticBox( this, -1, _T(""),
91                                              wxDefaultPosition, 
92                                              size ),
93                             wxVERTICAL );
94     hw->Add ( mwxHtmlWindow, 1, wxGROW  );
95     //sizer->Add ( mwxHtmlWindow, 1, wxGROW  );
96
97     sizer->Add ( hw, 1, wxGROW ); // | wxLEFT | wxRIGHT | wxBOTTOM, 10 );
98     
99  
100
101     panel       -> SetSizer(sizer);
102     panel       -> SetAutoLayout(true);
103     panel       -> Layout();
104
105     /*
106     Connect( mwxBackButton->GetId(), 
107              wxEVT_COMMAND_BUTTON_CLICKED , 
108              (wxObjectEventFunction) 
109              (void (wxPanel::*)(wxEvent&))
110              &WxBrowser::OnBackButton ); 
111     Connect( mwxForwardButton->GetId(), 
112              wxEVT_COMMAND_BUTTON_CLICKED , 
113              (wxObjectEventFunction) 
114              (void (wxPanel::*)(wxEvent&))
115              &WxBrowser::OnForwardButton ); 
116     
117     std::cout << "e="<<wxEVT_COMMAND_HTML_LINK_CLICKED<<std::endl;
118
119     Connect( wxID_ANY, //mwxHtmlWindow->GetId(), 
120              wxEVT_COMMAND_HTML_LINK_CLICKED , 
121              wxHtmlLinkEventHandler( 
122                                     //(wxObjectEventFunction) 
123                                     //       (void (wxPanel::*)(wxEvent&))&
124                                     WxBrowser::OnLinkClicked ) ); 
125     Connect( mwxHtmlWindow->GetId(), 
126              wxEVT_COMMAND_HTML_CELL_HOVER,
127              //(wxObjectEventFunction) 
128              wxHtmlCellEventHandler(
129                                                             //       (void (wxPanel::*)(wxHtmlCellEvent&))&
130                                     WxBrowser::OnCell ) ); 
131     */
132   }
133
134  
135   bool WxBrowser::GoTo(std::string& file)
136   { 
137     //  std::cout << "goto"<<std::endl;
138     bool r = mwxHtmlWindow->LoadPage(std2wx(file));
139     UpdateURL();
140     return r;
141   }
142   
143   void WxBrowser::OnBackButton(wxCommandEvent& )
144   {
145     //    std::cout << "back"<<std::endl;
146     mwxHtmlWindow->HistoryBack();
147     UpdateURL();
148   }
149   
150   void WxBrowser::OnForwardButton(wxCommandEvent& )
151   {
152     //    std::cout << "forward"<<std::endl;
153     mwxHtmlWindow->HistoryForward();
154     UpdateURL();
155   }
156
157   void WxBrowser::OnURLEnter( wxCommandEvent&)
158   { 
159     //std::cout  << "OnURL"<<std::endl;
160     mwxHtmlWindow->LoadPage(mwxURL->GetValue());
161   }
162
163   
164   void WxBrowser::OnLinkClicked(wxHtmlLinkEvent& e)
165   {
166     std::cout  << "WxBrowser::OnLinkClicked"<<std::endl;
167     std::cout  << e.GetLinkInfo().GetHref()<<std::endl;
168     mwxHtmlWindow->LoadPage(e.GetLinkInfo().GetHref());
169         UpdateURL();
170   }
171   /*
172   void WxBrowser::OnCell(wxHtmlCellEvent& )
173   {
174     std::cout  << "OnCell"<<std::endl;
175   }
176   */
177   void WxBrowser::UpdateURL()
178   {
179
180     wxString s = mwxHtmlWindow->GetOpenedPage();
181     if (!mwxHtmlWindow->GetOpenedAnchor().IsEmpty())
182     {
183             s += _T("#") + mwxHtmlWindow->GetOpenedAnchor();
184     }
185     mwxURL->Clear();
186     mwxURL->AppendText(s);
187   }
188   void WxBrowser::SetSize( wxSize s)
189   {
190     //    wxPanel::SetSize(s);
191     mwxHtmlWindow->SetSize(s);
192     Fit();
193   }
194
195 }
196
197 #endif