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