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