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