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