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