lyx_mirror/src/frontends/xforms/FormParagraph.C
Dekel Tsur 32ef0d04c4 Fix paragraph spacing
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@2562 a592a061-630c-0410-9148-cb99ea01b6c8
2001-08-20 19:08:46 +00:00

515 lines
15 KiB
C

/* This file is part of
* ======================================================
*
* LyX, The Document Processor
*
* Copyright 2000-2001 The LyX Team.
*
* @author Jürgen Vigna
*
*======================================================*/
#include <config.h>
#ifdef __GNUG_
#pragma implementation
#endif
#include FORMS_H_LOCATION
#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 "BufferView.h"
#include "Spacing.h"
#include "ParagraphParameters.h"
#include "input_validators.h"
using Liason::setMinibuffer;
using SigC::slot;
FormParagraph::FormParagraph(LyXView * lv, Dialogs * d)
: FormBaseBD(lv, d, _("Paragraph Layout")), par_(0)
{
// 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->showParagraph.connect(slot(this, &FormParagraph::show));
}
void FormParagraph::connect()
{
cp_ = d_->updateParagraph
.connect(slot(this, &FormParagraph::changedParagraph));
FormBaseBD::connect();
}
void FormParagraph::disconnect()
{
cp_.disconnect();
FormBaseBD::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();
}
void FormParagraph::changedParagraph()
{
/// Record the paragraph
Paragraph const * const p = getCurrentParagraph();
if (p == 0 || p == par_)
return;
// For now don't bother checking if the params are different,
// just activate the Apply button
bc().valid();
}
void FormParagraph::redraw()
{
if( form() && form()->visible )
fl_redraw_form( form() );
else
return;
FL_FORM * outer_form = fl_get_active_folder(dialog_->tabbed_folder);
if (outer_form && outer_form->visible)
fl_redraw_form( outer_form );
}
FL_FORM * FormParagraph::form() const
{
if (dialog_.get()) return dialog_->form;
return 0;
}
void FormParagraph::build()
{
// the tabbed folder
dialog_.reset(build_tabbed_paragraph());
// Manage the ok, apply, restore and cancel/close buttons
bc_.setOK(dialog_->button_ok);
bc_.setApply(dialog_->button_apply);
bc_.setCancel(dialog_->button_cancel);
bc_.setRestore(dialog_->button_restore);
// the general paragraph data form
general_.reset(build_paragraph_general());
fl_addto_choice(general_->choice_space_above,
_(" None | Defskip | Smallskip "
"| Medskip | Bigskip | VFill | Length "));
fl_addto_choice(general_->choice_space_below,
_(" None | Defskip | Smallskip "
"| Medskip | Bigskip | VFill | Length "));
fl_addto_choice(general_->choice_linespacing,
_(" Default | Single | OneHalf | Double | Other "));
fl_set_input_return(general_->input_space_above, FL_RETURN_CHANGED);
fl_set_input_return(general_->input_space_below, FL_RETURN_CHANGED);
fl_set_input_return(general_->input_labelwidth, FL_RETURN_CHANGED);
bc_.addReadOnly (general_->group_radio_alignment);
// bc_.addReadOnly (general_->radio_align_right);
// bc_.addReadOnly (general_->radio_align_left);
// bc_.addReadOnly (general_->radio_align_block);
// bc_.addReadOnly (general_->radio_align_center);
bc_.addReadOnly (general_->check_lines_top);
bc_.addReadOnly (general_->check_lines_bottom);
bc_.addReadOnly (general_->check_pagebreaks_top);
bc_.addReadOnly (general_->check_pagebreaks_bottom);
bc_.addReadOnly (general_->choice_space_above);
bc_.addReadOnly (general_->input_space_above);
bc_.addReadOnly (general_->check_space_above);
bc_.addReadOnly (general_->choice_space_below);
bc_.addReadOnly (general_->input_space_below);
bc_.addReadOnly (general_->check_space_below);
bc_.addReadOnly (general_->choice_linespacing);
bc_.addReadOnly (general_->input_linespacing);
bc_.addReadOnly (general_->check_noindent);
bc_.addReadOnly (general_->input_labelwidth);
fl_set_input_filter(general_->input_linespacing, fl_unsigned_float_filter);
// now make them fit together
fl_addto_tabfolder(dialog_->tabbed_folder,_("General"), general_->form);
}
void FormParagraph::apply()
{
if (!lv_->view()->available() || !dialog_.get())
return;
general_apply();
lv_->view()->update(lv_->view()->text,
BufferView::SELECT | BufferView::FITCUR | BufferView::CHANGE);
lv_->buffer()->markDirty();
setMinibuffer(lv_, _("Paragraph layout set"));
}
void FormParagraph::update()
{
if (!dialog_.get())
return;
// Do this first; some objects may be de/activated subsequently.
bc_.readOnly(lv_->buffer()->isReadonly());
general_update();
}
void FormParagraph::general_apply()
{
VSpace space_top, space_bottom;
LyXAlignment align;
string labelwidthstring;
bool noindent;
// If a vspace kind is "Length" but there's no text in
// the input field, reset the kind to "None".
if ((fl_get_choice (general_->choice_space_above) == 7) &&
!*(fl_get_input (general_->input_space_above)))
{
fl_set_choice (general_->choice_space_above, 1);
}
if ((fl_get_choice (general_->choice_space_below) == 7) &&
!*(fl_get_input (general_->input_space_below)))
{
fl_set_choice (general_->choice_space_below, 1);
}
bool line_top = fl_get_button(general_->check_lines_top);
bool line_bottom = fl_get_button(general_->check_lines_bottom);
bool pagebreak_top = fl_get_button(general_->check_pagebreaks_top);
bool pagebreak_bottom = fl_get_button(general_->check_pagebreaks_bottom);
switch (fl_get_choice (general_->choice_space_above)) {
case 1:
space_top = VSpace(VSpace::NONE);
break;
case 2:
space_top = VSpace(VSpace::DEFSKIP);
break;
case 3:
space_top = VSpace(VSpace::SMALLSKIP);
break;
case 4:
space_top = VSpace(VSpace::MEDSKIP);
break;
case 5:
space_top = VSpace(VSpace::BIGSKIP);
break;
case 6:
space_top = VSpace(VSpace::VFILL);
break;
case 7:
space_top =
VSpace(LyXGlueLength(fl_get_input(general_->input_space_above)));
break;
}
if (fl_get_button (general_->check_space_above))
space_top.setKeep (true);
switch (fl_get_choice (general_->choice_space_below)) {
case 1:
space_bottom = VSpace(VSpace::NONE);
break;
case 2:
space_bottom = VSpace(VSpace::DEFSKIP);
break;
case 3:
space_bottom = VSpace(VSpace::SMALLSKIP);
break;
case 4:
space_bottom = VSpace(VSpace::MEDSKIP);
break;
case 5:
space_bottom = VSpace(VSpace::BIGSKIP);
break;
case 6:
space_bottom = VSpace(VSpace::VFILL);
break;
case 7:
space_bottom =
VSpace(LyXGlueLength(fl_get_input(general_->input_space_below)));
break;
}
if (fl_get_button (general_->check_space_below))
space_bottom.setKeep (true);
if (fl_get_button(general_->radio_align_left))
align = LYX_ALIGN_LEFT;
else if (fl_get_button(general_->radio_align_right))
align = LYX_ALIGN_RIGHT;
else if (fl_get_button(general_->radio_align_center))
align = LYX_ALIGN_CENTER;
else
align = LYX_ALIGN_BLOCK;
labelwidthstring = fl_get_input(general_->input_labelwidth);
noindent = fl_get_button(general_->check_noindent);
Spacing::Space linespacing;
string other_linespacing;
switch (fl_get_choice(general_->choice_linespacing)) {
case 1: linespacing = Spacing::Default; break;
case 2: linespacing = Spacing::Single; break;
case 3: linespacing = Spacing::Onehalf; break;
case 4: linespacing = Spacing::Double; break;
case 5:
linespacing = Spacing::Other;
other_linespacing = fl_get_input(general_->input_linespacing);
break;
}
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;
text->setParagraph(lv_->view(), line_top, line_bottom, pagebreak_top,
pagebreak_bottom, space_top, space_bottom, spacing,
align, labelwidthstring, noindent);
}
void FormParagraph::general_update()
{
if (!general_.get())
return;
Buffer * buf = lv_->view()->buffer();
/// Record the paragraph
par_ = getCurrentParagraph();
fl_set_input(general_->input_labelwidth,
par_->getLabelWidthString().c_str());
fl_set_button(general_->radio_align_right, 0);
fl_set_button(general_->radio_align_left, 0);
fl_set_button(general_->radio_align_center, 0);
fl_set_button(general_->radio_align_block, 0);
int align = par_->getAlign();
if (align == LYX_ALIGN_LAYOUT)
align = textclasslist.Style(buf->params.textclass,
par_->getLayout()).align;
switch (align) {
case LYX_ALIGN_RIGHT:
fl_set_button(general_->radio_align_right, 1);
break;
case LYX_ALIGN_LEFT:
fl_set_button(general_->radio_align_left, 1);
break;
case LYX_ALIGN_CENTER:
fl_set_button(general_->radio_align_center, 1);
break;
default:
fl_set_button(general_->radio_align_block, 1);
break;
}
LyXAlignment alignpos =
textclasslist.Style(buf->params.textclass,
par_->getLayout()).alignpossible;
setEnabled(general_->radio_align_block, bool(alignpos & LYX_ALIGN_BLOCK));
setEnabled(general_->radio_align_center, bool(alignpos & LYX_ALIGN_CENTER));
setEnabled(general_->radio_align_left, bool(alignpos & LYX_ALIGN_LEFT));
setEnabled(general_->radio_align_right, bool(alignpos & LYX_ALIGN_RIGHT));
fl_set_button(general_->check_lines_top,
par_->params().lineTop());
fl_set_button(general_->check_lines_bottom,
par_->params().lineBottom());
fl_set_button(general_->check_pagebreaks_top,
par_->params().pagebreakTop());
fl_set_button(general_->check_pagebreaks_bottom,
par_->params().pagebreakBottom());
fl_set_button(general_->check_noindent,
par_->params().noindent());
int linespacing;
Spacing space = par_->params().spacing();
/*
switch (lv_->buffer()->params.spacing.getSpace()) {
case Spacing::Default: linespacing = 1; break;
case Spacing::Single: linespacing = 2; break;
case Spacing::Onehalf: linespacing = 3; break;
case Spacing::Double: linespacing = 4; break;
case Spacing::Other: linespacing = 5; break;
}
*/
switch (space.getSpace()) {
case Spacing::Default: linespacing = 1; break;
case Spacing::Single: linespacing = 2; break;
case Spacing::Onehalf: linespacing = 3; break;
case Spacing::Double: linespacing = 4; break;
case Spacing::Other: linespacing = 5; break;
}
fl_set_choice(general_->choice_linespacing, linespacing);
if (space.getSpace() == Spacing::Other) {
string sp;
if (space.getSpace() == Spacing::Default)
sp = tostr(lv_->buffer()->params.spacing.getValue());
else
sp = tostr(space.getValue());
fl_set_input(general_->input_linespacing, sp.c_str());
setEnabled(general_->input_linespacing, true);
} else {
fl_set_input(general_->input_linespacing, "");
setEnabled(general_->input_linespacing, false);
}
fl_set_input (general_->input_space_above, "");
switch (par_->params().spaceTop().kind()) {
case VSpace::NONE:
fl_set_choice (general_->choice_space_above, 1);
break;
case VSpace::DEFSKIP:
fl_set_choice (general_->choice_space_above, 2);
break;
case VSpace::SMALLSKIP:
fl_set_choice (general_->choice_space_above, 3);
break;
case VSpace::MEDSKIP:
fl_set_choice (general_->choice_space_above, 4);
break;
case VSpace::BIGSKIP:
fl_set_choice (general_->choice_space_above, 5);
break;
case VSpace::VFILL:
fl_set_choice (general_->choice_space_above, 6);
break;
case VSpace::LENGTH:
fl_set_choice (general_->choice_space_above, 7);
fl_set_input(general_->input_space_above, par_->
params().spaceTop().length().asString().c_str());
break;
}
fl_set_button (general_->check_space_above,
par_->params().spaceTop().keep());
fl_set_input (general_->input_space_below, "");
switch (par_->params().spaceBottom().kind()) {
case VSpace::NONE:
fl_set_choice (general_->choice_space_below, 1);
break;
case VSpace::DEFSKIP:
fl_set_choice (general_->choice_space_below, 2);
break;
case VSpace::SMALLSKIP:
fl_set_choice (general_->choice_space_below, 3);
break;
case VSpace::MEDSKIP:
fl_set_choice (general_->choice_space_below, 4);
break;
case VSpace::BIGSKIP:
fl_set_choice (general_->choice_space_below, 5);
break;
case VSpace::VFILL:
fl_set_choice (general_->choice_space_below, 6);
break;
case VSpace::LENGTH:
fl_set_choice (general_->choice_space_below, 7);
fl_set_input(general_->input_space_below, par_->
params().spaceBottom().length().asString().c_str());
break;
}
fl_set_button(general_->check_space_below,
par_->params().spaceBottom().keep());
fl_set_button(general_->check_noindent,
par_->params().noindent());
}
bool FormParagraph::input(FL_OBJECT * ob, long)
{
bool ret = true;
fl_hide_object(dialog_->text_warning);
// First check the buttons which are exclusive and you have to
// check only the actuall de/activated button.
//
// general form first
//
// "Synchronize" the choices and input fields, making it
// impossible to commit senseless data.
if (fl_get_choice (general_->choice_space_above) != 7)
fl_set_input (general_->input_space_above, "");
if (fl_get_choice (general_->choice_space_below) != 7)
fl_set_input (general_->input_space_below, "");
if (fl_get_choice (general_->choice_linespacing) == 4)
setEnabled (general_->input_linespacing, true);
else {
setEnabled (general_->input_linespacing, false);
fl_set_input (general_->input_linespacing, "");
}
//
// first the general form
//
string input = fl_get_input (general_->input_space_above);
bool invalid = false;
if (fl_get_choice(general_->choice_space_above)==7)
invalid = !input.empty() && !isValidGlueLength(input);
input = fl_get_input (general_->input_space_below);
if (fl_get_choice(general_->choice_space_below)==7)
invalid = invalid || (!input.empty() && !isValidGlueLength(input));
if (ob == general_->input_space_above || ob == general_->input_space_below) {
if (invalid) {
fl_set_object_label(dialog_->text_warning,
_("Warning: Invalid Length (valid example: 10mm)"));
fl_show_object(dialog_->text_warning);
return false;
} else {
fl_hide_object(dialog_->text_warning);
return true;
}
}
return ret;
}