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 "BufferView.h"
24 #include "paragraph.h"
25 #include "ParagraphParameters.h"
27 #include "frontends/Alert.h"
28 #include "frontends/LyXView.h"
30 #include "insets/insettext.h"
32 #include "mathed/math_cursor.h"
34 #include "support/tostr.h"
36 #include "support/std_sstream.h"
38 using namespace lyx::support;
40 using std::istringstream;
41 using std::ostringstream;
46 LyXFont freefont(LyXFont::ALL_IGNORE);
47 bool toggleall(false);
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);
73 data = STRCONV(os.str());
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(STRCONV(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 string const freefont2string()
155 if (font2string(freefont, toggleall, data))
161 void update_and_apply_freefont(BufferView * bv, string const & data)
165 if (string2font(data, font, toggle)) {
173 void apply_freefont(BufferView * bv)
175 toggleAndShow(bv, freefont, toggleall);
176 bv->owner()->view_state_changed();
177 bv->owner()->message(_("Character set"));
181 void emph(BufferView * bv)
183 LyXFont font(LyXFont::ALL_IGNORE);
184 font.setEmph(LyXFont::TOGGLE);
185 toggleAndShow(bv, font);
189 void bold(BufferView * bv)
191 LyXFont font(LyXFont::ALL_IGNORE);
192 font.setSeries(LyXFont::BOLD_SERIES);
193 toggleAndShow(bv, font);
197 void noun(BufferView * bv)
199 LyXFont font(LyXFont::ALL_IGNORE);
200 font.setNoun(LyXFont::TOGGLE);
201 toggleAndShow(bv, font);
205 void number(BufferView * bv)
207 LyXFont font(LyXFont::ALL_IGNORE);
208 font.setNumber(LyXFont::TOGGLE);
209 toggleAndShow(bv, font);
213 void lang(BufferView * bv, string const & l)
215 Language const * lang = languages.getLanguage(l);
219 LyXFont font(LyXFont::ALL_IGNORE);
220 font.setLanguage(lang);
221 toggleAndShow(bv, font);
225 bool changeDepth(BufferView * bv, LyXText * text, DEPTH_CHANGE type, bool test_only)
227 if (!bv->available() || !text)
231 return text->changeDepth(type, true);
233 bool const changed = text->changeDepth(type, false);
234 if (text->inset_owner)
235 bv->updateInset(text->inset_owner);
240 void code(BufferView * bv)
242 LyXFont font(LyXFont::ALL_IGNORE);
243 font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
244 toggleAndShow(bv, font);
248 void sans(BufferView * bv)
250 LyXFont font(LyXFont::ALL_IGNORE);
251 font.setFamily(LyXFont::SANS_FAMILY);
252 toggleAndShow(bv, font);
256 void roman(BufferView * bv)
258 LyXFont font(LyXFont::ALL_IGNORE);
259 font.setFamily(LyXFont::ROMAN_FAMILY);
260 toggleAndShow(bv, font);
264 void styleReset(BufferView * bv)
266 LyXFont font(LyXFont::ALL_INHERIT, ignore_language);
267 toggleAndShow(bv, font);
271 void underline(BufferView * bv)
273 LyXFont font(LyXFont::ALL_IGNORE);
274 font.setUnderbar(LyXFont::TOGGLE);
275 toggleAndShow(bv, font);
279 void fontSize(BufferView * bv, string const & size)
281 LyXFont font(LyXFont::ALL_IGNORE);
282 font.setLyXSize(size);
283 toggleAndShow(bv, font);
287 // Returns the current font and depth as a message.
288 string const currentState(BufferView * bv)
290 if (!bv->available())
294 return mathcursor->info();
298 LyXText * text = bv->getLyXText();
299 Buffer * buffer = bv->buffer();
300 LyXCursor const & c(text->cursor);
302 bool const show_change = buffer->params().tracking_changes
303 && c.pos() != c.par()->size()
304 && c.par()->lookupChange(c.pos()) != Change::UNCHANGED;
307 Change change(c.par()->lookupChangeFull(c.pos()));
308 Author const & a(bv->buffer()->authors().get(change.author));
309 state << _("Change: ") << a.name();
310 if (!a.email().empty()) {
311 state << " (" << a.email() << ")";
313 if (change.changetime)
314 state << _(" at ") << ctime(&change.changetime);
318 // I think we should only show changes from the default
320 LyXFont font = text->real_current_font;
321 LyXFont const & defaultfont =
322 buffer->params().getLyXTextClass().defaultfont();
323 font.reduce(defaultfont);
325 // avoid _(...) re-entrance problem
326 string const s = font.stateText(&buffer->params());
327 state << bformat(_("Font: %1$s"), s);
329 // state << bformat(_("Font: %1$s"), font.stateText(&buffer->params));
331 // The paragraph depth
332 int depth = text->getDepth();
334 state << bformat(_(", Depth: %1$s"), tostr(depth));
336 // The paragraph spacing, but only if different from
338 if (!text->cursor.par()->params().spacing().isDefault()) {
339 Spacing::Space cur_space =
340 text->cursor.par()->params().spacing().getSpace();
341 state << _(", Spacing: ");
344 case Spacing::Single:
345 state << _("Single");
347 case Spacing::Onehalf:
348 state << _("OneHalf");
350 case Spacing::Double:
351 state << _("Double");
354 state << _("Other (")
355 << text->cursor.par()->params().spacing().getValue()
358 case Spacing::Default:
359 // should never happen, do nothing
364 state << _(", Paragraph: ") << text->cursor.par()->id();
365 state << _(", Position: ") << text->cursor.pos();
366 RowList::iterator rit = text->cursorRow();
367 state << bformat(_(", Row b:%1$d e:%2$d"), rit->pos(), rit->end());
368 state << _(", Inset: ") <<
369 (text->cursor.par()->inInset() ? text->cursor.par()->inInset()->id() : -1);
371 return STRCONV(state.str());
375 /* Does the actual toggle job of the calls above.
376 * Also shows the current font state.
378 void toggleAndShow(BufferView * bv, LyXFont const & font, bool toggleall)
380 if (!bv->available())
383 if (bv->theLockingInset()) {
384 bv->theLockingInset()->setFont(bv, font, toggleall);
388 LyXText * text = bv->getLyXText();
389 text->toggleFree(font, toggleall);
392 if (font.language() != ignore_language ||
393 font.number() != LyXFont::IGNORE) {
394 LyXCursor & cursor = text->cursor;
395 text->computeBidiTables(text->cursor.par(), *bv->buffer(),
397 if (cursor.boundary() !=
398 text->isBoundary(*bv->buffer(), *cursor.par(), cursor.pos(),
399 text->real_current_font))
400 text->setCursor(cursor.par(), cursor.pos(),
401 false, !cursor.boundary());
406 // deletes a selection during an insertion
407 void replaceSelection(LyXText * lt)
409 if (lt->selection.set()) {
410 lt->cutSelection(true, false);
415 }; // namespace bv_funcs