]> git.lyx.org Git - lyx.git/blob - src/bufferview_funcs.cpp
header cleanup.
[lyx.git] / src / bufferview_funcs.cpp
1 /**
2  * \file bufferview_funcs.cpp
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  * \author Juergen Vigna
11  *
12  * Full author contact details are available in file CREDITS.
13  */
14
15 #include <config.h>
16
17 #include "bufferview_funcs.h"
18
19 #include "Author.h"
20 #include "Buffer.h"
21 #include "BufferParams.h"
22 #include "BufferView.h"
23 #include "Cursor.h"
24 #include "CoordCache.h"
25 #include "gettext.h"
26 #include "Language.h"
27 #include "Color.h"
28 #include "Lexer.h"
29
30 #include "frontends/alert.h"
31
32 #include "insets/InsetCommand.h"
33 #include "insets/InsetText.h"
34
35 #include "support/convert.h"
36
37 #include <sstream>
38
39
40 namespace lyx {
41
42 using support::bformat;
43
44 using std::istringstream;
45 using std::ostringstream;
46 using std::string;
47 using std::vector;
48 using std::find;
49
50
51 namespace bv_funcs {
52
53 // Set data using font and toggle
54 // If successful, returns true
55 bool font2string(Font 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, Font & font, bool & toggle)
81 {
82         istringstream is(data);
83         Lexer 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(Font::FONT_FAMILY(next));
98
99                 } else if (token == "series") {
100                         int const next = lex.getInteger();
101                         font.setSeries(Font::FONT_SERIES(next));
102
103                 } else if (token == "shape") {
104                         int const next = lex.getInteger();
105                         font.setShape(Font::FONT_SHAPE(next));
106
107                 } else if (token == "size") {
108                         int const next = lex.getInteger();
109                         font.setSize(Font::FONT_SIZE(next));
110
111                 } else if (token == "emph" || token == "underbar" ||
112                            token == "noun" || token == "number") {
113
114                         int const next = lex.getInteger();
115                         Font::FONT_MISC_STATE const misc =
116                                 Font::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(Color::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(BufferView const & bv, DocIterator const & dit,
156                 bool boundary)
157 {
158         int x = 0;
159         int y = 0;
160         int lastw = 0;
161
162         // Addup ontribution of nested insets, from inside to outside,
163         // keeping the outer paragraph for a special handling below
164         for (size_t i = dit.depth() - 1; i >= 1; --i) {
165                 CursorSlice const & sl = dit[i];
166                 int xx = 0;
167                 int yy = 0;
168                 
169                 // get relative position inside sl.inset()
170                 sl.inset().cursorPos(bv, sl, boundary && ((i+1) == dit.depth()), xx, yy);
171                 
172                 // Make relative position inside of the edited inset relative to sl.inset()
173                 x += xx;
174                 y += yy;
175                 
176                 // In case of an RTL inset, the edited inset will be positioned to the left
177                 // of xx:yy
178                 if (sl.text()) {
179                         bool boundary_i = boundary && i + 1 == dit.depth();
180                         bool rtl = sl.text()->isRTL(*bv.buffer(), sl, boundary_i);
181                         if (rtl)
182                                 x -= lastw;
183                 }
184                 
185                 // remember width for the case that sl.inset() is positioned in an RTL inset
186                 lastw = sl.inset().width();
187                 
188                 //lyxerr << "Cursor::getPos, i: "
189                 // << i << " x: " << xx << " y: " << y << endl;
190         }
191
192         // Add contribution of initial rows of outermost paragraph
193         CursorSlice const & sl = dit[0];
194         ParagraphMetrics const & pm = bv.parMetrics(sl.text(), sl.pit());
195         BOOST_ASSERT(!pm.rows().empty());
196         y -= pm.rows()[0].ascent();
197 #if 1
198         // FIXME: document this mess
199         size_t rend;
200         if (sl.pos() > 0 && dit.depth() == 1) {
201                 int pos = sl.pos();
202                 if (pos && boundary)
203                         --pos;
204 //              lyxerr << "coordOffset: boundary:" << boundary << " depth:" << dit.depth() << " pos:" << pos << " sl.pos:" << sl.pos() << std::endl;
205                 rend = pm.pos2row(pos);
206         } else
207                 rend = pm.pos2row(sl.pos());
208 #else
209         size_t rend = pm.pos2row(sl.pos());
210 #endif
211         for (size_t rit = 0; rit != rend; ++rit)
212                 y += pm.rows()[rit].height();
213         y += pm.rows()[rend].ascent();
214         
215         // Make relative position from the nested inset now bufferview absolute.
216         int xx = dit.bottom().text()->cursorX(bv, dit.bottom(), boundary && dit.depth() == 1);
217         x += xx;
218         
219         // In the RTL case place the nested inset at the left of the cursor in 
220         // the outer paragraph
221         bool boundary_1 = boundary && 1 == dit.depth();
222         bool rtl = dit.bottom().text()->isRTL(*bv.buffer(), dit.bottom(), boundary_1);
223         if (rtl)
224                 x -= lastw;
225         
226         return Point(x, y);
227 }
228
229
230 Point getPos(BufferView const & bv, DocIterator const & dit, bool boundary)
231 {
232         CursorSlice const & bot = dit.bottom();
233         CoordCache::ParPosCache::const_iterator cache_it =
234                 bv.coordCache().getParPos().find(bot.text());
235         if (cache_it == bv.coordCache().getParPos().end())
236                 return Point(-1, -1);
237
238         CoordCache::InnerParPosCache const & cache = cache_it->second;
239         CoordCache::InnerParPosCache::const_iterator it = cache.find(bot.pit());
240         if (it == cache.end()) {
241                 //lyxerr << "cursor out of view" << std::endl;
242                 return Point(-1, -1);
243         }
244         Point p = coordOffset(bv, dit, boundary); // offset from outer paragraph
245         p.y_ += it->second.y_;
246         return p;
247 }
248
249
250 // this could be used elsewhere as well?
251 // FIXME: This does not work within mathed!
252 CurStatus status(BufferView const * bv, DocIterator const & dit)
253 {
254         CoordCache::InnerParPosCache const & cache =
255                 bv->coordCache().getParPos().find(dit.bottom().text())->second;
256
257         if (cache.find(dit.bottom().pit()) != cache.end())
258                 return CUR_INSIDE;
259         else if (dit.bottom().pit() < bv->anchor_ref())
260                 return CUR_ABOVE;
261         else
262                 return CUR_BELOW;
263 }
264
265 namespace {
266
267 bool findNextInset(DocIterator & dit,
268                    vector<Inset_code> const & codes,
269                    string const & contents)
270 {
271         DocIterator tmpdit = dit;
272
273         while (tmpdit) {
274                 Inset const * inset = tmpdit.nextInset();
275                 if (inset
276                     && find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end()
277                     && (contents.empty() ||
278                         static_cast<InsetCommand const *>(inset)->getContents() == contents)) {
279                         dit = tmpdit;
280                         return true;
281                 }
282                 tmpdit.forwardInset();
283         }
284
285         return false;
286 }
287
288 } // namespace anon
289
290
291 bool findInset(DocIterator & dit, vector<Inset_code> const & codes,
292                bool same_content)
293 {
294         string contents;
295         DocIterator tmpdit = dit;
296         tmpdit.forwardInset();
297         if (!tmpdit)
298                 return false;
299
300         if (same_content) {
301                 Inset const * inset = tmpdit.nextInset();
302                 if (inset
303                     && find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end()) {
304                         contents = static_cast<InsetCommand const *>(inset)->getContents();
305                 }
306         }
307
308         if (!findNextInset(tmpdit, codes, contents)) {
309                 if (dit.depth() != 1 || dit.pit() != 0 || dit.pos() != 0) {
310                         tmpdit  = doc_iterator_begin(tmpdit.bottom().inset());
311                         if (!findNextInset(tmpdit, codes, contents)) {
312                                 return false;
313                         }
314                 } else
315                         return false;
316         }
317
318         dit = tmpdit;
319         return true;
320 }
321
322
323 void findInset(DocIterator & dit, Inset_code code, bool same_content)
324 {
325         findInset(dit, vector<Inset_code>(1, code), same_content);
326 }
327
328
329 void gotoInset(BufferView * bv, vector<Inset_code> const & codes,
330                bool same_content)
331 {
332         Cursor tmpcur = bv->cursor();
333         if (!findInset(tmpcur, codes, same_content)) {
334                 bv->cursor().message(_("No more insets"));
335                 return;
336         }
337
338         tmpcur.clearSelection();
339         bv->setCursor(tmpcur);
340 }
341
342
343 void gotoInset(BufferView * bv, Inset_code code, bool same_content)
344 {
345         gotoInset(bv, vector<Inset_code>(1, code), same_content);
346 }
347
348
349 } // namespace bv_funcs
350
351
352 } // namespace lyx