]> git.lyx.org Git - lyx.git/blobdiff - src/BufferView.C
Hopefully temporary fix for the Tabular crash problem. Of course, this is not the...
[lyx.git] / src / BufferView.C
index 431585ba7b8879a85b20cc1a8aecf621932ae331..2d1846ab94ee4f821510b53de6292023dee377cc 100644 (file)
 #include "insets/insettext.h"
 
 #include "frontends/Alert.h"
-#include "frontends/Clipboard.h"
-#include "frontends/Dialogs.h"
 #include "frontends/FileDialog.h"
-#include "frontends/font_metrics.h"
-#include "frontends/Gui.h"
-#include "frontends/LyXView.h"
-#include "frontends/Selection.h"
+#include "frontends/FontMetrics.h"
 
 #include "graphics/Previews.h"
 
 #include <vector>
 
 
-using lyx::frontend::Clipboard;
-using lyx::frontend::Gui;
-
+using lyx::CoordCache;
 using lyx::docstring;
 using lyx::pos_type;
+using lyx::Point;
 
 using lyx::support::addPath;
 using lyx::support::bformat;
@@ -109,8 +103,7 @@ using std::mem_fun_ref;
 using std::string;
 using std::vector;
 
-extern BufferList bufferlist;
-
+namespace Alert = lyx::frontend::Alert;
 
 namespace {
 
@@ -133,10 +126,11 @@ T * getInsetByCode(LCursor & cur, InsetBase::Code code)
 } // anon namespace
 
 
-BufferView::BufferView(LyXView * owner)
-       : owner_(owner), buffer_(0), wh_(0),
+BufferView::BufferView()
+       : buffer_(0), wh_(0),
          cursor_(*this),
-         multiparsel_cache_(false), anchor_ref_(0), offset_ref_(0)
+         multiparsel_cache_(false), anchor_ref_(0), offset_ref_(0),
+         intl_(new Intl)
 {
        xsel_cache_.set = false;
 
@@ -149,17 +143,13 @@ BufferView::BufferView(LyXView * owner)
                        saved_positions[bm->get<0>()] = Position( bm->get<1>(), bm->get<2>(), bm->get<3>() );
        // and then clear them
        bmList.clear();
-}
 
-
-BufferView::~BufferView()
-{
+       intl_->initKeyMapper(lyxrc.use_kbmap);
 }
 
 
-void BufferView::unsetXSel()
+BufferView::~BufferView()
 {
-       xsel_cache_.set = false;
 }
 
 
@@ -169,12 +159,6 @@ Buffer * BufferView::buffer() const
 }
 
 
-LyXView * BufferView::owner() const
-{
-       return owner_;
-}
-
-
 void BufferView::setBuffer(Buffer * b)
 {
        lyxerr[Debug::INFO] << BOOST_CURRENT_FUNCTION
@@ -203,7 +187,7 @@ void BufferView::setBuffer(Buffer * b)
                lyxerr[Debug::INFO] << BOOST_CURRENT_FUNCTION
                                    << " No Buffer!" << endl;
                // We are closing the buffer, use the first buffer as current
-               buffer_ = bufferlist.first();
+               buffer_ = theBufferList().first();
        } else {
                // Set current buffer
                buffer_ = b;
@@ -250,7 +234,7 @@ bool BufferView::loadLyXFile(string const & filename, bool tolastfiles)
                s = filename;
 
        // File already open?
-       if (bufferlist.exists(s)) {
+       if (theBufferList().exists(s)) {
                docstring const file = makeDisplayPath(s, 20);
                docstring text = bformat(_("The document %1$s is already "
                                                     "loaded.\n\nDo you want to revert "
@@ -259,11 +243,11 @@ bool BufferView::loadLyXFile(string const & filename, bool tolastfiles)
                        text, 0, 1,  _("&Revert"), _("&Switch to document"));
 
                if (ret != 0) {
-                       setBuffer(bufferlist.getBuffer(s));
+                       setBuffer(theBufferList().getBuffer(s));
                        return true;
                }
                // FIXME: should be LFUN_REVERT
-               if (!bufferlist.close(bufferlist.getBuffer(s), false))
+               if (!theBufferList().close(theBufferList().getBuffer(s), false))
                        return false;
                // Fall through to new load. (Asger)
        }
@@ -271,9 +255,9 @@ bool BufferView::loadLyXFile(string const & filename, bool tolastfiles)
        Buffer * b = 0;
 
        if (found) {
-               b = bufferlist.newBuffer(s);
+               b = theBufferList().newBuffer(s);
                if (!::loadLyXFile(b, s)) {
-                       bufferlist.release(b);
+                       theBufferList().release(b);
                        return false;
                }
        } else {
@@ -327,7 +311,7 @@ bool BufferView::loadLyXFile(string const & filename, bool tolastfiles)
 void BufferView::reload()
 {
        string const fn = buffer_->fileName();
-       if (bufferlist.close(buffer_, false))
+       if (theBufferList().close(buffer_, false))
                loadLyXFile(fn);
 }
 
@@ -348,10 +332,11 @@ void BufferView::resize()
 bool BufferView::fitCursor()
 {
        if (bv_funcs::status(this, cursor_) == bv_funcs::CUR_INSIDE) {
-               LyXFont const font = cursor_.getFont();
-               int const asc = font_metrics::maxAscent(font);
-               int const des = font_metrics::maxDescent(font);
-               Point const p = bv_funcs::getPos(cursor_, cursor_.boundary());
+               lyx::frontend::FontMetrics const & fm =
+                       theFontMetrics(cursor_.getFont());
+               int const asc = fm.maxAscent();
+               int const des = fm.maxDescent();
+               Point const p = bv_funcs::getPos(*this, cursor_, cursor_.boundary());
                if (p.y_ - asc >= 0 && p.y_ + des < height_)
                        return false;
        }
@@ -507,7 +492,7 @@ void BufferView::setCursorFromScrollbar()
                cur.clearSelection();
                break;
        case bv_funcs::CUR_INSIDE:
-               int const y = bv_funcs::getPos(cur, cur.boundary()).y_;
+               int const y = bv_funcs::getPos(*this, cur, cur.boundary()).y_;
                int const newy = min(last, max(y, first));
                if (y != newy) {
                        cur.reset(buffer_->inset());
@@ -517,15 +502,9 @@ void BufferView::setCursorFromScrollbar()
 }
 
 
-bool BufferView::available() const
+Change const BufferView::getCurrentChange() const
 {
-       return buffer_;
-}
-
-
-Change const BufferView::getCurrentChange()
-{
-       if (!buffer_->params().tracking_changes || !cursor_.selection())
+       if (!cursor_.selection())
                return Change(Change::UNCHANGED);
 
        DocIterator dit = cursor_.selectionBegin();
@@ -558,10 +537,10 @@ void BufferView::restorePosition(unsigned int i)
 
        if (fname != buffer_->fileName()) {
                Buffer * b = 0;
-               if (bufferlist.exists(fname))
-                       b = bufferlist.getBuffer(fname);
+               if (theBufferList().exists(fname))
+                       b = theBufferList().getBuffer(fname);
                else {
-                       b = bufferlist.newBuffer(fname);
+                       b = theBufferList().newBuffer(fname);
                        // Don't ask, just load it
                        ::loadLyXFile(b, fname);
                }
@@ -606,13 +585,12 @@ void BufferView::switchKeyMap()
        if (!lyxrc.rtl_support)
                return;
 
-       Intl & intl = owner_->getIntl();
        if (getLyXText()->real_current_font.isRightToLeft()) {
-               if (intl.keymap == Intl::PRIMARY)
-                       intl.keyMapSec();
+               if (intl_->keymap == Intl::PRIMARY)
+                       intl_->keyMapSec();
        } else {
-               if (intl.keymap == Intl::SECONDARY)
-                       intl.keyMapPrim();
+               if (intl_->keymap == Intl::SECONDARY)
+                       intl_->keyMapPrim();
        }
 }
 
@@ -630,7 +608,7 @@ void BufferView::center()
        bot.text()->redoParagraph(pit);
        Paragraph const & par = bot.text()->paragraphs()[pit];
        anchor_ref_ = pit;
-       offset_ref_ = bv_funcs::coordOffset(cursor_, cursor_.boundary()).y_
+       offset_ref_ = bv_funcs::coordOffset(*this, cursor_, cursor_.boundary()).y_
                + par.ascent() - height_ / 2;
 }
 
@@ -686,26 +664,25 @@ FuncStatus BufferView::getStatus(FuncRequest const & cmd)
        case LFUN_BOOKMARK_GOTO:
                flag.enabled(isSavedPosition(convert<unsigned int>(lyx::to_utf8(cmd.argument()))));
                break;
+
        case LFUN_CHANGES_TRACK:
                flag.enabled(true);
-               flag.setOnOff(buffer_->params().tracking_changes);
+               flag.setOnOff(buffer_->params().trackChanges);
                break;
 
        case LFUN_CHANGES_OUTPUT: {
                OutputParams runparams;
                LaTeXFeatures features(*buffer_, buffer_->params(), runparams);
-               flag.enabled(buffer_ && buffer_->params().tracking_changes
-                       && features.isAvailable("dvipost"));
-               flag.setOnOff(buffer_->params().output_changes);
+               flag.enabled(buffer_ && features.isAvailable("dvipost"));
+               flag.setOnOff(buffer_->params().outputChanges);
                break;
        }
 
        case LFUN_CHANGES_MERGE:
-       case LFUN_CHANGE_ACCEPT: // what about these two
-       case LFUN_CHANGE_REJECT: // what about these two
+       case LFUN_CHANGE_NEXT:
        case LFUN_ALL_CHANGES_ACCEPT:
        case LFUN_ALL_CHANGES_REJECT:
-               flag.enabled(buffer_ && buffer_->params().tracking_changes);
+               flag.enabled(buffer_); // FIXME: Change tracking (MG)
                break;
 
        case LFUN_BUFFER_TOGGLE_COMPRESSION: {
@@ -789,13 +766,13 @@ bool BufferView::dispatch(FuncRequest const & cmd)
                break;
 
        case LFUN_LABEL_GOTO: {
-               string label = lyx::to_utf8(cmd.argument());
+               docstring label = cmd.argument();
                if (label.empty()) {
                        InsetRef * inset =
                                getInsetByCode<InsetRef>(cursor_,
                                                         InsetBase::REF_CODE);
                        if (inset) {
-                               label = inset->getContents();
+                               label = lyx::from_utf8(inset->getContents());
                                savePosition(0);
                        }
                }
@@ -857,18 +834,21 @@ bool BufferView::dispatch(FuncRequest const & cmd)
        }
 
        case LFUN_CHANGES_TRACK:
-               trackChanges();
+               buffer_->params().trackChanges = !buffer_->params().trackChanges;
                break;
 
        case LFUN_CHANGES_OUTPUT: {
-               bool const state = buffer_->params().output_changes;
-               buffer_->params().output_changes = !state;
+               buffer_->params().outputChanges = !buffer_->params().outputChanges;
                break;
        }
 
+       case LFUN_CHANGE_NEXT:
+               lyx::find::findNextChange(this);
+               break;
+
        case LFUN_CHANGES_MERGE:
                if (lyx::find::findNextChange(this))
-                       owner_->getDialogs().show("changes");
+                       showDialog("changes");
                break;
 
        case LFUN_ALL_CHANGES_ACCEPT: {
@@ -1020,18 +1000,16 @@ bool BufferView::dispatch(FuncRequest const & cmd)
 }
 
 
-void BufferView::selectionRequested()
+docstring const BufferView::requestSelection()
 {
-       static docstring sel;
-
        if (!buffer_)
-               return;
+               return docstring();
 
        LCursor & cur = cursor_;
 
        if (!cur.selection()) {
                xsel_cache_.set = false;
-               return;
+               return docstring();
        }
 
        if (!xsel_cache_.set ||
@@ -1041,14 +1019,13 @@ void BufferView::selectionRequested()
                xsel_cache_.cursor = cur.top();
                xsel_cache_.anchor = cur.anchor_.top();
                xsel_cache_.set = cur.selection();
-               sel = cur.selectionAsString(false);
-               if (!sel.empty())
-                       owner_->gui().selection().put(sel);
+               return cur.selectionAsString(false);
        }
+       return docstring();
 }
 
 
-void BufferView::selectionLost()
+void BufferView::clearSelection()
 {
        if (buffer_) {
                cursor_.clearSelection();
@@ -1172,10 +1149,10 @@ void BufferView::setCursorFromRow(int row)
 }
 
 
-void BufferView::gotoLabel(string const & label)
+void BufferView::gotoLabel(docstring const & label)
 {
        for (InsetIterator it = inset_iterator_begin(buffer_->inset()); it; ++it) {
-               vector<string> labels;
+               vector<docstring> labels;
                it->getLabelList(*buffer_, labels);
                if (find(labels.begin(),labels.end(),label) != labels.end()) {
                        setCursor(it);
@@ -1208,12 +1185,6 @@ int BufferView::workHeight() const
 }
 
 
-LyXText * BufferView::text() const
-{
-       return buffer_ ? &buffer_->text() : 0;
-}
-
-
 void BufferView::setCursor(DocIterator const & dit)
 {
        size_t const n = dit.depth();
@@ -1282,12 +1253,6 @@ lyx::pit_type BufferView::anchor_ref() const
 }
 
 
-int BufferView::offset_ref() const
-{
-       return offset_ref_;
-}
-
-
 ViewMetricsInfo const & BufferView::viewMetricsInfo()
 {
        return metrics_info_;
@@ -1296,8 +1261,15 @@ ViewMetricsInfo const & BufferView::viewMetricsInfo()
 
 void BufferView::updateMetrics(bool singlepar)
 {
+       // FIXME (Abdel 19/10/2006):
+       // There's something fishy in tabular. The coord_cache_ is not
+       // correctly reconstructed when a character is trying to be inserted.
+       // Not clearing out the coord_cache_ fixes the crash but I don't know
+       // what side effect this could have on other insets.
+       //
        // Remove old position cache
-       theCoords.clear();
+       // coord_cache_.clear();
+
        LyXText & buftext = buffer_->text();
        lyx::pit_type size = int(buftext.paragraphs().size());
 
@@ -1361,7 +1333,7 @@ void BufferView::updateMetrics(bool singlepar)
 
        // The coordinates of all these paragraphs are correct, cache them
        int y = y1;
-       CoordCache::InnerParPosCache & parPos = theCoords.parPos()[&buftext];
+       CoordCache::InnerParPosCache & parPos = coord_cache_.parPos()[&buftext];
        for (lyx::pit_type pit = pit1; pit <= pit2; ++pit) {
                Paragraph const & par = buftext.getPar(pit);
                y += par.ascent();
@@ -1403,31 +1375,32 @@ void BufferView::menuInsertLyXFile(string const & filenm)
 
        if (filename.empty()) {
                // Launch a file browser
+               // FIXME UNICODE
                string initpath = lyxrc.document_path;
 
-               if (available()) {
+               if (buffer_) {
                        string const trypath = buffer_->filePath();
                        // If directory is writeable, use this as default.
                        if (isDirWriteable(trypath))
                                initpath = trypath;
                }
 
-               FileDialog fileDlg(lyx::to_utf8(_("Select LyX document to insert")),
+               // FIXME UNICODE
+               FileDialog fileDlg(_("Select LyX document to insert"),
                        LFUN_FILE_INSERT,
-                       make_pair(string(lyx::to_utf8(_("Documents|#o#O"))),
-                                 string(lyxrc.document_path)),
-                       make_pair(string(lyx::to_utf8(_("Examples|#E#e"))),
-                                 string(addPath(package().system_support(), "examples"))));
+                       make_pair(_("Documents|#o#O"), lyx::from_utf8(lyxrc.document_path)),
+                       make_pair(_("Examples|#E#e"), lyx::from_utf8(addPath(package().system_support(), "examples"))));
 
                FileDialog::Result result =
-                       fileDlg.open(initpath,
-                                    FileFilterList(lyx::to_utf8(_("LyX Documents (*.lyx)"))),
-                                    string());
+                       fileDlg.open(lyx::from_utf8(initpath),
+                                    FileFilterList(_("LyX Documents (*.lyx)")),
+                                    docstring());
 
                if (result.first == FileDialog::Later)
                        return;
 
-               filename = result.second;
+               // FIXME UNICODE
+               filename = lyx::to_utf8(result.second);
 
                // check selected filename
                if (filename.empty()) {
@@ -1462,33 +1435,3 @@ void BufferView::menuInsertLyXFile(string const & filenm)
        buffer_->errors("Parse");
        resize();
 }
-
-
-void BufferView::trackChanges()
-{
-       bool const tracking = buffer_->params().tracking_changes;
-
-       if (!tracking) {
-               for_each(buffer_->par_iterator_begin(),
-                        buffer_->par_iterator_end(),
-                        bind(&Paragraph::trackChanges, _1, Change::UNCHANGED));
-               buffer_->params().tracking_changes = true;
-
-               // We cannot allow undos beyond the freeze point
-               buffer_->undostack().clear();
-       } else {
-               cursor_.setCursor(doc_iterator_begin(buffer_->inset()));
-               if (lyx::find::findNextChange(this)) {
-                       owner_->getDialogs().show("changes");
-                       return;
-               }
-
-               for_each(buffer_->par_iterator_begin(),
-                        buffer_->par_iterator_end(),
-                        mem_fun_ref(&Paragraph::untrackChanges));
-
-               buffer_->params().tracking_changes = false;
-       }
-
-       buffer_->redostack().clear();
-}