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"
38 #include <boost/bind.hpp>
41 using namespace lyx::support;
46 LyXFont freefont(LyXFont::ALL_IGNORE);
47 bool toggleall(false);
54 void resizeInsets(BufferView * bv)
56 ParagraphList & paragraphs = bv->buffer()->paragraphs;
57 /// then remove all LyXText in text-insets
58 std::for_each(paragraphs.begin(), paragraphs.end(),
59 boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
63 // Set data using font and toggle
64 // If successful, returns true
65 bool font2string(LyXFont const & font, bool toggle, string & data)
67 string lang = "ignore";
69 lang = font.language()->lang();
72 os << "family " << font.family() << '\n'
73 << "series " << font.series() << '\n'
74 << "shape " << font.shape() << '\n'
75 << "size " << font.size() << '\n'
76 << "emph " << font.emph() << '\n'
77 << "underbar " << font.underbar() << '\n'
78 << "noun " << font.noun() << '\n'
79 << "number " << font.number() << '\n'
80 << "color " << font.color() << '\n'
81 << "language " << lang << '\n'
82 << "toggleall " << tostr(toggle);
83 data = STRCONV(os.str());
88 // Set font and toggle using data
89 // If successful, returns true
90 bool string2font(string const & data, LyXFont & font, bool & toggle)
92 istringstream is(STRCONV(data));
100 token = lex.getString();
102 if (token.empty() || !lex.next())
105 if (token == "family") {
106 int const next = lex.getInteger();
107 font.setFamily(LyXFont::FONT_FAMILY(next));
109 } else if (token == "series") {
110 int const next = lex.getInteger();
111 font.setSeries(LyXFont::FONT_SERIES(next));
113 } else if (token == "shape") {
114 int const next = lex.getInteger();
115 font.setShape(LyXFont::FONT_SHAPE(next));
117 } else if (token == "size") {
118 int const next = lex.getInteger();
119 font.setSize(LyXFont::FONT_SIZE(next));
121 } else if (token == "emph" || token == "underbar" ||
122 token == "noun" || token == "number") {
124 int const next = lex.getInteger();
125 LyXFont::FONT_MISC_STATE const misc =
126 LyXFont::FONT_MISC_STATE(next);
130 else if (token == "underbar")
131 font.setUnderbar(misc);
132 else if (token == "noun")
134 else if (token == "number")
135 font.setNumber(misc);
137 } else if (token == "color") {
138 int const next = lex.getInteger();
139 font.setColor(LColor::color(next));
141 } else if (token == "language") {
142 string const next = lex.getString();
143 if (next == "ignore")
144 font.setLanguage(ignore_language);
146 font.setLanguage(languages.getLanguage(next));
148 } else if (token == "toggleall") {
149 toggle = lex.getBool();
152 // Unrecognised token
162 string const freefont2string()
165 if (font2string(freefont, toggleall, data))
171 void update_and_apply_freefont(BufferView * bv, string const & data)
175 if (string2font(data, font, toggle)) {
183 void apply_freefont(BufferView * bv)
185 toggleAndShow(bv, freefont, toggleall);
186 bv->owner()->view_state_changed();
187 bv->owner()->message(_("Character set"));
191 void emph(BufferView * bv)
193 LyXFont font(LyXFont::ALL_IGNORE);
194 font.setEmph(LyXFont::TOGGLE);
195 toggleAndShow(bv, font);
199 void bold(BufferView * bv)
201 LyXFont font(LyXFont::ALL_IGNORE);
202 font.setSeries(LyXFont::BOLD_SERIES);
203 toggleAndShow(bv, font);
207 void noun(BufferView * bv)
209 LyXFont font(LyXFont::ALL_IGNORE);
210 font.setNoun(LyXFont::TOGGLE);
211 toggleAndShow(bv, font);
215 void number(BufferView * bv)
217 LyXFont font(LyXFont::ALL_IGNORE);
218 font.setNumber(LyXFont::TOGGLE);
219 toggleAndShow(bv, font);
222 void lang(BufferView * bv, string const & l)
224 LyXFont font(LyXFont::ALL_IGNORE);
225 Language const * lang = languages.getLanguage(l);
229 font.setLanguage(lang);
230 toggleAndShow(bv, font);
234 bool changeDepth(BufferView * bv, LyXText * text, DEPTH_CHANGE type, bool test_only)
236 if (!bv->available() || !text)
240 return text->changeDepth(type, true);
242 bv->update(BufferView::SELECT);
243 bool const changed = text->changeDepth(type, false);
244 if (text->inset_owner)
245 bv->updateInset((Inset *)text->inset_owner);
246 bv->update(BufferView::SELECT);
251 void code(BufferView * bv)
253 LyXFont font(LyXFont::ALL_IGNORE);
254 font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
255 toggleAndShow(bv, font);
259 void sans(BufferView * bv)
261 LyXFont font(LyXFont::ALL_IGNORE);
262 font.setFamily(LyXFont::SANS_FAMILY);
263 toggleAndShow(bv, font);
267 void roman(BufferView * bv)
269 LyXFont font(LyXFont::ALL_IGNORE);
270 font.setFamily(LyXFont::ROMAN_FAMILY);
271 toggleAndShow(bv, font);
275 void styleReset(BufferView * bv)
277 LyXFont font(LyXFont::ALL_INHERIT, ignore_language);
278 toggleAndShow(bv, font);
282 void underline(BufferView * bv)
284 LyXFont font(LyXFont::ALL_IGNORE);
285 font.setUnderbar(LyXFont::TOGGLE);
286 toggleAndShow(bv, font);
290 void fontSize(BufferView * bv, string const & size)
292 LyXFont font(LyXFont::ALL_IGNORE);
293 font.setLyXSize(size);
294 toggleAndShow(bv, font);
298 // Returns the current font and depth as a message.
299 string const currentState(BufferView * bv)
301 if (!bv->available())
305 return mathcursor->info();
309 LyXText * text = bv->getLyXText();
310 Buffer * buffer = bv->buffer();
311 LyXCursor const & c(text->cursor);
313 bool const show_change = buffer->params.tracking_changes
314 && c.pos() != c.par()->size()
315 && c.par()->lookupChange(c.pos()) != Change::UNCHANGED;
318 Change change(c.par()->lookupChangeFull(c.pos()));
319 Author const & a(bv->buffer()->authors().get(change.author));
320 state << _("Change: ") << a.name();
321 if (!a.email().empty()) {
322 state << " (" << a.email() << ")";
324 if (change.changetime)
325 state << _(" at ") << ctime(&change.changetime);
329 // I think we should only show changes from the default
331 LyXFont font = text->real_current_font;
332 LyXFont const & defaultfont =
333 buffer->params.getLyXTextClass().defaultfont();
334 font.reduce(defaultfont);
336 // avoid _(...) re-entrance problem
337 string const s = font.stateText(&buffer->params);
338 state << bformat(_("Font: %1$s"), s);
340 // state << bformat(_("Font: %1$s"), font.stateText(&buffer->params));
342 // The paragraph depth
343 int depth = text->getDepth();
345 state << bformat(_(", Depth: %1$s"), tostr(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();
376 state << " Inset: " <<
377 (text->cursor.par()->inInset() ? text->cursor.par()->inInset()->id() : -1);
379 return STRCONV(state.str());
383 /* Does the actual toggle job of the calls above.
384 * Also shows the current font state.
386 void toggleAndShow(BufferView * bv, LyXFont const & font, bool toggleall)
388 if (!bv->available())
391 if (bv->theLockingInset()) {
392 bv->theLockingInset()->setFont(bv, font, toggleall);
396 LyXText * text = bv->getLyXText();
397 // FIXME: can this happen ??
401 bv->update(text, BufferView::SELECT);
402 text->toggleFree(font, toggleall);
403 bv->update(text, BufferView::SELECT);
405 if (font.language() != ignore_language ||
406 font.number() != LyXFont::IGNORE) {
407 LyXCursor & cursor = text->cursor;
408 text->computeBidiTables(bv->buffer(), text->cursorRow());
409 if (cursor.boundary() !=
410 text->isBoundary(bv->buffer(), *cursor.par(), cursor.pos(),
411 text->real_current_font))
412 text->setCursor(cursor.par(), cursor.pos(),
413 false, !cursor.boundary());
418 // deletes a selection during an insertion
419 void replaceSelection(LyXText * lt)
421 if (lt->selection.set()) {
423 lt->cutSelection(true, false);
428 }; // namespace bv_funcs