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.
17 #include "bufferview_funcs.h"
18 #include "BufferView.h"
23 #include "paragraph.h"
24 #include "ParagraphParameters.h"
26 #include "frontends/LyXView.h"
27 #include "frontends/Alert.h"
28 #include "mathed/math_cursor.h"
30 #include "support/tostr.h"
31 #include "support/std_sstream.h"
33 #include "insets/insettext.h"
35 using namespace lyx::support;
37 using std::istringstream;
38 using std::ostringstream;
43 LyXFont freefont(LyXFont::ALL_IGNORE);
44 bool toggleall(false);
50 // Set data using font and toggle
51 // If successful, returns true
52 bool font2string(LyXFont const & font, bool toggle, string & data)
54 string lang = "ignore";
56 lang = font.language()->lang();
59 os << "family " << font.family() << '\n'
60 << "series " << font.series() << '\n'
61 << "shape " << font.shape() << '\n'
62 << "size " << font.size() << '\n'
63 << "emph " << font.emph() << '\n'
64 << "underbar " << font.underbar() << '\n'
65 << "noun " << font.noun() << '\n'
66 << "number " << font.number() << '\n'
67 << "color " << font.color() << '\n'
68 << "language " << lang << '\n'
69 << "toggleall " << tostr(toggle);
70 data = STRCONV(os.str());
75 // Set font and toggle using data
76 // If successful, returns true
77 bool string2font(string const & data, LyXFont & font, bool & toggle)
79 istringstream is(STRCONV(data));
87 token = lex.getString();
89 if (token.empty() || !lex.next())
92 if (token == "family") {
93 int const next = lex.getInteger();
94 font.setFamily(LyXFont::FONT_FAMILY(next));
96 } else if (token == "series") {
97 int const next = lex.getInteger();
98 font.setSeries(LyXFont::FONT_SERIES(next));
100 } else if (token == "shape") {
101 int const next = lex.getInteger();
102 font.setShape(LyXFont::FONT_SHAPE(next));
104 } else if (token == "size") {
105 int const next = lex.getInteger();
106 font.setSize(LyXFont::FONT_SIZE(next));
108 } else if (token == "emph" || token == "underbar" ||
109 token == "noun" || token == "number") {
111 int const next = lex.getInteger();
112 LyXFont::FONT_MISC_STATE const misc =
113 LyXFont::FONT_MISC_STATE(next);
117 else if (token == "underbar")
118 font.setUnderbar(misc);
119 else if (token == "noun")
121 else if (token == "number")
122 font.setNumber(misc);
124 } else if (token == "color") {
125 int const next = lex.getInteger();
126 font.setColor(LColor::color(next));
128 } else if (token == "language") {
129 string const next = lex.getString();
130 if (next == "ignore")
131 font.setLanguage(ignore_language);
133 font.setLanguage(languages.getLanguage(next));
135 } else if (token == "toggleall") {
136 toggle = lex.getBool();
139 // Unrecognised token
149 string const freefont2string()
152 if (font2string(freefont, toggleall, data))
158 void update_and_apply_freefont(BufferView * bv, string const & data)
162 if (string2font(data, font, toggle)) {
170 void apply_freefont(BufferView * bv)
172 toggleAndShow(bv, freefont, toggleall);
173 bv->owner()->view_state_changed();
174 bv->owner()->message(_("Character set"));
178 void emph(BufferView * bv)
180 LyXFont font(LyXFont::ALL_IGNORE);
181 font.setEmph(LyXFont::TOGGLE);
182 toggleAndShow(bv, font);
186 void bold(BufferView * bv)
188 LyXFont font(LyXFont::ALL_IGNORE);
189 font.setSeries(LyXFont::BOLD_SERIES);
190 toggleAndShow(bv, font);
194 void noun(BufferView * bv)
196 LyXFont font(LyXFont::ALL_IGNORE);
197 font.setNoun(LyXFont::TOGGLE);
198 toggleAndShow(bv, font);
202 void number(BufferView * bv)
204 LyXFont font(LyXFont::ALL_IGNORE);
205 font.setNumber(LyXFont::TOGGLE);
206 toggleAndShow(bv, font);
210 void lang(BufferView * bv, string const & l)
212 Language const * lang = languages.getLanguage(l);
216 LyXFont font(LyXFont::ALL_IGNORE);
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 bool const changed = text->changeDepth(type, false);
231 if (text->inset_owner)
232 bv->updateInset(text->inset_owner);
237 void code(BufferView * bv)
239 LyXFont font(LyXFont::ALL_IGNORE);
240 font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
241 toggleAndShow(bv, font);
245 void sans(BufferView * bv)
247 LyXFont font(LyXFont::ALL_IGNORE);
248 font.setFamily(LyXFont::SANS_FAMILY);
249 toggleAndShow(bv, font);
253 void roman(BufferView * bv)
255 LyXFont font(LyXFont::ALL_IGNORE);
256 font.setFamily(LyXFont::ROMAN_FAMILY);
257 toggleAndShow(bv, font);
261 void styleReset(BufferView * bv)
263 LyXFont font(LyXFont::ALL_INHERIT, ignore_language);
264 toggleAndShow(bv, font);
268 void underline(BufferView * bv)
270 LyXFont font(LyXFont::ALL_IGNORE);
271 font.setUnderbar(LyXFont::TOGGLE);
272 toggleAndShow(bv, font);
276 void fontSize(BufferView * bv, string const & size)
278 LyXFont font(LyXFont::ALL_IGNORE);
279 font.setLyXSize(size);
280 toggleAndShow(bv, font);
284 // Returns the current font and depth as a message.
285 string const currentState(BufferView * bv)
287 if (!bv->available())
291 return mathcursor->info();
295 LyXText * text = bv->getLyXText();
296 Buffer * buffer = bv->buffer();
297 LyXCursor const & c(text->cursor);
299 bool const show_change = buffer->params.tracking_changes
300 && c.pos() != c.par()->size()
301 && c.par()->lookupChange(c.pos()) != Change::UNCHANGED;
304 Change change(c.par()->lookupChangeFull(c.pos()));
305 Author const & a(bv->buffer()->authors().get(change.author));
306 state << _("Change: ") << a.name();
307 if (!a.email().empty()) {
308 state << " (" << a.email() << ")";
310 if (change.changetime)
311 state << _(" at ") << ctime(&change.changetime);
315 // I think we should only show changes from the default
317 LyXFont font = text->real_current_font;
318 LyXFont const & defaultfont =
319 buffer->params.getLyXTextClass().defaultfont();
320 font.reduce(defaultfont);
322 // avoid _(...) re-entrance problem
323 string const s = font.stateText(&buffer->params);
324 state << bformat(_("Font: %1$s"), s);
326 // state << bformat(_("Font: %1$s"), font.stateText(&buffer->params));
328 // The paragraph depth
329 int depth = text->getDepth();
331 state << bformat(_(", Depth: %1$s"), tostr(depth));
333 // The paragraph spacing, but only if different from
335 if (!text->cursor.par()->params().spacing().isDefault()) {
336 Spacing::Space cur_space =
337 text->cursor.par()->params().spacing().getSpace();
338 state << _(", Spacing: ");
341 case Spacing::Single:
342 state << _("Single");
344 case Spacing::Onehalf:
345 state << _("OneHalf");
347 case Spacing::Double:
348 state << _("Double");
351 state << _("Other (")
352 << text->cursor.par()->params().spacing().getValue()
355 case Spacing::Default:
356 // should never happen, do nothing
361 state << _(", Paragraph: ") << text->cursor.par()->id();
362 state << _(", Position: ") << text->cursor.pos();
363 RowList::iterator rit = text->cursorRow();
364 state << bformat(_(", Row b:%1$d e:%2$d"), rit->pos(), rit->end());
365 state << _(", Inset: ") <<
366 (text->cursor.par()->inInset() ? text->cursor.par()->inInset()->id() : -1);
368 return STRCONV(state.str());
372 /* Does the actual toggle job of the calls above.
373 * Also shows the current font state.
375 void toggleAndShow(BufferView * bv, LyXFont const & font, bool toggleall)
377 if (!bv->available())
380 if (bv->theLockingInset()) {
381 bv->theLockingInset()->setFont(bv, font, toggleall);
385 LyXText * text = bv->getLyXText();
386 text->toggleFree(font, toggleall);
389 if (font.language() != ignore_language ||
390 font.number() != LyXFont::IGNORE) {
391 LyXCursor & cursor = text->cursor;
392 text->computeBidiTables(text->cursor.par(), *bv->buffer(),
394 if (cursor.boundary() !=
395 text->isBoundary(*bv->buffer(), *cursor.par(), cursor.pos(),
396 text->real_current_font))
397 text->setCursor(cursor.par(), cursor.pos(),
398 false, !cursor.boundary());
403 // deletes a selection during an insertion
404 void replaceSelection(LyXText * lt)
406 if (lt->selection.set()) {
407 lt->cutSelection(true, false);
412 }; // namespace bv_funcs