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"
22 #include "ParagraphParameters.h"
24 #include "frontends/LyXView.h"
25 #include "frontends/Alert.h"
26 #include "mathed/math_cursor.h"
28 #include "support/tostr.h"
29 #include "support/std_sstream.h"
31 #include "insets/insettext.h"
33 using namespace lyx::support;
38 LyXFont freefont(LyXFont::ALL_IGNORE);
39 bool toggleall(false);
45 // Set data using font and toggle
46 // If successful, returns true
47 bool font2string(LyXFont const & font, bool toggle, string & data)
49 string lang = "ignore";
51 lang = font.language()->lang();
54 os << "family " << font.family() << '\n'
55 << "series " << font.series() << '\n'
56 << "shape " << font.shape() << '\n'
57 << "size " << font.size() << '\n'
58 << "emph " << font.emph() << '\n'
59 << "underbar " << font.underbar() << '\n'
60 << "noun " << font.noun() << '\n'
61 << "number " << font.number() << '\n'
62 << "color " << font.color() << '\n'
63 << "language " << lang << '\n'
64 << "toggleall " << tostr(toggle);
65 data = STRCONV(os.str());
70 // Set font and toggle using data
71 // If successful, returns true
72 bool string2font(string const & data, LyXFont & font, bool & toggle)
74 istringstream is(STRCONV(data));
82 token = lex.getString();
84 if (token.empty() || !lex.next())
87 if (token == "family") {
88 int const next = lex.getInteger();
89 font.setFamily(LyXFont::FONT_FAMILY(next));
91 } else if (token == "series") {
92 int const next = lex.getInteger();
93 font.setSeries(LyXFont::FONT_SERIES(next));
95 } else if (token == "shape") {
96 int const next = lex.getInteger();
97 font.setShape(LyXFont::FONT_SHAPE(next));
99 } else if (token == "size") {
100 int const next = lex.getInteger();
101 font.setSize(LyXFont::FONT_SIZE(next));
103 } else if (token == "emph" || token == "underbar" ||
104 token == "noun" || token == "number") {
106 int const next = lex.getInteger();
107 LyXFont::FONT_MISC_STATE const misc =
108 LyXFont::FONT_MISC_STATE(next);
112 else if (token == "underbar")
113 font.setUnderbar(misc);
114 else if (token == "noun")
116 else if (token == "number")
117 font.setNumber(misc);
119 } else if (token == "color") {
120 int const next = lex.getInteger();
121 font.setColor(LColor::color(next));
123 } else if (token == "language") {
124 string const next = lex.getString();
125 if (next == "ignore")
126 font.setLanguage(ignore_language);
128 font.setLanguage(languages.getLanguage(next));
130 } else if (token == "toggleall") {
131 toggle = lex.getBool();
134 // Unrecognised token
144 string const freefont2string()
147 if (font2string(freefont, toggleall, data))
153 void update_and_apply_freefont(BufferView * bv, string const & data)
157 if (string2font(data, font, toggle)) {
165 void apply_freefont(BufferView * bv)
167 toggleAndShow(bv, freefont, toggleall);
168 bv->owner()->view_state_changed();
169 bv->owner()->message(_("Character set"));
173 void emph(BufferView * bv)
175 LyXFont font(LyXFont::ALL_IGNORE);
176 font.setEmph(LyXFont::TOGGLE);
177 toggleAndShow(bv, font);
181 void bold(BufferView * bv)
183 LyXFont font(LyXFont::ALL_IGNORE);
184 font.setSeries(LyXFont::BOLD_SERIES);
185 toggleAndShow(bv, font);
189 void noun(BufferView * bv)
191 LyXFont font(LyXFont::ALL_IGNORE);
192 font.setNoun(LyXFont::TOGGLE);
193 toggleAndShow(bv, font);
197 void number(BufferView * bv)
199 LyXFont font(LyXFont::ALL_IGNORE);
200 font.setNumber(LyXFont::TOGGLE);
201 toggleAndShow(bv, font);
205 void lang(BufferView * bv, string const & l)
207 Language const * lang = languages.getLanguage(l);
211 LyXFont font(LyXFont::ALL_IGNORE);
212 font.setLanguage(lang);
213 toggleAndShow(bv, font);
217 bool changeDepth(BufferView * bv, LyXText * text, DEPTH_CHANGE type, bool test_only)
219 if (!bv->available() || !text)
223 return text->changeDepth(type, true);
225 bool const changed = text->changeDepth(type, false);
226 if (text->inset_owner)
227 bv->updateInset(text->inset_owner);
232 void code(BufferView * bv)
234 LyXFont font(LyXFont::ALL_IGNORE);
235 font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
236 toggleAndShow(bv, font);
240 void sans(BufferView * bv)
242 LyXFont font(LyXFont::ALL_IGNORE);
243 font.setFamily(LyXFont::SANS_FAMILY);
244 toggleAndShow(bv, font);
248 void roman(BufferView * bv)
250 LyXFont font(LyXFont::ALL_IGNORE);
251 font.setFamily(LyXFont::ROMAN_FAMILY);
252 toggleAndShow(bv, font);
256 void styleReset(BufferView * bv)
258 LyXFont font(LyXFont::ALL_INHERIT, ignore_language);
259 toggleAndShow(bv, font);
263 void underline(BufferView * bv)
265 LyXFont font(LyXFont::ALL_IGNORE);
266 font.setUnderbar(LyXFont::TOGGLE);
267 toggleAndShow(bv, font);
271 void fontSize(BufferView * bv, string const & size)
273 LyXFont font(LyXFont::ALL_IGNORE);
274 font.setLyXSize(size);
275 toggleAndShow(bv, font);
279 // Returns the current font and depth as a message.
280 string const currentState(BufferView * bv)
282 if (!bv->available())
286 return mathcursor->info();
290 LyXText * text = bv->getLyXText();
291 Buffer * buffer = bv->buffer();
292 LyXCursor const & c(text->cursor);
294 bool const show_change = buffer->params.tracking_changes
295 && c.pos() != c.par()->size()
296 && c.par()->lookupChange(c.pos()) != Change::UNCHANGED;
299 Change change(c.par()->lookupChangeFull(c.pos()));
300 Author const & a(bv->buffer()->authors().get(change.author));
301 state << _("Change: ") << a.name();
302 if (!a.email().empty()) {
303 state << " (" << a.email() << ")";
305 if (change.changetime)
306 state << _(" at ") << ctime(&change.changetime);
310 // I think we should only show changes from the default
312 LyXFont font = text->real_current_font;
313 LyXFont const & defaultfont =
314 buffer->params.getLyXTextClass().defaultfont();
315 font.reduce(defaultfont);
317 // avoid _(...) re-entrance problem
318 string const s = font.stateText(&buffer->params);
319 state << bformat(_("Font: %1$s"), s);
321 // state << bformat(_("Font: %1$s"), font.stateText(&buffer->params));
323 // The paragraph depth
324 int depth = text->getDepth();
326 state << bformat(_(", Depth: %1$s"), tostr(depth));
328 // The paragraph spacing, but only if different from
330 if (!text->cursor.par()->params().spacing().isDefault()) {
331 Spacing::Space cur_space =
332 text->cursor.par()->params().spacing().getSpace();
333 state << _(", Spacing: ");
336 case Spacing::Single:
337 state << _("Single");
339 case Spacing::Onehalf:
340 state << _("OneHalf");
342 case Spacing::Double:
343 state << _("Double");
346 state << _("Other (")
347 << text->cursor.par()->params().spacing().getValue()
350 case Spacing::Default:
351 // should never happen, do nothing
356 state << _(", Paragraph: ") << text->cursor.par()->id();
357 state << _(", Position: ") << text->cursor.pos();
358 RowList::iterator rit = text->cursorRow();
359 state << bformat(_(", Row b:%1$d e:%2$d"), rit->pos(), rit->end());
360 state << _(", Inset: ") <<
361 (text->cursor.par()->inInset() ? text->cursor.par()->inInset()->id() : -1);
363 return STRCONV(state.str());
367 /* Does the actual toggle job of the calls above.
368 * Also shows the current font state.
370 void toggleAndShow(BufferView * bv, LyXFont const & font, bool toggleall)
372 if (!bv->available())
375 if (bv->theLockingInset()) {
376 bv->theLockingInset()->setFont(bv, font, toggleall);
380 LyXText * text = bv->getLyXText();
381 text->toggleFree(font, toggleall);
384 if (font.language() != ignore_language ||
385 font.number() != LyXFont::IGNORE) {
386 LyXCursor & cursor = text->cursor;
387 text->computeBidiTables(text->cursor.par(), *bv->buffer(),
389 if (cursor.boundary() !=
390 text->isBoundary(*bv->buffer(), *cursor.par(), cursor.pos(),
391 text->real_current_font))
392 text->setCursor(cursor.par(), cursor.pos(),
393 false, !cursor.boundary());
398 // deletes a selection during an insertion
399 void replaceSelection(LyXText * lt)
401 if (lt->selection.set()) {
402 lt->cutSelection(true, false);
407 }; // namespace bv_funcs