]> 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 "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 "Color.h"
27 #include "Lexer.h"
28
29 #include "frontends/alert.h"
30
31 #include "insets/InsetCommand.h"
32 #include "insets/InsetText.h"
33
34 #include "support/convert.h"
35
36 #include <sstream>
37
38
39 namespace lyx {
40
41 using support::bformat;
42
43 using std::istringstream;
44 using std::ostringstream;
45 using std::string;
46 using std::vector;
47 using std::find;
48
49
50 namespace bv_funcs {
51
52 // Set data using font and toggle
53 // If successful, returns true
54 bool font2string(Font const & font, bool const toggle, string & data)
55 {
56         string lang = "ignore";
57         if (font.language())
58                 lang = font.language()->lang();
59
60         ostringstream os;
61         os << "family " << font.family() << '\n'
62            << "series " << font.series() << '\n'
63            << "shape " << font.shape() << '\n'
64            << "size " << font.size() << '\n'
65            << "emph " << font.emph() << '\n'
66            << "underbar " << font.underbar() << '\n'
67            << "noun " << font.noun() << '\n'
68            << "number " << font.number() << '\n'
69            << "color " << font.color() << '\n'
70            << "language " << lang << '\n'
71            << "toggleall " << convert<string>(toggle);
72         data = os.str();
73         return true;
74 }
75
76
77 // Set font and toggle using data
78 // If successful, returns true
79 bool string2font(string const & data, Font & font, bool & toggle)
80 {
81         istringstream is(data);
82         Lexer lex(0,0);
83         lex.setStream(is);
84
85         int nset = 0;
86         while (lex.isOK()) {
87                 string token;
88                 if (lex.next())
89                         token = lex.getString();
90
91                 if (token.empty() || !lex.next())
92                         break;
93
94                 if (token == "family") {
95                         int const next = lex.getInteger();
96                         font.setFamily(Font::FONT_FAMILY(next));
97
98                 } else if (token == "series") {
99                         int const next = lex.getInteger();
100                         font.setSeries(Font::FONT_SERIES(next));
101
102                 } else if (token == "shape") {
103                         int const next = lex.getInteger();
104                         font.setShape(Font::FONT_SHAPE(next));
105
106                 } else if (token == "size") {
107                         int const next = lex.getInteger();
108                         font.setSize(Font::FONT_SIZE(next));
109
110                 } else if (token == "emph" || token == "underbar" ||
111                            token == "noun" || token == "number") {
112
113                         int const next = lex.getInteger();
114                         Font::FONT_MISC_STATE const misc =
115                                 Font::FONT_MISC_STATE(next);
116
117                         if (token == "emph")
118                             font.setEmph(misc);
119                         else if (token == "underbar")
120                                 font.setUnderbar(misc);
121                         else if (token == "noun")
122                                 font.setNoun(misc);
123                         else if (token == "number")
124                                 font.setNumber(misc);
125
126                 } else if (token == "color") {
127                         int const next = lex.getInteger();
128                         font.setColor(Color::color(next));
129
130                 } else if (token == "language") {
131                         string const next = lex.getString();
132                         if (next == "ignore")
133                                 font.setLanguage(ignore_language);
134                         else
135                                 font.setLanguage(languages.getLanguage(next));
136
137                 } else if (token == "toggleall") {
138                         toggle = lex.getBool();
139
140                 } else {
141                         // Unrecognised token
142                         break;
143                 }
144
145                 ++nset;
146         }
147         return (nset > 0);
148 }
149
150
151 // the next two should probably go elsewhere
152 // this give the position relative to (0, baseline) of outermost
153 // paragraph
154 Point coordOffset(BufferView const & bv, DocIterator const & dit,
155                 bool boundary)
156 {
157         int x = 0;
158         int y = 0;
159         int lastw = 0;
160
161         // Addup ontribution of nested insets, from inside to outside,
162         // keeping the outer paragraph for a special handling below
163         for (size_t i = dit.depth() - 1; i >= 1; --i) {
164                 CursorSlice const & sl = dit[i];
165                 int xx = 0;
166                 int yy = 0;
167                 
168                 // get relative position inside sl.inset()
169                 sl.inset().cursorPos(bv, sl, boundary && ((i+1) == dit.depth()), xx, yy);
170                 
171                 // Make relative position inside of the edited inset relative to sl.inset()
172                 x += xx;
173                 y += yy;
174                 
175                 // In case of an RTL inset, the edited inset will be positioned to the left
176                 // of xx:yy
177                 if (sl.text()) {
178                         bool boundary_i = boundary && i + 1 == dit.depth();
179                         bool rtl = sl.text()->isRTL(*bv.buffer(), sl, boundary_i);
180                         if (rtl)
181                                 x -= lastw;
182                 }
183                 
184                 // remember width for the case that sl.inset() is positioned in an RTL inset
185                 lastw = sl.inset().width();
186                 
187                 //lyxerr << "Cursor::getPos, i: "
188                 // << i << " x: " << xx << " y: " << y << endl;
189         }
190
191         // Add contribution of initial rows of outermost paragraph
192         CursorSlice const & sl = dit[0];
193         ParagraphMetrics const & pm = bv.parMetrics(sl.text(), sl.pit());
194         BOOST_ASSERT(!pm.rows().empty());
195         y -= pm.rows()[0].ascent();
196 #if 1
197         // FIXME: document this mess
198         size_t rend;
199         if (sl.pos() > 0 && dit.depth() == 1) {
200                 int pos = sl.pos();
201                 if (pos && boundary)
202                         --pos;
203 //              lyxerr << "coordOffset: boundary:" << boundary << " depth:" << dit.depth() << " pos:" << pos << " sl.pos:" << sl.pos() << std::endl;
204                 rend = pm.pos2row(pos);
205         } else
206                 rend = pm.pos2row(sl.pos());
207 #else
208         size_t rend = pm.pos2row(sl.pos());
209 #endif
210         for (size_t rit = 0; rit != rend; ++rit)
211                 y += pm.rows()[rit].height();
212         y += pm.rows()[rend].ascent();
213         
214         // Make relative position from the nested inset now bufferview absolute.
215         int xx = dit.bottom().text()->cursorX(bv, dit.bottom(), boundary && dit.depth() == 1);
216         x += xx;
217         
218         // In the RTL case place the nested inset at the left of the cursor in 
219         // the outer paragraph
220         bool boundary_1 = boundary && 1 == dit.depth();
221         bool rtl = dit.bottom().text()->isRTL(*bv.buffer(), dit.bottom(), boundary_1);
222         if (rtl)
223                 x -= lastw;
224         
225         return Point(x, y);
226 }
227
228
229 Point getPos(BufferView const & bv, DocIterator const & dit, bool boundary)
230 {
231         CursorSlice const & bot = dit.bottom();
232         CoordCache::ParPosCache::const_iterator cache_it =
233                 bv.coordCache().getParPos().find(bot.text());
234         if (cache_it == bv.coordCache().getParPos().end())
235                 return Point(-1, -1);
236
237         CoordCache::InnerParPosCache const & cache = cache_it->second;
238         CoordCache::InnerParPosCache::const_iterator it = cache.find(bot.pit());
239         if (it == cache.end()) {
240                 //lyxerr << "cursor out of view" << std::endl;
241                 return Point(-1, -1);
242         }
243         Point p = coordOffset(bv, dit, boundary); // offset from outer paragraph
244         p.y_ += it->second.y_;
245         return p;
246 }
247
248
249 // this could be used elsewhere as well?
250 // FIXME: This does not work within mathed!
251 CurStatus status(BufferView const * bv, DocIterator const & dit)
252 {
253         CoordCache::InnerParPosCache const & cache =
254                 bv->coordCache().getParPos().find(dit.bottom().text())->second;
255
256         if (cache.find(dit.bottom().pit()) != cache.end())
257                 return CUR_INSIDE;
258         else if (dit.bottom().pit() < bv->anchor_ref())
259                 return CUR_ABOVE;
260         else
261                 return CUR_BELOW;
262 }
263
264 namespace {
265
266 bool findNextInset(DocIterator & dit,
267                    vector<Inset_code> const & codes,
268                    string const & contents)
269 {
270         DocIterator tmpdit = dit;
271
272         while (tmpdit) {
273                 Inset const * inset = tmpdit.nextInset();
274                 if (inset
275                     && find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end()
276                     && (contents.empty() ||
277                         static_cast<InsetCommand const *>(inset)->getContents() == contents)) {
278                         dit = tmpdit;
279                         return true;
280                 }
281                 tmpdit.forwardInset();
282         }
283
284         return false;
285 }
286
287 } // namespace anon
288
289
290 bool findInset(DocIterator & dit, vector<Inset_code> const & codes,
291                bool same_content)
292 {
293         string contents;
294         DocIterator tmpdit = dit;
295         tmpdit.forwardInset();
296         if (!tmpdit)
297                 return false;
298
299         if (same_content) {
300                 Inset const * inset = tmpdit.nextInset();
301                 if (inset
302                     && find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end()) {
303                         contents = static_cast<InsetCommand const *>(inset)->getContents();
304                 }
305         }
306
307         if (!findNextInset(tmpdit, codes, contents)) {
308                 if (dit.depth() != 1 || dit.pit() != 0 || dit.pos() != 0) {
309                         tmpdit  = doc_iterator_begin(tmpdit.bottom().inset());
310                         if (!findNextInset(tmpdit, codes, contents)) {
311                                 return false;
312                         }
313                 } else
314                         return false;
315         }
316
317         dit = tmpdit;
318         return true;
319 }
320
321
322 void findInset(DocIterator & dit, Inset_code code, bool same_content)
323 {
324         findInset(dit, vector<Inset_code>(1, code), same_content);
325 }
326
327
328 void gotoInset(BufferView * bv, vector<Inset_code> const & codes,
329                bool same_content)
330 {
331         Cursor tmpcur = bv->cursor();
332         if (!findInset(tmpcur, codes, same_content)) {
333                 bv->cursor().message(_("No more insets"));
334                 return;
335         }
336
337         tmpcur.clearSelection();
338         bv->setCursor(tmpcur);
339 }
340
341
342 void gotoInset(BufferView * bv, Inset_code code, bool same_content)
343 {
344         gotoInset(bv, vector<Inset_code>(1, code), same_content);
345 }
346
347
348 } // namespace bv_funcs
349
350
351 } // namespace lyx