From 2a6106ff5fe34fbd412b4414eebdaf277fcc7d91 Mon Sep 17 00:00:00 2001 From: Alfredo Braunstein Date: Wed, 26 Nov 2003 09:48:21 +0000 Subject: [PATCH] updateCounters fix + changeDepth simplification git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@8138 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/ChangeLog | 9 +++ src/bufferview_funcs.C | 15 +++-- src/bufferview_funcs.h | 11 ++-- src/lyxfunc.C | 4 +- src/lyxtext.h | 11 ++-- src/text2.C | 143 +++++++++++++++++++++++------------------ src/text3.C | 6 +- src/textcursor.C | 9 +-- 8 files changed, 114 insertions(+), 94 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index 100fb7b37a..ef775a8c4c 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,12 @@ +2003-11-26 Alfredo Braunstein + + * bufferview_funcs.[Ch]: split changeDepthAllowed from changeDepth + * lyxtext.h: ditto + * text2.C: same thing + updateCounters fix + redoCursor also adjusts + selection cursors + * lyxfunc.C: adjust + * text3.C: adjust + re-allow multi par depth changes + * textcursor.C: simplify a bit 2003-11-25 Martin Vermeer diff --git a/src/bufferview_funcs.C b/src/bufferview_funcs.C index 3f67f87d0e..c80dff5927 100644 --- a/src/bufferview_funcs.C +++ b/src/bufferview_funcs.C @@ -149,17 +149,20 @@ bool string2font(string const & data, LyXFont & font, bool & toggle) } -bool changeDepth(BufferView * bv, LyXText * text, DEPTH_CHANGE type, bool test_only) +bool changeDepthAllowed(BufferView * bv, LyXText * text, DEPTH_CHANGE type) { if (!bv->available() || !text) return false; - if (test_only) - return text->changeDepth(type, true); + return text->changeDepthAllowed(type); +} - bool const changed = text->changeDepth(type, false); - bv->update(); - return changed; + +void changeDepth(BufferView * bv, LyXText * text, DEPTH_CHANGE type) +{ + if (!bv->available() || !text) + return; + text->changeDepth(type); } diff --git a/src/bufferview_funcs.h b/src/bufferview_funcs.h index 6504f4fc90..5ab134fd42 100644 --- a/src/bufferview_funcs.h +++ b/src/bufferview_funcs.h @@ -48,12 +48,11 @@ enum DEPTH_CHANGE { DEC_DEPTH }; -/** - * Increase or decrease the nesting depth of the selected paragraph(s) - * if test_only, don't change any depths. Returns whether something - * (would have) changed - */ -bool changeDepth(BufferView *, LyXText *, DEPTH_CHANGE, bool test_only); +/// Increase or decrease the nesting depth of the selected paragraph(s) +void changeDepth(BufferView *, LyXText *, DEPTH_CHANGE); + +/// Returns whether something would be changed by changeDepth +bool changeDepthAllowed(BufferView *, LyXText *, DEPTH_CHANGE); /// Returns the current font and depth as a message. std::string const currentState(BufferView *); diff --git a/src/lyxfunc.C b/src/lyxfunc.C index 9ab4ff5677..11d366b0ff 100644 --- a/src/lyxfunc.C +++ b/src/lyxfunc.C @@ -349,11 +349,11 @@ FuncStatus LyXFunc::getStatus(FuncRequest const & ev) const break; case LFUN_DEPTH_MIN: - disable = !changeDepth(view(), view()->getLyXText(), DEC_DEPTH, true); + disable = !changeDepthAllowed(view(), view()->getLyXText(), DEC_DEPTH); break; case LFUN_DEPTH_PLUS: - disable = !changeDepth(view(), view()->getLyXText(), INC_DEPTH, true); + disable = !changeDepthAllowed(view(), view()->getLyXText(), INC_DEPTH); break; case LFUN_LAYOUT: diff --git a/src/lyxtext.h b/src/lyxtext.h index ba8329128f..e2bd921582 100644 --- a/src/lyxtext.h +++ b/src/lyxtext.h @@ -87,12 +87,11 @@ public: /// void setLayout(std::string const & layout); - /** - * Increase or decrease the nesting depth of the selected paragraph(s) - * if test_only, don't change any depths. Returns whether something - * (would have) changed - */ - bool changeDepth(bv_funcs::DEPTH_CHANGE type, bool test_only); + /// Increase or decrease the nesting depth of the selected paragraph(s) + void changeDepth(bv_funcs::DEPTH_CHANGE type); + + /// Returns whether something would be changed by changeDepth + bool changeDepthAllowed(bv_funcs::DEPTH_CHANGE type); /// get the depth at current cursor position int getDepth() const; diff --git a/src/text2.C b/src/text2.C index 73d96266ee..c50e499cc7 100644 --- a/src/text2.C +++ b/src/text2.C @@ -393,79 +393,83 @@ void LyXText::setLayout(string const & layout) } -bool LyXText::changeDepth(bv_funcs::DEPTH_CHANGE type, bool test_only) -{ - ParagraphList::iterator pit = cursorPar(); - ParagraphList::iterator end = pit; - ParagraphList::iterator start = pit; +namespace { - if (selection.set()) { - pit = getPar(selection.start); - end = getPar(selection.end); - start = pit; + +void getSelectionSpan(LyXText & text, + ParagraphList::iterator & beg, + ParagraphList::iterator & end) +{ + if (!text.selection.set()) { + beg = text.cursorPar(); + end = boost::next(beg); + } else { + beg = text.getPar(text.selection.start); + end = boost::next(text.getPar(text.selection.end)); } +} - if (!test_only) - recUndo(parOffset(start), parOffset(end)); - bool changed = false; +bool changeDepthAllowed(bv_funcs::DEPTH_CHANGE type, + Paragraph const & par, + int max_depth) +{ + if (par.layout()->labeltype == LABEL_BIBLIO) + return false; + int const depth = par.params().depth(); + if (type == bv_funcs::INC_DEPTH && depth < max_depth) + return true; + if (type == bv_funcs::DEC_DEPTH && depth > 0) + return true; + return false; +} - int prev_after_depth = 0; -#warning parlist ... could be nicer ? - if (start != ownerParagraphs().begin()) { - prev_after_depth = boost::prior(start)->getMaxDepthAfter(); - } - while (true) { - int const depth = pit->params().depth(); - if (type == bv_funcs::INC_DEPTH) { - if (depth < prev_after_depth - && pit->layout()->labeltype != LABEL_BIBLIO) { - changed = true; - if (!test_only) - pit->params().depth(depth + 1); - } - } else if (depth) { - changed = true; - if (!test_only) - pit->params().depth(depth - 1); - } +} - prev_after_depth = pit->getMaxDepthAfter(); -#warning SERIOUS: Uahh... does this mean we access end->getMaxDepthAfter? - if (pit == end) { - break; - } +bool LyXText::changeDepthAllowed(bv_funcs::DEPTH_CHANGE type) +{ + ParagraphList::iterator beg, end; + getSelectionSpan(*this, beg, end); + int max_depth = 0; + if (beg != ownerParagraphs().begin()) + max_depth = boost::prior(beg)->getMaxDepthAfter(); - ++pit; + for (ParagraphList::iterator pit = beg; pit != end; ++pit) { + if (::changeDepthAllowed(type, *pit, max_depth)) + return true; + max_depth = pit->getMaxDepthAfter(); } + return false; +} - if (test_only) - return changed; - redoParagraphs(start, boost::next(end)); +void LyXText::changeDepth(bv_funcs::DEPTH_CHANGE type) +{ + ParagraphList::iterator beg, end; + getSelectionSpan(*this, beg, end); + + recUndo(parOffset(beg), parOffset(end) - 1); - // We need to actually move the text->cursor. I don't - // understand why ... - LyXCursor tmpcursor = cursor; + int max_depth = 0; + if (beg != ownerParagraphs().begin()) + max_depth = boost::prior(beg)->getMaxDepthAfter(); - // we have to reset the visual selection because the - // geometry could have changed - if (selection.set()) { - setCursor(selection.start.par(), selection.start.pos()); - selection.cursor = cursor; - setCursor(selection.end.par(), selection.end.pos()); + for (ParagraphList::iterator pit = beg; pit != end; ++pit) { + if (::changeDepthAllowed(type, *pit, max_depth)) { + int const depth = pit->params().depth(); + if (type == bv_funcs::INC_DEPTH) + pit->params().depth(depth + 1); + else + pit->params().depth(depth - 1); + } + max_depth = pit->getMaxDepthAfter(); } - // this handles the counter labels, and also fixes up // depth values for follow-on (child) paragraphs updateCounters(); - - setSelection(); - setCursor(tmpcursor.par(), tmpcursor.pos()); - - return changed; + redoCursor(); } @@ -654,8 +658,7 @@ string LyXText::getStringToIndex() // they do not duplicate themself and you cannot play dirty tricks with // them! -void LyXText::setParagraph( - VSpace const & space_top, +void LyXText::setParagraph(VSpace const & space_top, VSpace const & space_bottom, Spacing const & spacing, LyXAlignment align, @@ -720,7 +723,6 @@ void LyXText::setParagraph( setCursor(selection.end.par(), selection.end.pos()); setSelection(); setCursor(tmpcursor.par(), tmpcursor.pos()); - bv()->update(); } @@ -980,17 +982,18 @@ void LyXText::setCounter(Buffer const & buf, ParagraphList::iterator pit) } -// Updates all counters. Paragraphs with changed label string will be -// not be rebroken as this is too expensive. The next round will get it -// right anyway... +// Updates all counters. void LyXText::updateCounters() { // start over bv()->buffer()->params().getLyXTextClass().counters().reset(); + bool update_pos = false; + ParagraphList::iterator beg = ownerParagraphs().begin(); ParagraphList::iterator end = ownerParagraphs().end(); for (ParagraphList::iterator pit = beg; pit != end; ++pit) { + string const oldLabel = pit->params().labelString(); size_t maxdepth = 0; if (pit != beg) maxdepth = boost::prior(pit)->getMaxDepthAfter(); @@ -1000,7 +1003,15 @@ void LyXText::updateCounters() // setCounter can potentially change the labelString. setCounter(*bv()->buffer(), pit); + string const & newLabel = pit->params().labelString(); + if (oldLabel != newLabel) { + redoParagraphInternal(pit); + update_pos = true; + } + } + if (update_pos) + updateParPositions(); } @@ -1268,8 +1279,16 @@ bool LyXText::setCursor(paroffset_type par, pos_type pos, bool setfont, bool bou void LyXText::redoCursor() { -#warning maybe the same for selections? setCursor(cursor, cursor.par(), cursor.pos(), cursor.boundary()); + + if (!selection.set()) + return; + + LyXCursor tmpcursor = cursor; + setCursor(selection.cursor.par(), selection.cursor.pos()); + selection.cursor = cursor; + setCursor(tmpcursor.par(), tmpcursor.pos()); + setSelection(); } diff --git a/src/text3.C b/src/text3.C index 43e3eba453..3629ac427b 100644 --- a/src/text3.C +++ b/src/text3.C @@ -1436,14 +1436,12 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd) break; case LFUN_DEPTH_MIN: - clearSelection(); - bv_funcs::changeDepth(bv, this, bv_funcs::DEC_DEPTH, false); + bv_funcs::changeDepth(bv, this, bv_funcs::DEC_DEPTH); bv->update(); break; case LFUN_DEPTH_PLUS: - clearSelection(); - bv_funcs::changeDepth(bv, this, bv_funcs::INC_DEPTH, false); + bv_funcs::changeDepth(bv, this, bv_funcs::INC_DEPTH); bv->update(); break; diff --git a/src/textcursor.C b/src/textcursor.C index 875ea7170b..c9e26ca2af 100644 --- a/src/textcursor.C +++ b/src/textcursor.C @@ -22,14 +22,8 @@ using std::string; void TextCursor::setSelection() { - if (!selection.set()) { - selection.start = selection.cursor; - selection.end = selection.cursor; - } - selection.set(true); - // and now the whole selection if (selection.cursor.par() == cursor.par()) if (selection.cursor.pos() < cursor.pos()) { selection.end = cursor; @@ -43,8 +37,7 @@ void TextCursor::setSelection() && selection.cursor.pos() < cursor.pos())) { selection.end = cursor; selection.start = selection.cursor; - } - else { + } else { selection.end = selection.cursor; selection.start = cursor; } -- 2.39.2