]> git.lyx.org Git - lyx.git/blobdiff - src/BufferView.cpp
HTML for math fonts.
[lyx.git] / src / BufferView.cpp
index 64f3065898ac4d92f04a05d3cc8526373891f30e..0d5b76a61482d1284b18502ef258c39a0acd0c70 100644 (file)
@@ -39,7 +39,6 @@
 #include "LyX.h"
 #include "LyXAction.h"
 #include "lyxfind.h"
-#include "LyXFunc.h"
 #include "Layout.h"
 #include "LyXRC.h"
 #include "MetricsInfo.h"
@@ -221,7 +220,8 @@ struct BufferView::Private
        Private(BufferView & bv): wh_(0), cursor_(bv),
                anchor_pit_(0), anchor_ypos_(0),
                inlineCompletionUniqueChars_(0),
-               last_inset_(0), bookmark_edit_position_(0), gui_(0)
+               last_inset_(0), mouse_position_cache_(),
+               bookmark_edit_position_(0), gui_(0)
        {}
 
        ///
@@ -264,6 +264,11 @@ struct BufferView::Private
          */
        Inset * last_inset_;
 
+       /// position of the mouse at the time of the last mouse move
+       /// This is used to update the hovering status of inset in
+       /// cases where the buffer is scrolled, but the mouse didn't move.
+       Point mouse_position_cache_;
+
        // cache for id of the paragraph which was edited the last time
        int bookmark_edit_position_;
 
@@ -398,10 +403,6 @@ bool BufferView::fitCursor()
 
 void BufferView::processUpdateFlags(Update::flags flags)
 {
-       // last_inset_ points to the last visited inset. This pointer may become
-       // invalid because of keyboard editing. Since all such operations
-       // causes screen update(), I reset last_inset_ to avoid such a problem.
-       d->last_inset_ = 0;
        // This is close to a hot-path.
        LYXERR(Debug::DEBUG, "BufferView::processUpdateFlags()"
                << "[fitcursor = " << (flags & Update::FitCursor)
@@ -466,6 +467,8 @@ void BufferView::processUpdateFlags(Update::flags flags)
                // refresh it:
                showCursor();
        }
+
+       updateHoveredInset();
 }
 
 
@@ -574,6 +577,7 @@ void BufferView::scrollDocView(int value)
        if (abs(offset) <= 2 * height_) {
                d->anchor_ypos_ -= offset;
                buffer_.changed(true);
+               updateHoveredInset();
                return;
        }
 
@@ -697,17 +701,19 @@ void BufferView::saveBookmark(unsigned int idx)
        // acturately locate a bookmark in a 'live' lyx session.
        // pit and pos will be updated with bottom level pit/pos
        // when lyx exits.
-       theSession().bookmarks().save(
-               buffer_.fileName(),
-               d->cursor_.bottom().pit(),
-               d->cursor_.bottom().pos(),
-               d->cursor_.paragraph().id(),
-               d->cursor_.pos(),
-               idx
-       );
-       if (idx)
-               // emit message signal.
-               message(_("Save bookmark"));
+       if (!buffer_.isInternal()) {
+               theSession().bookmarks().save(
+                       buffer_.fileName(),
+                       d->cursor_.bottom().pit(),
+                       d->cursor_.bottom().pos(),
+                       d->cursor_.paragraph().id(),
+                       d->cursor_.pos(),
+                       idx
+                       );
+               if (idx)
+                       // emit message signal.
+                       message(_("Save bookmark"));
+       }
 }
 
 
@@ -809,14 +815,19 @@ void BufferView::showCursor()
 
 void BufferView::showCursor(DocIterator const & dit, bool recenter)
 {
-       if (scrollToCursor(dit, recenter))
-               buffer_.changed(false);
+       if (scrollToCursor(dit, recenter)) {
+               buffer_.changed(true);
+               updateHoveredInset();
+       }
 }
 
 
 void BufferView::scrollToCursor()
 {
-       scrollToCursor(d->cursor_, false);
+       if (scrollToCursor(d->cursor_, false)) {
+               buffer_.changed(true);
+               updateHoveredInset();
+       }
 }
 
 
@@ -864,11 +875,7 @@ bool BufferView::scrollToCursor(DocIterator const & dit, bool recenter)
                        scrolled = scrollDown(ypos - height_ + defaultRowHeight() ); 
 
                // else, nothing to do, the cursor is already visible so we just return.
-               if (scrolled != 0) {
-                       updateMetrics();
-                       return true;
-               }
-               return false;
+               return scrolled != 0;
        }
 
        // fix inline completion position
@@ -895,25 +902,24 @@ bool BufferView::scrollToCursor(DocIterator const & dit, bool recenter)
        else
                d->anchor_ypos_ = defaultRowHeight() * 2;
 
-       updateMetrics();
        return true;
 }
 
 
-void BufferView::updateLayout(DocumentClass const * const oldlayout)
+void BufferView::updateDocumentClass(DocumentClass const * const olddc)
 {
        message(_("Converting document to new document class..."));
        
        StableDocIterator backcur(d->cursor_);
        ErrorList & el = buffer_.errorList("Class Switch");
        cap::switchBetweenClasses(
-                       oldlayout, buffer_.params().documentClassPtr(),
+                       olddc, buffer_.params().documentClassPtr(),
                        static_cast<InsetText &>(buffer_.inset()), el);
 
        setCursor(backcur.asDocIterator(&buffer_));
 
        buffer_.errors("Class Switch");
-       buffer_.updateLabels();
+       buffer_.updateBuffer();
 }
 
 /** Return the change status at cursor position, taking in account the
@@ -1024,11 +1030,6 @@ bool BufferView::getStatus(FuncRequest const & cmd, FuncStatus & flag)
                flag.setEnabled(true);
                break;
 
-       case LFUN_REGEXP_MODE:
-               // FIXME: Test if current WorkArea is the search WorkArea
-               flag.setEnabled(buffer().isInternal() && !cur.inRegexped());
-               break;
-
        case LFUN_LABEL_COPY_AS_REF: {
                // if there is an inset at cursor, see whether it
                // handles the lfun
@@ -1185,7 +1186,7 @@ void BufferView::dispatch(FuncRequest const & cmd, DispatchResult & dr)
                                                << unknown_tokens << " unknown token"
                                                << (unknown_tokens == 1 ? "" : "s"));
                }
-               updateLayout(oldClass);
+               updateDocumentClass(oldClass);
                        
                // We are most certainly here because of a change in the document
                // It is then better to make sure that all dialogs are in sync with
@@ -1200,7 +1201,7 @@ void BufferView::dispatch(FuncRequest const & cmd, DispatchResult & dr)
                cur.recordUndoFullDocument();
                buffer_.params().clearLayoutModules();
                buffer_.params().makeDocumentClass();
-               updateLayout(oldClass);
+               updateDocumentClass(oldClass);
                dr.update(Update::Force | Update::FitCursor);
                break;
        }
@@ -1217,7 +1218,7 @@ void BufferView::dispatch(FuncRequest const & cmd, DispatchResult & dr)
                cur.recordUndoFullDocument();
                buffer_.params().addLayoutModule(argument);
                buffer_.params().makeDocumentClass();
-               updateLayout(oldClass);
+               updateDocumentClass(oldClass);
                dr.update(Update::Force | Update::FitCursor);
                break;
        }
@@ -1240,7 +1241,7 @@ void BufferView::dispatch(FuncRequest const & cmd, DispatchResult & dr)
                cur.recordUndoFullDocument();
                buffer_.params().setBaseClass(argument);
                buffer_.params().makeDocumentClass();
-               updateLayout(oldDocClass);
+               updateDocumentClass(oldDocClass);
                dr.update(Update::Force | Update::FitCursor);
                break;
        }
@@ -1256,7 +1257,7 @@ void BufferView::dispatch(FuncRequest const & cmd, DispatchResult & dr)
                LayoutFileList::get().reset(bc);
                buffer_.params().setBaseClass(bc);
                buffer_.params().makeDocumentClass();
-               updateLayout(oldClass);
+               updateDocumentClass(oldClass);
                dr.update(Update::Force | Update::FitCursor);
                break;
        }
@@ -1346,9 +1347,9 @@ void BufferView::dispatch(FuncRequest const & cmd, DispatchResult & dr)
                                dr.update(Update::Force | Update::FitCursor);
                        } else {
                                // Switch to other buffer view and resend cmd
-                               theLyXFunc().dispatch(FuncRequest(
+                               lyx::dispatch(FuncRequest(
                                        LFUN_BUFFER_SWITCH, b->absFileName()));
-                               theLyXFunc().dispatch(cmd);
+                               lyx::dispatch(cmd);
                        }
                        break;
                }
@@ -1463,7 +1464,7 @@ void BufferView::dispatch(FuncRequest const & cmd, DispatchResult & dr)
                if (cmd.argument().empty() && !d->search_request_cache_.argument().empty())
                        req = d->search_request_cache_;
                if (req.argument().empty()) {
-                       theLyXFunc().dispatch(FuncRequest(LFUN_DIALOG_SHOW, "findreplace"));
+                       lyx::dispatch(FuncRequest(LFUN_DIALOG_SHOW, "findreplace"));
                        break;
                }
                if (find(this, req))
@@ -1638,6 +1639,8 @@ void BufferView::dispatch(FuncRequest const & cmd, DispatchResult & dr)
                bool const in_texted = cur.inTexted();
                cur.reset();
                buffer_.changed(true);
+               updateHoveredInset();
+
                d->text_metrics_[&buffer_.text()].editXY(cur, p.x_, p.y_,
                        true, cmd.action == LFUN_SCREEN_UP); 
                //FIXME: what to do with cur.x_target()?
@@ -1939,6 +1942,42 @@ Inset const * BufferView::getCoveringInset(Text const & text,
 }
 
 
+void BufferView::updateHoveredInset() const
+{
+       // Get inset under mouse, if there is one.
+       Inset const * covering_inset = getCoveringInset(buffer_.text(),
+                       d->mouse_position_cache_.x_, d->mouse_position_cache_.y_);
+       if (covering_inset == d->last_inset_)
+               // Same inset, no need to do anything...
+               return;
+
+       bool need_redraw = false;
+       if (d->last_inset_)
+               // Remove the hint on the last hovered inset (if any).
+               need_redraw |= d->last_inset_->setMouseHover(false);
+       
+       // const_cast because of setMouseHover().
+       Inset * inset = const_cast<Inset *>(covering_inset);
+       if (inset)
+               // Highlight the newly hovered inset (if any).
+               need_redraw |= inset->setMouseHover(true);
+
+       d->last_inset_ = inset;
+       
+       if (need_redraw) {
+               LYXERR(Debug::PAINTING, "Mouse hover detected at: ("
+                               << d->mouse_position_cache_.x_ << ", " 
+                               << d->mouse_position_cache_.y_ << ")");
+       
+               d->update_strategy_ = DecorationUpdate;
+
+               // This event (moving without mouse click) is not passed further.
+               // This should be changed if it is further utilized.
+               buffer_.changed(false);
+       }
+}
+
+
 void BufferView::mouseEventDispatch(FuncRequest const & cmd0)
 {
        //lyxerr << "[ cmd0 " << cmd0 << "]" << endl;
@@ -1958,36 +1997,11 @@ void BufferView::mouseEventDispatch(FuncRequest const & cmd0)
        // make sure we stay within the screen...
        cmd.y = min(max(cmd.y, -1), height_);
 
-       if (cmd.action == LFUN_MOUSE_MOTION && cmd.button() == mouse_button::none) {
-
-               // Get inset under mouse, if there is one.
-               Inset const * covering_inset =
-                       getCoveringInset(buffer_.text(), cmd.x, cmd.y);
-               if (covering_inset == d->last_inset_)
-                       // Same inset, no need to do anything...
-                       return;
-
-               bool need_redraw = false;
-               // const_cast because of setMouseHover().
-               Inset * inset = const_cast<Inset *>(covering_inset);
-               if (d->last_inset_)
-                       // Remove the hint on the last hovered inset (if any).
-                       need_redraw |= d->last_inset_->setMouseHover(false);
-               if (inset)
-                       // Highlighted the newly hovered inset (if any).
-                       need_redraw |= inset->setMouseHover(true);
-               d->last_inset_ = inset;
-               if (!need_redraw)
-                       return;
-
-               LYXERR(Debug::PAINTING, "Mouse hover detected at: ("
-                       << cmd.x << ", " << cmd.y << ")");
-
-               d->update_strategy_ = DecorationUpdate;
+       d->mouse_position_cache_.x_ = cmd.x;
+       d->mouse_position_cache_.y_ = cmd.y;
 
-               // This event (moving without mouse click) is not passed further.
-               // This should be changed if it is further utilized.
-               buffer_.changed(false);
+       if (cmd.action == LFUN_MOUSE_MOTION && cmd.button() == mouse_button::none) {
+               updateHoveredInset();
                return;
        }
 
@@ -2033,10 +2047,12 @@ void BufferView::mouseEventDispatch(FuncRequest const & cmd0)
 void BufferView::lfunScroll(FuncRequest const & cmd)
 {
        string const scroll_type = cmd.getArg(0);
-       int const scroll_step = 
-               (scroll_type == "line") ? d->scrollbarParameters_.single_step
-               : (scroll_type == "page") ? d->scrollbarParameters_.page_step : 0;
-       if (scroll_step == 0)
+       int scroll_step = 0;
+       if (scroll_type == "line")
+               scroll_step = d->scrollbarParameters_.single_step;
+       else if (scroll_type == "page")
+               scroll_step = d->scrollbarParameters_.page_step;
+       else
                return;
        string const scroll_quantity = cmd.getArg(1);
        if (scroll_quantity == "up")
@@ -2049,6 +2065,7 @@ void BufferView::lfunScroll(FuncRequest const & cmd)
                        scroll(scroll_step * scroll_value);
        }
        buffer_.changed(true);
+       updateHoveredInset();
 }
 
 
@@ -2233,7 +2250,7 @@ bool BufferView::checkDepm(Cursor & cur, Cursor & old)
 
        d->cursor_ = cur;
 
-       buffer_.updateLabels();
+       buffer_.updateBuffer();
        buffer_.changed(true);
        return true;
 }