]> git.saurik.com Git - wxWidgets.git/blame - src/html/htmlwin.cpp
Took out somebody's hard coded path (j:\dev\something...) and put back
[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;
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
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 115 if (m_Cell) {
89de9af3
VS
116 delete m_Cell;
117 m_Cell = NULL;
f61815af 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 123 CreateLayout();
89de9af3 124 if (m_tmpCanDrawLocks == 0) Refresh();
5526e819
VS
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 136
89de9af3 137 m_tmpCanDrawLocks++;
5526e819
VS
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*/;
89de9af3 146 m_tmpCanDrawLocks--;
5526e819
VS
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);
89de9af3 162 m_tmpCanDrawLocks--;
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 189 if (src == wxEmptyString) {
89de9af3
VS
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) {
89de9af3 198// m_tmpCanDrawLocks--;
5526e819 199 ScrollToAnchor(f -> GetAnchor());
89de9af3 200// m_tmpCanDrawLocks++;
5526e819
VS
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
89de9af3
VS
220 wxYield();
221 m_tmpCanDrawLocks--;
5526e819
VS
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);
89de9af3 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 }
89de9af3
VS
283 else { /* we fit into window, no need for scrollbars */
284 SetScrollbars(1, 1, 0, 0); // disable...
285 GetClientSize(&ClientWidth, &ClientHeight);
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 }
fb5700fe 315 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;
89de9af3 360 m_tmpCanDrawLocks++;
5526e819
VS
361 if (a == wxEmptyString) LoadPage(l);
362 else LoadPage(l + "#" + a);
363 m_HistoryOn = TRUE;
364 Scroll(0, m_History[m_HistoryPos].GetPos());
89de9af3
VS
365// wxYield();
366 m_tmpCanDrawLocks--;
5526e819
VS
367 Refresh();
368 return TRUE;
369}
370
371
372
373bool wxHtmlWindow::HistoryForward()
374{
375 wxString a, l;
376
377 if (m_HistoryPos == -1) return FALSE;
378 if (m_HistoryPos >= (int)m_History.GetCount() - 1)return FALSE;
379
380 m_OpenedPage = wxEmptyString; // this will disable adding new entry into history in LoadPage()
381
382 m_HistoryPos++;
383 l = m_History[m_HistoryPos].GetPage();
384 a = m_History[m_HistoryPos].GetAnchor();
385 m_HistoryOn = FALSE;
89de9af3 386 m_tmpCanDrawLocks++;
5526e819
VS
387 if (a == wxEmptyString) LoadPage(l);
388 else LoadPage(l + "#" + a);
389 m_HistoryOn = TRUE;
390 Scroll(0, m_History[m_HistoryPos].GetPos());
89de9af3
VS
391// wxYield();
392 m_tmpCanDrawLocks--;
5526e819
VS
393 Refresh();
394 return TRUE;
395}
396
397
398
399void wxHtmlWindow::HistoryClear()
400{
401 m_History.Empty();
402 m_HistoryPos = -1;
403}
404
405
406
407wxList wxHtmlWindow::m_Filters;
a76015e6
VS
408wxHtmlFilter *wxHtmlWindow::m_DefaultFilter = NULL;
409
410void wxHtmlWindow::CleanUpStatics()
411{
412 if (m_DefaultFilter) delete m_DefaultFilter;
413 m_DefaultFilter = NULL;
269e8200
RD
414 m_Filters.DeleteContents(TRUE);
415 m_Filters.Clear();
416
a76015e6
VS
417}
418
419
5526e819
VS
420
421void wxHtmlWindow::AddFilter(wxHtmlFilter *filter)
422{
5526e819
VS
423 m_Filters.Append(filter);
424}
425
426
427
428
429void wxHtmlWindow::OnLinkClicked(const wxString& link)
430{
431 LoadPage(link);
432}
433
434
435
436void wxHtmlWindow::OnDraw(wxDC& dc)
437{
438 int x, y;
439 wxRegionIterator upd(GetUpdateRegion()); // get the update rect list
440 int v_y, v_h;
441
89de9af3 442 if (m_tmpCanDrawLocks > 0) return;
5526e819
VS
443 dc.SetMapMode(wxMM_TEXT);
444#if defined(_MSC_VER) && (_MSC_VER == 1200)
445 ::SetMapMode((HDC)dc.GetHDC(), MM_TEXT);
446#endif
447 dc.SetBackgroundMode(wxTRANSPARENT);
448 ViewStart(&x, &y);
449
450 while (upd) {
451 v_y = upd.GetY();
452 v_h = upd.GetH();
efba2b89 453 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
454 upd++;
455 }
456}
457
458
459
460
461void wxHtmlWindow::OnSize(wxSizeEvent& event)
462{
463 wxScrolledWindow::OnSize(event);
464 CreateLayout();
465}
466
467
468
469void wxHtmlWindow::OnKeyDown(wxKeyEvent& event)
470{
471 int dummy;
472 int sty, szy, cliy;
473
474 ViewStart(&dummy, &sty);
efba2b89
VS
475 GetClientSize(&dummy, &cliy); cliy /= wxHTML_SCROLL_STEP;
476 GetVirtualSize(&dummy, &szy); szy /= wxHTML_SCROLL_STEP;
5526e819
VS
477
478 switch (event.KeyCode()) {
479 case WXK_PAGEUP :
480 case WXK_PRIOR :
481 Scroll(-1, sty - cliy);
482 break;
483 case WXK_PAGEDOWN :
484 case WXK_NEXT :
485 Scroll(-1, sty + cliy);
486 break;
487 case WXK_HOME :
488 Scroll(-1, 0);
489 break;
490 case WXK_END :
491 Scroll(-1, szy - cliy);
492 break;
493 case WXK_UP :
494 Scroll(-1, sty - 1);
495 break;
496 case WXK_DOWN :
497 Scroll(-1, sty + 1);
498 break;
499 }
500}
501
502
503
504void wxHtmlWindow::OnMouseEvent(wxMouseEvent& event)
505{
506 m_tmpMouseMoved = TRUE;
507
508 if (event.ButtonDown()) {
509 int sx, sy;
510 wxPoint pos;
511 wxString lnk;
512
efba2b89 513 ViewStart(&sx, &sy); sx *= wxHTML_SCROLL_STEP; sy *= wxHTML_SCROLL_STEP;
5526e819
VS
514 pos = event.GetPosition();
515
516 if (m_Cell)
517 m_Cell -> OnMouseClick(this, sx + pos.x, sy + pos.y, event.ButtonDown(1), event.ButtonDown(2), event.ButtonDown(3));
518 }
519}
520
521
522
523void wxHtmlWindow::OnIdle(wxIdleEvent& event)
524{
525 static wxCursor cur_hand(wxCURSOR_HAND), cur_arrow(wxCURSOR_ARROW);
526
527 if (m_tmpMouseMoved && (m_Cell != NULL)) {
528 int sx, sy;
529 int x, y;
530 wxString lnk;
531
efba2b89 532 ViewStart(&sx, &sy); sx *= wxHTML_SCROLL_STEP; sy *= wxHTML_SCROLL_STEP;
5526e819
VS
533 wxGetMousePosition(&x, &y);
534 ScreenToClient(&x, &y);
535 lnk = m_Cell -> GetLink(sx + x, sy + y);
536
622ea783
VS
537 if (lnk != m_tmpLastLink) {
538 if (lnk == wxEmptyString) {
539 SetCursor(cur_arrow);
540 if (m_RelatedStatusBar != -1) m_RelatedFrame -> SetStatusText(wxEmptyString, m_RelatedStatusBar);
541 }
542 else {
543 SetCursor(cur_hand);
544 if (m_RelatedStatusBar != -1) m_RelatedFrame -> SetStatusText(lnk, m_RelatedStatusBar);
545 }
546 m_tmpLastLink = lnk;
5526e819
VS
547 }
548 m_tmpMouseMoved = FALSE;
549 }
550}
551
552
553
554
555IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow,wxScrolledWindow)
556
557BEGIN_EVENT_TABLE(wxHtmlWindow, wxScrolledWindow)
558 EVT_SIZE(wxHtmlWindow::OnSize)
559 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseEvent)
560 EVT_MOTION(wxHtmlWindow::OnMouseEvent)
561 EVT_IDLE(wxHtmlWindow::OnIdle)
562 EVT_KEY_DOWN(wxHtmlWindow::OnKeyDown)
563END_EVENT_TABLE()
564
565
566
567
568
a76015e6
VS
569// A module to allow initialization/cleanup
570// without calling these functions from app.cpp or from
571// the user's application.
572
573class wxHtmlWinModule: public wxModule
574{
575DECLARE_DYNAMIC_CLASS(wxHtmlWinModule)
576public:
577 wxHtmlWinModule() : wxModule() {}
578 bool OnInit() { return TRUE; }
579 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
580};
581
582IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule, wxModule)
583
5526e819
VS
584
585
586
587///// default mod handlers are forced there:
588
589FORCE_LINK(mod_layout)
590FORCE_LINK(mod_fonts)
591FORCE_LINK(mod_image)
592FORCE_LINK(mod_list)
593FORCE_LINK(mod_pre)
594FORCE_LINK(mod_hline)
595FORCE_LINK(mod_links)
596FORCE_LINK(mod_tables)
597
598
483ff5a5 599#endif