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"
19 #include "bufferparams.h"
20 #include "BufferView.h"
25 #include "paragraph.h"
26 #include "ParagraphParameters.h"
28 #include "frontends/Alert.h"
29 #include "frontends/LyXView.h"
31 #include "insets/insettext.h"
33 #include "mathed/math_cursor.h"
35 #include "support/tostr.h"
37 #include "support/std_sstream.h"
39 using namespace lyx::support;
41 using std::istringstream;
42 using std::ostringstream;
47 LyXFont freefont(LyXFont::ALL_IGNORE);
48 bool toggleall(false);
54 // Set data using font and toggle
55 // If successful, returns true
56 bool font2string(LyXFont const & font, bool toggle, string & data)
58 string lang = "ignore";
60 lang = font.language()->lang();
63 os << "family " << font.family() << '\n'
64 << "series " << font.series() << '\n'
65 << "shape " << font.shape() << '\n'
66 << "size " << font.size() << '\n'
67 << "emph " << font.emph() << '\n'
68 << "underbar " << font.underbar() << '\n'
69 << "noun " << font.noun() << '\n'
70 << "number " << font.number() << '\n'
71 << "color " << font.color() << '\n'
72 << "language " << lang << '\n'
73 << "toggleall " << tostr(toggle);
74 data = STRCONV(os.str());
79 // Set font and toggle using data
80 // If successful, returns true
81 bool string2font(string const & data, LyXFont & font, bool & toggle)
83 istringstream is(STRCONV(data));
91 token = lex.getString();
93 if (token.empty() || !lex.next())
96 if (token == "family") {
97 int const next = lex.getInteger();
98 font.setFamily(LyXFont::FONT_FAMILY(next));
100 } else if (token == "series") {
101 int const next = lex.getInteger();
102 font.setSeries(LyXFont::FONT_SERIES(next));
104 } else if (token == "shape") {
105 int const next = lex.getInteger();
106 font.setShape(LyXFont::FONT_SHAPE(next));
108 } else if (token == "size") {
109 int const next = lex.getInteger();
110 font.setSize(LyXFont::FONT_SIZE(next));
112 } else if (token == "emph" || token == "underbar" ||
113 token == "noun" || token == "number") {
115 int const next = lex.getInteger();
116 LyXFont::FONT_MISC_STATE const misc =
117 LyXFont::FONT_MISC_STATE(next);
121 else if (token == "underbar")
122 font.setUnderbar(misc);
123 else if (token == "noun")
125 else if (token == "number")
126 font.setNumber(misc);
128 } else if (token == "color") {
129 int const next = lex.getInteger();
130 font.setColor(LColor::color(next));
132 } else if (token == "language") {
133 string const next = lex.getString();
134 if (next == "ignore")
135 font.setLanguage(ignore_language);
137 font.setLanguage(languages.getLanguage(next));
139 } else if (token == "toggleall") {
140 toggle = lex.getBool();
143 // Unrecognised token
153 string const freefont2string()
156 if (font2string(freefont, toggleall, data))
162 void update_and_apply_freefont(BufferView * bv, string const & data)
166 if (string2font(data, font, toggle)) {
174 void apply_freefont(BufferView * bv)
176 toggleAndShow(bv, freefont, toggleall);
177 bv->owner()->view_state_changed();
178 bv->owner()->message(_("Character set"));
182 void emph(BufferView * bv)
184 LyXFont font(LyXFont::ALL_IGNORE);
185 font.setEmph(LyXFont::TOGGLE);
186 toggleAndShow(bv, font);
190 void bold(BufferView * bv)
192 LyXFont font(LyXFont::ALL_IGNORE);
193 font.setSeries(LyXFont::BOLD_SERIES);
194 toggleAndShow(bv, font);
198 void noun(BufferView * bv)
200 LyXFont font(LyXFont::ALL_IGNORE);
201 font.setNoun(LyXFont::TOGGLE);
202 toggleAndShow(bv, font);
206 void number(BufferView * bv)
208 LyXFont font(LyXFont::ALL_IGNORE);
209 font.setNumber(LyXFont::TOGGLE);
210 toggleAndShow(bv, font);
214 void lang(BufferView * bv, string const & l)
216 Language const * lang = languages.getLanguage(l);
220 LyXFont font(LyXFont::ALL_IGNORE);
221 font.setLanguage(lang);
222 toggleAndShow(bv, font);
226 bool changeDepth(BufferView * bv, LyXText * text, DEPTH_CHANGE type, bool test_only)
228 if (!bv->available() || !text)
232 return text->changeDepth(type, true);
234 bool const changed = text->changeDepth(type, false);
235 if (text->inset_owner)
236 bv->updateInset(text->inset_owner);
241 void code(BufferView * bv)
243 LyXFont font(LyXFont::ALL_IGNORE);
244 font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
245 toggleAndShow(bv, font);
249 void sans(BufferView * bv)
251 LyXFont font(LyXFont::ALL_IGNORE);
252 font.setFamily(LyXFont::SANS_FAMILY);
253 toggleAndShow(bv, font);
257 void roman(BufferView * bv)
259 LyXFont font(LyXFont::ALL_IGNORE);
260 font.setFamily(LyXFont::ROMAN_FAMILY);
261 toggleAndShow(bv, font);
265 void styleReset(BufferView * bv)
267 LyXFont font(LyXFont::ALL_INHERIT, ignore_language);
268 toggleAndShow(bv, font);
272 void underline(BufferView * bv)
274 LyXFont font(LyXFont::ALL_IGNORE);
275 font.setUnderbar(LyXFont::TOGGLE);
276 toggleAndShow(bv, font);
280 void fontSize(BufferView * bv, string const & size)
282 LyXFont font(LyXFont::ALL_IGNORE);
283 font.setLyXSize(size);
284 toggleAndShow(bv, font);
288 // Returns the current font and depth as a message.
289 string const currentState(BufferView * bv)
291 if (!bv->available())
295 return mathcursor->info();
299 LyXText * text = bv->getLyXText();
300 Buffer * buffer = bv->buffer();
301 LyXCursor const & c(text->cursor);
303 bool const show_change = buffer->params().tracking_changes
304 && c.pos() != c.par()->size()
305 && c.par()->lookupChange(c.pos()) != Change::UNCHANGED;
308 Change change(c.par()->lookupChangeFull(c.pos()));
309 Author const & a(bv->buffer()->authors().get(change.author));
310 state << _("Change: ") << a.name();
311 if (!a.email().empty()) {
312 state << " (" << a.email() << ")";
314 if (change.changetime)
315 state << _(" at ") << ctime(&change.changetime);
319 // I think we should only show changes from the default
321 LyXFont font = text->real_current_font;
322 LyXFont const & defaultfont =
323 buffer->params().getLyXTextClass().defaultfont();
324 font.reduce(defaultfont);
326 // avoid _(...) re-entrance problem
327 string const s = font.stateText(&buffer->params());
328 state << bformat(_("Font: %1$s"), s);
330 // state << bformat(_("Font: %1$s"), font.stateText(&buffer->params));
332 // The paragraph depth
333 int depth = text->getDepth();
335 state << bformat(_(", Depth: %1$s"), tostr(depth));
337 // The paragraph spacing, but only if different from
339 if (!text->cursor.par()->params().spacing().isDefault()) {
340 Spacing::Space cur_space =
341 text->cursor.par()->params().spacing().getSpace();
342 state << _(", Spacing: ");
345 case Spacing::Single:
346 state << _("Single");
348 case Spacing::Onehalf:
349 state << _("OneHalf");
351 case Spacing::Double:
352 state << _("Double");
355 state << _("Other (")
356 << text->cursor.par()->params().spacing().getValue()
359 case Spacing::Default:
360 // should never happen, do nothing
365 state << _(", Paragraph: ") << text->cursor.par()->id();
366 state << _(", Position: ") << text->cursor.pos();
367 RowList::iterator rit = text->cursorRow();
368 state << bformat(_(", Row b:%1$d e:%2$d"), rit->pos(), rit->end());
369 state << _(", Inset: ") <<
370 (text->cursor.par()->inInset() ? text->cursor.par()->inInset()->id() : -1);
372 return STRCONV(state.str());
376 /* Does the actual toggle job of the calls above.
377 * Also shows the current font state.
379 void toggleAndShow(BufferView * bv, LyXFont const & font, bool toggleall)
381 if (!bv->available())
384 if (bv->theLockingInset()) {
385 bv->theLockingInset()->setFont(bv, font, toggleall);
389 LyXText * text = bv->getLyXText();
390 text->toggleFree(font, toggleall);
393 if (font.language() != ignore_language ||
394 font.number() != LyXFont::IGNORE) {
395 LyXCursor & cursor = text->cursor;
396 text->computeBidiTables(text->cursor.par(), *bv->buffer(),
398 if (cursor.boundary() !=
399 text->isBoundary(*bv->buffer(), *cursor.par(), cursor.pos(),
400 text->real_current_font))
401 text->setCursor(cursor.par(), cursor.pos(),
402 false, !cursor.boundary());
407 // deletes a selection during an insertion
408 void replaceSelection(LyXText * lt)
410 if (lt->selection.set()) {
411 lt->cutSelection(true, false);
416 }; // namespace bv_funcs