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