]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/xforms/FormTabular.C
fix crash with "save as"
[lyx.git] / src / frontends / xforms / FormTabular.C
index ad802a295b99d5bd62f0a3339092b1a652936cef..da022825ee3075ed3bc95f0e758be832eeea7446 100644 (file)
@@ -1,9 +1,11 @@
 /**
  * \file FormTabular.C
- * Copyright 2000-2001 The LyX Team.
- * See the file COPYING.
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
  *
- * \author Jürgen Vigna, jug@sad.it
+ * \author Jürgen Vigna
+ *
+ * Full author contact details are available in file CREDITS
  */
 
 #include <config.h>
 #pragma implementation
 #endif
 
-#include "debug.h"
-
+#include <vector>
+#include <algorithm>
+#include "ControlTabular.h"
+#include "xformsBC.h"
+#include "insets/insettabular.h"
 #include "FormTabular.h"
 #include "forms/form_tabular.h"
-#include "frontends/LyXView.h"
-#include "Dialogs.h"
-#include "buffer.h"
+#include "debug.h"
 #include "xforms_helpers.h"
-#include "lyxrc.h" // to set the default length values
+#include "gettext.h"
+#include "lyxrc.h"
 #include "helper_funcs.h"
 #include "input_validators.h"
-
-#include "insets/insettabular.h"
-
 #include "support/lstrings.h"
 
-#include <boost/bind.hpp>
-
-#include <functional>
 #include FORMS_H_LOCATION
 
-
 using std::vector;
 using std::bind2nd;
-using std::remove_if;
 
 
-FormTabular::FormTabular(LyXView * lv, Dialogs * d)
-       : FormInset(lv, d, _("Tabular Layout")),
-         inset_(0), actCell_(-1), closing_(false)
+typedef FormCB<ControlTabular, FormDB<FD_tabular> > base_class;
+
+FormTabular::FormTabular()
+       : base_class(_("Edit table settings")),
+       closing_(false), actCell_(-1)
 {
-       // let the dialog be shown
-       // This is a permanent connection so we won't bother
-       // storing a copy because we won't be disconnecting.
-       d->showTabular = boost::bind(&FormTabular::showInset, this, _1);
-       d->updateTabular = boost::bind(&FormTabular::updateInset, this, _1);
 }
 
 
@@ -64,51 +59,6 @@ void FormTabular::redraw()
 }
 
 
-FL_FORM * FormTabular::form() const
-{
-       if (dialog_.get())
-               return dialog_->form;
-       return 0;
-}
-
-
-void FormTabular::disconnect()
-{
-       inset_ = 0;
-       FormInset::disconnect();
-}
-
-
-void FormTabular::showInset(InsetTabular * inset)
-{
-       if (inset == 0) return;
-
-       // If connected to another inset, disconnect from it.
-       if (inset_ != inset) {
-               ih_.disconnect();
-               ih_ = inset->hideDialog.connect(boost::bind(&FormTabular::hide, this));
-               inset_ = inset;
-       }
-
-       show();
-}
-
-
-void FormTabular::updateInset(InsetTabular * inset)
-{
-       if (inset == 0 || inset_ == 0) return;
-
-       // If connected to another inset, disconnect from it.
-       if (inset_ != inset) {
-               ih_.disconnect();
-               ih_ = inset->hideDialog.connect(boost::bind(&FormTabular::hide, this));
-               inset_ = inset;
-       }
-
-       update();
-}
-
-
 void FormTabular::build()
 {
        dialog_.reset(build_tabular(this));
@@ -116,30 +66,53 @@ void FormTabular::build()
        // Allow the base class to control messages
        setMessageWidget(dialog_->text_warning);
 
+       // trigger an input event for cut&paste with middle mouse button.
        setPrehandler(dialog_->input_tabular_column);
        setPrehandler(dialog_->input_tabular_row);
 
+       // Create the contents of the unit choices; don't include the "%" terms.
+       vector<string> units_vec = getLatexUnits();
+       vector<string>::iterator ret =
+               remove_if(units_vec.begin(), units_vec.end(),
+                         bind2nd(contains_functor(), "%"));
+       units_vec.erase(ret, units_vec.end());
+       string const units = getStringFromVector(units_vec, "|");
+
+       // tabular options form
        tabular_options_.reset(build_tabular_options(this));
+
+       // column options form
        column_options_.reset(build_tabular_column(this));
-       cell_options_.reset(build_tabular_cell(this));
-       longtable_options_.reset(build_tabular_longtable(this));
 
        fl_set_input_return(column_options_->input_column_width,
                            FL_RETURN_END);
        fl_set_input_return(column_options_->input_special_alignment,
                            FL_RETURN_END);
 
+       // trigger an input event for cut&paste with middle mouse button.
        setPrehandler(column_options_->input_column_width);
        setPrehandler(column_options_->input_special_alignment);
 
+       fl_addto_choice(column_options_->choice_value_column_width,
+                       units.c_str());
+
+       // cell options form
+       cell_options_.reset(build_tabular_cell(this));
+
        fl_set_input_return(cell_options_->input_mcolumn_width,
                            FL_RETURN_END);
        fl_set_input_return(cell_options_->input_special_multialign,
                            FL_RETURN_END);
 
+       // trigger an input event for cut&paste with middle mouse button.
        setPrehandler(cell_options_->input_mcolumn_width);
        setPrehandler(cell_options_->input_special_multialign);
 
+       fl_addto_choice(cell_options_->choice_value_mcolumn_width,
+                       units.c_str());
+
+       longtable_options_.reset(build_tabular_longtable(this));
+
        fl_addto_tabfolder(dialog_->tabfolder, _("Tabular"),
                           tabular_options_->form);
        fl_addto_tabfolder(dialog_->tabfolder, _("Column/Row"),
@@ -149,28 +122,10 @@ void FormTabular::build()
        fl_addto_tabfolder(dialog_->tabfolder, _("LongTable"),
                           longtable_options_->form);
 
-       // Create the contents of the unit choices
-       // Don't include the "%" terms...
-       vector<string> units_vec = getLatexUnits();
-#if 0
-       for (vector<string>::iterator it = units_vec.begin();
-            it != units_vec.end(); ++it) {
-               if (contains(*it, "%"))
-                       it = units_vec.erase(it, it + 1) - 1;
-       }
-#else
-       vector<string>::iterator ret =
-               remove_if(units_vec.begin(), units_vec.end(),
-                         bind2nd(contains_functor(), "%"));
-       units_vec.erase(ret, units_vec.end());
-#endif
-
-       string units = getStringFromVector(units_vec, "|");
+       // work-around xforms bug re update of folder->x, folder->y coords.
+       setPrehandler(dialog_->tabfolder);
 
-       fl_addto_choice(column_options_->choice_value_column_width,
-                       units.c_str());
-       fl_addto_choice(cell_options_->choice_value_mcolumn_width,
-                       units.c_str());
+       //  FIXME: addReadOnly everything
 }
 
 
@@ -178,17 +133,16 @@ void FormTabular::update()
 {
        if (closing_)
                return;
-
-       if (!inset_ || !inset_->tabular.get())
-               return;
-
-       LyXTabular * tabular = inset_->tabular.get();
+       LyXTabular * tabular = controller().tabular();
+       InsetTabular * inset = controller().inset();
        int align;
        char buf[12];
        LyXLength pwidth;
        string special;
 
-       int cell = inset_->getActCell();
+       int cell = inset->getActCell();
        actCell_ = cell;
        int column = tabular->column_of_cell(cell) + 1;
        clearMessage();
@@ -234,7 +188,7 @@ void FormTabular::update()
                }
                pwidth = tabular->GetMColumnPWidth(cell);
                align = tabular->GetAlignment(cell);
-               if (!pwidth.zero() || (align == LYX_ALIGN_LEFT))
+               if (align == LYX_ALIGN_LEFT)
                        fl_set_button(cell_options_->radio_align_left, 1);
                else if (align == LYX_ALIGN_RIGHT)
                        fl_set_button(cell_options_->radio_align_right, 1);
@@ -254,13 +208,13 @@ void FormTabular::update()
 
                special = tabular->GetAlignSpecial(cell, LyXTabular::SET_SPECIAL_MULTI);
                fl_set_input(cell_options_->input_special_multialign, special.c_str());
-               bool const metric = lyxrc.default_papersize > 3;
+               bool const metric(controller().metric());
                string const default_unit = metric ? "cm" : "in";
                updateWidgetsFromLength(cell_options_->input_mcolumn_width,
                                        cell_options_->choice_value_mcolumn_width,
                                        pwidth, default_unit);
 
-               if (!lv_->buffer()->isReadonly()) {
+               if (bc().bp().isReadOnly()) {
                        setEnabled(cell_options_->input_special_multialign, true);
                        setEnabled(cell_options_->input_mcolumn_width, true);
                        setEnabled(cell_options_->choice_value_mcolumn_width, true);
@@ -270,9 +224,9 @@ void FormTabular::update()
                setEnabled(cell_options_->radio_valign_bottom, !pwidth.zero());
                setEnabled(cell_options_->radio_valign_center, !pwidth.zero());
 
-               setEnabled(cell_options_->radio_align_left,   pwidth.zero());
-               setEnabled(cell_options_->radio_align_right,  pwidth.zero());
-               setEnabled(cell_options_->radio_align_center, pwidth.zero());
+               setEnabled(cell_options_->radio_align_left,   true);
+               setEnabled(cell_options_->radio_align_right,  true);
+               setEnabled(cell_options_->radio_align_center, true);
        } else {
                fl_set_button(cell_options_->check_multicolumn, 0);
 
@@ -336,11 +290,11 @@ void FormTabular::update()
        special = tabular->GetAlignSpecial(cell, LyXTabular::SET_SPECIAL_COLUMN);
        fl_set_input(column_options_->input_special_alignment, special.c_str());
 
-       bool const isReadonly = lv_->buffer()->isReadonly();
+       bool const isReadonly = bc().bp().isReadOnly();
        setEnabled(column_options_->input_special_alignment, !isReadonly);
 
        pwidth = tabular->GetColumnPWidth(cell);
-       bool const metric = lyxrc.default_papersize > 3;
+       bool const metric = lyxrc.default_papersize > BufferParams::PAPER_EXECUTIVEPAPER;
        string const default_unit = metric ? "cm" : "in";
        updateWidgetsFromLength(column_options_->input_column_width,
                                column_options_->choice_value_column_width,
@@ -361,12 +315,15 @@ void FormTabular::update()
        fl_set_button(column_options_->radio_align_left, 0);
        fl_set_button(column_options_->radio_align_right, 0);
        fl_set_button(column_options_->radio_align_center, 0);
-       if (!pwidth.zero() || (align == LYX_ALIGN_LEFT))
+       fl_set_button(column_options_->radio_align_block, 0);
+       if (align == LYX_ALIGN_LEFT)
                fl_set_button(column_options_->radio_align_left, 1);
        else if (align == LYX_ALIGN_RIGHT)
                fl_set_button(column_options_->radio_align_right, 1);
-       else
+       else if (align == LYX_ALIGN_CENTER)
                fl_set_button(column_options_->radio_align_center, 1);
+       else
+               fl_set_button(column_options_->radio_align_block, 1);
        align = tabular->GetVAlignment(cell, true);
        fl_set_button(column_options_->radio_valign_top, 0);
        fl_set_button(column_options_->radio_valign_bottom, 0);
@@ -378,10 +335,10 @@ void FormTabular::update()
        else
                fl_set_button(column_options_->radio_valign_top, 1);
 
-       setEnabled(column_options_->radio_align_left,   pwidth.zero());
-       setEnabled(column_options_->radio_align_right,  pwidth.zero());
-       setEnabled(column_options_->radio_align_center, pwidth.zero());
-
+       setEnabled(column_options_->radio_align_left,   true);
+       setEnabled(column_options_->radio_align_right,  true);
+       setEnabled(column_options_->radio_align_center, true);
+       setEnabled(column_options_->radio_align_block,   !pwidth.zero());
        setEnabled(column_options_->radio_valign_top,    !pwidth.zero());
        setEnabled(column_options_->radio_valign_bottom, !pwidth.zero());
        setEnabled(column_options_->radio_valign_center, !pwidth.zero());
@@ -415,14 +372,14 @@ void FormTabular::update()
                if (ltt.set) {
                        fl_set_button(longtable_options_->check_head_2border_above,
                                      ltt.topDL);
-                       fl_set_button(longtable_options_->check_head_2border_above,
-                                     ltt.topDL);
+                       fl_set_button(longtable_options_->check_head_2border_below,
+                                     ltt.bottomDL);
                        use_empty = true;
                } else {
                        setEnabled(longtable_options_->check_head_2border_above, 0);
                        setEnabled(longtable_options_->check_head_2border_below, 0);
                        fl_set_button(longtable_options_->check_head_2border_above,0);
-                       fl_set_button(longtable_options_->check_head_2border_above,0);
+                       fl_set_button(longtable_options_->check_head_2border_below,0);
                        fl_set_button(longtable_options_->check_1head_empty,0);
                        setEnabled(longtable_options_->check_1head_empty, 0);
                        use_empty = false;
@@ -433,13 +390,13 @@ void FormTabular::update()
                if (ltt.set && (!ltt.empty || !use_empty)) {
                        fl_set_button(longtable_options_->check_1head_2border_above,
                                      ltt.topDL);
-                       fl_set_button(longtable_options_->check_1head_2border_above,
-                                     ltt.topDL);
+                       fl_set_button(longtable_options_->check_1head_2border_below,
+                                     ltt.bottomDL);
                } else {
                        setEnabled(longtable_options_->check_1head_2border_above, 0);
                        setEnabled(longtable_options_->check_1head_2border_below, 0);
                        fl_set_button(longtable_options_->check_1head_2border_above,0);
-                       fl_set_button(longtable_options_->check_1head_2border_above,0);
+                       fl_set_button(longtable_options_->check_1head_2border_below,0);
                        if (use_empty) {
                                fl_set_button(longtable_options_->check_1head_empty,ltt.empty);
                                if (ltt.empty)
@@ -452,14 +409,14 @@ void FormTabular::update()
                if (ltt.set) {
                        fl_set_button(longtable_options_->check_foot_2border_above,
                                      ltt.topDL);
-                       fl_set_button(longtable_options_->check_foot_2border_above,
-                                     ltt.topDL);
+                       fl_set_button(longtable_options_->check_foot_2border_below,
+                                     ltt.bottomDL);
                        use_empty = true;
                } else {
                        setEnabled(longtable_options_->check_foot_2border_above, 0);
                        setEnabled(longtable_options_->check_foot_2border_below, 0);
                        fl_set_button(longtable_options_->check_foot_2border_above,0);
-                       fl_set_button(longtable_options_->check_foot_2border_above,0);
+                       fl_set_button(longtable_options_->check_foot_2border_below,0);
                        fl_set_button(longtable_options_->check_lastfoot_empty, 0);
                        setEnabled(longtable_options_->check_lastfoot_empty, 0);
                        use_empty = false;
@@ -470,13 +427,13 @@ void FormTabular::update()
                if (ltt.set && (!ltt.empty || !use_empty)) {
                        fl_set_button(longtable_options_->check_lastfoot_2border_above,
                                      ltt.topDL);
-                       fl_set_button(longtable_options_->check_lastfoot_2border_above,
-                                     ltt.topDL);
+                       fl_set_button(longtable_options_->check_lastfoot_2border_below,
+                                     ltt.bottomDL);
                } else {
                        setEnabled(longtable_options_->check_lastfoot_2border_above,0);
                        setEnabled(longtable_options_->check_lastfoot_2border_below,0);
                        fl_set_button(longtable_options_->check_lastfoot_2border_above, 0);
-                       fl_set_button(longtable_options_->check_lastfoot_2border_above, 0);
+                       fl_set_button(longtable_options_->check_lastfoot_2border_below, 0);
                        if (use_empty) {
                                fl_set_button(longtable_options_->check_lastfoot_empty,
                                              ltt.empty);
@@ -489,17 +446,17 @@ void FormTabular::update()
        } else {
                fl_set_button(longtable_options_->check_lt_firsthead, 0);
                fl_set_button(longtable_options_->check_1head_2border_above, 0);
-               fl_set_button(longtable_options_->check_1head_2border_above, 0);
+               fl_set_button(longtable_options_->check_1head_2border_below, 0);
                fl_set_button(longtable_options_->check_1head_empty, 0);
                fl_set_button(longtable_options_->check_lt_head, 0);
                fl_set_button(longtable_options_->check_head_2border_above, 0);
-               fl_set_button(longtable_options_->check_head_2border_above, 0);
+               fl_set_button(longtable_options_->check_head_2border_below, 0);
                fl_set_button(longtable_options_->check_lt_foot, 0);
                fl_set_button(longtable_options_->check_foot_2border_above, 0);
-               fl_set_button(longtable_options_->check_foot_2border_above, 0);
+               fl_set_button(longtable_options_->check_foot_2border_below, 0);
                fl_set_button(longtable_options_->check_lt_lastfoot, 0);
                fl_set_button(longtable_options_->check_lastfoot_2border_above, 0);
-               fl_set_button(longtable_options_->check_lastfoot_2border_above, 0);
+               fl_set_button(longtable_options_->check_lastfoot_2border_below, 0);
                fl_set_button(longtable_options_->check_lastfoot_empty, 0);
                fl_set_button(longtable_options_->check_lt_newpage, 0);
        }
@@ -508,22 +465,21 @@ void FormTabular::update()
 }
 
 
-bool FormTabular::input(FL_OBJECT * ob, long)
+ButtonPolicy::SMInput FormTabular::input(FL_OBJECT * ob, long)
 {
-       if (!inset_)
-               return false;
-
        int s;
        LyXTabular::Feature num = LyXTabular::LAST_ACTION;
-       string special;;
+       string special;
 
-       int cell = inset_->getActCell();
+       InsetTabular * inset(controller().inset());
+       LyXTabular * tabular(controller().tabular());
+       int cell = inset->getActCell();
 
        // ugly hack to auto-apply the stuff that hasn't been
        // yet. don't let this continue to exist ...
        if (ob == dialog_->button_close) {
                closing_ = true;
-               LyXTabular * tabular = inset_->tabular.get();
                string str1 =
                        getLengthFromWidgets(column_options_->input_column_width,
                                             column_options_->choice_value_column_width);
@@ -542,47 +498,49 @@ bool FormTabular::input(FL_OBJECT * ob, long)
                        str2 = llen.asString();
                if (str1 != str2)
                        input(cell_options_->input_mcolumn_width, 0);
-               str1 = fl_get_input(column_options_->input_special_alignment);
+               str1 = getString(column_options_->input_special_alignment);
                str2 = tabular->GetAlignSpecial(cell, LyXTabular::SET_SPECIAL_COLUMN);
                if (str1 != str2)
                        input(column_options_->input_special_alignment, 0);
-               str1 = fl_get_input(cell_options_->input_special_multialign);
+               str1 = getString(cell_options_->input_special_multialign);
                str2 = tabular->GetAlignSpecial(cell, LyXTabular::SET_SPECIAL_MULTI);
                if (str1 != str2)
                        input(cell_options_->input_special_multialign, 0);
                closing_ = false;
-               ok();
-               return true;
+               controller().OKButton(); 
+               return ButtonPolicy::SMI_VALID;
        }
 
        if (actCell_ != cell) {
                update();
                postWarning(_("Wrong Cursor position, updated window"));
-               return false;
+               return ButtonPolicy::SMI_VALID;
        }
        // No point in processing directives that you can't do anything with
        // anyhow, so exit now if the buffer is read-only.
-       if (lv_->buffer()->isReadonly()) {
+       if (bc().bp().isReadOnly()) {
                update();
-               return false;
+               return ButtonPolicy::SMI_VALID;
        }
        if ((ob == column_options_->input_column_width) ||
            (ob == column_options_->choice_value_column_width)) {
                string const str =
                        getLengthFromWidgets(column_options_->input_column_width,
                                             column_options_->choice_value_column_width);
-               inset_->tabularFeatures(lv_->view(), LyXTabular::SET_PWIDTH, str);
+               controller().set(LyXTabular::SET_PWIDTH, str);
 
                //check if the input is valid
-               string const input =
-                       fl_get_input(column_options_->input_column_width);
+               string const input = getString(column_options_->input_column_width);
                if (!input.empty() && !isValidLength(input) && !isStrDbl(input)) {
                        postWarning(_("Invalid Length (valid example: 10mm)"));
-                       return false;
+                       return ButtonPolicy::SMI_INVALID;
                }
 
                update(); // update for alignment
-               return true;
+               return ButtonPolicy::SMI_VALID;
        }
 
        if ((ob == cell_options_->input_mcolumn_width) ||
@@ -590,17 +548,16 @@ bool FormTabular::input(FL_OBJECT * ob, long)
                string const str =
                        getLengthFromWidgets(cell_options_->input_mcolumn_width,
                                             cell_options_->choice_value_mcolumn_width);
-               inset_->tabularFeatures(lv_->view(), LyXTabular::SET_MPWIDTH, str);
+               controller().set(LyXTabular::SET_MPWIDTH, str);
 
                //check if the input is valid
-               string const input =
-                       fl_get_input(cell_options_->input_mcolumn_width);
+               string const input = getString(cell_options_->input_mcolumn_width);
                if (!input.empty() && !isValidLength(input) && !isStrDbl(input)) {
                        postWarning(_("Invalid Length (valid example: 10mm)"));
-                       return false;
+                       return ButtonPolicy::SMI_INVALID;
                }
                update(); // update for alignment
-               return true;
+               return ButtonPolicy::SMI_VALID;
        }
 
        if (ob == tabular_options_->button_append_row)
@@ -629,6 +586,8 @@ bool FormTabular::input(FL_OBJECT * ob, long)
                num = LyXTabular::ALIGN_RIGHT;
        else if (ob == column_options_->radio_align_center)
                num = LyXTabular::ALIGN_CENTER;
+       else if (ob == column_options_->radio_align_block)
+               num = LyXTabular::ALIGN_BLOCK;
        else if (ob == column_options_->radio_valign_top)
                num = LyXTabular::VALIGN_TOP;
        else if (ob == column_options_->radio_valign_bottom)
@@ -675,10 +634,10 @@ bool FormTabular::input(FL_OBJECT * ob, long)
        } else if (ob == longtable_options_->check_lt_newpage) {
                num = LyXTabular::SET_LTNEWPAGE;
        } else if (ob == column_options_->input_special_alignment) {
-               special = fl_get_input(column_options_->input_special_alignment);
+               special = getString(column_options_->input_special_alignment);
                num = LyXTabular::SET_SPECIAL_COLUMN;
        } else if (ob == cell_options_->input_special_multialign) {
-               special = fl_get_input(cell_options_->input_special_multialign);
+               special = getString(cell_options_->input_special_multialign);
                num = LyXTabular::SET_SPECIAL_MULTI;
        } else if (ob == cell_options_->check_border_top)
                num = LyXTabular::M_TOGGLE_LINE_TOP;
@@ -701,13 +660,14 @@ bool FormTabular::input(FL_OBJECT * ob, long)
        else if (ob == cell_options_->radio_valign_center)
                num = LyXTabular::M_VALIGN_CENTER;
        else
-               return false;
+               return ButtonPolicy::SMI_VALID;
 
-       inset_->tabularFeatures(lv_->view(), num, special);
+       controller().set(num, special);
        update();
 
-       return true;
+       return ButtonPolicy::SMI_VALID;
 }
 
 int FormTabular::checkLongtableOptions(FL_OBJECT * ob, string & special)
 {