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