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