]> git.lyx.org Git - lyx.git/blobdiff - src/BufferView.cpp
Move Dialog.{h,cpp} and ButtonPolicy.{h,cpp} to frontends/qt4/
[lyx.git] / src / BufferView.cpp
index c9afa72693bef80ee2ebd3294f4ed9f7edeae526..4bd4a99c242a7eb52520b1e9e9c397598c1d3bef 100644 (file)
@@ -54,7 +54,6 @@
 #include "VSpace.h"
 #include "WordLangTuple.h"
 
-
 #include "insets/InsetBibtex.h"
 #include "insets/InsetCommand.h" // ChangeRefs
 #include "insets/InsetRef.h"
 #include "graphics/Previews.h"
 
 #include "support/convert.h"
+#include "support/lstrings.h"
 #include "support/FileFilterList.h"
 #include "support/filetools.h"
 #include "support/Package.h"
 #include "support/types.h"
-#include "support/fs_extras.h"
 
-#include <boost/bind.hpp>
 #include <boost/current_function.hpp>
-#include <boost/next_prior.hpp>
-#include <boost/filesystem/operations.hpp>
 
 #include <cerrno>
 #include <fstream>
 #include <functional>
+#include <iterator>
 #include <vector>
 
 using std::distance;
@@ -98,8 +95,6 @@ using std::mem_fun_ref;
 using std::string;
 using std::vector;
 
-namespace fs = boost::filesystem;
-
 namespace lyx {
 
 using support::addPath;
@@ -212,132 +207,16 @@ void gotoInset(BufferView * bv, InsetCode code, bool same_content)
 }
 
 
-
-/// the type of outline operation
-enum OutlineOp {
-       OutlineUp, // Move this header with text down
-       OutlineDown,   // Move this header with text up
-       OutlineIn, // Make this header deeper
-       OutlineOut // Make this header shallower
-};
-
-
-void outline(OutlineOp mode, Cursor & cur)
-{
-       Buffer & buf = cur.buffer();
-       pit_type & pit = cur.pit();
-       ParagraphList & pars = buf.text().paragraphs();
-       ParagraphList::iterator bgn = pars.begin();
-       // The first paragraph of the area to be copied:
-       ParagraphList::iterator start = boost::next(bgn, pit);
-       // The final paragraph of area to be copied:
-       ParagraphList::iterator finish = start;
-       ParagraphList::iterator end = pars.end();
-
-       TextClass::const_iterator lit =
-               buf.params().getTextClass().begin();
-       TextClass::const_iterator const lend =
-               buf.params().getTextClass().end();
-
-       int const thistoclevel = start->layout()->toclevel;
-       int toclevel;
-       switch (mode) {
-               case OutlineUp: {
-                       // Move out (down) from this section header
-                       if (finish != end)
-                               ++finish;
-                       // Seek the one (on same level) below
-                       for (; finish != end; ++finish) {
-                               toclevel = finish->layout()->toclevel;
-                               if (toclevel != Layout::NOT_IN_TOC
-                                   && toclevel <= thistoclevel) {
-                                       break;
-                               }
-                       }
-                       ParagraphList::iterator dest = start;
-                       // Move out (up) from this header
-                       if (dest == bgn)
-                               break;
-                       // Search previous same-level header above
-                       do {
-                               --dest;
-                               toclevel = dest->layout()->toclevel;
-                       } while(dest != bgn
-                               && (toclevel == Layout::NOT_IN_TOC
-                                   || toclevel > thistoclevel));
-                       // Not found; do nothing
-                       if (toclevel == Layout::NOT_IN_TOC || toclevel > thistoclevel)
-                               break;
-                       pit_type const newpit = std::distance(bgn, dest);
-                       pit_type const len = std::distance(start, finish);
-                       pit_type const deletepit = pit + len;
-                       buf.undo().recordUndo(cur, ATOMIC_UNDO, newpit, deletepit - 1);
-                       pars.insert(dest, start, finish);
-                       start = boost::next(pars.begin(), deletepit);
-                       pit = newpit;
-                       pars.erase(start, finish);
-                       break;
-               }
-               case OutlineDown: {
-                       // Go down out of current header:
-                       if (finish != end)
-                               ++finish;
-                       // Find next same-level header:
-                       for (; finish != end; ++finish) {
-                               toclevel = finish->layout()->toclevel;
-                               if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
-                                       break;
-                       }
-                       ParagraphList::iterator dest = finish;
-                       // Go one down from *this* header:
-                       if (dest != end)
-                               ++dest;
-                       else
-                               break;
-                       // Go further down to find header to insert in front of:
-                       for (; dest != end; ++dest) {
-                               toclevel = dest->layout()->toclevel;
-                               if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
-                                       break;
-                       }
-                       // One such was found:
-                       pit_type newpit = std::distance(bgn, dest);
-                       pit_type const len = std::distance(start, finish);
-                       buf.undo().recordUndo(cur, ATOMIC_UNDO, pit, newpit - 1);
-                       pars.insert(dest, start, finish);
-                       start = boost::next(bgn, pit);
-                       pit = newpit - len;
-                       pars.erase(start, finish);
-                       break;
-               }
-               case OutlineIn:
-                       buf.undo().recordUndo(cur);
-                       for (; lit != lend; ++lit) {
-                               if ((*lit)->toclevel == thistoclevel + 1 &&
-                                   start->layout()->labeltype == (*lit)->labeltype) {
-                                       start->layout((*lit));
-                                       break;
-                               }
-                       }
-                       break;
-               case OutlineOut:
-                       buf.undo().recordUndo(cur);
-                       for (; lit != lend; ++lit) {
-                               if ((*lit)->toclevel == thistoclevel - 1 &&
-                                   start->layout()->labeltype == (*lit)->labeltype) {
-                                       start->layout((*lit));
-                                       break;
-                               }
-                       }
-                       break;
-               default:
-                       break;
-       }
-}
-
 /// A map from a Text to the associated text metrics
 typedef std::map<Text const *, TextMetrics> TextMetricsCache;
 
+enum ScreenUpdateStrategy {
+       NoScreenUpdate,
+       SingleParUpdate,
+       FullScreenUpdate,
+       DecorationUpdate
+};
+
 } // anon namespace
 
 
@@ -357,7 +236,7 @@ struct BufferView::Private
        ///
        ScrollbarParameters scrollbarParameters_;
        ///
-       ViewMetricsInfo metrics_info_;
+       ScreenUpdateStrategy update_strategy_;
        ///
        CoordCache coord_cache_;
 
@@ -486,12 +365,11 @@ void BufferView::processUpdateFlags(Update::flags flags)
        // 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)
-               << BOOST_CURRENT_FUNCTION
+       LYXERR(Debug::DEBUG, "BufferView::processUpdateFlags()"
                << "[fitcursor = " << (flags & Update::FitCursor)
                << ", forceupdate = " << (flags & Update::Force)
                << ", singlepar = " << (flags & Update::SinglePar)
-               << "]  buffer: " << &buffer_ << endl;
+               << "]  buffer: " << &buffer_);
 
        // Update macro store
        if (!(cursor().inMathed() && cursor().inMacroMode()))
@@ -504,12 +382,12 @@ void BufferView::processUpdateFlags(Update::flags flags)
        // Case when no explicit update is requested.
        if (!flags) {
                // no need to redraw anything.
-               d->metrics_info_.update_strategy = NoScreenUpdate;
+               d->update_strategy_ = NoScreenUpdate;
                return;
        }
 
        if (flags == Update::Decoration) {
-               d->metrics_info_.update_strategy = DecorationUpdate;
+               d->update_strategy_ = DecorationUpdate;
                buffer_.changed();
                return;
        }
@@ -524,12 +402,12 @@ void BufferView::processUpdateFlags(Update::flags flags)
                        return;
                }
                if (flags & Update::Decoration) {
-                       d->metrics_info_.update_strategy = DecorationUpdate;
+                       d->update_strategy_ = DecorationUpdate;
                        buffer_.changed();
                        return;
                }
                // no screen update is needed.
-               d->metrics_info_.update_strategy = NoScreenUpdate;
+               d->update_strategy_ = NoScreenUpdate;
                return;
        }
 
@@ -571,11 +449,10 @@ void BufferView::updateScrollbar()
                d->offset_ref_ = 0;
        }
 
-       LYXERR(Debug::GUI)
-               << BOOST_CURRENT_FUNCTION
+       LYXERR(Debug::GUI, BOOST_CURRENT_FUNCTION
                << " Updating scrollbar: height: " << t.paragraphs().size()
                << " curr par: " << d->cursor_.bottom().pit()
-               << " default height " << defaultRowHeight() << endl;
+               << " default height " << defaultRowHeight());
 
        // It would be better to fix the scrollbar to understand
        // values in [0..1] and divide everything by wh
@@ -624,8 +501,7 @@ ScrollbarParameters const & BufferView::scrollbarParameters() const
 
 void BufferView::scrollDocView(int value)
 {
-       LYXERR(Debug::GUI) << BOOST_CURRENT_FUNCTION
-                          << "[ value = " << value << "]" << endl;
+       LYXERR(Debug::GUI, BOOST_CURRENT_FUNCTION << "[ value = " << value << "]");
 
        Text & t = buffer_.text();
        TextMetrics & tm = d->text_metrics_[&t];
@@ -771,8 +647,13 @@ bool BufferView::moveToPosition(pit_type bottom_pit, pos_type bottom_pos,
                setCursor(doc_it);
                // set the current font.
                d->cursor_.setCurrentFont();
-               // center the screen on this new position.
-               center();
+               // To center the screen on this new position we need the
+               // paragraph position which is computed at draw() time.
+               // So we need a redraw!
+               buffer_.changed();
+               if (fitCursor())
+                       // We need another redraw because of the screen recentering.
+                       buffer_.changed();
        }
 
        return success;
@@ -854,11 +735,6 @@ FuncStatus BufferView::getStatus(FuncRequest const & cmd)
        case LFUN_LABEL_INSERT:
        case LFUN_INFO_INSERT:
        case LFUN_PARAGRAPH_GOTO:
-       // FIXME handle non-trivially
-       case LFUN_OUTLINE_UP:
-       case LFUN_OUTLINE_DOWN:
-       case LFUN_OUTLINE_IN:
-       case LFUN_OUTLINE_OUT:
        case LFUN_NOTE_NEXT:
        case LFUN_REFERENCE_NEXT:
        case LFUN_WORD_FIND:
@@ -991,13 +867,12 @@ Update::flags BufferView::dispatch(FuncRequest const & cmd)
        //       << [ cmd = " << cmd << "]" << endl;
 
        // Make sure that the cached BufferView is correct.
-       LYXERR(Debug::ACTION) << BOOST_CURRENT_FUNCTION
+       LYXERR(Debug::ACTION, BOOST_CURRENT_FUNCTION
                << " action[" << cmd.action << ']'
                << " arg[" << to_utf8(cmd.argument()) << ']'
                << " x[" << cmd.x << ']'
                << " y[" << cmd.y << ']'
-               << " button[" << cmd.button() << ']'
-               << endl;
+               << " button[" << cmd.button() << ']');
 
        Cursor & cur = d->cursor_;
        // Default Update flags.
@@ -1072,14 +947,11 @@ Update::flags BufferView::dispatch(FuncRequest const & cmd)
 
                        ParIterator par = b->getParFromID(id);
                        if (par == b->par_iterator_end()) {
-                               LYXERR(Debug::INFO)
-                                       << "No matching paragraph found! ["
-                                       << id << "]." << endl;
+                               LYXERR(Debug::INFO, "No matching paragraph found! [" << id << "].");
                        } else {
-                               LYXERR(Debug::INFO)
-                                       << "Paragraph " << par->id()
+                               LYXERR(Debug::INFO, "Paragraph " << par->id()
                                        << " found in buffer `"
-                                       << b->absFileName() << "'." << endl;
+                                       << b->absFileName() << "'.");
 
                                if (b == &buffer_) {
                                        // Set the cursor
@@ -1098,25 +970,6 @@ Update::flags BufferView::dispatch(FuncRequest const & cmd)
                break;
        }
 
-       case LFUN_OUTLINE_UP:
-               outline(OutlineUp, d->cursor_);
-               d->cursor_.text()->setCursor(d->cursor_, d->cursor_.pit(), 0);
-               updateLabels(buffer_);
-               break;
-       case LFUN_OUTLINE_DOWN:
-               outline(OutlineDown, d->cursor_);
-               d->cursor_.text()->setCursor(d->cursor_, d->cursor_.pit(), 0);
-               updateLabels(buffer_);
-               break;
-       case LFUN_OUTLINE_IN:
-               outline(OutlineIn, d->cursor_);
-               updateLabels(buffer_);
-               break;
-       case LFUN_OUTLINE_OUT:
-               outline(OutlineOut, d->cursor_);
-               updateLabels(buffer_);
-               break;
-
        case LFUN_NOTE_NEXT:
                gotoInset(this, NOTE_CODE, false);
                break;
@@ -1335,7 +1188,7 @@ Update::flags BufferView::dispatch(FuncRequest const & cmd)
                        //FIXME: updateMetrics() does not update paragraph position
                        // This is done at draw() time. So we need a redraw!
                        // But no screen update is needed.
-                       d->metrics_info_.update_strategy = NoScreenUpdate;
+                       d->update_strategy_ = NoScreenUpdate;
                        buffer_.changed();
                        p = getPos(cur, cur.boundary());
                }
@@ -1364,7 +1217,7 @@ Update::flags BufferView::dispatch(FuncRequest const & cmd)
                }
                // FIXME: we need to do a redraw again because of the selection
                // But no screen update is needed.
-               d->metrics_info_.update_strategy = NoScreenUpdate;
+               d->update_strategy_ = NoScreenUpdate;
                buffer_.changed();
                updateFlags = Update::Force | Update::FitCursor;
                break;
@@ -1493,23 +1346,10 @@ void BufferView::mouseEventDispatch(FuncRequest const & cmd0)
                if (!need_redraw)
                        return;
 
-               // if last metrics update was in singlepar mode, WorkArea::redraw() will
-               // not expose the button for redraw. We adjust here the metrics dimension
-               // to enable a full redraw in any case as this is not costly.
-               TextMetrics & tm = d->text_metrics_[&buffer_.text()];
-               std::pair<pit_type, ParagraphMetrics const *> firstpm = tm.first();
-               std::pair<pit_type, ParagraphMetrics const *> lastpm = tm.last();
-               int y1 = firstpm.second->position() - firstpm.second->ascent();
-               int y2 = lastpm.second->position() + lastpm.second->descent();
-               d->metrics_info_ = ViewMetricsInfo(firstpm.first, lastpm.first, y1, y2,
-                       FullScreenUpdate, buffer_.text().paragraphs().size());
-               // Reinitialize anchor to first pit.
-               d->anchor_ref_ = firstpm.first;
-               d->offset_ref_ = -y1;
-               LYXERR(Debug::PAINTING)
-                       << "Mouse hover detected at: (" << cmd.x << ", " << cmd.y << ")"
-                       << "\nTriggering redraw: y1: " << y1 << " y2: " << y2
-                       << " pit1: " << firstpm.first << " pit2: " << lastpm.first << endl;
+               LYXERR(Debug::PAINTING, "Mouse hover detected at: ("
+                       << cmd.x << ", " << cmd.y << ")");
+
+               d->update_strategy_ = DecorationUpdate;
 
                // This event (moving without mouse click) is not passed further.
                // This should be changed if it is further utilized.
@@ -1788,12 +1628,6 @@ pit_type BufferView::anchor_ref() const
 }
 
 
-ViewMetricsInfo const & BufferView::viewMetricsInfo()
-{
-       return d->metrics_info_;
-}
-
-
 bool BufferView::singleParUpdate()
 {
        Text & buftext = buffer_.text();
@@ -1812,17 +1646,13 @@ bool BufferView::singleParUpdate()
                // the singlePar optimisation.
                return false;
 
-       int y1 = pm.position() - pm.ascent();
-       int y2 = pm.position() + pm.descent();
-       d->metrics_info_ = ViewMetricsInfo(bottom_pit, bottom_pit, y1, y2,
-               SingleParUpdate, buftext.paragraphs().size());
-       LYXERR(Debug::PAINTING)
-               << BOOST_CURRENT_FUNCTION
-               << "\ny1: " << y1
-               << " y2: " << y2
+       d->update_strategy_ = SingleParUpdate;
+
+       LYXERR(Debug::PAINTING, BOOST_CURRENT_FUNCTION
+               << "\ny1: " << pm.position() - pm.ascent()
+               << " y2: " << pm.position() + pm.descent()
                << " pit: " << bottom_pit
-               << " singlepar: 1"
-               << endl;
+               << " singlepar: 1");
        return true;
 }
 
@@ -1894,21 +1724,18 @@ void BufferView::updateMetrics()
        // Take care of descent of last line
        y2 += tm.parMetrics(pit2).descent();
 
-       LYXERR(Debug::PAINTING)
-               << BOOST_CURRENT_FUNCTION
+       LYXERR(Debug::PAINTING, BOOST_CURRENT_FUNCTION
                << "\n y1: " << y1
                << " y2: " << y2
                << " pit1: " << pit1
                << " pit2: " << pit2
                << " npit: " << npit
-               << " singlepar: 0"
-               << endl;
+               << " singlepar: 0");
 
-       d->metrics_info_ = ViewMetricsInfo(pit1, pit2, y1, y2,
-               FullScreenUpdate, npit);
+       d->update_strategy_ = FullScreenUpdate;
 
        if (lyxerr.debugging(Debug::WORKAREA)) {
-               LYXERR(Debug::WORKAREA) << "BufferView::updateMetrics" << endl;
+               LYXERR(Debug::WORKAREA, "BufferView::updateMetrics");
                d->coord_cache_.dump();
        }
 }
@@ -2088,26 +1915,28 @@ Point BufferView::getPos(DocIterator const & dit, bool boundary) const
 
 void BufferView::draw(frontend::Painter & pain)
 {
-       LYXERR(Debug::PAINTING) << "\t\t*** START DRAWING ***" << endl;
+       LYXERR(Debug::PAINTING, "\t\t*** START DRAWING ***");
        Text & text = buffer_.text();
        TextMetrics const & tm = d->text_metrics_[&text];
-       int const y = d->metrics_info_.y1 
-               + tm.parMetrics(d->metrics_info_.p1).ascent();
+       int const y = - d->offset_ref_ + tm.parMetrics(d->anchor_ref_).ascent();
        PainterInfo pi(this, pain);
 
-       switch (d->metrics_info_.update_strategy) {
+       switch (d->update_strategy_) {
 
        case NoScreenUpdate:
                // If no screen painting is actually needed, only some the different
                // coordinates of insets and paragraphs needs to be updated.
-               pi.pain.setDrawingEnabled(false);
                pi.full_repaint = true;
+               pi.pain.setDrawingEnabled(false);
+               tm.draw(pi, 0, y);
                break;
 
        case SingleParUpdate:
-               // Only the current outermost paragraph will be redrawn.
                pi.full_repaint = false;
-               tm.drawParagraph(pi, d->metrics_info_.p1, 0, y);
+               // In general, only the current row of the outermost paragraph
+               // will be redrawn. Particular cases where selection spans
+               // multiple paragraph are correctly detected in TextMetrics.
+               tm.draw(pi, 0, y);
                break;
 
        case DecorationUpdate:
@@ -2119,25 +1948,20 @@ void BufferView::draw(frontend::Painter & pain)
                // The whole screen, including insets, will be refreshed.
                pi.full_repaint = true;
 
-               // Clear background (if not delegated to rows)
-               pain.fillRectangle(0, d->metrics_info_.y1, width_,
-                       d->metrics_info_.y2 - d->metrics_info_.y1,
+               // Clear background.
+               pain.fillRectangle(0, 0, width_, height_,
                        buffer_.inset().backgroundColor());
                tm.draw(pi, 0, y);
 
-               // and grey out above (should not happen later)
-               if (d->metrics_info_.y1 > 0)
-                       pain.fillRectangle(0, 0, width_,
-                               d->metrics_info_.y1, Color_bottomarea);
-
                // and possibly grey out below
-               if (d->metrics_info_.y2 < height_)
-                       pain.fillRectangle(0, d->metrics_info_.y2, width_,
-                               height_ - d->metrics_info_.y2, Color_bottomarea);
+               std::pair<pit_type, ParagraphMetrics const *> lastpm = tm.last();
+               int const y2 = lastpm.second->position() + lastpm.second->descent();
+               if (y2 < height_)
+                       pain.fillRectangle(0, y2, width_, height_ - y2, Color_bottomarea);
                break;
        }
 
-       LYXERR(Debug::PAINTING) << "\n\t\t*** END DRAWING  ***" << endl;
+       LYXERR(Debug::PAINTING, "\n\t\t*** END DRAWING  ***");
 }
 
 
@@ -2151,23 +1975,15 @@ void BufferView::message(docstring const & msg)
 void BufferView::showDialog(std::string const & name)
 {
        if (d->gui_)
-               d->gui_->showDialog(name);
-}
-
-
-void BufferView::showDialogWithData(std::string const & name,
-       std::string const & data)
-{
-       if (d->gui_)
-               d->gui_->showDialogWithData(name, data);
+               d->gui_->showDialog(name, string());
 }
 
 
-void BufferView::showInsetDialog(std::string const & name,
+void BufferView::showDialog(std::string const & name,
        std::string const & data, Inset * inset)
 {
        if (d->gui_)
-               d->gui_->showInsetDialog(name, data, inset);
+               d->gui_->showDialog(name, data, inset);
 }
 
 
@@ -2209,7 +2025,7 @@ docstring BufferView::contentsOfPlaintextFile(string const & f,
                        return docstring();
        }
 
-       if (!fs::is_readable(fname.toFilesystemEncoding())) {
+       if (!fname.isReadable()) {
                docstring const error = from_ascii(strerror(errno));
                docstring const file = makeDisplayPath(fname.absFilename(), 50);
                docstring const text =