]> 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 8f83e0f1ddb671a913c7e5947f0344ad702276f4..afbd485c5025a78ae0fbf971bacaaa1f80383ddd 100644 (file)
@@ -1,4 +1,3 @@
-// -*- C++ -*-
 /**
  * \file ControlTabular.C
  * This file is part of LyX, the document processor.
@@ -6,36 +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 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.get()));
-               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;
 }
 
@@ -43,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_;
 }
@@ -55,7 +65,7 @@ int ControlTabular::getActiveCell() const
 
 LyXTabular const & ControlTabular::tabular() const
 {
-       lyx::Assert(params_.get());
+       BOOST_ASSERT(params_.get());
        return *params_.get();
 }
 
@@ -63,19 +73,19 @@ 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 > BufferParams::PAPER_EXECUTIVEPAPER;
+       return lyxrc.default_papersize > PAPER_USEXECUTIVE;
 }
 
 
 void ControlTabular::toggleTopLine()
 {
-       if (tabular().IsMultiColumn(getActiveCell()))
+       if (tabular().isMultiColumn(getActiveCell()))
                set(LyXTabular::M_TOGGLE_LINE_TOP);
        else
                set(LyXTabular::TOGGLE_LINE_TOP);
@@ -84,7 +94,7 @@ void ControlTabular::toggleTopLine()
 
 void ControlTabular::toggleBottomLine()
 {
-       if (tabular().IsMultiColumn(getActiveCell()))
+       if (tabular().isMultiColumn(getActiveCell()))
                set(LyXTabular::M_TOGGLE_LINE_BOTTOM);
        else
                set(LyXTabular::TOGGLE_LINE_BOTTOM);
@@ -93,7 +103,7 @@ void ControlTabular::toggleBottomLine()
 
 void ControlTabular::toggleLeftLine()
 {
-       if (tabular().IsMultiColumn(getActiveCell()))
+       if (tabular().isMultiColumn(getActiveCell()))
                set(LyXTabular::M_TOGGLE_LINE_LEFT);
        else
                set(LyXTabular::TOGGLE_LINE_LEFT);
@@ -102,7 +112,7 @@ void ControlTabular::toggleLeftLine()
 
 void ControlTabular::toggleRightLine()
 {
-       if (tabular().IsMultiColumn(getActiveCell()))
+       if (tabular().isMultiColumn(getActiveCell()))
                set(LyXTabular::M_TOGGLE_LINE_RIGHT);
        else
                set(LyXTabular::TOGGLE_LINE_RIGHT);
@@ -111,7 +121,7 @@ void ControlTabular::toggleRightLine()
 
 void ControlTabular::setSpecial(string const & special)
 {
-       if (tabular().IsMultiColumn(getActiveCell()))
+       if (tabular().isMultiColumn(getActiveCell()))
                set(LyXTabular::SET_SPECIAL_MULTI, special);
        else
                set(LyXTabular::SET_SPECIAL_COLUMN, special);
@@ -120,7 +130,7 @@ void ControlTabular::setSpecial(string const & special)
 
 void ControlTabular::setWidth(string const & width)
 {
-       if (tabular().IsMultiColumn(getActiveCell()))
+       if (tabular().isMultiColumn(getActiveCell()))
                set(LyXTabular::SET_MPWIDTH, width);
        else
                set(LyXTabular::SET_PWIDTH, width);
@@ -178,7 +188,7 @@ void ControlTabular::halign(ControlTabular::HALIGN h)
                        break;
        }
 
-       if (tabular().IsMultiColumn(getActiveCell()))
+       if (tabular().isMultiColumn(getActiveCell()))
                set(multi_num);
        else
                set(num);
@@ -187,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;
@@ -205,7 +215,7 @@ void ControlTabular::valign(ControlTabular::VALIGN v)
                        break;
        }
 
-       if (tabular().IsMultiColumn(getActiveCell()))
+       if (tabular().isMultiColumn(getActiveCell()))
                set(multi_num);
        else
                set(num);
@@ -219,3 +229,6 @@ void ControlTabular::longTabular(bool yes)
        else
                set(LyXTabular::UNSET_LONGTABULAR);
 }
+
+} // namespace frontend
+} // namespace lyx