]> git.lyx.org Git - lyx.git/blob - src/bufferview_funcs.C
update build instructions (Qt 4.2.2 etc.)
[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
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(LyXFont 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, LyXFont & font, bool & toggle)
85 {
86         istringstream is(data);
87         LyXLex 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(LyXFont::FONT_FAMILY(next));
102
103                 } else if (token == "series") {
104                         int const next = lex.getInteger();
105                         font.setSeries(LyXFont::FONT_SERIES(next));
106
107                 } else if (token == "shape") {
108                         int const next = lex.getInteger();
109                         font.setShape(LyXFont::FONT_SHAPE(next));
110
111                 } else if (token == "size") {
112                         int const next = lex.getInteger();
113                         font.setSize(LyXFont::FONT_SIZE(next));
114
115                 } else if (token == "emph" || token == "underbar" ||
116                            token == "noun" || token == "number") {
117
118                         int const next = lex.getInteger();
119                         LyXFont::FONT_MISC_STATE const misc =
120                                 LyXFont::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(LColor::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
165         // Contribution of nested insets
166         for (size_t i = 1; i != dit.depth(); ++i) {
167                 CursorSlice const & sl = dit[i];
168                 int xx = 0;
169                 int yy = 0;
170                 sl.inset().cursorPos(bv, sl, boundary && ((i+1) == dit.depth()), xx, yy);
171                 x += xx;
172                 y += yy;
173                 //lyxerr << "LCursor::getPos, i: "
174                 // << i << " x: " << xx << " y: " << y << endl;
175         }
176
177         // Add contribution of initial rows of outermost paragraph
178         CursorSlice const & sl = dit[0];
179         ParagraphMetrics const & pm = bv.parMetrics(sl.text(), sl.pit());
180         BOOST_ASSERT(!pm.rows().empty());
181         y -= pm.rows()[0].ascent();
182 #if 1
183         size_t rend;
184         if (sl.pos() > 0 && dit.depth() == 1) {
185                 int pos = sl.pos();
186                 if (pos && boundary)
187                         --pos;
188 //              lyxerr << "coordOffset: boundary:" << boundary << " depth:" << dit.depth() << " pos:" << pos << " sl.pos:" << sl.pos() << std::endl;
189                 rend = pm.pos2row(pos);
190         } else
191                 rend = pm.pos2row(sl.pos());
192 #else
193         size_t rend = pm.pos2row(sl.pos());
194 #endif
195         for (size_t rit = 0; rit != rend; ++rit)
196                 y += pm.rows()[rit].height();
197         y += pm.rows()[rend].ascent();
198         x += dit.bottom().text()->cursorX(bv, dit.bottom(), boundary && dit.depth() == 1);
199
200         return Point(x, y);
201 }
202
203
204 Point getPos(BufferView const & bv, DocIterator const & dit, bool boundary)
205 {
206         CursorSlice const & bot = dit.bottom();
207         CoordCache::ParPosCache::const_iterator cache_it = 
208                 bv.coordCache().getParPos().find(bot.text());
209         if (cache_it == bv.coordCache().getParPos().end())
210                 return Point(-1, -1);
211
212         CoordCache::InnerParPosCache const & cache = cache_it->second;
213         CoordCache::InnerParPosCache::const_iterator it = cache.find(bot.pit());
214         if (it == cache.end()) {
215                 //lyxerr << "cursor out of view" << std::endl;
216                 return Point(-1, -1);
217         }
218         Point p = coordOffset(bv, dit, boundary); // offset from outer paragraph
219         p.y_ += it->second.y_;
220         return p;
221 }
222
223
224 // this could be used elsewhere as well?
225 // FIXME: This does not work within mathed!
226 CurStatus status(BufferView const * bv, DocIterator const & dit)
227 {
228         CoordCache::InnerParPosCache const & cache =
229                 bv->coordCache().getParPos().find(dit.bottom().text())->second;
230
231         if (cache.find(dit.bottom().pit()) != cache.end())
232                 return CUR_INSIDE;
233         else if (dit.bottom().pit() < bv->anchor_ref())
234                 return CUR_ABOVE;
235         else
236                 return CUR_BELOW;
237 }
238
239 namespace {
240
241 bool findNextInset(DocIterator & dit,
242                    vector<InsetBase_code> const & codes,
243                    string const & contents)
244 {
245         DocIterator tmpdit = dit;
246
247         while (tmpdit) {
248                 InsetBase const * inset = tmpdit.nextInset();
249                 if (inset
250                     && find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end()
251                     && (contents.empty() ||
252                         static_cast<InsetCommand const *>(inset)->getContents() == contents)) {
253                         dit = tmpdit;
254                         return true;
255                 }
256                 tmpdit.forwardInset();
257         }
258
259         return false;
260 }
261
262 } // namespace anon
263
264
265 bool findInset(DocIterator & dit, vector<InsetBase_code> const & codes,
266                bool same_content)
267 {
268         string contents;
269         DocIterator tmpdit = dit;
270         tmpdit.forwardInset();
271         if (!tmpdit)
272                 return false;
273
274         if (same_content) {
275                 InsetBase const * inset = tmpdit.nextInset();
276                 if (inset
277                     && find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end()) {
278                         contents = static_cast<InsetCommand const *>(inset)->getContents();
279                 }
280         }
281
282         if (!findNextInset(tmpdit, codes, contents)) {
283                 if (dit.depth() != 1 || dit.pit() != 0 || dit.pos() != 0) {
284                         tmpdit  = doc_iterator_begin(tmpdit.bottom().inset());
285                         if (!findNextInset(tmpdit, codes, contents)) {
286                                 return false;
287                         }
288                 } else
289                         return false;
290         }
291
292         dit = tmpdit;
293         return true;
294 }
295
296
297 void findInset(DocIterator & dit, InsetBase_code code, bool same_content)
298 {
299         findInset(dit, vector<InsetBase_code>(1, code), same_content);
300 }
301
302
303 void gotoInset(BufferView * bv, vector<InsetBase_code> const & codes,
304                bool same_content)
305 {
306         LCursor tmpcur = bv->cursor();
307         if (!findInset(tmpcur, codes, same_content)) {
308                 bv->cursor().message(_("No more insets"));
309                 return;
310         }
311
312         tmpcur.clearSelection();
313         bv->setCursor(tmpcur);
314 }
315
316
317 void gotoInset(BufferView * bv, InsetBase_code code, bool same_content)
318 {
319         gotoInset(bv, vector<InsetBase_code>(1, code), same_content);
320 }
321
322
323 } // namespace bv_funcs
324
325
326 } // namespace lyx