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;
50 LyXFont freefont(LyXFont::ALL_IGNORE);
51 bool toggleall(false);
57 // Set data using font and toggle
58 // If successful, returns true
59 bool font2string(LyXFont const & font, bool toggle, string & data)
61 string lang = "ignore";
63 lang = font.language()->lang();
66 os << "family " << font.family() << '\n'
67 << "series " << font.series() << '\n'
68 << "shape " << font.shape() << '\n'
69 << "size " << font.size() << '\n'
70 << "emph " << font.emph() << '\n'
71 << "underbar " << font.underbar() << '\n'
72 << "noun " << font.noun() << '\n'
73 << "number " << font.number() << '\n'
74 << "color " << font.color() << '\n'
75 << "language " << lang << '\n'
76 << "toggleall " << tostr(toggle);
82 // Set font and toggle using data
83 // If successful, returns true
84 bool string2font(string const & data, LyXFont & font, bool & toggle)
86 istringstream is(data);
94 token = lex.getString();
96 if (token.empty() || !lex.next())
99 if (token == "family") {
100 int const next = lex.getInteger();
101 font.setFamily(LyXFont::FONT_FAMILY(next));
103 } else if (token == "series") {
104 int const next = lex.getInteger();
105 font.setSeries(LyXFont::FONT_SERIES(next));
107 } else if (token == "shape") {
108 int const next = lex.getInteger();
109 font.setShape(LyXFont::FONT_SHAPE(next));
111 } else if (token == "size") {
112 int const next = lex.getInteger();
113 font.setSize(LyXFont::FONT_SIZE(next));
115 } else if (token == "emph" || token == "underbar" ||
116 token == "noun" || token == "number") {
118 int const next = lex.getInteger();
119 LyXFont::FONT_MISC_STATE const misc =
120 LyXFont::FONT_MISC_STATE(next);
124 else if (token == "underbar")
125 font.setUnderbar(misc);
126 else if (token == "noun")
128 else if (token == "number")
129 font.setNumber(misc);
131 } else if (token == "color") {
132 int const next = lex.getInteger();
133 font.setColor(LColor::color(next));
135 } else if (token == "language") {
136 string const next = lex.getString();
137 if (next == "ignore")
138 font.setLanguage(ignore_language);
140 font.setLanguage(languages.getLanguage(next));
142 } else if (token == "toggleall") {
143 toggle = lex.getBool();
146 // Unrecognised token
156 string const freefont2string()
159 if (font2string(freefont, toggleall, data))
165 void update_and_apply_freefont(BufferView * bv, string const & data)
169 if (string2font(data, font, toggle)) {
177 void apply_freefont(BufferView * bv)
179 toggleAndShow(bv, freefont, toggleall);
180 bv->owner()->view_state_changed();
181 bv->owner()->message(_("Character set"));
185 void emph(BufferView * bv)
187 LyXFont font(LyXFont::ALL_IGNORE);
188 font.setEmph(LyXFont::TOGGLE);
189 toggleAndShow(bv, font);
193 void bold(BufferView * bv)
195 LyXFont font(LyXFont::ALL_IGNORE);
196 font.setSeries(LyXFont::BOLD_SERIES);
197 toggleAndShow(bv, font);
201 void noun(BufferView * bv)
203 LyXFont font(LyXFont::ALL_IGNORE);
204 font.setNoun(LyXFont::TOGGLE);
205 toggleAndShow(bv, font);
209 void number(BufferView * bv)
211 LyXFont font(LyXFont::ALL_IGNORE);
212 font.setNumber(LyXFont::TOGGLE);
213 toggleAndShow(bv, font);
217 void lang(BufferView * bv, string const & l)
219 Language const * lang = languages.getLanguage(l);
223 LyXFont font(LyXFont::ALL_IGNORE);
224 font.setLanguage(lang);
225 toggleAndShow(bv, font);
229 bool changeDepth(BufferView * bv, LyXText * text, DEPTH_CHANGE type, bool test_only)
231 if (!bv->available() || !text)
235 return text->changeDepth(type, true);
237 bool const changed = text->changeDepth(type, false);
238 if (text->inset_owner)
239 bv->updateInset(text->inset_owner);
244 void code(BufferView * bv)
246 LyXFont font(LyXFont::ALL_IGNORE);
247 font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
248 toggleAndShow(bv, font);
252 void sans(BufferView * bv)
254 LyXFont font(LyXFont::ALL_IGNORE);
255 font.setFamily(LyXFont::SANS_FAMILY);
256 toggleAndShow(bv, font);
260 void roman(BufferView * bv)
262 LyXFont font(LyXFont::ALL_IGNORE);
263 font.setFamily(LyXFont::ROMAN_FAMILY);
264 toggleAndShow(bv, font);
268 void styleReset(BufferView * bv)
270 LyXFont font(LyXFont::ALL_INHERIT, ignore_language);
271 toggleAndShow(bv, font);
275 void underline(BufferView * bv)
277 LyXFont font(LyXFont::ALL_IGNORE);
278 font.setUnderbar(LyXFont::TOGGLE);
279 toggleAndShow(bv, font);
283 void fontSize(BufferView * bv, string const & size)
285 LyXFont font(LyXFont::ALL_IGNORE);
286 font.setLyXSize(size);
287 toggleAndShow(bv, font);
291 // Returns the current font and depth as a message.
292 string const currentState(BufferView * bv)
294 if (!bv->available())
298 return mathcursor->info();
302 LyXText * text = bv->getLyXText();
303 Buffer * buffer = bv->buffer();
304 LyXCursor const & c = text->cursor;
306 bool const show_change = buffer->params().tracking_changes
307 && text->cursor.pos() != text->cursorPar()->size()
308 && text->cursorPar()->lookupChange(c.pos()) != Change::UNCHANGED;
311 Change change = text->cursorPar()->lookupChangeFull(c.pos());
312 Author const & a = bv->buffer()->params().authors().get(change.author);
313 state << _("Change: ") << a.name();
314 if (!a.email().empty())
315 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 font.reduce(buffer->params().getLyXTextClass().defaultfont());
326 // avoid _(...) re-entrance problem
327 string const s = font.stateText(&buffer->params());
328 state << bformat(_("Font: %1$s"), s);
330 // state << bformat(_("Font: %1$s"), font.stateText(&buffer->params));
332 // The paragraph depth
333 int depth = text->getDepth();
335 state << bformat(_(", Depth: %1$s"), tostr(depth));
337 // The paragraph spacing, but only if different from
339 if (!text->cursorPar()->params().spacing().isDefault()) {
340 Spacing::Space cur_space =
341 text->cursorPar()->params().spacing().getSpace();
342 state << _(", Spacing: ");
345 case Spacing::Single:
346 state << _("Single");
348 case Spacing::Onehalf:
349 state << _("OneHalf");
351 case Spacing::Double:
352 state << _("Double");
355 state << _("Other (")
356 << text->cursorPar()->params().spacing().getValue()
359 case Spacing::Default:
360 // should never happen, do nothing
365 ParagraphList::iterator pit = text->cursorPar();
366 state << _(", Paragraph: ") << pit->id();
367 state << _(", Position: ") << text->cursor.pos();
368 RowList::iterator rit = pit->getRow(text->cursor.pos());
369 state << bformat(_(", Row b:%1$d e:%2$d"), rit->pos(), rit->endpos());
370 state << _(", Inset: ");
371 InsetOld * inset = pit->inInset();
373 state << inset << " id: " << inset->id()
374 << " text: " << inset->getLyXText(bv, true)
375 << " owner: " << inset->owner();
383 /* Does the actual toggle job of the calls above.
384 * Also shows the current font state.
386 void toggleAndShow(BufferView * bv, LyXFont const & font, bool toggleall)
388 if (!bv->available())
391 if (bv->theLockingInset()) {
392 bv->theLockingInset()->setFont(bv, font, toggleall);
396 LyXText * text = bv->getLyXText();
397 text->toggleFree(font, toggleall);
400 if (font.language() != ignore_language ||
401 font.number() != LyXFont::IGNORE) {
402 LyXCursor & cursor = text->cursor;
403 Paragraph & par = *text->cursorPar();
404 text->bidi.computeTables(par, *bv->buffer(),
405 *par.getRow(cursor.pos()));
406 if (cursor.boundary() !=
407 text->bidi.isBoundary(*bv->buffer(), par,
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 * text)
419 if (text->selection.set()) {
420 text->cutSelection(true, false);
421 text->bv()->update();
425 }; // namespace bv_funcs