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"
29 #include "PosIterator.h"
30 #include "iterators.h"
32 #include "frontends/Alert.h"
33 #include "frontends/LyXView.h"
35 #include "insets/insettext.h"
37 #include "mathed/math_cursor.h"
39 #include "support/tostr.h"
41 #include "support/std_sstream.h"
43 using lyx::support::bformat;
45 using std::istringstream;
46 using std::ostringstream;
52 LyXFont freefont(LyXFont::ALL_IGNORE);
53 bool toggleall(false);
59 // Set data using font and toggle
60 // If successful, returns true
61 bool font2string(LyXFont const & font, bool toggle, string & data)
63 string lang = "ignore";
65 lang = font.language()->lang();
68 os << "family " << font.family() << '\n'
69 << "series " << font.series() << '\n'
70 << "shape " << font.shape() << '\n'
71 << "size " << font.size() << '\n'
72 << "emph " << font.emph() << '\n'
73 << "underbar " << font.underbar() << '\n'
74 << "noun " << font.noun() << '\n'
75 << "number " << font.number() << '\n'
76 << "color " << font.color() << '\n'
77 << "language " << lang << '\n'
78 << "toggleall " << tostr(toggle);
84 // Set font and toggle using data
85 // If successful, returns true
86 bool string2font(string const & data, LyXFont & font, bool & toggle)
88 istringstream is(data);
96 token = lex.getString();
98 if (token.empty() || !lex.next())
101 if (token == "family") {
102 int const next = lex.getInteger();
103 font.setFamily(LyXFont::FONT_FAMILY(next));
105 } else if (token == "series") {
106 int const next = lex.getInteger();
107 font.setSeries(LyXFont::FONT_SERIES(next));
109 } else if (token == "shape") {
110 int const next = lex.getInteger();
111 font.setShape(LyXFont::FONT_SHAPE(next));
113 } else if (token == "size") {
114 int const next = lex.getInteger();
115 font.setSize(LyXFont::FONT_SIZE(next));
117 } else if (token == "emph" || token == "underbar" ||
118 token == "noun" || token == "number") {
120 int const next = lex.getInteger();
121 LyXFont::FONT_MISC_STATE const misc =
122 LyXFont::FONT_MISC_STATE(next);
126 else if (token == "underbar")
127 font.setUnderbar(misc);
128 else if (token == "noun")
130 else if (token == "number")
131 font.setNumber(misc);
133 } else if (token == "color") {
134 int const next = lex.getInteger();
135 font.setColor(LColor::color(next));
137 } else if (token == "language") {
138 string const next = lex.getString();
139 if (next == "ignore")
140 font.setLanguage(ignore_language);
142 font.setLanguage(languages.getLanguage(next));
144 } else if (token == "toggleall") {
145 toggle = lex.getBool();
148 // Unrecognised token
158 string const freefont2string()
161 if (font2string(freefont, toggleall, data))
167 void update_and_apply_freefont(BufferView * bv, string const & data)
171 if (string2font(data, font, toggle)) {
179 void apply_freefont(BufferView * bv)
181 toggleAndShow(bv, freefont, toggleall);
182 bv->owner()->view_state_changed();
183 bv->owner()->message(_("Character set"));
187 void emph(BufferView * bv)
189 LyXFont font(LyXFont::ALL_IGNORE);
190 font.setEmph(LyXFont::TOGGLE);
191 toggleAndShow(bv, font);
195 void bold(BufferView * bv)
197 LyXFont font(LyXFont::ALL_IGNORE);
198 font.setSeries(LyXFont::BOLD_SERIES);
199 toggleAndShow(bv, font);
203 void noun(BufferView * bv)
205 LyXFont font(LyXFont::ALL_IGNORE);
206 font.setNoun(LyXFont::TOGGLE);
207 toggleAndShow(bv, font);
211 void number(BufferView * bv)
213 LyXFont font(LyXFont::ALL_IGNORE);
214 font.setNumber(LyXFont::TOGGLE);
215 toggleAndShow(bv, font);
219 void lang(BufferView * bv, string const & l)
221 Language const * lang = languages.getLanguage(l);
225 LyXFont font(LyXFont::ALL_IGNORE);
226 font.setLanguage(lang);
227 toggleAndShow(bv, font);
231 bool changeDepth(BufferView * bv, LyXText * text, DEPTH_CHANGE type, bool test_only)
233 if (!bv->available() || !text)
237 return text->changeDepth(type, true);
239 bool const changed = text->changeDepth(type, false);
240 if (text->inset_owner)
241 bv->updateInset(text->inset_owner);
246 void code(BufferView * bv)
248 LyXFont font(LyXFont::ALL_IGNORE);
249 font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
250 toggleAndShow(bv, font);
254 void sans(BufferView * bv)
256 LyXFont font(LyXFont::ALL_IGNORE);
257 font.setFamily(LyXFont::SANS_FAMILY);
258 toggleAndShow(bv, font);
262 void roman(BufferView * bv)
264 LyXFont font(LyXFont::ALL_IGNORE);
265 font.setFamily(LyXFont::ROMAN_FAMILY);
266 toggleAndShow(bv, font);
270 void styleReset(BufferView * bv)
272 LyXFont font(LyXFont::ALL_INHERIT, ignore_language);
273 toggleAndShow(bv, font);
277 void underline(BufferView * bv)
279 LyXFont font(LyXFont::ALL_IGNORE);
280 font.setUnderbar(LyXFont::TOGGLE);
281 toggleAndShow(bv, font);
285 void fontSize(BufferView * bv, string const & size)
287 LyXFont font(LyXFont::ALL_IGNORE);
288 font.setLyXSize(size);
289 toggleAndShow(bv, font);
293 // Returns the current font and depth as a message.
294 string const currentState(BufferView * bv)
296 if (!bv->available())
300 return mathcursor->info();
304 LyXText * text = bv->getLyXText();
305 Buffer * buffer = bv->buffer();
306 LyXCursor const & c = text->cursor;
308 bool const show_change = buffer->params().tracking_changes
309 && text->cursor.pos() != text->cursorPar()->size()
310 && text->cursorPar()->lookupChange(c.pos()) != Change::UNCHANGED;
313 Change change = text->cursorPar()->lookupChangeFull(c.pos());
314 Author const & a = bv->buffer()->params().authors().get(change.author);
315 state << _("Change: ") << a.name();
316 if (!a.email().empty())
317 state << " (" << a.email() << ")";
318 if (change.changetime)
319 state << _(" at ") << ctime(&change.changetime);
323 // I think we should only show changes from the default
325 LyXFont font = text->real_current_font;
326 font.reduce(buffer->params().getLyXTextClass().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->cursorPar()->params().spacing().isDefault()) {
342 Spacing::Space cur_space =
343 text->cursorPar()->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->cursorPar()->params().spacing().getValue()
361 case Spacing::Default:
362 // should never happen, do nothing
367 ParagraphList::iterator pit = text->cursorPar();
368 state << _(", Paragraph: ") << pit->id();
369 state << _(", Position: ") << text->cursor.pos();
370 RowList::iterator rit = pit->getRow(text->cursor.pos());
371 state << bformat(_(", Row b:%1$d e:%2$d"), rit->pos(), rit->endpos());
372 state << _(", Inset: ");
373 InsetOld * inset = pit->inInset();
376 << " text: " << inset->getLyXText(bv, true)
377 << " owner: " << inset->owner();
385 /* Does the actual toggle job of the calls above.
386 * Also shows the current font state.
388 void toggleAndShow(BufferView * bv, LyXFont const & font, bool toggleall)
390 if (!bv->available())
393 if (bv->theLockingInset()) {
394 bv->theLockingInset()->setFont(bv, font, toggleall);
398 LyXText * text = bv->getLyXText();
399 text->toggleFree(font, toggleall);
402 if (font.language() != ignore_language ||
403 font.number() != LyXFont::IGNORE) {
404 LyXCursor & cursor = text->cursor;
405 Paragraph & par = *text->cursorPar();
406 text->bidi.computeTables(par, *bv->buffer(),
407 *par.getRow(cursor.pos()));
408 if (cursor.boundary() !=
409 text->bidi.isBoundary(*bv->buffer(), par,
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 * text)
421 if (text->selection.set()) {
422 text->cutSelection(true, false);
423 text->bv()->update();
428 void put_selection_at(BufferView * bv, PosIterator const & cur,
429 int length, bool backwards)
431 ParIterator par = bv->buffer()->par_iterator_begin();
432 for (; par.pit() != cur.pit(); ++par)
435 bv->getLyXText()->clearSelection();
437 LyXText * text = par.text() ? par.text() : bv->text;
441 text->setCursor(cur.pit(), cur.pos());
444 text->setSelectionRange(length);
445 text->setSelection();
447 text->cursor = text->selection.start;
457 }; // namespace bv_funcs