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