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