doubleclick selects word
[wxWidgets.git] / src / html / htmlwin.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: htmlwin.cpp
3 // Purpose: wxHtmlWindow class for parsing & displaying HTML (implementation)
4 // Author: Vaclav Slavik
5 // RCS-ID: $Id$
6 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows Licence
8 /////////////////////////////////////////////////////////////////////////////
9
10
11 #ifdef __GNUG__
12 #pragma implementation "htmlwin.h"
13 #pragma implementation "htmlproc.h"
14 #endif
15
16 #include "wx/wxprec.h"
17
18 #include "wx/defs.h"
19 #if wxUSE_HTML && wxUSE_STREAMS
20
21 #ifdef __BORLANDC__
22 #pragma hdrstop
23 #endif
24
25 #ifndef WXPRECOMP
26 #include "wx/log.h"
27 #include "wx/intl.h"
28 #include "wx/dcclient.h"
29 #include "wx/frame.h"
30 #endif
31
32 #include "wx/html/htmlwin.h"
33 #include "wx/html/htmlproc.h"
34 #include "wx/list.h"
35 #include "wx/clipbrd.h"
36 #include "wx/timer.h"
37 #include "wx/dcmemory.h"
38
39 #include "wx/arrimpl.cpp"
40 #include "wx/listimpl.cpp"
41
42
43
44 #if wxUSE_CLIPBOARD
45 // ----------------------------------------------------------------------------
46 // wxHtmlWinAutoScrollTimer: the timer used to generate a stream of scroll
47 // events when a captured mouse is held outside the window
48 // ----------------------------------------------------------------------------
49
50 class wxHtmlWinAutoScrollTimer : public wxTimer
51 {
52 public:
53 wxHtmlWinAutoScrollTimer(wxScrolledWindow *win,
54 wxEventType eventTypeToSend,
55 int pos, int orient)
56 {
57 m_win = win;
58 m_eventType = eventTypeToSend;
59 m_pos = pos;
60 m_orient = orient;
61 }
62
63 virtual void Notify();
64
65 private:
66 wxScrolledWindow *m_win;
67 wxEventType m_eventType;
68 int m_pos,
69 m_orient;
70
71 DECLARE_NO_COPY_CLASS(wxHtmlWinAutoScrollTimer)
72 };
73
74 void wxHtmlWinAutoScrollTimer::Notify()
75 {
76 // only do all this as long as the window is capturing the mouse
77 if ( wxWindow::GetCapture() != m_win )
78 {
79 Stop();
80 }
81 else // we still capture the mouse, continue generating events
82 {
83 // first scroll the window if we are allowed to do it
84 wxScrollWinEvent event1(m_eventType, m_pos, m_orient);
85 event1.SetEventObject(m_win);
86 if ( m_win->GetEventHandler()->ProcessEvent(event1) )
87 {
88 // and then send a pseudo mouse-move event to refresh the selection
89 wxMouseEvent event2(wxEVT_MOTION);
90 wxGetMousePosition(&event2.m_x, &event2.m_y);
91
92 // the mouse event coordinates should be client, not screen as
93 // returned by wxGetMousePosition
94 wxWindow *parentTop = m_win;
95 while ( parentTop->GetParent() )
96 parentTop = parentTop->GetParent();
97 wxPoint ptOrig = parentTop->GetPosition();
98 event2.m_x -= ptOrig.x;
99 event2.m_y -= ptOrig.y;
100
101 event2.SetEventObject(m_win);
102
103 // FIXME: we don't fill in the other members - ok?
104 m_win->GetEventHandler()->ProcessEvent(event2);
105 }
106 else // can't scroll further, stop
107 {
108 Stop();
109 }
110 }
111 }
112 #endif
113
114
115
116 //-----------------------------------------------------------------------------
117 // wxHtmlHistoryItem
118 //-----------------------------------------------------------------------------
119
120 // item of history list
121 class WXDLLEXPORT wxHtmlHistoryItem
122 {
123 public:
124 wxHtmlHistoryItem(const wxString& p, const wxString& a) {m_Page = p, m_Anchor = a, m_Pos = 0;}
125 int GetPos() const {return m_Pos;}
126 void SetPos(int p) {m_Pos = p;}
127 const wxString& GetPage() const {return m_Page;}
128 const wxString& GetAnchor() const {return m_Anchor;}
129
130 private:
131 wxString m_Page;
132 wxString m_Anchor;
133 int m_Pos;
134 };
135
136
137 //-----------------------------------------------------------------------------
138 // our private arrays:
139 //-----------------------------------------------------------------------------
140
141 WX_DECLARE_OBJARRAY(wxHtmlHistoryItem, wxHtmlHistoryArray);
142 WX_DEFINE_OBJARRAY(wxHtmlHistoryArray);
143
144 WX_DECLARE_LIST(wxHtmlProcessor, wxHtmlProcessorList);
145 WX_DEFINE_LIST(wxHtmlProcessorList);
146
147 //-----------------------------------------------------------------------------
148 // wxHtmlWindow
149 //-----------------------------------------------------------------------------
150
151
152 void wxHtmlWindow::Init()
153 {
154 m_tmpMouseMoved = FALSE;
155 m_tmpLastLink = NULL;
156 m_tmpLastCell = NULL;
157 m_tmpCanDrawLocks = 0;
158 m_FS = new wxFileSystem();
159 m_RelatedStatusBar = -1;
160 m_RelatedFrame = NULL;
161 m_TitleFormat = wxT("%s");
162 m_OpenedPage = m_OpenedAnchor = m_OpenedPageTitle = wxEmptyString;
163 m_Cell = NULL;
164 m_Parser = new wxHtmlWinParser(this);
165 m_Parser->SetFS(m_FS);
166 m_HistoryPos = -1;
167 m_HistoryOn = TRUE;
168 m_History = new wxHtmlHistoryArray;
169 m_Processors = NULL;
170 m_Style = 0;
171 SetBorders(10);
172 m_selection = NULL;
173 m_makingSelection = false;
174 #if wxUSE_CLIPBOARD
175 m_timerAutoScroll = NULL;
176 #endif
177 m_backBuffer = NULL;
178 }
179
180 bool wxHtmlWindow::Create(wxWindow *parent, wxWindowID id,
181 const wxPoint& pos, const wxSize& size,
182 long style, const wxString& name)
183 {
184 if (!wxScrolledWindow::Create(parent, id, pos, size,
185 style | wxVSCROLL | wxHSCROLL, name))
186 return FALSE;
187
188 m_Style = style;
189 SetPage(wxT("<html><body></body></html>"));
190 return TRUE;
191 }
192
193
194 wxHtmlWindow::~wxHtmlWindow()
195 {
196 #if wxUSE_CLIPBOARD
197 StopAutoScrolling();
198 #endif
199 HistoryClear();
200
201 if (m_Cell) delete m_Cell;
202
203 delete m_Parser;
204 delete m_FS;
205 delete m_History;
206 delete m_Processors;
207 delete m_backBuffer;
208 }
209
210
211
212 void wxHtmlWindow::SetRelatedFrame(wxFrame* frame, const wxString& format)
213 {
214 m_RelatedFrame = frame;
215 m_TitleFormat = format;
216 }
217
218
219
220 void wxHtmlWindow::SetRelatedStatusBar(int bar)
221 {
222 m_RelatedStatusBar = bar;
223 }
224
225
226
227 void wxHtmlWindow::SetFonts(wxString normal_face, wxString fixed_face, const int *sizes)
228 {
229 wxString op = m_OpenedPage;
230
231 m_Parser->SetFonts(normal_face, fixed_face, sizes);
232 // fonts changed => contents invalid, so reload the page:
233 SetPage(wxT("<html><body></body></html>"));
234 if (!op.IsEmpty()) LoadPage(op);
235 }
236
237
238
239 bool wxHtmlWindow::SetPage(const wxString& source)
240 {
241 wxString newsrc(source);
242
243 wxDELETE(m_selection);
244
245 // pass HTML through registered processors:
246 if (m_Processors || m_GlobalProcessors)
247 {
248 wxHtmlProcessorList::Node *nodeL, *nodeG;
249 int prL, prG;
250
251 nodeL = (m_Processors) ? m_Processors->GetFirst() : NULL;
252 nodeG = (m_GlobalProcessors) ? m_GlobalProcessors->GetFirst() : NULL;
253
254 // VS: there are two lists, global and local, both of them sorted by
255 // priority. Since we have to go through _both_ lists with
256 // decreasing priority, we "merge-sort" the lists on-line by
257 // processing that one of the two heads that has higher priority
258 // in every iteration
259 while (nodeL || nodeG)
260 {
261 prL = (nodeL) ? nodeL->GetData()->GetPriority() : -1;
262 prG = (nodeG) ? nodeG->GetData()->GetPriority() : -1;
263 if (prL > prG)
264 {
265 if (nodeL->GetData()->IsEnabled())
266 newsrc = nodeL->GetData()->Process(newsrc);
267 nodeL = nodeL->GetNext();
268 }
269 else // prL <= prG
270 {
271 if (nodeG->GetData()->IsEnabled())
272 newsrc = nodeG->GetData()->Process(newsrc);
273 nodeG = nodeG->GetNext();
274 }
275 }
276 }
277
278 // ...and run the parser on it:
279 wxClientDC *dc = new wxClientDC(this);
280 dc->SetMapMode(wxMM_TEXT);
281 SetBackgroundColour(wxColour(0xFF, 0xFF, 0xFF));
282 m_OpenedPage = m_OpenedAnchor = m_OpenedPageTitle = wxEmptyString;
283 m_Parser->SetDC(dc);
284 if (m_Cell)
285 {
286 delete m_Cell;
287 m_Cell = NULL;
288 }
289 m_Cell = (wxHtmlContainerCell*) m_Parser->Parse(newsrc);
290 delete dc;
291 m_Cell->SetIndent(m_Borders, wxHTML_INDENT_ALL, wxHTML_UNITS_PIXELS);
292 m_Cell->SetAlignHor(wxHTML_ALIGN_CENTER);
293 CreateLayout();
294 if (m_tmpCanDrawLocks == 0)
295 Refresh();
296 return TRUE;
297 }
298
299 bool wxHtmlWindow::AppendToPage(const wxString& source)
300 {
301 return SetPage(*(GetParser()->GetSource()) + source);
302 }
303
304 bool wxHtmlWindow::LoadPage(const wxString& location)
305 {
306 wxBusyCursor busyCursor;
307
308 wxFSFile *f;
309 bool rt_val;
310 bool needs_refresh = FALSE;
311
312 m_tmpCanDrawLocks++;
313 if (m_HistoryOn && (m_HistoryPos != -1))
314 {
315 // store scroll position into history item:
316 int x, y;
317 GetViewStart(&x, &y);
318 (*m_History)[m_HistoryPos].SetPos(y);
319 }
320
321 if (location[0] == wxT('#'))
322 {
323 // local anchor:
324 wxString anch = location.Mid(1) /*1 to end*/;
325 m_tmpCanDrawLocks--;
326 rt_val = ScrollToAnchor(anch);
327 m_tmpCanDrawLocks++;
328 }
329 else if (location.Find(wxT('#')) != wxNOT_FOUND && location.BeforeFirst(wxT('#')) == m_OpenedPage)
330 {
331 wxString anch = location.AfterFirst(wxT('#'));
332 m_tmpCanDrawLocks--;
333 rt_val = ScrollToAnchor(anch);
334 m_tmpCanDrawLocks++;
335 }
336 else if (location.Find(wxT('#')) != wxNOT_FOUND &&
337 (m_FS->GetPath() + location.BeforeFirst(wxT('#'))) == m_OpenedPage)
338 {
339 wxString anch = location.AfterFirst(wxT('#'));
340 m_tmpCanDrawLocks--;
341 rt_val = ScrollToAnchor(anch);
342 m_tmpCanDrawLocks++;
343 }
344
345 else
346 {
347 needs_refresh = TRUE;
348 // load&display it:
349 if (m_RelatedStatusBar != -1)
350 {
351 m_RelatedFrame->SetStatusText(_("Connecting..."), m_RelatedStatusBar);
352 Refresh(FALSE);
353 }
354
355 f = m_Parser->OpenURL(wxHTML_URL_PAGE, location);
356
357 // try to interpret 'location' as filename instead of URL:
358 if (f == NULL)
359 {
360 wxFileName fn(location);
361 wxString location2 = wxFileSystem::FileNameToURL(fn);
362 f = m_Parser->OpenURL(wxHTML_URL_PAGE, location2);
363 }
364
365 if (f == NULL)
366 {
367 wxLogError(_("Unable to open requested HTML document: %s"), location.c_str());
368 m_tmpCanDrawLocks--;
369 return FALSE;
370 }
371
372 else
373 {
374 wxNode *node;
375 wxString src = wxEmptyString;
376
377 if (m_RelatedStatusBar != -1)
378 {
379 wxString msg = _("Loading : ") + location;
380 m_RelatedFrame->SetStatusText(msg, m_RelatedStatusBar);
381 Refresh(FALSE);
382 }
383
384 node = m_Filters.GetFirst();
385 while (node)
386 {
387 wxHtmlFilter *h = (wxHtmlFilter*) node->GetData();
388 if (h->CanRead(*f))
389 {
390 src = h->ReadFile(*f);
391 break;
392 }
393 node = node->GetNext();
394 }
395 if (src == wxEmptyString)
396 {
397 if (m_DefaultFilter == NULL) m_DefaultFilter = GetDefaultFilter();
398 src = m_DefaultFilter->ReadFile(*f);
399 }
400
401 m_FS->ChangePathTo(f->GetLocation());
402 rt_val = SetPage(src);
403 m_OpenedPage = f->GetLocation();
404 if (f->GetAnchor() != wxEmptyString)
405 {
406 ScrollToAnchor(f->GetAnchor());
407 }
408
409 delete f;
410
411 if (m_RelatedStatusBar != -1) m_RelatedFrame->SetStatusText(_("Done"), m_RelatedStatusBar);
412 }
413 }
414
415 if (m_HistoryOn) // add this page to history there:
416 {
417 int c = m_History->GetCount() - (m_HistoryPos + 1);
418
419 if (m_HistoryPos < 0 ||
420 (*m_History)[m_HistoryPos].GetPage() != m_OpenedPage ||
421 (*m_History)[m_HistoryPos].GetAnchor() != m_OpenedAnchor)
422 {
423 m_HistoryPos++;
424 for (int i = 0; i < c; i++)
425 m_History->RemoveAt(m_HistoryPos);
426 m_History->Add(new wxHtmlHistoryItem(m_OpenedPage, m_OpenedAnchor));
427 }
428 }
429
430 if (m_OpenedPageTitle == wxEmptyString)
431 OnSetTitle(wxFileNameFromPath(m_OpenedPage));
432
433 if (needs_refresh)
434 {
435 m_tmpCanDrawLocks--;
436 Refresh();
437 }
438 else
439 m_tmpCanDrawLocks--;
440
441 return rt_val;
442 }
443
444
445 bool wxHtmlWindow::LoadFile(const wxFileName& filename)
446 {
447 wxString url = wxFileSystem::FileNameToURL(filename);
448 return LoadPage(url);
449 }
450
451
452 bool wxHtmlWindow::ScrollToAnchor(const wxString& anchor)
453 {
454 const wxHtmlCell *c = m_Cell->Find(wxHTML_COND_ISANCHOR, &anchor);
455 if (!c)
456 {
457 wxLogWarning(_("HTML anchor %s does not exist."), anchor.c_str());
458 return FALSE;
459 }
460 else
461 {
462 int y;
463
464 for (y = 0; c != NULL; c = c->GetParent()) y += c->GetPosY();
465 Scroll(-1, y / wxHTML_SCROLL_STEP);
466 m_OpenedAnchor = anchor;
467 return TRUE;
468 }
469 }
470
471
472 void wxHtmlWindow::OnSetTitle(const wxString& title)
473 {
474 if (m_RelatedFrame)
475 {
476 wxString tit;
477 tit.Printf(m_TitleFormat, title.c_str());
478 m_RelatedFrame->SetTitle(tit);
479 }
480 m_OpenedPageTitle = title;
481 }
482
483
484
485
486
487 void wxHtmlWindow::CreateLayout()
488 {
489 int ClientWidth, ClientHeight;
490
491 if (!m_Cell) return;
492
493 if (m_Style & wxHW_SCROLLBAR_NEVER)
494 {
495 SetScrollbars(wxHTML_SCROLL_STEP, 1, m_Cell->GetWidth() / wxHTML_SCROLL_STEP, 0); // always off
496 GetClientSize(&ClientWidth, &ClientHeight);
497 m_Cell->Layout(ClientWidth);
498 }
499
500 else {
501 GetClientSize(&ClientWidth, &ClientHeight);
502 m_Cell->Layout(ClientWidth);
503 if (ClientHeight < m_Cell->GetHeight() + GetCharHeight())
504 {
505 SetScrollbars(
506 wxHTML_SCROLL_STEP, wxHTML_SCROLL_STEP,
507 m_Cell->GetWidth() / wxHTML_SCROLL_STEP,
508 (m_Cell->GetHeight() + GetCharHeight()) / wxHTML_SCROLL_STEP
509 /*cheat: top-level frag is always container*/);
510 }
511 else /* we fit into window, no need for scrollbars */
512 {
513 SetScrollbars(wxHTML_SCROLL_STEP, 1, m_Cell->GetWidth() / wxHTML_SCROLL_STEP, 0); // disable...
514 GetClientSize(&ClientWidth, &ClientHeight);
515 m_Cell->Layout(ClientWidth); // ...and relayout
516 }
517 }
518 }
519
520
521
522 void wxHtmlWindow::ReadCustomization(wxConfigBase *cfg, wxString path)
523 {
524 wxString oldpath;
525 wxString tmp;
526 int p_fontsizes[7];
527 wxString p_fff, p_ffn;
528
529 if (path != wxEmptyString)
530 {
531 oldpath = cfg->GetPath();
532 cfg->SetPath(path);
533 }
534
535 m_Borders = cfg->Read(wxT("wxHtmlWindow/Borders"), m_Borders);
536 p_fff = cfg->Read(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser->m_FontFaceFixed);
537 p_ffn = cfg->Read(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser->m_FontFaceNormal);
538 for (int i = 0; i < 7; i++)
539 {
540 tmp.Printf(wxT("wxHtmlWindow/FontsSize%i"), i);
541 p_fontsizes[i] = cfg->Read(tmp, m_Parser->m_FontsSizes[i]);
542 }
543 SetFonts(p_ffn, p_fff, p_fontsizes);
544
545 if (path != wxEmptyString)
546 cfg->SetPath(oldpath);
547 }
548
549
550
551 void wxHtmlWindow::WriteCustomization(wxConfigBase *cfg, wxString path)
552 {
553 wxString oldpath;
554 wxString tmp;
555
556 if (path != wxEmptyString)
557 {
558 oldpath = cfg->GetPath();
559 cfg->SetPath(path);
560 }
561
562 cfg->Write(wxT("wxHtmlWindow/Borders"), (long) m_Borders);
563 cfg->Write(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser->m_FontFaceFixed);
564 cfg->Write(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser->m_FontFaceNormal);
565 for (int i = 0; i < 7; i++)
566 {
567 tmp.Printf(wxT("wxHtmlWindow/FontsSize%i"), i);
568 cfg->Write(tmp, (long) m_Parser->m_FontsSizes[i]);
569 }
570
571 if (path != wxEmptyString)
572 cfg->SetPath(oldpath);
573 }
574
575
576
577 bool wxHtmlWindow::HistoryBack()
578 {
579 wxString a, l;
580
581 if (m_HistoryPos < 1) return FALSE;
582
583 // store scroll position into history item:
584 int x, y;
585 GetViewStart(&x, &y);
586 (*m_History)[m_HistoryPos].SetPos(y);
587
588 // go to previous position:
589 m_HistoryPos--;
590
591 l = (*m_History)[m_HistoryPos].GetPage();
592 a = (*m_History)[m_HistoryPos].GetAnchor();
593 m_HistoryOn = FALSE;
594 m_tmpCanDrawLocks++;
595 if (a == wxEmptyString) LoadPage(l);
596 else LoadPage(l + wxT("#") + a);
597 m_HistoryOn = TRUE;
598 m_tmpCanDrawLocks--;
599 Scroll(0, (*m_History)[m_HistoryPos].GetPos());
600 Refresh();
601 return TRUE;
602 }
603
604 bool wxHtmlWindow::HistoryCanBack()
605 {
606 if (m_HistoryPos < 1) return FALSE;
607 return TRUE ;
608 }
609
610
611 bool wxHtmlWindow::HistoryForward()
612 {
613 wxString a, l;
614
615 if (m_HistoryPos == -1) return FALSE;
616 if (m_HistoryPos >= (int)m_History->GetCount() - 1)return FALSE;
617
618 m_OpenedPage = wxEmptyString; // this will disable adding new entry into history in LoadPage()
619
620 m_HistoryPos++;
621 l = (*m_History)[m_HistoryPos].GetPage();
622 a = (*m_History)[m_HistoryPos].GetAnchor();
623 m_HistoryOn = FALSE;
624 m_tmpCanDrawLocks++;
625 if (a == wxEmptyString) LoadPage(l);
626 else LoadPage(l + wxT("#") + a);
627 m_HistoryOn = TRUE;
628 m_tmpCanDrawLocks--;
629 Scroll(0, (*m_History)[m_HistoryPos].GetPos());
630 Refresh();
631 return TRUE;
632 }
633
634 bool wxHtmlWindow::HistoryCanForward()
635 {
636 if (m_HistoryPos == -1) return FALSE;
637 if (m_HistoryPos >= (int)m_History->GetCount() - 1)return FALSE;
638 return TRUE ;
639 }
640
641
642 void wxHtmlWindow::HistoryClear()
643 {
644 m_History->Empty();
645 m_HistoryPos = -1;
646 }
647
648 void wxHtmlWindow::AddProcessor(wxHtmlProcessor *processor)
649 {
650 if (!m_Processors)
651 {
652 m_Processors = new wxHtmlProcessorList;
653 m_Processors->DeleteContents(TRUE);
654 }
655 wxHtmlProcessorList::Node *node;
656
657 for (node = m_Processors->GetFirst(); node; node = node->GetNext())
658 {
659 if (processor->GetPriority() > node->GetData()->GetPriority())
660 {
661 m_Processors->Insert(node, processor);
662 return;
663 }
664 }
665 m_Processors->Append(processor);
666 }
667
668 /*static */ void wxHtmlWindow::AddGlobalProcessor(wxHtmlProcessor *processor)
669 {
670 if (!m_GlobalProcessors)
671 {
672 m_GlobalProcessors = new wxHtmlProcessorList;
673 m_GlobalProcessors->DeleteContents(TRUE);
674 }
675 wxHtmlProcessorList::Node *node;
676
677 for (node = m_GlobalProcessors->GetFirst(); node; node = node->GetNext())
678 {
679 if (processor->GetPriority() > node->GetData()->GetPriority())
680 {
681 m_GlobalProcessors->Insert(node, processor);
682 return;
683 }
684 }
685 m_GlobalProcessors->Append(processor);
686 }
687
688
689
690 wxList wxHtmlWindow::m_Filters;
691 wxHtmlFilter *wxHtmlWindow::m_DefaultFilter = NULL;
692 wxCursor *wxHtmlWindow::s_cur_hand = NULL;
693 wxCursor *wxHtmlWindow::s_cur_arrow = NULL;
694 wxHtmlProcessorList *wxHtmlWindow::m_GlobalProcessors = NULL;
695
696 void wxHtmlWindow::CleanUpStatics()
697 {
698 wxDELETE(m_DefaultFilter);
699 m_Filters.DeleteContents(TRUE);
700 m_Filters.Clear();
701 wxDELETE(m_GlobalProcessors);
702 wxDELETE(s_cur_hand);
703 wxDELETE(s_cur_arrow);
704 }
705
706
707
708 void wxHtmlWindow::AddFilter(wxHtmlFilter *filter)
709 {
710 m_Filters.Append(filter);
711 }
712
713
714 bool wxHtmlWindow::IsSelectionEnabled() const
715 {
716 #if wxUSE_CLIPBOARD
717 return !(m_Style & wxHW_NO_SELECTION);
718 #else
719 return false;
720 #endif
721 }
722
723
724 #if wxUSE_CLIPBOARD
725 wxString wxHtmlWindow::SelectionToText()
726 {
727 if ( !m_selection )
728 return wxEmptyString;
729
730 wxClientDC dc(this);
731
732 const wxHtmlCell *end = m_selection->GetToCell();
733 wxString text;
734 wxHtmlTerminalCellsInterator i(m_selection->GetFromCell(), end);
735 if ( i )
736 {
737 text << i->ConvertToText(m_selection);
738 ++i;
739 }
740 const wxHtmlCell *prev = *i;
741 while ( i )
742 {
743 if ( prev->GetParent() != i->GetParent() )
744 text << _T('\n');
745 text << i->ConvertToText(*i == end ? m_selection : NULL);
746 prev = *i;
747 ++i;
748 }
749 return text;
750 }
751
752 void wxHtmlWindow::CopySelection(ClipboardType t)
753 {
754 if ( m_selection )
755 {
756 wxTheClipboard->UsePrimarySelection(t == Primary);
757 wxString txt(SelectionToText());
758 if ( wxTheClipboard->Open() )
759 {
760 wxTheClipboard->SetData(new wxTextDataObject(txt));
761 wxTheClipboard->Close();
762 wxLogTrace(_T("wxhtmlselection"),
763 _("Copied to clipboard:\"%s\""), txt.c_str());
764 }
765 }
766 }
767 #endif
768
769
770 void wxHtmlWindow::OnLinkClicked(const wxHtmlLinkInfo& link)
771 {
772 const wxMouseEvent *e = link.GetEvent();
773 if (e == NULL || e->LeftUp())
774 LoadPage(link.GetHref());
775 }
776
777 void wxHtmlWindow::OnCellClicked(wxHtmlCell *cell,
778 wxCoord x, wxCoord y,
779 const wxMouseEvent& event)
780 {
781 wxCHECK_RET( cell, _T("can't be called with NULL cell") );
782
783 cell->OnMouseClick(this, x, y, event);
784 }
785
786 void wxHtmlWindow::OnCellMouseHover(wxHtmlCell * WXUNUSED(cell),
787 wxCoord WXUNUSED(x), wxCoord WXUNUSED(y))
788 {
789 // do nothing here
790 }
791
792 void wxHtmlWindow::OnEraseBackground(wxEraseEvent& event)
793 {
794 }
795
796 void wxHtmlWindow::OnPaint(wxPaintEvent& WXUNUSED(event))
797 {
798 wxPaintDC dc(this);
799
800 if (m_tmpCanDrawLocks > 0 || m_Cell == NULL) return;
801
802 int x, y;
803 GetViewStart(&x, &y);
804 wxRect rect = GetUpdateRegion().GetBox();
805 wxSize sz = GetSize();
806
807 wxMemoryDC dcm;
808 if ( !m_backBuffer )
809 m_backBuffer = new wxBitmap(sz.x, sz.y);
810 dcm.SelectObject(*m_backBuffer);
811 dcm.SetBackground(wxBrush(GetBackgroundColour(), wxSOLID));
812 dcm.Clear();
813 PrepareDC(dcm);
814 dcm.SetMapMode(wxMM_TEXT);
815 dcm.SetBackgroundMode(wxTRANSPARENT);
816
817 wxHtmlRenderingInfo rinfo;
818 wxDefaultHtmlRenderingStyle rstyle;
819 rinfo.SetSelection(m_selection);
820 rinfo.SetStyle(&rstyle);
821 m_Cell->Draw(dcm, 0, 0,
822 y * wxHTML_SCROLL_STEP + rect.GetTop(),
823 y * wxHTML_SCROLL_STEP + rect.GetBottom(),
824 rinfo);
825
826 dcm.SetDeviceOrigin(0,0);
827 dc.Blit(0, rect.GetTop(),
828 sz.x, rect.GetBottom() - rect.GetTop() + 1,
829 &dcm,
830 0, rect.GetTop());
831 }
832
833
834
835
836 void wxHtmlWindow::OnSize(wxSizeEvent& event)
837 {
838 wxDELETE(m_backBuffer);
839
840 wxScrolledWindow::OnSize(event);
841 CreateLayout();
842
843 // Recompute selection if necessary:
844 if ( m_selection )
845 {
846 m_selection->Set(m_selection->GetFromCell(),
847 m_selection->GetToCell());
848 m_selection->ClearPrivPos();
849 }
850
851 Refresh();
852 }
853
854
855 void wxHtmlWindow::OnMouseMove(wxMouseEvent& event)
856 {
857 m_tmpMouseMoved = true;
858 }
859
860 void wxHtmlWindow::OnMouseDown(wxMouseEvent& event)
861 {
862 if ( event.LeftDown() && IsSelectionEnabled() )
863 {
864 m_makingSelection = true;
865
866 if ( m_selection )
867 {
868 wxDELETE(m_selection);
869 Refresh();
870 }
871 m_tmpSelFromPos = CalcUnscrolledPosition(event.GetPosition());
872 m_tmpSelFromCell = NULL;
873
874 CaptureMouse();
875 }
876 }
877
878 void wxHtmlWindow::OnMouseUp(wxMouseEvent& event)
879 {
880 #if wxUSE_CLIPBOARD
881 if ( m_makingSelection )
882 {
883 ReleaseMouse();
884 m_makingSelection = false;
885
886 // did the user move the mouse far enough from starting point?
887 if ( m_selection )
888 {
889 #ifdef __UNIX__
890 CopySelection(Primary);
891 #endif
892 // we don't want mouse up event that ended selecting to be
893 // handled as mouse click and e.g. follow hyperlink:
894 return;
895 }
896 }
897 #endif
898
899 SetFocus();
900 if ( m_Cell )
901 {
902 wxPoint pos = CalcUnscrolledPosition(event.GetPosition());
903 wxHtmlCell *cell = m_Cell->FindCellByPos(pos.x, pos.y);
904
905 // VZ: is it possible that we don't find anything at all?
906 // VS: yes. FindCellByPos returns terminal cell and
907 // containers may have empty borders
908 if ( cell )
909 OnCellClicked(cell, pos.x, pos.y, event);
910 }
911 }
912
913
914
915 void wxHtmlWindow::OnIdle(wxIdleEvent& WXUNUSED(event))
916 {
917 if (s_cur_hand == NULL)
918 {
919 s_cur_hand = new wxCursor(wxCURSOR_HAND);
920 s_cur_arrow = new wxCursor(wxCURSOR_ARROW);
921 }
922
923 if (m_tmpMouseMoved && (m_Cell != NULL))
924 {
925 int xc, yc, x, y;
926 wxGetMousePosition(&xc, &yc);
927 ScreenToClient(&xc, &yc);
928 CalcUnscrolledPosition(xc, yc, &x, &y);
929
930 wxHtmlCell *cell = m_Cell->FindCellByPos(x, y);
931
932 // handle selection update:
933 if ( m_makingSelection )
934 {
935 bool goingDown = m_tmpSelFromPos.y < y ||
936 m_tmpSelFromPos.y == y && m_tmpSelFromPos.x < x;
937
938 if ( !m_tmpSelFromCell )
939 {
940 if (goingDown)
941 {
942 m_tmpSelFromCell = m_Cell->FindCellByPos(
943 m_tmpSelFromPos.x,m_tmpSelFromPos.y,
944 wxHTML_FIND_NEAREST_AFTER);
945 if (!m_tmpSelFromCell)
946 m_tmpSelFromCell = m_Cell->GetFirstTerminal();
947 }
948 else
949 {
950 m_tmpSelFromCell = m_Cell->FindCellByPos(
951 m_tmpSelFromPos.x,m_tmpSelFromPos.y,
952 wxHTML_FIND_NEAREST_BEFORE);
953 if (!m_tmpSelFromCell)
954 m_tmpSelFromCell = m_Cell->GetLastTerminal();
955 }
956 }
957
958 wxHtmlCell *selcell = cell;
959 if (!selcell)
960 {
961 if (goingDown)
962 {
963 selcell = m_Cell->FindCellByPos(x, y,
964 wxHTML_FIND_NEAREST_AFTER);
965 if (!selcell)
966 selcell = m_Cell->GetLastTerminal();
967 }
968 else
969 {
970 selcell = m_Cell->FindCellByPos(x, y,
971 wxHTML_FIND_NEAREST_BEFORE);
972 if (!selcell)
973 selcell = m_Cell->GetFirstTerminal();
974 }
975 }
976
977 // NB: it may *rarely* happen that the code above didn't find one
978 // of the cells, e.g. if wxHtmlWindow doesn't contain any
979 // visible cells.
980 if ( selcell && m_tmpSelFromCell )
981 {
982 if ( !m_selection )
983 {
984 // start selecting only if mouse movement was big enough
985 // (otherwise it was meant as mouse click, not selection):
986 const int PRECISION = 2;
987 wxPoint diff = m_tmpSelFromPos - wxPoint(x,y);
988 if (abs(diff.x) > PRECISION || abs(diff.y) > PRECISION)
989 {
990 m_selection = new wxHtmlSelection();
991 }
992 }
993 if ( m_selection )
994 {
995 if ( m_tmpSelFromCell->IsBefore(selcell) )
996 {
997 m_selection->Set(m_tmpSelFromPos, m_tmpSelFromCell,
998 wxPoint(x,y), selcell); }
999 else
1000 {
1001 m_selection->Set(wxPoint(x,y), selcell,
1002 m_tmpSelFromPos, m_tmpSelFromCell);
1003 }
1004 m_selection->ClearPrivPos();
1005 Refresh();
1006 }
1007 }
1008 }
1009
1010 // handle cursor and status bar text changes:
1011 if ( cell != m_tmpLastCell )
1012 {
1013 wxHtmlLinkInfo *lnk = cell ? cell->GetLink(x, y) : NULL;
1014
1015 if (lnk != m_tmpLastLink)
1016 {
1017 if (lnk == NULL)
1018 {
1019 SetCursor(*s_cur_arrow);
1020 if (m_RelatedStatusBar != -1)
1021 m_RelatedFrame->SetStatusText(wxEmptyString,
1022 m_RelatedStatusBar);
1023 }
1024 else
1025 {
1026 SetCursor(*s_cur_hand);
1027 if (m_RelatedStatusBar != -1)
1028 m_RelatedFrame->SetStatusText(lnk->GetHref(),
1029 m_RelatedStatusBar);
1030 }
1031 m_tmpLastLink = lnk;
1032 }
1033
1034 m_tmpLastCell = cell;
1035 }
1036 else // mouse moved but stayed in the same cell
1037 {
1038 if ( cell )
1039 OnCellMouseHover(cell, x, y);
1040 }
1041
1042 m_tmpMouseMoved = FALSE;
1043 }
1044 }
1045
1046 #if wxUSE_CLIPBOARD
1047 void wxHtmlWindow::StopAutoScrolling()
1048 {
1049 if ( m_timerAutoScroll )
1050 {
1051 wxDELETE(m_timerAutoScroll);
1052 }
1053 }
1054
1055 void wxHtmlWindow::OnMouseEnter(wxMouseEvent& event)
1056 {
1057 StopAutoScrolling();
1058 event.Skip();
1059 }
1060
1061 void wxHtmlWindow::OnMouseLeave(wxMouseEvent& event)
1062 {
1063 // don't prevent the usual processing of the event from taking place
1064 event.Skip();
1065
1066 // when a captured mouse leave a scrolled window we start generate
1067 // scrolling events to allow, for example, extending selection beyond the
1068 // visible area in some controls
1069 if ( wxWindow::GetCapture() == this )
1070 {
1071 // where is the mouse leaving?
1072 int pos, orient;
1073 wxPoint pt = event.GetPosition();
1074 if ( pt.x < 0 )
1075 {
1076 orient = wxHORIZONTAL;
1077 pos = 0;
1078 }
1079 else if ( pt.y < 0 )
1080 {
1081 orient = wxVERTICAL;
1082 pos = 0;
1083 }
1084 else // we're lower or to the right of the window
1085 {
1086 wxSize size = GetClientSize();
1087 if ( pt.x > size.x )
1088 {
1089 orient = wxHORIZONTAL;
1090 pos = GetVirtualSize().x / wxHTML_SCROLL_STEP;
1091 }
1092 else if ( pt.y > size.y )
1093 {
1094 orient = wxVERTICAL;
1095 pos = GetVirtualSize().y / wxHTML_SCROLL_STEP;
1096 }
1097 else // this should be impossible
1098 {
1099 // but seems to happen sometimes under wxMSW - maybe it's a bug
1100 // there but for now just ignore it
1101
1102 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
1103
1104 return;
1105 }
1106 }
1107
1108 // only start the auto scroll timer if the window can be scrolled in
1109 // this direction
1110 if ( !HasScrollbar(orient) )
1111 return;
1112
1113 delete m_timerAutoScroll;
1114 m_timerAutoScroll = new wxHtmlWinAutoScrollTimer
1115 (
1116 this,
1117 pos == 0 ? wxEVT_SCROLLWIN_LINEUP
1118 : wxEVT_SCROLLWIN_LINEDOWN,
1119 pos,
1120 orient
1121 );
1122 m_timerAutoScroll->Start(50); // FIXME: make configurable
1123 }
1124 }
1125
1126 void wxHtmlWindow::OnKeyUp(wxKeyEvent& event)
1127 {
1128 if ( IsSelectionEnabled() &&
1129 event.GetKeyCode() == 'C' && event.ControlDown() )
1130 {
1131 if ( m_selection )
1132 CopySelection();
1133 }
1134 }
1135
1136 void wxHtmlWindow::OnCopy(wxCommandEvent& event)
1137 {
1138 if ( m_selection )
1139 CopySelection();
1140 }
1141
1142 void wxHtmlWindow::OnDoubleClick(wxMouseEvent& event)
1143 {
1144 // select word under cursor:
1145 if ( IsSelectionEnabled() )
1146 {
1147 wxPoint pos = CalcUnscrolledPosition(event.GetPosition());
1148 wxHtmlCell *cell = m_Cell->FindCellByPos(pos.x, pos.y);
1149 if ( cell )
1150 {
1151 delete m_selection;
1152 m_selection = new wxHtmlSelection();
1153 m_selection->Set(cell, cell);
1154 RefreshRect(wxRect(CalcScrolledPosition(cell->GetAbsPos()),
1155 wxSize(cell->GetWidth(), cell->GetHeight())));
1156 }
1157 }
1158 else
1159 event.Skip();
1160 }
1161 #endif
1162
1163
1164
1165 IMPLEMENT_ABSTRACT_CLASS(wxHtmlProcessor,wxObject)
1166
1167 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow,wxScrolledWindow)
1168
1169 BEGIN_EVENT_TABLE(wxHtmlWindow, wxScrolledWindow)
1170 EVT_SIZE(wxHtmlWindow::OnSize)
1171 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseDown)
1172 EVT_LEFT_UP(wxHtmlWindow::OnMouseUp)
1173 EVT_RIGHT_UP(wxHtmlWindow::OnMouseUp)
1174 EVT_MOTION(wxHtmlWindow::OnMouseMove)
1175 EVT_IDLE(wxHtmlWindow::OnIdle)
1176 EVT_ERASE_BACKGROUND(wxHtmlWindow::OnEraseBackground)
1177 EVT_PAINT(wxHtmlWindow::OnPaint)
1178 #if wxUSE_CLIPBOARD
1179 EVT_LEFT_DCLICK(wxHtmlWindow::OnDoubleClick)
1180 EVT_ENTER_WINDOW(wxHtmlWindow::OnMouseEnter)
1181 EVT_LEAVE_WINDOW(wxHtmlWindow::OnMouseLeave)
1182 EVT_KEY_UP(wxHtmlWindow::OnKeyUp)
1183 EVT_MENU(wxID_COPY, wxHtmlWindow::OnCopy)
1184 #endif
1185 END_EVENT_TABLE()
1186
1187
1188
1189
1190
1191 // A module to allow initialization/cleanup
1192 // without calling these functions from app.cpp or from
1193 // the user's application.
1194
1195 class wxHtmlWinModule: public wxModule
1196 {
1197 DECLARE_DYNAMIC_CLASS(wxHtmlWinModule)
1198 public:
1199 wxHtmlWinModule() : wxModule() {}
1200 bool OnInit() { return TRUE; }
1201 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
1202 };
1203
1204 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule, wxModule)
1205
1206
1207 // This hack forces the linker to always link in m_* files
1208 // (wxHTML doesn't work without handlers from these files)
1209 #include "wx/html/forcelnk.h"
1210 FORCE_WXHTML_MODULES()
1211
1212 #endif