]> git.lyx.org Git - lyx.git/blob - src/bufferview_funcs.C
minimal effort implementation of:
[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                 sl.inset().cursorPos(sl, boundary && ((i+1) == dit.depth()), xx, yy);
167                 x += xx;
168                 y += yy;
169                 //lyxerr << "LCursor::getPos, i: "
170                 // << i << " x: " << xx << " y: " << y << endl;
171         }
172
173         // Add contribution of initial rows of outermost paragraph
174         CursorSlice const & sl = dit[0];
175         Paragraph const & par = sl.text()->getPar(sl.pit());
176         y -= par.rows()[0].ascent();
177 #if 1
178         size_t rend;
179         if (sl.pos() > 0 && dit.depth() == 1) {
180                 int pos = sl.pos();
181                 if (pos && boundary)
182                         --pos;
183 //              lyxerr << "coordOffset: boundary:" << boundary << " depth:" << dit.depth() << " pos:" << pos << " sl.pos:" << sl.pos() << std::endl;
184                 rend = par.pos2row(pos);
185         } else
186                 rend = par.pos2row(sl.pos());
187 #else
188         size_t rend = par.pos2row(sl.pos());
189 #endif
190         for (size_t rit = 0; rit != rend; ++rit)
191                 y += par.rows()[rit].height();
192         y += par.rows()[rend].ascent();
193         x += dit.bottom().text()->cursorX(dit.bottom(), boundary && dit.depth() == 1);
194         // The following correction should not be there at all.
195         // The cursor looks much better with the -1, though.
196         --x;
197         return Point(x, y);
198 }
199
200
201 Point getPos(DocIterator const & dit, bool boundary)
202 {
203         CursorSlice const & bot = dit.bottom();
204         CoordCache::ParPosCache::const_iterator cache_it = 
205                 theCoords.getParPos().find(bot.text());
206         if (cache_it == theCoords.getParPos().end())
207                 return Point(-1, -1);
208
209         CoordCache::InnerParPosCache const & cache = cache_it->second;
210         CoordCache::InnerParPosCache::const_iterator it = cache.find(bot.pit());
211         if (it == cache.end()) {
212                 //lyxerr << "cursor out of view" << std::endl;
213                 return Point(-1, -1);
214         }
215         Point p = coordOffset(dit, boundary); // offset from outer paragraph
216         p.y_ += it->second.y_;
217         return p;
218 }
219
220
221 // this could be used elsewhere as well?
222 CurStatus status(BufferView const * bv, DocIterator const & dit)
223 {
224         CoordCache::InnerParPosCache const & cache = theCoords.getParPos().find(dit.bottom().text())->second;
225
226         if (cache.find(dit.bottom().pit()) != cache.end())
227                 return CUR_INSIDE;
228         else if (dit.bottom().pit() < bv->anchor_ref())
229                 return CUR_ABOVE;
230         else
231                 return CUR_BELOW;
232 }
233
234 namespace {
235
236 bool findNextInset(DocIterator & dit,
237                    vector<InsetBase_code> const & codes,
238                    string const & contents)
239 {
240         DocIterator tmpdit = dit;
241
242         while (tmpdit) {
243                 InsetBase const * inset = tmpdit.nextInset();
244                 if (inset
245                     && find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end()
246                     && (contents.empty() ||
247                         static_cast<InsetCommand const *>(inset)->getContents() == contents)) {
248                         dit = tmpdit;
249                         return true;
250                 }
251                 tmpdit.forwardInset();
252         }
253
254         return false;
255 }
256
257 } // namespace anon
258
259
260 bool findInset(DocIterator & dit, vector<InsetBase_code> const & codes,
261                bool same_content)
262 {
263         string contents;
264         DocIterator tmpdit = dit;
265         tmpdit.forwardInset();
266         if (!tmpdit)
267                 return false;
268
269         if (same_content) {
270                 InsetBase const * inset = tmpdit.nextInset();
271                 if (inset
272                     && find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end()) {
273                         contents = static_cast<InsetCommand const *>(inset)->getContents();
274                 }
275         }
276
277         if (!findNextInset(tmpdit, codes, contents)) {
278                 if (dit.depth() != 1 || dit.pit() != 0 || dit.pos() != 0) {
279                         tmpdit  = doc_iterator_begin(tmpdit.bottom().inset());
280                         if (!findNextInset(tmpdit, codes, contents)) {
281                                 return false;
282                         }
283                 } else
284                         return false;
285         }
286
287         dit = tmpdit;
288         return true;
289 }
290
291
292 void findInset(DocIterator & dit, InsetBase_code code, bool same_content)
293 {
294         findInset(dit, vector<InsetBase_code>(1, code), same_content);
295 }
296
297
298 void gotoInset(BufferView * bv, vector<InsetBase_code> const & codes,
299                bool same_content)
300 {
301         LCursor tmpcur = bv->cursor();
302         if (!findInset(tmpcur, codes, same_content)) {
303                 bv->cursor().message(_("No more insets"));
304                 return;
305         }
306
307         tmpcur.clearSelection();
308         bv->setCursor(tmpcur);
309 }
310
311
312 void gotoInset(BufferView * bv, InsetBase_code code, bool same_content)
313 {
314         gotoInset(bv, vector<InsetBase_code>(1, code), same_content);
315 }
316
317
318 } // namespace bv_funcs