]> git.saurik.com Git - wxWidgets.git/blob - src/html/htmlwin.cpp
380ed40818b11f57637b675b0480d4aebc22a258
[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 #include "wx/html/forcelnk.h"
30
31
32
33 //-----------------------------------------------------------------------------
34 // wxHtmlWindow
35 //-----------------------------------------------------------------------------
36
37
38
39 #include "wx/arrimpl.cpp"
40 WX_DEFINE_OBJARRAY(HtmlHistoryArray)
41
42
43 wxHtmlWindow::wxHtmlWindow(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
44 long style, const wxString& name) : wxScrolledWindow(parent, id, pos, size, wxVSCROLL, name)
45 {
46 m_tmpMouseMoved = FALSE;
47 m_tmpLastLink = NULL;
48 m_tmpCanDrawLocks = 0;
49 m_FS = new wxFileSystem();
50 m_RelatedStatusBar = -1;
51 m_RelatedFrame = NULL;
52 m_TitleFormat = "%s";
53 m_OpenedPage = m_OpenedAnchor = m_OpenedPageTitle = 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, wxString fixed_face, const int *sizes)
97 {
98 wxString op = m_OpenedPage;
99
100 m_Parser -> SetFonts(normal_face, fixed_face, sizes);
101 SetPage(wxT("<html><body></body></html>")); // fonts changed => contents invalid
102 if (!op.IsEmpty()) LoadPage(op);
103 }
104
105
106
107 bool wxHtmlWindow::SetPage(const wxString& source)
108 {
109 wxClientDC *dc = new wxClientDC(this);
110
111 dc -> SetMapMode(wxMM_TEXT);
112 SetBackgroundColour(wxColour(0xFF, 0xFF, 0xFF));
113 m_OpenedPage = m_OpenedAnchor = m_OpenedPageTitle = wxEmptyString;
114 m_Parser -> SetDC(dc);
115 if (m_Cell) {
116 delete m_Cell;
117 m_Cell = NULL;
118 }
119 m_Cell = (wxHtmlContainerCell*) m_Parser -> Parse(source);
120 delete dc;
121 m_Cell -> SetIndent(m_Borders, wxHTML_INDENT_ALL, wxHTML_UNITS_PIXELS);
122 m_Cell -> SetAlignHor(wxHTML_ALIGN_CENTER);
123 CreateLayout();
124 if (m_tmpCanDrawLocks == 0) Refresh();
125 return TRUE;
126 }
127
128
129 bool wxHtmlWindow::LoadPage(const wxString& location)
130 {
131 wxFSFile *f;
132 bool rt_val;
133
134 SetCursor(*wxHOURGLASS_CURSOR);
135 wxYield();
136
137 m_tmpCanDrawLocks++;
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_tmpCanDrawLocks--;
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);
162 m_tmpCanDrawLocks--;
163 Refresh();
164 wxMessageBox(err, "Error");
165
166 SetCursor(*wxSTANDARD_CURSOR);
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 }
189 if (src == wxEmptyString) {
190 if (m_DefaultFilter == NULL) m_DefaultFilter = GetDefaultFilter();
191 src = m_DefaultFilter -> ReadFile(*f);
192 }
193
194 m_FS -> ChangePathTo(f -> GetLocation());
195 rt_val = SetPage(src);
196 m_OpenedPage = f -> GetLocation();
197 if (f -> GetAnchor() != wxEmptyString) {
198 // m_tmpCanDrawLocks--;
199 ScrollToAnchor(f -> GetAnchor());
200 // m_tmpCanDrawLocks++;
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
218 SetCursor(*wxSTANDARD_CURSOR);
219
220 wxYield();
221 m_tmpCanDrawLocks--;
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(wxHTML_SCROLL_STEP, 1, m_Cell -> GetWidth() / wxHTML_SCROLL_STEP, 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(wxHTML_SCROLL_STEP, 1, m_Cell -> GetWidth() / wxHTML_SCROLL_STEP, 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(wxHTML_SCROLL_STEP, 1, m_Cell -> GetWidth() / wxHTML_SCROLL_STEP, 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
300 if (path != wxEmptyString) {
301 oldpath = cfg -> GetPath();
302 cfg -> SetPath(path);
303 }
304
305 m_Borders = cfg -> Read("wxHtmlWindow/Borders", m_Borders);
306 p_fff = cfg -> Read("wxHtmlWindow/FontFaceFixed", m_Parser -> m_FontFaceFixed);
307 p_ffn = cfg -> Read("wxHtmlWindow/FontFaceNormal", m_Parser -> m_FontFaceNormal);
308 for (int i = 0; i < 7; i++) {
309 tmp.Printf(wxT("wxHtmlWindow/FontsSize%i"), i);
310 p_fontsizes[i] = cfg -> Read(tmp, m_Parser -> m_FontsSizes[i]);
311 }
312 SetFonts(p_ffn, p_fff, p_fontsizes);
313
314 if (path != wxEmptyString)
315 cfg -> SetPath(oldpath);
316 }
317
318
319
320 void wxHtmlWindow::WriteCustomization(wxConfigBase *cfg, wxString path)
321 {
322 wxString oldpath;
323 wxString tmp;
324
325 if (path != wxEmptyString) {
326 oldpath = cfg -> GetPath();
327 cfg -> SetPath(path);
328 }
329
330 cfg -> Write("wxHtmlWindow/Borders", (long) m_Borders);
331 cfg -> Write("wxHtmlWindow/FontFaceFixed", m_Parser -> m_FontFaceFixed);
332 cfg -> Write("wxHtmlWindow/FontFaceNormal", m_Parser -> m_FontFaceNormal);
333 for (int i = 0; i < 7; i++) {
334 tmp.Printf(wxT("wxHtmlWindow/FontsSize%i"), i);
335 cfg -> Write(tmp, (long) m_Parser -> m_FontsSizes[i]);
336 }
337
338 if (path != wxEmptyString)
339 cfg -> SetPath(oldpath);
340 }
341
342
343
344 bool wxHtmlWindow::HistoryBack()
345 {
346 wxString a, l;
347
348 if (m_HistoryPos < 1) return FALSE;
349
350 m_HistoryPos--;
351
352 l = m_History[m_HistoryPos].GetPage();
353 a = m_History[m_HistoryPos].GetAnchor();
354 m_HistoryOn = FALSE;
355 m_tmpCanDrawLocks++;
356 if (a == wxEmptyString) LoadPage(l);
357 else LoadPage(l + "#" + a);
358 m_HistoryOn = TRUE;
359 wxYield();
360 m_tmpCanDrawLocks--;
361 Scroll(0, m_History[m_HistoryPos].GetPos());
362 Refresh();
363 return TRUE;
364 }
365
366
367
368 bool wxHtmlWindow::HistoryForward()
369 {
370 wxString a, l;
371
372 if (m_HistoryPos == -1) return FALSE;
373 if (m_HistoryPos >= (int)m_History.GetCount() - 1)return FALSE;
374
375 m_OpenedPage = wxEmptyString; // this will disable adding new entry into history in LoadPage()
376
377 m_HistoryPos++;
378 l = m_History[m_HistoryPos].GetPage();
379 a = m_History[m_HistoryPos].GetAnchor();
380 m_HistoryOn = FALSE;
381 m_tmpCanDrawLocks++;
382 if (a == wxEmptyString) LoadPage(l);
383 else LoadPage(l + "#" + a);
384 m_HistoryOn = TRUE;
385 wxYield();
386 m_tmpCanDrawLocks--;
387 Scroll(0, m_History[m_HistoryPos].GetPos());
388 Refresh();
389 return TRUE;
390 }
391
392
393
394 void wxHtmlWindow::HistoryClear()
395 {
396 m_History.Empty();
397 m_HistoryPos = -1;
398 }
399
400
401
402 wxList wxHtmlWindow::m_Filters;
403 wxHtmlFilter *wxHtmlWindow::m_DefaultFilter = NULL;
404
405 void wxHtmlWindow::CleanUpStatics()
406 {
407 if (m_DefaultFilter) delete m_DefaultFilter;
408 m_DefaultFilter = NULL;
409 m_Filters.DeleteContents(TRUE);
410 m_Filters.Clear();
411
412 }
413
414
415
416 void wxHtmlWindow::AddFilter(wxHtmlFilter *filter)
417 {
418 m_Filters.Append(filter);
419 }
420
421
422
423
424 void wxHtmlWindow::OnLinkClicked(wxHtmlLinkInfo *link)
425 {
426 LoadPage(link -> GetHref());
427 }
428
429
430
431 void wxHtmlWindow::OnDraw(wxDC& dc)
432 {
433 int x, y;
434 wxRegionIterator upd(GetUpdateRegion()); // get the update rect list
435 int v_y, v_h;
436
437 if (m_tmpCanDrawLocks > 0) return;
438 dc.SetMapMode(wxMM_TEXT);
439 #if defined(_MSC_VER) && (_MSC_VER == 1200)
440 ::SetMapMode((HDC)dc.GetHDC(), MM_TEXT);
441 #endif
442 dc.SetBackgroundMode(wxTRANSPARENT);
443 ViewStart(&x, &y);
444
445 while (upd) {
446 v_y = upd.GetY();
447 v_h = upd.GetH();
448 if (m_Cell) m_Cell -> Draw(dc, 0, 0, y * wxHTML_SCROLL_STEP + v_y, y * wxHTML_SCROLL_STEP + v_h + v_y);
449 upd++;
450 }
451 }
452
453
454
455
456 void wxHtmlWindow::OnSize(wxSizeEvent& event)
457 {
458 wxScrolledWindow::OnSize(event);
459 CreateLayout();
460 }
461
462
463
464 void wxHtmlWindow::OnKeyDown(wxKeyEvent& event)
465 {
466 int dummy;
467 int sty, szy, cliy;
468
469 ViewStart(&dummy, &sty);
470 GetClientSize(&dummy, &cliy); cliy /= wxHTML_SCROLL_STEP;
471 GetVirtualSize(&dummy, &szy); szy /= wxHTML_SCROLL_STEP;
472
473 switch (event.KeyCode()) {
474 case WXK_PAGEUP :
475 case WXK_PRIOR :
476 Scroll(-1, sty - cliy);
477 break;
478 case WXK_PAGEDOWN :
479 case WXK_NEXT :
480 Scroll(-1, sty + cliy);
481 break;
482 case WXK_HOME :
483 Scroll(-1, 0);
484 break;
485 case WXK_END :
486 Scroll(-1, szy - cliy);
487 break;
488 case WXK_UP :
489 Scroll(-1, sty - 1);
490 break;
491 case WXK_DOWN :
492 Scroll(-1, sty + 1);
493 break;
494 }
495 }
496
497
498
499 void wxHtmlWindow::OnMouseEvent(wxMouseEvent& event)
500 {
501 m_tmpMouseMoved = TRUE;
502
503 if (event.ButtonDown()) {
504 int sx, sy;
505 wxPoint pos;
506 wxString lnk;
507
508 ViewStart(&sx, &sy); sx *= wxHTML_SCROLL_STEP; sy *= wxHTML_SCROLL_STEP;
509 pos = event.GetPosition();
510
511 if (m_Cell)
512 m_Cell -> OnMouseClick(this, sx + pos.x, sy + pos.y, event.ButtonDown(1), event.ButtonDown(2), event.ButtonDown(3));
513 }
514 }
515
516
517
518 void wxHtmlWindow::OnIdle(wxIdleEvent& event)
519 {
520 static wxCursor cur_hand(wxCURSOR_HAND), cur_arrow(wxCURSOR_ARROW);
521
522 if (m_tmpMouseMoved && (m_Cell != NULL)) {
523 int sx, sy;
524 int x, y;
525 wxHtmlLinkInfo *lnk;
526
527 ViewStart(&sx, &sy); sx *= wxHTML_SCROLL_STEP; sy *= wxHTML_SCROLL_STEP;
528 wxGetMousePosition(&x, &y);
529 ScreenToClient(&x, &y);
530 lnk = m_Cell -> GetLink(sx + x, sy + y);
531
532 if (lnk != m_tmpLastLink) {
533 if (lnk == NULL) {
534 SetCursor(cur_arrow);
535 if (m_RelatedStatusBar != -1) m_RelatedFrame -> SetStatusText(wxEmptyString, m_RelatedStatusBar);
536 }
537 else {
538 SetCursor(cur_hand);
539 if (m_RelatedStatusBar != -1)
540 m_RelatedFrame -> SetStatusText(lnk -> GetHref(), m_RelatedStatusBar);
541 }
542 m_tmpLastLink = lnk;
543 }
544 m_tmpMouseMoved = FALSE;
545 }
546 }
547
548
549
550
551 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow,wxScrolledWindow)
552
553 BEGIN_EVENT_TABLE(wxHtmlWindow, wxScrolledWindow)
554 EVT_SIZE(wxHtmlWindow::OnSize)
555 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseEvent)
556 EVT_MOTION(wxHtmlWindow::OnMouseEvent)
557 EVT_IDLE(wxHtmlWindow::OnIdle)
558 EVT_KEY_DOWN(wxHtmlWindow::OnKeyDown)
559 END_EVENT_TABLE()
560
561
562
563
564
565 // A module to allow initialization/cleanup
566 // without calling these functions from app.cpp or from
567 // the user's application.
568
569 class wxHtmlWinModule: public wxModule
570 {
571 DECLARE_DYNAMIC_CLASS(wxHtmlWinModule)
572 public:
573 wxHtmlWinModule() : wxModule() {}
574 bool OnInit() { return TRUE; }
575 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
576 };
577
578 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule, wxModule)
579
580
581
582
583 ///// default mod handlers are forced there:
584
585 FORCE_LINK(m_layout)
586 FORCE_LINK(m_fonts)
587 FORCE_LINK(m_image)
588 FORCE_LINK(m_list)
589 FORCE_LINK(m_dflist)
590 FORCE_LINK(m_pre)
591 FORCE_LINK(m_hline)
592 FORCE_LINK(m_links)
593 FORCE_LINK(m_tables)
594
595
596 #endif