X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Finsets%2Finsetminipage.C;h=c763d727a161ec9c8ce4e2d10a9e623d1a4399de;hb=26f1a5bfcae4b7fd7b946a1884c396d842b24925;hp=902b7ea2c4e8e7450d1b04289b0fad32f50ea64e;hpb=5115349d11baf91abeb9cbe369ebde28bef29288;p=lyx.git diff --git a/src/insets/insetminipage.C b/src/insets/insetminipage.C index 902b7ea2c4..c763d727a1 100644 --- a/src/insets/insetminipage.C +++ b/src/insets/insetminipage.C @@ -1,29 +1,33 @@ -/* 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 -#ifdef __GNUG__ -#pragma implementation -#endif #include "insetminipage.h" +#include "insettext.h" + +#include "BufferView.h" +#include "debug.h" +#include "funcrequest.h" #include "gettext.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 "frontends/LyXView.h" +#include "frontends/Dialogs.h" + #include "support/LOstream.h" #include "support/lstrings.h" -#include "debug.h" using std::ostream; using std::endl; @@ -57,155 +61,347 @@ using std::endl; // 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() +InsetMinipage::InsetMinipage(InsetMinipage const & in, bool same_id) + : InsetCollapsable(in, same_id), params_(in.params_) +{} + + +Inset * InsetMinipage::clone(Buffer const &, bool same_id) const { - hideDialog(); + return new InsetMinipage(*const_cast(this), same_id); } -void InsetMinipage::Write(Buffer const * buf, ostream & os) const +InsetMinipage::~InsetMinipage() { - os << getInsetName() << "\n"; - InsetCollapsable::Write(buf, os); + InsetMinipageMailer mailer(*this); + mailer.hideDialog(); } -Inset * InsetMinipage::Clone(Buffer const &) const +dispatch_result InsetMinipage::localDispatch(FuncRequest const & cmd) { - InsetMinipage * result = new InsetMinipage; - result->inset->init(inset); - - result->collapsed = collapsed; + Inset::RESULT result = UNDISPATCHED; + + switch (cmd.action) { + case LFUN_INSET_MODIFY: { + InsetMinipage::Params params; + InsetMinipageMailer::string2params(cmd.argument, params); + + params_.pos = params.pos; + params_.width = params.width; + + // FIXME: what magical mysterious commands are actually + // needed here to update the bloody size of the inset !!! + cmd.view()->updateInset(this); + result = DISPATCHED; + } + break; + + case LFUN_INSET_DIALOG_UPDATE: { + InsetMinipageMailer mailer(*this); + mailer.updateDialog(cmd.view()); + } + break; + + default: + result = InsetCollapsable::localDispatch(cmd); + } + return result; } -string const InsetMinipage::EditMessage() const +void InsetMinipage::Params::write(ostream & os) const { - return _("Opened Minipage Inset"); + os << "Minipage" << '\n' + << "position " << pos << '\n' + << "inner_position " << inner_pos << '\n' + << "height \"" << height.asString() << "\"\n" + << "width \"" << width.asString() << "\"\n"; } -int InsetMinipage::Latex(Buffer const * buf, - ostream & os, bool fragile, bool fp) const +void InsetMinipage::Params::read(LyXLex & lex) { - os << "\\begin{minipage}{\\columnwidth}%\n"; - - int i = inset->Latex(buf, os, fragile, fp); - os << "\\end{minipage}%\n"; - - return i + 2; + if (lex.isOK()) { + lex.next(); + string const token = lex.getString(); + if (token == "position") { + lex.next(); + pos = static_cast(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(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); + } + } } -bool InsetMinipage::InsertInsetAllowed(Inset * in) const +void InsetMinipage::write(Buffer const * buf, ostream & os) const { - if ((in->LyxCode() == Inset::FLOAT_CODE) || - (in->LyxCode() == Inset::MARGIN_CODE)) { - return false; - } - return true; + params_.write(os); + InsetCollapsable::write(buf, os); } -InsetMinipage::Position InsetMinipage::pos() const +void InsetMinipage::read(Buffer const * buf, LyXLex & lex) { - return pos_; + params_.read(lex); + InsetCollapsable::read(buf, lex); } -void InsetMinipage::pos(InsetMinipage::Position p) +int InsetMinipage::ascent(BufferView * bv, LyXFont const & font) const { - pos_ = p; + if (collapsed_) + return ascent_collapsed(); + else { + // Take placement into account. + int i = 0; + switch (params_.pos) { + case top: + i = InsetCollapsable::ascent(bv, font); + break; + case center: + i = (InsetCollapsable::ascent(bv, font) + + InsetCollapsable::descent(bv, font)) / 2; + break; + case bottom: + i = InsetCollapsable::descent(bv, font); + break; + } + return i; + } } -InsetMinipage::InnerPosition InsetMinipage::innerPos() const +int InsetMinipage::descent(BufferView * bv, LyXFont const & font) const { - return inner_pos_; + if (collapsed_) + return descent_collapsed(); + else { + // Take placement into account. + int i = 0; + switch (params_.pos) { + case top: + i = InsetCollapsable::descent(bv, font); + break; + case center: + i = (InsetCollapsable::ascent(bv, font) + + InsetCollapsable::descent(bv, font)) / 2; + break; + case bottom: + i = InsetCollapsable::ascent(bv, font); + break; + } + return i; + } } -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, bool fragile, bool fp) 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, fragile, fp); + + os << "\\end{minipage}%\n"; + return i + 2; } -void InsetMinipage::height(LyXLength const & ll) +bool InsetMinipage::insetAllowed(Inset::Code code) const { - height_ = ll; + if ((code == Inset::FLOAT_CODE) || (code == Inset::MARGIN_CODE)) + return false; + + return InsetCollapsable::insetAllowed(code); } -string const & InsetMinipage::width() const +bool InsetMinipage::showInsetDialog(BufferView * bv) const { - return width_; + if (!inset.showInsetDialog(bv)) { + InsetMinipage * tmp = const_cast(this); + InsetMinipageMailer mailer(*tmp); + mailer.showDialog(bv); + } + + return true; } -void InsetMinipage::width(string const & ll) +int InsetMinipage::getMaxWidth(BufferView * bv, UpdatableInset const * inset) + const { - width_ = ll; + if (owner() && + static_cast(owner())->getMaxWidth(bv, inset) < 0) { + return -1; + } + if (!params_.width.zero()) { + int ww1 = latexTextWidth(bv); + int ww2 = InsetCollapsable::getMaxWidth(bv, inset); + if (ww2 > 0 && ww2 < ww1) { + return ww2; + } + return ww1; + } + // this should not happen! + return InsetCollapsable::getMaxWidth(bv, inset); } -int InsetMinipage::widthp() const + +int InsetMinipage::latexTextWidth(BufferView * bv) const { - return widthp_; + return params_.width.inPixels(InsetCollapsable::latexTextWidth(bv)); } -void InsetMinipage::widthp(int 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() const { - widthp_ = ll; + return params2string(inset_.params()); } -void InsetMinipage::widthp(string const & ll) +void InsetMinipageMailer::string2params(string const & in, + InsetMinipage::Params & params) { - widthp_ = strToInt(ll); + params = InsetMinipage::Params(); + + istringstream data(in); + LyXLex lex(0,0); + lex.setStream(data); + + if (lex.isOK()) { + lex.next(); + string const token = lex.getString(); + if (token != "minipage") + return; + } + + // 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); + } } -void InsetMinipage::InsetButtonRelease(BufferView * bv, int x, int y, - int button) +string const +InsetMinipageMailer::params2string(InsetMinipage::Params const & 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); - return; - } - } -#endif - bv->owner()->getDialogs()->showMinipage(this); - return; - } - InsetCollapsable::InsetButtonRelease(bv, x, y, button); + ostringstream data; + data << name_ << ' '; + params.write(data); + + return data.str(); }