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->buffer()->markDirty();
181 bv->owner()->message(_("Character set"));
185 void emph(BufferView * bv)
187 LyXFont font(LyXFont::ALL_IGNORE);
188 font.setEmph(LyXFont::TOGGLE);
189 toggleAndShow(bv, font);
193 void bold(BufferView * bv)
195 LyXFont font(LyXFont::ALL_IGNORE);
196 font.setSeries(LyXFont::BOLD_SERIES);
197 toggleAndShow(bv, font);
201 void noun(BufferView * bv)
203 LyXFont font(LyXFont::ALL_IGNORE);
204 font.setNoun(LyXFont::TOGGLE);
205 toggleAndShow(bv, font);
209 void number(BufferView * bv)
211 LyXFont font(LyXFont::ALL_IGNORE);
212 font.setNumber(LyXFont::TOGGLE);
213 toggleAndShow(bv, font);
216 void lang(BufferView * bv, string const & l)
218 LyXFont font(LyXFont::ALL_IGNORE);
219 Language const * lang = languages.getLanguage(l);
221 font.setLanguage(lang);
222 toggleAndShow(bv, font);
224 Alert::alert(_("Error! unknown language"),l);
228 // Change environment depth.
229 // if decInc >= 0, increment depth
230 // if decInc < 0, decrement depth
231 void changeDepth(BufferView * bv, LyXText * text, int decInc)
233 if (!bv->available() || !text)
237 bv->update(bv->text, BufferView::SELECT|BufferView::FITCUR);
242 if (text->inset_owner)
243 bv->updateInset((Inset *)text->inset_owner, true);
244 bv->update(bv->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
248 void code(BufferView * bv)
250 LyXFont font(LyXFont::ALL_IGNORE);
251 font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
252 toggleAndShow(bv, font);
256 void sans(BufferView * bv)
258 LyXFont font(LyXFont::ALL_IGNORE);
259 font.setFamily(LyXFont::SANS_FAMILY);
260 toggleAndShow(bv, font);
264 void roman(BufferView * bv)
266 LyXFont font(LyXFont::ALL_IGNORE);
267 font.setFamily(LyXFont::ROMAN_FAMILY);
268 toggleAndShow(bv, font);
272 void styleReset(BufferView * bv)
274 LyXFont font(LyXFont::ALL_INHERIT, ignore_language);
275 toggleAndShow(bv, font);
279 void underline(BufferView * bv)
281 LyXFont font(LyXFont::ALL_IGNORE);
282 font.setUnderbar(LyXFont::TOGGLE);
283 toggleAndShow(bv, font);
287 void fontSize(BufferView * bv, string const & size)
289 LyXFont font(LyXFont::ALL_IGNORE);
290 font.setLyXSize(size);
291 toggleAndShow(bv, font);
295 // Returns the current font and depth as a message.
296 string const currentState(BufferView * bv)
298 if (!bv->available())
303 LyXText * text = bv->getLyXText();
304 Buffer * buffer = bv->buffer();
305 LyXCursor const & c(text->cursor);
307 bool const show_change = buffer->params.tracking_changes
308 && c.pos() != c.par()->size()
309 && c.par()->lookupChange(c.pos()) != Change::UNCHANGED;
312 Change change(c.par()->lookupChangeFull(c.pos()));
313 Author const & a(bv->buffer()->authors().get(change.author));
314 state << _("Change: ") << a.name();
315 if (!a.email().empty()) {
316 state << " (" << a.email() << ")";
318 if (change.changetime)
319 state << _(" at ") << ctime(&change.changetime);
323 // I think we should only show changes from the default
325 LyXFont font = text->real_current_font;
326 LyXFont const & defaultfont =
327 buffer->params.getLyXTextClass().defaultfont();
328 font.reduce(defaultfont);
331 state << boost::format(_("Font: %1$s")) % font.stateText(&buffer->params);
333 state << _("Font: ") << font.stateText(&buffer->params);
336 // The paragraph depth
337 int depth = text->getDepth();
340 state << boost::format(_(", Depth: %1$d")) % depth;
342 state << _(", Depth: ") << depth;
347 // The paragraph spacing, but only if different from
349 if (!text->cursor.par()->params().spacing().isDefault()) {
350 Spacing::Space cur_space =
351 text->cursor.par()->params().spacing().getSpace();
352 state << _(", Spacing: ");
355 case Spacing::Single:
356 state << _("Single");
358 case Spacing::Onehalf:
359 state << _("OneHalf");
361 case Spacing::Double:
362 state << _("Double");
365 state << _("Other (")
366 << text->cursor.par()->params().spacing().getValue()
369 case Spacing::Default:
370 // should never happen, do nothing
375 state << _(", Paragraph: ") << text->cursor.par()->id();
377 return STRCONV(state.str());
381 /* Does the actual toggle job of the calls above.
382 * Also shows the current font state.
384 void toggleAndShow(BufferView * bv, LyXFont const & font, bool toggleall)
386 if (!bv->available())
389 if (bv->theLockingInset()) {
390 bv->theLockingInset()->setFont(bv, font, toggleall);
394 LyXText * text = bv->getLyXText();
395 // FIXME: can this happen ??
400 bv->update(text, BufferView::SELECT | BufferView::FITCUR);
401 text->toggleFree(bv, font, toggleall);
402 bv->update(text, BufferView::SELECT | BufferView::FITCUR | BufferView::CHANGE);
404 if (font.language() != ignore_language ||
405 font.number() != LyXFont::IGNORE) {
406 LyXCursor & cursor = text->cursor;
407 text->computeBidiTables(bv->buffer(), cursor.row());
408 if (cursor.boundary() !=
409 text->isBoundary(bv->buffer(), cursor.par(), cursor.pos(),
410 text->real_current_font))
411 text->setCursor(bv, cursor.par(), cursor.pos(),
412 false, !cursor.boundary());