]> git.lyx.org Git - lyx.git/blob - src/bufferview_funcs.C
get rid of broken_header.h and some unneeded tests
[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/insettext.h"
37
38 #include "support/tostr.h"
39
40 #include <sstream>
41
42 using lyx::support::bformat;
43
44 using std::istringstream;
45 using std::ostringstream;
46 using std::string;
47
48
49 namespace bv_funcs {
50
51 // Set data using font and toggle
52 // If successful, returns true
53 bool font2string(LyXFont const & font, bool 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 " << tostr(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, LyXFont & font, bool & toggle)
79 {
80         istringstream is(data);
81         LyXLex 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(LyXFont::FONT_FAMILY(next));
96
97                 } else if (token == "series") {
98                         int const next = lex.getInteger();
99                         font.setSeries(LyXFont::FONT_SERIES(next));
100
101                 } else if (token == "shape") {
102                         int const next = lex.getInteger();
103                         font.setShape(LyXFont::FONT_SHAPE(next));
104
105                 } else if (token == "size") {
106                         int const next = lex.getInteger();
107                         font.setSize(LyXFont::FONT_SIZE(next));
108
109                 } else if (token == "emph" || token == "underbar" ||
110                            token == "noun" || token == "number") {
111
112                         int const next = lex.getInteger();
113                         LyXFont::FONT_MISC_STATE const misc =
114                                 LyXFont::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(LColor::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(DocIterator const & dit) 
154 {
155         int x = 0;
156         int y = 0;
157         
158         // Contribution of nested insets
159         for (size_t i = 1; i != dit.size(); ++i) {
160                 CursorSlice const & sl = dit[i];
161                 int xx = 0, yy = 0;
162                 sl.inset().getCursorPos(sl, xx, yy);
163                 x += xx;
164                 y += yy;
165                 //lyxerr << "LCursor::getPos, i: " << i << " x: " << xx << " y: " << y << endl;
166         }
167
168         // Add contribution of initial rows of outermost paragraph
169         CursorSlice const & sl = dit[0];
170         Paragraph const & par = sl.text()->getPar(sl.pit());
171         y -= par.rows()[0].ascent();
172         for (size_t rit = 0, rend = par.pos2row(sl.pos()); rit != rend; ++rit)
173                 y += par.rows()[rit].height();
174         y += par.rows()[par.pos2row(sl.pos())].ascent();
175         x += dit.bottom().text()->cursorX(dit.bottom());
176         return Point(x,y);
177 }
178
179
180 Point getPos(DocIterator const & dit)
181 {
182         CursorSlice const & bot = dit.bottom();
183         CoordCache::InnerParPosCache & cache = theCoords.pars_[bot.text()];
184         CoordCache::InnerParPosCache::iterator it = cache.find(bot.pit());
185         if (it == cache.end()) {
186                 //lyxerr << "cursor out of view" << std::endl;
187                 return Point(-1,-1);
188         }
189         Point p = coordOffset(dit); // offset from outer paragraph
190         p.y_ += it->second.y_;
191         return p;
192 }
193
194
195 // this could be used elsewhere as well?
196 CurStatus status(BufferView const * bv, DocIterator const & dit)
197 {
198         CoordCache::InnerParPosCache & cache = theCoords.pars_[dit.bottom().text()];
199         
200         if (cache.find(dit.bottom().pit()) != cache.end())
201                 return CUR_INSIDE;
202         else if (dit.bottom().pit() < bv->anchor_ref())
203                 return CUR_ABOVE;
204         else
205                 return CUR_BELOW;
206 }
207
208
209 } // namespace bv_funcs