]> git.lyx.org Git - lyx.git/blobdiff - src/insets/insetminipage.C
My patch from yesterday
[lyx.git] / src / insets / insetminipage.C
index 3424835567afcf4082e2007c02ea3f66666da368..c763d727a161ec9c8ce4e2d10a9e623d1a4399de 100644 (file)
@@ -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 <config.h>
 
-#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,136 +61,172 @@ 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");
-       widthp_ = 100; // set default to 100% of column_width
 }
 
 
-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<InsetMinipage *>(this), same_id);
 }
 
 
-void InsetMinipage::Write(Buffer const * buf, ostream & os) const 
+InsetMinipage::~InsetMinipage()
 {
-       os << getInsetName() << "\n"
-          << "position " << pos_ << "\n"
-          << "inner_position " << inner_pos_ << "\n"
-          << "height \"" << height_ << "\"\n"
-          << "width \"" << width_ << "\"\n"
-          << "widthp " << widthp_ << "\n";
-       InsetCollapsable::Write(buf, os);
+       InsetMinipageMailer mailer(*this);
+       mailer.hideDialog();
 }
 
 
-void InsetMinipage::Read(Buffer const * buf, LyXLex & lex)
+dispatch_result InsetMinipage::localDispatch(FuncRequest const & cmd)
 {
-    string token;
-
-    if (lex.IsOK()) {
-       lex.next();
-       token = lex.GetString();
-       if (token == "position") {
-           lex.next();
-           pos_ = static_cast<Position>(lex.GetInteger());
-           token = string();
-       } else {
-               lyxerr << "InsetMinipage::Read: Missing 'position'-tag!"
-                      << endl;
-       }
-    }
-    if (lex.IsOK()) {
-       if (token.empty()) {
-           lex.next();
-           token = lex.GetString();
-       }
-       if (token == "inner_position") {
-           lex.next();
-           inner_pos_ = static_cast<InnerPosition>(lex.GetInteger());
-           token = string();
-       } else {
-               lyxerr << "InsetMinipage::Read: Missing 'inner_position'-tag!"
-                      << endl;
+       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;
        }
-    }
-    if (lex.IsOK()) {
-       if (token.empty()) {
-           lex.next();
-           token = lex.GetString();
+       break;
+
+       case LFUN_INSET_DIALOG_UPDATE: {
+               InsetMinipageMailer mailer(*this);
+               mailer.updateDialog(cmd.view());
        }
-       if (token == "height") {
-           lex.next();
-           height_ = lex.GetString();
-           token = string();
-       } else {
-               lyxerr << "InsetMinipage::Read: Missing 'height'-tag!"
-                      << endl;
+       break;
+
+       default:
+               result = InsetCollapsable::localDispatch(cmd);
        }
-    }
-    if (lex.IsOK()) {
-       if (token.empty()) {
-           lex.next();
-           token = lex.GetString();
+
+       return result;
+}
+
+
+void InsetMinipage::Params::write(ostream & os) const
+{
+       os << "Minipage" << '\n'
+          << "position " << pos << '\n'
+          << "inner_position " << inner_pos << '\n'
+          << "height \"" << height.asString() << "\"\n"
+          << "width \"" << width.asString() << "\"\n";
+}
+
+
+void InsetMinipage::Params::read(LyXLex & lex)
+{
+       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 (token == "width") {
-           lex.next();
-           width_ = lex.GetString();
-           token = string();
-       } else {
-               lyxerr << "InsetMinipage::Read: Missing 'width'-tag!"
-                      << endl;
+       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()) {
-       if (token.empty()) {
-           lex.next();
-           token = lex.GetString();
+       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 (token == "widthp") {
-           lex.next();
-           widthp_ = lex.GetInteger();
-           token = string();
-       } else {
-               lyxerr << "InsetMinipage::Read: Missing 'widthp_'-tag!"
-                      << endl;
+       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);
+               }
        }
-    }
-    InsetCollapsable::Read(buf, lex);
 }
 
 
-Inset * InsetMinipage::Clone(Buffer const &) const
+void InsetMinipage::write(Buffer const * buf, ostream & os) const
 {
-       InsetMinipage * result = new InsetMinipage;
-       result->inset->init(inset);
-       
-       result->collapsed = collapsed;
-       return result;
+       params_.write(os);
+       InsetCollapsable::write(buf, os);
+}
+
+
+void InsetMinipage::read(Buffer const * buf, LyXLex & lex)
+{
+       params_.read(lex);
+       InsetCollapsable::read(buf, lex);
 }
 
 
 int InsetMinipage::ascent(BufferView * bv, LyXFont const & font) const
 {
-       lyxerr << "InsetMinipage::ascent" << endl;
-       
-       if (collapsed)
-               return ascent_collapsed(bv->painter(), font);
+       if (collapsed_)
+               return ascent_collapsed();
        else {
                // Take placement into account.
                int i = 0;
-               switch (pos_) {
+               switch (params_.pos) {
                case top:
                        i = InsetCollapsable::ascent(bv, font);
                        break;
@@ -205,12 +245,12 @@ int InsetMinipage::ascent(BufferView * bv, LyXFont const & font) const
 
 int InsetMinipage::descent(BufferView * bv, LyXFont const & font) const
 {
-       if (collapsed)
-               return descent_collapsed(bv->painter(), font);
+       if (collapsed_)
+               return descent_collapsed();
        else {
                // Take placement into account.
                int i = 0;
-               switch (pos_) {
+               switch (params_.pos) {
                case top:
                        i = InsetCollapsable::descent(bv, font);
                        break;
@@ -227,143 +267,141 @@ int InsetMinipage::descent(BufferView * bv, LyXFont const & font) const
 }
 
 
-string const InsetMinipage::EditMessage() const
+string const InsetMinipage::editMessage() const
 {
        return _("Opened Minipage Inset");
 }
 
 
-int InsetMinipage::Latex(Buffer const * buf,
+int InsetMinipage::latex(Buffer const * buf,
                         ostream & os, bool fragile, bool fp) const
 {
        string s_pos;
-       switch (pos_) {
+       switch (params_.pos) {
        case top:
-               s_pos += "t";
+               s_pos += 't';
                break;
        case center:
-               s_pos += "c";
+               s_pos += 'c';
                break;
        case bottom:
-               s_pos += "b";
+               s_pos += 'b';
                break;
        }
-       
-       if (width_.empty()) {
-           os << "\\begin{minipage}[" << s_pos << "]{."
-              << widthp_ << "\\columnwidth}%\n";
-       } else {
-           os << "\\begin{minipage}[" << s_pos << "]{"
-              << width_ << "}%\n";
-       }
-       
-       int i = inset->Latex(buf, os, fragile, fp);
+       os << "\\begin{minipage}[" << s_pos << "]{"
+          << params_.width.asLatexString() << "}%\n";
+
+       int i = inset.latex(buf, os, fragile, fp);
+
        os << "\\end{minipage}%\n";
-       
        return i + 2;
 }
 
 
-bool InsetMinipage::InsertInsetAllowed(Inset * in) const
+bool InsetMinipage::insetAllowed(Inset::Code code) const
 {
-       if ((in->LyxCode() == Inset::FLOAT_CODE) ||
-           (in->LyxCode() == Inset::MARGIN_CODE)) {
+       if ((code == Inset::FLOAT_CODE) || (code == Inset::MARGIN_CODE))
                return false;
-       }
-       return true;
-}
-
 
-InsetMinipage::Position InsetMinipage::pos() const 
-{
-       return pos_;
+       return InsetCollapsable::insetAllowed(code);
 }
 
 
-void InsetMinipage::pos(InsetMinipage::Position p)
+bool InsetMinipage::showInsetDialog(BufferView * bv) const
 {
-       pos_ = p;
-}
-
+       if (!inset.showInsetDialog(bv)) {
+               InsetMinipage * tmp = const_cast<InsetMinipage *>(this);
+               InsetMinipageMailer mailer(*tmp);
+               mailer.showDialog(bv);
+       }
 
-InsetMinipage::InnerPosition InsetMinipage::innerPos() const
-{
-       return inner_pos_;
+       return true;
 }
 
 
-void InsetMinipage::innerPos(InsetMinipage::InnerPosition ip)
+int InsetMinipage::getMaxWidth(BufferView * bv, UpdatableInset const * inset)
+       const
 {
-       inner_pos_ = ip;
+       if (owner() &&
+           static_cast<UpdatableInset*>(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);
 }
 
 
-string const & InsetMinipage::height() const
+int InsetMinipage::latexTextWidth(BufferView * bv) const
 {
-       return height_;
+       return params_.width.inPixels(InsetCollapsable::latexTextWidth(bv));
 }
 
 
-void InsetMinipage::height(string const & ll)
-{
-       height_ = ll;
-}
+InsetMinipage::Params::Params()
+       : pos(center),
+         inner_pos(inner_center),
+         width(100, LyXLength::PCW)
+{}
 
 
-string const & InsetMinipage::width() const
-{
-       return width_;
-}
+string const InsetMinipageMailer:: name_("minipage");
 
+InsetMinipageMailer::InsetMinipageMailer(InsetMinipage & inset)
+       : inset_(inset)
+{}
 
-void InsetMinipage::width(string const & ll)
-{
-       width_ = ll;
-}
 
-int InsetMinipage::widthp() const
+string const InsetMinipageMailer::inset2string() const
 {
-       return widthp_;
+       return params2string(inset_.params());
 }
 
 
-void InsetMinipage::widthp(int ll)
+void InsetMinipageMailer::string2params(string const & in,
+                                       InsetMinipage::Params & params)
 {
-       widthp_ = ll;
-}
+       params = InsetMinipage::Params();
 
+       istringstream data(in);
+       LyXLex lex(0,0);
+       lex.setStream(data);
 
-void InsetMinipage::widthp(string const & ll)
-{
-       widthp_ = strToInt(ll);
-}
+       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;
+       }
 
-void InsetMinipage::InsetButtonRelease(BufferView * bv, int x, int y,
-                                      int button)
-{
-    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;
-           }
+       if (lex.isOK()) {
+               params.read(lex);
        }
-#endif
-       bv->owner()->getDialogs()->showMinipage(this);
-       return;
-    }
-    InsetCollapsable::InsetButtonRelease(bv, x, y, button);
 }
 
-int InsetMinipage::getMaxWidth(Painter & pain, UpdatableInset const * inset)
-    const
+
+string const
+InsetMinipageMailer::params2string(InsetMinipage::Params const & params)
 {
-    if (!width_.empty())
-       return VSpace(width_).inPixels(0, 0);
-    return InsetCollapsable::getMaxWidth(pain, inset) / 100 * widthp_;
+       ostringstream data;
+       data << name_ << ' ';
+       params.write(data);
+
+       return data.str();
 }