]> git.lyx.org Git - lyx.git/blobdiff - src/insets/Inset.cpp
This optional argument to the InsetCollapsable constructor
[lyx.git] / src / insets / Inset.cpp
index e2213ef51df6857476d78726896e097d0714d8c4..40f33d9fd11d540d405ec1dc525abbd4610f0a3f 100644 (file)
 
 #include "Inset.h"
 
+#include "buffer_funcs.h"
 #include "Buffer.h"
+#include "BufferList.h"
 #include "BufferParams.h"
 #include "BufferView.h"
 #include "CoordCache.h"
 #include "Cursor.h"
-#include "debug.h"
 #include "Dimension.h"
 #include "DispatchResult.h"
 #include "FuncRequest.h"
 #include "FuncStatus.h"
-#include "gettext.h"
+#include "MetricsInfo.h"
 #include "Text.h"
 #include "TextClass.h"
-#include "MetricsInfo.h"
-#include "MetricsInfo.h"
 
-#include "frontends/Painter.h"
 #include "frontends/Application.h"
+#include "frontends/Painter.h"
 
 #include "support/convert.h"
-
-#include <boost/current_function.hpp>
+#include "support/debug.h"
+#include "support/docstream.h"
+#include "support/ExceptionMessage.h"
+#include "support/gettext.h"
+#include "support/lassert.h"
 
 #include <map>
-#include <typeinfo>
 
+using namespace std;
+using namespace lyx::support;
 
 namespace lyx {
 
-extern bool quitting;
-
 class InsetName {
 public:
-       InsetName(std::string const & n, InsetCode c) : name(n), code(c) {}
-       std::string name;
+       InsetName(string const & n, InsetCode c) : name(n), code(c) {}
+       string name;
        InsetCode code;
 };
 
 
-typedef std::map<std::string, InsetCode> TranslatorMap;
+typedef map<string, InsetCode> TranslatorMap;
 
 
 static TranslatorMap const build_translator()
@@ -93,26 +94,26 @@ static TranslatorMap const build_translator()
                InsetName("index_print", INDEX_PRINT_CODE),
                InsetName("nomencl_print", NOMENCL_PRINT_CODE),
                InsetName("optarg", OPTARG_CODE),
-               InsetName("environment", ENVIRONMENT_CODE),
-               InsetName("hfill", HFILL_CODE),
                InsetName("newline", NEWLINE_CODE),
                InsetName("line", LINE_CODE),
                InsetName("branch", BRANCH_CODE),
                InsetName("box", BOX_CODE),
                InsetName("flex", FLEX_CODE),
+               InsetName("space", SPACE_CODE),
                InsetName("vspace", VSPACE_CODE),
                InsetName("mathmacroarg", MATHMACROARG_CODE),
                InsetName("listings", LISTINGS_CODE),
                InsetName("info", INFO_CODE),
                InsetName("collapsable", COLLAPSABLE_CODE),
-               InsetName("pagebreak", PAGEBREAK_CODE),
+               InsetName("newpage", NEWPAGE_CODE),
+               InsetName("tablecell", CELL_CODE)
        };
 
-       std::size_t const insetnames_size =
+       size_t const insetnames_size =
                sizeof(insetnames) / sizeof(insetnames[0]);
 
-       std::map<std::string, InsetCode> data;
-       for (std::size_t i = 0; i != insetnames_size; ++i) {
+       map<string, InsetCode> data;
+       for (size_t i = 0; i != insetnames_size; ++i) {
                InsetName const & var = insetnames[i];
                data[var.name] = var.code;
        }
@@ -121,19 +122,70 @@ static TranslatorMap const build_translator()
 }
 
 
+void Inset::setBuffer(Buffer & buffer)
+{
+       buffer_ = &buffer;
+}
+
+
+Buffer & Inset::buffer()
+{
+       if (!buffer_) {
+               odocstringstream s;
+               lyxerr << "LyX Code: " << lyxCode() << " name: " << name() << std::endl;
+               s << "LyX Code: " << lyxCode() << " name: " << name();
+               LASSERT(false, /**/);
+               throw ExceptionMessage(BufferException, 
+                       from_ascii("Inset::buffer_ member not initialized!"), s.str());
+       }
+       return *buffer_;
+}
+
+
+Buffer const & Inset::buffer() const
+{
+       return const_cast<Inset *>(this)->buffer();
+}
+
+
+bool Inset::isBufferValid() const
+{
+       return buffer_ && theBufferList().isLoaded(buffer_);
+}
+
+
 docstring Inset::name() const
 {
        return from_ascii("unknown");
 }
 
 
+void Inset::initView()
+{
+       if (isLabeled())
+               lyx::updateLabels(buffer());
+}
+
+
+docstring Inset::toolTip(BufferView const &, int, int) const
+{
+       return docstring();
+}
+
+
+docstring Inset::contextMenu(BufferView const &, int, int) const
+{
+       return docstring();
+}
+
+
 Dimension const Inset::dimension(BufferView const & bv) const
 {
        return bv.coordCache().getInsets().dim(this);
 }
 
 
-InsetCode insetCode(std::string const & name)
+InsetCode insetCode(string const & name)
 {
        static TranslatorMap const translator = build_translator();
 
@@ -142,7 +194,7 @@ InsetCode insetCode(std::string const & name)
 }
 
 
-std::string insetName(InsetCode c) 
+string insetName(InsetCode c) 
 {
        static TranslatorMap const translator = build_translator();
 
@@ -152,7 +204,7 @@ std::string insetName(InsetCode c)
                if (it->second == c)
                        return it->first;
        }
-       return std::string();
+       return string();
 }
 
 
@@ -164,10 +216,18 @@ void Inset::dispatch(Cursor & cur, FuncRequest & cmd)
 }
 
 
-void Inset::doDispatch(Cursor & cur, FuncRequest &)
+void Inset::doDispatch(Cursor & cur, FuncRequest &cmd)
 {
-       cur.noUpdate();
-       cur.undispatched();
+       switch (cmd.action) {
+       case LFUN_INSET_TOGGLE:
+               edit(cur, true);
+               cur.dispatched();
+               break;
+       default:
+               cur.noUpdate();
+               cur.undispatched();
+               break;
+       }
 }
 
 
@@ -187,31 +247,37 @@ bool Inset::getStatus(Cursor &, FuncRequest const & cmd,
                // Allow modification of our data.
                // This needs to be handled in the doDispatch method of our
                // instantiatable children.
-               flag.enabled(true);
+               flag.setEnabled(true);
                return true;
 
        case LFUN_INSET_INSERT:
                // Don't allow insertion of new insets.
                // Every inset that wants to allow new insets from open
                // dialogs needs to override this.
-               flag.enabled(false);
+               flag.setEnabled(false);
+               return true;
+
+       case LFUN_INSET_TOGGLE:
+               // remove this if we dissociate toggle from edit.
+               flag.setEnabled(editable() == IS_EDITABLE);
                return true;
 
        default:
-               return false;
+               break;
        }
+       return false;
 }
 
 
-void Inset::edit(Cursor &, bool)
+void Inset::edit(Cursor &, bool, EntryDirection)
 {
-       LYXERR(Debug::INSETS, BOOST_CURRENT_FUNCTION << ": edit left/right");
+       LYXERR(Debug::INSETS, "edit left/right");
 }
 
 
 Inset * Inset::editXY(Cursor &, int x, int y)
 {
-       LYXERR(Debug::INSETS, BOOST_CURRENT_FUNCTION << ": x=" << x << " y= " << y);
+       LYXERR(Debug::INSETS, "x: " << x << " y: " << y);
        return this;
 }
 
@@ -219,11 +285,9 @@ Inset * Inset::editXY(Cursor &, int x, int y)
 Inset::idx_type Inset::index(row_type row, col_type col) const
 {
        if (row != 0)
-               lyxerr << BOOST_CURRENT_FUNCTION
-                      << ": illegal row: " << row << std::endl;
+               LYXERR0("illegal row: " << row);
        if (col != 0)
-               lyxerr << BOOST_CURRENT_FUNCTION
-                      << ": illegal col: " << col << std::endl;
+               LYXERR0("illegal col: " << col);
        return 0;
 }
 
@@ -240,8 +304,7 @@ bool Inset::idxUpDown(Cursor &, bool) const
 }
 
 
-int Inset::docbook(Buffer const &,
-       odocstream &, OutputParams const &) const
+int Inset::docbook(odocstream &, OutputParams const &) const
 {
        return 0;
 }
@@ -265,7 +328,7 @@ bool Inset::autoDelete() const
 }
 
 
-docstring const Inset::editMessage() const
+docstring Inset::editMessage() const
 {
        return _("Opened inset");
 }
@@ -274,7 +337,7 @@ docstring const Inset::editMessage() const
 void Inset::cursorPos(BufferView const & /*bv*/, CursorSlice const &,
                bool, int & x, int & y) const
 {
-       lyxerr << "Inset::cursorPos called directly" << std::endl;
+       LYXERR0("Inset::cursorPos called directly");
        x = 100;
        y = 100;
 }
@@ -329,7 +392,7 @@ void Inset::drawMarkers2(PainterInfo & pi, int x, int y) const
 }
 
 
-bool Inset::editing(BufferView * bv) const
+bool Inset::editing(BufferView const * bv) const
 {
        return bv->cursor().isInside(this);
 }
@@ -355,26 +418,25 @@ bool Inset::covers(BufferView const & bv, int x, int y) const
 
 InsetLayout const & Inset::getLayout(BufferParams const & bp) const
 {
-       return bp.getTextClass().insetlayout(name());  
+       return bp.documentClass().insetLayout(name());  
 }
 
 
 void Inset::dump() const
 {
-       Buffer buf("foo", 1);
-       write(buf, lyxerr);
+       write(lyxerr);
 }
 
 
 ColorCode Inset::backgroundColor() const
 {
-       return Color_background;
+       return Color_none;
 }
 
 
 void Inset::setPosCache(PainterInfo const & pi, int x, int y) const
 {
-       //lyxerr << "Inset:: position cache to " << x << " " << y << std::endl;
+       //LYXERR("Inset: set position cache to " << x << " " << y);
        pi.base.bv->coordCache().insets().add(this, x, y);
 }
 
@@ -387,9 +449,13 @@ void Inset::setDimCache(MetricsInfo const & mi, Dimension const & dim) const
 
 Buffer const * Inset::updateFrontend() const
 {
-       if (quitting)
-               return 0;
-       return theApp()->updateInset(this);
+       return theApp() ? theApp()->updateInset(this) : 0;
+}
+
+
+docstring Inset::completionPrefix(Cursor const &) const 
+{
+       return docstring();
 }
 
 } // namespace lyx