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"
33 #include "support/lstrings.h"
36 #include "insets/updatableinset.h"
38 #include "BoostFormat.h"
41 LyXFont freefont(LyXFont::ALL_IGNORE);
42 bool toggleall(false);
46 // Set data using font and toggle
47 // If successful, returns true
48 bool font2string(LyXFont const & font, bool toggle, string & data)
50 string lang = "ignore";
52 lang = font.language()->lang();
55 os << "family " << font.family() << '\n'
56 << "series " << font.series() << '\n'
57 << "shape " << font.shape() << '\n'
58 << "size " << font.size() << '\n'
59 << "emph " << font.emph() << '\n'
60 << "underbar " << font.underbar() << '\n'
61 << "noun " << font.noun() << '\n'
62 << "number " << font.number() << '\n'
63 << "color " << font.color() << '\n'
64 << "language " << lang << '\n'
65 << "toggleall " << tostr(toggle);
71 // Set font and toggle using data
72 // If successful, returns true
73 bool string2font(string const & data, LyXFont & font, bool & toggle)
75 istringstream is(data);
86 string const token = lex.getString();
88 if (token == "family" ||
93 token == "underbar" ||
98 Int = lex.getInteger();
99 } else if (token == "language") {
101 String = lex.getString();
102 } else if (token == "toggleall") {
104 Bool = lex.getBool();
106 // Unrecognised token
114 if (token == "family") {
115 font.setFamily(static_cast<LyXFont::FONT_FAMILY>(Int));
117 } else if (token == "series") {
118 font.setSeries(static_cast<LyXFont::FONT_SERIES>(Int));
120 } else if (token == "shape") {
121 font.setShape(static_cast<LyXFont::FONT_SHAPE>(Int));
123 } else if (token == "size") {
124 font.setSize(static_cast<LyXFont::FONT_SIZE>(Int));
126 } else if (token == "emph") {
127 font.setEmph(static_cast<LyXFont::FONT_MISC_STATE>(Int));
129 } else if (token == "underbar") {
130 font.setUnderbar(static_cast<LyXFont::FONT_MISC_STATE>(Int));
132 } else if (token == "noun") {
133 font.setNoun(static_cast<LyXFont::FONT_MISC_STATE>(Int));
135 } else if (token == "number") {
136 font.setNumber(static_cast<LyXFont::FONT_MISC_STATE>(Int));
138 } else if (token == "color") {
139 font.setColor(static_cast<LColor::color>(Int));
141 } else if (token == "language") {
142 if (String == "ignore")
143 font.setLanguage(ignore_language);
145 font.setLanguage(languages.getLanguage(String));
147 } else if (token == "toggleall") {
155 string const freefont2string()
158 if (font2string(freefont, toggleall, data))
164 void update_and_apply_freefont(BufferView * bv, string const & data)
168 if (string2font(data, font, toggle)) {
176 void apply_freefont(BufferView * bv)
178 toggleAndShow(bv, freefont, toggleall);
179 bv->owner()->view_state_changed();
180 bv->owner()->message(_("Character set"));
184 void emph(BufferView * bv)
186 LyXFont font(LyXFont::ALL_IGNORE);
187 font.setEmph(LyXFont::TOGGLE);
188 toggleAndShow(bv, font);
192 void bold(BufferView * bv)
194 LyXFont font(LyXFont::ALL_IGNORE);
195 font.setSeries(LyXFont::BOLD_SERIES);
196 toggleAndShow(bv, font);
200 void noun(BufferView * bv)
202 LyXFont font(LyXFont::ALL_IGNORE);
203 font.setNoun(LyXFont::TOGGLE);
204 toggleAndShow(bv, font);
208 void number(BufferView * bv)
210 LyXFont font(LyXFont::ALL_IGNORE);
211 font.setNumber(LyXFont::TOGGLE);
212 toggleAndShow(bv, font);
215 void lang(BufferView * bv, string const & l)
217 LyXFont font(LyXFont::ALL_IGNORE);
218 Language const * lang = languages.getLanguage(l);
222 font.setLanguage(lang);
223 toggleAndShow(bv, font);
227 // Change environment depth.
228 // if decInc >= 0, increment depth
229 // if decInc < 0, decrement depth
230 void changeDepth(BufferView * bv, LyXText * text, int decInc)
232 if (!bv->available() || !text)
236 bv->update(BufferView::SELECT);
241 if (text->inset_owner)
242 bv->updateInset((Inset *)text->inset_owner);
243 bv->update(BufferView::SELECT);
247 void code(BufferView * bv)
249 LyXFont font(LyXFont::ALL_IGNORE);
250 font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
251 toggleAndShow(bv, font);
255 void sans(BufferView * bv)
257 LyXFont font(LyXFont::ALL_IGNORE);
258 font.setFamily(LyXFont::SANS_FAMILY);
259 toggleAndShow(bv, font);
263 void roman(BufferView * bv)
265 LyXFont font(LyXFont::ALL_IGNORE);
266 font.setFamily(LyXFont::ROMAN_FAMILY);
267 toggleAndShow(bv, font);
271 void styleReset(BufferView * bv)
273 LyXFont font(LyXFont::ALL_INHERIT, ignore_language);
274 toggleAndShow(bv, font);
278 void underline(BufferView * bv)
280 LyXFont font(LyXFont::ALL_IGNORE);
281 font.setUnderbar(LyXFont::TOGGLE);
282 toggleAndShow(bv, font);
286 void fontSize(BufferView * bv, string const & size)
288 LyXFont font(LyXFont::ALL_IGNORE);
289 font.setLyXSize(size);
290 toggleAndShow(bv, font);
294 // Returns the current font and depth as a message.
295 string const currentState(BufferView * bv)
297 if (!bv->available())
302 LyXText * text = bv->getLyXText();
303 Buffer * buffer = bv->buffer();
304 LyXCursor const & c(text->cursor);
306 bool const show_change = buffer->params.tracking_changes
307 && c.pos() != c.par()->size()
308 && c.par()->lookupChange(c.pos()) != Change::UNCHANGED;
311 Change change(c.par()->lookupChangeFull(c.pos()));
312 Author const & a(bv->buffer()->authors().get(change.author));
313 state << _("Change: ") << a.name();
314 if (!a.email().empty()) {
315 state << " (" << a.email() << ")";
317 if (change.changetime)
318 state << _(" at ") << ctime(&change.changetime);
322 // I think we should only show changes from the default
324 LyXFont font = text->real_current_font;
325 LyXFont const & defaultfont =
326 buffer->params.getLyXTextClass().defaultfont();
327 font.reduce(defaultfont);
330 state << boost::format(_("Font: %1$s")) % font.stateText(&buffer->params);
332 state << _("Font: ") << font.stateText(&buffer->params);
335 // The paragraph depth
336 int depth = text->getDepth();
339 state << boost::format(_(", Depth: %1$d")) % depth;
341 state << _(", Depth: ") << depth;
346 // The paragraph spacing, but only if different from
348 if (!text->cursor.par()->params().spacing().isDefault()) {
349 Spacing::Space cur_space =
350 text->cursor.par()->params().spacing().getSpace();
351 state << _(", Spacing: ");
354 case Spacing::Single:
355 state << _("Single");
357 case Spacing::Onehalf:
358 state << _("OneHalf");
360 case Spacing::Double:
361 state << _("Double");
364 state << _("Other (")
365 << text->cursor.par()->params().spacing().getValue()
368 case Spacing::Default:
369 // should never happen, do nothing
374 state << _(", Paragraph: ") << text->cursor.par()->id();
376 return STRCONV(state.str());
380 /* Does the actual toggle job of the calls above.
381 * Also shows the current font state.
383 void toggleAndShow(BufferView * bv, LyXFont const & font, bool toggleall)
385 if (!bv->available())
388 if (bv->theLockingInset()) {
389 bv->theLockingInset()->setFont(bv, font, toggleall);
393 LyXText * text = bv->getLyXText();
394 // FIXME: can this happen ??
399 bv->update(text, BufferView::SELECT);
400 text->toggleFree(font, toggleall);
401 bv->update(text, BufferView::SELECT);
403 if (font.language() != ignore_language ||
404 font.number() != LyXFont::IGNORE) {
405 LyXCursor & cursor = text->cursor;
406 text->computeBidiTables(bv->buffer(), cursor.row());
407 if (cursor.boundary() !=
408 text->isBoundary(bv->buffer(), cursor.par(), cursor.pos(),
409 text->real_current_font))
410 text->setCursor(cursor.par(), cursor.pos(),
411 false, !cursor.boundary());