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