]> git.saurik.com Git - wxWidgets.git/blob - src/html/htmlwin.cpp
11086c4fe6765540c270ee0261091e52da76fb66
[wxWidgets.git] / src / html / htmlwin.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/html/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 #include "wx/wxprec.h"
11
12 #ifdef __BORLANDC__
13 #pragma hdrstop
14 #endif
15
16 #if wxUSE_HTML && wxUSE_STREAMS
17
18 #ifndef WX_PRECOMP
19 #include "wx/list.h"
20 #include "wx/log.h"
21 #include "wx/intl.h"
22 #include "wx/dcclient.h"
23 #include "wx/frame.h"
24 #include "wx/dcmemory.h"
25 #include "wx/timer.h"
26 #include "wx/settings.h"
27 #include "wx/dataobj.h"
28 #include "wx/statusbr.h"
29 #endif
30
31 #include "wx/html/htmlwin.h"
32 #include "wx/html/htmlproc.h"
33 #include "wx/clipbrd.h"
34 #include "wx/recguard.h"
35
36 #include "wx/arrimpl.cpp"
37 #include "wx/listimpl.cpp"
38
39 // uncomment this line to visually show the extent of the selection
40 //#define DEBUG_HTML_SELECTION
41
42 // HTML events:
43 IMPLEMENT_DYNAMIC_CLASS(wxHtmlLinkEvent, wxCommandEvent)
44 IMPLEMENT_DYNAMIC_CLASS(wxHtmlCellEvent, wxCommandEvent)
45
46 wxDEFINE_EVENT( wxEVT_HTML_CELL_CLICKED, wxHtmlCellEvent );
47 wxDEFINE_EVENT( wxEVT_HTML_CELL_HOVER, wxHtmlCellEvent );
48 wxDEFINE_EVENT( wxEVT_HTML_LINK_CLICKED, wxHtmlLinkEvent );
49
50
51 #if wxUSE_CLIPBOARD
52 // ----------------------------------------------------------------------------
53 // wxHtmlWinAutoScrollTimer: the timer used to generate a stream of scroll
54 // events when a captured mouse is held outside the window
55 // ----------------------------------------------------------------------------
56
57 class wxHtmlWinAutoScrollTimer : public wxTimer
58 {
59 public:
60 wxHtmlWinAutoScrollTimer(wxScrolledWindow *win,
61 wxEventType eventTypeToSend,
62 int pos, int orient)
63 {
64 m_win = win;
65 m_eventType = eventTypeToSend;
66 m_pos = pos;
67 m_orient = orient;
68 }
69
70 virtual void Notify();
71
72 private:
73 wxScrolledWindow *m_win;
74 wxEventType m_eventType;
75 int m_pos,
76 m_orient;
77
78 wxDECLARE_NO_COPY_CLASS(wxHtmlWinAutoScrollTimer);
79 };
80
81 void wxHtmlWinAutoScrollTimer::Notify()
82 {
83 // only do all this as long as the window is capturing the mouse
84 if ( wxWindow::GetCapture() != m_win )
85 {
86 Stop();
87 }
88 else // we still capture the mouse, continue generating events
89 {
90 // first scroll the window if we are allowed to do it
91 wxScrollWinEvent event1(m_eventType, m_pos, m_orient);
92 event1.SetEventObject(m_win);
93 if ( m_win->GetEventHandler()->ProcessEvent(event1) )
94 {
95 // and then send a pseudo mouse-move event to refresh the selection
96 wxMouseEvent event2(wxEVT_MOTION);
97 wxGetMousePosition(&event2.m_x, &event2.m_y);
98
99 // the mouse event coordinates should be client, not screen as
100 // returned by wxGetMousePosition
101 wxWindow *parentTop = m_win;
102 while ( parentTop->GetParent() )
103 parentTop = parentTop->GetParent();
104 wxPoint ptOrig = parentTop->GetPosition();
105 event2.m_x -= ptOrig.x;
106 event2.m_y -= ptOrig.y;
107
108 event2.SetEventObject(m_win);
109
110 // FIXME: we don't fill in the other members - ok?
111 m_win->GetEventHandler()->ProcessEvent(event2);
112 }
113 else // can't scroll further, stop
114 {
115 Stop();
116 }
117 }
118 }
119
120 #endif // wxUSE_CLIPBOARD
121
122
123
124 //-----------------------------------------------------------------------------
125 // wxHtmlHistoryItem
126 //-----------------------------------------------------------------------------
127
128 // item of history list
129 class WXDLLIMPEXP_HTML wxHtmlHistoryItem
130 {
131 public:
132 wxHtmlHistoryItem(const wxString& p, const wxString& a) {m_Page = p, m_Anchor = a, m_Pos = 0;}
133 int GetPos() const {return m_Pos;}
134 void SetPos(int p) {m_Pos = p;}
135 const wxString& GetPage() const {return m_Page;}
136 const wxString& GetAnchor() const {return m_Anchor;}
137
138 private:
139 wxString m_Page;
140 wxString m_Anchor;
141 int m_Pos;
142 };
143
144
145 //-----------------------------------------------------------------------------
146 // our private arrays:
147 //-----------------------------------------------------------------------------
148
149 WX_DECLARE_OBJARRAY(wxHtmlHistoryItem, wxHtmlHistoryArray);
150 WX_DEFINE_OBJARRAY(wxHtmlHistoryArray)
151
152 WX_DECLARE_LIST(wxHtmlProcessor, wxHtmlProcessorList);
153 WX_DEFINE_LIST(wxHtmlProcessorList)
154
155 //-----------------------------------------------------------------------------
156 // wxHtmlWindowMouseHelper
157 //-----------------------------------------------------------------------------
158
159 wxHtmlWindowMouseHelper::wxHtmlWindowMouseHelper(wxHtmlWindowInterface *iface)
160 : m_tmpMouseMoved(false),
161 m_tmpLastLink(NULL),
162 m_tmpLastCell(NULL),
163 m_interface(iface)
164 {
165 }
166
167 void wxHtmlWindowMouseHelper::HandleMouseMoved()
168 {
169 m_tmpMouseMoved = true;
170 }
171
172 bool wxHtmlWindowMouseHelper::HandleMouseClick(wxHtmlCell *rootCell,
173 const wxPoint& pos,
174 const wxMouseEvent& event)
175 {
176 if (!rootCell)
177 return false;
178
179 wxHtmlCell *cell = rootCell->FindCellByPos(pos.x, pos.y);
180 // this check is needed because FindCellByPos returns terminal cell and
181 // containers may have empty borders -- in this case NULL will be
182 // returned
183 if (!cell)
184 return false;
185
186 // adjust the coordinates to be relative to this cell:
187 wxPoint relpos = pos - cell->GetAbsPos(rootCell);
188
189 return OnCellClicked(cell, relpos.x, relpos.y, event);
190 }
191
192 void wxHtmlWindowMouseHelper::HandleIdle(wxHtmlCell *rootCell,
193 const wxPoint& pos)
194 {
195 wxHtmlCell *cell = rootCell ? rootCell->FindCellByPos(pos.x, pos.y) : NULL;
196
197 if (cell != m_tmpLastCell)
198 {
199 wxHtmlLinkInfo *lnk = NULL;
200 if (cell)
201 {
202 // adjust the coordinates to be relative to this cell:
203 wxPoint relpos = pos - cell->GetAbsPos(rootCell);
204 lnk = cell->GetLink(relpos.x, relpos.y);
205 }
206
207 wxCursor cur;
208 if (cell)
209 cur = cell->GetMouseCursorAt(m_interface, pos);
210 else
211 cur = m_interface->GetHTMLCursor(
212 wxHtmlWindowInterface::HTMLCursor_Default);
213
214 m_interface->GetHTMLWindow()->SetCursor(cur);
215
216 if (lnk != m_tmpLastLink)
217 {
218 if (lnk)
219 m_interface->SetHTMLStatusText(lnk->GetHref());
220 else
221 m_interface->SetHTMLStatusText(wxEmptyString);
222
223 m_tmpLastLink = lnk;
224 }
225
226 m_tmpLastCell = cell;
227 }
228 else // mouse moved but stayed in the same cell
229 {
230 if ( cell )
231 {
232 // A single cell can have different cursors for different positions,
233 // so update cursor for this case as well.
234 wxCursor cur = cell->GetMouseCursorAt(m_interface, pos);
235 m_interface->GetHTMLWindow()->SetCursor(cur);
236
237 OnCellMouseHover(cell, pos.x, pos.y);
238 }
239 }
240
241 m_tmpMouseMoved = false;
242 }
243
244 bool wxHtmlWindowMouseHelper::OnCellClicked(wxHtmlCell *cell,
245 wxCoord x, wxCoord y,
246 const wxMouseEvent& event)
247 {
248 wxHtmlCellEvent ev(wxEVT_HTML_CELL_CLICKED,
249 m_interface->GetHTMLWindow()->GetId(),
250 cell, wxPoint(x,y), event);
251
252 if (!m_interface->GetHTMLWindow()->GetEventHandler()->ProcessEvent(ev))
253 {
254 // if the event wasn't handled, do the default processing here:
255
256 wxASSERT_MSG( cell, wxT("can't be called with NULL cell") );
257
258 cell->ProcessMouseClick(m_interface, ev.GetPoint(), ev.GetMouseEvent());
259 }
260
261 // true if a link was clicked, false otherwise
262 return ev.GetLinkClicked();
263 }
264
265 void wxHtmlWindowMouseHelper::OnCellMouseHover(wxHtmlCell * cell,
266 wxCoord x,
267 wxCoord y)
268 {
269 wxHtmlCellEvent ev(wxEVT_HTML_CELL_HOVER,
270 m_interface->GetHTMLWindow()->GetId(),
271 cell, wxPoint(x,y), wxMouseEvent());
272 m_interface->GetHTMLWindow()->GetEventHandler()->ProcessEvent(ev);
273 }
274
275
276
277
278 //-----------------------------------------------------------------------------
279 // wxHtmlWindow
280 //-----------------------------------------------------------------------------
281
282 wxList wxHtmlWindow::m_Filters;
283 wxHtmlFilter *wxHtmlWindow::m_DefaultFilter = NULL;
284 wxHtmlProcessorList *wxHtmlWindow::m_GlobalProcessors = NULL;
285 wxCursor *wxHtmlWindow::ms_cursorLink = NULL;
286 wxCursor *wxHtmlWindow::ms_cursorText = NULL;
287
288 void wxHtmlWindow::CleanUpStatics()
289 {
290 wxDELETE(m_DefaultFilter);
291 WX_CLEAR_LIST(wxList, m_Filters);
292 if (m_GlobalProcessors)
293 WX_CLEAR_LIST(wxHtmlProcessorList, *m_GlobalProcessors);
294 wxDELETE(m_GlobalProcessors);
295 wxDELETE(ms_cursorLink);
296 wxDELETE(ms_cursorText);
297 }
298
299 void wxHtmlWindow::Init()
300 {
301 m_tmpCanDrawLocks = 0;
302 m_FS = new wxFileSystem();
303 #if wxUSE_STATUSBAR
304 m_RelatedStatusBar = NULL;
305 m_RelatedStatusBarIndex = -1;
306 #endif // wxUSE_STATUSBAR
307 m_RelatedFrame = NULL;
308 m_TitleFormat = wxT("%s");
309 m_OpenedPage = m_OpenedAnchor = m_OpenedPageTitle = wxEmptyString;
310 m_Cell = NULL;
311 m_Parser = new wxHtmlWinParser(this);
312 m_Parser->SetFS(m_FS);
313 m_HistoryPos = -1;
314 m_HistoryOn = true;
315 m_History = new wxHtmlHistoryArray;
316 m_Processors = NULL;
317 SetBorders(10);
318 m_selection = NULL;
319 m_makingSelection = false;
320 #if wxUSE_CLIPBOARD
321 m_timerAutoScroll = NULL;
322 m_lastDoubleClick = 0;
323 #endif // wxUSE_CLIPBOARD
324 m_tmpSelFromCell = NULL;
325 }
326
327 bool wxHtmlWindow::Create(wxWindow *parent, wxWindowID id,
328 const wxPoint& pos, const wxSize& size,
329 long style, const wxString& name)
330 {
331 if (!wxScrolledWindow::Create(parent, id, pos, size,
332 style | wxVSCROLL | wxHSCROLL,
333 name))
334 return false;
335
336 // We can't erase our background in EVT_ERASE_BACKGROUND handler and use
337 // double buffering in EVT_PAINT handler as this requires blitting back
338 // something already drawn on the window to the backing store bitmap when
339 // handling EVT_PAINT but blitting in this direction is simply not
340 // supported by OS X.
341 //
342 // So instead we use a hack with artificial EVT_ERASE_BACKGROUND generation
343 // from OnPaint() and this means that we never need the "real" erase event
344 // at all so disable it to avoid executing any user-defined handlers twice
345 // (and to avoid processing unnecessary event if no handlers are defined).
346 SetBackgroundStyle(wxBG_STYLE_PAINT);
347 SetPage(wxT("<html><body></body></html>"));
348
349 SetInitialSize(size);
350 return true;
351 }
352
353
354 wxHtmlWindow::~wxHtmlWindow()
355 {
356 #if wxUSE_CLIPBOARD
357 StopAutoScrolling();
358 #endif // wxUSE_CLIPBOARD
359 HistoryClear();
360
361 delete m_selection;
362
363 delete m_Cell;
364
365 if ( m_Processors )
366 {
367 WX_CLEAR_LIST(wxHtmlProcessorList, *m_Processors);
368 }
369
370 delete m_Parser;
371 delete m_FS;
372 delete m_History;
373 delete m_Processors;
374 }
375
376
377
378 void wxHtmlWindow::SetRelatedFrame(wxFrame* frame, const wxString& format)
379 {
380 m_RelatedFrame = frame;
381 m_TitleFormat = format;
382 }
383
384
385
386 #if wxUSE_STATUSBAR
387 void wxHtmlWindow::SetRelatedStatusBar(int index)
388 {
389 m_RelatedStatusBarIndex = index;
390 }
391
392 void wxHtmlWindow::SetRelatedStatusBar(wxStatusBar* statusbar, int index)
393 {
394 m_RelatedStatusBar = statusbar;
395 m_RelatedStatusBarIndex = index;
396 }
397
398 #endif // wxUSE_STATUSBAR
399
400
401
402 void wxHtmlWindow::SetFonts(const wxString& normal_face, const wxString& fixed_face, const int *sizes)
403 {
404 m_Parser->SetFonts(normal_face, fixed_face, sizes);
405
406 // re-layout the page after changing fonts:
407 DoSetPage(*(m_Parser->GetSource()));
408 }
409
410 void wxHtmlWindow::SetStandardFonts(int size,
411 const wxString& normal_face,
412 const wxString& fixed_face)
413 {
414 m_Parser->SetStandardFonts(size, normal_face, fixed_face);
415
416 // re-layout the page after changing fonts:
417 DoSetPage(*(m_Parser->GetSource()));
418 }
419
420 bool wxHtmlWindow::SetPage(const wxString& source)
421 {
422 m_OpenedPage = m_OpenedAnchor = m_OpenedPageTitle = wxEmptyString;
423 return DoSetPage(source);
424 }
425
426 bool wxHtmlWindow::DoSetPage(const wxString& source)
427 {
428 wxString newsrc(source);
429
430 wxDELETE(m_selection);
431
432 // we will soon delete all the cells, so clear pointers to them:
433 m_tmpSelFromCell = NULL;
434
435 // pass HTML through registered processors:
436 if (m_Processors || m_GlobalProcessors)
437 {
438 wxHtmlProcessorList::compatibility_iterator nodeL, nodeG;
439 int prL, prG;
440
441 if ( m_Processors )
442 nodeL = m_Processors->GetFirst();
443 if ( m_GlobalProcessors )
444 nodeG = m_GlobalProcessors->GetFirst();
445
446 // VS: there are two lists, global and local, both of them sorted by
447 // priority. Since we have to go through _both_ lists with
448 // decreasing priority, we "merge-sort" the lists on-line by
449 // processing that one of the two heads that has higher priority
450 // in every iteration
451 while (nodeL || nodeG)
452 {
453 prL = (nodeL) ? nodeL->GetData()->GetPriority() : -1;
454 prG = (nodeG) ? nodeG->GetData()->GetPriority() : -1;
455 if (prL > prG)
456 {
457 if (nodeL->GetData()->IsEnabled())
458 newsrc = nodeL->GetData()->Process(newsrc);
459 nodeL = nodeL->GetNext();
460 }
461 else // prL <= prG
462 {
463 if (nodeG->GetData()->IsEnabled())
464 newsrc = nodeG->GetData()->Process(newsrc);
465 nodeG = nodeG->GetNext();
466 }
467 }
468 }
469
470 // ...and run the parser on it:
471 wxClientDC *dc = new wxClientDC(this);
472 dc->SetMapMode(wxMM_TEXT);
473 SetBackgroundColour(wxColour(0xFF, 0xFF, 0xFF));
474 SetBackgroundImage(wxNullBitmap);
475
476 m_Parser->SetDC(dc);
477
478 // notice that it's important to set m_Cell to NULL here before calling
479 // Parse() below, even if it will be overwritten by its return value as
480 // without this we may crash if it's used from inside Parse(), so use
481 // wxDELETE() and not just delete here
482 wxDELETE(m_Cell);
483
484 m_Cell = (wxHtmlContainerCell*) m_Parser->Parse(newsrc);
485 delete dc;
486 m_Cell->SetIndent(m_Borders, wxHTML_INDENT_ALL, wxHTML_UNITS_PIXELS);
487 m_Cell->SetAlignHor(wxHTML_ALIGN_CENTER);
488 CreateLayout();
489 if (m_tmpCanDrawLocks == 0)
490 Refresh();
491 return true;
492 }
493
494 bool wxHtmlWindow::AppendToPage(const wxString& source)
495 {
496 return DoSetPage(*(GetParser()->GetSource()) + source);
497 }
498
499 bool wxHtmlWindow::LoadPage(const wxString& location)
500 {
501 wxCHECK_MSG( !location.empty(), false, "location must be non-empty" );
502
503 wxBusyCursor busyCursor;
504
505 bool rt_val;
506 bool needs_refresh = false;
507
508 m_tmpCanDrawLocks++;
509 if (m_HistoryOn && (m_HistoryPos != -1))
510 {
511 // store scroll position into history item:
512 int x, y;
513 GetViewStart(&x, &y);
514 (*m_History)[m_HistoryPos].SetPos(y);
515 }
516
517 // first check if we're moving to an anchor in the same page
518 size_t posLocalAnchor = location.Find('#');
519 if ( posLocalAnchor != wxString::npos && posLocalAnchor != 0 )
520 {
521 // check if the part before the anchor is the same as the (either
522 // relative or absolute) URI of the current page
523 const wxString beforeAnchor = location.substr(0, posLocalAnchor);
524 if ( beforeAnchor != m_OpenedPage &&
525 m_FS->GetPath() + beforeAnchor != m_OpenedPage )
526 {
527 // indicate that we're not moving to a local anchor
528 posLocalAnchor = wxString::npos;
529 }
530 }
531
532 if ( posLocalAnchor != wxString::npos )
533 {
534 m_tmpCanDrawLocks--;
535 rt_val = ScrollToAnchor(location.substr(posLocalAnchor + 1));
536 m_tmpCanDrawLocks++;
537 }
538 else // moving to another page
539 {
540 needs_refresh = true;
541 #if wxUSE_STATUSBAR
542 // load&display it:
543 if (m_RelatedStatusBarIndex != -1)
544 {
545 SetHTMLStatusText(_("Connecting..."));
546 Refresh(false);
547 }
548 #endif // wxUSE_STATUSBAR
549
550 wxFSFile *f = m_Parser->OpenURL(wxHTML_URL_PAGE, location);
551
552 // try to interpret 'location' as filename instead of URL:
553 if (f == NULL)
554 {
555 wxFileName fn(location);
556 wxString location2 = wxFileSystem::FileNameToURL(fn);
557 f = m_Parser->OpenURL(wxHTML_URL_PAGE, location2);
558 }
559
560 if (f == NULL)
561 {
562 wxLogError(_("Unable to open requested HTML document: %s"), location.c_str());
563 m_tmpCanDrawLocks--;
564 SetHTMLStatusText(wxEmptyString);
565 return false;
566 }
567
568 else
569 {
570 wxList::compatibility_iterator node;
571 wxString src = wxEmptyString;
572
573 #if wxUSE_STATUSBAR
574 if (m_RelatedStatusBarIndex != -1)
575 {
576 wxString msg = _("Loading : ") + location;
577 SetHTMLStatusText(msg);
578 Refresh(false);
579 }
580 #endif // wxUSE_STATUSBAR
581
582 node = m_Filters.GetFirst();
583 while (node)
584 {
585 wxHtmlFilter *h = (wxHtmlFilter*) node->GetData();
586 if (h->CanRead(*f))
587 {
588 src = h->ReadFile(*f);
589 break;
590 }
591 node = node->GetNext();
592 }
593 if (src == wxEmptyString)
594 {
595 if (m_DefaultFilter == NULL) m_DefaultFilter = GetDefaultFilter();
596 src = m_DefaultFilter->ReadFile(*f);
597 }
598
599 m_FS->ChangePathTo(f->GetLocation());
600 rt_val = SetPage(src);
601 m_OpenedPage = f->GetLocation();
602 if (f->GetAnchor() != wxEmptyString)
603 {
604 ScrollToAnchor(f->GetAnchor());
605 }
606
607 delete f;
608
609 #if wxUSE_STATUSBAR
610 if (m_RelatedStatusBarIndex != -1)
611 {
612 SetHTMLStatusText(_("Done"));
613 }
614 #endif // wxUSE_STATUSBAR
615 }
616 }
617
618 if (m_HistoryOn) // add this page to history there:
619 {
620 int c = m_History->GetCount() - (m_HistoryPos + 1);
621
622 if (m_HistoryPos < 0 ||
623 (*m_History)[m_HistoryPos].GetPage() != m_OpenedPage ||
624 (*m_History)[m_HistoryPos].GetAnchor() != m_OpenedAnchor)
625 {
626 m_HistoryPos++;
627 for (int i = 0; i < c; i++)
628 m_History->RemoveAt(m_HistoryPos);
629 m_History->Add(new wxHtmlHistoryItem(m_OpenedPage, m_OpenedAnchor));
630 }
631 }
632
633 if (m_OpenedPageTitle == wxEmptyString)
634 OnSetTitle(wxFileNameFromPath(m_OpenedPage));
635
636 if (needs_refresh)
637 {
638 m_tmpCanDrawLocks--;
639 Refresh();
640 }
641 else
642 m_tmpCanDrawLocks--;
643
644 return rt_val;
645 }
646
647
648 bool wxHtmlWindow::LoadFile(const wxFileName& filename)
649 {
650 wxString url = wxFileSystem::FileNameToURL(filename);
651 return LoadPage(url);
652 }
653
654
655 bool wxHtmlWindow::ScrollToAnchor(const wxString& anchor)
656 {
657 const wxHtmlCell *c = m_Cell->Find(wxHTML_COND_ISANCHOR, &anchor);
658 if (!c)
659 {
660 wxLogWarning(_("HTML anchor %s does not exist."), anchor.c_str());
661 return false;
662 }
663 else
664 {
665 // Go to next visible cell in current container, if it exists. This
666 // yields a bit better (even though still imperfect) results in that
667 // there's better chance of using a suitable cell for upper Y
668 // coordinate value. See bug #11406 for additional discussion.
669 const wxHtmlCell *c_save = c;
670 while ( c && c->IsFormattingCell() )
671 c = c->GetNext();
672 if ( !c )
673 c = c_save;
674
675 int y;
676
677 for (y = 0; c != NULL; c = c->GetParent()) y += c->GetPosY();
678 Scroll(-1, y / wxHTML_SCROLL_STEP);
679 m_OpenedAnchor = anchor;
680 return true;
681 }
682 }
683
684
685 void wxHtmlWindow::OnSetTitle(const wxString& title)
686 {
687 if (m_RelatedFrame)
688 {
689 wxString tit;
690 tit.Printf(m_TitleFormat, title.c_str());
691 m_RelatedFrame->SetTitle(tit);
692 }
693 m_OpenedPageTitle = title;
694 }
695
696
697 // return scroll steps such that a) scrollbars aren't shown needlessly
698 // and b) entire content is viewable (i.e. round up)
699 static int ScrollSteps(int size, int available)
700 {
701 if ( size <= available )
702 return 0;
703 else
704 return (size + wxHTML_SCROLL_STEP - 1) / wxHTML_SCROLL_STEP;
705 }
706
707
708 void wxHtmlWindow::CreateLayout()
709 {
710 // SetScrollbars() results in size change events -- and thus a nested
711 // CreateLayout() call -- on some platforms. Ignore nested calls, toplevel
712 // CreateLayout() will do the right thing eventually.
713 static wxRecursionGuardFlag s_flagReentrancy;
714 wxRecursionGuard guard(s_flagReentrancy);
715 if ( guard.IsInside() )
716 return;
717
718 if (!m_Cell)
719 return;
720
721 int clientWidth, clientHeight;
722 GetClientSize(&clientWidth, &clientHeight);
723
724 const int vscrollbar = wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
725 const int hscrollbar = wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y);
726
727 if ( HasScrollbar(wxHORIZONTAL) )
728 clientHeight += hscrollbar;
729
730 if ( HasScrollbar(wxVERTICAL) )
731 clientWidth += vscrollbar;
732
733 if ( HasFlag(wxHW_SCROLLBAR_NEVER) )
734 {
735 SetScrollbars(1, 1, 0, 0); // always off
736 m_Cell->Layout(clientWidth);
737 }
738 else // !wxHW_SCROLLBAR_NEVER
739 {
740 // Lay the content out with the assumption that it's too large to fit
741 // in the window (this is likely to be the case):
742 m_Cell->Layout(clientWidth - vscrollbar);
743
744 // If the layout is wider than the window, horizontal scrollbar will
745 // certainly be shown. Account for it here for subsequent computations.
746 if ( m_Cell->GetWidth() > clientWidth )
747 clientHeight -= hscrollbar;
748
749 if ( m_Cell->GetHeight() <= clientHeight )
750 {
751 // we fit into the window, hide vertical scrollbar:
752 SetScrollbars
753 (
754 wxHTML_SCROLL_STEP, wxHTML_SCROLL_STEP,
755 ScrollSteps(m_Cell->GetWidth(), clientWidth - vscrollbar),
756 0
757 );
758 // ...and redo the layout to use the extra space
759 m_Cell->Layout(clientWidth);
760 }
761 else
762 {
763 // If the content doesn't fit into the window by only a small
764 // margin, chances are that it may fit fully with scrollbar turned
765 // off. It's something worth trying but on the other hand, we don't
766 // want to waste too much time redoing the layout (twice!) for
767 // long -- and thus expensive to layout -- pages. The cut-off value
768 // is an arbitrary heuristics.
769 static const int SMALL_OVERLAP = 60;
770 if ( m_Cell->GetHeight() <= clientHeight + SMALL_OVERLAP )
771 {
772 m_Cell->Layout(clientWidth);
773
774 if ( m_Cell->GetHeight() <= clientHeight )
775 {
776 // Great, we fit in. Hide the scrollbar.
777 SetScrollbars
778 (
779 wxHTML_SCROLL_STEP, wxHTML_SCROLL_STEP,
780 ScrollSteps(m_Cell->GetWidth(), clientWidth),
781 0
782 );
783 return;
784 }
785 else
786 {
787 // That didn't work out, go back to previous layout. Note
788 // that redoing the layout once again here isn't as bad as
789 // it looks -- thanks to the small cut-off value, it's a
790 // reasonably small page.
791 m_Cell->Layout(clientWidth - vscrollbar);
792 }
793 }
794 // else: the page is very long, it will certainly need scrollbar
795
796 SetScrollbars
797 (
798 wxHTML_SCROLL_STEP, wxHTML_SCROLL_STEP,
799 ScrollSteps(m_Cell->GetWidth(), clientWidth - vscrollbar),
800 ScrollSteps(m_Cell->GetHeight(), clientHeight)
801 );
802 }
803 }
804 }
805
806 #if wxUSE_CONFIG
807 void wxHtmlWindow::ReadCustomization(wxConfigBase *cfg, wxString path)
808 {
809 wxString oldpath;
810 wxString tmp;
811 int p_fontsizes[7];
812 wxString p_fff, p_ffn;
813
814 if (path != wxEmptyString)
815 {
816 oldpath = cfg->GetPath();
817 cfg->SetPath(path);
818 }
819
820 m_Borders = cfg->Read(wxT("wxHtmlWindow/Borders"), m_Borders);
821 p_fff = cfg->Read(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser->m_FontFaceFixed);
822 p_ffn = cfg->Read(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser->m_FontFaceNormal);
823 for (int i = 0; i < 7; i++)
824 {
825 tmp.Printf(wxT("wxHtmlWindow/FontsSize%i"), i);
826 p_fontsizes[i] = cfg->Read(tmp, m_Parser->m_FontsSizes[i]);
827 }
828 SetFonts(p_ffn, p_fff, p_fontsizes);
829
830 if (path != wxEmptyString)
831 cfg->SetPath(oldpath);
832 }
833
834
835
836 void wxHtmlWindow::WriteCustomization(wxConfigBase *cfg, wxString path)
837 {
838 wxString oldpath;
839 wxString tmp;
840
841 if (path != wxEmptyString)
842 {
843 oldpath = cfg->GetPath();
844 cfg->SetPath(path);
845 }
846
847 cfg->Write(wxT("wxHtmlWindow/Borders"), (long) m_Borders);
848 cfg->Write(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser->m_FontFaceFixed);
849 cfg->Write(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser->m_FontFaceNormal);
850 for (int i = 0; i < 7; i++)
851 {
852 tmp.Printf(wxT("wxHtmlWindow/FontsSize%i"), i);
853 cfg->Write(tmp, (long) m_Parser->m_FontsSizes[i]);
854 }
855
856 if (path != wxEmptyString)
857 cfg->SetPath(oldpath);
858 }
859 #endif // wxUSE_CONFIG
860
861 bool wxHtmlWindow::HistoryBack()
862 {
863 wxString a, l;
864
865 if (m_HistoryPos < 1) return false;
866
867 // store scroll position into history item:
868 int x, y;
869 GetViewStart(&x, &y);
870 (*m_History)[m_HistoryPos].SetPos(y);
871
872 // go to previous position:
873 m_HistoryPos--;
874
875 l = (*m_History)[m_HistoryPos].GetPage();
876 a = (*m_History)[m_HistoryPos].GetAnchor();
877 m_HistoryOn = false;
878 m_tmpCanDrawLocks++;
879 if (a == wxEmptyString) LoadPage(l);
880 else LoadPage(l + wxT("#") + a);
881 m_HistoryOn = true;
882 m_tmpCanDrawLocks--;
883 Scroll(0, (*m_History)[m_HistoryPos].GetPos());
884 Refresh();
885 return true;
886 }
887
888 bool wxHtmlWindow::HistoryCanBack()
889 {
890 if (m_HistoryPos < 1) return false;
891 return true ;
892 }
893
894
895 bool wxHtmlWindow::HistoryForward()
896 {
897 wxString a, l;
898
899 if (m_HistoryPos == -1) return false;
900 if (m_HistoryPos >= (int)m_History->GetCount() - 1)return false;
901
902 m_OpenedPage = wxEmptyString; // this will disable adding new entry into history in LoadPage()
903
904 m_HistoryPos++;
905 l = (*m_History)[m_HistoryPos].GetPage();
906 a = (*m_History)[m_HistoryPos].GetAnchor();
907 m_HistoryOn = false;
908 m_tmpCanDrawLocks++;
909 if (a == wxEmptyString) LoadPage(l);
910 else LoadPage(l + wxT("#") + a);
911 m_HistoryOn = true;
912 m_tmpCanDrawLocks--;
913 Scroll(0, (*m_History)[m_HistoryPos].GetPos());
914 Refresh();
915 return true;
916 }
917
918 bool wxHtmlWindow::HistoryCanForward()
919 {
920 if (m_HistoryPos == -1) return false;
921 if (m_HistoryPos >= (int)m_History->GetCount() - 1)return false;
922 return true ;
923 }
924
925
926 void wxHtmlWindow::HistoryClear()
927 {
928 m_History->Empty();
929 m_HistoryPos = -1;
930 }
931
932 void wxHtmlWindow::AddProcessor(wxHtmlProcessor *processor)
933 {
934 if (!m_Processors)
935 {
936 m_Processors = new wxHtmlProcessorList;
937 }
938 wxHtmlProcessorList::compatibility_iterator node;
939
940 for (node = m_Processors->GetFirst(); node; node = node->GetNext())
941 {
942 if (processor->GetPriority() > node->GetData()->GetPriority())
943 {
944 m_Processors->Insert(node, processor);
945 return;
946 }
947 }
948 m_Processors->Append(processor);
949 }
950
951 /*static */ void wxHtmlWindow::AddGlobalProcessor(wxHtmlProcessor *processor)
952 {
953 if (!m_GlobalProcessors)
954 {
955 m_GlobalProcessors = new wxHtmlProcessorList;
956 }
957 wxHtmlProcessorList::compatibility_iterator node;
958
959 for (node = m_GlobalProcessors->GetFirst(); node; node = node->GetNext())
960 {
961 if (processor->GetPriority() > node->GetData()->GetPriority())
962 {
963 m_GlobalProcessors->Insert(node, processor);
964 return;
965 }
966 }
967 m_GlobalProcessors->Append(processor);
968 }
969
970
971
972 void wxHtmlWindow::AddFilter(wxHtmlFilter *filter)
973 {
974 m_Filters.Append(filter);
975 }
976
977
978 bool wxHtmlWindow::IsSelectionEnabled() const
979 {
980 #if wxUSE_CLIPBOARD
981 return !HasFlag(wxHW_NO_SELECTION);
982 #else
983 return false;
984 #endif
985 }
986
987
988 #if wxUSE_CLIPBOARD
989 wxString wxHtmlWindow::DoSelectionToText(wxHtmlSelection *sel)
990 {
991 if ( !sel )
992 return wxEmptyString;
993
994 wxClientDC dc(this);
995 wxString text;
996
997 wxHtmlTerminalCellsInterator i(sel->GetFromCell(), sel->GetToCell());
998 const wxHtmlCell *prev = NULL;
999
1000 while ( i )
1001 {
1002 // When converting HTML content to plain text, the entire paragraph
1003 // (container in wxHTML) goes on single line. A new paragraph (that
1004 // should go on its own line) has its own container. Therefore, the
1005 // simplest way of detecting where to insert newlines in plain text
1006 // is to check if the parent container changed -- if it did, we moved
1007 // to a new paragraph.
1008 if ( prev && prev->GetParent() != i->GetParent() )
1009 text << '\n';
1010
1011 // NB: we don't need to pass the selection to ConvertToText() in the
1012 // middle of the selected text; it's only useful when only part of
1013 // a cell is selected
1014 text << i->ConvertToText(sel);
1015
1016 prev = *i;
1017 ++i;
1018 }
1019 return text;
1020 }
1021
1022 wxString wxHtmlWindow::ToText()
1023 {
1024 if (m_Cell)
1025 {
1026 wxHtmlSelection sel;
1027 sel.Set(m_Cell->GetFirstTerminal(), m_Cell->GetLastTerminal());
1028 return DoSelectionToText(&sel);
1029 }
1030 else
1031 return wxEmptyString;
1032 }
1033
1034 #endif // wxUSE_CLIPBOARD
1035
1036 bool wxHtmlWindow::CopySelection(ClipboardType t)
1037 {
1038 #if wxUSE_CLIPBOARD
1039 if ( m_selection )
1040 {
1041 #if defined(__UNIX__) && !defined(__WXMAC__)
1042 wxTheClipboard->UsePrimarySelection(t == Primary);
1043 #else // !__UNIX__
1044 // Primary selection exists only under X11, so don't do anything under
1045 // the other platforms when we try to access it
1046 //
1047 // TODO: this should be abstracted at wxClipboard level!
1048 if ( t == Primary )
1049 return false;
1050 #endif // __UNIX__/!__UNIX__
1051
1052 if ( wxTheClipboard->Open() )
1053 {
1054 const wxString txt(SelectionToText());
1055 wxTheClipboard->SetData(new wxTextDataObject(txt));
1056 wxTheClipboard->Close();
1057 wxLogTrace(wxT("wxhtmlselection"),
1058 _("Copied to clipboard:\"%s\""), txt.c_str());
1059
1060 return true;
1061 }
1062 }
1063 #else
1064 wxUnusedVar(t);
1065 #endif // wxUSE_CLIPBOARD
1066
1067 return false;
1068 }
1069
1070
1071 void wxHtmlWindow::OnLinkClicked(const wxHtmlLinkInfo& link)
1072 {
1073 wxHtmlLinkEvent event(GetId(), link);
1074 event.SetEventObject(this);
1075 if (!GetEventHandler()->ProcessEvent(event))
1076 {
1077 // the default behaviour is to load the URL in this window
1078 const wxMouseEvent *e = event.GetLinkInfo().GetEvent();
1079 if (e == NULL || e->LeftUp())
1080 LoadPage(event.GetLinkInfo().GetHref());
1081 }
1082 }
1083
1084 void wxHtmlWindow::DoEraseBackground(wxDC& dc)
1085 {
1086 // if we don't have any background bitmap we just fill it with background
1087 // colour and we also must do it if the background bitmap is not fully
1088 // opaque as otherwise junk could be left there
1089 if ( !m_bmpBg.IsOk() || m_bmpBg.GetMask() )
1090 {
1091 dc.SetBackground(GetBackgroundColour());
1092 dc.Clear();
1093 }
1094
1095 if ( m_bmpBg.IsOk() )
1096 {
1097 // draw the background bitmap tiling it over the entire window area
1098 const wxSize sz = GetVirtualSize();
1099 const wxSize sizeBmp(m_bmpBg.GetWidth(), m_bmpBg.GetHeight());
1100 for ( wxCoord x = 0; x < sz.x; x += sizeBmp.x )
1101 {
1102 for ( wxCoord y = 0; y < sz.y; y += sizeBmp.y )
1103 {
1104 dc.DrawBitmap(m_bmpBg, x, y, true /* use mask */);
1105 }
1106 }
1107 }
1108 }
1109
1110 void wxHtmlWindow::OnEraseBackground(wxEraseEvent& WXUNUSED(event))
1111 {
1112 // We never get real erase background events as we changed our background
1113 // style to wxBG_STYLE_PAINT in our ctor so the only time when we get here
1114 // is when an artificial wxEraseEvent is generated by our own OnPaint()
1115 // below. This handler only exists to stop the event from propagating
1116 // downwards to wxWindow which may erase the background itself when it gets
1117 // it in some ports (currently this happens in wxUniv), so we simply stop
1118 // processing here and set a special flag allowing OnPaint() to see that
1119 // the event hadn't been really processed.
1120 m_isBgReallyErased = false;
1121 }
1122
1123 void wxHtmlWindow::OnPaint(wxPaintEvent& WXUNUSED(event))
1124 {
1125 wxPaintDC dcPaint(this);
1126
1127 if (m_tmpCanDrawLocks > 0 || m_Cell == NULL)
1128 return;
1129
1130 int x, y;
1131 GetViewStart(&x, &y);
1132 const wxRect rect = GetUpdateRegion().GetBox();
1133 const wxSize sz = GetClientSize();
1134
1135 // set up the DC we're drawing on: if the window is already double buffered
1136 // we do it directly on wxPaintDC, otherwise we allocate a backing store
1137 // buffer and compose the drawing there and then blit it to screen all at
1138 // once
1139 wxDC *dc;
1140 wxMemoryDC dcm;
1141 if ( IsDoubleBuffered() )
1142 {
1143 dc = &dcPaint;
1144 }
1145 else // window is not double buffered by the system, do it ourselves
1146 {
1147 if ( !m_backBuffer.IsOk() )
1148 m_backBuffer.Create(sz.x, sz.y);
1149 dcm.SelectObject(m_backBuffer);
1150 dc = &dcm;
1151 }
1152
1153 PrepareDC(*dc);
1154
1155 // Erase the background: for compatibility, we must generate the event to
1156 // allow the user-defined handlers to do it, hence this hack with sending
1157 // an artificial wxEraseEvent to trigger the execution of such handlers.
1158 wxEraseEvent eraseEvent(GetId(), dc);
1159 eraseEvent.SetEventObject(this);
1160
1161 // Hack inside a hack: the background wasn't really erased if our own
1162 // OnEraseBackground() was executed, so we need to check for the flag set
1163 // by it whenever it's called.
1164 m_isBgReallyErased = true; // Initially assume it wasn't.
1165 if ( !ProcessWindowEvent(eraseEvent) || !m_isBgReallyErased )
1166 {
1167 // erase background ourselves
1168 DoEraseBackground(*dc);
1169 }
1170 //else: background erased by the user-defined handler
1171
1172
1173 // draw the HTML window contents
1174 dc->SetMapMode(wxMM_TEXT);
1175 dc->SetBackgroundMode(wxTRANSPARENT);
1176 dc->SetLayoutDirection(GetLayoutDirection());
1177
1178 wxHtmlRenderingInfo rinfo;
1179 wxDefaultHtmlRenderingStyle rstyle;
1180 rinfo.SetSelection(m_selection);
1181 rinfo.SetStyle(&rstyle);
1182 m_Cell->Draw(*dc, 0, 0,
1183 y * wxHTML_SCROLL_STEP + rect.GetTop(),
1184 y * wxHTML_SCROLL_STEP + rect.GetBottom(),
1185 rinfo);
1186
1187 #ifdef DEBUG_HTML_SELECTION
1188 {
1189 int xc, yc, x, y;
1190 wxGetMousePosition(&xc, &yc);
1191 ScreenToClient(&xc, &yc);
1192 CalcUnscrolledPosition(xc, yc, &x, &y);
1193 wxHtmlCell *at = m_Cell->FindCellByPos(x, y);
1194 wxHtmlCell *before =
1195 m_Cell->FindCellByPos(x, y, wxHTML_FIND_NEAREST_BEFORE);
1196 wxHtmlCell *after =
1197 m_Cell->FindCellByPos(x, y, wxHTML_FIND_NEAREST_AFTER);
1198
1199 dc->SetBrush(*wxTRANSPARENT_BRUSH);
1200 dc->SetPen(*wxBLACK_PEN);
1201 if (at)
1202 dc->DrawRectangle(at->GetAbsPos(),
1203 wxSize(at->GetWidth(),at->GetHeight()));
1204 dc->SetPen(*wxGREEN_PEN);
1205 if (before)
1206 dc->DrawRectangle(before->GetAbsPos().x+1, before->GetAbsPos().y+1,
1207 before->GetWidth()-2,before->GetHeight()-2);
1208 dc->SetPen(*wxRED_PEN);
1209 if (after)
1210 dc->DrawRectangle(after->GetAbsPos().x+2, after->GetAbsPos().y+2,
1211 after->GetWidth()-4,after->GetHeight()-4);
1212 }
1213 #endif // DEBUG_HTML_SELECTION
1214
1215 if ( dc != &dcPaint )
1216 {
1217 dc->SetDeviceOrigin(0,0);
1218 dcPaint.Blit(0, rect.GetTop(),
1219 sz.x, rect.GetBottom() - rect.GetTop() + 1,
1220 dc,
1221 0, rect.GetTop());
1222 }
1223 }
1224
1225
1226
1227
1228 void wxHtmlWindow::OnSize(wxSizeEvent& event)
1229 {
1230 event.Skip();
1231
1232 m_backBuffer = wxNullBitmap;
1233
1234 CreateLayout();
1235
1236 // Recompute selection if necessary:
1237 if ( m_selection )
1238 {
1239 m_selection->Set(m_selection->GetFromCell(),
1240 m_selection->GetToCell());
1241 m_selection->ClearFromToCharacterPos();
1242 }
1243
1244 Refresh();
1245 }
1246
1247
1248 void wxHtmlWindow::OnMouseMove(wxMouseEvent& WXUNUSED(event))
1249 {
1250 wxHtmlWindowMouseHelper::HandleMouseMoved();
1251 }
1252
1253 void wxHtmlWindow::OnMouseDown(wxMouseEvent& event)
1254 {
1255 #if wxUSE_CLIPBOARD
1256 if ( event.LeftDown() && IsSelectionEnabled() )
1257 {
1258 const long TRIPLECLICK_LEN = 200; // 0.2 sec after doubleclick
1259 if ( wxGetLocalTimeMillis() - m_lastDoubleClick <= TRIPLECLICK_LEN )
1260 {
1261 SelectLine(CalcUnscrolledPosition(event.GetPosition()));
1262
1263 (void) CopySelection();
1264 }
1265 else
1266 {
1267 m_makingSelection = true;
1268
1269 if ( m_selection )
1270 {
1271 wxDELETE(m_selection);
1272 Refresh();
1273 }
1274 m_tmpSelFromPos = CalcUnscrolledPosition(event.GetPosition());
1275 m_tmpSelFromCell = NULL;
1276
1277 CaptureMouse();
1278 }
1279 }
1280 #endif // wxUSE_CLIPBOARD
1281
1282 // in any case, let the default handler set focus to this window
1283 event.Skip();
1284 }
1285
1286 void wxHtmlWindow::OnMouseUp(wxMouseEvent& event)
1287 {
1288 #if wxUSE_CLIPBOARD
1289 if ( m_makingSelection )
1290 {
1291 ReleaseMouse();
1292 m_makingSelection = false;
1293
1294 // if m_selection=NULL, the user didn't move the mouse far enough from
1295 // starting point and the mouse up event is part of a click, the user
1296 // is not selecting text:
1297 if ( m_selection )
1298 {
1299 CopySelection(Primary);
1300
1301 // we don't want mouse up event that ended selecting to be
1302 // handled as mouse click and e.g. follow hyperlink:
1303 return;
1304 }
1305 }
1306 #endif // wxUSE_CLIPBOARD
1307
1308 wxPoint pos = CalcUnscrolledPosition(event.GetPosition());
1309 if ( !wxHtmlWindowMouseHelper::HandleMouseClick(m_Cell, pos, event) )
1310 event.Skip();
1311 }
1312
1313 #if wxUSE_CLIPBOARD
1314 void wxHtmlWindow::OnMouseCaptureLost(wxMouseCaptureLostEvent& WXUNUSED(event))
1315 {
1316 if ( !m_makingSelection )
1317 return;
1318
1319 // discard the selecting operation
1320 m_makingSelection = false;
1321 wxDELETE(m_selection);
1322 m_tmpSelFromCell = NULL;
1323 Refresh();
1324 }
1325 #endif // wxUSE_CLIPBOARD
1326
1327
1328 void wxHtmlWindow::OnInternalIdle()
1329 {
1330 wxWindow::OnInternalIdle();
1331
1332 if (m_Cell != NULL && DidMouseMove())
1333 {
1334 #ifdef DEBUG_HTML_SELECTION
1335 Refresh();
1336 #endif
1337 int xc, yc, x, y;
1338 wxGetMousePosition(&xc, &yc);
1339 ScreenToClient(&xc, &yc);
1340 CalcUnscrolledPosition(xc, yc, &x, &y);
1341
1342 wxHtmlCell *cell = m_Cell->FindCellByPos(x, y);
1343
1344 // handle selection update:
1345 if ( m_makingSelection )
1346 {
1347 if ( !m_tmpSelFromCell )
1348 m_tmpSelFromCell = m_Cell->FindCellByPos(
1349 m_tmpSelFromPos.x,m_tmpSelFromPos.y);
1350
1351 // NB: a trick - we adjust selFromPos to be upper left or bottom
1352 // right corner of the first cell of the selection depending
1353 // on whether the mouse is moving to the right or to the left.
1354 // This gives us more "natural" behaviour when selecting
1355 // a line (specifically, first cell of the next line is not
1356 // included if you drag selection from left to right over
1357 // entire line):
1358 wxPoint dirFromPos;
1359 if ( !m_tmpSelFromCell )
1360 {
1361 dirFromPos = m_tmpSelFromPos;
1362 }
1363 else
1364 {
1365 dirFromPos = m_tmpSelFromCell->GetAbsPos();
1366 if ( x < m_tmpSelFromPos.x )
1367 {
1368 dirFromPos.x += m_tmpSelFromCell->GetWidth();
1369 dirFromPos.y += m_tmpSelFromCell->GetHeight();
1370 }
1371 }
1372 bool goingDown = dirFromPos.y < y ||
1373 (dirFromPos.y == y && dirFromPos.x < x);
1374
1375 // determine selection span:
1376 if ( /*still*/ !m_tmpSelFromCell )
1377 {
1378 if (goingDown)
1379 {
1380 m_tmpSelFromCell = m_Cell->FindCellByPos(
1381 m_tmpSelFromPos.x,m_tmpSelFromPos.y,
1382 wxHTML_FIND_NEAREST_AFTER);
1383 if (!m_tmpSelFromCell)
1384 m_tmpSelFromCell = m_Cell->GetFirstTerminal();
1385 }
1386 else
1387 {
1388 m_tmpSelFromCell = m_Cell->FindCellByPos(
1389 m_tmpSelFromPos.x,m_tmpSelFromPos.y,
1390 wxHTML_FIND_NEAREST_BEFORE);
1391 if (!m_tmpSelFromCell)
1392 m_tmpSelFromCell = m_Cell->GetLastTerminal();
1393 }
1394 }
1395
1396 wxHtmlCell *selcell = cell;
1397 if (!selcell)
1398 {
1399 if (goingDown)
1400 {
1401 selcell = m_Cell->FindCellByPos(x, y,
1402 wxHTML_FIND_NEAREST_BEFORE);
1403 if (!selcell)
1404 selcell = m_Cell->GetLastTerminal();
1405 }
1406 else
1407 {
1408 selcell = m_Cell->FindCellByPos(x, y,
1409 wxHTML_FIND_NEAREST_AFTER);
1410 if (!selcell)
1411 selcell = m_Cell->GetFirstTerminal();
1412 }
1413 }
1414
1415 // NB: it may *rarely* happen that the code above didn't find one
1416 // of the cells, e.g. if wxHtmlWindow doesn't contain any
1417 // visible cells.
1418 if ( selcell && m_tmpSelFromCell )
1419 {
1420 if ( !m_selection )
1421 {
1422 // start selecting only if mouse movement was big enough
1423 // (otherwise it was meant as mouse click, not selection):
1424 const int PRECISION = 2;
1425 wxPoint diff = m_tmpSelFromPos - wxPoint(x,y);
1426 if (abs(diff.x) > PRECISION || abs(diff.y) > PRECISION)
1427 {
1428 m_selection = new wxHtmlSelection();
1429 }
1430 }
1431 if ( m_selection )
1432 {
1433 if ( m_tmpSelFromCell->IsBefore(selcell) )
1434 {
1435 m_selection->Set(m_tmpSelFromPos, m_tmpSelFromCell,
1436 wxPoint(x,y), selcell);
1437 }
1438 else
1439 {
1440 m_selection->Set(wxPoint(x,y), selcell,
1441 m_tmpSelFromPos, m_tmpSelFromCell);
1442 }
1443 m_selection->ClearFromToCharacterPos();
1444 Refresh();
1445 }
1446 }
1447 }
1448
1449 // handle cursor and status bar text changes:
1450
1451 // NB: because we're passing in 'cell' and not 'm_Cell' (so that the
1452 // leaf cell lookup isn't done twice), we need to adjust the
1453 // position for the new root:
1454 wxPoint posInCell(x, y);
1455 if (cell)
1456 posInCell -= cell->GetAbsPos();
1457 wxHtmlWindowMouseHelper::HandleIdle(cell, posInCell);
1458 }
1459 }
1460
1461 #if wxUSE_CLIPBOARD
1462 void wxHtmlWindow::StopAutoScrolling()
1463 {
1464 if ( m_timerAutoScroll )
1465 {
1466 wxDELETE(m_timerAutoScroll);
1467 }
1468 }
1469
1470 void wxHtmlWindow::OnMouseEnter(wxMouseEvent& event)
1471 {
1472 StopAutoScrolling();
1473 event.Skip();
1474 }
1475
1476 void wxHtmlWindow::OnMouseLeave(wxMouseEvent& event)
1477 {
1478 // don't prevent the usual processing of the event from taking place
1479 event.Skip();
1480
1481 // when a captured mouse leave a scrolled window we start generate
1482 // scrolling events to allow, for example, extending selection beyond the
1483 // visible area in some controls
1484 if ( wxWindow::GetCapture() == this )
1485 {
1486 // where is the mouse leaving?
1487 int pos, orient;
1488 wxPoint pt = event.GetPosition();
1489 if ( pt.x < 0 )
1490 {
1491 orient = wxHORIZONTAL;
1492 pos = 0;
1493 }
1494 else if ( pt.y < 0 )
1495 {
1496 orient = wxVERTICAL;
1497 pos = 0;
1498 }
1499 else // we're lower or to the right of the window
1500 {
1501 wxSize size = GetClientSize();
1502 if ( pt.x > size.x )
1503 {
1504 orient = wxHORIZONTAL;
1505 pos = GetVirtualSize().x / wxHTML_SCROLL_STEP;
1506 }
1507 else if ( pt.y > size.y )
1508 {
1509 orient = wxVERTICAL;
1510 pos = GetVirtualSize().y / wxHTML_SCROLL_STEP;
1511 }
1512 else // this should be impossible
1513 {
1514 // but seems to happen sometimes under wxMSW - maybe it's a bug
1515 // there but for now just ignore it
1516
1517 //wxFAIL_MSG( wxT("can't understand where has mouse gone") );
1518
1519 return;
1520 }
1521 }
1522
1523 // only start the auto scroll timer if the window can be scrolled in
1524 // this direction
1525 if ( !HasScrollbar(orient) )
1526 return;
1527
1528 delete m_timerAutoScroll;
1529 m_timerAutoScroll = new wxHtmlWinAutoScrollTimer
1530 (
1531 this,
1532 pos == 0 ? wxEVT_SCROLLWIN_LINEUP
1533 : wxEVT_SCROLLWIN_LINEDOWN,
1534 pos,
1535 orient
1536 );
1537 m_timerAutoScroll->Start(50); // FIXME: make configurable
1538 }
1539 }
1540
1541 void wxHtmlWindow::OnKeyUp(wxKeyEvent& event)
1542 {
1543 if ( IsSelectionEnabled() &&
1544 (event.GetKeyCode() == 'C' && event.CmdDown()) )
1545 {
1546 wxClipboardTextEvent evt(wxEVT_TEXT_COPY, GetId());
1547
1548 evt.SetEventObject(this);
1549
1550 GetEventHandler()->ProcessEvent(evt);
1551 }
1552 else
1553 {
1554 event.Skip();
1555 }
1556 }
1557
1558 void wxHtmlWindow::OnCopy(wxCommandEvent& WXUNUSED(event))
1559 {
1560 (void) CopySelection();
1561 }
1562
1563 void wxHtmlWindow::OnClipboardEvent(wxClipboardTextEvent& WXUNUSED(event))
1564 {
1565 (void) CopySelection();
1566 }
1567
1568 void wxHtmlWindow::OnDoubleClick(wxMouseEvent& event)
1569 {
1570 // select word under cursor:
1571 if ( IsSelectionEnabled() )
1572 {
1573 SelectWord(CalcUnscrolledPosition(event.GetPosition()));
1574
1575 (void) CopySelection(Primary);
1576
1577 m_lastDoubleClick = wxGetLocalTimeMillis();
1578 }
1579 else
1580 event.Skip();
1581 }
1582
1583 void wxHtmlWindow::SelectWord(const wxPoint& pos)
1584 {
1585 if ( m_Cell )
1586 {
1587 wxHtmlCell *cell = m_Cell->FindCellByPos(pos.x, pos.y);
1588 if ( cell )
1589 {
1590 delete m_selection;
1591 m_selection = new wxHtmlSelection();
1592 m_selection->Set(cell, cell);
1593 RefreshRect(wxRect(CalcScrolledPosition(cell->GetAbsPos()),
1594 wxSize(cell->GetWidth(), cell->GetHeight())));
1595 }
1596 }
1597 }
1598
1599 void wxHtmlWindow::SelectLine(const wxPoint& pos)
1600 {
1601 if ( m_Cell )
1602 {
1603 wxHtmlCell *cell = m_Cell->FindCellByPos(pos.x, pos.y);
1604 if ( cell )
1605 {
1606 // We use following heuristic to find a "line": let the line be all
1607 // cells in same container as the cell under mouse cursor that are
1608 // neither completely above nor completely below the clicked cell
1609 // (i.e. are likely to be words positioned on same line of text).
1610
1611 int y1 = cell->GetAbsPos().y;
1612 int y2 = y1 + cell->GetHeight();
1613 int y;
1614 const wxHtmlCell *c;
1615 const wxHtmlCell *before = NULL;
1616 const wxHtmlCell *after = NULL;
1617
1618 // find last cell of line:
1619 for ( c = cell->GetNext(); c; c = c->GetNext())
1620 {
1621 y = c->GetAbsPos().y;
1622 if ( y + c->GetHeight() > y1 && y < y2 )
1623 after = c;
1624 else
1625 break;
1626 }
1627 if ( !after )
1628 after = cell;
1629
1630 // find first cell of line:
1631 for ( c = cell->GetParent()->GetFirstChild();
1632 c && c != cell; c = c->GetNext())
1633 {
1634 y = c->GetAbsPos().y;
1635 if ( y + c->GetHeight() > y1 && y < y2 )
1636 {
1637 if ( ! before )
1638 before = c;
1639 }
1640 else
1641 before = NULL;
1642 }
1643 if ( !before )
1644 before = cell;
1645
1646 delete m_selection;
1647 m_selection = new wxHtmlSelection();
1648 m_selection->Set(before, after);
1649
1650 Refresh();
1651 }
1652 }
1653 }
1654
1655 void wxHtmlWindow::SelectAll()
1656 {
1657 if ( m_Cell )
1658 {
1659 delete m_selection;
1660 m_selection = new wxHtmlSelection();
1661 m_selection->Set(m_Cell->GetFirstTerminal(), m_Cell->GetLastTerminal());
1662 Refresh();
1663 }
1664 }
1665
1666 #endif // wxUSE_CLIPBOARD
1667
1668
1669
1670 IMPLEMENT_ABSTRACT_CLASS(wxHtmlProcessor,wxObject)
1671
1672 wxBEGIN_PROPERTIES_TABLE(wxHtmlWindow)
1673 /*
1674 TODO PROPERTIES
1675 style , wxHW_SCROLLBAR_AUTO
1676 borders , (dimension)
1677 url , string
1678 htmlcode , string
1679 */
1680 wxEND_PROPERTIES_TABLE()
1681
1682 wxBEGIN_HANDLERS_TABLE(wxHtmlWindow)
1683 wxEND_HANDLERS_TABLE()
1684
1685 wxCONSTRUCTOR_5( wxHtmlWindow , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size , long , WindowStyle )
1686
1687 wxIMPLEMENT_DYNAMIC_CLASS_XTI(wxHtmlWindow, wxScrolledWindow,"wx/html/htmlwin.h")
1688
1689 BEGIN_EVENT_TABLE(wxHtmlWindow, wxScrolledWindow)
1690 EVT_SIZE(wxHtmlWindow::OnSize)
1691 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseDown)
1692 EVT_LEFT_UP(wxHtmlWindow::OnMouseUp)
1693 EVT_RIGHT_UP(wxHtmlWindow::OnMouseUp)
1694 EVT_MOTION(wxHtmlWindow::OnMouseMove)
1695 EVT_PAINT(wxHtmlWindow::OnPaint)
1696 EVT_ERASE_BACKGROUND(wxHtmlWindow::OnEraseBackground)
1697 #if wxUSE_CLIPBOARD
1698 EVT_LEFT_DCLICK(wxHtmlWindow::OnDoubleClick)
1699 EVT_ENTER_WINDOW(wxHtmlWindow::OnMouseEnter)
1700 EVT_LEAVE_WINDOW(wxHtmlWindow::OnMouseLeave)
1701 EVT_MOUSE_CAPTURE_LOST(wxHtmlWindow::OnMouseCaptureLost)
1702 EVT_KEY_UP(wxHtmlWindow::OnKeyUp)
1703 EVT_MENU(wxID_COPY, wxHtmlWindow::OnCopy)
1704 EVT_TEXT_COPY(wxID_ANY, wxHtmlWindow::OnClipboardEvent)
1705 #endif // wxUSE_CLIPBOARD
1706 END_EVENT_TABLE()
1707
1708 //-----------------------------------------------------------------------------
1709 // wxHtmlWindowInterface implementation in wxHtmlWindow
1710 //-----------------------------------------------------------------------------
1711
1712 void wxHtmlWindow::SetHTMLWindowTitle(const wxString& title)
1713 {
1714 OnSetTitle(title);
1715 }
1716
1717 void wxHtmlWindow::OnHTMLLinkClicked(const wxHtmlLinkInfo& link)
1718 {
1719 OnLinkClicked(link);
1720 }
1721
1722 wxHtmlOpeningStatus wxHtmlWindow::OnHTMLOpeningURL(wxHtmlURLType type,
1723 const wxString& url,
1724 wxString *redirect) const
1725 {
1726 return OnOpeningURL(type, url, redirect);
1727 }
1728
1729 wxPoint wxHtmlWindow::HTMLCoordsToWindow(wxHtmlCell *WXUNUSED(cell),
1730 const wxPoint& pos) const
1731 {
1732 return CalcScrolledPosition(pos);
1733 }
1734
1735 wxWindow* wxHtmlWindow::GetHTMLWindow()
1736 {
1737 return this;
1738 }
1739
1740 wxColour wxHtmlWindow::GetHTMLBackgroundColour() const
1741 {
1742 return GetBackgroundColour();
1743 }
1744
1745 void wxHtmlWindow::SetHTMLBackgroundColour(const wxColour& clr)
1746 {
1747 SetBackgroundColour(clr);
1748 }
1749
1750 void wxHtmlWindow::SetHTMLBackgroundImage(const wxBitmap& bmpBg)
1751 {
1752 SetBackgroundImage(bmpBg);
1753 }
1754
1755 void wxHtmlWindow::SetHTMLStatusText(const wxString& text)
1756 {
1757 #if wxUSE_STATUSBAR
1758 if (m_RelatedStatusBarIndex != -1)
1759 {
1760 if (m_RelatedStatusBar)
1761 {
1762 m_RelatedStatusBar->SetStatusText(text, m_RelatedStatusBarIndex);
1763 }
1764 else if (m_RelatedFrame)
1765 {
1766 m_RelatedFrame->SetStatusText(text, m_RelatedStatusBarIndex);
1767 }
1768 }
1769 #else
1770 wxUnusedVar(text);
1771 #endif // wxUSE_STATUSBAR
1772 }
1773
1774 /*static*/
1775 wxCursor wxHtmlWindow::GetDefaultHTMLCursor(HTMLCursor type)
1776 {
1777 switch (type)
1778 {
1779 case HTMLCursor_Link:
1780 if ( !ms_cursorLink )
1781 ms_cursorLink = new wxCursor(wxCURSOR_HAND);
1782 return *ms_cursorLink;
1783
1784 case HTMLCursor_Text:
1785 if ( !ms_cursorText )
1786 ms_cursorText = new wxCursor(wxCURSOR_IBEAM);
1787 return *ms_cursorText;
1788
1789 case HTMLCursor_Default:
1790 default:
1791 return *wxSTANDARD_CURSOR;
1792 }
1793 }
1794
1795 wxCursor wxHtmlWindow::GetHTMLCursor(HTMLCursor type) const
1796 {
1797 return GetDefaultHTMLCursor(type);
1798 }
1799
1800
1801 //-----------------------------------------------------------------------------
1802 // wxHtmlWinModule
1803 //-----------------------------------------------------------------------------
1804
1805 // A module to allow initialization/cleanup
1806 // without calling these functions from app.cpp or from
1807 // the user's application.
1808
1809 class wxHtmlWinModule: public wxModule
1810 {
1811 DECLARE_DYNAMIC_CLASS(wxHtmlWinModule)
1812 public:
1813 wxHtmlWinModule() : wxModule() {}
1814 bool OnInit() { return true; }
1815 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
1816 };
1817
1818 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule, wxModule)
1819
1820
1821 // This hack forces the linker to always link in m_* files
1822 // (wxHTML doesn't work without handlers from these files)
1823 #include "wx/html/forcelnk.h"
1824 FORCE_WXHTML_MODULES()
1825
1826 #endif // wxUSE_HTML