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"
22 #include "ParagraphParameters.h"
24 #include "frontends/LyXView.h"
25 #include "frontends/Alert.h"
26 #include "mathed/math_cursor.h"
28 #include "support/tostr.h"
29 #include "support/std_sstream.h"
31 #include "insets/insettext.h"
33 using namespace lyx::support;
35 using std::istringstream;
36 using std::ostringstream;
41 LyXFont freefont(LyXFont::ALL_IGNORE);
42 bool toggleall(false);
48 // Set data using font and toggle
49 // If successful, returns true
50 bool font2string(LyXFont const & font, bool toggle, string & data)
52 string lang = "ignore";
54 lang = font.language()->lang();
57 os << "family " << font.family() << '\n'
58 << "series " << font.series() << '\n'
59 << "shape " << font.shape() << '\n'
60 << "size " << font.size() << '\n'
61 << "emph " << font.emph() << '\n'
62 << "underbar " << font.underbar() << '\n'
63 << "noun " << font.noun() << '\n'
64 << "number " << font.number() << '\n'
65 << "color " << font.color() << '\n'
66 << "language " << lang << '\n'
67 << "toggleall " << tostr(toggle);
68 data = STRCONV(os.str());
73 // Set font and toggle using data
74 // If successful, returns true
75 bool string2font(string const & data, LyXFont & font, bool & toggle)
77 istringstream is(STRCONV(data));
85 token = lex.getString();
87 if (token.empty() || !lex.next())
90 if (token == "family") {
91 int const next = lex.getInteger();
92 font.setFamily(LyXFont::FONT_FAMILY(next));
94 } else if (token == "series") {
95 int const next = lex.getInteger();
96 font.setSeries(LyXFont::FONT_SERIES(next));
98 } else if (token == "shape") {
99 int const next = lex.getInteger();
100 font.setShape(LyXFont::FONT_SHAPE(next));
102 } else if (token == "size") {
103 int const next = lex.getInteger();
104 font.setSize(LyXFont::FONT_SIZE(next));
106 } else if (token == "emph" || token == "underbar" ||
107 token == "noun" || token == "number") {
109 int const next = lex.getInteger();
110 LyXFont::FONT_MISC_STATE const misc =
111 LyXFont::FONT_MISC_STATE(next);
115 else if (token == "underbar")
116 font.setUnderbar(misc);
117 else if (token == "noun")
119 else if (token == "number")
120 font.setNumber(misc);
122 } else if (token == "color") {
123 int const next = lex.getInteger();
124 font.setColor(LColor::color(next));
126 } else if (token == "language") {
127 string const next = lex.getString();
128 if (next == "ignore")
129 font.setLanguage(ignore_language);
131 font.setLanguage(languages.getLanguage(next));
133 } else if (token == "toggleall") {
134 toggle = lex.getBool();
137 // Unrecognised token
147 string const freefont2string()
150 if (font2string(freefont, toggleall, data))
156 void update_and_apply_freefont(BufferView * bv, string const & data)
160 if (string2font(data, font, toggle)) {
168 void apply_freefont(BufferView * bv)
170 toggleAndShow(bv, freefont, toggleall);
171 bv->owner()->view_state_changed();
172 bv->owner()->message(_("Character set"));
176 void emph(BufferView * bv)
178 LyXFont font(LyXFont::ALL_IGNORE);
179 font.setEmph(LyXFont::TOGGLE);
180 toggleAndShow(bv, font);
184 void bold(BufferView * bv)
186 LyXFont font(LyXFont::ALL_IGNORE);
187 font.setSeries(LyXFont::BOLD_SERIES);
188 toggleAndShow(bv, font);
192 void noun(BufferView * bv)
194 LyXFont font(LyXFont::ALL_IGNORE);
195 font.setNoun(LyXFont::TOGGLE);
196 toggleAndShow(bv, font);
200 void number(BufferView * bv)
202 LyXFont font(LyXFont::ALL_IGNORE);
203 font.setNumber(LyXFont::TOGGLE);
204 toggleAndShow(bv, font);
208 void lang(BufferView * bv, string const & l)
210 Language const * lang = languages.getLanguage(l);
214 LyXFont font(LyXFont::ALL_IGNORE);
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 bool const changed = text->changeDepth(type, false);
229 if (text->inset_owner)
230 bv->updateInset(text->inset_owner);
235 void code(BufferView * bv)
237 LyXFont font(LyXFont::ALL_IGNORE);
238 font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
239 toggleAndShow(bv, font);
243 void sans(BufferView * bv)
245 LyXFont font(LyXFont::ALL_IGNORE);
246 font.setFamily(LyXFont::SANS_FAMILY);
247 toggleAndShow(bv, font);
251 void roman(BufferView * bv)
253 LyXFont font(LyXFont::ALL_IGNORE);
254 font.setFamily(LyXFont::ROMAN_FAMILY);
255 toggleAndShow(bv, font);
259 void styleReset(BufferView * bv)
261 LyXFont font(LyXFont::ALL_INHERIT, ignore_language);
262 toggleAndShow(bv, font);
266 void underline(BufferView * bv)
268 LyXFont font(LyXFont::ALL_IGNORE);
269 font.setUnderbar(LyXFont::TOGGLE);
270 toggleAndShow(bv, font);
274 void fontSize(BufferView * bv, string const & size)
276 LyXFont font(LyXFont::ALL_IGNORE);
277 font.setLyXSize(size);
278 toggleAndShow(bv, font);
282 // Returns the current font and depth as a message.
283 string const currentState(BufferView * bv)
285 if (!bv->available())
289 return mathcursor->info();
293 LyXText * text = bv->getLyXText();
294 Buffer * buffer = bv->buffer();
295 LyXCursor const & c(text->cursor);
297 bool const show_change = buffer->params.tracking_changes
298 && c.pos() != c.par()->size()
299 && c.par()->lookupChange(c.pos()) != Change::UNCHANGED;
302 Change change(c.par()->lookupChangeFull(c.pos()));
303 Author const & a(bv->buffer()->authors().get(change.author));
304 state << _("Change: ") << a.name();
305 if (!a.email().empty()) {
306 state << " (" << a.email() << ")";
308 if (change.changetime)
309 state << _(" at ") << ctime(&change.changetime);
313 // I think we should only show changes from the default
315 LyXFont font = text->real_current_font;
316 LyXFont const & defaultfont =
317 buffer->params.getLyXTextClass().defaultfont();
318 font.reduce(defaultfont);
320 // avoid _(...) re-entrance problem
321 string const s = font.stateText(&buffer->params);
322 state << bformat(_("Font: %1$s"), s);
324 // state << bformat(_("Font: %1$s"), font.stateText(&buffer->params));
326 // The paragraph depth
327 int depth = text->getDepth();
329 state << bformat(_(", Depth: %1$s"), tostr(depth));
331 // The paragraph spacing, but only if different from
333 if (!text->cursor.par()->params().spacing().isDefault()) {
334 Spacing::Space cur_space =
335 text->cursor.par()->params().spacing().getSpace();
336 state << _(", Spacing: ");
339 case Spacing::Single:
340 state << _("Single");
342 case Spacing::Onehalf:
343 state << _("OneHalf");
345 case Spacing::Double:
346 state << _("Double");
349 state << _("Other (")
350 << text->cursor.par()->params().spacing().getValue()
353 case Spacing::Default:
354 // should never happen, do nothing
359 state << _(", Paragraph: ") << text->cursor.par()->id();
360 state << _(", Position: ") << text->cursor.pos();
361 RowList::iterator rit = text->cursorRow();
362 state << bformat(_(", Row b:%1$d e:%2$d"), rit->pos(), rit->end());
363 state << _(", Inset: ") <<
364 (text->cursor.par()->inInset() ? text->cursor.par()->inInset()->id() : -1);
366 return STRCONV(state.str());
370 /* Does the actual toggle job of the calls above.
371 * Also shows the current font state.
373 void toggleAndShow(BufferView * bv, LyXFont const & font, bool toggleall)
375 if (!bv->available())
378 if (bv->theLockingInset()) {
379 bv->theLockingInset()->setFont(bv, font, toggleall);
383 LyXText * text = bv->getLyXText();
384 text->toggleFree(font, toggleall);
387 if (font.language() != ignore_language ||
388 font.number() != LyXFont::IGNORE) {
389 LyXCursor & cursor = text->cursor;
390 text->computeBidiTables(text->cursor.par(), *bv->buffer(),
392 if (cursor.boundary() !=
393 text->isBoundary(*bv->buffer(), *cursor.par(), cursor.pos(),
394 text->real_current_font))
395 text->setCursor(cursor.par(), cursor.pos(),
396 false, !cursor.boundary());
401 // deletes a selection during an insertion
402 void replaceSelection(LyXText * lt)
404 if (lt->selection.set()) {
405 lt->cutSelection(true, false);
410 }; // namespace bv_funcs