]> git.lyx.org Git - lyx.git/blob - src/bufferview_funcs.C
introduce namespace lyx::support
[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/tostr.h"
35 #include "Lsstream.h"
36
37 #include "insets/updatableinset.h"
38
39 using namespace lyx::support;
40
41
42 namespace {
43
44 LyXFont freefont(LyXFont::ALL_IGNORE);
45 bool toggleall(false);
46
47 }
48
49 namespace bv_funcs {
50
51 // Set data using font and toggle
52 // If successful, returns true
53 bool font2string(LyXFont const & font, bool toggle, string & data)
54 {
55         string lang = "ignore";
56         if (font.language())
57                 lang = font.language()->lang();
58
59         ostringstream os;
60         os << "family " << font.family() << '\n'
61            << "series " << font.series() << '\n'
62            << "shape " << font.shape() << '\n'
63            << "size " << font.size() << '\n'
64            << "emph " << font.emph() << '\n'
65            << "underbar " << font.underbar() << '\n'
66            << "noun " << font.noun() << '\n'
67            << "number " << font.number() << '\n'
68            << "color " << font.color() << '\n'
69            << "language " << lang << '\n'
70            << "toggleall " << tostr(toggle);
71         data = STRCONV(os.str());
72         return true;
73 }
74
75
76 // Set font and toggle using data
77 // If successful, returns true
78 bool string2font(string const & data, LyXFont & font, bool & toggle)
79 {
80         istringstream is(STRCONV(data));
81         LyXLex lex(0,0);
82         lex.setStream(is);
83
84         int nset = 0;
85         while (lex.isOK()) {
86                 string token;
87                 if (lex.next())
88                         token = lex.getString();
89
90                 if (token.empty() || !lex.next())
91                         break;
92
93                 if (token == "family") {
94                         int const next = lex.getInteger();
95                         font.setFamily(LyXFont::FONT_FAMILY(next));
96
97                 } else if (token == "series") {
98                         int const next = lex.getInteger();
99                         font.setSeries(LyXFont::FONT_SERIES(next));
100
101                 } else if (token == "shape") {
102                         int const next = lex.getInteger();
103                         font.setShape(LyXFont::FONT_SHAPE(next));
104
105                 } else if (token == "size") {
106                         int const next = lex.getInteger();
107                         font.setSize(LyXFont::FONT_SIZE(next));
108
109                 } else if (token == "emph" || token == "underbar" ||
110                            token == "noun" || token == "number") {
111
112                         int const next = lex.getInteger();
113                         LyXFont::FONT_MISC_STATE const misc =
114                                 LyXFont::FONT_MISC_STATE(next);
115
116                         if (token == "emph")
117                             font.setEmph(misc);
118                         else if (token == "underbar")
119                                 font.setUnderbar(misc);
120                         else if (token == "noun")
121                                 font.setNoun(misc);
122                         else if (token == "number")
123                                 font.setNumber(misc);
124
125                 } else if (token == "color") {
126                         int const next = lex.getInteger();
127                         font.setColor(LColor::color(next));
128
129                 } else if (token == "language") {
130                         string const next = lex.getString();
131                         if (next == "ignore")
132                                 font.setLanguage(ignore_language);
133                         else
134                                 font.setLanguage(languages.getLanguage(next));
135
136                 } else if (token == "toggleall") {
137                         toggle = lex.getBool();
138
139                 } else {
140                         // Unrecognised token
141                         break;
142                 }
143
144                 ++nset;
145         }
146         return (nset > 0);
147 }
148
149
150 string const freefont2string()
151 {
152         string data;
153         if (font2string(freefont, toggleall, data))
154                 return data;
155         return string();
156 }
157
158
159 void update_and_apply_freefont(BufferView * bv, string const & data)
160 {
161         LyXFont font;
162         bool toggle;
163         if (string2font(data, font, toggle)) {
164                 freefont = font;
165                 toggleall = toggle;
166                 apply_freefont(bv);
167         }
168 }
169
170
171 void apply_freefont(BufferView * bv)
172 {
173         toggleAndShow(bv, freefont, toggleall);
174         bv->owner()->view_state_changed();
175         bv->owner()->message(_("Character set"));
176 }
177
178
179 void emph(BufferView * bv)
180 {
181         LyXFont font(LyXFont::ALL_IGNORE);
182         font.setEmph(LyXFont::TOGGLE);
183         toggleAndShow(bv, font);
184 }
185
186
187 void bold(BufferView * bv)
188 {
189         LyXFont font(LyXFont::ALL_IGNORE);
190         font.setSeries(LyXFont::BOLD_SERIES);
191         toggleAndShow(bv, font);
192 }
193
194
195 void noun(BufferView * bv)
196 {
197         LyXFont font(LyXFont::ALL_IGNORE);
198         font.setNoun(LyXFont::TOGGLE);
199         toggleAndShow(bv, font);
200 }
201
202
203 void number(BufferView * bv)
204 {
205         LyXFont font(LyXFont::ALL_IGNORE);
206         font.setNumber(LyXFont::TOGGLE);
207         toggleAndShow(bv, font);
208 }
209
210 void lang(BufferView * bv, string const & l)
211 {
212         LyXFont font(LyXFont::ALL_IGNORE);
213         Language const * lang = languages.getLanguage(l);
214         if (!lang)
215                 return;
216
217         font.setLanguage(lang);
218         toggleAndShow(bv, font);
219 }
220
221
222 bool changeDepth(BufferView * bv, LyXText * text, DEPTH_CHANGE type, bool test_only)
223 {
224         if (!bv->available() || !text)
225             return false;
226
227         if (test_only)
228                 return text->changeDepth(type, true);
229
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         // avoid _(...) re-entrance problem
325         string const s = font.stateText(&buffer->params);
326         state << bformat(_("Font: %1$s"), s);
327
328         // state << bformat(_("Font: %1$s"), font.stateText(&buffer->params));
329
330         // The paragraph depth
331         int depth = text->getDepth();
332         if (depth > 0)
333                 state << bformat(_(", Depth: %1$s"), tostr(depth));
334
335         // The paragraph spacing, but only if different from
336         // buffer spacing.
337         if (!text->cursor.par()->params().spacing().isDefault()) {
338                 Spacing::Space cur_space =
339                         text->cursor.par()->params().spacing().getSpace();
340                 state << _(", Spacing: ");
341
342                 switch (cur_space) {
343                 case Spacing::Single:
344                         state << _("Single");
345                         break;
346                 case Spacing::Onehalf:
347                         state << _("OneHalf");
348                         break;
349                 case Spacing::Double:
350                         state << _("Double");
351                         break;
352                 case Spacing::Other:
353                         state << _("Other (")
354                               << text->cursor.par()->params().spacing().getValue()
355                               << ')';
356                         break;
357                 case Spacing::Default:
358                         // should never happen, do nothing
359                         break;
360                 }
361         }
362 #ifdef DEVEL_VERSION
363         state << _(", Paragraph: ") << text->cursor.par()->id();
364         state << "  Inset: " <<
365                 (text->cursor.par()->inInset() ? text->cursor.par()->inInset()->id() : -1);
366 #endif
367         return STRCONV(state.str());
368 }
369
370
371 /* Does the actual toggle job of the calls above.
372  * Also shows the current font state.
373  */
374 void toggleAndShow(BufferView * bv, LyXFont const & font, bool toggleall)
375 {
376         if (!bv->available())
377                 return;
378
379         if (bv->theLockingInset()) {
380                 bv->theLockingInset()->setFont(bv, font, toggleall);
381                 return;
382         }
383
384         LyXText * text = bv->getLyXText();
385         // FIXME: can this happen ??
386         if (!text)
387                 return;
388
389         bv->update(text, BufferView::SELECT);
390         text->toggleFree(font, toggleall);
391         bv->update(text, BufferView::SELECT);
392
393         if (font.language() != ignore_language ||
394             font.number() != LyXFont::IGNORE) {
395                 LyXCursor & cursor = text->cursor;
396                 text->computeBidiTables(bv->buffer(), text->cursorRow());
397                 if (cursor.boundary() !=
398                     text->isBoundary(bv->buffer(), *cursor.par(), cursor.pos(),
399                                      text->real_current_font))
400                         text->setCursor(cursor.par(), cursor.pos(),
401                                         false, !cursor.boundary());
402         }
403 }
404
405 }; // namespace bv_funcs