]> git.lyx.org Git - lyx.git/blob - src/bufferview_funcs.C
do not define boost::throw_exceptions if we are compiling with exceptions
[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::InnerParPosCache const & cache =
205                 theCoords.getParPos().find(bot.text())->second;
206         CoordCache::InnerParPosCache::const_iterator it = cache.find(bot.pit());
207         if (it == cache.end()) {
208                 //lyxerr << "cursor out of view" << std::endl;
209                 return Point(-1, -1);
210         }
211         Point p = coordOffset(dit, boundary); // offset from outer paragraph
212         p.y_ += it->second.y_;
213         return p;
214 }
215
216
217 // this could be used elsewhere as well?
218 CurStatus status(BufferView const * bv, DocIterator const & dit)
219 {
220         CoordCache::InnerParPosCache const & cache = theCoords.getParPos().find(dit.bottom().text())->second;
221
222         if (cache.find(dit.bottom().pit()) != cache.end())
223                 return CUR_INSIDE;
224         else if (dit.bottom().pit() < bv->anchor_ref())
225                 return CUR_ABOVE;
226         else
227                 return CUR_BELOW;
228 }
229
230 namespace {
231
232 bool findNextInset(DocIterator & dit,
233                    vector<InsetBase_code> const & codes,
234                    string const & contents)
235 {
236         DocIterator tmpdit = dit;
237
238         while (tmpdit) {
239                 InsetBase const * inset = tmpdit.nextInset();
240                 if (inset
241                     && find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end()
242                     && (contents.empty() ||
243                         static_cast<InsetCommand const *>(inset)->getContents() == contents)) {
244                         dit = tmpdit;
245                         return true;
246                 }
247                 tmpdit.forwardInset();
248         }
249
250         return false;
251 }
252
253 } // namespace anon
254
255
256 bool findInset(DocIterator & dit, vector<InsetBase_code> const & codes,
257                bool same_content)
258 {
259         string contents;
260         DocIterator tmpdit = dit;
261         tmpdit.forwardInset();
262
263         if (same_content) {
264                 InsetBase const * inset = tmpdit.nextInset();
265                 if (inset
266                     && find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end()) {
267                         contents = static_cast<InsetCommand const *>(inset)->getContents();
268                 }
269         }
270
271         if (!findNextInset(tmpdit, codes, contents)) {
272                 if (dit.depth() != 1 || dit.pit() != 0 || dit.pos() != 0) {
273                         tmpdit  = doc_iterator_begin(tmpdit.bottom().inset());
274                         if (!findNextInset(tmpdit, codes, contents)) {
275                                 return false;
276                         }
277                 } else
278                         return false;
279         }
280         
281         dit = tmpdit;
282         return true;
283 }
284
285
286 void findInset(DocIterator & dit, InsetBase_code code, bool same_content)
287 {
288         findInset(dit, vector<InsetBase_code>(1, code), same_content);
289 }
290
291
292 void gotoInset(BufferView * bv, vector<InsetBase_code> const & codes,
293                bool same_content)
294 {
295         LCursor tmpcur = bv->cursor();
296         if (!findInset(tmpcur, codes, same_content)) {
297                 bv->cursor().message(_("No more insets"));
298                 return;
299         }
300
301         tmpcur.clearSelection();
302         bv->setCursor(tmpcur);
303 }
304
305
306 void gotoInset(BufferView * bv, InsetBase_code code, bool same_content)
307 {
308         gotoInset(bv, vector<InsetBase_code>(1, code), same_content);
309 }
310
311
312 } // namespace bv_funcs