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 Language const * lang = languages.getLanguage(l);
228 LyXFont font(LyXFont::ALL_IGNORE);
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 bool const changed = text->changeDepth(type, false);
243 if (text->inset_owner)
250 void code(BufferView * bv)
252 LyXFont font(LyXFont::ALL_IGNORE);
253 font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
254 toggleAndShow(bv, font);
258 void sans(BufferView * bv)
260 LyXFont font(LyXFont::ALL_IGNORE);
261 font.setFamily(LyXFont::SANS_FAMILY);
262 toggleAndShow(bv, font);
266 void roman(BufferView * bv)
268 LyXFont font(LyXFont::ALL_IGNORE);
269 font.setFamily(LyXFont::ROMAN_FAMILY);
270 toggleAndShow(bv, font);
274 void styleReset(BufferView * bv)
276 LyXFont font(LyXFont::ALL_INHERIT, ignore_language);
277 toggleAndShow(bv, font);
281 void underline(BufferView * bv)
283 LyXFont font(LyXFont::ALL_IGNORE);
284 font.setUnderbar(LyXFont::TOGGLE);
285 toggleAndShow(bv, font);
289 void fontSize(BufferView * bv, string const & size)
291 LyXFont font(LyXFont::ALL_IGNORE);
292 font.setLyXSize(size);
293 toggleAndShow(bv, font);
297 // Returns the current font and depth as a message.
298 string const currentState(BufferView * bv)
300 if (!bv->available())
304 return mathcursor->info();
308 LyXText * text = bv->getLyXText();
309 Buffer * buffer = bv->buffer();
310 LyXCursor const & c(text->cursor);
312 bool const show_change = buffer->params.tracking_changes
313 && c.pos() != c.par()->size()
314 && c.par()->lookupChange(c.pos()) != Change::UNCHANGED;
317 Change change(c.par()->lookupChangeFull(c.pos()));
318 Author const & a(bv->buffer()->authors().get(change.author));
319 state << _("Change: ") << a.name();
320 if (!a.email().empty()) {
321 state << " (" << a.email() << ")";
323 if (change.changetime)
324 state << _(" at ") << ctime(&change.changetime);
328 // I think we should only show changes from the default
330 LyXFont font = text->real_current_font;
331 LyXFont const & defaultfont =
332 buffer->params.getLyXTextClass().defaultfont();
333 font.reduce(defaultfont);
335 // avoid _(...) re-entrance problem
336 string const s = font.stateText(&buffer->params);
337 state << bformat(_("Font: %1$s"), s);
339 // state << bformat(_("Font: %1$s"), font.stateText(&buffer->params));
341 // The paragraph depth
342 int depth = text->getDepth();
344 state << bformat(_(", Depth: %1$s"), tostr(depth));
346 // The paragraph spacing, but only if different from
348 if (!text->cursor.par()->params().spacing().isDefault()) {
349 Spacing::Space cur_space =
350 text->cursor.par()->params().spacing().getSpace();
351 state << _(", Spacing: ");
354 case Spacing::Single:
355 state << _("Single");
357 case Spacing::Onehalf:
358 state << _("OneHalf");
360 case Spacing::Double:
361 state << _("Double");
364 state << _("Other (")
365 << text->cursor.par()->params().spacing().getValue()
368 case Spacing::Default:
369 // should never happen, do nothing
374 state << _(", Paragraph: ") << text->cursor.par()->id();
375 state << " Inset: " <<
376 (text->cursor.par()->inInset() ? text->cursor.par()->inInset()->id() : -1);
378 return STRCONV(state.str());
382 /* Does the actual toggle job of the calls above.
383 * Also shows the current font state.
385 void toggleAndShow(BufferView * bv, LyXFont const & font, bool toggleall)
387 if (!bv->available())
390 if (bv->theLockingInset()) {
391 bv->theLockingInset()->setFont(bv, font, toggleall);
395 LyXText * text = bv->getLyXText();
396 // FIXME: can this happen ??
400 text->toggleFree(font, toggleall);
403 if (font.language() != ignore_language ||
404 font.number() != LyXFont::IGNORE) {
405 LyXCursor & cursor = text->cursor;
406 text->computeBidiTables(bv->buffer(), text->cursorRow());
407 if (cursor.boundary() !=
408 text->isBoundary(bv->buffer(), *cursor.par(), cursor.pos(),
409 text->real_current_font))
410 text->setCursor(cursor.par(), cursor.pos(),
411 false, !cursor.boundary());
416 // deletes a selection during an insertion
417 void replaceSelection(LyXText * lt)
419 if (lt->selection.set()) {
421 lt->cutSelection(true, false);
426 }; // namespace bv_funcs