2002-09-10 10:18:58 +00:00
|
|
|
/**
|
|
|
|
* \file insetwrap.C
|
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
*
|
|
|
|
* \author Dekel Tsur
|
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "insetwrap.h"
|
2003-03-05 23:19:45 +00:00
|
|
|
#include "insettext.h"
|
|
|
|
|
|
|
|
#include "buffer.h"
|
|
|
|
#include "BufferView.h"
|
|
|
|
#include "debug.h"
|
|
|
|
#include "funcrequest.h"
|
|
|
|
#include "FloatList.h"
|
2002-09-10 10:18:58 +00:00
|
|
|
#include "gettext.h"
|
2003-03-05 23:19:45 +00:00
|
|
|
#include "LaTeXFeatures.h"
|
2002-09-10 10:18:58 +00:00
|
|
|
#include "lyxfont.h"
|
2003-03-05 23:19:45 +00:00
|
|
|
#include "lyxlex.h"
|
2002-09-10 10:18:58 +00:00
|
|
|
#include "lyxtext.h"
|
2003-03-05 23:19:45 +00:00
|
|
|
|
2002-09-10 10:18:58 +00:00
|
|
|
#include "frontends/LyXView.h"
|
|
|
|
#include "frontends/Dialogs.h"
|
2003-03-05 23:19:45 +00:00
|
|
|
|
|
|
|
#include "support/LOstream.h"
|
|
|
|
#include "support/lstrings.h"
|
2002-09-10 10:18:58 +00:00
|
|
|
|
|
|
|
using std::ostream;
|
|
|
|
using std::endl;
|
|
|
|
|
2002-09-24 18:36:20 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// this should not be hardcoded, but be part of the definition
|
|
|
|
// of the float (JMarc)
|
|
|
|
string const caplayout("Caption");
|
|
|
|
string floatname(string const & type, BufferParams const & bp)
|
|
|
|
{
|
|
|
|
FloatList const & floats = bp.getLyXTextClass().floats();
|
|
|
|
FloatList::const_iterator it = floats[type];
|
|
|
|
if (it == floats.end())
|
|
|
|
return type;
|
|
|
|
|
|
|
|
return _(it->second.name());
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace anon
|
|
|
|
|
2002-09-10 10:18:58 +00:00
|
|
|
|
|
|
|
InsetWrap::InsetWrap(BufferParams const & bp, string const & type)
|
2003-03-05 23:19:45 +00:00
|
|
|
: InsetCollapsable(bp)
|
2002-09-10 10:18:58 +00:00
|
|
|
{
|
2002-09-24 18:36:20 +00:00
|
|
|
string lab(_("wrap: "));
|
|
|
|
lab += floatname(type, bp);
|
2002-09-10 10:18:58 +00:00
|
|
|
setLabel(lab);
|
|
|
|
LyXFont font(LyXFont::ALL_SANE);
|
|
|
|
font.decSize();
|
|
|
|
font.decSize();
|
|
|
|
font.setColor(LColor::collapsable);
|
|
|
|
setLabelFont(font);
|
2003-03-05 23:19:45 +00:00
|
|
|
params_.type = type;
|
|
|
|
params_.width = LyXLength(50, LyXLength::PCW);
|
2002-09-10 10:18:58 +00:00
|
|
|
setInsetName(type);
|
2002-09-24 18:36:20 +00:00
|
|
|
LyXTextClass const & tclass = bp.getLyXTextClass();
|
|
|
|
if (tclass.hasLayout(caplayout))
|
|
|
|
inset.paragraph()->layout(tclass[caplayout]);
|
2002-09-10 10:18:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
InsetWrap::InsetWrap(InsetWrap const & in, bool same_id)
|
2003-03-05 23:19:45 +00:00
|
|
|
: InsetCollapsable(in, same_id), params_(in.params_)
|
2002-09-10 10:18:58 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
InsetWrap::~InsetWrap()
|
|
|
|
{
|
2003-03-05 23:19:45 +00:00
|
|
|
InsetWrapMailer mailer(*this);
|
|
|
|
mailer.hideDialog();
|
2002-09-10 10:18:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-03-05 23:19:45 +00:00
|
|
|
dispatch_result InsetWrap::localDispatch(FuncRequest const & cmd)
|
2002-09-10 10:18:58 +00:00
|
|
|
{
|
2003-03-05 23:19:45 +00:00
|
|
|
Inset::RESULT result = UNDISPATCHED;
|
2002-09-10 10:18:58 +00:00
|
|
|
|
2003-03-05 23:19:45 +00:00
|
|
|
switch (cmd.action) {
|
|
|
|
case LFUN_INSET_MODIFY: {
|
|
|
|
InsetWrapParams params;
|
|
|
|
InsetWrapMailer::string2params(cmd.argument, params);
|
|
|
|
|
|
|
|
params_.placement = params.placement;
|
|
|
|
params_.width = params.width;
|
|
|
|
|
|
|
|
cmd.view()->updateInset(this, true);
|
|
|
|
result = DISPATCHED;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
result = InsetCollapsable::localDispatch(cmd);
|
2002-09-10 10:18:58 +00:00
|
|
|
}
|
|
|
|
|
2003-03-05 23:19:45 +00:00
|
|
|
return result;
|
2002-09-10 10:18:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-03-05 23:19:45 +00:00
|
|
|
void InsetWrapParams::write(ostream & os) const
|
|
|
|
{
|
|
|
|
os << "Wrap " // getInsetName()
|
|
|
|
<< type << '\n';
|
|
|
|
|
|
|
|
if (!placement.empty()) {
|
|
|
|
os << "placement " << placement << "\n";
|
|
|
|
}
|
|
|
|
os << "width \"" << width.asString() << "\"\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void InsetWrapParams::read(LyXLex & lex)
|
2002-09-10 10:18:58 +00:00
|
|
|
{
|
|
|
|
if (lex.isOK()) {
|
|
|
|
lex.next();
|
|
|
|
string token = lex.getString();
|
|
|
|
if (token == "placement") {
|
|
|
|
lex.next();
|
2003-03-05 23:19:45 +00:00
|
|
|
placement = lex.getString();
|
2002-09-10 10:18:58 +00:00
|
|
|
} else {
|
|
|
|
// take countermeasures
|
|
|
|
lex.pushToken(token);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (lex.isOK()) {
|
|
|
|
lex.next();
|
|
|
|
string token = lex.getString();
|
|
|
|
if (token == "width") {
|
|
|
|
lex.next();
|
2003-03-05 23:19:45 +00:00
|
|
|
width = LyXLength(lex.getString());
|
2002-09-10 10:18:58 +00:00
|
|
|
} else {
|
|
|
|
lyxerr << "InsetWrap::Read:: Missing 'width'-tag!"
|
|
|
|
<< endl;
|
|
|
|
// take countermeasures
|
|
|
|
lex.pushToken(token);
|
|
|
|
}
|
|
|
|
}
|
2003-03-05 23:19:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void InsetWrap::write(Buffer const * buf, ostream & os) const
|
|
|
|
{
|
|
|
|
params_.write(os);
|
|
|
|
InsetCollapsable::write(buf, os);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void InsetWrap::read(Buffer const * buf, LyXLex & lex)
|
|
|
|
{
|
|
|
|
params_.read(lex);
|
2002-09-10 10:18:58 +00:00
|
|
|
InsetCollapsable::read(buf, lex);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void InsetWrap::validate(LaTeXFeatures & features) const
|
|
|
|
{
|
|
|
|
features.require("floatflt");
|
|
|
|
InsetCollapsable::validate(features);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Inset * InsetWrap::clone(Buffer const &, bool same_id) const
|
|
|
|
{
|
|
|
|
return new InsetWrap(*const_cast<InsetWrap *>(this), same_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string const InsetWrap::editMessage() const
|
|
|
|
{
|
|
|
|
return _("Opened Wrap Inset");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int InsetWrap::latex(Buffer const * buf,
|
|
|
|
ostream & os, bool fragile, bool fp) const
|
|
|
|
{
|
2003-03-05 23:19:45 +00:00
|
|
|
os << "\\begin{floating" << params_.type << '}';
|
|
|
|
if (!params_.placement.empty()) {
|
|
|
|
os << '[' << params_.placement << ']';
|
2002-09-10 10:18:58 +00:00
|
|
|
}
|
2003-03-05 23:19:45 +00:00
|
|
|
os << '{' << params_.width.asLatexString() << "}%\n";
|
2002-09-10 10:18:58 +00:00
|
|
|
|
|
|
|
int const i = inset.latex(buf, os, fragile, fp);
|
|
|
|
|
2003-03-05 23:19:45 +00:00
|
|
|
os << "\\end{floating" << params_.type << "}%\n";
|
2002-09-10 10:18:58 +00:00
|
|
|
return i + 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int InsetWrap::docbook(Buffer const * buf, ostream & os, bool mixcont) const
|
|
|
|
{
|
2003-03-05 23:19:45 +00:00
|
|
|
os << '<' << params_.type << '>';
|
2002-09-10 10:18:58 +00:00
|
|
|
int const i = inset.docbook(buf, os, mixcont);
|
2003-03-05 23:19:45 +00:00
|
|
|
os << "</" << params_.type << '>';
|
2002-09-10 10:18:58 +00:00
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool InsetWrap::insetAllowed(Inset::Code code) const
|
|
|
|
{
|
|
|
|
switch(code) {
|
|
|
|
case FLOAT_CODE:
|
|
|
|
case FOOT_CODE:
|
|
|
|
case MARGIN_CODE:
|
2002-11-27 10:30:28 +00:00
|
|
|
return false;
|
2002-09-10 10:18:58 +00:00
|
|
|
default:
|
|
|
|
return InsetCollapsable::insetAllowed(code);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-27 10:30:28 +00:00
|
|
|
|
2002-09-10 10:18:58 +00:00
|
|
|
int InsetWrap::getMaxWidth(BufferView * bv, UpdatableInset const * inset)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
if (owner() &&
|
|
|
|
static_cast<UpdatableInset*>(owner())->getMaxWidth(bv, inset) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2003-03-05 23:19:45 +00:00
|
|
|
if (!params_.width.zero()) {
|
2002-11-27 10:30:28 +00:00
|
|
|
int const ww1 = latexTextWidth(bv);
|
|
|
|
int const ww2 = InsetCollapsable::getMaxWidth(bv, inset);
|
2002-09-10 10:18:58 +00:00
|
|
|
if (ww2 > 0 && ww2 < ww1) {
|
|
|
|
return ww2;
|
|
|
|
}
|
|
|
|
return ww1;
|
|
|
|
}
|
|
|
|
// this should not happen!
|
|
|
|
return InsetCollapsable::getMaxWidth(bv, inset);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int InsetWrap::latexTextWidth(BufferView * bv) const
|
|
|
|
{
|
2003-03-05 23:19:45 +00:00
|
|
|
return params_.width.inPixels(InsetCollapsable::latexTextWidth(bv));
|
2002-09-10 10:18:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string const & InsetWrap::type() const
|
|
|
|
{
|
2003-03-05 23:19:45 +00:00
|
|
|
return params_.type;
|
2002-09-10 10:18:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LyXLength const & InsetWrap::pageWidth() const
|
|
|
|
{
|
2003-03-05 23:19:45 +00:00
|
|
|
return params_.width;
|
2002-09-10 10:18:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void InsetWrap::pageWidth(LyXLength const & ll)
|
|
|
|
{
|
2003-03-05 23:19:45 +00:00
|
|
|
if (ll != params_.width) {
|
|
|
|
params_.width = ll;
|
2002-09-10 10:18:58 +00:00
|
|
|
need_update = FULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void InsetWrap::placement(string const & p)
|
|
|
|
{
|
2003-03-05 23:19:45 +00:00
|
|
|
params_.placement = p;
|
2002-09-10 10:18:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string const & InsetWrap::placement() const
|
|
|
|
{
|
2003-03-05 23:19:45 +00:00
|
|
|
return params_.placement;
|
2002-09-10 10:18:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool InsetWrap::showInsetDialog(BufferView * bv) const
|
|
|
|
{
|
|
|
|
if (!inset.showInsetDialog(bv)) {
|
2003-03-05 23:19:45 +00:00
|
|
|
InsetWrap * tmp = const_cast<InsetWrap *>(this);
|
|
|
|
InsetWrapMailer mailer(*tmp);
|
|
|
|
mailer.showDialog();
|
2002-09-10 10:18:58 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2002-09-24 18:36:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
void InsetWrap::addToToc(toc::TocList & toclist, Buffer const * buf) const
|
|
|
|
{
|
|
|
|
// Now find the caption in the float...
|
|
|
|
// We now tranverse the paragraphs of
|
|
|
|
// the inset...
|
|
|
|
Paragraph * tmp = inset.paragraph();
|
|
|
|
while (tmp) {
|
|
|
|
if (tmp->layout()->name() == caplayout) {
|
2002-12-08 22:33:55 +00:00
|
|
|
string const name = floatname(type(), buf->params);
|
2002-09-24 18:36:20 +00:00
|
|
|
string const str =
|
2002-12-08 22:33:55 +00:00
|
|
|
tostr(toclist[name].size() + 1)
|
2002-09-24 18:36:20 +00:00
|
|
|
+ ". " + tmp->asString(buf, false);
|
2003-02-22 20:05:13 +00:00
|
|
|
toc::TocItem const item(tmp->id(), 0 , str);
|
2002-12-08 22:33:55 +00:00
|
|
|
toclist[name].push_back(item);
|
2002-09-24 18:36:20 +00:00
|
|
|
}
|
|
|
|
tmp = tmp->next();
|
|
|
|
}
|
|
|
|
}
|
2003-03-05 23:19:45 +00:00
|
|
|
|
|
|
|
|
2003-03-07 14:08:10 +00:00
|
|
|
string const InsetWrapMailer::name_("wrap");
|
|
|
|
|
2003-03-05 23:19:45 +00:00
|
|
|
InsetWrapMailer::InsetWrapMailer(InsetWrap & inset)
|
2003-03-07 14:08:10 +00:00
|
|
|
: inset_(inset)
|
2003-03-05 23:19:45 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
string const InsetWrapMailer::inset2string() const
|
|
|
|
{
|
2003-03-07 14:08:10 +00:00
|
|
|
return params2string(inset_.params());
|
2003-03-05 23:19:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void InsetWrapMailer::string2params(string const & in,
|
|
|
|
InsetWrapParams & params)
|
|
|
|
{
|
|
|
|
params = InsetWrapParams();
|
|
|
|
|
2003-03-07 14:08:10 +00:00
|
|
|
istringstream data(in);
|
|
|
|
LyXLex lex(0,0);
|
|
|
|
lex.setStream(data);
|
2003-03-05 23:19:45 +00:00
|
|
|
|
2003-03-07 14:08:10 +00:00
|
|
|
if (lex.isOK()) {
|
|
|
|
lex.next();
|
|
|
|
string const token = lex.getString();
|
|
|
|
if (token != name_)
|
|
|
|
return;
|
|
|
|
}
|
2003-03-05 23:19:45 +00:00
|
|
|
|
|
|
|
// This is part of the inset proper that is usually swallowed
|
|
|
|
// by Buffer::readInset
|
2003-03-07 14:08:10 +00:00
|
|
|
if (lex.isOK()) {
|
|
|
|
lex.next();
|
|
|
|
string const token = lex.getString();
|
|
|
|
if (token != "Wrap" || !lex.eatLine())
|
|
|
|
return;
|
|
|
|
}
|
2003-03-05 23:19:45 +00:00
|
|
|
|
|
|
|
params.read(lex);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string const
|
2003-03-07 14:08:10 +00:00
|
|
|
InsetWrapMailer::params2string(InsetWrapParams const & params)
|
2003-03-05 23:19:45 +00:00
|
|
|
{
|
|
|
|
ostringstream data;
|
2003-03-07 14:08:10 +00:00
|
|
|
data << name_ << ' ';
|
2003-03-05 23:19:45 +00:00
|
|
|
params.write(data);
|
|
|
|
|
|
|
|
return data.str();
|
|
|
|
}
|