]> git.saurik.com Git - wxWidgets.git/blob - src/html/htmlwin.cpp
fixed some clipping related bugs in wxHTML priting
[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 = 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 m_Parser -> SetFonts(normal_face, normal_italic_mode, fixed_face, fixed_italic_mode, sizes);
100 if (!m_OpenedPage.IsEmpty()) LoadPage(m_OpenedPage);
101 }
102
103
104
105 bool wxHtmlWindow::SetPage(const wxString& source)
106 {
107 wxClientDC *dc = new wxClientDC(this);
108
109 dc -> SetMapMode(wxMM_TEXT);
110 SetBackgroundColour(wxColour(0xFF, 0xFF, 0xFF));
111 m_OpenedPage = m_OpenedAnchor = wxEmptyString;
112 m_Parser -> SetDC(dc);
113 if (m_Cell) {
114 delete m_Cell;
115 m_Cell = NULL;
116 }
117 m_Cell = (wxHtmlContainerCell*) m_Parser -> Parse(source);
118 delete dc;
119 m_Cell -> SetIndent(m_Borders, wxHTML_INDENT_ALL, wxHTML_UNITS_PIXELS);
120 m_Cell -> SetAlignHor(wxHTML_ALIGN_CENTER);
121 CreateLayout();
122 Refresh();
123 return TRUE;
124 }
125
126
127 bool wxHtmlWindow::LoadPage(const wxString& location)
128 {
129 wxFSFile *f;
130 bool rt_val;
131 wxBusyCursor b;
132
133 m_tmpCanDraw = FALSE;
134 if (m_HistoryOn && (m_HistoryPos != -1)) { // store scroll position into history item
135 int x, y;
136 ViewStart(&x, &y);
137 m_History[m_HistoryPos].SetPos(y);
138 }
139
140 if (location[0] == '#') { // local anchor
141 wxString anch = location.Mid(1) /*1 to end*/;
142 m_tmpCanDraw = TRUE;
143 rt_val = ScrollToAnchor(anch);
144 }
145
146 else {
147 // load&display it:
148 if (m_RelatedStatusBar != -1) {
149 m_RelatedFrame -> SetStatusText(_("Connecting..."), m_RelatedStatusBar);
150 Refresh();
151 }
152
153 f = m_FS -> OpenFile(location);
154 if (f == NULL) {
155 wxString err;
156
157 err.Printf(_("The browser is unable to open requested location :\n\n%s"), WXSTRINGCAST location);
158 m_tmpCanDraw = TRUE;
159 Refresh();
160 wxMessageBox(err, "Error");
161 return FALSE;
162 }
163
164 else {
165 wxNode *node;
166 wxString src = wxEmptyString;
167
168 if (m_RelatedStatusBar != -1) {
169 wxString msg = _("Loading : ") + location;
170 m_RelatedFrame -> SetStatusText(msg, m_RelatedStatusBar);
171 Refresh();
172 }
173
174 node = m_Filters.GetFirst();
175 while (node){
176 wxHtmlFilter *h = (wxHtmlFilter*) node -> GetData();
177 if (h -> CanRead(*f)) {
178 src = h -> ReadFile(*f);
179 break;
180 }
181 node = node -> GetNext();
182 }
183 if (src == wxEmptyString) {
184 if (m_DefaultFilter == NULL) m_DefaultFilter = GetDefaultFilter();
185 src = m_DefaultFilter -> ReadFile(*f);
186 }
187
188 m_FS -> ChangePathTo(f -> GetLocation());
189 rt_val = SetPage(src);
190 m_OpenedPage = f -> GetLocation();
191 if (f -> GetAnchor() != wxEmptyString) {
192 m_tmpCanDraw = TRUE;
193 ScrollToAnchor(f -> GetAnchor());
194 m_tmpCanDraw = FALSE;
195 }
196
197 delete f;
198
199 if (m_RelatedStatusBar != -1) m_RelatedFrame -> SetStatusText(_("Done"), m_RelatedStatusBar);
200 }
201 }
202
203 if (m_HistoryOn) { // add this page to history there:
204 int c = m_History.GetCount() - (m_HistoryPos + 1);
205
206 m_HistoryPos++;
207 for (int i = 0; i < c; i++)
208 m_History.Remove(m_HistoryPos);
209 m_History.Add(new HtmlHistoryItem(m_OpenedPage, m_OpenedAnchor));
210 }
211
212 m_tmpCanDraw = TRUE;
213 Refresh();
214 return rt_val;
215 }
216
217
218
219 bool wxHtmlWindow::ScrollToAnchor(const wxString& anchor)
220 {
221 const wxHtmlCell *c = m_Cell -> Find(wxHTML_COND_ISANCHOR, &anchor);
222 if (!c) return FALSE;
223 else {
224 int y;
225
226 for (y = 0; c != NULL; c = c -> GetParent()) y += c -> GetPosY();
227 Scroll(-1, y / wxHTML_SCROLL_STEP);
228 m_OpenedAnchor = anchor;
229 return TRUE;
230 }
231 }
232
233
234 void wxHtmlWindow::SetTitle(const wxString& title)
235 {
236 if (m_RelatedFrame) {
237 wxString tit;
238 tit.Printf(m_TitleFormat, title.c_str());
239 m_RelatedFrame -> SetTitle(tit);
240 }
241 }
242
243
244
245
246
247 void wxHtmlWindow::CreateLayout()
248 {
249 int ClientWidth, ClientHeight;
250
251 if (!m_Cell) return;
252
253 if (m_Style == wxHW_SCROLLBAR_NEVER) {
254 SetScrollbars(1, 1, 0, 0); // always off
255 GetClientSize(&ClientWidth, &ClientHeight);
256 m_Cell -> Layout(ClientWidth);
257 }
258
259 else {
260 GetClientSize(&ClientWidth, &ClientHeight);
261 #ifndef __WXMSW__
262 // VS : this looks extremely ugly under windoze, better fix needed!
263 SetScrollbars(1, 1, 0, ClientHeight * 2); // always on
264 #endif
265 GetClientSize(&ClientWidth, &ClientHeight);
266 m_Cell -> Layout(ClientWidth);
267 if (ClientHeight < m_Cell -> GetHeight()) {
268 SetScrollbars(wxHTML_SCROLL_STEP, wxHTML_SCROLL_STEP,
269 m_Cell -> GetWidth() / wxHTML_SCROLL_STEP,
270 m_Cell -> GetHeight() / wxHTML_SCROLL_STEP
271 /*cheat: top-level frag is always container*/);
272 }
273 else { /* we fit into window, no need for scrollbars */
274 SetScrollbars(1, 1, 0, 0); // disable...
275 GetClientSize(&ClientWidth, &ClientHeight);
276 m_Cell -> Layout(ClientWidth); // ...and relayout
277 }
278 }
279 }
280
281
282
283 void wxHtmlWindow::ReadCustomization(wxConfigBase *cfg, wxString path)
284 {
285 wxString oldpath;
286 wxString tmp;
287
288 if (path != wxEmptyString) {
289 oldpath = cfg -> GetPath();
290 cfg -> SetPath(path);
291 }
292
293 m_Borders = cfg -> Read("wxHtmlWindow/Borders", m_Borders);
294 m_Parser -> m_FontFaceFixed = cfg -> Read("wxHtmlWindow/FontFaceFixed", m_Parser -> m_FontFaceFixed);
295 m_Parser -> m_FontFaceNormal = cfg -> Read("wxHtmlWindow/FontFaceNormal", m_Parser -> m_FontFaceNormal);
296 m_Parser -> m_ItalicModeFixed = cfg -> Read("wxHtmlWindow/ItalicModeFixed", m_Parser -> m_ItalicModeFixed);
297 m_Parser -> m_ItalicModeNormal = cfg -> Read("wxHtmlWindow/ItalicModeNormal", m_Parser -> m_ItalicModeNormal);
298 for (int i = 0; i < 7; i++) {
299 tmp.Printf(wxT("wxHtmlWindow/FontsSize%i"), i);
300 m_Parser -> m_FontsSizes[i] = cfg -> Read(tmp, m_Parser -> m_FontsSizes[i]);
301 }
302
303 if (path != wxEmptyString)
304 cfg -> SetPath(oldpath);
305 }
306
307
308
309 void wxHtmlWindow::WriteCustomization(wxConfigBase *cfg, wxString path)
310 {
311 wxString oldpath;
312 wxString tmp;
313
314 if (path != wxEmptyString) {
315 oldpath = cfg -> GetPath();
316 cfg -> SetPath(path);
317 }
318
319 cfg -> Write("wxHtmlWindow/Borders", (long) m_Borders);
320 cfg -> Write("wxHtmlWindow/FontFaceFixed", m_Parser -> m_FontFaceFixed);
321 cfg -> Write("wxHtmlWindow/FontFaceNormal", m_Parser -> m_FontFaceNormal);
322 cfg -> Write("wxHtmlWindow/ItalicModeFixed", (long) m_Parser -> m_ItalicModeFixed);
323 cfg -> Write("wxHtmlWindow/ItalicModeNormal", (long) m_Parser -> m_ItalicModeNormal);
324 for (int i = 0; i < 7; i++) {
325 tmp.Printf(wxT("wxHtmlWindow/FontsSize%i"), i);
326 cfg -> Write(tmp, (long) m_Parser -> m_FontsSizes[i]);
327 }
328
329 if (path != wxEmptyString)
330 cfg -> SetPath(oldpath);
331 }
332
333
334
335 bool wxHtmlWindow::HistoryBack()
336 {
337 wxString a, l;
338
339 if (m_HistoryPos < 1) return FALSE;
340
341 m_HistoryPos--;
342
343 l = m_History[m_HistoryPos].GetPage();
344 a = m_History[m_HistoryPos].GetAnchor();
345 m_HistoryOn = FALSE;
346 if (a == wxEmptyString) LoadPage(l);
347 else LoadPage(l + "#" + a);
348 m_HistoryOn = TRUE;
349 Scroll(0, m_History[m_HistoryPos].GetPos());
350 Refresh();
351 return TRUE;
352 }
353
354
355
356 bool wxHtmlWindow::HistoryForward()
357 {
358 wxString a, l;
359
360 if (m_HistoryPos == -1) return FALSE;
361 if (m_HistoryPos >= (int)m_History.GetCount() - 1)return FALSE;
362
363 m_OpenedPage = wxEmptyString; // this will disable adding new entry into history in LoadPage()
364
365 m_HistoryPos++;
366 l = m_History[m_HistoryPos].GetPage();
367 a = m_History[m_HistoryPos].GetAnchor();
368 m_HistoryOn = FALSE;
369 if (a == wxEmptyString) LoadPage(l);
370 else LoadPage(l + "#" + a);
371 m_HistoryOn = TRUE;
372 Scroll(0, m_History[m_HistoryPos].GetPos());
373 Refresh();
374 return TRUE;
375 }
376
377
378
379 void wxHtmlWindow::HistoryClear()
380 {
381 m_History.Empty();
382 m_HistoryPos = -1;
383 }
384
385
386
387 wxList wxHtmlWindow::m_Filters;
388 wxHtmlFilter *wxHtmlWindow::m_DefaultFilter = NULL;
389
390 void wxHtmlWindow::CleanUpStatics()
391 {
392 if (m_DefaultFilter) delete m_DefaultFilter;
393 m_DefaultFilter = NULL;
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 * wxHTML_SCROLL_STEP + v_y, y * wxHTML_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 /= wxHTML_SCROLL_STEP;
456 GetVirtualSize(&dummy, &szy); szy /= wxHTML_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 *= wxHTML_SCROLL_STEP; sy *= wxHTML_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 *= wxHTML_SCROLL_STEP; sy *= wxHTML_SCROLL_STEP;
513 wxGetMousePosition(&x, &y);
514 ScreenToClient(&x, &y);
515 lnk = m_Cell -> GetLink(sx + x, sy + y);
516
517 if (lnk != m_tmpLastLink) {
518 if (lnk == wxEmptyString) {
519 SetCursor(cur_arrow);
520 if (m_RelatedStatusBar != -1) m_RelatedFrame -> SetStatusText(wxEmptyString, m_RelatedStatusBar);
521 }
522 else {
523 SetCursor(cur_hand);
524 if (m_RelatedStatusBar != -1) m_RelatedFrame -> SetStatusText(lnk, m_RelatedStatusBar);
525 }
526 m_tmpLastLink = lnk;
527 }
528 m_tmpMouseMoved = FALSE;
529 }
530 }
531
532
533
534
535 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow,wxScrolledWindow)
536
537 BEGIN_EVENT_TABLE(wxHtmlWindow, wxScrolledWindow)
538 EVT_SIZE(wxHtmlWindow::OnSize)
539 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseEvent)
540 EVT_MOTION(wxHtmlWindow::OnMouseEvent)
541 EVT_IDLE(wxHtmlWindow::OnIdle)
542 EVT_KEY_DOWN(wxHtmlWindow::OnKeyDown)
543 END_EVENT_TABLE()
544
545
546
547
548
549 // A module to allow initialization/cleanup
550 // without calling these functions from app.cpp or from
551 // the user's application.
552
553 class wxHtmlWinModule: public wxModule
554 {
555 DECLARE_DYNAMIC_CLASS(wxHtmlWinModule)
556 public:
557 wxHtmlWinModule() : wxModule() {}
558 bool OnInit() { return TRUE; }
559 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
560 };
561
562 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule, wxModule)
563
564
565
566
567 ///// default mod handlers are forced there:
568
569 FORCE_LINK(mod_layout)
570 FORCE_LINK(mod_fonts)
571 FORCE_LINK(mod_image)
572 FORCE_LINK(mod_list)
573 FORCE_LINK(mod_pre)
574 FORCE_LINK(mod_hline)
575 FORCE_LINK(mod_links)
576 FORCE_LINK(mod_tables)
577
578
579 #endif