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"
28 #include "paragraph.h"
29 #include "ParagraphParameters.h"
30 #include "iterators.h"
32 #include "frontends/Alert.h"
33 #include "frontends/LyXView.h"
35 #include "insets/insettext.h"
37 #include "support/tostr.h"
38 #include "support/std_sstream.h"
40 using lyx::support::bformat;
42 using std::istringstream;
43 using std::ostringstream;
49 // Set data using font and toggle
50 // If successful, returns true
51 bool font2string(LyXFont const & font, bool toggle, string & data)
53 string lang = "ignore";
55 lang = font.language()->lang();
58 os << "family " << font.family() << '\n'
59 << "series " << font.series() << '\n'
60 << "shape " << font.shape() << '\n'
61 << "size " << font.size() << '\n'
62 << "emph " << font.emph() << '\n'
63 << "underbar " << font.underbar() << '\n'
64 << "noun " << font.noun() << '\n'
65 << "number " << font.number() << '\n'
66 << "color " << font.color() << '\n'
67 << "language " << lang << '\n'
68 << "toggleall " << tostr(toggle);
74 // Set font and toggle using data
75 // If successful, returns true
76 bool string2font(string const & data, LyXFont & font, bool & toggle)
78 istringstream is(data);
86 token = lex.getString();
88 if (token.empty() || !lex.next())
91 if (token == "family") {
92 int const next = lex.getInteger();
93 font.setFamily(LyXFont::FONT_FAMILY(next));
95 } else if (token == "series") {
96 int const next = lex.getInteger();
97 font.setSeries(LyXFont::FONT_SERIES(next));
99 } else if (token == "shape") {
100 int const next = lex.getInteger();
101 font.setShape(LyXFont::FONT_SHAPE(next));
103 } else if (token == "size") {
104 int const next = lex.getInteger();
105 font.setSize(LyXFont::FONT_SIZE(next));
107 } else if (token == "emph" || token == "underbar" ||
108 token == "noun" || token == "number") {
110 int const next = lex.getInteger();
111 LyXFont::FONT_MISC_STATE const misc =
112 LyXFont::FONT_MISC_STATE(next);
116 else if (token == "underbar")
117 font.setUnderbar(misc);
118 else if (token == "noun")
120 else if (token == "number")
121 font.setNumber(misc);
123 } else if (token == "color") {
124 int const next = lex.getInteger();
125 font.setColor(LColor::color(next));
127 } else if (token == "language") {
128 string const next = lex.getString();
129 if (next == "ignore")
130 font.setLanguage(ignore_language);
132 font.setLanguage(languages.getLanguage(next));
134 } else if (token == "toggleall") {
135 toggle = lex.getBool();
138 // Unrecognised token
148 bool changeDepthAllowed(BufferView * bv, LyXText * text, DEPTH_CHANGE type)
150 return bv->available() && text && text->changeDepthAllowed(type);
154 void changeDepth(BufferView * bv, LyXText * text, DEPTH_CHANGE type)
156 if (bv->available() && text)
157 text->changeDepth(type);
161 // Returns the current font and depth as a message.
162 string const currentState(BufferView * bv)
166 if (!bv->available())
169 if (bv->cursor().inMathed()) {
170 bv->cursor().info(state);
174 LyXText * text = bv->getLyXText();
175 Buffer * buffer = bv->buffer();
176 CursorSlice const & c = text->cursor();
178 bool const show_change = buffer->params().tracking_changes
179 && text->cursor().pos() != text->cursorPar()->size()
180 && text->cursorPar()->lookupChange(c.pos()) != Change::UNCHANGED;
183 Change change = text->cursorPar()->lookupChangeFull(c.pos());
184 Author const & a = bv->buffer()->params().authors().get(change.author);
185 state << _("Change: ") << a.name();
186 if (!a.email().empty())
187 state << " (" << a.email() << ")";
188 if (change.changetime)
189 state << _(" at ") << ctime(&change.changetime);
193 // I think we should only show changes from the default
195 LyXFont font = text->real_current_font;
196 font.reduce(buffer->params().getLyXTextClass().defaultfont());
198 // avoid _(...) re-entrance problem
199 string const s = font.stateText(&buffer->params());
200 state << bformat(_("Font: %1$s"), s);
202 // state << bformat(_("Font: %1$s"), font.stateText(&buffer->params));
204 // The paragraph depth
205 int depth = text->getDepth();
207 state << bformat(_(", Depth: %1$s"), tostr(depth));
209 // The paragraph spacing, but only if different from
211 if (!text->cursorPar()->params().spacing().isDefault()) {
212 Spacing::Space cur_space =
213 text->cursorPar()->params().spacing().getSpace();
214 state << _(", Spacing: ");
217 case Spacing::Single:
218 state << _("Single");
220 case Spacing::Onehalf:
221 state << _("OneHalf");
223 case Spacing::Double:
224 state << _("Double");
227 state << _("Other (")
228 << text->cursorPar()->params().spacing().getValue()
231 case Spacing::Default:
232 // should never happen, do nothing
237 ParagraphList::iterator pit = text->cursorPar();
238 state << _(", Paragraph: ") << pit->id();
239 state << _(", Position: ") << text->cursor().pos();
240 RowList::iterator rit = pit->getRow(text->cursor().pos());
241 state << bformat(_(", Row b:%1$d e:%2$d"), rit->pos(), rit->endpos());
242 state << _(", Inset: ");
243 InsetOld * inset = pit->inInset();
245 state << inset << " owner: " << inset->owner();
253 // deletes a selection during an insertion
254 void replaceSelection(LyXText * text)
256 if (text->bv()->cursor().selection()) {
257 text->cutSelection(true, false);
258 text->bv()->update();
262 } // namespace bv_funcs