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