]> git.lyx.org Git - lyx.git/blob - src/bufferview_funcs.C
more shuffling...
[lyx.git] / src / bufferview_funcs.C
1 /**
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.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Jean-Marc Lasgouttes
8  * \author John Levon
9  * \author Angus Leeming
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #include <config.h>
15
16 #include "bufferview_funcs.h"
17
18 #include "author.h"
19 #include "buffer.h"
20 #include "bufferparams.h"
21 #include "BufferView.h"
22 #include "cursor.h"
23 #include "gettext.h"
24 #include "language.h"
25 #include "LColor.h"
26 #include "lyxlex.h"
27 #include "lyxrow.h"
28 #include "paragraph.h"
29 #include "ParagraphParameters.h"
30 #include "iterators.h"
31
32 #include "frontends/Alert.h"
33 #include "frontends/LyXView.h"
34
35 #include "insets/insettext.h"
36
37 #include "mathed/math_cursor.h"
38
39 #include "support/tostr.h"
40
41 #include "support/std_sstream.h"
42
43 using lyx::support::bformat;
44
45 using std::istringstream;
46 using std::ostringstream;
47 using std::string;
48
49
50 namespace bv_funcs {
51
52 // Set data using font and toggle
53 // If successful, returns true
54 bool font2string(LyXFont const & font, bool toggle, string & data)
55 {
56         string lang = "ignore";
57         if (font.language())
58                 lang = font.language()->lang();
59
60         ostringstream os;
61         os << "family " << font.family() << '\n'
62            << "series " << font.series() << '\n'
63            << "shape " << font.shape() << '\n'
64            << "size " << font.size() << '\n'
65            << "emph " << font.emph() << '\n'
66            << "underbar " << font.underbar() << '\n'
67            << "noun " << font.noun() << '\n'
68            << "number " << font.number() << '\n'
69            << "color " << font.color() << '\n'
70            << "language " << lang << '\n'
71            << "toggleall " << tostr(toggle);
72         data = os.str();
73         return true;
74 }
75
76
77 // Set font and toggle using data
78 // If successful, returns true
79 bool string2font(string const & data, LyXFont & font, bool & toggle)
80 {
81         istringstream is(data);
82         LyXLex lex(0,0);
83         lex.setStream(is);
84
85         int nset = 0;
86         while (lex.isOK()) {
87                 string token;
88                 if (lex.next())
89                         token = lex.getString();
90
91                 if (token.empty() || !lex.next())
92                         break;
93
94                 if (token == "family") {
95                         int const next = lex.getInteger();
96                         font.setFamily(LyXFont::FONT_FAMILY(next));
97
98                 } else if (token == "series") {
99                         int const next = lex.getInteger();
100                         font.setSeries(LyXFont::FONT_SERIES(next));
101
102                 } else if (token == "shape") {
103                         int const next = lex.getInteger();
104                         font.setShape(LyXFont::FONT_SHAPE(next));
105
106                 } else if (token == "size") {
107                         int const next = lex.getInteger();
108                         font.setSize(LyXFont::FONT_SIZE(next));
109
110                 } else if (token == "emph" || token == "underbar" ||
111                            token == "noun" || token == "number") {
112
113                         int const next = lex.getInteger();
114                         LyXFont::FONT_MISC_STATE const misc =
115                                 LyXFont::FONT_MISC_STATE(next);
116
117                         if (token == "emph")
118                             font.setEmph(misc);
119                         else if (token == "underbar")
120                                 font.setUnderbar(misc);
121                         else if (token == "noun")
122                                 font.setNoun(misc);
123                         else if (token == "number")
124                                 font.setNumber(misc);
125
126                 } else if (token == "color") {
127                         int const next = lex.getInteger();
128                         font.setColor(LColor::color(next));
129
130                 } else if (token == "language") {
131                         string const next = lex.getString();
132                         if (next == "ignore")
133                                 font.setLanguage(ignore_language);
134                         else
135                                 font.setLanguage(languages.getLanguage(next));
136
137                 } else if (token == "toggleall") {
138                         toggle = lex.getBool();
139
140                 } else {
141                         // Unrecognised token
142                         break;
143                 }
144
145                 ++nset;
146         }
147         return (nset > 0);
148 }
149
150
151 bool changeDepthAllowed(BufferView * bv, LyXText * text, DEPTH_CHANGE type)
152 {
153         if (!bv->available() || !text)
154                 return false;
155
156         return text->changeDepthAllowed(type);
157 }
158
159
160 void changeDepth(BufferView * bv, LyXText * text, DEPTH_CHANGE type)
161 {
162         if (!bv->available() || !text)
163                 return;
164         text->changeDepth(type);
165 }
166
167
168 // Returns the current font and depth as a message.
169 string const currentState(BufferView * bv)
170 {
171         if (!bv->available())
172                 return string();
173
174         if (mathcursor)
175                 return mathcursor->info();
176
177         ostringstream state;
178
179         LyXText * text = bv->getLyXText();
180         Buffer * buffer = bv->buffer();
181         CursorSlice const & c = text->cursor();
182
183         bool const show_change = buffer->params().tracking_changes
184                 && text->cursor().pos() != text->cursorPar()->size()
185                 && text->cursorPar()->lookupChange(c.pos()) != Change::UNCHANGED;
186
187         if (show_change) {
188                 Change change = text->cursorPar()->lookupChangeFull(c.pos());
189                 Author const & a = bv->buffer()->params().authors().get(change.author);
190                 state << _("Change: ") << a.name();
191                 if (!a.email().empty())
192                         state << " (" << a.email() << ")";
193                 if (change.changetime)
194                         state << _(" at ") << ctime(&change.changetime);
195                 state << " : ";
196         }
197
198         // I think we should only show changes from the default
199         // font. (Asger)
200         LyXFont font = text->real_current_font;
201         font.reduce(buffer->params().getLyXTextClass().defaultfont());
202
203         // avoid _(...) re-entrance problem
204         string const s = font.stateText(&buffer->params());
205         state << bformat(_("Font: %1$s"), s);
206
207         // state << bformat(_("Font: %1$s"), font.stateText(&buffer->params));
208
209         // The paragraph depth
210         int depth = text->getDepth();
211         if (depth > 0)
212                 state << bformat(_(", Depth: %1$s"), tostr(depth));
213
214         // The paragraph spacing, but only if different from
215         // buffer spacing.
216         if (!text->cursorPar()->params().spacing().isDefault()) {
217                 Spacing::Space cur_space =
218                         text->cursorPar()->params().spacing().getSpace();
219                 state << _(", Spacing: ");
220
221                 switch (cur_space) {
222                 case Spacing::Single:
223                         state << _("Single");
224                         break;
225                 case Spacing::Onehalf:
226                         state << _("OneHalf");
227                         break;
228                 case Spacing::Double:
229                         state << _("Double");
230                         break;
231                 case Spacing::Other:
232                         state << _("Other (")
233                               << text->cursorPar()->params().spacing().getValue()
234                               << ')';
235                         break;
236                 case Spacing::Default:
237                         // should never happen, do nothing
238                         break;
239                 }
240         }
241 #ifdef DEVEL_VERSION
242         ParagraphList::iterator pit = text->cursorPar();
243         state << _(", Paragraph: ") << pit->id();
244         state << _(", Position: ") << text->cursor().pos();
245         RowList::iterator rit = pit->getRow(text->cursor().pos());
246         state << bformat(_(", Row b:%1$d e:%2$d"), rit->pos(), rit->endpos());
247         state << _(", Inset: ");
248         InsetOld * inset = pit->inInset();
249         if (inset)
250                 state << inset << " owner: " << inset->owner();
251         else
252                 state << -1;
253 #endif
254         return state.str();
255 }
256
257
258 // deletes a selection during an insertion
259 void replaceSelection(LyXText * text)
260 {
261         if (text->bv()->selection().set()) {
262                 text->cutSelection(true, false);
263                 text->bv()->update();
264         }
265 }
266
267 } // namespace bv_funcs