]> git.lyx.org Git - lyx.git/blob - src/bufferview_funcs.C
currentState() should show mathed status not lyxtext, when in
[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 #include "BufferView.h"
18 #include "paragraph.h"
19 #include "lyxfont.h"
20 #include "lyxlex.h"
21 #include "lyxtext.h"
22 #include "buffer.h"
23 #include "lyx_cb.h"
24 #include "language.h"
25 #include "gettext.h"
26 #include "ParagraphParameters.h"
27 #include "author.h"
28 #include "changes.h"
29
30 #include "frontends/LyXView.h"
31 #include "frontends/Alert.h"
32 #include "mathed/math_cursor.h"
33
34 #include "support/lstrings.h"
35 #include "Lsstream.h"
36
37 #include "insets/updatableinset.h"
38
39 #include "support/BoostFormat.h"
40
41 namespace {
42
43 LyXFont freefont(LyXFont::ALL_IGNORE);
44 bool toggleall(false);
45
46 }
47
48 namespace bv_funcs {
49
50 // Set data using font and toggle
51 // If successful, returns true
52 bool font2string(LyXFont const & font, bool toggle, string & data)
53 {
54         string lang = "ignore";
55         if (font.language())
56                 lang = font.language()->lang();
57
58         ostringstream os;
59         os << "family " << font.family() << '\n'
60            << "series " << font.series() << '\n'
61            << "shape " << font.shape() << '\n'
62            << "size " << font.size() << '\n'
63            << "emph " << font.emph() << '\n'
64            << "underbar " << font.underbar() << '\n'
65            << "noun " << font.noun() << '\n'
66            << "number " << font.number() << '\n'
67            << "color " << font.color() << '\n'
68            << "language " << lang << '\n'
69            << "toggleall " << tostr(toggle);
70         data = os.str();
71         return true;
72 }
73
74
75 // Set font and toggle using data
76 // If successful, returns true
77 bool string2font(string const & data, LyXFont & font, bool & toggle)
78 {
79         istringstream is(data);
80         LyXLex lex(0,0);
81         lex.setStream(is);
82
83         int nset = 0;
84         while (lex.isOK()) {
85                 string token;
86                 if (lex.next())
87                         token = lex.getString();
88
89                 if (token.empty() || !lex.next())
90                         break;
91
92                 if (token == "family") {
93                         int const next = lex.getInteger();
94                         font.setFamily(LyXFont::FONT_FAMILY(next));
95
96                 } else if (token == "series") {
97                         int const next = lex.getInteger();
98                         font.setSeries(LyXFont::FONT_SERIES(next));
99
100                 } else if (token == "shape") {
101                         int const next = lex.getInteger();
102                         font.setShape(LyXFont::FONT_SHAPE(next));
103
104                 } else if (token == "size") {
105                         int const next = lex.getInteger();
106                         font.setSize(LyXFont::FONT_SIZE(next));
107
108                 } else if (token == "emph" || token == "underbar" ||
109                            token == "noun" || token == "number") {
110
111                         int const next = lex.getInteger();
112                         LyXFont::FONT_MISC_STATE const misc =
113                                 LyXFont::FONT_MISC_STATE(next);
114
115                         if (token == "emph")
116                             font.setEmph(misc);
117                         else if (token == "underbar")
118                                 font.setUnderbar(misc);
119                         else if (token == "noun")
120                                 font.setNoun(misc);
121                         else if (token == "number")
122                                 font.setNumber(misc);
123
124                 } else if (token == "color") {
125                         int const next = lex.getInteger();
126                         font.setColor(LColor::color(next));
127
128                 } else if (token == "language") {
129                         string const next = lex.getString();
130                         if (next == "ignore")
131                                 font.setLanguage(ignore_language);
132                         else
133                                 font.setLanguage(languages.getLanguage(next));
134
135                 } else if (token == "toggleall") {
136                         toggle = lex.getBool();
137
138                 } else {
139                         // Unrecognised token
140                         break;
141                 }
142
143                 ++nset;
144         }
145         return (nset > 0);
146 }
147
148
149 string const freefont2string()
150 {
151         string data;
152         if (font2string(freefont, toggleall, data))
153                 return data;
154         return string();
155 }
156
157
158 void update_and_apply_freefont(BufferView * bv, string const & data)
159 {
160         LyXFont font;
161         bool toggle;
162         if (string2font(data, font, toggle)) {
163                 freefont = font;
164                 toggleall = toggle;
165                 apply_freefont(bv);
166         }
167 }
168
169
170 void apply_freefont(BufferView * bv)
171 {
172         toggleAndShow(bv, freefont, toggleall);
173         bv->owner()->view_state_changed();
174         bv->owner()->message(_("Character set"));
175 }
176
177
178 void emph(BufferView * bv)
179 {
180         LyXFont font(LyXFont::ALL_IGNORE);
181         font.setEmph(LyXFont::TOGGLE);
182         toggleAndShow(bv, font);
183 }
184
185
186 void bold(BufferView * bv)
187 {
188         LyXFont font(LyXFont::ALL_IGNORE);
189         font.setSeries(LyXFont::BOLD_SERIES);
190         toggleAndShow(bv, font);
191 }
192
193
194 void noun(BufferView * bv)
195 {
196         LyXFont font(LyXFont::ALL_IGNORE);
197         font.setNoun(LyXFont::TOGGLE);
198         toggleAndShow(bv, font);
199 }
200
201
202 void number(BufferView * bv)
203 {
204         LyXFont font(LyXFont::ALL_IGNORE);
205         font.setNumber(LyXFont::TOGGLE);
206         toggleAndShow(bv, font);
207 }
208
209 void lang(BufferView * bv, string const & l)
210 {
211         LyXFont font(LyXFont::ALL_IGNORE);
212         Language const * lang = languages.getLanguage(l);
213         if (!lang)
214                 return;
215
216         font.setLanguage(lang);
217         toggleAndShow(bv, font);
218 }
219
220
221 bool changeDepth(BufferView * bv, LyXText * text, DEPTH_CHANGE type, bool test_only)
222 {
223         if (!bv->available() || !text)
224             return false;
225
226         if (test_only)
227                 return text->changeDepth(type, true);
228
229         bv->hideCursor();
230         bv->update(BufferView::SELECT);
231         bool const changed = text->changeDepth(type, false);
232         if (text->inset_owner)
233                 bv->updateInset((Inset *)text->inset_owner);
234         bv->update(BufferView::SELECT);
235         return changed;
236 }
237
238
239 void code(BufferView * bv)
240 {
241         LyXFont font(LyXFont::ALL_IGNORE);
242         font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
243         toggleAndShow(bv, font);
244 }
245
246
247 void sans(BufferView * bv)
248 {
249         LyXFont font(LyXFont::ALL_IGNORE);
250         font.setFamily(LyXFont::SANS_FAMILY);
251         toggleAndShow(bv, font);
252 }
253
254
255 void roman(BufferView * bv)
256 {
257         LyXFont font(LyXFont::ALL_IGNORE);
258         font.setFamily(LyXFont::ROMAN_FAMILY);
259         toggleAndShow(bv, font);
260 }
261
262
263 void styleReset(BufferView * bv)
264 {
265         LyXFont font(LyXFont::ALL_INHERIT, ignore_language);
266         toggleAndShow(bv, font);
267 }
268
269
270 void underline(BufferView * bv)
271 {
272         LyXFont font(LyXFont::ALL_IGNORE);
273         font.setUnderbar(LyXFont::TOGGLE);
274         toggleAndShow(bv, font);
275 }
276
277
278 void fontSize(BufferView * bv, string const & size)
279 {
280         LyXFont font(LyXFont::ALL_IGNORE);
281         font.setLyXSize(size);
282         toggleAndShow(bv, font);
283 }
284
285
286 // Returns the current font and depth as a message.
287 string const currentState(BufferView * bv)
288 {
289         if (!bv->available())
290                 return string();
291
292         if (mathcursor)
293                 return mathcursor->info();
294
295         ostringstream state;
296
297         LyXText * text = bv->getLyXText();
298         Buffer * buffer = bv->buffer();
299         LyXCursor const & c(text->cursor);
300
301         bool const show_change = buffer->params.tracking_changes
302                 && c.pos() != c.par()->size()
303                 && c.par()->lookupChange(c.pos()) != Change::UNCHANGED;
304
305         if (show_change) {
306                 Change change(c.par()->lookupChangeFull(c.pos()));
307                 Author const & a(bv->buffer()->authors().get(change.author));
308                 state << _("Change: ") << a.name();
309                 if (!a.email().empty()) {
310                         state << " (" << a.email() << ")";
311                 }
312                 if (change.changetime)
313                         state << _(" at ") << ctime(&change.changetime);
314                 state << " : ";
315         }
316
317         // I think we should only show changes from the default
318         // font. (Asger)
319         LyXFont font = text->real_current_font;
320         LyXFont const & defaultfont =
321                 buffer->params.getLyXTextClass().defaultfont();
322         font.reduce(defaultfont);
323
324 #if USE_BOOST_FORMAT
325         state << boost::format(_("Font: %1$s")) % font.stateText(&buffer->params);
326 #else
327         state << _("Font: ") << font.stateText(&buffer->params);
328 #endif
329
330         // The paragraph depth
331         int depth = text->getDepth();
332         if (depth > 0) {
333 #if USE_BOOST_FORMAT
334                 state << boost::format(_(", Depth: %1$d")) % depth;
335 #else
336                 state << _(", Depth: ") << depth;
337 #endif
338         }
339
340
341         // The paragraph spacing, but only if different from
342         // buffer spacing.
343         if (!text->cursor.par()->params().spacing().isDefault()) {
344                 Spacing::Space cur_space =
345                         text->cursor.par()->params().spacing().getSpace();
346                 state << _(", Spacing: ");
347
348                 switch (cur_space) {
349                 case Spacing::Single:
350                         state << _("Single");
351                         break;
352                 case Spacing::Onehalf:
353                         state << _("OneHalf");
354                         break;
355                 case Spacing::Double:
356                         state << _("Double");
357                         break;
358                 case Spacing::Other:
359                         state << _("Other (")
360                               << text->cursor.par()->params().spacing().getValue()
361                               << ')';
362                         break;
363                 case Spacing::Default:
364                         // should never happen, do nothing
365                         break;
366                 }
367         }
368 #ifdef DEVEL_VERSION
369         state << _(", Paragraph: ") << text->cursor.par()->id();
370 #endif
371         return STRCONV(state.str());
372 }
373
374
375 /* Does the actual toggle job of the calls above.
376  * Also shows the current font state.
377  */
378 void toggleAndShow(BufferView * bv, LyXFont const & font, bool toggleall)
379 {
380         if (!bv->available())
381                 return;
382
383         if (bv->theLockingInset()) {
384                 bv->theLockingInset()->setFont(bv, font, toggleall);
385                 return;
386         }
387
388         LyXText * text = bv->getLyXText();
389         // FIXME: can this happen ??
390         if (!text)
391                 return;
392
393         bv->hideCursor();
394         bv->update(text, BufferView::SELECT);
395         text->toggleFree(font, toggleall);
396         bv->update(text, BufferView::SELECT);
397
398         if (font.language() != ignore_language ||
399             font.number() != LyXFont::IGNORE) {
400                 LyXCursor & cursor = text->cursor;
401                 text->computeBidiTables(bv->buffer(), cursor.row());
402                 if (cursor.boundary() !=
403                     text->isBoundary(bv->buffer(), cursor.par(), cursor.pos(),
404                                      text->real_current_font))
405                         text->setCursor(cursor.par(), cursor.pos(),
406                                         false, !cursor.boundary());
407         }
408 }
409
410 }; // namespace bv_funcs