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"
39 using namespace lyx::support;
44 LyXFont freefont(LyXFont::ALL_IGNORE);
45 bool toggleall(false);
51 // Set data using font and toggle
52 // If successful, returns true
53 bool font2string(LyXFont const & font, bool toggle, string & data)
55 string lang = "ignore";
57 lang = font.language()->lang();
60 os << "family " << font.family() << '\n'
61 << "series " << font.series() << '\n'
62 << "shape " << font.shape() << '\n'
63 << "size " << font.size() << '\n'
64 << "emph " << font.emph() << '\n'
65 << "underbar " << font.underbar() << '\n'
66 << "noun " << font.noun() << '\n'
67 << "number " << font.number() << '\n'
68 << "color " << font.color() << '\n'
69 << "language " << lang << '\n'
70 << "toggleall " << tostr(toggle);
71 data = STRCONV(os.str());
76 // Set font and toggle using data
77 // If successful, returns true
78 bool string2font(string const & data, LyXFont & font, bool & toggle)
80 istringstream is(STRCONV(data));
88 token = lex.getString();
90 if (token.empty() || !lex.next())
93 if (token == "family") {
94 int const next = lex.getInteger();
95 font.setFamily(LyXFont::FONT_FAMILY(next));
97 } else if (token == "series") {
98 int const next = lex.getInteger();
99 font.setSeries(LyXFont::FONT_SERIES(next));
101 } else if (token == "shape") {
102 int const next = lex.getInteger();
103 font.setShape(LyXFont::FONT_SHAPE(next));
105 } else if (token == "size") {
106 int const next = lex.getInteger();
107 font.setSize(LyXFont::FONT_SIZE(next));
109 } else if (token == "emph" || token == "underbar" ||
110 token == "noun" || token == "number") {
112 int const next = lex.getInteger();
113 LyXFont::FONT_MISC_STATE const misc =
114 LyXFont::FONT_MISC_STATE(next);
118 else if (token == "underbar")
119 font.setUnderbar(misc);
120 else if (token == "noun")
122 else if (token == "number")
123 font.setNumber(misc);
125 } else if (token == "color") {
126 int const next = lex.getInteger();
127 font.setColor(LColor::color(next));
129 } else if (token == "language") {
130 string const next = lex.getString();
131 if (next == "ignore")
132 font.setLanguage(ignore_language);
134 font.setLanguage(languages.getLanguage(next));
136 } else if (token == "toggleall") {
137 toggle = lex.getBool();
140 // Unrecognised token
150 string const freefont2string()
153 if (font2string(freefont, toggleall, data))
159 void update_and_apply_freefont(BufferView * bv, string const & data)
163 if (string2font(data, font, toggle)) {
171 void apply_freefont(BufferView * bv)
173 toggleAndShow(bv, freefont, toggleall);
174 bv->owner()->view_state_changed();
175 bv->owner()->message(_("Character set"));
179 void emph(BufferView * bv)
181 LyXFont font(LyXFont::ALL_IGNORE);
182 font.setEmph(LyXFont::TOGGLE);
183 toggleAndShow(bv, font);
187 void bold(BufferView * bv)
189 LyXFont font(LyXFont::ALL_IGNORE);
190 font.setSeries(LyXFont::BOLD_SERIES);
191 toggleAndShow(bv, font);
195 void noun(BufferView * bv)
197 LyXFont font(LyXFont::ALL_IGNORE);
198 font.setNoun(LyXFont::TOGGLE);
199 toggleAndShow(bv, font);
203 void number(BufferView * bv)
205 LyXFont font(LyXFont::ALL_IGNORE);
206 font.setNumber(LyXFont::TOGGLE);
207 toggleAndShow(bv, font);
210 void lang(BufferView * bv, string const & l)
212 LyXFont font(LyXFont::ALL_IGNORE);
213 Language const * lang = languages.getLanguage(l);
217 font.setLanguage(lang);
218 toggleAndShow(bv, font);
222 bool changeDepth(BufferView * bv, LyXText * text, DEPTH_CHANGE type, bool test_only)
224 if (!bv->available() || !text)
228 return text->changeDepth(type, true);
230 bv->update(BufferView::SELECT);
231 bool const changed = text->changeDepth(type, false);
232 if (text->inset_owner)
233 bv->updateInset((Inset *)text->inset_owner);
234 bv->update(BufferView::SELECT);
239 void code(BufferView * bv)
241 LyXFont font(LyXFont::ALL_IGNORE);
242 font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
243 toggleAndShow(bv, font);
247 void sans(BufferView * bv)
249 LyXFont font(LyXFont::ALL_IGNORE);
250 font.setFamily(LyXFont::SANS_FAMILY);
251 toggleAndShow(bv, font);
255 void roman(BufferView * bv)
257 LyXFont font(LyXFont::ALL_IGNORE);
258 font.setFamily(LyXFont::ROMAN_FAMILY);
259 toggleAndShow(bv, font);
263 void styleReset(BufferView * bv)
265 LyXFont font(LyXFont::ALL_INHERIT, ignore_language);
266 toggleAndShow(bv, font);
270 void underline(BufferView * bv)
272 LyXFont font(LyXFont::ALL_IGNORE);
273 font.setUnderbar(LyXFont::TOGGLE);
274 toggleAndShow(bv, font);
278 void fontSize(BufferView * bv, string const & size)
280 LyXFont font(LyXFont::ALL_IGNORE);
281 font.setLyXSize(size);
282 toggleAndShow(bv, font);
286 // Returns the current font and depth as a message.
287 string const currentState(BufferView * bv)
289 if (!bv->available())
293 return mathcursor->info();
297 LyXText * text = bv->getLyXText();
298 Buffer * buffer = bv->buffer();
299 LyXCursor const & c(text->cursor);
301 bool const show_change = buffer->params.tracking_changes
302 && c.pos() != c.par()->size()
303 && c.par()->lookupChange(c.pos()) != Change::UNCHANGED;
306 Change change(c.par()->lookupChangeFull(c.pos()));
307 Author const & a(bv->buffer()->authors().get(change.author));
308 state << _("Change: ") << a.name();
309 if (!a.email().empty()) {
310 state << " (" << a.email() << ")";
312 if (change.changetime)
313 state << _(" at ") << ctime(&change.changetime);
317 // I think we should only show changes from the default
319 LyXFont font = text->real_current_font;
320 LyXFont const & defaultfont =
321 buffer->params.getLyXTextClass().defaultfont();
322 font.reduce(defaultfont);
324 // avoid _(...) re-entrance problem
325 string const s = font.stateText(&buffer->params);
326 state << bformat(_("Font: %1$s"), s);
328 // state << bformat(_("Font: %1$s"), font.stateText(&buffer->params));
330 // The paragraph depth
331 int depth = text->getDepth();
333 state << bformat(_(", Depth: %1$s"), tostr(depth));
335 // The paragraph spacing, but only if different from
337 if (!text->cursor.par()->params().spacing().isDefault()) {
338 Spacing::Space cur_space =
339 text->cursor.par()->params().spacing().getSpace();
340 state << _(", Spacing: ");
343 case Spacing::Single:
344 state << _("Single");
346 case Spacing::Onehalf:
347 state << _("OneHalf");
349 case Spacing::Double:
350 state << _("Double");
353 state << _("Other (")
354 << text->cursor.par()->params().spacing().getValue()
357 case Spacing::Default:
358 // should never happen, do nothing
363 state << _(", Paragraph: ") << text->cursor.par()->id();
364 state << " Inset: " <<
365 (text->cursor.par()->inInset() ? text->cursor.par()->inInset()->id() : -1);
367 return STRCONV(state.str());
371 /* Does the actual toggle job of the calls above.
372 * Also shows the current font state.
374 void toggleAndShow(BufferView * bv, LyXFont const & font, bool toggleall)
376 if (!bv->available())
379 if (bv->theLockingInset()) {
380 bv->theLockingInset()->setFont(bv, font, toggleall);
384 LyXText * text = bv->getLyXText();
385 // 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(), text->cursorRow());
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());
405 }; // namespace bv_funcs