]> git.saurik.com Git - wxWidgets.git/blob - src/html/htmlwin.cpp
SetTitle method (internal use only) changed to virtual OnSetTitle, added GetOpenedPag...
[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_tmpLastLink = wxEmptyString;
49 m_tmpCanDraw = TRUE;
50 m_FS = new wxFileSystem();
51 m_RelatedStatusBar = -1;
52 m_RelatedFrame = NULL;
53 m_TitleFormat = "%s";
54 m_OpenedPage = m_OpenedAnchor = m_OpenedPageTitle = wxEmptyString;
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;
61 m_Style = style;
62 SetPage("<html><body></body></html>");
63 }
64
65
66
67 wxHtmlWindow::~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
82 void wxHtmlWindow::SetRelatedFrame(wxFrame* frame, const wxString& format)
83 {
84 m_RelatedFrame = frame;
85 m_TitleFormat = format;
86 }
87
88
89
90 void wxHtmlWindow::SetRelatedStatusBar(int bar)
91 {
92 m_RelatedStatusBar = bar;
93 }
94
95
96
97 void wxHtmlWindow::SetFonts(wxString normal_face, int normal_italic_mode, wxString fixed_face, int fixed_italic_mode, const int *sizes)
98 {
99 wxString op = m_OpenedPage;
100
101 m_Parser -> SetFonts(normal_face, normal_italic_mode, fixed_face, fixed_italic_mode, sizes);
102 SetPage(wxT("")); // fonts changed => contents invalid
103 if (!op.IsEmpty()) LoadPage(op);
104 }
105
106
107
108 bool wxHtmlWindow::SetPage(const wxString& source)
109 {
110 wxClientDC *dc = new wxClientDC(this);
111
112 dc -> SetMapMode(wxMM_TEXT);
113 SetBackgroundColour(wxColour(0xFF, 0xFF, 0xFF));
114 m_OpenedPage = m_OpenedAnchor = m_OpenedPageTitle = wxEmptyString;
115 m_Parser -> SetDC(dc);
116 if (m_Cell) {
117 delete m_Cell;
118 m_Cell = NULL;
119 }
120 m_Cell = (wxHtmlContainerCell*) m_Parser -> Parse(source);
121 delete dc;
122 m_Cell -> SetIndent(m_Borders, wxHTML_INDENT_ALL, wxHTML_UNITS_PIXELS);
123 m_Cell -> SetAlignHor(wxHTML_ALIGN_CENTER);
124 CreateLayout();
125 Refresh();
126 return TRUE;
127 }
128
129
130 bool wxHtmlWindow::LoadPage(const wxString& location)
131 {
132 wxFSFile *f;
133 bool rt_val;
134
135 SetCursor(*wxHOURGLASS_CURSOR);
136 wxYield();
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);
163 m_tmpCanDraw = TRUE;
164 Refresh();
165 wxMessageBox(err, "Error");
166
167 SetCursor(*wxSTANDARD_CURSOR);
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 }
190 if (src == wxEmptyString) {
191 if (m_DefaultFilter == NULL) m_DefaultFilter = GetDefaultFilter();
192 src = m_DefaultFilter -> ReadFile(*f);
193 }
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
219 SetCursor(*wxSTANDARD_CURSOR);
220
221 m_tmpCanDraw = TRUE;
222 Refresh();
223 return rt_val;
224 }
225
226
227
228 bool wxHtmlWindow::ScrollToAnchor(const wxString& anchor)
229 {
230 const wxHtmlCell *c = m_Cell -> Find(wxHTML_COND_ISANCHOR, &anchor);
231 if (!c) return FALSE;
232 else {
233 int y;
234
235 for (y = 0; c != NULL; c = c -> GetParent()) y += c -> GetPosY();
236 Scroll(-1, y / wxHTML_SCROLL_STEP);
237 m_OpenedAnchor = anchor;
238 return TRUE;
239 }
240 }
241
242
243 void wxHtmlWindow::OnSetTitle(const wxString& title)
244 {
245 if (m_RelatedFrame) {
246 wxString tit;
247 tit.Printf(m_TitleFormat, title.c_str());
248 m_RelatedFrame -> SetTitle(tit);
249 }
250 m_OpenedPageTitle = title;
251 }
252
253
254
255
256
257 void wxHtmlWindow::CreateLayout()
258 {
259 int ClientWidth, ClientHeight;
260
261 if (!m_Cell) return;
262
263 if (m_Style == wxHW_SCROLLBAR_NEVER) {
264 SetScrollbars(1, 1, 0, 0); // always off
265 GetClientSize(&ClientWidth, &ClientHeight);
266 m_Cell -> Layout(ClientWidth);
267 }
268
269 else {
270 GetClientSize(&ClientWidth, &ClientHeight);
271 #ifndef __WXMSW__
272 // VS : this looks extremely ugly under windoze, better fix needed!
273 SetScrollbars(1, 1, 0, ClientHeight * 2); // always on
274 #endif
275 GetClientSize(&ClientWidth, &ClientHeight);
276 m_Cell -> Layout(ClientWidth);
277 if (ClientHeight < m_Cell -> GetHeight()) {
278 SetScrollbars(wxHTML_SCROLL_STEP, wxHTML_SCROLL_STEP,
279 m_Cell -> GetWidth() / wxHTML_SCROLL_STEP,
280 m_Cell -> GetHeight() / wxHTML_SCROLL_STEP
281 /*cheat: top-level frag is always container*/);
282 }
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 }
288 }
289 }
290
291
292
293 void wxHtmlWindow::ReadCustomization(wxConfigBase *cfg, wxString path)
294 {
295 wxString oldpath;
296 wxString tmp;
297 int p_fontsizes[7];
298 wxString p_fff, p_ffn;
299 int p_imf, p_imn;
300
301 if (path != wxEmptyString) {
302 oldpath = cfg -> GetPath();
303 cfg -> SetPath(path);
304 }
305
306 m_Borders = cfg -> Read("wxHtmlWindow/Borders", m_Borders);
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);
311 for (int i = 0; i < 7; i++) {
312 tmp.Printf(wxT("wxHtmlWindow/FontsSize%i"), i);
313 p_fontsizes[i] = cfg -> Read(tmp, m_Parser -> m_FontsSizes[i]);
314 }
315 m_Parser -> SetFonts(p_ffn, p_imn, p_fff, p_imf, p_fontsizes);
316
317 if (path != wxEmptyString)
318 cfg -> SetPath(oldpath);
319 }
320
321
322
323 void 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++) {
339 tmp.Printf(wxT("wxHtmlWindow/FontsSize%i"), i);
340 cfg -> Write(tmp, (long) m_Parser -> m_FontsSizes[i]);
341 }
342
343 if (path != wxEmptyString)
344 cfg -> SetPath(oldpath);
345 }
346
347
348
349 bool 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
370 bool 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
393 void wxHtmlWindow::HistoryClear()
394 {
395 m_History.Empty();
396 m_HistoryPos = -1;
397 }
398
399
400
401 wxList wxHtmlWindow::m_Filters;
402 wxHtmlFilter *wxHtmlWindow::m_DefaultFilter = NULL;
403
404 void wxHtmlWindow::CleanUpStatics()
405 {
406 if (m_DefaultFilter) delete m_DefaultFilter;
407 m_DefaultFilter = NULL;
408 m_Filters.DeleteContents(TRUE);
409 m_Filters.Clear();
410
411 }
412
413
414
415 void wxHtmlWindow::AddFilter(wxHtmlFilter *filter)
416 {
417 m_Filters.Append(filter);
418 }
419
420
421
422
423 void wxHtmlWindow::OnLinkClicked(const wxString& link)
424 {
425 LoadPage(link);
426 }
427
428
429
430 void 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();
447 if (m_Cell) m_Cell -> Draw(dc, 0, 0, y * wxHTML_SCROLL_STEP + v_y, y * wxHTML_SCROLL_STEP + v_h + v_y);
448 upd++;
449 }
450 }
451
452
453
454
455 void wxHtmlWindow::OnSize(wxSizeEvent& event)
456 {
457 wxScrolledWindow::OnSize(event);
458 CreateLayout();
459 }
460
461
462
463 void wxHtmlWindow::OnKeyDown(wxKeyEvent& event)
464 {
465 int dummy;
466 int sty, szy, cliy;
467
468 ViewStart(&dummy, &sty);
469 GetClientSize(&dummy, &cliy); cliy /= wxHTML_SCROLL_STEP;
470 GetVirtualSize(&dummy, &szy); szy /= wxHTML_SCROLL_STEP;
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
498 void 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
507 ViewStart(&sx, &sy); sx *= wxHTML_SCROLL_STEP; sy *= wxHTML_SCROLL_STEP;
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
517 void 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
526 ViewStart(&sx, &sy); sx *= wxHTML_SCROLL_STEP; sy *= wxHTML_SCROLL_STEP;
527 wxGetMousePosition(&x, &y);
528 ScreenToClient(&x, &y);
529 lnk = m_Cell -> GetLink(sx + x, sy + y);
530
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;
541 }
542 m_tmpMouseMoved = FALSE;
543 }
544 }
545
546
547
548
549 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow,wxScrolledWindow)
550
551 BEGIN_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)
557 END_EVENT_TABLE()
558
559
560
561
562
563 // A module to allow initialization/cleanup
564 // without calling these functions from app.cpp or from
565 // the user's application.
566
567 class wxHtmlWinModule: public wxModule
568 {
569 DECLARE_DYNAMIC_CLASS(wxHtmlWinModule)
570 public:
571 wxHtmlWinModule() : wxModule() {}
572 bool OnInit() { return TRUE; }
573 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
574 };
575
576 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule, wxModule)
577
578
579
580
581 ///// default mod handlers are forced there:
582
583 FORCE_LINK(mod_layout)
584 FORCE_LINK(mod_fonts)
585 FORCE_LINK(mod_image)
586 FORCE_LINK(mod_list)
587 FORCE_LINK(mod_pre)
588 FORCE_LINK(mod_hline)
589 FORCE_LINK(mod_links)
590 FORCE_LINK(mod_tables)
591
592
593 #endif