]> git.lyx.org Git - lyx.git/blob - src/bufferview_funcs.C
rename Inset to InsetOld
[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         bv->update(BufferView::SELECT);
243         bool const changed = text->changeDepth(type, false);
244         if (text->inset_owner)
245                 bv->updateInset((InsetOld *)text->inset_owner);
246         bv->update(BufferView::SELECT);
247         return changed;
248 }
249
250
251 void code(BufferView * bv)
252 {
253         LyXFont font(LyXFont::ALL_IGNORE);
254         font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
255         toggleAndShow(bv, font);
256 }
257
258
259 void sans(BufferView * bv)
260 {
261         LyXFont font(LyXFont::ALL_IGNORE);
262         font.setFamily(LyXFont::SANS_FAMILY);
263         toggleAndShow(bv, font);
264 }
265
266
267 void roman(BufferView * bv)
268 {
269         LyXFont font(LyXFont::ALL_IGNORE);
270         font.setFamily(LyXFont::ROMAN_FAMILY);
271         toggleAndShow(bv, font);
272 }
273
274
275 void styleReset(BufferView * bv)
276 {
277         LyXFont font(LyXFont::ALL_INHERIT, ignore_language);
278         toggleAndShow(bv, font);
279 }
280
281
282 void underline(BufferView * bv)
283 {
284         LyXFont font(LyXFont::ALL_IGNORE);
285         font.setUnderbar(LyXFont::TOGGLE);
286         toggleAndShow(bv, font);
287 }
288
289
290 void fontSize(BufferView * bv, string const & size)
291 {
292         LyXFont font(LyXFont::ALL_IGNORE);
293         font.setLyXSize(size);
294         toggleAndShow(bv, font);
295 }
296
297
298 // Returns the current font and depth as a message.
299 string const currentState(BufferView * bv)
300 {
301         if (!bv->available())
302                 return string();
303
304         if (mathcursor)
305                 return mathcursor->info();
306
307         ostringstream state;
308
309         LyXText * text = bv->getLyXText();
310         Buffer * buffer = bv->buffer();
311         LyXCursor const & c(text->cursor);
312
313         bool const show_change = buffer->params.tracking_changes
314                 && c.pos() != c.par()->size()
315                 && c.par()->lookupChange(c.pos()) != Change::UNCHANGED;
316
317         if (show_change) {
318                 Change change(c.par()->lookupChangeFull(c.pos()));
319                 Author const & a(bv->buffer()->authors().get(change.author));
320                 state << _("Change: ") << a.name();
321                 if (!a.email().empty()) {
322                         state << " (" << a.email() << ")";
323                 }
324                 if (change.changetime)
325                         state << _(" at ") << ctime(&change.changetime);
326                 state << " : ";
327         }
328
329         // I think we should only show changes from the default
330         // font. (Asger)
331         LyXFont font = text->real_current_font;
332         LyXFont const & defaultfont =
333                 buffer->params.getLyXTextClass().defaultfont();
334         font.reduce(defaultfont);
335
336         // avoid _(...) re-entrance problem
337         string const s = font.stateText(&buffer->params);
338         state << bformat(_("Font: %1$s"), s);
339
340         // state << bformat(_("Font: %1$s"), font.stateText(&buffer->params));
341
342         // The paragraph depth
343         int depth = text->getDepth();
344         if (depth > 0)
345                 state << bformat(_(", Depth: %1$s"), tostr(depth));
346
347         // The paragraph spacing, but only if different from
348         // buffer spacing.
349         if (!text->cursor.par()->params().spacing().isDefault()) {
350                 Spacing::Space cur_space =
351                         text->cursor.par()->params().spacing().getSpace();
352                 state << _(", Spacing: ");
353
354                 switch (cur_space) {
355                 case Spacing::Single:
356                         state << _("Single");
357                         break;
358                 case Spacing::Onehalf:
359                         state << _("OneHalf");
360                         break;
361                 case Spacing::Double:
362                         state << _("Double");
363                         break;
364                 case Spacing::Other:
365                         state << _("Other (")
366                               << text->cursor.par()->params().spacing().getValue()
367                               << ')';
368                         break;
369                 case Spacing::Default:
370                         // should never happen, do nothing
371                         break;
372                 }
373         }
374 #ifdef DEVEL_VERSION
375         state << _(", Paragraph: ") << text->cursor.par()->id();
376         state << "  Inset: " <<
377                 (text->cursor.par()->inInset() ? text->cursor.par()->inInset()->id() : -1);
378 #endif
379         return STRCONV(state.str());
380 }
381
382
383 /* Does the actual toggle job of the calls above.
384  * Also shows the current font state.
385  */
386 void toggleAndShow(BufferView * bv, LyXFont const & font, bool toggleall)
387 {
388         if (!bv->available())
389                 return;
390
391         if (bv->theLockingInset()) {
392                 bv->theLockingInset()->setFont(bv, font, toggleall);
393                 return;
394         }
395
396         LyXText * text = bv->getLyXText();
397         // FIXME: can this happen ??
398         if (!text)
399                 return;
400
401         bv->update(text, BufferView::SELECT);
402         text->toggleFree(font, toggleall);
403         bv->update(text, BufferView::SELECT);
404
405         if (font.language() != ignore_language ||
406             font.number() != LyXFont::IGNORE) {
407                 LyXCursor & cursor = text->cursor;
408                 text->computeBidiTables(bv->buffer(), text->cursorRow());
409                 if (cursor.boundary() !=
410                     text->isBoundary(bv->buffer(), *cursor.par(), cursor.pos(),
411                                      text->real_current_font))
412                         text->setCursor(cursor.par(), cursor.pos(),
413                                         false, !cursor.boundary());
414         }
415 }
416
417
418 // deletes a selection during an insertion
419 void replaceSelection(LyXText * lt)
420 {
421         if (lt->selection.set()) {
422                 lt->update();
423                 lt->cutSelection(true, false);
424                 lt->update();
425         }
426 }
427
428 }; // namespace bv_funcs