]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/xforms/FormParagraph.C
layout as string
[lyx.git] / src / frontends / xforms / FormParagraph.C
index 1006f38376fd0a9915fe338d3b5a957b97deb2a6..648114776a5788625e64ba97ba4bc2b4cdc74d5a 100644 (file)
 #include "FormParagraph.h"
 #include "form_paragraph.h"
 #include "Dialogs.h"
-#include "support/lstrings.h" 
 #include "Liason.h"
 #include "LyXView.h"
 #include "buffer.h"
 #include "lyxtext.h"
 #include "xforms_helpers.h"
+#include "lyxrc.h" // to set the deafult length values
 #include "BufferView.h"
+#include "lyxtextclasslist.h"
 #include "Spacing.h"
 #include "ParagraphParameters.h"
 #include "input_validators.h"
+#include "helper_funcs.h"
+
+#include "support/lstrings.h"
+
+#include <functional>
 
 using Liason::setMinibuffer;
 using SigC::slot;
+using std::vector;
+using std::bind2nd;
+using std::remove_if;
 
 
 FormParagraph::FormParagraph(LyXView * lv, Dialogs * d)
@@ -62,13 +71,7 @@ void FormParagraph::disconnect()
 
 Paragraph const * FormParagraph::getCurrentParagraph() const
 {
-       LyXText * text = 0;
-
-       if (lv_->view()->theLockingInset())
-               text = lv_->view()->theLockingInset()->getLyXText(lv_->view());
-       if (!text)
-               text = lv_->view()->text;
-       return text->cursor.par();
+       return lv_->view()->getLyXText()->cursor.par();
 }
 
 
@@ -78,6 +81,12 @@ void FormParagraph::changedParagraph()
        Paragraph const * const p = getCurrentParagraph();
        if (p == 0 || p == par_)
                return;
+#ifdef WITH_WARNINGS
+#warning Please someone of you GUII people have a look at this!
+#endif
+       // shouldn't we chage the par_ pointer too?
+       // anyway for me the below function does just nothing!
+       // (Jug 20020108)
 
        // For now don't bother checking if the params are different,
        // just activate the Apply button
@@ -87,8 +96,8 @@ void FormParagraph::changedParagraph()
 
 void FormParagraph::redraw()
 {
-       if( form() && form()->visible )
-               fl_redraw_form( form() );
+       if (form() && form()->visible)
+               fl_redraw_form(form());
 }
 
 
@@ -120,6 +129,29 @@ void FormParagraph::build()
     fl_set_input_return(dialog_->input_linespacing, FL_RETURN_CHANGED);
     fl_set_input_filter(dialog_->input_linespacing, fl_unsigned_float_filter);
 
+    // Create the contents of the unit choices
+    // Don't include the "%" terms...
+#warning A bit dangerous... (Lgb)
+    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
+    // Something similar to this is a better way to erase
+    vector<string>::iterator del =
+           remove_if(units_vec.begin(), units_vec.end(),
+                     bind2nd(contains_functor(), "%"));
+    units_vec.erase(del, units_vec.end());
+#endif
+    
+    string units = getStringFromVector(units_vec, "|");
+
+    fl_addto_choice(dialog_->choice_value_space_above,  units.c_str());
+    fl_addto_choice(dialog_->choice_value_space_below, units.c_str());
+
     // Manage the ok, apply, restore and cancel/close buttons
     bc_.setOK(dialog_->button_ok);
     bc_.setApply(dialog_->button_apply);
@@ -196,10 +228,16 @@ void FormParagraph::apply()
        space_top = VSpace(VSpace::VFILL);
        break;
     case 7:
+    {
+           string const length =
+                   getLengthFromWidgets(dialog_->input_space_above,
+                                        dialog_->choice_value_space_above);
        space_top =
-               VSpace(LyXGlueLength(fl_get_input(dialog_->input_space_above)));
+               VSpace(LyXGlueLength(length));
        break;
     }
+    }
+
     if (fl_get_button (dialog_->check_space_above))
        space_top.setKeep (true);
     switch (fl_get_choice (dialog_->choice_space_below)) {
@@ -222,8 +260,10 @@ void FormParagraph::apply()
        space_bottom = VSpace(VSpace::VFILL);
        break;
     case 7:
-       space_bottom =
-               VSpace(LyXGlueLength(fl_get_input(dialog_->input_space_below)));
+       string const length =
+               getLengthFromWidgets(dialog_->input_space_below,
+                                    dialog_->choice_value_space_below);
+       space_bottom = VSpace(LyXGlueLength(length));
        break;
     }
     if (fl_get_button (dialog_->check_space_below))
@@ -254,18 +294,14 @@ void FormParagraph::apply()
     }
 
     Spacing const spacing(linespacing, other_linespacing);
-    LyXText * text = 0;
-    if (lv_->view()->theLockingInset())
-       text = lv_->view()->theLockingInset()->getLyXText(lv_->view());
-    if (!text)
-       text = lv_->view()->text;
+    LyXText * text(lv_->view()->getLyXText());
     text->setParagraph(lv_->view(), line_top, line_bottom, pagebreak_top,
                       pagebreak_bottom, space_top, space_bottom, spacing,
                        align, labelwidthstring, noindent);
 
 
     // Actually apply these settings
-    lv_->view()->update(lv_->view()->text, 
+    lv_->view()->update(text, 
                        BufferView::SELECT | BufferView::FITCUR | BufferView::CHANGE);
     lv_->buffer()->markDirty();
     setMinibuffer(lv_, _("Paragraph layout set"));
@@ -287,15 +323,19 @@ void FormParagraph::update()
 
     fl_set_input(dialog_->input_labelwidth,
                 par_->getLabelWidthString().c_str());
+    setEnabled(dialog_->input_labelwidth,
+              (par_->getLabelWidthString() != _("Senseless with this layout!")));
+
     fl_set_button(dialog_->radio_align_right, 0);
     fl_set_button(dialog_->radio_align_left, 0);
     fl_set_button(dialog_->radio_align_center, 0);
     fl_set_button(dialog_->radio_align_block, 0);
 
+    LyXTextClass const & tclass = textclasslist[buf->params.textclass];
+    
     int align = par_->getAlign();
     if (align == LYX_ALIGN_LAYOUT)
-       align = textclasslist.Style(buf->params.textclass,
-                                   par_->getLayout()).align;
+           align = tclass[par_->layout()].align;
 
     switch (align) {
     case LYX_ALIGN_RIGHT:
@@ -312,15 +352,17 @@ void FormParagraph::update()
        break;
     }
 
-    LyXAlignment alignpos =
-           textclasslist.Style(buf->params.textclass,
-                               par_->getLayout()).alignpossible;
+    LyXAlignment alignpos = tclass[par_->layout()].alignpossible;
 
     setEnabled(dialog_->radio_align_block,  bool(alignpos & LYX_ALIGN_BLOCK));
     setEnabled(dialog_->radio_align_center, bool(alignpos & LYX_ALIGN_CENTER));
     setEnabled(dialog_->radio_align_left,   bool(alignpos & LYX_ALIGN_LEFT));
     setEnabled(dialog_->radio_align_right,  bool(alignpos & LYX_ALIGN_RIGHT));
     
+    // no inset-text-owned paragraph may have pagebreaks
+    setEnabled(dialog_->check_pagebreaks_top, !par_->inInset());
+    setEnabled(dialog_->check_pagebreaks_bottom, !par_->inInset());
+
     fl_set_button(dialog_->check_lines_top,
                  par_->params().lineTop());
     fl_set_button(dialog_->check_lines_bottom,
@@ -356,6 +398,7 @@ void FormParagraph::update()
     fl_set_input (dialog_->input_space_above, "");
 
     setEnabled(dialog_->input_space_above, false);
+    setEnabled(dialog_->choice_value_space_above, false);
     switch (par_->params().spaceTop().kind()) {
     case VSpace::NONE:
        fl_set_choice (dialog_->choice_space_above, 1);
@@ -376,18 +419,26 @@ void FormParagraph::update()
        fl_set_choice (dialog_->choice_space_above, 6);
        break;
     case VSpace::LENGTH:
-       setEnabled(dialog_->input_space_above, true);
-       fl_set_choice (dialog_->choice_space_above, 7);
-       fl_set_input(dialog_->input_space_above, par_->
-                    params().spaceTop().length().asString().c_str());
+    {
+        fl_set_choice (dialog_->choice_space_above, 7);
+        setEnabled(dialog_->input_space_above, true);
+        setEnabled(dialog_->choice_value_space_above, true);
+        bool const metric = lyxrc.default_papersize > 3;
+        string const default_unit = metric ? "cm" : "in";
+        string const length = par_->params().spaceTop().length().asString();
+       updateWidgetsFromLengthString(dialog_->input_space_above,
+                                             dialog_->choice_value_space_above,
+                                             length, default_unit);
        break;
     }
+    }
     
     fl_set_button (dialog_->check_space_above,
-                  par_->params().spaceTop().keep());
+           par_->params().spaceTop().keep());
     fl_set_input (dialog_->input_space_below, "");
 
     setEnabled(dialog_->input_space_below, false);
+    setEnabled(dialog_->choice_value_space_below, false);
     switch (par_->params().spaceBottom().kind()) {
     case VSpace::NONE:
        fl_set_choice (dialog_->choice_space_below, 1);
@@ -408,14 +459,23 @@ void FormParagraph::update()
        fl_set_choice (dialog_->choice_space_below, 6);
        break;
     case VSpace::LENGTH:
+    {
+        fl_set_choice (dialog_->choice_space_below, 7);
         setEnabled(dialog_->input_space_below, true);
-       fl_set_choice (dialog_->choice_space_below, 7);
-        fl_set_input(dialog_->input_space_below, par_->
-                    params().spaceBottom().length().asString().c_str());
+        setEnabled(dialog_->choice_value_space_below, true);
+        bool const metric = lyxrc.default_papersize > 3;
+        string const default_unit = metric ? "cm" : "in";
+        string const length =
+               par_->params().spaceBottom().length().asString();
+       updateWidgetsFromLengthString(dialog_->input_space_below,
+                                             dialog_->choice_value_space_below,
+                                             length, default_unit);
        break;
     }
+    }
+
     fl_set_button(dialog_->check_space_below,
-                  par_->params().spaceBottom().keep());
+                 par_->params().spaceBottom().keep());
     fl_set_button(dialog_->check_noindent,
                  par_->params().noindent());
 }
@@ -437,56 +497,65 @@ bool FormParagraph::input(FL_OBJECT * ob, long)
         if (fl_get_choice (dialog_->choice_space_above) != 7) {
             fl_set_input (dialog_->input_space_above, "");
             setEnabled (dialog_->input_space_above, false);
+            setEnabled (dialog_->choice_value_space_above, false);
         } else {
             setEnabled (dialog_->input_space_above, !lv_->buffer()->isReadonly());
+           setEnabled (dialog_->choice_value_space_above, !lv_->buffer()->isReadonly());
+           bool const metric = lyxrc.default_papersize > 3;
+           int const default_unit = metric ? 8 : 9;
+           if (strip(fl_get_input(dialog_->input_space_above)).empty())
+                fl_set_choice(dialog_->choice_value_space_above,
+                             default_unit);
         }
     }
     if (ob == dialog_->choice_space_below) {
         if (fl_get_choice (dialog_->choice_space_below) != 7) {
             fl_set_input (dialog_->input_space_below, "");
             setEnabled (dialog_->input_space_below, false);
+            setEnabled (dialog_->choice_value_space_below, false);
         } else {
             setEnabled (dialog_->input_space_below, !lv_->buffer()->isReadonly());
+           setEnabled (dialog_->choice_value_space_below, !lv_->buffer()->isReadonly());
+           bool const metric = lyxrc.default_papersize > 3;
+           int const default_unit = metric ? 8 : 9;
+           if (strip(fl_get_input(dialog_->input_space_below)).empty())
+                   fl_set_choice(dialog_->choice_value_space_below,
+                                 default_unit);
         }
     }
  
-    if (fl_get_choice (dialog_->choice_linespacing) == 5)
-        setEnabled (dialog_->input_linespacing, true);
-    else {
-        setEnabled (dialog_->input_linespacing, false);
-        fl_set_input (dialog_->input_linespacing, "");
-    }
-
+    //
+    // warnings if input is senseless
+    //
     string input = fl_get_input (dialog_->input_space_above);
-       
-    if (fl_get_choice(dialog_->choice_space_above)==7 &&
-        input.empty() || !isValidGlueLength(input))
-        valid = false;
+    bool invalid = false;
 
-    if (ob == dialog_->input_space_above) {
-        if (!isValidGlueLength(input)) {
-            fl_set_object_label(dialog_->text_warning,
-                _("Warning: Invalid Length (valid example: 10mm)"));
-            fl_show_object(dialog_->text_warning);
-            valid = false;
-        } else
-            fl_hide_object(dialog_->text_warning);
-    }
+    if (fl_get_choice(dialog_->choice_space_above)==7)
+        invalid = !input.empty() && !isValidGlueLength(input) && !isStrDbl(input);
 
     input = fl_get_input (dialog_->input_space_below);
 
-    if (fl_get_choice(dialog_->choice_space_below)==7 &&
-        input.empty() || !isValidGlueLength(input))
-        valid = false;
+    if (fl_get_choice(dialog_->choice_space_below)==7)
+        invalid = invalid
+               || (!input.empty() && !isValidGlueLength(input) && !isStrDbl(input));
 
-    if (ob == dialog_->input_space_below) {
-        if (!isValidGlueLength(input)) {
+    if (ob == dialog_->input_space_above || ob == dialog_->input_space_below) {
+       if (invalid) {
             fl_set_object_label(dialog_->text_warning,
                 _("Warning: Invalid Length (valid example: 10mm)"));
             fl_show_object(dialog_->text_warning);
-            valid = false;
-        } else
+            return false;
+        } else {
             fl_hide_object(dialog_->text_warning);
+            return true;
+        }
+    }
+
+    if (fl_get_choice (dialog_->choice_linespacing) == 5)
+        setEnabled (dialog_->input_linespacing, true);
+    else {
+        setEnabled (dialog_->input_linespacing, false);
+        fl_set_input (dialog_->input_linespacing, "");
     }
 
     double spacing(strToDbl(fl_get_input(dialog_->input_linespacing)));