]> git.lyx.org Git - lyx.git/blobdiff - src/Text2.cpp
adjust
[lyx.git] / src / Text2.cpp
index 375cb15ff45a552889b7525e0d48d894af6d73a6..bd7effb23f36ced85b42935a167e2c8170271782 100644 (file)
 #include "BufferList.h"
 #include "BufferParams.h"
 #include "BufferView.h"
-#include "bufferview_funcs.h"
 #include "Bullet.h"
 #include "Color.h"
-#include "CoordCache.h"
 #include "Cursor.h"
 #include "CutAndPaste.h"
 #include "debug.h"
 #include "FuncRequest.h"
 #include "gettext.h"
 #include "Language.h"
+#include "Layout.h"
 #include "Lexer.h"
 #include "LyXFunc.h"
 #include "LyXRC.h"
-#include "Row.h"
 #include "Paragraph.h"
-#include "TextMetrics.h"
 #include "paragraph_funcs.h"
 #include "ParagraphParameters.h"
 #include "ParIterator.h"
 #include "Server.h"
 #include "ServerSocket.h"
+#include "TextMetrics.h"
 #include "Undo.h"
 #include "VSpace.h"
 
 #include "support/textutils.h"
 
 #include <boost/current_function.hpp>
+#include <boost/next_prior.hpp>
 
 #include <sstream>
 
-
-namespace lyx {
-
 using std::endl;
 using std::ostringstream;
 using std::string;
@@ -75,10 +71,10 @@ using std::max;
 using std::min;
 using std::istringstream;
 
+namespace lyx {
+
 Text::Text()
-       : current_font(Font::ALL_INHERIT),
-         background_color_(Color::background),
-         autoBreakRows_(false)
+       : autoBreakRows_(false)
 {}
 
 
@@ -88,92 +84,6 @@ bool Text::isMainText(Buffer const & buffer) const
 }
 
 
-// Gets the fully instantiated font at a given position in a paragraph
-// Basically the same routine as Paragraph::getFont() in Paragraph.cpp.
-// The difference is that this one is used for displaying, and thus we
-// are allowed to make cosmetic improvements. For instance make footnotes
-// smaller. (Asger)
-Font Text::getFont(Buffer const & buffer, Paragraph const & par,
-               pos_type const pos) const
-{
-       BOOST_ASSERT(pos >= 0);
-
-       LayoutPtr const & layout = par.layout();
-       // FIXME: broken?
-       BufferParams const & params = buffer.params();
-       pos_type const body_pos = par.beginOfBody();
-
-       // We specialize the 95% common case:
-       if (!par.getDepth()) {
-               Font f = par.getFontSettings(params, pos);
-               if (!isMainText(buffer))
-                       applyOuterFont(buffer, f);
-               Font lf;
-               Font rlf;
-               if (layout->labeltype == LABEL_MANUAL && pos < body_pos) {
-                       lf = layout->labelfont;
-                       rlf = layout->reslabelfont;
-               } else {
-                       lf = layout->font;
-                       rlf = layout->resfont;
-               }
-               // In case the default family has been customized
-               if (lf.family() == Font::INHERIT_FAMILY)
-                       rlf.setFamily(params.getFont().family());
-               return f.realize(rlf);
-       }
-
-       // The uncommon case need not be optimized as much
-       Font layoutfont;
-       if (pos < body_pos)
-               layoutfont = layout->labelfont;
-       else
-               layoutfont = layout->font;
-
-       Font font = par.getFontSettings(params, pos);
-       font.realize(layoutfont);
-
-       if (!isMainText(buffer))
-               applyOuterFont(buffer, font);
-
-       // Find the pit value belonging to paragraph. This will not break
-       // even if pars_ would not be a vector anymore.
-       // Performance appears acceptable.
-
-       pit_type pit = pars_.size();
-       for (pit_type it = 0; it < pit; ++it)
-               if (&pars_[it] == &par) {
-                       pit = it;
-                       break;
-               }
-       // Realize against environment font information
-       // NOTE: the cast to pit_type should be removed when pit_type
-       // changes to a unsigned integer.
-       if (pit < pit_type(pars_.size()))
-               font.realize(outerFont(pit, pars_));
-
-       // Realize with the fonts of lesser depth.
-       font.realize(params.getFont());
-
-       return font;
-}
-
-// There are currently two font mechanisms in LyX:
-// 1. The font attributes in a lyxtext, and
-// 2. The inset-specific font properties, defined in an inset's
-// metrics() and draw() methods and handed down the inset chain through
-// the pi/mi parameters, and stored locally in a lyxtext in font_.
-// This is where the two are integrated in the final fully realized
-// font.
-void Text::applyOuterFont(Buffer const & buffer, Font & font) const {
-       Font lf(font_);
-       lf.reduce(buffer.params().getFont());
-       lf.realize(font);
-       lf.setLanguage(font.language());
-       font = lf;
-}
-
-
 Font Text::getLayoutFont(Buffer const & buffer, pit_type const pit) const
 {
        LayoutPtr const & layout = pars_[pit].layout();
@@ -216,7 +126,7 @@ Font Text::getLabelFont(Buffer const & buffer, Paragraph const & par) const
 
 
 void Text::setCharFont(Buffer const & buffer, pit_type pit,
-               pos_type pos, Font const & fnt)
+               pos_type pos, Font const & fnt, Font const & display_font)
 {
        Font font = fnt;
        LayoutPtr const & layout = pars_[pit].layout();
@@ -244,7 +154,7 @@ void Text::setCharFont(Buffer const & buffer, pit_type pit,
        // Inside inset, apply the inset's font attributes if any
        // (charstyle!)
        if (!isMainText(buffer))
-               layoutfont.realize(font_);
+               layoutfont.realize(display_font);
 
        layoutfont.realize(buffer.params().getFont());
 
@@ -255,7 +165,7 @@ void Text::setCharFont(Buffer const & buffer, pit_type pit,
 }
 
 
-void Text::setInsetFont(Buffer const & buffer, pit_type pit,
+void Text::setInsetFont(BufferView const & bv, pit_type pit,
                pos_type pos, Font const & font, bool toggleall)
 {
        BOOST_ASSERT(pars_[pit].isInset(pos) &&
@@ -270,7 +180,7 @@ void Text::setInsetFont(Buffer const & buffer, pit_type pit,
                        CursorSlice cellend = cs;
                        cellend.pit() = cellend.lastpit();
                        cellend.pos() = cellend.lastpos();
-                       text->setFont(buffer, cs, cellend, font, toggleall);
+                       text->setFont(bv, cs, cellend, font, toggleall);
                }
        }
 }
@@ -415,15 +325,15 @@ void Text::setFont(Cursor & cur, Font const & font, bool toggleall)
                layoutfont = getLayoutFont(cur.buffer(), pit);
 
        // Update current font
-       real_current_font.update(font,
+       cur.real_current_font.update(font,
                                        cur.buffer().params().language,
                                        toggleall);
 
        // Reduce to implicit settings
-       current_font = real_current_font;
-       current_font.reduce(layoutfont);
+       cur.current_font = cur.real_current_font;
+       cur.current_font.reduce(layoutfont);
        // And resolve it completely
-       real_current_font.realize(layoutfont);
+       cur.real_current_font.realize(layoutfont);
 
        // if there is no selection that's all we need to do
        if (!cur.selection())
@@ -432,15 +342,17 @@ void Text::setFont(Cursor & cur, Font const & font, bool toggleall)
        // Ok, we have a selection.
        recordUndoSelection(cur);
 
-       setFont(cur.buffer(), cur.selectionBegin().top(), 
+       setFont(cur.bv(), cur.selectionBegin().top(), 
                cur.selectionEnd().top(), font, toggleall);
 }
 
 
-void Text::setFont(Buffer const & buffer, CursorSlice const & begin,
+void Text::setFont(BufferView const & bv, CursorSlice const & begin,
                CursorSlice const & end, Font const & font,
                bool toggleall)
 {
+       Buffer const & buffer = bv.buffer();
+
        // Don't use forwardChar here as ditend might have
        // pos() == lastpos() and forwardChar would miss it.
        // Can't use forwardPos either as this descends into
@@ -456,49 +368,16 @@ void Text::setFont(Buffer const & buffer, CursorSlice const & begin,
                                // text cells of the inset (bug 1973).
                                // FIXME: This should change, see documentation
                                // of noFontChange in Inset.h
-                               setInsetFont(buffer, pit, pos, font, toggleall);
-                       Font f = getFont(buffer, dit.paragraph(), pos);
+                               setInsetFont(bv, pit, pos, font, toggleall);
+                       TextMetrics const & tm = bv.textMetrics(this);
+                       Font f = tm.getDisplayFont(pit, pos);
                        f.update(font, language, toggleall);
-                       setCharFont(buffer, pit, pos, f);
+                       setCharFont(buffer, pit, pos, f, tm.font_);
                }
        }
 }
 
 
-// the cursor set functions have a special mechanism. When they
-// realize you left an empty paragraph, they will delete it.
-
-bool Text::cursorHome(Cursor & cur)
-{
-       BOOST_ASSERT(this == cur.text());
-       ParagraphMetrics const & pm = cur.bv().parMetrics(this, cur.pit());
-       Row const & row = pm.getRow(cur.pos(),cur.boundary());
-       return setCursor(cur, cur.pit(), row.pos());
-}
-
-
-bool Text::cursorEnd(Cursor & cur)
-{
-       BOOST_ASSERT(this == cur.text());
-       // if not on the last row of the par, put the cursor before
-       // the final space exept if I have a spanning inset or one string
-       // is so long that we force a break.
-       pos_type end = cur.textRow().endpos();
-       if (end == 0)
-               // empty text, end-1 is no valid position
-               return false;
-       bool boundary = false;
-       if (end != cur.lastpos()) {
-               if (!cur.paragraph().isLineSeparator(end-1)
-                   && !cur.paragraph().isNewline(end-1))
-                       boundary = true;
-               else
-                       --end;
-       }
-       return setCursor(cur, cur.pit(), end, true, boundary);
-}
-
-
 bool Text::cursorTop(Cursor & cur)
 {
        BOOST_ASSERT(this == cur.text());
@@ -613,7 +492,7 @@ void Text::insertInset(Cursor & cur, Inset * inset)
 {
        BOOST_ASSERT(this == cur.text());
        BOOST_ASSERT(inset);
-       cur.paragraph().insertInset(cur.pos(), inset, current_font,
+       cur.paragraph().insertInset(cur.pos(), inset, cur.current_font,
                                    Change(cur.buffer().params().trackChanges ?
                                           Change::INSERTED : Change::UNCHANGED));
 }
@@ -623,7 +502,7 @@ void Text::insertInset(Cursor & cur, Inset * inset)
 void Text::insertStringAsLines(Cursor & cur, docstring const & str)
 {
        cur.buffer().insertStringAsLines(pars_, cur.pit(), cur.pos(),
-                                        current_font, str, autoBreakRows_);
+                                        cur.current_font, str, autoBreakRows_);
 }
 
 
@@ -694,50 +573,7 @@ void Text::setCursorIntern(Cursor & cur,
        cur.boundary(boundary);
        setCursor(cur.top(), par, pos);
        if (setfont)
-               setCurrentFont(cur);
-}
-
-
-void Text::setCurrentFont(Cursor & cur)
-{
-       BOOST_ASSERT(this == cur.text());
-       pos_type pos = cur.pos();
-       Paragraph & par = cur.paragraph();
-
-       // are we behind previous char in fact? -> go to that char
-       if (pos > 0 && cur.boundary())
-               --pos;
-
-       // find position to take the font from
-       if (pos != 0) {
-               // paragraph end? -> font of last char
-               if (pos == cur.lastpos())
-                       --pos;
-               // on space? -> look at the words in front of space
-               else if (pos > 0 && par.isSeparator(pos))       {
-                       // abc| def -> font of c
-                       // abc |[WERBEH], i.e. boundary==true -> font of c
-                       // abc [WERBEH]| def, font of the space
-                       if (!isRTLBoundary(cur.buffer(), par, pos))
-                               --pos;
-               }
-       }
-
-       // get font
-       BufferParams const & bufparams = cur.buffer().params();
-       current_font = par.getFontSettings(bufparams, pos);
-       real_current_font = getFont(cur.buffer(), par, pos);
-
-       // special case for paragraph end
-       if (cur.pos() == cur.lastpos()
-           && isRTLBoundary(cur.buffer(), par, cur.pos())
-           && !cur.boundary()) {
-               Language const * lang = par.getParLanguage(bufparams);
-               current_font.setLanguage(lang);
-               current_font.setNumber(Font::OFF);
-               real_current_font.setLanguage(lang);
-               real_current_font.setNumber(Font::OFF);
-       }
+               cur.setCurrentFont();
 }
 
 
@@ -750,7 +586,7 @@ bool Text::checkAndActivateInset(Cursor & cur, bool front)
        if (!front && cur.pos() == 0)
                return false;
        Inset * inset = front ? cur.nextInset() : cur.prevInset();
-       if (!isHighlyEditableInset(inset))
+       if (!inset || inset->editable() != Inset::HIGHLY_EDITABLE)
                return false;
        /*
         * Apparently, when entering an inset we are expected to be positioned
@@ -803,7 +639,7 @@ bool Text::cursorLeft(Cursor & cur)
 
        // move to the previous paragraph or do nothing
        if (cur.pit() > 0)
-               return setCursor(cur, cur.pit() - 1, getPar(cur.pit() - 1).size());
+               return setCursor(cur, cur.pit() - 1, getPar(cur.pit() - 1).size(), true, false);
        return false;
 }
 
@@ -819,10 +655,10 @@ bool Text::cursorRight(Cursor & cur)
                if (checkAndActivateInset(cur, true))
                        return false;
 
+               TextMetrics const & tm = cur.bv().textMetrics(this);
                // if left of boundary -> just jump to right side
-         // but for RTL boundaries don't, because: abc|DDEEFFghi -> abcDDEEF|Fghi
-         if (cur.boundary() && 
-                               !isRTLBoundary(cur.buffer(), cur.paragraph(), cur.pos()))
+               // but for RTL boundaries don't, because: abc|DDEEFFghi -> abcDDEEF|Fghi
+               if (cur.boundary() && !tm.isRTLBoundary(cur.pit(), cur.pos()))
                        return setCursor(cur, cur.pit(), cur.pos(), true, false);
 
                // next position is left of boundary, 
@@ -851,7 +687,7 @@ bool Text::cursorRight(Cursor & cur)
                
                // in front of RTL boundary? Stay on this side of the boundary because:
                //   ab|cDDEEFFghi -> abc|DDEEFFghi
-               if (isRTLBoundary(cur.buffer(), cur.paragraph(), cur.pos() + 1))
+               if (tm.isRTLBoundary(cur.pit(), cur.pos() + 1))
                        return setCursor(cur, cur.pit(), cur.pos() + 1, true, true);
                
                // move right
@@ -860,7 +696,7 @@ bool Text::cursorRight(Cursor & cur)
 
        // move to next paragraph
        if (cur.pit() != cur.lastpit())
-               return setCursor(cur, cur.pit() + 1, 0);
+               return setCursor(cur, cur.pit() + 1, 0, true, false);
        return false;
 }
 
@@ -980,7 +816,11 @@ bool Text::deleteEmptyParagraphMechanism(Cursor & cur,
                           max(old.pit() - 1, pit_type(0)),
                           min(old.pit() + 1, old.lastpit()));
                ParagraphList & plist = old.text()->paragraphs();
+               bool const soa = oldpar.params().startOfAppendix();
                plist.erase(boost::next(plist.begin(), old.pit()));
+               // do not lose start of appendix marker (bug 4212)
+               if (soa)
+                       plist[old.pit()].params().startOfAppendix(true);
 
                // see #warning (FIXME?) above 
                if (cur.depth() >= old.depth()) {