]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/controllers/ControlTabular.C
fix crash due to invalidated iterator
[lyx.git] / src / frontends / controllers / ControlTabular.C
index af098ff8a99db57006e027369003f50087b265f7..afbd485c5025a78ae0fbf971bacaaa1f80383ddd 100644 (file)
 
 #include <config.h>
 
+#include "BufferView.h"
 #include "ControlTabular.h"
+#include "cursor.h"
 #include "funcrequest.h"
 #include "lyxrc.h"
 #include "paragraph.h"
 #include "insets/insettabular.h"
-#include "support/LAssert.h"
 
-using namespace lyx::support;
 
+using std::string;
+
+namespace lyx {
+namespace frontend {
 
 ControlTabular::ControlTabular(Dialog & parent)
-       : Dialog::Controller(parent), active_cell_(-1)
+       : Dialog::Controller(parent), active_cell_(LyXTabular::npos)
 {}
 
 
 bool ControlTabular::initialiseParams(string const & data)
 {
-       Buffer & buffer = kernel().buffer();
-
-       InsetTabular tmp(buffer);
-       int cell = InsetTabularMailer::string2params(data, tmp);
-       if (cell != -1) {
-               params_.reset(new LyXTabular(tmp.tabular));
-               active_cell_ = cell;
+       // try to get the current cell
+       BufferView const * const bv = kernel().bufferview();
+       if (bv) {
+               LCursor const & cur = bv->cursor();
+               // get the innermost tabular inset;
+               // assume that it is "ours"
+               for (int i = cur.depth() - 1; i >= 0; --i)
+                       if (cur[i].inset().lyxCode() == InsetBase::TABULAR_CODE) {
+                               active_cell_ = cur[i].idx();
+                               break;
+                       }
        }
+       InsetTabular tmp(kernel().buffer());
+       InsetTabularMailer::string2params(data, tmp);
+       params_.reset(new LyXTabular(tmp.tabular));
        return true;
 }
 
@@ -42,11 +53,11 @@ bool ControlTabular::initialiseParams(string const & data)
 void ControlTabular::clearParams()
 {
        params_.reset();
-       active_cell_ = -1;
+       active_cell_ = LyXTabular::npos;
 }
 
 
-int ControlTabular::getActiveCell() const
+LyXTabular::idx_type ControlTabular::getActiveCell() const
 {
        return active_cell_;
 }
@@ -54,7 +65,7 @@ int ControlTabular::getActiveCell() const
 
 LyXTabular const & ControlTabular::tabular() const
 {
-       Assert(params_.get());
+       BOOST_ASSERT(params_.get());
        return *params_.get();
 }
 
@@ -62,13 +73,13 @@ LyXTabular const & ControlTabular::tabular() const
 void ControlTabular::set(LyXTabular::Feature f, string const & arg)
 {
        string const data = featureAsString(f) + ' ' + arg;
-       kernel().dispatch(FuncRequest(LFUN_TABULAR_FEATURE, data));
+       kernel().dispatch(FuncRequest(getLfun(), data));
 }
 
 
 bool ControlTabular::useMetricUnits() const
 {
-       return lyxrc.default_papersize > PAPER_EXECUTIVEPAPER;
+       return lyxrc.default_papersize > PAPER_USEXECUTIVE;
 }
 
 
@@ -218,3 +229,6 @@ void ControlTabular::longTabular(bool yes)
        else
                set(LyXTabular::UNSET_LONGTABULAR);
 }
+
+} // namespace frontend
+} // namespace lyx