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"
42 LyXFont freefont(LyXFont::ALL_IGNORE);
43 bool toggleall(false);
49 // Set data using font and toggle
50 // If successful, returns true
51 bool font2string(LyXFont const & font, bool toggle, string & data)
53 string lang = "ignore";
55 lang = font.language()->lang();
58 os << "family " << font.family() << '\n'
59 << "series " << font.series() << '\n'
60 << "shape " << font.shape() << '\n'
61 << "size " << font.size() << '\n'
62 << "emph " << font.emph() << '\n'
63 << "underbar " << font.underbar() << '\n'
64 << "noun " << font.noun() << '\n'
65 << "number " << font.number() << '\n'
66 << "color " << font.color() << '\n'
67 << "language " << lang << '\n'
68 << "toggleall " << tostr(toggle);
69 data = STRCONV(os.str());
74 // Set font and toggle using data
75 // If successful, returns true
76 bool string2font(string const & data, LyXFont & font, bool & toggle)
78 istringstream is(STRCONV(data));
86 token = lex.getString();
88 if (token.empty() || !lex.next())
91 if (token == "family") {
92 int const next = lex.getInteger();
93 font.setFamily(LyXFont::FONT_FAMILY(next));
95 } else if (token == "series") {
96 int const next = lex.getInteger();
97 font.setSeries(LyXFont::FONT_SERIES(next));
99 } else if (token == "shape") {
100 int const next = lex.getInteger();
101 font.setShape(LyXFont::FONT_SHAPE(next));
103 } else if (token == "size") {
104 int const next = lex.getInteger();
105 font.setSize(LyXFont::FONT_SIZE(next));
107 } else if (token == "emph" || token == "underbar" ||
108 token == "noun" || token == "number") {
110 int const next = lex.getInteger();
111 LyXFont::FONT_MISC_STATE const misc =
112 LyXFont::FONT_MISC_STATE(next);
116 else if (token == "underbar")
117 font.setUnderbar(misc);
118 else if (token == "noun")
120 else if (token == "number")
121 font.setNumber(misc);
123 } else if (token == "color") {
124 int const next = lex.getInteger();
125 font.setColor(LColor::color(next));
127 } else if (token == "language") {
128 string const next = lex.getString();
129 if (next == "ignore")
130 font.setLanguage(ignore_language);
132 font.setLanguage(languages.getLanguage(next));
134 } else if (token == "toggleall") {
135 toggle = lex.getBool();
138 // Unrecognised token
148 string const freefont2string()
151 if (font2string(freefont, toggleall, data))
157 void update_and_apply_freefont(BufferView * bv, string const & data)
161 if (string2font(data, font, toggle)) {
169 void apply_freefont(BufferView * bv)
171 toggleAndShow(bv, freefont, toggleall);
172 bv->owner()->view_state_changed();
173 bv->owner()->message(_("Character set"));
177 void emph(BufferView * bv)
179 LyXFont font(LyXFont::ALL_IGNORE);
180 font.setEmph(LyXFont::TOGGLE);
181 toggleAndShow(bv, font);
185 void bold(BufferView * bv)
187 LyXFont font(LyXFont::ALL_IGNORE);
188 font.setSeries(LyXFont::BOLD_SERIES);
189 toggleAndShow(bv, font);
193 void noun(BufferView * bv)
195 LyXFont font(LyXFont::ALL_IGNORE);
196 font.setNoun(LyXFont::TOGGLE);
197 toggleAndShow(bv, font);
201 void number(BufferView * bv)
203 LyXFont font(LyXFont::ALL_IGNORE);
204 font.setNumber(LyXFont::TOGGLE);
205 toggleAndShow(bv, font);
208 void lang(BufferView * bv, string const & l)
210 LyXFont font(LyXFont::ALL_IGNORE);
211 Language const * lang = languages.getLanguage(l);
215 font.setLanguage(lang);
216 toggleAndShow(bv, font);
220 bool changeDepth(BufferView * bv, LyXText * text, DEPTH_CHANGE type, bool test_only)
222 if (!bv->available() || !text)
226 return text->changeDepth(type, true);
228 bv->update(BufferView::SELECT);
229 bool const changed = text->changeDepth(type, false);
230 if (text->inset_owner)
231 bv->updateInset((Inset *)text->inset_owner);
232 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())
291 return mathcursor->info();
295 LyXText * text = bv->getLyXText();
296 Buffer * buffer = bv->buffer();
297 LyXCursor const & c(text->cursor);
299 bool const show_change = buffer->params.tracking_changes
300 && c.pos() != c.par()->size()
301 && c.par()->lookupChange(c.pos()) != Change::UNCHANGED;
304 Change change(c.par()->lookupChangeFull(c.pos()));
305 Author const & a(bv->buffer()->authors().get(change.author));
306 state << _("Change: ") << a.name();
307 if (!a.email().empty()) {
308 state << " (" << a.email() << ")";
310 if (change.changetime)
311 state << _(" at ") << ctime(&change.changetime);
315 // I think we should only show changes from the default
317 LyXFont font = text->real_current_font;
318 LyXFont const & defaultfont =
319 buffer->params.getLyXTextClass().defaultfont();
320 font.reduce(defaultfont);
322 // avoid _(...) re-entrance problem
323 string const s = font.stateText(&buffer->params);
324 state << bformat(_("Font: %1$s"), s);
326 // state << bformat(_("Font: %1$s"), font.stateText(&buffer->params));
328 // The paragraph depth
329 int depth = text->getDepth();
331 state << bformat(_(", Depth: %1$s"), tostr(depth));
333 // The paragraph spacing, but only if different from
335 if (!text->cursor.par()->params().spacing().isDefault()) {
336 Spacing::Space cur_space =
337 text->cursor.par()->params().spacing().getSpace();
338 state << _(", Spacing: ");
341 case Spacing::Single:
342 state << _("Single");
344 case Spacing::Onehalf:
345 state << _("OneHalf");
347 case Spacing::Double:
348 state << _("Double");
351 state << _("Other (")
352 << text->cursor.par()->params().spacing().getValue()
355 case Spacing::Default:
356 // should never happen, do nothing
361 state << _(", Paragraph: ") << text->cursor.par()->id();
362 state << " Inset: " <<
363 (text->cursor.par()->inInset() ? text->cursor.par()->inInset()->id() : -1);
365 return STRCONV(state.str());
369 /* Does the actual toggle job of the calls above.
370 * Also shows the current font state.
372 void toggleAndShow(BufferView * bv, LyXFont const & font, bool toggleall)
374 if (!bv->available())
377 if (bv->theLockingInset()) {
378 bv->theLockingInset()->setFont(bv, font, toggleall);
382 LyXText * text = bv->getLyXText();
383 // FIXME: can this happen ??
387 bv->update(text, BufferView::SELECT);
388 text->toggleFree(font, toggleall);
389 bv->update(text, BufferView::SELECT);
391 if (font.language() != ignore_language ||
392 font.number() != LyXFont::IGNORE) {
393 LyXCursor & cursor = text->cursor;
394 text->computeBidiTables(bv->buffer(), text->cursorRow());
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());
403 }; // namespace bv_funcs