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