]> git.saurik.com Git - wxWidgets.git/blob - src/html/htmlwin.cpp
started adding swigged sources
[wxWidgets.git] / src / html / htmlwin.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: htmlwin.cpp
3 // Purpose: wxHtmlWindow class for parsing & displaying HTML (implementation)
4 // Author: Vaclav Slavik
5 // Copyright: (c) 1999 Vaclav Slavik
6 // Licence: wxWindows Licence
7 /////////////////////////////////////////////////////////////////////////////
8
9
10 #ifdef __GNUG__
11 #pragma implementation
12 #endif
13
14 #include <wx/wxprec.h>
15
16 #include "wx/defs.h"
17 #if wxUSE_HTML
18
19 #ifdef __BORDLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WXPRECOMP
24 #include <wx/wx.h>
25 #endif
26
27 #include <wx/html/htmlwin.h>
28
29 #include <wx/html/forcelink.h>
30
31
32
33 //-----------------------------------------------------------------------------
34 // wxHtmlWindow
35 //-----------------------------------------------------------------------------
36
37
38
39 #include <wx/arrimpl.cpp>
40 WX_DEFINE_OBJARRAY(HtmlHistoryArray)
41
42
43 wxHtmlWindow::wxHtmlWindow(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
44 long style, const wxString& name) : wxScrolledWindow(parent, id, pos, size, wxVSCROLL, name)
45 {
46 m_tmpMouseMoved = FALSE;
47 m_tmpCanDraw = TRUE;
48 m_FS = new wxFileSystem();
49 m_RelatedStatusBar = -1;
50 m_RelatedFrame = NULL;
51 m_TitleFormat = "%s";
52 m_OpenedPage = m_OpenedAnchor = wxEmptyString;
53 m_Cell = NULL;
54 m_Parser = new wxHtmlWinParser(this);
55 m_Parser -> SetFS(m_FS);
56 SetBorders(10);
57 m_HistoryPos = -1;
58 m_HistoryOn = TRUE;
59 m_Style = style;
60 SetPage("<html><body></body></html>");
61 }
62
63
64
65 wxHtmlWindow::~wxHtmlWindow()
66 {
67 HistoryClear();
68
69 if (m_Cell) delete m_Cell;
70
71 wxList *parser_data = m_Parser -> GetTempData();
72 if (parser_data) delete parser_data;
73
74 delete m_Parser;
75 delete m_FS;
76 }
77
78
79
80 void wxHtmlWindow::SetRelatedFrame(wxFrame* frame, const wxString& format)
81 {
82 m_RelatedFrame = frame;
83 m_TitleFormat = format;
84 }
85
86
87
88 void wxHtmlWindow::SetRelatedStatusBar(int bar)
89 {
90 m_RelatedStatusBar = bar;
91 }
92
93
94
95 void wxHtmlWindow::SetFonts(wxString normal_face, int normal_italic_mode, wxString fixed_face, int fixed_italic_mode, int *sizes)
96 {
97 m_Parser -> SetFonts(normal_face, normal_italic_mode, fixed_face, fixed_italic_mode, sizes);
98 if (!m_OpenedPage.IsEmpty()) LoadPage(m_OpenedPage);
99 }
100
101
102
103 bool wxHtmlWindow::SetPage(const wxString& source)
104 {
105 wxClientDC *dc = new wxClientDC(this);
106
107 dc -> SetMapMode(wxMM_TEXT);
108 SetBackgroundColour(wxColour(0xFF, 0xFF, 0xFF));
109 m_OpenedPage = m_OpenedAnchor = wxEmptyString;
110 m_Parser -> SetDC(dc);
111 if (m_Cell) {
112 delete m_Cell;
113 m_Cell = NULL;
114 }
115 m_Cell = (wxHtmlContainerCell*) m_Parser -> Parse(source);
116 delete dc;
117 m_Cell -> SetIndent(m_Borders, HTML_INDENT_ALL, HTML_UNITS_PIXELS);
118 m_Cell -> SetAlignHor(HTML_ALIGN_CENTER);
119 CreateLayout();
120 Refresh();
121 return TRUE;
122 }
123
124
125 bool wxHtmlWindow::LoadPage(const wxString& location)
126 {
127 wxFSFile *f;
128 bool rt_val;
129 wxBusyCursor b;
130
131 m_tmpCanDraw = FALSE;
132 if (m_HistoryOn && (m_HistoryPos != -1)) { // store scroll position into history item
133 int x, y;
134 ViewStart(&x, &y);
135 m_History[m_HistoryPos].SetPos(y);
136 }
137
138 if (location[0] == '#') { // local anchor
139 wxString anch = location.Mid(1) /*1 to end*/;
140 m_tmpCanDraw = TRUE;
141 rt_val = ScrollToAnchor(anch);
142 }
143
144 else {
145 // load&display it:
146 if (m_RelatedStatusBar != -1) {
147 m_RelatedFrame -> SetStatusText(_("Connecting..."), m_RelatedStatusBar);
148 Refresh();
149 }
150
151 f = m_FS -> OpenFile(location);
152 if (f == NULL) {
153 wxString err;
154
155 err.Printf(_("The browser is unable to open requested location :\n\n%s"), WXSTRINGCAST location);
156 m_tmpCanDraw = TRUE;
157 Refresh();
158 wxMessageBox(err, "Error");
159 return FALSE;
160 }
161
162 else {
163 wxNode *node;
164 wxString src = wxEmptyString;
165
166 if (m_RelatedStatusBar != -1) {
167 wxString msg = _("Loading : ") + location;
168 m_RelatedFrame -> SetStatusText(msg, m_RelatedStatusBar);
169 Refresh();
170 }
171
172 node = m_Filters.GetFirst();
173 while (node){
174 wxHtmlFilter *h = (wxHtmlFilter*) node -> GetData();
175 if (h -> CanRead(*f)) {
176 src = h -> ReadFile(*f);
177 break;
178 }
179 node = node -> GetNext();
180 }
181 if (src == wxEmptyString) {
182 if (m_DefaultFilter == NULL) m_DefaultFilter = GetDefaultFilter();
183 src = m_DefaultFilter -> ReadFile(*f);
184 }
185
186 m_FS -> ChangePathTo(f -> GetLocation());
187 rt_val = SetPage(src);
188 m_OpenedPage = f -> GetLocation();
189 if (f -> GetAnchor() != wxEmptyString) {
190 m_tmpCanDraw = TRUE;
191 ScrollToAnchor(f -> GetAnchor());
192 m_tmpCanDraw = FALSE;
193 }
194
195 delete f;
196
197 if (m_RelatedStatusBar != -1) m_RelatedFrame -> SetStatusText(_("Done"), m_RelatedStatusBar);
198 }
199 }
200
201 if (m_HistoryOn) { // add this page to history there:
202 int c = m_History.GetCount() - (m_HistoryPos + 1);
203
204 m_HistoryPos++;
205 for (int i = 0; i < c; i++)
206 m_History.Remove(m_HistoryPos);
207 m_History.Add(new HtmlHistoryItem(m_OpenedPage, m_OpenedAnchor));
208 }
209
210 m_tmpCanDraw = TRUE;
211 Refresh();
212 return rt_val;
213 }
214
215
216
217 bool wxHtmlWindow::ScrollToAnchor(const wxString& anchor)
218 {
219 const wxHtmlCell *c = m_Cell -> Find(HTML_COND_ISANCHOR, &anchor);
220 if (!c) return FALSE;
221 else {
222 int y;
223
224 for (y = 0; c != NULL; c = c -> GetParent()) y += c -> GetPosY();
225 Scroll(-1, y / HTML_SCROLL_STEP);
226 m_OpenedAnchor = anchor;
227 return TRUE;
228 }
229 }
230
231
232 void wxHtmlWindow::SetTitle(const wxString& title)
233 {
234 if (m_RelatedFrame) {
235 wxString tit;
236 tit.Printf(m_TitleFormat, title.c_str());
237 m_RelatedFrame -> SetTitle(tit);
238 }
239 }
240
241
242
243
244
245 void wxHtmlWindow::CreateLayout()
246 {
247 int ClientWidth, ClientHeight;
248
249 if (!m_Cell) return;
250
251 if (m_Style == wxHW_SCROLLBAR_NEVER) {
252 SetScrollbars(1, 1, 0, 0); // always off
253 GetClientSize(&ClientWidth, &ClientHeight);
254 m_Cell -> Layout(ClientWidth);
255 }
256
257 else {
258 GetClientSize(&ClientWidth, &ClientHeight);
259 #ifndef __WXMSW__
260 // VS : this looks extremely ugly under windoze, better fix needed!
261 SetScrollbars(1, 1, 0, ClientHeight * 2); // always on
262 #endif
263 GetClientSize(&ClientWidth, &ClientHeight);
264 m_Cell -> Layout(ClientWidth);
265 if (ClientHeight < m_Cell -> GetHeight()) {
266 SetScrollbars(HTML_SCROLL_STEP, HTML_SCROLL_STEP,
267 m_Cell -> GetWidth() / HTML_SCROLL_STEP,
268 m_Cell -> GetHeight() / HTML_SCROLL_STEP
269 /*cheat: top-level frag is always container*/);
270 }
271 else { /* we fit into window, no need for scrollbars */
272 SetScrollbars(1, 1, 0, 0); // disable...
273 GetClientSize(&ClientWidth, &ClientHeight);
274 m_Cell -> Layout(ClientWidth); // ...and relayout
275 }
276 }
277 }
278
279
280
281 void wxHtmlWindow::ReadCustomization(wxConfigBase *cfg, wxString path)
282 {
283 wxString oldpath;
284 wxString tmp;
285
286 if (path != wxEmptyString) {
287 oldpath = cfg -> GetPath();
288 cfg -> SetPath(path);
289 }
290
291 m_Borders = cfg -> Read("wxHtmlWindow/Borders", m_Borders);
292 m_Parser -> m_FontFaceFixed = cfg -> Read("wxHtmlWindow/FontFaceFixed", m_Parser -> m_FontFaceFixed);
293 m_Parser -> m_FontFaceNormal = cfg -> Read("wxHtmlWindow/FontFaceNormal", m_Parser -> m_FontFaceNormal);
294 m_Parser -> m_ItalicModeFixed = cfg -> Read("wxHtmlWindow/ItalicModeFixed", m_Parser -> m_ItalicModeFixed);
295 m_Parser -> m_ItalicModeNormal = cfg -> Read("wxHtmlWindow/ItalicModeNormal", m_Parser -> m_ItalicModeNormal);
296 for (int i = 0; i < 7; i++) {
297 tmp.Printf("wxHtmlWindow/FontsSize%i", i);
298 m_Parser -> m_FontsSizes[i] = cfg -> Read(tmp, m_Parser -> m_FontsSizes[i]);
299 }
300
301 if (path != wxEmptyString)
302 cfg -> SetPath(oldpath);
303 }
304
305
306
307 void wxHtmlWindow::WriteCustomization(wxConfigBase *cfg, wxString path)
308 {
309 wxString oldpath;
310 wxString tmp;
311
312 if (path != wxEmptyString) {
313 oldpath = cfg -> GetPath();
314 cfg -> SetPath(path);
315 }
316
317 cfg -> Write("wxHtmlWindow/Borders", (long) m_Borders);
318 cfg -> Write("wxHtmlWindow/FontFaceFixed", m_Parser -> m_FontFaceFixed);
319 cfg -> Write("wxHtmlWindow/FontFaceNormal", m_Parser -> m_FontFaceNormal);
320 cfg -> Write("wxHtmlWindow/ItalicModeFixed", (long) m_Parser -> m_ItalicModeFixed);
321 cfg -> Write("wxHtmlWindow/ItalicModeNormal", (long) m_Parser -> m_ItalicModeNormal);
322 for (int i = 0; i < 7; i++) {
323 tmp.Printf("wxHtmlWindow/FontsSize%i", i);
324 cfg -> Write(tmp, (long) m_Parser -> m_FontsSizes[i]);
325 }
326
327 if (path != wxEmptyString)
328 cfg -> SetPath(oldpath);
329 }
330
331
332
333 bool wxHtmlWindow::HistoryBack()
334 {
335 wxString a, l;
336
337 if (m_HistoryPos < 1) return FALSE;
338
339 m_HistoryPos--;
340
341 l = m_History[m_HistoryPos].GetPage();
342 a = m_History[m_HistoryPos].GetAnchor();
343 m_HistoryOn = FALSE;
344 if (a == wxEmptyString) LoadPage(l);
345 else LoadPage(l + "#" + a);
346 m_HistoryOn = TRUE;
347 Scroll(0, m_History[m_HistoryPos].GetPos());
348 Refresh();
349 return TRUE;
350 }
351
352
353
354 bool wxHtmlWindow::HistoryForward()
355 {
356 wxString a, l;
357
358 if (m_HistoryPos == -1) return FALSE;
359 if (m_HistoryPos >= (int)m_History.GetCount() - 1)return FALSE;
360
361 m_OpenedPage = wxEmptyString; // this will disable adding new entry into history in LoadPage()
362
363 m_HistoryPos++;
364 l = m_History[m_HistoryPos].GetPage();
365 a = m_History[m_HistoryPos].GetAnchor();
366 m_HistoryOn = FALSE;
367 if (a == wxEmptyString) LoadPage(l);
368 else LoadPage(l + "#" + a);
369 m_HistoryOn = TRUE;
370 Scroll(0, m_History[m_HistoryPos].GetPos());
371 Refresh();
372 return TRUE;
373 }
374
375
376
377 void wxHtmlWindow::HistoryClear()
378 {
379 m_History.Empty();
380 m_HistoryPos = -1;
381 }
382
383
384
385 wxList wxHtmlWindow::m_Filters;
386 wxHtmlFilter *wxHtmlWindow::m_DefaultFilter = NULL;
387
388 void wxHtmlWindow::CleanUpStatics()
389 {
390 if (m_DefaultFilter) delete m_DefaultFilter;
391 m_DefaultFilter = NULL;
392 }
393
394
395
396 void wxHtmlWindow::AddFilter(wxHtmlFilter *filter)
397 {
398 m_Filters.DeleteContents(TRUE);
399 m_Filters.Append(filter);
400 }
401
402
403
404
405 void wxHtmlWindow::OnLinkClicked(const wxString& link)
406 {
407 LoadPage(link);
408 }
409
410
411
412 void wxHtmlWindow::OnDraw(wxDC& dc)
413 {
414 int x, y;
415 wxRegionIterator upd(GetUpdateRegion()); // get the update rect list
416 int v_y, v_h;
417
418 if (!m_tmpCanDraw) return;
419 dc.SetMapMode(wxMM_TEXT);
420 #if defined(_MSC_VER) && (_MSC_VER == 1200)
421 ::SetMapMode((HDC)dc.GetHDC(), MM_TEXT);
422 #endif
423 dc.SetBackgroundMode(wxTRANSPARENT);
424 ViewStart(&x, &y);
425
426 while (upd) {
427 v_y = upd.GetY();
428 v_h = upd.GetH();
429 if (m_Cell) m_Cell -> Draw(dc, 0, 0, y * HTML_SCROLL_STEP + v_y, y * HTML_SCROLL_STEP + v_h + v_y);
430 upd++;
431 }
432 }
433
434
435
436
437 void wxHtmlWindow::OnSize(wxSizeEvent& event)
438 {
439 wxScrolledWindow::OnSize(event);
440 CreateLayout();
441 }
442
443
444
445 void wxHtmlWindow::OnKeyDown(wxKeyEvent& event)
446 {
447 int dummy;
448 int sty, szy, cliy;
449
450 ViewStart(&dummy, &sty);
451 GetClientSize(&dummy, &cliy); cliy /= HTML_SCROLL_STEP;
452 GetVirtualSize(&dummy, &szy); szy /= HTML_SCROLL_STEP;
453
454 switch (event.KeyCode()) {
455 case WXK_PAGEUP :
456 case WXK_PRIOR :
457 Scroll(-1, sty - cliy);
458 break;
459 case WXK_PAGEDOWN :
460 case WXK_NEXT :
461 Scroll(-1, sty + cliy);
462 break;
463 case WXK_HOME :
464 Scroll(-1, 0);
465 break;
466 case WXK_END :
467 Scroll(-1, szy - cliy);
468 break;
469 case WXK_UP :
470 Scroll(-1, sty - 1);
471 break;
472 case WXK_DOWN :
473 Scroll(-1, sty + 1);
474 break;
475 }
476 }
477
478
479
480 void wxHtmlWindow::OnMouseEvent(wxMouseEvent& event)
481 {
482 m_tmpMouseMoved = TRUE;
483
484 if (event.ButtonDown()) {
485 int sx, sy;
486 wxPoint pos;
487 wxString lnk;
488
489 ViewStart(&sx, &sy); sx *= HTML_SCROLL_STEP; sy *= HTML_SCROLL_STEP;
490 pos = event.GetPosition();
491
492 if (m_Cell)
493 m_Cell -> OnMouseClick(this, sx + pos.x, sy + pos.y, event.ButtonDown(1), event.ButtonDown(2), event.ButtonDown(3));
494 }
495 }
496
497
498
499 void wxHtmlWindow::OnIdle(wxIdleEvent& event)
500 {
501 static wxCursor cur_hand(wxCURSOR_HAND), cur_arrow(wxCURSOR_ARROW);
502
503 if (m_tmpMouseMoved && (m_Cell != NULL)) {
504 int sx, sy;
505 int x, y;
506 wxString lnk;
507
508 ViewStart(&sx, &sy); sx *= HTML_SCROLL_STEP; sy *= HTML_SCROLL_STEP;
509 wxGetMousePosition(&x, &y);
510 ScreenToClient(&x, &y);
511 lnk = m_Cell -> GetLink(sx + x, sy + y);
512
513 if (lnk == wxEmptyString) {
514 SetCursor(cur_arrow);
515 if (m_RelatedStatusBar != -1) m_RelatedFrame -> SetStatusText(wxEmptyString, m_RelatedStatusBar);
516 }
517 else {
518 SetCursor(cur_hand);
519 if (m_RelatedStatusBar != -1) m_RelatedFrame -> SetStatusText(lnk, m_RelatedStatusBar);
520 }
521 m_tmpMouseMoved = FALSE;
522 }
523 }
524
525
526
527
528 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow,wxScrolledWindow)
529
530 BEGIN_EVENT_TABLE(wxHtmlWindow, wxScrolledWindow)
531 EVT_SIZE(wxHtmlWindow::OnSize)
532 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseEvent)
533 EVT_MOTION(wxHtmlWindow::OnMouseEvent)
534 EVT_IDLE(wxHtmlWindow::OnIdle)
535 EVT_KEY_DOWN(wxHtmlWindow::OnKeyDown)
536 END_EVENT_TABLE()
537
538
539
540
541
542 // A module to allow initialization/cleanup
543 // without calling these functions from app.cpp or from
544 // the user's application.
545
546 class wxHtmlWinModule: public wxModule
547 {
548 DECLARE_DYNAMIC_CLASS(wxHtmlWinModule)
549 public:
550 wxHtmlWinModule() : wxModule() {}
551 bool OnInit() { return TRUE; }
552 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
553 };
554
555 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule, wxModule)
556
557
558
559
560 ///// default mod handlers are forced there:
561
562 FORCE_LINK(mod_layout)
563 FORCE_LINK(mod_fonts)
564 FORCE_LINK(mod_image)
565 FORCE_LINK(mod_list)
566 FORCE_LINK(mod_pre)
567 FORCE_LINK(mod_hline)
568 FORCE_LINK(mod_links)
569 FORCE_LINK(mod_tables)
570
571
572 #endif