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 "support/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);
83 token = lex.getString();
85 if (token.empty() || !lex.next())
88 if (token == "family") {
89 int const next = lex.getInteger();
90 font.setFamily(LyXFont::FONT_FAMILY(next));
92 } else if (token == "series") {
93 int const next = lex.getInteger();
94 font.setSeries(LyXFont::FONT_SERIES(next));
96 } else if (token == "shape") {
97 int const next = lex.getInteger();
98 font.setShape(LyXFont::FONT_SHAPE(next));
100 } else if (token == "size") {
101 int const next = lex.getInteger();
102 font.setSize(LyXFont::FONT_SIZE(next));
104 } else if (token == "emph" || token == "underbar" ||
105 token == "noun" || token == "number") {
107 int const next = lex.getInteger();
108 LyXFont::FONT_MISC_STATE const misc =
109 LyXFont::FONT_MISC_STATE(next);
113 else if (token == "underbar")
114 font.setUnderbar(misc);
115 else if (token == "noun")
117 else if (token == "number")
118 font.setNumber(misc);
120 } else if (token == "color") {
121 int const next = lex.getInteger();
122 font.setColor(LColor::color(next));
124 } else if (token == "language") {
125 string const next = lex.getString();
126 if (next == "ignore")
127 font.setLanguage(ignore_language);
129 font.setLanguage(languages.getLanguage(next));
131 } else if (token == "toggleall") {
132 toggle = lex.getBool();
135 // Unrecognised token
145 string const freefont2string()
148 if (font2string(freefont, toggleall, data))
154 void update_and_apply_freefont(BufferView * bv, string const & data)
158 if (string2font(data, font, toggle)) {
166 void apply_freefont(BufferView * bv)
168 toggleAndShow(bv, freefont, toggleall);
169 bv->owner()->view_state_changed();
170 bv->owner()->message(_("Character set"));
174 void emph(BufferView * bv)
176 LyXFont font(LyXFont::ALL_IGNORE);
177 font.setEmph(LyXFont::TOGGLE);
178 toggleAndShow(bv, font);
182 void bold(BufferView * bv)
184 LyXFont font(LyXFont::ALL_IGNORE);
185 font.setSeries(LyXFont::BOLD_SERIES);
186 toggleAndShow(bv, font);
190 void noun(BufferView * bv)
192 LyXFont font(LyXFont::ALL_IGNORE);
193 font.setNoun(LyXFont::TOGGLE);
194 toggleAndShow(bv, font);
198 void number(BufferView * bv)
200 LyXFont font(LyXFont::ALL_IGNORE);
201 font.setNumber(LyXFont::TOGGLE);
202 toggleAndShow(bv, font);
205 void lang(BufferView * bv, string const & l)
207 LyXFont font(LyXFont::ALL_IGNORE);
208 Language const * lang = languages.getLanguage(l);
212 font.setLanguage(lang);
213 toggleAndShow(bv, font);
217 // Change environment depth.
218 // if decInc >= 0, increment depth
219 // if decInc < 0, decrement depth
220 void changeDepth(BufferView * bv, LyXText * text, int decInc)
222 if (!bv->available() || !text)
226 bv->update(BufferView::SELECT);
231 if (text->inset_owner)
232 bv->updateInset((Inset *)text->inset_owner);
233 bv->update(BufferView::SELECT);
237 void code(BufferView * bv)
239 LyXFont font(LyXFont::ALL_IGNORE);
240 font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
241 toggleAndShow(bv, font);
245 void sans(BufferView * bv)
247 LyXFont font(LyXFont::ALL_IGNORE);
248 font.setFamily(LyXFont::SANS_FAMILY);
249 toggleAndShow(bv, font);
253 void roman(BufferView * bv)
255 LyXFont font(LyXFont::ALL_IGNORE);
256 font.setFamily(LyXFont::ROMAN_FAMILY);
257 toggleAndShow(bv, font);
261 void styleReset(BufferView * bv)
263 LyXFont font(LyXFont::ALL_INHERIT, ignore_language);
264 toggleAndShow(bv, font);
268 void underline(BufferView * bv)
270 LyXFont font(LyXFont::ALL_IGNORE);
271 font.setUnderbar(LyXFont::TOGGLE);
272 toggleAndShow(bv, font);
276 void fontSize(BufferView * bv, string const & size)
278 LyXFont font(LyXFont::ALL_IGNORE);
279 font.setLyXSize(size);
280 toggleAndShow(bv, font);
284 // Returns the current font and depth as a message.
285 string const currentState(BufferView * bv)
287 if (!bv->available())
292 LyXText * text = bv->getLyXText();
293 Buffer * buffer = bv->buffer();
294 LyXCursor const & c(text->cursor);
296 bool const show_change = buffer->params.tracking_changes
297 && c.pos() != c.par()->size()
298 && c.par()->lookupChange(c.pos()) != Change::UNCHANGED;
301 Change change(c.par()->lookupChangeFull(c.pos()));
302 Author const & a(bv->buffer()->authors().get(change.author));
303 state << _("Change: ") << a.name();
304 if (!a.email().empty()) {
305 state << " (" << a.email() << ")";
307 if (change.changetime)
308 state << _(" at ") << ctime(&change.changetime);
312 // I think we should only show changes from the default
314 LyXFont font = text->real_current_font;
315 LyXFont const & defaultfont =
316 buffer->params.getLyXTextClass().defaultfont();
317 font.reduce(defaultfont);
320 state << boost::format(_("Font: %1$s")) % font.stateText(&buffer->params);
322 state << _("Font: ") << font.stateText(&buffer->params);
325 // The paragraph depth
326 int depth = text->getDepth();
329 state << boost::format(_(", Depth: %1$d")) % depth;
331 state << _(", Depth: ") << 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();
366 return STRCONV(state.str());
370 /* Does the actual toggle job of the calls above.
371 * Also shows the current font state.
373 void toggleAndShow(BufferView * bv, LyXFont const & font, bool toggleall)
375 if (!bv->available())
378 if (bv->theLockingInset()) {
379 bv->theLockingInset()->setFont(bv, font, toggleall);
383 LyXText * text = bv->getLyXText();
384 // FIXME: can this happen ??
389 bv->update(text, BufferView::SELECT);
390 text->toggleFree(font, toggleall);
391 bv->update(text, BufferView::SELECT);
393 if (font.language() != ignore_language ||
394 font.number() != LyXFont::IGNORE) {
395 LyXCursor & cursor = text->cursor;
396 text->computeBidiTables(bv->buffer(), cursor.row());
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());