]> git.lyx.org Git - lyx.git/blob - src/cursor_slice.C
fix bug 2089: Touching Navigate menu crashes Lyx when a TOC inset is in a section...
[lyx.git] / src / cursor_slice.C
1 /**
2  * \file cursor_slice.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Matthias Ettrich
8  * \author André Pönitz
9  * \author Jürgen Vigna
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #include <config.h>
15
16 #include "cursor_slice.h"
17 #include "debug.h"
18 #include "lyxtext.h"
19 #include "paragraph.h"
20
21 #include "mathed/math_inset.h"
22 #include "mathed/math_data.h"
23
24 #include <boost/assert.hpp>
25
26 using std::endl;
27
28
29 CursorSlice::CursorSlice()
30         : inset_(0), idx_(0), pit_(0), pos_(0)
31 {}
32
33
34 CursorSlice::CursorSlice(InsetBase & p)
35         : inset_(&p), idx_(0), pit_(0), pos_(0)
36 {
37         BOOST_ASSERT(inset_);
38 }
39
40
41 MathArray & CursorSlice::cell() const
42 {
43         return inset_->asMathInset()->cell(idx_);
44 }
45
46
47 Paragraph & CursorSlice::paragraph()
48 {
49         return text()->getPar(pit_);
50 }
51
52
53 Paragraph const & CursorSlice::paragraph() const
54 {
55         return text()->getPar(pit_);
56 }
57
58
59 CursorSlice::pos_type CursorSlice::lastpos() const
60 {
61         BOOST_ASSERT(inset_);
62         return inset_->asMathInset() ? cell().size() : paragraph().size();
63 }
64
65
66 CursorSlice::row_type CursorSlice::row() const
67 {
68         BOOST_ASSERT(asMathInset());
69         return asMathInset()->row(idx_);
70 }
71
72
73 CursorSlice::col_type CursorSlice::col() const
74 {
75         BOOST_ASSERT(asMathInset());
76         return asMathInset()->col(idx_);
77 }
78
79
80 bool operator==(CursorSlice const & p, CursorSlice const & q)
81 {
82         return &p.inset() == &q.inset()
83                && p.idx() == q.idx()
84                && p.pit() == q.pit()
85                && p.pos() == q.pos();
86 }
87
88
89 bool operator!=(CursorSlice const & p, CursorSlice const & q)
90 {
91         return &p.inset() != &q.inset()
92                || p.idx() != q.idx()
93                || p.pit() != q.pit()
94                || p.pos() != q.pos();
95 }
96
97
98 bool operator<(CursorSlice const & p, CursorSlice const & q)
99 {
100         if (&p.inset() != &q.inset()) {
101                 lyxerr << "can't compare cursor and anchor in different insets\n"
102                        << "p: " << p << '\n' << "q: " << q << endl;
103                 BOOST_ASSERT(false);
104         }
105         if (p.idx() != q.idx())
106                 return p.idx() < q.idx();
107         if (p.pit() != q.pit())
108                 return p.pit() < q.pit();
109         return p.pos() < q.pos();
110 }
111
112
113 bool operator>(CursorSlice const & p, CursorSlice const & q)
114 {
115         return q < p;
116 }
117
118
119 bool operator<=(CursorSlice const & p, CursorSlice const & q)
120 {
121         return !(q < p);
122 }
123
124
125 std::ostream & operator<<(std::ostream & os, CursorSlice const & item)
126 {
127         return os
128            << "inset: " << &item.inset()
129 //         << " text: " << item.text()
130            << " idx: " << item.idx()
131            << " par: " << item.pit()
132            << " pos: " << item.pos()
133 //         << " x: " << item.inset().x()
134 //         << " y: " << item.inset().y()
135 ;
136 }