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/lstrings.h"
37 #include "insets/updatableinset.h"
39 #include "support/BoostFormat.h"
43 LyXFont freefont(LyXFont::ALL_IGNORE);
44 bool toggleall(false);
50 // Set data using font and toggle
51 // If successful, returns true
52 bool font2string(LyXFont const & font, bool toggle, string & data)
54 string lang = "ignore";
56 lang = font.language()->lang();
59 os << "family " << font.family() << '\n'
60 << "series " << font.series() << '\n'
61 << "shape " << font.shape() << '\n'
62 << "size " << font.size() << '\n'
63 << "emph " << font.emph() << '\n'
64 << "underbar " << font.underbar() << '\n'
65 << "noun " << font.noun() << '\n'
66 << "number " << font.number() << '\n'
67 << "color " << font.color() << '\n'
68 << "language " << lang << '\n'
69 << "toggleall " << tostr(toggle);
75 // Set font and toggle using data
76 // If successful, returns true
77 bool string2font(string const & data, LyXFont & font, bool & toggle)
79 istringstream is(data);
87 token = lex.getString();
89 if (token.empty() || !lex.next())
92 if (token == "family") {
93 int const next = lex.getInteger();
94 font.setFamily(LyXFont::FONT_FAMILY(next));
96 } else if (token == "series") {
97 int const next = lex.getInteger();
98 font.setSeries(LyXFont::FONT_SERIES(next));
100 } else if (token == "shape") {
101 int const next = lex.getInteger();
102 font.setShape(LyXFont::FONT_SHAPE(next));
104 } else if (token == "size") {
105 int const next = lex.getInteger();
106 font.setSize(LyXFont::FONT_SIZE(next));
108 } else if (token == "emph" || token == "underbar" ||
109 token == "noun" || token == "number") {
111 int const next = lex.getInteger();
112 LyXFont::FONT_MISC_STATE const misc =
113 LyXFont::FONT_MISC_STATE(next);
117 else if (token == "underbar")
118 font.setUnderbar(misc);
119 else if (token == "noun")
121 else if (token == "number")
122 font.setNumber(misc);
124 } else if (token == "color") {
125 int const next = lex.getInteger();
126 font.setColor(LColor::color(next));
128 } else if (token == "language") {
129 string const next = lex.getString();
130 if (next == "ignore")
131 font.setLanguage(ignore_language);
133 font.setLanguage(languages.getLanguage(next));
135 } else if (token == "toggleall") {
136 toggle = lex.getBool();
139 // Unrecognised token
149 string const freefont2string()
152 if (font2string(freefont, toggleall, data))
158 void update_and_apply_freefont(BufferView * bv, string const & data)
162 if (string2font(data, font, toggle)) {
170 void apply_freefont(BufferView * bv)
172 toggleAndShow(bv, freefont, toggleall);
173 bv->owner()->view_state_changed();
174 bv->owner()->message(_("Character set"));
178 void emph(BufferView * bv)
180 LyXFont font(LyXFont::ALL_IGNORE);
181 font.setEmph(LyXFont::TOGGLE);
182 toggleAndShow(bv, font);
186 void bold(BufferView * bv)
188 LyXFont font(LyXFont::ALL_IGNORE);
189 font.setSeries(LyXFont::BOLD_SERIES);
190 toggleAndShow(bv, font);
194 void noun(BufferView * bv)
196 LyXFont font(LyXFont::ALL_IGNORE);
197 font.setNoun(LyXFont::TOGGLE);
198 toggleAndShow(bv, font);
202 void number(BufferView * bv)
204 LyXFont font(LyXFont::ALL_IGNORE);
205 font.setNumber(LyXFont::TOGGLE);
206 toggleAndShow(bv, font);
209 void lang(BufferView * bv, string const & l)
211 LyXFont font(LyXFont::ALL_IGNORE);
212 Language const * lang = languages.getLanguage(l);
216 font.setLanguage(lang);
217 toggleAndShow(bv, font);
221 bool changeDepth(BufferView * bv, LyXText * text, DEPTH_CHANGE type, bool test_only)
223 if (!bv->available() || !text)
227 return text->changeDepth(type, true);
229 bv->update(BufferView::SELECT);
230 bool const changed = text->changeDepth(type, false);
231 if (text->inset_owner)
232 bv->updateInset((Inset *)text->inset_owner);
233 bv->update(BufferView::SELECT);
238 void code(BufferView * bv)
240 LyXFont font(LyXFont::ALL_IGNORE);
241 font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
242 toggleAndShow(bv, font);
246 void sans(BufferView * bv)
248 LyXFont font(LyXFont::ALL_IGNORE);
249 font.setFamily(LyXFont::SANS_FAMILY);
250 toggleAndShow(bv, font);
254 void roman(BufferView * bv)
256 LyXFont font(LyXFont::ALL_IGNORE);
257 font.setFamily(LyXFont::ROMAN_FAMILY);
258 toggleAndShow(bv, font);
262 void styleReset(BufferView * bv)
264 LyXFont font(LyXFont::ALL_INHERIT, ignore_language);
265 toggleAndShow(bv, font);
269 void underline(BufferView * bv)
271 LyXFont font(LyXFont::ALL_IGNORE);
272 font.setUnderbar(LyXFont::TOGGLE);
273 toggleAndShow(bv, font);
277 void fontSize(BufferView * bv, string const & size)
279 LyXFont font(LyXFont::ALL_IGNORE);
280 font.setLyXSize(size);
281 toggleAndShow(bv, font);
285 // Returns the current font and depth as a message.
286 string const currentState(BufferView * bv)
288 if (!bv->available())
292 return mathcursor->info();
296 LyXText * text = bv->getLyXText();
297 Buffer * buffer = bv->buffer();
298 LyXCursor const & c(text->cursor);
300 bool const show_change = buffer->params.tracking_changes
301 && c.pos() != c.par()->size()
302 && c.par()->lookupChange(c.pos()) != Change::UNCHANGED;
305 Change change(c.par()->lookupChangeFull(c.pos()));
306 Author const & a(bv->buffer()->authors().get(change.author));
307 state << _("Change: ") << a.name();
308 if (!a.email().empty()) {
309 state << " (" << a.email() << ")";
311 if (change.changetime)
312 state << _(" at ") << ctime(&change.changetime);
316 // I think we should only show changes from the default
318 LyXFont font = text->real_current_font;
319 LyXFont const & defaultfont =
320 buffer->params.getLyXTextClass().defaultfont();
321 font.reduce(defaultfont);
324 state << boost::format(_("Font: %1$s")) % font.stateText(&buffer->params);
326 state << _("Font: ") << font.stateText(&buffer->params);
329 // The paragraph depth
330 int depth = text->getDepth();
333 state << boost::format(_(", Depth: %1$d")) % depth;
335 state << _(", Depth: ") << depth;
340 // The paragraph spacing, but only if different from
342 if (!text->cursor.par()->params().spacing().isDefault()) {
343 Spacing::Space cur_space =
344 text->cursor.par()->params().spacing().getSpace();
345 state << _(", Spacing: ");
348 case Spacing::Single:
349 state << _("Single");
351 case Spacing::Onehalf:
352 state << _("OneHalf");
354 case Spacing::Double:
355 state << _("Double");
358 state << _("Other (")
359 << text->cursor.par()->params().spacing().getValue()
362 case Spacing::Default:
363 // should never happen, do nothing
368 state << _(", Paragraph: ") << text->cursor.par()->id();
370 return STRCONV(state.str());
374 /* Does the actual toggle job of the calls above.
375 * Also shows the current font state.
377 void toggleAndShow(BufferView * bv, LyXFont const & font, bool toggleall)
379 if (!bv->available())
382 if (bv->theLockingInset()) {
383 bv->theLockingInset()->setFont(bv, font, toggleall);
387 LyXText * text = bv->getLyXText();
388 // FIXME: can this happen ??
392 bv->update(text, BufferView::SELECT);
393 text->toggleFree(font, toggleall);
394 bv->update(text, BufferView::SELECT);
396 if (font.language() != ignore_language ||
397 font.number() != LyXFont::IGNORE) {
398 LyXCursor & cursor = text->cursor;
399 text->computeBidiTables(bv->buffer(), cursor.row());
400 if (cursor.boundary() !=
401 text->isBoundary(bv->buffer(), *cursor.par(), cursor.pos(),
402 text->real_current_font))
403 text->setCursor(cursor.par(), cursor.pos(),
404 false, !cursor.boundary());
408 }; // namespace bv_funcs