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"
17 #include "BufferView.h"
18 #include "paragraph.h"
26 #include "ParagraphParameters.h"
30 #include "frontends/LyXView.h"
31 #include "frontends/Alert.h"
32 #include "mathed/math_cursor.h"
34 #include "support/tostr.h"
37 #include "insets/updatableinset.h"
38 #include <boost/bind.hpp>
41 using namespace lyx::support;
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)
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 << " Inset: " <<
367 (text->cursor.par()->inInset() ? text->cursor.par()->inInset()->id() : -1);
369 return STRCONV(state.str());
373 /* Does the actual toggle job of the calls above.
374 * Also shows the current font state.
376 void toggleAndShow(BufferView * bv, LyXFont const & font, bool toggleall)
378 if (!bv->available())
381 if (bv->theLockingInset()) {
382 bv->theLockingInset()->setFont(bv, font, toggleall);
386 LyXText * text = bv->getLyXText();
387 text->toggleFree(font, toggleall);
390 if (font.language() != ignore_language ||
391 font.number() != LyXFont::IGNORE) {
392 LyXCursor & cursor = text->cursor;
393 text->computeBidiTables(text->cursor.par(), bv->buffer(),
395 if (cursor.boundary() !=
396 text->isBoundary(bv->buffer(), *cursor.par(), cursor.pos(),
397 text->real_current_font))
398 text->setCursor(cursor.par(), cursor.pos(),
399 false, !cursor.boundary());
404 // deletes a selection during an insertion
405 void replaceSelection(LyXText * lt)
407 if (lt->selection.set()) {
408 lt->cutSelection(true, false);
413 }; // namespace bv_funcs