-/* This file is part of
- * ======================================================
- *
- * LyX, The Document Processor
- *
- * Copyright 1998 The LyX Team.
+/**
+ * \file insetminipage.C
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
*
- *======================================================*/
+ * \author Jürgen Vigna
+ * \author Lars Gullik Bjønnes
+ *
+ * Full author contact details are available in file CREDITS
+ */
#include <config.h>
-#ifdef __GNUG__
-#pragma implementation
-#endif
-
#include "insetminipage.h"
+#include "insettext.h"
+
+#include "BufferView.h"
+#include "debug.h"
+#include "dimension.h"
+#include "funcrequest.h"
#include "gettext.h"
+#include "Lsstream.h"
#include "lyxfont.h"
-#include "BufferView.h"
-#include "LyXView.h"
-#include "frontends/Dialogs.h"
+#include "lyxlex.h"
#include "lyxtext.h"
-#include "insets/insettext.h"
+#include "Lsstream.h"
+#include "metricsinfo.h"
+
+#include "frontends/LyXView.h"
+#include "frontends/Dialogs.h"
+
#include "support/LOstream.h"
-#include "support/lstrings.h"
-#include "debug.h"
using std::ostream;
using std::endl;
+using std::auto_ptr;
// Some information about Minipages in LaTeX:
// have to output "" for minipages.
// (Lgb)
-InsetMinipage::InsetMinipage()
- : InsetCollapsable(), pos_(center),
- inner_pos_(inner_center)
+InsetMinipage::InsetMinipage(BufferParams const & bp)
+ : InsetCollapsable(bp)
{
setLabel(_("minipage"));
LyXFont font(LyXFont::ALL_SANE);
font.decSize();
font.decSize();
- font.setColor(LColor::footnote);
+ font.setColor(LColor::collapsable);
setLabelFont(font);
+#if 0
setAutoCollapse(false);
+#endif
+
+#if 0
+#ifdef WITH_WARNINGS
+#warning Remove this color definitions before 1.2.0 final!
+#endif
+ // just for experimentation :)
+ setBackgroundColor(LColor::green);
+#endif
+
+ inset.setFrameColor(0, LColor::blue);
setInsetName("Minipage");
- collapsed = false;
}
-InsetMinipage::~InsetMinipage()
-{
- hideDialog();
-}
+InsetMinipage::InsetMinipage(InsetMinipage const & in)
+ : InsetCollapsable(in), params_(in.params_)
+{}
-void InsetMinipage::Write(Buffer const * buf, ostream & os) const
+auto_ptr<InsetBase> InsetMinipage::clone() const
{
- os << getInsetName() << "\n";
- InsetCollapsable::Write(buf, os);
+ return auto_ptr<InsetBase>(new InsetMinipage(*this));
}
-Inset * InsetMinipage::Clone(Buffer const &) const
+InsetMinipage::~InsetMinipage()
{
- InsetMinipage * result = new InsetMinipage;
- result->inset->init(inset);
-
- result->collapsed = collapsed;
- return result;
+ InsetMinipageMailer mailer(*this);
+ mailer.hideDialog();
}
-string const InsetMinipage::EditMessage() const
+dispatch_result InsetMinipage::localDispatch(FuncRequest const & cmd)
{
- return _("Opened Minipage Inset");
+ switch (cmd.action) {
+ case LFUN_INSET_MODIFY: {
+ InsetMinipage::Params params;
+ InsetMinipageMailer::string2params(cmd.argument, params);
+
+ params_.pos = params.pos;
+ params_.width = params.width;
+
+ /* FIXME: I refuse to believe we have to live
+ * with ugliness like this ... */
+ inset.getLyXText(cmd.view())->fullRebreak();
+ cmd.view()->updateInset(this);
+ return DISPATCHED;
+ }
+
+ case LFUN_INSET_DIALOG_UPDATE:
+ InsetMinipageMailer(*this).updateDialog(cmd.view());
+ return DISPATCHED;
+
+ default:
+ return InsetCollapsable::localDispatch(cmd);
+ }
}
-int InsetMinipage::Latex(Buffer const * buf,
- ostream & os, bool fragile, bool fp) const
+void InsetMinipage::Params::write(ostream & os) const
{
- os << "\\begin{minipage}{\\columnwidth}%\n";
-
- int i = inset->Latex(buf, os, fragile, fp);
- os << "\\end{minipage}%\n";
-
- return i + 2;
+ os << "Minipage" << '\n'
+ << "position " << pos << '\n'
+ << "inner_position " << inner_pos << '\n'
+ << "height \"" << height.asString() << "\"\n"
+ << "width \"" << width.asString() << "\"\n";
}
-bool InsetMinipage::InsertInsetAllowed(Inset * in) const
+void InsetMinipage::Params::read(LyXLex & lex)
{
- if ((in->LyxCode() == Inset::FLOAT_CODE) ||
- (in->LyxCode() == Inset::MARGIN_CODE)) {
- return false;
+ if (lex.isOK()) {
+ lex.next();
+ string const token = lex.getString();
+ if (token == "position") {
+ lex.next();
+ pos = static_cast<Position>(lex.getInteger());
+ } else {
+ lyxerr << "InsetMinipage::Read: Missing 'position'-tag!"
+ << endl;
+ // take countermeasures
+ lex.pushToken(token);
+ }
+ }
+ if (lex.isOK()) {
+ lex.next();
+ string const token = lex.getString();
+ if (token == "inner_position") {
+ lex.next();
+ inner_pos = static_cast<InnerPosition>(lex.getInteger());
+ } else {
+ lyxerr << "InsetMinipage::Read: Missing 'inner_position'-tag!"
+ << endl;
+ // take countermeasures
+ lex.pushToken(token);
+ }
+ }
+ if (lex.isOK()) {
+ lex.next();
+ string const token = lex.getString();
+ if (token == "height") {
+ lex.next();
+ height = LyXLength(lex.getString());
+ } else {
+ lyxerr << "InsetMinipage::Read: Missing 'height'-tag!"
+ << endl;
+ // take countermeasures
+ lex.pushToken(token);
+ }
+ }
+ if (lex.isOK()) {
+ lex.next();
+ string const token = lex.getString();
+ if (token == "width") {
+ lex.next();
+ width = LyXLength(lex.getString());
+ } else {
+ lyxerr << "InsetMinipage::Read: Missing 'width'-tag!"
+ << endl;
+ // take countermeasures
+ lex.pushToken(token);
+ }
}
- return true;
}
-InsetMinipage::Position InsetMinipage::pos() const
+void InsetMinipage::write(Buffer const * buf, ostream & os) const
{
- return pos_;
+ params_.write(os);
+ InsetCollapsable::write(buf, os);
}
-void InsetMinipage::pos(InsetMinipage::Position p)
+void InsetMinipage::read(Buffer const * buf, LyXLex & lex)
{
- pos_ = p;
+ params_.read(lex);
+ InsetCollapsable::read(buf, lex);
}
-InsetMinipage::InnerPosition InsetMinipage::innerPos() const
+void InsetMinipage::metrics(MetricsInfo & mi, Dimension & dim) const
{
- return inner_pos_;
+ if (collapsed_)
+ dimension_collapsed(dim);
+ else {
+ Dimension d;
+ MetricsInfo m = mi;
+ m.base.textwidth = params_.width.inPixels(mi.base.textwidth);
+ InsetCollapsable::metrics(m, d);
+ switch (params_.pos) {
+ case top:
+ dim.asc = d.asc;
+ dim.des = d.des;
+ break;
+ case center:
+ dim.asc = d.ascent() + d.descent() / 2;
+ dim.des = dim.asc;
+ break;
+ case bottom:
+ dim.asc = d.des;
+ dim.des = d.asc;
+ break;
+ }
+ dim.wid = d.wid;
+ }
+ dim_ = dim;
}
-void InsetMinipage::innerPos(InsetMinipage::InnerPosition ip)
+string const InsetMinipage::editMessage() const
{
- inner_pos_ = ip;
+ return _("Opened Minipage Inset");
}
-LyXLength const & InsetMinipage::height() const
+int InsetMinipage::latex(Buffer const * buf, ostream & os,
+ LatexRunParams const & runparams) const
{
- return height_;
-}
+ string s_pos;
+ switch (params_.pos) {
+ case top:
+ s_pos += 't';
+ break;
+ case center:
+ s_pos += 'c';
+ break;
+ case bottom:
+ s_pos += 'b';
+ break;
+ }
+ os << "\\begin{minipage}[" << s_pos << "]{"
+ << params_.width.asLatexString() << "}%\n";
+ int i = inset.latex(buf, os, runparams);
-void InsetMinipage::height(LyXLength const & ll)
-{
- height_ = ll;
+ os << "\\end{minipage}%\n";
+ return i + 2;
}
-string const & InsetMinipage::width() const
+bool InsetMinipage::insetAllowed(InsetOld::Code code) const
{
- return width_;
+ if (code == InsetOld::FLOAT_CODE || code == InsetOld::MARGIN_CODE)
+ return false;
+
+ return InsetCollapsable::insetAllowed(code);
}
-void InsetMinipage::width(string const & ll)
+bool InsetMinipage::showInsetDialog(BufferView * bv) const
{
- width_ = ll;
-}
+ if (!inset.showInsetDialog(bv)) {
+ InsetMinipage * tmp = const_cast<InsetMinipage *>(this);
+ InsetMinipageMailer mailer(*tmp);
+ mailer.showDialog(bv);
+ }
-int InsetMinipage::widthp() const
-{
- return widthp_;
+ return true;
}
-void InsetMinipage::widthp(int ll)
+int InsetMinipage::latexTextWidth(BufferView * bv) const
{
- widthp_ = ll;
+ return params_.width.inPixels(InsetCollapsable::latexTextWidth(bv));
}
-void InsetMinipage::widthp(string const & ll)
+InsetMinipage::Params::Params()
+ : pos(center),
+ inner_pos(inner_center),
+ width(100, LyXLength::PCW)
+{}
+
+
+string const InsetMinipageMailer:: name_("minipage");
+
+InsetMinipageMailer::InsetMinipageMailer(InsetMinipage & inset)
+ : inset_(inset)
+{}
+
+
+string const InsetMinipageMailer::inset2string(Buffer const &) const
{
- widthp_ = strToInt(ll);
+ return params2string(inset_.params());
}
-void InsetMinipage::InsetButtonRelease(BufferView * bv, int x, int y,
- int button)
+void InsetMinipageMailer::string2params(string const & in,
+ InsetMinipage::Params & params)
{
- if (button == 3) {
-#if 0
-// we have to check first if we have a locking inset and if this locking inset
-// has a popup menu with the 3rd button
- if (the_locking_inset) {
- UpdatableInset * i;
- if ((i=the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE))) {
- i->InsetButtonRelease(bv, x, y, button);
+ params = InsetMinipage::Params();
+
+ if (in.empty())
return;
- }
+
+ istringstream data(STRCONV(in));
+ LyXLex lex(0, 0);
+ lex.setStream(data);
+
+ if (lex.isOK()) {
+ lex.next();
+ string const token = lex.getString();
+ if (token != "minipage")
+ return;
}
-#endif
- bv->owner()->getDialogs()->showMinipage(this);
- return;
- }
- InsetCollapsable::InsetButtonRelease(bv, x, y, button);
+
+ // This is part of the inset proper that is usually swallowed
+ // by Buffer::readInset
+ if (lex.isOK()) {
+ lex.next();
+ string const token = lex.getString();
+ if (token != "Minipage")
+ return;
+ }
+
+ if (lex.isOK()) {
+ params.read(lex);
+ }
+}
+
+
+string const
+InsetMinipageMailer::params2string(InsetMinipage::Params const & params)
+{
+ ostringstream data;
+ data << name_ << ' ';
+ params.write(data);
+ return STRCONV(data.str());
}