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"
27 #include "paragraph.h"
28 #include "ParagraphParameters.h"
29 #include "PosIterator.h"
30 #include "iterators.h"
32 #include "frontends/Alert.h"
33 #include "frontends/LyXView.h"
35 #include "insets/insettext.h"
37 #include "mathed/math_cursor.h"
39 #include "support/tostr.h"
41 #include "support/std_sstream.h"
43 using lyx::support::bformat;
45 using std::istringstream;
46 using std::ostringstream;
52 // Set data using font and toggle
53 // If successful, returns true
54 bool font2string(LyXFont const & font, bool toggle, string & data)
56 string lang = "ignore";
58 lang = font.language()->lang();
61 os << "family " << font.family() << '\n'
62 << "series " << font.series() << '\n'
63 << "shape " << font.shape() << '\n'
64 << "size " << font.size() << '\n'
65 << "emph " << font.emph() << '\n'
66 << "underbar " << font.underbar() << '\n'
67 << "noun " << font.noun() << '\n'
68 << "number " << font.number() << '\n'
69 << "color " << font.color() << '\n'
70 << "language " << lang << '\n'
71 << "toggleall " << tostr(toggle);
77 // Set font and toggle using data
78 // If successful, returns true
79 bool string2font(string const & data, LyXFont & font, bool & toggle)
81 istringstream is(data);
89 token = lex.getString();
91 if (token.empty() || !lex.next())
94 if (token == "family") {
95 int const next = lex.getInteger();
96 font.setFamily(LyXFont::FONT_FAMILY(next));
98 } else if (token == "series") {
99 int const next = lex.getInteger();
100 font.setSeries(LyXFont::FONT_SERIES(next));
102 } else if (token == "shape") {
103 int const next = lex.getInteger();
104 font.setShape(LyXFont::FONT_SHAPE(next));
106 } else if (token == "size") {
107 int const next = lex.getInteger();
108 font.setSize(LyXFont::FONT_SIZE(next));
110 } else if (token == "emph" || token == "underbar" ||
111 token == "noun" || token == "number") {
113 int const next = lex.getInteger();
114 LyXFont::FONT_MISC_STATE const misc =
115 LyXFont::FONT_MISC_STATE(next);
119 else if (token == "underbar")
120 font.setUnderbar(misc);
121 else if (token == "noun")
123 else if (token == "number")
124 font.setNumber(misc);
126 } else if (token == "color") {
127 int const next = lex.getInteger();
128 font.setColor(LColor::color(next));
130 } else if (token == "language") {
131 string const next = lex.getString();
132 if (next == "ignore")
133 font.setLanguage(ignore_language);
135 font.setLanguage(languages.getLanguage(next));
137 } else if (token == "toggleall") {
138 toggle = lex.getBool();
141 // Unrecognised token
151 bool changeDepth(BufferView * bv, LyXText * text, DEPTH_CHANGE type, bool test_only)
153 if (!bv->available() || !text)
157 return text->changeDepth(type, true);
159 bool const changed = text->changeDepth(type, false);
165 // Returns the current font and depth as a message.
166 string const currentState(BufferView * bv)
168 if (!bv->available())
172 return mathcursor->info();
176 LyXText * text = bv->getLyXText();
177 Buffer * buffer = bv->buffer();
178 LyXCursor const & c = text->cursor;
180 bool const show_change = buffer->params().tracking_changes
181 && text->cursor.pos() != text->cursorPar()->size()
182 && text->cursorPar()->lookupChange(c.pos()) != Change::UNCHANGED;
185 Change change = text->cursorPar()->lookupChangeFull(c.pos());
186 Author const & a = bv->buffer()->params().authors().get(change.author);
187 state << _("Change: ") << a.name();
188 if (!a.email().empty())
189 state << " (" << a.email() << ")";
190 if (change.changetime)
191 state << _(" at ") << ctime(&change.changetime);
195 // I think we should only show changes from the default
197 LyXFont font = text->real_current_font;
198 font.reduce(buffer->params().getLyXTextClass().defaultfont());
200 // avoid _(...) re-entrance problem
201 string const s = font.stateText(&buffer->params());
202 state << bformat(_("Font: %1$s"), s);
204 // state << bformat(_("Font: %1$s"), font.stateText(&buffer->params));
206 // The paragraph depth
207 int depth = text->getDepth();
209 state << bformat(_(", Depth: %1$s"), tostr(depth));
211 // The paragraph spacing, but only if different from
213 if (!text->cursorPar()->params().spacing().isDefault()) {
214 Spacing::Space cur_space =
215 text->cursorPar()->params().spacing().getSpace();
216 state << _(", Spacing: ");
219 case Spacing::Single:
220 state << _("Single");
222 case Spacing::Onehalf:
223 state << _("OneHalf");
225 case Spacing::Double:
226 state << _("Double");
229 state << _("Other (")
230 << text->cursorPar()->params().spacing().getValue()
233 case Spacing::Default:
234 // should never happen, do nothing
239 ParagraphList::iterator pit = text->cursorPar();
240 state << _(", Paragraph: ") << pit->id();
241 state << _(", Position: ") << text->cursor.pos();
242 RowList::iterator rit = pit->getRow(text->cursor.pos());
243 state << bformat(_(", Row b:%1$d e:%2$d"), rit->pos(), rit->endpos());
244 state << _(", Inset: ");
245 InsetOld * inset = pit->inInset();
247 state << inset << " owner: " << inset->owner();
256 // deletes a selection during an insertion
257 void replaceSelection(LyXText * text)
259 if (text->selection.set()) {
260 text->cutSelection(true, false);
261 text->bv()->update();
266 void put_selection_at(BufferView * bv, PosIterator const & cur,
267 int length, bool backwards)
269 ParIterator par(cur);
271 bv->getLyXText()->clearSelection();
273 LyXText * text = par.text(bv);
276 text->setCursor(cur.pit(), cur.pos());
279 text->setSelectionRange(length);
280 text->setSelection();
282 text->cursor = text->selection.start;
290 }; // namespace bv_funcs