X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Finsets%2FInset.cpp;h=40f33d9fd11d540d405ec1dc525abbd4610f0a3f;hb=d6f1915684328c6e3fe61c6eef8846b5cabec334;hp=8664c5f8d9b410aed637c6b828da879b393d0802;hpb=4ba4d701f798488c098ecbc740c3020ed514cd96;p=lyx.git diff --git a/src/insets/Inset.cpp b/src/insets/Inset.cpp index 8664c5f8d9..40f33d9fd1 100644 --- a/src/insets/Inset.cpp +++ b/src/insets/Inset.cpp @@ -16,97 +16,104 @@ #include "Inset.h" +#include "buffer_funcs.h" #include "Buffer.h" +#include "BufferList.h" +#include "BufferParams.h" #include "BufferView.h" -#include "Color.h" #include "CoordCache.h" #include "Cursor.h" -#include "debug.h" -#include "debug.h" #include "Dimension.h" #include "DispatchResult.h" #include "FuncRequest.h" #include "FuncStatus.h" -#include "gettext.h" -#include "Text.h" -#include "MetricsInfo.h" #include "MetricsInfo.h" +#include "Text.h" +#include "TextClass.h" +#include "frontends/Application.h" #include "frontends/Painter.h" -#include +#include "support/convert.h" +#include "support/debug.h" +#include "support/docstream.h" +#include "support/ExceptionMessage.h" +#include "support/gettext.h" +#include "support/lassert.h" #include -#include +using namespace std; +using namespace lyx::support; namespace lyx { class InsetName { public: - InsetName(std::string const & n, Inset::Code c) - : name(n), code(c) {} - std::string name; - Inset::Code code; + InsetName(string const & n, InsetCode c) : name(n), code(c) {} + string name; + InsetCode code; }; -typedef std::map TranslatorMap; +typedef map TranslatorMap; static TranslatorMap const build_translator() { InsetName const insetnames[] = { - InsetName("toc", Inset::TOC_CODE), - InsetName("quote", Inset::QUOTE_CODE), - InsetName("ref", Inset::REF_CODE), - InsetName("url", Inset::URL_CODE), - InsetName("htmlurl", Inset::HTMLURL_CODE), - InsetName("separator", Inset::SEPARATOR_CODE), - InsetName("ending", Inset::ENDING_CODE), - InsetName("label", Inset::LABEL_CODE), - InsetName("note", Inset::NOTE_CODE), - InsetName("accent", Inset::ACCENT_CODE), - InsetName("math", Inset::MATH_CODE), - InsetName("index", Inset::INDEX_CODE), - InsetName("nomenclature", Inset::NOMENCL_CODE), - InsetName("include", Inset::INCLUDE_CODE), - InsetName("graphics", Inset::GRAPHICS_CODE), - InsetName("bibitem", Inset::BIBITEM_CODE), - InsetName("bibtex", Inset::BIBTEX_CODE), - InsetName("text", Inset::TEXT_CODE), - InsetName("ert", Inset::ERT_CODE), - InsetName("foot", Inset::FOOT_CODE), - InsetName("margin", Inset::MARGIN_CODE), - InsetName("float", Inset::FLOAT_CODE), - InsetName("wrap", Inset::WRAP_CODE), - InsetName("specialchar", Inset::SPECIALCHAR_CODE), - InsetName("tabular", Inset::TABULAR_CODE), - InsetName("external", Inset::EXTERNAL_CODE), - InsetName("caption", Inset::CAPTION_CODE), - InsetName("mathmacro", Inset::MATHMACRO_CODE), - InsetName("cite", Inset::CITE_CODE), - InsetName("float_list", Inset::FLOAT_LIST_CODE), - InsetName("index_print", Inset::INDEX_PRINT_CODE), - InsetName("nomencl_print", Inset::NOMENCL_PRINT_CODE), - InsetName("optarg", Inset::OPTARG_CODE), - InsetName("environment", Inset::ENVIRONMENT_CODE), - InsetName("hfill", Inset::HFILL_CODE), - InsetName("newline", Inset::NEWLINE_CODE), - InsetName("line", Inset::LINE_CODE), - InsetName("branch", Inset::BRANCH_CODE), - InsetName("box", Inset::BOX_CODE), - InsetName("charstyle", Inset::CHARSTYLE_CODE), - InsetName("vspace", Inset::VSPACE_CODE), - InsetName("mathmacroarg", Inset::MATHMACROARG_CODE), - InsetName("listings", Inset::LISTINGS_CODE), + InsetName("toc", TOC_CODE), + InsetName("quote", QUOTE_CODE), + InsetName("ref", REF_CODE), + InsetName("href", HYPERLINK_CODE), + InsetName("separator", SEPARATOR_CODE), + InsetName("ending", ENDING_CODE), + InsetName("label", LABEL_CODE), + InsetName("note", NOTE_CODE), + InsetName("accent", ACCENT_CODE), + InsetName("math", MATH_CODE), + InsetName("index", INDEX_CODE), + InsetName("nomenclature", NOMENCL_CODE), + InsetName("include", INCLUDE_CODE), + InsetName("graphics", GRAPHICS_CODE), + InsetName("bibitem", BIBITEM_CODE), + InsetName("bibtex", BIBTEX_CODE), + InsetName("text", TEXT_CODE), + InsetName("ert", ERT_CODE), + InsetName("foot", FOOT_CODE), + InsetName("margin", MARGIN_CODE), + InsetName("float", FLOAT_CODE), + InsetName("wrap", WRAP_CODE), + InsetName("specialchar", SPECIALCHAR_CODE), + InsetName("tabular", TABULAR_CODE), + InsetName("external", EXTERNAL_CODE), + InsetName("caption", CAPTION_CODE), + InsetName("mathmacro", MATHMACRO_CODE), + InsetName("citation", CITE_CODE), + InsetName("floatlist", FLOAT_LIST_CODE), + InsetName("index_print", INDEX_PRINT_CODE), + InsetName("nomencl_print", NOMENCL_PRINT_CODE), + InsetName("optarg", OPTARG_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("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 data; - for (std::size_t i = 0; i != insetnames_size; ++i) { + map data; + for (size_t i = 0; i != insetnames_size; ++i) { InsetName const & var = insetnames[i]; data[var.name] = var.code; } @@ -115,21 +122,70 @@ static TranslatorMap const build_translator() } -/// pretty arbitrary dimensions -Inset::Inset() - : dim_(10, 10, 10) -{} +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(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(); +} -std::auto_ptr Inset::clone() const +docstring Inset::contextMenu(BufferView const &, int, int) const { - std::auto_ptr b = doClone(); - BOOST_ASSERT(typeid(*b) == typeid(*this)); - return b; + return docstring(); } -Inset::Code Inset::translate(std::string const & name) +Dimension const Inset::dimension(BufferView const & bv) const +{ + return bv.coordCache().getInsets().dim(this); +} + + +InsetCode insetCode(string const & name) { static TranslatorMap const translator = build_translator(); @@ -138,6 +194,20 @@ Inset::Code Inset::translate(std::string const & name) } +string insetName(InsetCode c) +{ + static TranslatorMap const translator = build_translator(); + + TranslatorMap::const_iterator it = translator.begin(); + TranslatorMap::const_iterator end = translator.end(); + for (; it != end; ++it) { + if (it->second == c) + return it->first; + } + return string(); +} + + void Inset::dispatch(Cursor & cur, FuncRequest & cmd) { cur.updateFlags(Update::Force | Update::FitCursor); @@ -146,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; + } } @@ -169,34 +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" << std::endl; + LYXERR(Debug::INSETS, "edit left/right"); } Inset * Inset::editXY(Cursor &, int x, int y) { - LYXERR(Debug::INSETS) << BOOST_CURRENT_FUNCTION - << ": x=" << x << " y= " << y - << std::endl; + LYXERR(Debug::INSETS, "x: " << x << " y: " << y); return this; } @@ -204,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; } @@ -225,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; } @@ -250,7 +328,7 @@ bool Inset::autoDelete() const } -docstring const Inset::editMessage() const +docstring Inset::editMessage() const { return _("Opened inset"); } @@ -259,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; } @@ -282,11 +360,13 @@ void Inset::metricsMarkers2(Dimension & dim, int framesize) const void Inset::drawMarkers(PainterInfo & pi, int x, int y) const { - Color::color pen_color = mouseHovered() || editing(pi.base.bv)? - Color::mathframe : Color::mathcorners; + ColorCode pen_color = mouseHovered() || editing(pi.base.bv)? + Color_mathframe : Color_mathcorners; + + Dimension const dim = dimension(*pi.base.bv); - int const t = x + width() - 1; - int const d = y + descent(); + int const t = x + dim.width() - 1; + int const d = y + dim.descent(); pi.pain.line(x, d - 3, x, d, pen_color); pi.pain.line(t, d - 3, t, d, pen_color); pi.pain.line(x, d, x + 3, d, pen_color); @@ -297,12 +377,13 @@ void Inset::drawMarkers(PainterInfo & pi, int x, int y) const void Inset::drawMarkers2(PainterInfo & pi, int x, int y) const { - Color::color pen_color = mouseHovered() || editing(pi.base.bv)? - Color::mathframe : Color::mathcorners; + ColorCode pen_color = mouseHovered() || editing(pi.base.bv)? + Color_mathframe : Color_mathcorners; drawMarkers(pi, x, y); - int const t = x + width() - 1; - int const a = y - ascent(); + Dimension const dim = dimension(*pi.base.bv); + int const t = x + dim.width() - 1; + int const a = y - dim.ascent(); pi.pain.line(x, a + 3, x, a, pen_color); pi.pain.line(t, a + 3, t, a, pen_color); pi.pain.line(x, a, x + 3, a, pen_color); @@ -311,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); } @@ -331,51 +412,50 @@ int Inset::yo(BufferView const & bv) const bool Inset::covers(BufferView const & bv, int x, int y) const { - //lyxerr << "Inset::covers, x: " << x << " y: " << y - // << " xo: " << xo(bv) << " yo: " << yo() - // << " x1: " << xo(bv) << " x2: " << xo() + width() - // << " y1: " << yo(bv) - ascent() << " y2: " << yo() + descent() - // << std::endl; - return bv.coordCache().getInsets().has(this) - && x >= xo(bv) - && x <= xo(bv) + width() - && y >= yo(bv) - ascent() - && y <= yo(bv) + descent(); + return bv.coordCache().getInsets().covers(this, x, y); +} + + +InsetLayout const & Inset::getLayout(BufferParams const & bp) const +{ + return bp.documentClass().insetLayout(name()); } void Inset::dump() const { - Buffer buf("foo", 1); - write(buf, lyxerr); + write(lyxerr); } -Color_color Inset::backgroundColor() const +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); } -///////////////////////////////////////// - -bool isEditableInset(Inset const * inset) +void Inset::setDimCache(MetricsInfo const & mi, Dimension const & dim) const { - return inset && inset->editable(); + mi.base.bv->coordCache().insets().add(this, dim); } -bool isHighlyEditableInset(Inset const * inset) +Buffer const * Inset::updateFrontend() const { - return inset && inset->editable() == Inset::HIGHLY_EDITABLE; + return theApp() ? theApp()->updateInset(this) : 0; } +docstring Inset::completionPrefix(Cursor const &) const +{ + return docstring(); +} + } // namespace lyx