]> git.lyx.org Git - lyx.git/blobdiff - src/insets/inset.C
First step towards unified insets...
[lyx.git] / src / insets / inset.C
index 19249a574e4bf9a6ad7af379ba157397b4dec113..21ca38df6c5f569c926d1714fdae411304a1423b 100644 (file)
-/* This file is part of
- * ====================================================== 
- * 
- *           LyX, The Document Processor
- *      
- *         Copyright 1995 Matthias Ettrich
- *          Copyright 1995-2000 The LyX Team.
+/**
+ * \file inset.C
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
  *
- * ====================================================== */
+ * \author Alejandro Aguilar Sierra
+ * \author Jürgen Vigna
+ * \author Lars Gullik Bjønnes
+ * \author Matthias Ettrich
+ *
+ * Full author contact details are available in file CREDITS
+ */
 
 #include <config.h>
 
-#ifdef __GNUG__
-#pragma implementation "lyxinset.h"
-#endif
 
-#include "lyxinset.h"
-#include "debug.h"
+#include "inset.h"
+
 #include "BufferView.h"
+#include "debug.h"
+#include "funcrequest.h"
+#include "gettext.h"
+#include "lyxcursor.h"
+#include "lyxfont.h"
+#include "lyxtext.h"
+#include "WordLangTuple.h"
+
+#include "frontends/Painter.h"
+#include "frontends/mouse_state.h"
+
+#include "support/lstrings.h"
 #include "support/lstrings.h"
-#include "Painter.h"
 
 using std::endl;
 
-/* Insets default methods */
+// Insets default methods
+
+// Initialization of the counter for the inset id's,
+unsigned int Inset::inset_id = 0;
 
-bool Inset::Deletable() const
+Inset::Inset()
+       :       InsetBase(),
+               top_x(0), topx_set(false), top_baseline(0), scx(0),
+         id_(inset_id++), owner_(0), par_owner_(0),
+         background_color_(LColor::inherit)
+{}
+
+
+Inset::Inset(Inset const & in, bool same_id)
+       :       InsetBase(),
+               top_x(0), topx_set(false), top_baseline(0), scx(0), owner_(0),
+         name_(in.name_), background_color_(in.background_color_)
 {
-  return true;
+       if (same_id)
+               id_ = in.id();
+       else
+               id_ = inset_id++;
 }
 
 
-bool Inset::DirectWrite() const
+bool Inset::directWrite() const
 {
-  return false;
+       return false;
 }
 
 
-Inset::EDITABLE Inset::Editable() const
+Inset::EDITABLE Inset::editable() const
 {
-  return NOT_EDITABLE;
+       return NOT_EDITABLE;
 }
 
 
-bool Inset::IsTextInset() const
+void Inset::edit(BufferView *, int, int, mouse_button::state)
+{}
+
+
+void Inset::validate(LaTeXFeatures &) const
+{}
+
+
+bool Inset::autoDelete() const
 {
-       return ((LyxCode() == TEXT_CODE) ||
-               (LyxCode() == ERT_CODE) ||
-               (LyxCode() == FOOT_CODE) ||
-               (LyxCode() == MARGIN_CODE));
+       return false;
 }
 
 
-void Inset::Validate(LaTeXFeatures &) const
+void Inset::edit(BufferView *, bool)
+{}
+
+
+Inset::RESULT Inset::localDispatch(FuncRequest const &)
 {
+       return UNDISPATCHED;
 }
 
 
-bool Inset::AutoDelete() const
+#if 0
+LyXFont const Inset::convertFont(LyXFont const & font) const
 {
-  return false;
+#if 1
+       return font;
+#else
+       return LyXFont(font);
+#endif
 }
+#endif
 
 
-void Inset::Edit(BufferView *, int, int, unsigned int)
+string const Inset::editMessage() const
 {
+       return _("Opened inset");
 }
 
 
-LyXFont Inset::ConvertFont(LyXFont font)
+LyXText * Inset::getLyXText(BufferView const * bv, bool const) const
 {
-  return font;
+       if (owner())
+               return owner()->getLyXText(bv, false);
+       else
+               return bv->text;
 }
 
 
-char const * Inset::EditMessage() const 
+void Inset::setBackgroundColor(LColor::color color)
 {
-       return _("Opened inset");
+       background_color_ = color;
 }
 
- /* some stuff for inset locking */
 
-void UpdatableInset::InsetButtonPress(BufferView *, int x, int y, int button)
+LColor::color Inset::backgroundColor() const
 {
-       lyxerr.debug() << "Inset Button Press x=" << x
-                      << ", y=" << y << ", button=" << button << endl;
+       if (background_color_ == LColor::inherit) {
+               if (owner())
+                       return owner()->backgroundColor();
+               else
+                       return LColor::background;
+       } else
+               return background_color_;
 }
 
 
-void UpdatableInset::InsetButtonRelease(BufferView *, int x, int y, int button)
+int Inset::id() const
 {
-       lyxerr.debug() << "Inset Button Release x=" << x
-                      << ", y=" << y << ", button=" << button << endl;
+       return id_;
 }
 
-
-void UpdatableInset::InsetKeyPress(XKeyEvent *)
+void Inset::id(int id_arg)
 {
-       lyxerr.debug() << "Inset Keypress" << endl;
+       id_ = id_arg;
 }
 
+void Inset::setFont(BufferView *, LyXFont const &, bool, bool)
+{}
+
+
+bool Inset::forceDefaultParagraphs(Inset const * in) const
+{
+       if (owner())
+               return owner()->forceDefaultParagraphs(in);
+       return false;
+}
 
-void UpdatableInset::InsetMotionNotify(BufferView *, int x, int y, int state)
+int Inset::latexTextWidth(BufferView * bv) const
 {
-       lyxerr.debug() << "Inset Motion Notify x=" << x
-                      << ", y=" << y << ", state=" << state << endl;
+       if (owner())
+               return (owner()->latexTextWidth(bv));
+       return bv->workWidth();
 }
 
+// some stuff for inset locking
 
-void UpdatableInset::InsetUnlock(BufferView *)
+UpdatableInset::UpdatableInset()
+       : Inset(), cursor_visible_(false), block_drawing_(false)
+{}
+
+
+UpdatableInset::UpdatableInset(UpdatableInset const & in, bool same_id)
+       : Inset(in, same_id), cursor_visible_(false), block_drawing_(false)
+{}
+
+
+void UpdatableInset::insetUnlock(BufferView *)
 {
-       lyxerr.debug() << "Inset Unlock" << endl;
+       lyxerr[Debug::INFO] << "Inset Unlock" << endl;
 }
 
 
 // An updatable inset is highly editable by definition
-Inset::EDITABLE UpdatableInset::Editable() const
+Inset::EDITABLE UpdatableInset::editable() const
 {
        return HIGHLY_EDITABLE;
 }
 
 
-void UpdatableInset::ToggleInsetCursor(BufferView *)
+void UpdatableInset::toggleInsetCursor(BufferView *)
+{}
+
+
+void UpdatableInset::showInsetCursor(BufferView *, bool)
+{}
+
+
+void UpdatableInset::hideInsetCursor(BufferView *)
+{}
+
+
+void UpdatableInset::fitInsetCursor(BufferView *) const
+{}
+
+
+void UpdatableInset::edit(BufferView *, int, int, mouse_button::state)
+{}
+
+
+void UpdatableInset::edit(BufferView *, bool)
+{}
+
+
+void UpdatableInset::draw(BufferView *, LyXFont const &,
+                         int /* baseline */, float & x,
+                         bool/*cleared*/) const
 {
+       x += float(scx);
+       // ATTENTION: don't do the following here!!!
+       //    top_x = int(x);
+       //    top_baseline = baseline;
 }
 
 
-void UpdatableInset::ShowInsetCursor(BufferView *)
+void UpdatableInset::scroll(BufferView * bv, float s) const
 {
-}
+       LyXFont font;
+
+       if (!s) {
+               scx = 0;
+               return;
+       }
+
+       int const workW = bv->workWidth();
+       int const tmp_top_x = top_x - scx;
+
+       if (tmp_top_x > 0 &&
+           (tmp_top_x + width(bv, font)) < workW)
+               return;
+       if (s > 0 && top_x > 0)
+               return;
+
+       // int mx_scx=abs((width(bv,font) - bv->workWidth())/2);
+       //int const save_scx = scx;
+
+       scx = int(s * workW / 2);
+       // if (!display())
+       // scx += 20;
+
+       if ((tmp_top_x + scx + width(bv, font)) < (workW / 2)) {
+               scx += (workW / 2) - (tmp_top_x + scx + width(bv, font));
+       }
 
+       // bv->updateInset(const_cast<UpdatableInset *>(this), false);
+}
 
-void UpdatableInset::HideInsetCursor(BufferView *)
+void UpdatableInset::scroll(BufferView * bv, int offset) const
 {
+       if (offset > 0) {
+               if (!scx && top_x >= 20)
+                       return;
+               if ((top_x + offset) > 20)
+                       scx = 0;
+               // scx += offset - (top_x - scx + offset - 20);
+               else
+                       scx += offset;
+       } else {
+               LyXFont const font;
+               if (!scx && (top_x + width(bv, font)) < (bv->workWidth() - 20))
+                       return;
+               if ((top_x - scx + offset + width(bv, font)) < (bv->workWidth() - 20)) {
+                       scx = bv->workWidth() - width(bv, font) - top_x + scx - 20;
+               } else {
+                       scx += offset;
+               }
+       }
+//     bv->updateInset(const_cast<UpdatableInset *>(this), false);
 }
 
 
-void UpdatableInset::Edit(BufferView * bv, int, int, unsigned int)
+///  An updatable inset could handle lyx editing commands
+Inset::RESULT UpdatableInset::localDispatch(FuncRequest const & ev)
 {
-    LyXFont font;
+       if (ev.action == LFUN_MOUSE_RELEASE)
+               return (editable() == IS_EDITABLE) ? DISPATCHED : UNDISPATCHED;
+
+       if (!ev.argument.empty() && ev.action == LFUN_SCROLL_INSET) {
+               if (ev.argument.find('.') != ev.argument.npos) {
+                       float const xx = static_cast<float>(strToDbl(ev.argument));
+                       scroll(ev.view(), xx);
+               } else {
+                       int const xx = strToInt(ev.argument);
+                       scroll(ev.view(), xx);
+               }
+               ev.view()->updateInset(this, false);
+
+               return DISPATCHED;
+       }
+       return UNDISPATCHED;
+}
 
-    scx = 0;
 
-    mx_scx = abs((width(bv->painter(), font) - bv->workWidth()) / 2);
+int UpdatableInset::getMaxWidth(BufferView * bv, UpdatableInset const *) const
+{
+       int w;
+       if (owner()) {
+               w = static_cast<UpdatableInset*>
+                       (owner())->getMaxWidth(bv, this);
+       } else {
+               w = bv->text->workWidth(bv, const_cast<UpdatableInset *>(this));
+       }
+       if (w < 0) {
+               return -1;
+       }
+       // check for margins left/right and extra right margin "const 5"
+       if ((w - ((2 * TEXT_TO_INSET_OFFSET) + 5)) >= 0)
+               w -= (2 * TEXT_TO_INSET_OFFSET) + 5;
+       if (topx_set && owner()) {
+               w -= top_x;
+               w += owner()->x();
+       } else if (owner()) {
+               // this is needed as otherwise we might have a too large inset if
+               // its top_x afterwards changes to LeftMargin so we try to put at
+               // least the default margin as top_x
+               w -= 20;
+       }
+       if (w < 10) {
+               w = 10;
+       }
+       return w;
 }
 
 
-void UpdatableInset::draw(Painter &, LyXFont const &,
-                         int /* baseline */, float & x) const
+LyXCursor const & Inset::cursor(BufferView * bv) const
 {
-    if (scx) x += float(scx);
-// ATTENTION: don't do the following here!!!
-//    top_x = int(x);
-//    top_baseline = baseline;
+       if (owner())
+               return owner()->getLyXText(bv, false)->cursor;
+       return bv->text->cursor;
 }
 
 
-void UpdatableInset::SetFont(BufferView *, LyXFont const &, bool )
+WordLangTuple const
+UpdatableInset::selectNextWordToSpellcheck(BufferView *bv, float & value) const
 {
+       // we have to unlock ourself in this function by default!
+       bv->unlockInset(const_cast<UpdatableInset *>(this));
+       value = 0;
+       return WordLangTuple();
 }
 
 
-///  An updatable inset could handle lyx editing commands
-#ifdef SCROLL_INSET
-UpdatableInset::RESULT
-UpdatableInset::LocalDispatch(BufferView *, 
-                             int action, string const & arg) 
-#else
-UpdatableInset::RESULT
-UpdatableInset::LocalDispatch(BufferView *, int, string const &)
-#endif
+bool UpdatableInset::nextChange(BufferView * bv, lyx::pos_type &)
 {
-#ifdef SCROLL_INSET
-    if (action==LFUN_SCROLL_INSET)
-       {
-           float xx;
-           sscanf(arg.c_str(), "%f", &xx);     
-           scroll(xx);
+       // we have to unlock ourself in this function by default!
+       bv->unlockInset(const_cast<UpdatableInset *>(this));
+       return false;
+}
 
-           return DISPATCHED;
-       }
-#endif
-    return UNDISPATCHED; 
+bool UpdatableInset::searchForward(BufferView * bv, string const &,
+                                  bool, bool)
+{
+       // we have to unlock ourself in this function by default!
+       bv->unlockInset(const_cast<UpdatableInset *>(this));
+       return false;
 }
 
 
-int UpdatableInset::getMaxWidth(Painter & pain, UpdatableInset const *) const
+bool UpdatableInset::searchBackward(BufferView * bv, string const &,
+                                   bool, bool)
 {
-    if (owner())
-        return static_cast<UpdatableInset*>(owner())->getMaxWidth(pain, this);
-    return pain.paperWidth();
+       // we have to unlock ourself in this function by default!
+       bv->unlockInset(const_cast<UpdatableInset *>(this));
+       return false;
 }