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.
6 * \author Lars Gullik Bjønnes
7 * \author Jean-Marc Lasgouttes
9 * \author Angus Leeming
11 * Full author contact details are available in file CREDITS.
16 #include "bufferview_funcs.h"
20 #include "bufferparams.h"
21 #include "BufferView.h"
28 #include "paragraph.h"
29 #include "ParagraphParameters.h"
30 #include "PosIterator.h"
31 #include "iterators.h"
33 #include "frontends/Alert.h"
34 #include "frontends/LyXView.h"
36 #include "insets/insettext.h"
38 #include "mathed/math_cursor.h"
40 #include "support/tostr.h"
42 #include "support/std_sstream.h"
44 using lyx::support::bformat;
46 using std::istringstream;
47 using std::ostringstream;
53 // Set data using font and toggle
54 // If successful, returns true
55 bool font2string(LyXFont const & font, bool toggle, string & data)
57 string lang = "ignore";
59 lang = font.language()->lang();
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 " << tostr(toggle);
78 // Set font and toggle using data
79 // If successful, returns true
80 bool string2font(string const & data, LyXFont & font, bool & toggle)
82 istringstream is(data);
90 token = lex.getString();
92 if (token.empty() || !lex.next())
95 if (token == "family") {
96 int const next = lex.getInteger();
97 font.setFamily(LyXFont::FONT_FAMILY(next));
99 } else if (token == "series") {
100 int const next = lex.getInteger();
101 font.setSeries(LyXFont::FONT_SERIES(next));
103 } else if (token == "shape") {
104 int const next = lex.getInteger();
105 font.setShape(LyXFont::FONT_SHAPE(next));
107 } else if (token == "size") {
108 int const next = lex.getInteger();
109 font.setSize(LyXFont::FONT_SIZE(next));
111 } else if (token == "emph" || token == "underbar" ||
112 token == "noun" || token == "number") {
114 int const next = lex.getInteger();
115 LyXFont::FONT_MISC_STATE const misc =
116 LyXFont::FONT_MISC_STATE(next);
120 else if (token == "underbar")
121 font.setUnderbar(misc);
122 else if (token == "noun")
124 else if (token == "number")
125 font.setNumber(misc);
127 } else if (token == "color") {
128 int const next = lex.getInteger();
129 font.setColor(LColor::color(next));
131 } else if (token == "language") {
132 string const next = lex.getString();
133 if (next == "ignore")
134 font.setLanguage(ignore_language);
136 font.setLanguage(languages.getLanguage(next));
138 } else if (token == "toggleall") {
139 toggle = lex.getBool();
142 // Unrecognised token
152 bool changeDepth(BufferView * bv, LyXText * text, DEPTH_CHANGE type, bool test_only)
154 if (!bv->available() || !text)
158 return text->changeDepth(type, true);
160 bool const changed = text->changeDepth(type, false);
166 // Returns the current font and depth as a message.
167 string const currentState(BufferView * bv)
169 if (!bv->available())
173 return mathcursor->info();
177 LyXText * text = bv->getLyXText();
178 Buffer * buffer = bv->buffer();
179 LyXCursor const & c = text->cursor;
181 bool const show_change = buffer->params().tracking_changes
182 && text->cursor.pos() != text->cursorPar()->size()
183 && text->cursorPar()->lookupChange(c.pos()) != Change::UNCHANGED;
186 Change change = text->cursorPar()->lookupChangeFull(c.pos());
187 Author const & a = bv->buffer()->params().authors().get(change.author);
188 state << _("Change: ") << a.name();
189 if (!a.email().empty())
190 state << " (" << a.email() << ")";
191 if (change.changetime)
192 state << _(" at ") << ctime(&change.changetime);
196 // I think we should only show changes from the default
198 LyXFont font = text->real_current_font;
199 font.reduce(buffer->params().getLyXTextClass().defaultfont());
201 // avoid _(...) re-entrance problem
202 string const s = font.stateText(&buffer->params());
203 state << bformat(_("Font: %1$s"), s);
205 // state << bformat(_("Font: %1$s"), font.stateText(&buffer->params));
207 // The paragraph depth
208 int depth = text->getDepth();
210 state << bformat(_(", Depth: %1$s"), tostr(depth));
212 // The paragraph spacing, but only if different from
214 if (!text->cursorPar()->params().spacing().isDefault()) {
215 Spacing::Space cur_space =
216 text->cursorPar()->params().spacing().getSpace();
217 state << _(", Spacing: ");
220 case Spacing::Single:
221 state << _("Single");
223 case Spacing::Onehalf:
224 state << _("OneHalf");
226 case Spacing::Double:
227 state << _("Double");
230 state << _("Other (")
231 << text->cursorPar()->params().spacing().getValue()
234 case Spacing::Default:
235 // should never happen, do nothing
240 ParagraphList::iterator pit = text->cursorPar();
241 state << _(", Paragraph: ") << pit->id();
242 state << _(", Position: ") << text->cursor.pos();
243 RowList::iterator rit = pit->getRow(text->cursor.pos());
244 state << bformat(_(", Row b:%1$d e:%2$d"), rit->pos(), rit->endpos());
245 state << _(", Inset: ");
246 InsetOld * inset = pit->inInset();
248 state << inset << " owner: " << inset->owner();
257 // deletes a selection during an insertion
258 void replaceSelection(LyXText * text)
260 if (text->selection.set()) {
261 text->cutSelection(true, false);
262 text->bv()->update();
268 if the fitCursor call refers to some point in never-explored-land, then we
269 don't have y information in insets there, then we cannot even do an update
270 to get it (because we need the y infomation for setting top_y first). So
271 this is solved in put_selection_at with:
273 - setting top_y to the y of the outerPar (that has good info)
275 - calling cursor().updatePos()
276 - then call fitCursor()
281 void put_selection_at(BufferView * bv, PosIterator const & cur,
282 int length, bool backwards)
284 ParIterator par(cur);
286 bv->getLyXText()->clearSelection();
288 LyXText * text = par.text(bv);
290 //hack for the chicken and egg problem
292 bv->top_y(par.outerPar()->y);
294 text->setCursor(cur.pit(), cur.pos());
295 bv->cursor().updatePos();
298 text->setSelectionRange(length);
299 text->setSelection();
301 text->cursor = text->selection.start;
309 }; // namespace bv_funcs