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"
20 #include "bufferparams.h"
21 #include "BufferView.h"
27 #include "paragraph.h"
28 #include "ParagraphParameters.h"
30 #include "frontends/Alert.h"
31 #include "frontends/LyXView.h"
33 #include "insets/insettext.h"
35 #include "mathed/math_cursor.h"
37 #include "support/tostr.h"
39 #include "support/std_sstream.h"
41 using lyx::support::bformat;
43 using std::istringstream;
44 using std::ostringstream;
49 LyXFont freefont(LyXFont::ALL_IGNORE);
50 bool toggleall(false);
56 // Set data using font and toggle
57 // If successful, returns true
58 bool font2string(LyXFont const & font, bool toggle, string & data)
60 string lang = "ignore";
62 lang = font.language()->lang();
65 os << "family " << font.family() << '\n'
66 << "series " << font.series() << '\n'
67 << "shape " << font.shape() << '\n'
68 << "size " << font.size() << '\n'
69 << "emph " << font.emph() << '\n'
70 << "underbar " << font.underbar() << '\n'
71 << "noun " << font.noun() << '\n'
72 << "number " << font.number() << '\n'
73 << "color " << font.color() << '\n'
74 << "language " << lang << '\n'
75 << "toggleall " << tostr(toggle);
81 // Set font and toggle using data
82 // If successful, returns true
83 bool string2font(string const & data, LyXFont & font, bool & toggle)
85 istringstream is(data);
93 token = lex.getString();
95 if (token.empty() || !lex.next())
98 if (token == "family") {
99 int const next = lex.getInteger();
100 font.setFamily(LyXFont::FONT_FAMILY(next));
102 } else if (token == "series") {
103 int const next = lex.getInteger();
104 font.setSeries(LyXFont::FONT_SERIES(next));
106 } else if (token == "shape") {
107 int const next = lex.getInteger();
108 font.setShape(LyXFont::FONT_SHAPE(next));
110 } else if (token == "size") {
111 int const next = lex.getInteger();
112 font.setSize(LyXFont::FONT_SIZE(next));
114 } else if (token == "emph" || token == "underbar" ||
115 token == "noun" || token == "number") {
117 int const next = lex.getInteger();
118 LyXFont::FONT_MISC_STATE const misc =
119 LyXFont::FONT_MISC_STATE(next);
123 else if (token == "underbar")
124 font.setUnderbar(misc);
125 else if (token == "noun")
127 else if (token == "number")
128 font.setNumber(misc);
130 } else if (token == "color") {
131 int const next = lex.getInteger();
132 font.setColor(LColor::color(next));
134 } else if (token == "language") {
135 string const next = lex.getString();
136 if (next == "ignore")
137 font.setLanguage(ignore_language);
139 font.setLanguage(languages.getLanguage(next));
141 } else if (token == "toggleall") {
142 toggle = lex.getBool();
145 // Unrecognised token
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->owner()->message(_("Character set"));
184 void emph(BufferView * bv)
186 LyXFont font(LyXFont::ALL_IGNORE);
187 font.setEmph(LyXFont::TOGGLE);
188 toggleAndShow(bv, font);
192 void bold(BufferView * bv)
194 LyXFont font(LyXFont::ALL_IGNORE);
195 font.setSeries(LyXFont::BOLD_SERIES);
196 toggleAndShow(bv, font);
200 void noun(BufferView * bv)
202 LyXFont font(LyXFont::ALL_IGNORE);
203 font.setNoun(LyXFont::TOGGLE);
204 toggleAndShow(bv, font);
208 void number(BufferView * bv)
210 LyXFont font(LyXFont::ALL_IGNORE);
211 font.setNumber(LyXFont::TOGGLE);
212 toggleAndShow(bv, font);
216 void lang(BufferView * bv, string const & l)
218 Language const * lang = languages.getLanguage(l);
222 LyXFont font(LyXFont::ALL_IGNORE);
223 font.setLanguage(lang);
224 toggleAndShow(bv, font);
228 bool changeDepth(BufferView * bv, LyXText * text, DEPTH_CHANGE type, bool test_only)
230 if (!bv->available() || !text)
234 return text->changeDepth(type, true);
236 bool const changed = text->changeDepth(type, false);
237 if (text->inset_owner)
238 bv->updateInset(text->inset_owner);
243 void code(BufferView * bv)
245 LyXFont font(LyXFont::ALL_IGNORE);
246 font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
247 toggleAndShow(bv, font);
251 void sans(BufferView * bv)
253 LyXFont font(LyXFont::ALL_IGNORE);
254 font.setFamily(LyXFont::SANS_FAMILY);
255 toggleAndShow(bv, font);
259 void roman(BufferView * bv)
261 LyXFont font(LyXFont::ALL_IGNORE);
262 font.setFamily(LyXFont::ROMAN_FAMILY);
263 toggleAndShow(bv, font);
267 void styleReset(BufferView * bv)
269 LyXFont font(LyXFont::ALL_INHERIT, ignore_language);
270 toggleAndShow(bv, font);
274 void underline(BufferView * bv)
276 LyXFont font(LyXFont::ALL_IGNORE);
277 font.setUnderbar(LyXFont::TOGGLE);
278 toggleAndShow(bv, font);
282 void fontSize(BufferView * bv, string const & size)
284 LyXFont font(LyXFont::ALL_IGNORE);
285 font.setLyXSize(size);
286 toggleAndShow(bv, font);
290 // Returns the current font and depth as a message.
291 string const currentState(BufferView * bv)
293 if (!bv->available())
297 return mathcursor->info();
301 LyXText * text = bv->getLyXText();
302 Buffer * buffer = bv->buffer();
303 LyXCursor const & c(text->cursor);
305 bool const show_change = buffer->params().tracking_changes
306 && c.pos() != c.par()->size()
307 && c.par()->lookupChange(c.pos()) != Change::UNCHANGED;
310 Change change(c.par()->lookupChangeFull(c.pos()));
311 Author const & a(bv->buffer()->params().authors().get(change.author));
312 state << _("Change: ") << a.name();
313 if (!a.email().empty()) {
314 state << " (" << a.email() << ")";
316 if (change.changetime)
317 state << _(" at ") << ctime(&change.changetime);
321 // I think we should only show changes from the default
323 LyXFont font = text->real_current_font;
324 LyXFont const & defaultfont =
325 buffer->params().getLyXTextClass().defaultfont();
326 font.reduce(defaultfont);
328 // avoid _(...) re-entrance problem
329 string const s = font.stateText(&buffer->params());
330 state << bformat(_("Font: %1$s"), s);
332 // state << bformat(_("Font: %1$s"), font.stateText(&buffer->params));
334 // The paragraph depth
335 int depth = text->getDepth();
337 state << bformat(_(", Depth: %1$s"), tostr(depth));
339 // The paragraph spacing, but only if different from
341 if (!text->cursor.par()->params().spacing().isDefault()) {
342 Spacing::Space cur_space =
343 text->cursor.par()->params().spacing().getSpace();
344 state << _(", Spacing: ");
347 case Spacing::Single:
348 state << _("Single");
350 case Spacing::Onehalf:
351 state << _("OneHalf");
353 case Spacing::Double:
354 state << _("Double");
357 state << _("Other (")
358 << text->cursor.par()->params().spacing().getValue()
361 case Spacing::Default:
362 // should never happen, do nothing
367 state << _(", Paragraph: ") << text->cursor.par()->id();
368 state << _(", Position: ") << text->cursor.pos();
369 RowList::iterator rit = text->cursorRow();
370 state << bformat(_(", Row b:%1$d e:%2$d"), rit->pos(), rit->end());
371 state << _(", Inset: ");
372 InsetOld * inset = text->cursor.par()->inInset();
374 state << inset << " id: " << inset->id()
375 << " text: " << inset->getLyXText(bv, true)
376 << " owner: " << inset->owner();
384 /* Does the actual toggle job of the calls above.
385 * Also shows the current font state.
387 void toggleAndShow(BufferView * bv, LyXFont const & font, bool toggleall)
389 if (!bv->available())
392 if (bv->theLockingInset()) {
393 bv->theLockingInset()->setFont(bv, font, toggleall);
397 LyXText * text = bv->getLyXText();
398 text->toggleFree(font, toggleall);
401 if (font.language() != ignore_language ||
402 font.number() != LyXFont::IGNORE) {
403 LyXCursor & cursor = text->cursor;
404 text->computeBidiTables(text->cursor.par(), *bv->buffer(),
406 if (cursor.boundary() !=
407 text->isBoundary(*bv->buffer(), *cursor.par(), cursor.pos(),
408 text->real_current_font))
409 text->setCursor(cursor.par(), cursor.pos(),
410 false, !cursor.boundary());
415 // deletes a selection during an insertion
416 void replaceSelection(LyXText * lt)
418 if (lt->selection.set()) {
419 lt->cutSelection(true, false);
424 }; // namespace bv_funcs