]> git.lyx.org Git - lyx.git/blob - src/bufferview_funcs.C
make boundary property an iterator property instead of a CursorSlice property
[lyx.git] / src / bufferview_funcs.C
1 /**
2  * \file bufferview_funcs.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 Jean-Marc Lasgouttes
8  * \author John Levon
9  * \author Angus Leeming
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #include <config.h>
15
16 #include "bufferview_funcs.h"
17
18 #include "author.h"
19 #include "buffer.h"
20 #include "bufferparams.h"
21 #include "BufferView.h"
22 #include "cursor.h"
23 #include "coordcache.h"
24 #include "gettext.h"
25 #include "language.h"
26 #include "LColor.h"
27 #include "lyxlex.h"
28 #include "lyxrow.h"
29 #include "paragraph.h"
30 #include "ParagraphParameters.h"
31 #include "pariterator.h"
32
33 #include "frontends/Alert.h"
34 #include "frontends/LyXView.h"
35
36 #include "insets/insetcommand.h"
37 #include "insets/insettext.h"
38
39 #include "support/convert.h"
40
41 #include <sstream>
42
43 using lyx::support::bformat;
44
45 using std::istringstream;
46 using std::ostringstream;
47 using std::string;
48 using std::vector;
49
50
51 namespace bv_funcs {
52
53 // Set data using font and toggle
54 // If successful, returns true
55 bool font2string(LyXFont const & font, bool const toggle, string & data)
56 {
57         string lang = "ignore";
58         if (font.language())
59                 lang = font.language()->lang();
60
61         ostringstream os;
62         os << "family " << font.family() << '\n'
63            << "series " << font.series() << '\n'
64            << "shape " << font.shape() << '\n'
65            << "size " << font.size() << '\n'
66            << "emph " << font.emph() << '\n'
67            << "underbar " << font.underbar() << '\n'
68            << "noun " << font.noun() << '\n'
69            << "number " << font.number() << '\n'
70            << "color " << font.color() << '\n'
71            << "language " << lang << '\n'
72            << "toggleall " << convert<string>(toggle);
73         data = os.str();
74         return true;
75 }
76
77
78 // Set font and toggle using data
79 // If successful, returns true
80 bool string2font(string const & data, LyXFont & font, bool & toggle)
81 {
82         istringstream is(data);
83         LyXLex lex(0,0);
84         lex.setStream(is);
85
86         int nset = 0;
87         while (lex.isOK()) {
88                 string token;
89                 if (lex.next())
90                         token = lex.getString();
91
92                 if (token.empty() || !lex.next())
93                         break;
94
95                 if (token == "family") {
96                         int const next = lex.getInteger();
97                         font.setFamily(LyXFont::FONT_FAMILY(next));
98
99                 } else if (token == "series") {
100                         int const next = lex.getInteger();
101                         font.setSeries(LyXFont::FONT_SERIES(next));
102
103                 } else if (token == "shape") {
104                         int const next = lex.getInteger();
105                         font.setShape(LyXFont::FONT_SHAPE(next));
106
107                 } else if (token == "size") {
108                         int const next = lex.getInteger();
109                         font.setSize(LyXFont::FONT_SIZE(next));
110
111                 } else if (token == "emph" || token == "underbar" ||
112                            token == "noun" || token == "number") {
113
114                         int const next = lex.getInteger();
115                         LyXFont::FONT_MISC_STATE const misc =
116                                 LyXFont::FONT_MISC_STATE(next);
117
118                         if (token == "emph")
119                             font.setEmph(misc);
120                         else if (token == "underbar")
121                                 font.setUnderbar(misc);
122                         else if (token == "noun")
123                                 font.setNoun(misc);
124                         else if (token == "number")
125                                 font.setNumber(misc);
126
127                 } else if (token == "color") {
128                         int const next = lex.getInteger();
129                         font.setColor(LColor::color(next));
130
131                 } else if (token == "language") {
132                         string const next = lex.getString();
133                         if (next == "ignore")
134                                 font.setLanguage(ignore_language);
135                         else
136                                 font.setLanguage(languages.getLanguage(next));
137
138                 } else if (token == "toggleall") {
139                         toggle = lex.getBool();
140
141                 } else {
142                         // Unrecognised token
143                         break;
144                 }
145
146                 ++nset;
147         }
148         return (nset > 0);
149 }
150
151
152 // the next two should probably go elsewhere
153 // this give the position relative to (0, baseline) of outermost
154 // paragraph
155 Point coordOffset(DocIterator const & dit, bool boundary)
156 {
157         int x = 0;
158         int y = 0;
159
160         // Contribution of nested insets
161         for (size_t i = 1; i != dit.depth(); ++i) {
162                 CursorSlice const & sl = dit[i];
163                 int xx = 0;
164                 int yy = 0;
165                 sl.inset().cursorPos(sl, boundary, xx, yy);
166                 x += xx;
167                 y += yy;
168                 //lyxerr << "LCursor::getPos, i: "
169                 // << i << " x: " << xx << " y: " << y << endl;
170         }
171
172         // Add contribution of initial rows of outermost paragraph
173         CursorSlice const & sl = dit[0];
174         Paragraph const & par = sl.text()->getPar(sl.pit());
175         y -= par.rows()[0].ascent();
176         //size_t rend = par.pos2row(sl.pos() - boundary ? 1 : 0);
177         size_t rend = par.pos2row(sl.pos());
178         for (size_t rit = 0; rit != rend; ++rit)
179                 y += par.rows()[rit].height();
180         y += par.rows()[rend].ascent();
181         x += dit.bottom().text()->cursorX(dit.bottom(), boundary);
182         // The following correction should not be there at all.
183         // The cursor looks much better with the -1, though.
184         --x;
185         return Point(x, y);
186 }
187
188
189 Point getPos(DocIterator const & dit, bool boundary)
190 {
191         CursorSlice const & bot = dit.bottom();
192         CoordCache::InnerParPosCache const & cache =
193                 theCoords.getParPos().find(bot.text())->second;
194         CoordCache::InnerParPosCache::const_iterator it = cache.find(bot.pit());
195         if (it == cache.end()) {
196                 //lyxerr << "cursor out of view" << std::endl;
197                 return Point(-1, -1);
198         }
199         Point p = coordOffset(dit, boundary); // offset from outer paragraph
200         p.y_ += it->second.y_;
201         return p;
202 }
203
204
205 // this could be used elsewhere as well?
206 CurStatus status(BufferView const * bv, DocIterator const & dit)
207 {
208         CoordCache::InnerParPosCache const & cache = theCoords.getParPos().find(dit.bottom().text())->second;
209
210         if (cache.find(dit.bottom().pit()) != cache.end())
211                 return CUR_INSIDE;
212         else if (dit.bottom().pit() < bv->anchor_ref())
213                 return CUR_ABOVE;
214         else
215                 return CUR_BELOW;
216 }
217
218 namespace {
219
220 bool findNextInset(DocIterator & dit,
221                    vector<InsetBase_code> const & codes,
222                    string const & contents)
223 {
224         DocIterator tmpdit = dit;
225
226         while (tmpdit) {
227                 InsetBase const * inset = tmpdit.nextInset();
228                 if (inset
229                     && find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end()
230                     && (contents.empty() ||
231                         static_cast<InsetCommand const *>(inset)->getContents() == contents)) {
232                         dit = tmpdit;
233                         return true;
234                 }
235                 tmpdit.forwardInset();
236         }
237
238         return false;
239 }
240
241 } // namespace anon
242
243
244 bool findInset(DocIterator & dit, vector<InsetBase_code> const & codes,
245                bool same_content)
246 {
247         string contents;
248         DocIterator tmpdit = dit;
249         tmpdit.forwardInset();
250
251         if (same_content) {
252                 InsetBase const * inset = tmpdit.nextInset();
253                 if (inset
254                     && find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end()) {
255                         contents = static_cast<InsetCommand const *>(inset)->getContents();
256                 }
257         }
258
259         if (!findNextInset(tmpdit, codes, contents)) {
260                 if (dit.depth() != 1 || dit.pit() != 0 || dit.pos() != 0) {
261                         tmpdit  = doc_iterator_begin(tmpdit.bottom().inset());
262                         if (!findNextInset(tmpdit, codes, contents)) {
263                                 return false;
264                         }
265                 } else
266                         return false;
267         }
268         
269         dit = tmpdit;
270         return true;
271 }
272
273
274 void findInset(DocIterator & dit, InsetBase_code code, bool same_content)
275 {
276         findInset(dit, vector<InsetBase_code>(1, code), same_content);
277 }
278
279
280 void gotoInset(BufferView * bv, vector<InsetBase_code> const & codes,
281                bool same_content)
282 {
283         LCursor tmpcur = bv->cursor();
284         if (!findInset(tmpcur, codes, same_content)) {
285                 bv->cursor().message(_("No more insets"));
286                 return;
287         }
288
289         tmpcur.clearSelection();
290         bv->setCursor(tmpcur);
291 }
292
293
294 void gotoInset(BufferView * bv, InsetBase_code code, bool same_content)
295 {
296         gotoInset(bv, vector<InsetBase_code>(1, code), same_content);
297 }
298
299
300 } // namespace bv_funcs