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"
26 #include "paragraph.h"
27 #include "ParagraphParameters.h"
29 #include "frontends/Alert.h"
30 #include "frontends/LyXView.h"
32 #include "insets/insettext.h"
34 #include "mathed/math_cursor.h"
36 #include "support/tostr.h"
38 #include "support/std_sstream.h"
40 using lyx::support::bformat;
42 using std::istringstream;
43 using std::ostringstream;
48 LyXFont freefont(LyXFont::ALL_IGNORE);
49 bool toggleall(false);
55 // Set data using font and toggle
56 // If successful, returns true
57 bool font2string(LyXFont const & font, bool toggle, string & data)
59 string lang = "ignore";
61 lang = font.language()->lang();
64 os << "family " << font.family() << '\n'
65 << "series " << font.series() << '\n'
66 << "shape " << font.shape() << '\n'
67 << "size " << font.size() << '\n'
68 << "emph " << font.emph() << '\n'
69 << "underbar " << font.underbar() << '\n'
70 << "noun " << font.noun() << '\n'
71 << "number " << font.number() << '\n'
72 << "color " << font.color() << '\n'
73 << "language " << lang << '\n'
74 << "toggleall " << tostr(toggle);
75 data = STRCONV(os.str());
80 // Set font and toggle using data
81 // If successful, returns true
82 bool string2font(string const & data, LyXFont & font, bool & toggle)
84 istringstream is(STRCONV(data));
92 token = lex.getString();
94 if (token.empty() || !lex.next())
97 if (token == "family") {
98 int const next = lex.getInteger();
99 font.setFamily(LyXFont::FONT_FAMILY(next));
101 } else if (token == "series") {
102 int const next = lex.getInteger();
103 font.setSeries(LyXFont::FONT_SERIES(next));
105 } else if (token == "shape") {
106 int const next = lex.getInteger();
107 font.setShape(LyXFont::FONT_SHAPE(next));
109 } else if (token == "size") {
110 int const next = lex.getInteger();
111 font.setSize(LyXFont::FONT_SIZE(next));
113 } else if (token == "emph" || token == "underbar" ||
114 token == "noun" || token == "number") {
116 int const next = lex.getInteger();
117 LyXFont::FONT_MISC_STATE const misc =
118 LyXFont::FONT_MISC_STATE(next);
122 else if (token == "underbar")
123 font.setUnderbar(misc);
124 else if (token == "noun")
126 else if (token == "number")
127 font.setNumber(misc);
129 } else if (token == "color") {
130 int const next = lex.getInteger();
131 font.setColor(LColor::color(next));
133 } else if (token == "language") {
134 string const next = lex.getString();
135 if (next == "ignore")
136 font.setLanguage(ignore_language);
138 font.setLanguage(languages.getLanguage(next));
140 } else if (token == "toggleall") {
141 toggle = lex.getBool();
144 // Unrecognised token
154 string const freefont2string()
157 if (font2string(freefont, toggleall, data))
163 void update_and_apply_freefont(BufferView * bv, string const & data)
167 if (string2font(data, font, toggle)) {
175 void apply_freefont(BufferView * bv)
177 toggleAndShow(bv, freefont, toggleall);
178 bv->owner()->view_state_changed();
179 bv->owner()->message(_("Character set"));
183 void emph(BufferView * bv)
185 LyXFont font(LyXFont::ALL_IGNORE);
186 font.setEmph(LyXFont::TOGGLE);
187 toggleAndShow(bv, font);
191 void bold(BufferView * bv)
193 LyXFont font(LyXFont::ALL_IGNORE);
194 font.setSeries(LyXFont::BOLD_SERIES);
195 toggleAndShow(bv, font);
199 void noun(BufferView * bv)
201 LyXFont font(LyXFont::ALL_IGNORE);
202 font.setNoun(LyXFont::TOGGLE);
203 toggleAndShow(bv, font);
207 void number(BufferView * bv)
209 LyXFont font(LyXFont::ALL_IGNORE);
210 font.setNumber(LyXFont::TOGGLE);
211 toggleAndShow(bv, font);
215 void lang(BufferView * bv, string const & l)
217 Language const * lang = languages.getLanguage(l);
221 LyXFont font(LyXFont::ALL_IGNORE);
222 font.setLanguage(lang);
223 toggleAndShow(bv, font);
227 bool changeDepth(BufferView * bv, LyXText * text, DEPTH_CHANGE type, bool test_only)
229 if (!bv->available() || !text)
233 return text->changeDepth(type, true);
235 bool const changed = text->changeDepth(type, false);
236 if (text->inset_owner)
237 bv->updateInset(text->inset_owner);
242 void code(BufferView * bv)
244 LyXFont font(LyXFont::ALL_IGNORE);
245 font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
246 toggleAndShow(bv, font);
250 void sans(BufferView * bv)
252 LyXFont font(LyXFont::ALL_IGNORE);
253 font.setFamily(LyXFont::SANS_FAMILY);
254 toggleAndShow(bv, font);
258 void roman(BufferView * bv)
260 LyXFont font(LyXFont::ALL_IGNORE);
261 font.setFamily(LyXFont::ROMAN_FAMILY);
262 toggleAndShow(bv, font);
266 void styleReset(BufferView * bv)
268 LyXFont font(LyXFont::ALL_INHERIT, ignore_language);
269 toggleAndShow(bv, font);
273 void underline(BufferView * bv)
275 LyXFont font(LyXFont::ALL_IGNORE);
276 font.setUnderbar(LyXFont::TOGGLE);
277 toggleAndShow(bv, font);
281 void fontSize(BufferView * bv, string const & size)
283 LyXFont font(LyXFont::ALL_IGNORE);
284 font.setLyXSize(size);
285 toggleAndShow(bv, font);
289 // Returns the current font and depth as a message.
290 string const currentState(BufferView * bv)
292 if (!bv->available())
296 return mathcursor->info();
300 LyXText * text = bv->getLyXText();
301 Buffer * buffer = bv->buffer();
302 LyXCursor const & c(text->cursor);
304 bool const show_change = buffer->params().tracking_changes
305 && c.pos() != c.par()->size()
306 && c.par()->lookupChange(c.pos()) != Change::UNCHANGED;
309 Change change(c.par()->lookupChangeFull(c.pos()));
310 Author const & a(bv->buffer()->params().authors().get(change.author));
311 state << _("Change: ") << a.name();
312 if (!a.email().empty()) {
313 state << " (" << a.email() << ")";
315 if (change.changetime)
316 state << _(" at ") << ctime(&change.changetime);
320 // I think we should only show changes from the default
322 LyXFont font = text->real_current_font;
323 LyXFont const & defaultfont =
324 buffer->params().getLyXTextClass().defaultfont();
325 font.reduce(defaultfont);
327 // avoid _(...) re-entrance problem
328 string const s = font.stateText(&buffer->params());
329 state << bformat(_("Font: %1$s"), s);
331 // state << bformat(_("Font: %1$s"), font.stateText(&buffer->params));
333 // The paragraph depth
334 int depth = text->getDepth();
336 state << bformat(_(", Depth: %1$s"), tostr(depth));
338 // The paragraph spacing, but only if different from
340 if (!text->cursor.par()->params().spacing().isDefault()) {
341 Spacing::Space cur_space =
342 text->cursor.par()->params().spacing().getSpace();
343 state << _(", Spacing: ");
346 case Spacing::Single:
347 state << _("Single");
349 case Spacing::Onehalf:
350 state << _("OneHalf");
352 case Spacing::Double:
353 state << _("Double");
356 state << _("Other (")
357 << text->cursor.par()->params().spacing().getValue()
360 case Spacing::Default:
361 // should never happen, do nothing
366 state << _(", Paragraph: ") << text->cursor.par()->id();
367 state << _(", Position: ") << text->cursor.pos();
368 RowList::iterator rit = text->cursorRow();
369 state << bformat(_(", Row b:%1$d e:%2$d"), rit->pos(), rit->end());
370 state << _(", Inset: ") <<
371 (text->cursor.par()->inInset() ? text->cursor.par()->inInset()->id() : -1);
373 return STRCONV(state.str());
377 /* Does the actual toggle job of the calls above.
378 * Also shows the current font state.
380 void toggleAndShow(BufferView * bv, LyXFont const & font, bool toggleall)
382 if (!bv->available())
385 if (bv->theLockingInset()) {
386 bv->theLockingInset()->setFont(bv, font, toggleall);
390 LyXText * text = bv->getLyXText();
391 text->toggleFree(font, toggleall);
394 if (font.language() != ignore_language ||
395 font.number() != LyXFont::IGNORE) {
396 LyXCursor & cursor = text->cursor;
397 text->computeBidiTables(text->cursor.par(), *bv->buffer(),
399 if (cursor.boundary() !=
400 text->isBoundary(*bv->buffer(), *cursor.par(), cursor.pos(),
401 text->real_current_font))
402 text->setCursor(cursor.par(), cursor.pos(),
403 false, !cursor.boundary());
408 // deletes a selection during an insertion
409 void replaceSelection(LyXText * lt)
411 if (lt->selection.set()) {
412 lt->cutSelection(true, false);
417 }; // namespace bv_funcs