]> git.lyx.org Git - lyx.git/blobdiff - src/BufferView.cpp
small simplification
[lyx.git] / src / BufferView.cpp
index 695950fe82eb3fb9c9a552840f037a5c180977f2..c95a19a0c78e6874fac2053718e4426fdce5a72f 100644 (file)
@@ -20,7 +20,6 @@
 #include "buffer_funcs.h"
 #include "BufferList.h"
 #include "BufferParams.h"
-#include "bufferview_funcs.h"
 #include "callback.h" // added for Dispatch functions
 #include "CoordCache.h"
 #include "CutAndPaste.h"
@@ -50,7 +49,6 @@
 #include "TexRow.h"
 #include "Text.h"
 #include "TextClass.h"
-#include "toc.h"
 #include "Undo.h"
 #include "VSpace.h"
 #include "WordLangTuple.h"
@@ -61,6 +59,7 @@
 #include "insets/InsetText.h"
 
 #include "frontends/alert.h"
+#include "frontends/Delegates.h"
 #include "frontends/FileDialog.h"
 #include "frontends/FontMetrics.h"
 #include "frontends/Painter.h"
@@ -76,6 +75,7 @@
 
 #include <boost/bind.hpp>
 #include <boost/current_function.hpp>
+#include <boost/next_prior.hpp>
 
 #include <functional>
 #include <vector>
@@ -204,14 +204,145 @@ void gotoInset(BufferView * bv, Inset_code 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;
+                       recordUndo(cur, Undo::ATOMIC, 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);
+                       recordUndo(cur, Undo::ATOMIC, pit, newpit - 1);
+                       pars.insert(dest, start, finish);
+                       start = boost::next(bgn, pit);
+                       pit = newpit - len;
+                       pars.erase(start, finish);
+                       break;
+               }
+               case OutlineIn:
+                       recordUndo(cur);
+                       for (; lit != lend; ++lit) {
+                               if ((*lit)->toclevel == thistoclevel + 1 &&
+                                   start->layout()->labeltype == (*lit)->labeltype) {
+                                       start->layout((*lit));
+                                       break;
+                               }
+                       }
+                       break;
+               case OutlineOut:
+                       recordUndo(cur);
+                       for (; lit != lend; ++lit) {
+                               if ((*lit)->toclevel == thistoclevel - 1 &&
+                                   start->layout()->labeltype == (*lit)->labeltype) {
+                                       start->layout((*lit));
+                                       break;
+                               }
+                       }
+                       break;
+               default:
+                       break;
+       }
+}
+
 } // anon namespace
 
 
+/////////////////////////////////////////////////////////////////////
+//
+// BufferView
+//
+/////////////////////////////////////////////////////////////////////
+
+
 BufferView::BufferView(Buffer & buf)
        : width_(0), height_(0), buffer_(buf), wh_(0),
          cursor_(*this),
          multiparsel_cache_(false), anchor_ref_(0), offset_ref_(0),
-         need_centering_(false), intl_(new Intl), last_inset_(0)
+         need_centering_(false), intl_(new Intl), last_inset_(0),
+         gui_(0)
 {
        xsel_cache_.set = false;
        intl_->initKeyMapper(lyxrc.use_kbmap);
@@ -832,21 +963,21 @@ Update::flags BufferView::dispatch(FuncRequest const & cmd)
        }
 
        case LFUN_OUTLINE_UP:
-               toc::outline(toc::Up, cursor_);
+               outline(OutlineUp, cursor_);
                cursor_.text()->setCursor(cursor_, cursor_.pit(), 0);
                updateLabels(buffer_);
                break;
        case LFUN_OUTLINE_DOWN:
-               toc::outline(toc::Down, cursor_);
+               outline(OutlineDown, cursor_);
                cursor_.text()->setCursor(cursor_, cursor_.pit(), 0);
                updateLabels(buffer_);
                break;
        case LFUN_OUTLINE_IN:
-               toc::outline(toc::In, cursor_);
+               outline(OutlineIn, cursor_);
                updateLabels(buffer_);
                break;
        case LFUN_OUTLINE_OUT:
-               toc::outline(toc::Out, cursor_);
+               outline(OutlineOut, cursor_);
                updateLabels(buffer_);
                break;
 
@@ -1844,4 +1975,48 @@ void BufferView::draw(frontend::Painter & pain)
                        height_ - metrics_info_.y2, Color::bottomarea);
 }
 
+
+void BufferView::message(docstring const & msg)
+{
+       if (gui_)
+               gui_->message(msg);
+}
+
+
+void BufferView::showDialog(std::string const & name)
+{
+       if (gui_)
+               gui_->showDialog(name);
+}
+
+
+void BufferView::showDialogWithData(std::string const & name,
+       std::string const & data)
+{
+       if (gui_)
+               gui_->showDialogWithData(name, data);
+}
+
+
+void BufferView::showInsetDialog(std::string const & name,
+       std::string const & data, Inset * inset)
+{
+       if (gui_)
+               gui_->showInsetDialog(name, data, inset);
+}
+
+
+void BufferView::updateDialog(std::string const & name, std::string const & data)
+{
+       if (gui_)
+               gui_->updateDialog(name, data);
+}
+
+
+void BufferView::setGuiDelegate(frontend::GuiBufferViewDelegate * gui)
+{
+       gui_ = gui;
+}
+
+
 } // namespace lyx