]> 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 40d4854fb023517e56064861d9bd64e39becf5d0..afbd485c5025a78ae0fbf971bacaaa1f80383ddd 100644 (file)
@@ -1,4 +1,3 @@
-// -*- C++ -*-
 /**
  * \file ControlTabular.C
  * This file is part of LyX, the document processor.
@@ -6,37 +5,47 @@
  *
  * \author John Levon
  *
- * Full author contact details are available in file CREDITS
+ * Full author contact details are available in file CREDITS.
  */
 
 #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();
-       if (!buffer)
-               return false;
-
-       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;
 }
 
@@ -44,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_;
 }
@@ -56,7 +65,7 @@ int ControlTabular::getActiveCell() const
 
 LyXTabular const & ControlTabular::tabular() const
 {
-       Assert(params_.get());
+       BOOST_ASSERT(params_.get());
        return *params_.get();
 }
 
@@ -64,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;
 }
 
 
@@ -188,17 +197,17 @@ void ControlTabular::halign(ControlTabular::HALIGN h)
 
 void ControlTabular::valign(ControlTabular::VALIGN v)
 {
-       LyXTabular::Feature num = LyXTabular::VALIGN_CENTER;
-       LyXTabular::Feature multi_num = LyXTabular::M_VALIGN_CENTER;
+       LyXTabular::Feature num = LyXTabular::VALIGN_MIDDLE;
+       LyXTabular::Feature multi_num = LyXTabular::M_VALIGN_MIDDLE;
 
        switch (v) {
                case TOP:
                        num = LyXTabular::VALIGN_TOP;
                        multi_num = LyXTabular::M_VALIGN_TOP;
                        break;
-               case VCENTER:
-                       num = LyXTabular::VALIGN_CENTER;
-                       multi_num = LyXTabular::M_VALIGN_CENTER;
+               case MIDDLE:
+                       num = LyXTabular::VALIGN_MIDDLE;
+                       multi_num = LyXTabular::M_VALIGN_MIDDLE;
                        break;
                case BOTTOM:
                        num = LyXTabular::VALIGN_BOTTOM;
@@ -220,3 +229,6 @@ void ControlTabular::longTabular(bool yes)
        else
                set(LyXTabular::UNSET_LONGTABULAR);
 }
+
+} // namespace frontend
+} // namespace lyx