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