move the Selection structure from LyXText to BufferView

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@8342 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
André Pönitz 2004-01-14 14:16:11 +00:00
parent bc596c4af7
commit 4eb75f09eb
11 changed files with 132 additions and 135 deletions

View File

@ -478,6 +478,18 @@ CursorSlice const & BufferView::anchor() const
}
Selection & BufferView::selection()
{
return selection_;
}
Selection const & BufferView::selection() const
{
return selection_;
}
void BufferView::x_target(int x)
{
x_target_ = x;

View File

@ -32,10 +32,33 @@ class LyXText;
class LyXScreen;
class LyXView;
class Painter;
class Selection;
class TeXErrors;
class UpdatableInset;
// The structure that keeps track of the selections set.
struct Selection {
Selection()
: set_(false), mark_(false)
{}
bool set() const {
return set_;
}
void set(bool s) {
set_ = s;
}
bool mark() const {
return mark_;
}
void mark(bool m) {
mark_ = m;
}
private:
bool set_; // former selection
bool mark_; // former mark_set
};
/**
* A buffer view encapsulates a view onto a particular
* buffer, and allows access to operate upon it. A view
@ -206,6 +229,13 @@ public:
LyXText * text() const;
///
void resetAnchor();
///
Selection & selection();
///
Selection const & selection() const;
///
Selection selection_;
private:
///

View File

@ -381,7 +381,7 @@ void BufferView::Pimpl::resizeCurrentBuffer()
pos_type pos = 0;
pos_type selstartpos = 0;
pos_type selendpos = 0;
bool selection = false;
bool sel = false;
bool mark_set = false;
owner_->busy(true);
@ -399,18 +399,18 @@ void BufferView::Pimpl::resizeCurrentBuffer()
selstartpos = text->selStart().pos();
selendpar = text->selEnd().par();
selendpos = text->selEnd().pos();
selection = text->selection.set();
mark_set = text->selection.mark();
sel = bv_->selection().set();
mark_set = bv_->selection().mark();
text->textwidth_ = bv_->workWidth();
text->fullRebreak();
update();
if (par != -1) {
text->selection.set(true);
bv_->selection().set(true);
// At this point just to avoid the Delete-Empty-Paragraph-
// Mechanism when setting the cursor.
text->selection.mark(mark_set);
if (selection) {
bv_->selection().mark(mark_set);
if (sel) {
text->setCursor(selstartpar, selstartpos);
bv_->resetAnchor();
text->setCursor(selendpar, selendpos);
@ -419,7 +419,7 @@ void BufferView::Pimpl::resizeCurrentBuffer()
} else {
text->setCursor(par, pos);
bv_->resetAnchor();
text->selection.set(false);
bv_->selection().set(false);
}
}
@ -531,7 +531,7 @@ void BufferView::Pimpl::selectionRequested()
LyXText * text = bv_->getLyXText();
if (!text->selection.set()) {
if (!bv_->selection().set()) {
xsel_cache_.set = false;
return;
}
@ -542,7 +542,7 @@ void BufferView::Pimpl::selectionRequested()
{
xsel_cache_.cursor = bv_->cursor();
xsel_cache_.anchor = bv_->anchor();
xsel_cache_.set = text->selection.set();
xsel_cache_.set = bv_->selection().set();
sel = text->selectionAsString(*bv_->buffer(), false);
if (!sel.empty())
workarea().putClipboard(sel);
@ -634,7 +634,7 @@ Change const BufferView::Pimpl::getCurrentChange()
LyXText * text = bv_->getLyXText();
if (!text->selection.set())
if (!bv_->selection().set())
return Change(Change::UNCHANGED);
return text->getPar(text->selStart())

View File

@ -256,11 +256,10 @@ string const currentState(BufferView * bv)
}
// deletes a selection during an insertion
void replaceSelection(LyXText * text)
{
if (text->selection.set()) {
if (text->bv()->selection().set()) {
text->cutSelection(true, false);
text->bv()->update();
}

View File

@ -235,15 +235,15 @@ void InsetText::updateLocal(BufferView * bv)
if (!autoBreakRows_ && paragraphs().size() > 1)
collapseParagraphs(bv);
if (!text_.selection.set())
text_.anchor() = text_.cursor();
if (!bv->selection().set())
bv->resetAnchor();
bv->owner()->view_state_changed();
bv->owner()->updateMenubar();
bv->owner()->updateToolbar();
if (old_par != text_.cursor().par()) {
if (old_par != bv->cursor().par()) {
bv->owner()->setLayout(text_.cursorPar()->layout()->name());
old_par = text_.cursor().par();
old_par = bv->cursor().par();
}
}

View File

@ -344,7 +344,7 @@ FuncStatus LyXFunc::getStatus(FuncRequest const & ev) const
if (tab && tab->hasSelection())
disable = false;
else
disable = !mathcursor && !view()->getLyXText()->selection.set();
disable = !mathcursor && !view()->selection().set();
break;
case LFUN_RUNCHKTEX:

View File

@ -41,29 +41,6 @@ class UpdatableInset;
class VSpace;
// The structure that keeps track of the selections set.
struct Selection {
Selection()
: set_(false), mark_(false)
{}
bool set() const {
return set_;
}
void set(bool s) {
set_ = s;
}
bool mark() const {
return mark_;
}
void mark(bool m) {
mark_ = m;
}
private:
bool set_; // former selection
bool mark_; // former mark_set
};
/// This class encapsulates the main text data and operations in LyX
class LyXText {
public:
@ -433,33 +410,16 @@ public:
///
void clearSelection();
///
CursorSlice const & selStart() const;
///
CursorSlice const & selEnd() const;
///
CursorSlice & selStart();
///
CursorSlice const & selStart() const;
///
CursorSlice & selEnd();
///
CursorSlice const & selEnd() const;
public:
/** The cursor.
Later this variable has to be removed. There should be no internal
cursor in a text (and thus not in a buffer). By keeping this it is
(I think) impossible to have several views with the same buffer, but
the cursor placed at different places.
[later]
Since the LyXText now has been moved from Buffer to BufferView
it should not be absolutely needed to move the cursor...
[even later]
Nevertheless, it should still be moved, in order to keep classes
and interdependencies small.
*/
// the other end of the selection
CursorSlice anchor_;
//
Selection selection;
///
int height;
///

View File

@ -132,7 +132,7 @@ RowPainter::RowPainter(BufferView const & bv, LyXText const & text,
paintBackground();
// paint the selection background
if (text_.selection.set() && &text_ == bv_.fullCursor().innerText())
if (bv_.selection().set() && &text_ == bv_.fullCursor().innerText())
paintSelection();
// vertical lines for appendix

View File

@ -1114,9 +1114,9 @@ void LyXText::selectWord(word_location loc)
// selection is currently set
bool LyXText::selectWordWhenUnderCursor(word_location loc)
{
if (!selection.set()) {
if (!bv()->selection().set()) {
selectWord(loc);
return selection.set();
return bv()->selection().set();
}
return false;
}
@ -1124,7 +1124,7 @@ bool LyXText::selectWordWhenUnderCursor(word_location loc)
void LyXText::acceptChange()
{
if (!selection.set() && cursorPar()->size())
if (!bv()->selection().set() && cursorPar()->size())
return;
if (selStart().par() == selEnd().par()) {
@ -1143,7 +1143,7 @@ void LyXText::acceptChange()
void LyXText::rejectChange()
{
if (!selection.set() && cursorPar()->size())
if (!bv()->selection().set() && cursorPar()->size())
return;
if (selStart().par() == selEnd().par()) {
@ -1167,7 +1167,7 @@ void LyXText::deleteWordForward()
cursorRight(bv());
else {
CursorSlice tmpcursor = cursor();
selection.set(true); // to avoid deletion
bv()->selection().set(true); // to avoid deletion
cursorRightOneWord();
setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
bv()->resetAnchor();
@ -1185,7 +1185,7 @@ void LyXText::deleteWordBackward()
cursorLeft(bv());
else {
CursorSlice tmpcursor = cursor();
selection.set(true); // to avoid deletion
bv()->selection().set(true); // to avoid deletion
cursorLeftOneWord();
setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
bv()->resetAnchor();
@ -1204,14 +1204,14 @@ void LyXText::deleteLineForward()
cursorRight(bv());
} else {
CursorSlice tmpcursor = cursor();
selection.set(true); // to avoid deletion
bv()->selection().set(true); // to avoid deletion
cursorEnd();
setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
bv()->resetAnchor();
cursor() = tmpcursor;
setSelection();
// What is this test for ??? (JMarc)
if (!selection.set())
if (!bv()->selection().set())
deleteWordForward();
else
cutSelection(true, false);
@ -1224,7 +1224,7 @@ void LyXText::changeCase(LyXText::TextCase action)
CursorSlice from;
CursorSlice to;
if (selection.set()) {
if (bv()->selection().set()) {
from = selStart();
to = selEnd();
} else {
@ -1507,7 +1507,7 @@ void LyXText::previousRow(ParagraphList::iterator & pit,
string LyXText::selectionAsString(Buffer const & buffer, bool label) const
{
if (!selection.set())
if (!bv()->selection().set())
return string();
// should be const ...
@ -1961,7 +1961,7 @@ CursorSlice const & LyXText::anchor() const
CursorSlice const & LyXText::selStart() const
{
if (!selection.set())
if (!bv()->selection().set())
return cursor();
// can't use std::min as this creates a new object
return anchor() < cursor() ? anchor() : cursor();
@ -1970,7 +1970,7 @@ CursorSlice const & LyXText::selStart() const
CursorSlice const & LyXText::selEnd() const
{
if (!selection.set())
if (!bv()->selection().set())
return cursor();
return anchor() > cursor() ? anchor() : cursor();
}
@ -1978,7 +1978,7 @@ CursorSlice const & LyXText::selEnd() const
CursorSlice & LyXText::selStart()
{
if (!selection.set())
if (!bv()->selection().set())
return cursor();
return anchor() < cursor() ? anchor() : cursor();
}
@ -1986,7 +1986,7 @@ CursorSlice & LyXText::selStart()
CursorSlice & LyXText::selEnd()
{
if (!selection.set())
if (!bv()->selection().set())
return cursor();
return anchor() > cursor() ? anchor() : cursor();
}
@ -1994,17 +1994,17 @@ CursorSlice & LyXText::selEnd()
void LyXText::setSelection()
{
selection.set(true);
bv()->selection().set(true);
// a selection with no contents is not a selection
if (cursor().par() == anchor().par() && cursor().pos() == anchor().pos())
selection.set(false);
bv()->selection().set(false);
}
void LyXText::clearSelection()
{
selection.set(false);
selection.mark(false);
bv()->selection().set(false);
bv()->selection().mark(false);
bv()->resetAnchor();
// reset this in the bv()!
if (bv() && bv()->text())

View File

@ -349,7 +349,7 @@ void getSelectionSpan(LyXText & text,
ParagraphList::iterator & beg,
ParagraphList::iterator & end)
{
if (!text.selection.set()) {
if (!text.bv()->selection().set()) {
beg = text.cursorPar();
end = boost::next(beg);
} else {
@ -425,7 +425,7 @@ void LyXText::changeDepth(bv_funcs::DEPTH_CHANGE type)
void LyXText::setFont(LyXFont const & font, bool toggleall)
{
// if there is no selection just set the current_font
if (!selection.set()) {
if (!bv()->selection().set()) {
// Determine basis font
LyXFont layoutfont;
if (cursor().pos() < cursorPar()->beginOfBody())
@ -547,7 +547,7 @@ string LyXText::getStringToIndex()
selectWordWhenUnderCursor(lyx::PREVIOUS_WORD);
string idxstring;
if (!selection.set())
if (!bv()->selection().set())
bv()->owner()->message(_("Nothing to index!"));
else if (selStart().par() != selEnd().par())
bv()->owner()->message(_("Cannot index more than one paragraph!"));
@ -929,7 +929,7 @@ void LyXText::cutSelection(bool doclear, bool realcut)
bv()->stuffClipboard(selectionAsString(*bv()->buffer(), true));
// This doesn't make sense, if there is no selection
if (!selection.set())
if (!bv()->selection().set())
return;
// OK, we have a selection. This is always between selStart()
@ -979,7 +979,7 @@ void LyXText::copySelection()
bv()->stuffClipboard(selectionAsString(*bv()->buffer(), true));
// this doesnt make sense, if there is no selection
if (!selection.set())
if (!bv()->selection().set())
return;
// ok we have a selection. This is always between selStart()
@ -1563,7 +1563,7 @@ void LyXText::fixCursorAfterDelete(CursorSlice & cur, CursorSlice const & where)
bool LyXText::deleteEmptyParagraphMechanism(CursorSlice const & old_cursor)
{
// Would be wrong to delete anything if we have a selection.
if (selection.set())
if (bv()->selection().set())
return false;
// Don't do anything if the cursor is invalid

View File

@ -217,13 +217,10 @@ namespace {
{
LyXText * lt = bv->getLyXText();
// if (!lt->selection.set())
// lt->selection.cursor = lt->cursor();
if (selecting || lt->selection.mark())
if (selecting || bv->selection().mark())
lt->setSelection();
if (!lt->selection.set())
if (!bv->selection().set())
bv->haveSelection(false);
bv->switchKeyMap();
}
@ -414,8 +411,7 @@ void specialChar(LyXText * lt, BufferView * bv, InsetSpecialChar::Kind kind)
}
void doInsertInset(LyXText const & lt, FuncRequest const & cmd,
bool edit, bool pastesel)
void doInsertInset(FuncRequest const & cmd, bool edit, bool pastesel)
{
InsetOld * inset = createInset(cmd);
if (!inset)
@ -424,7 +420,7 @@ void doInsertInset(LyXText const & lt, FuncRequest const & cmd,
BufferView * bv = cmd.view();
bool gotsel = false;
if (lt.selection.set()) {
if (bv->selection().set()) {
bv->owner()->dispatch(FuncRequest(LFUN_CUT));
gotsel = true;
}
@ -508,7 +504,7 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
break;
case LFUN_WORDRIGHT:
if (!selection.mark())
if (!bv->selection().mark())
clearSelection();
if (rtl())
cursorLeftOneWord();
@ -518,7 +514,7 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
break;
case LFUN_WORDLEFT:
if (!selection.mark())
if (!bv->selection().mark())
clearSelection();
if (rtl())
cursorRightOneWord();
@ -528,21 +524,21 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
break;
case LFUN_BEGINNINGBUF:
if (!selection.mark())
if (!bv->selection().mark())
clearSelection();
cursorTop();
finishChange(bv);
break;
case LFUN_ENDBUF:
if (selection.mark())
if (bv->selection().mark())
clearSelection();
cursorBottom();
finishChange(bv);
break;
case LFUN_RIGHTSEL:
if (!selection.set())
if (!bv->selection().set())
bv->resetAnchor();
if (rtl())
cursorLeft(bv);
@ -552,7 +548,7 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
break;
case LFUN_LEFTSEL:
if (!selection.set())
if (!bv->selection().set())
bv->resetAnchor();
if (rtl())
cursorRight(bv);
@ -562,63 +558,63 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
break;
case LFUN_UPSEL:
if (!selection.set())
if (!bv->selection().set())
bv->resetAnchor();
cursorUp(true);
finishChange(bv, true);
break;
case LFUN_DOWNSEL:
if (!selection.set())
if (!bv->selection().set())
bv->resetAnchor();
cursorDown(true);
finishChange(bv, true);
break;
case LFUN_UP_PARAGRAPHSEL:
if (!selection.set())
if (!bv->selection().set())
bv->resetAnchor();
cursorUpParagraph();
finishChange(bv, true);
break;
case LFUN_DOWN_PARAGRAPHSEL:
if (!selection.set())
if (!bv->selection().set())
bv->resetAnchor();
cursorDownParagraph();
finishChange(bv, true);
break;
case LFUN_PRIORSEL:
if (!selection.set())
if (!bv->selection().set())
bv->resetAnchor();
cursorPrevious();
finishChange(bv, true);
break;
case LFUN_NEXTSEL:
if (!selection.set())
if (!bv->selection().set())
bv->resetAnchor();
cursorNext();
finishChange(bv, true);
break;
case LFUN_HOMESEL:
if (!selection.set())
if (!bv->selection().set())
bv->resetAnchor();
cursorHome();
finishChange(bv, true);
break;
case LFUN_ENDSEL:
if (!selection.set())
if (!bv->selection().set())
bv->resetAnchor();
cursorEnd();
finishChange(bv, true);
break;
case LFUN_WORDRIGHTSEL:
if (!selection.set())
if (!bv->selection().set())
bv->resetAnchor();
if (rtl())
cursorLeftOneWord();
@ -628,7 +624,7 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
break;
case LFUN_WORDLEFTSEL:
if (!selection.set())
if (!bv->selection().set())
bv->resetAnchor();
if (rtl())
cursorRightOneWord();
@ -660,21 +656,21 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
return moveDown();
case LFUN_UP_PARAGRAPH:
if (!selection.mark())
if (!bv->selection().mark())
clearSelection();
cursorUpParagraph();
finishChange(bv);
break;
case LFUN_DOWN_PARAGRAPH:
if (!selection.mark())
if (!bv->selection().mark())
clearSelection();
cursorDownParagraph();
finishChange(bv, false);
break;
case LFUN_PRIOR:
if (!selection.mark())
if (!bv->selection().mark())
clearSelection();
finishChange(bv, false);
if (cursorPar() == firstPar() && cursorRow() == firstRow())
@ -683,7 +679,7 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
break;
case LFUN_NEXT:
if (!selection.mark())
if (!bv->selection().mark())
clearSelection();
finishChange(bv, false);
if (cursorPar() == lastPar() && cursorRow() == lastRow())
@ -692,14 +688,14 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
break;
case LFUN_HOME:
if (!selection.mark())
if (!bv->selection().mark())
clearSelection();
cursorHome();
finishChange(bv, false);
break;
case LFUN_END:
if (!selection.mark())
if (!bv->selection().mark())
clearSelection();
cursorEnd();
finishChange(bv, false);
@ -719,7 +715,7 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
}
case LFUN_DELETE:
if (!selection.set()) {
if (!bv->selection().set()) {
Delete();
bv->resetAnchor();
// It is possible to make it a lot faster still
@ -733,7 +729,7 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
case LFUN_DELETE_SKIP:
// Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
if (!selection.set()) {
if (!bv->selection().set()) {
if (cursor().pos() == cursorPar()->size()) {
cursorRight(bv);
cursorLeft(bv);
@ -751,7 +747,7 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
case LFUN_BACKSPACE:
if (!selection.set()) {
if (!bv->selection().set()) {
if (bv->owner()->getIntl().getTransManager().backspace()) {
backspace();
bv->resetAnchor();
@ -768,7 +764,7 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
case LFUN_BACKSPACE_SKIP:
// Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
if (!selection.set()) {
if (!bv->selection().set()) {
CursorSlice cur = cursor();
backspace();
anchor() = cur;
@ -896,7 +892,7 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
if (cursorPar()->layout()->free_spacing)
insertChar(' ');
else
doInsertInset(*this, cmd, false, false);
doInsertInset(cmd, false, false);
moveCursor(bv, false);
break;
@ -929,7 +925,7 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
case LFUN_MARK_ON:
clearSelection();
selection.mark(true);
bv->selection().mark(true);
bv->update();
bv->resetAnchor();
cmd.message(N_("Mark on"));
@ -937,10 +933,10 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
case LFUN_SETMARK:
clearSelection();
if (selection.mark()) {
if (bv->selection().mark()) {
cmd.message(N_("Mark removed"));
} else {
selection.mark(true);
bv->selection().mark(true);
cmd.message(N_("Mark set"));
}
bv->resetAnchor();
@ -996,7 +992,7 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
case LFUN_BEGINNINGBUFSEL:
if (in_inset_)
return DispatchResult(false);
if (!selection.set())
if (!bv->selection().set())
bv->resetAnchor();
cursorTop();
finishChange(bv, true);
@ -1005,7 +1001,7 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
case LFUN_ENDBUFSEL:
if (in_inset_)
return DispatchResult(false);
if (!selection.set())
if (!bv->selection().set())
bv->resetAnchor();
cursorBottom();
finishChange(bv, true);
@ -1076,7 +1072,7 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
bool change_layout = (current_layout != layout);
if (!change_layout && selection.set() &&
if (!change_layout && bv->selection().set() &&
selStart().par() != selEnd().par())
{
ParagraphList::iterator spit = getPar(selStart());
@ -1184,7 +1180,7 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
bv->resetAnchor();
cursorEnd();
setSelection();
bv->haveSelection(selection.set());
bv->haveSelection(bv->selection().set());
}
break;
@ -1194,7 +1190,7 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
if (cmd.button() == mouse_button::button1) {
selection_possible = true;
selectWord(lyx::WHOLE_WORD_STRICT);
bv->haveSelection(selection.set());
bv->haveSelection(bv->selection().set());
}
break;
@ -1252,7 +1248,7 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
// it could get cleared on the unlocking of the inset so
// we have to check this first
bool paste_internally = false;
if (cmd.button() == mouse_button::button2 && selection.set()) {
if (cmd.button() == mouse_button::button2 && bv->selection().set()) {
bv->owner()->dispatch(FuncRequest(LFUN_COPY));
paste_internally = true;
}
@ -1305,7 +1301,7 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
// finish selection
if (cmd.button() == mouse_button::button1)
bv->haveSelection(selection.set());
bv->haveSelection(bv->selection().set());
bv->switchKeyMap();
bv->owner()->view_state_changed();
@ -1325,7 +1321,7 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
// true (on).
if (lyxrc.auto_region_delete) {
if (selection.set())
if (bv->selection().set())
cutSelection(false, false);
bv->haveSelection(false);
}
@ -1393,12 +1389,12 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
case LFUN_ENVIRONMENT_INSERT:
// Open the inset, and move the current selection
// inside it.
doInsertInset(*this, cmd, true, true);
doInsertInset(cmd, true, true);
break;
case LFUN_INDEX_INSERT:
// Just open the inset
doInsertInset(*this, cmd, true, false);
doInsertInset(cmd, true, false);
break;
case LFUN_INDEX_PRINT:
@ -1407,7 +1403,7 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
case LFUN_INSERT_LINE:
case LFUN_INSERT_PAGEBREAK:
// do nothing fancy
doInsertInset(*this, cmd, false, false);
doInsertInset(cmd, false, false);
break;
case LFUN_DEPTH_MIN: