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