]> git.lyx.org Git - lyx.git/blobdiff - src/BufferView.cpp
Fix wrongly copy-pasted entries in SpellcheckerUi.ui
[lyx.git] / src / BufferView.cpp
index 8dbd511defb020024a0ad72edd33b5a843098603..fc7c7bb80828f9a977d39c12ef906120a7c6a56c 100644 (file)
@@ -107,10 +107,7 @@ T * getInsetByCode(Cursor const & cur, InsetCode code)
        return 0;
 }
 
-
-bool findInset(DocIterator & dit, vector<InsetCode> const & codes,
-       bool same_content);
-
+/// Note that comparing contents can only be used for InsetCommand
 bool findNextInset(DocIterator & dit, vector<InsetCode> const & codes,
        docstring const & contents)
 {
@@ -118,12 +115,17 @@ bool findNextInset(DocIterator & dit, vector<InsetCode> const & codes,
 
        while (tmpdit) {
                Inset const * inset = tmpdit.nextInset();
-               if (inset
-                   && std::find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end()
-                   && (contents.empty() ||
-                   static_cast<InsetCommand const *>(inset)->getFirstNonOptParam() == contents)) {
-                       dit = tmpdit;
-                       return true;
+               if (inset) {
+                       bool const valid_code = std::find(codes.begin(), codes.end(), 
+                               inset->lyxCode()) != codes.end();
+                       InsetCommand const * ic = inset->asInsetCommand();
+                       bool const same_or_no_contents =  contents.empty()
+                               || (ic && (ic->getFirstNonOptParam() == contents));
+                       
+                       if (valid_code && same_or_no_contents) {
+                               dit = tmpdit;
+                               return true;
+                       }
                }
                tmpdit.forwardInset();
        }
@@ -133,6 +135,7 @@ bool findNextInset(DocIterator & dit, vector<InsetCode> const & codes,
 
 
 /// Looks for next inset with one of the given codes.
+/// Note that same_content can only be used for InsetCommand
 bool findInset(DocIterator & dit, vector<InsetCode> const & codes,
        bool same_content)
 {
@@ -142,11 +145,14 @@ bool findInset(DocIterator & dit, vector<InsetCode> const & codes,
        if (!tmpdit)
                return false;
 
-       if (same_content) {
-               Inset const * inset = tmpdit.nextInset();
-               if (inset
-                   && std::find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end()) {
-                       contents = static_cast<InsetCommand const *>(inset)->getFirstNonOptParam();
+       Inset const * inset = tmpdit.nextInset();
+       if (same_content && inset) {
+               InsetCommand const * ic = inset->asInsetCommand();
+               if (ic) {
+                       bool const valid_code = std::find(codes.begin(), codes.end(),
+                               ic->lyxCode()) != codes.end();
+                       if (valid_code)
+                               contents = ic->getFirstNonOptParam();
                }
        }
 
@@ -263,7 +269,7 @@ struct BufferView::Private
        /** kept to send setMouseHover(false).
          * Not owned, so don't delete.
          */
-       Inset * last_inset_;
+       Inset const * last_inset_;
        /// are we hovering something that we can click
        bool clickable_inset_;
 
@@ -302,8 +308,7 @@ BufferView::BufferView(Buffer & buf)
        d->cursor_.resetAnchor();
        d->cursor_.setCurrentFont();
 
-       if (graphics::Previews::status() != LyXRC::PREVIEW_OFF)
-               thePreviews().generateBufferPreviews(buffer_);
+       buffer_.updatePreviews();
 }
 
 
@@ -1120,7 +1125,7 @@ bool BufferView::getStatus(FuncRequest const & cmd, FuncStatus & flag)
                if (cur.inset().lyxCode() == CAPTION_CODE)
                        return cur.inset().getStatus(cur, cmd, flag);
                // FIXME we should consider passthru paragraphs too.
-               flag.setEnabled(!cur.inset().getLayout().isPassThru());
+               flag.setEnabled(!(cur.inTexted() && cur.paragraph().isPassThru()));
                break;
 
        case LFUN_CITATION_INSERT: {
@@ -1534,8 +1539,14 @@ void BufferView::dispatch(FuncRequest const & cmd, DispatchResult & dr)
                FindAndReplaceOptions opt;
                istringstream iss(to_utf8(cmd.argument()));
                iss >> opt;
-               if (findAdv(this, opt))
+               if (findAdv(this, opt)) {
                        dr.screenUpdate(Update::Force | Update::FitCursor);
+                       cur.dispatched();
+                       dispatched = true;
+               } else {
+                       cur.undispatched();
+                       dispatched = false;
+               }
                break;
        }
 
@@ -1740,7 +1751,7 @@ void BufferView::dispatch(FuncRequest const & cmd, DispatchResult & dr)
                        Inset * ins = cur.nextInset();
                        if (!ins)
                                break;
-                       docstring insname = ins->name();
+                       docstring insname = ins->layoutName();
                        while (!insname.empty()) {
                                if (insname == name || name == from_utf8("*")) {
                                        cur.recordUndo();
@@ -1840,7 +1851,7 @@ void BufferView::dispatch(FuncRequest const & cmd, DispatchResult & dr)
                icp["key"] = from_utf8(arg);
                if (!opt1.empty())
                        icp["before"] = from_utf8(opt1);
-               string icstr = InsetCommand::params2string("citation", icp);
+               string icstr = InsetCommand::params2string(icp);
                FuncRequest fr(LFUN_INSET_INSERT, icstr);
                lyx::dispatch(fr);
                break;
@@ -1868,13 +1879,14 @@ void BufferView::dispatch(FuncRequest const & cmd, DispatchResult & dr)
        }
 
        default:
-               dispatched = false;
+               // OK, so try the Buffer itself...
+               buffer_.dispatch(cmd, dr);
+               dispatched = dr.dispatched();
                break;
        }
 
        buffer_.undo().endUndoGroup();
        dr.dispatched(dispatched);
-       return;
 }
 
 
@@ -1979,14 +1991,12 @@ void BufferView::updateHoveredInset() const
                d->last_inset_ = 0;
        }
        
-       // const_cast because of setMouseHover().
-       Inset * inset = const_cast<Inset *>(covering_inset);
-       if (inset && inset->setMouseHover(this, true)) {
+       if (covering_inset && covering_inset->setMouseHover(this, true)) {
                need_redraw = true;
                // Only the insets that accept the hover state, do 
                // clear the last_inset_, so only set the last_inset_
                // member if the hovered setting is accepted.
-               d->last_inset_ = inset;
+               d->last_inset_ = covering_inset;
        }
 
        if (need_redraw) {
@@ -2233,6 +2243,7 @@ TextMetrics const & BufferView::textMetrics(Text const * t) const
 
 TextMetrics & BufferView::textMetrics(Text const * t)
 {
+       LASSERT(t, /**/);
        TextMetricsCache::iterator tmc_it  = d->text_metrics_.find(t);
        if (tmc_it == d->text_metrics_.end()) {
                tmc_it = d->text_metrics_.insert(
@@ -2352,6 +2363,42 @@ void BufferView::putSelectionAt(DocIterator const & cur,
 }
 
 
+bool BufferView::selectIfEmpty(DocIterator & cur)
+{
+       if (!cur.paragraph().empty())
+               return false;
+
+       pit_type const beg_pit = cur.pit();
+       if (beg_pit > 0) {
+               // The paragraph associated to this item isn't
+               // the first one, so it can be selected
+               cur.backwardPos();
+       } else {
+               // We have to resort to select the space between the
+               // end of this item and the begin of the next one
+               cur.forwardPos();
+       }
+       if (cur.empty()) {
+               // If it is the only item in the document,
+               // nothing can be selected
+               return false;
+       }
+       pit_type const end_pit = cur.pit();
+       pos_type const end_pos = cur.pos();
+       d->cursor_.clearSelection();
+       d->cursor_.reset();
+       d->cursor_.setCursor(cur);
+       d->cursor_.pit() = beg_pit;
+       d->cursor_.pos() = 0;
+       d->cursor_.setSelection(false);
+       d->cursor_.resetAnchor();
+       d->cursor_.pit() = end_pit;
+       d->cursor_.pos() = end_pos;
+       d->cursor_.setSelection();
+       return true;
+}
+
+
 Cursor & BufferView::cursor()
 {
        return d->cursor_;
@@ -2506,8 +2553,8 @@ void BufferView::insertLyXFile(FileName const & fname)
        message(bformat(_("Inserting document %1$s..."), disp_fn));
 
        docstring res;
-       Buffer buf("", false);
-       if (buf.loadLyXFile(filename)) {
+       Buffer buf(filename.absFileName(), false);
+       if (buf.loadLyXFile() == Buffer::ReadSuccess) {
                ErrorList & el = buffer_.errorList("Parse");
                // Copy the inserted document error list into the current buffer one.
                el = buf.errorList("Parse");
@@ -2522,7 +2569,6 @@ void BufferView::insertLyXFile(FileName const & fname)
        buffer_.changed(true);
        // emit message signal.
        message(bformat(res, disp_fn));
-       buffer_.errors("Parse");
 }
 
 
@@ -2849,6 +2895,12 @@ DocIterator const & BufferView::inlineCompletionPos() const
 }
 
 
+void BufferView::resetInlineCompletionPos()
+{
+       d->inlineCompletionPos_ = DocIterator();
+}
+
+
 bool samePar(DocIterator const & a, DocIterator const & b)
 {
        if (a.empty() && b.empty())