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