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