]> git.lyx.org Git - lyx.git/blob - src/bufferview_funcs.C
more Alert fixes
[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 "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 Int = 0;
80         bool Bool = false;
81         string String;
82                         
83         int nset = 0;
84         while (lex.isOK()) {
85                 lex.next();
86                 string const token = lex.getString();
87
88                 if (token == "family" ||
89                     token == "series" ||
90                     token == "shape" ||
91                     token == "size" ||
92                     token == "emph" ||
93                     token == "underbar" ||
94                     token == "noun" ||
95                     token == "number" ||
96                     token == "color") {
97                         lex.next();
98                         Int = lex.getInteger();
99                 } else if (token == "language") {
100                         lex.next();
101                         String = lex.getString();
102                 } else if (token == "toggleall") {
103                         lex.next();
104                         Bool = lex.getBool();
105                 } else {
106                         // Unrecognised token
107                         break;
108                 }
109
110                 if (!lex.isOK())
111                         break;
112                 ++nset;
113
114                 if (token == "family") {
115                         font.setFamily(static_cast<LyXFont::FONT_FAMILY>(Int));
116                         
117                 } else if (token == "series") {
118                         font.setSeries(static_cast<LyXFont::FONT_SERIES>(Int));
119
120                 } else if (token == "shape") {
121                         font.setShape(static_cast<LyXFont::FONT_SHAPE>(Int));
122
123                 } else if (token == "size") {
124                         font.setSize(static_cast<LyXFont::FONT_SIZE>(Int));
125
126                 } else if (token == "emph") {
127                         font.setEmph(static_cast<LyXFont::FONT_MISC_STATE>(Int));
128
129                 } else if (token == "underbar") {
130                         font.setUnderbar(static_cast<LyXFont::FONT_MISC_STATE>(Int));
131
132                 } else if (token == "noun") {
133                         font.setNoun(static_cast<LyXFont::FONT_MISC_STATE>(Int));
134
135                 } else if (token == "number") {
136                         font.setNumber(static_cast<LyXFont::FONT_MISC_STATE>(Int));
137
138                 } else if (token == "color") {
139                         font.setColor(static_cast<LColor::color>(Int));
140
141                 } else if (token == "language") {
142                         if (String == "ignore")
143                                 font.setLanguage(ignore_language);
144                         else
145                                 font.setLanguage(languages.getLanguage(String));
146
147                 } else if (token == "toggleall") {
148                         toggle = Bool;
149                 }
150         }
151         return (nset > 0);
152 }
153
154
155 string const freefont2string()
156 {
157         string data;
158         if (font2string(freefont, toggleall, data))
159                 return data;
160         return string();
161 }
162
163
164 void update_and_apply_freefont(BufferView * bv, string const & data)
165 {
166         LyXFont font;
167         bool toggle;
168         if (string2font(data, font, toggle)) {
169                 freefont = font;
170                 toggleall = toggle;
171                 apply_freefont(bv);
172         }
173 }
174
175
176 void apply_freefont(BufferView * bv)
177 {
178         toggleAndShow(bv, freefont, toggleall);
179         bv->owner()->view_state_changed();
180         bv->owner()->message(_("Character set"));
181 }
182
183
184 void emph(BufferView * bv)
185 {
186         LyXFont font(LyXFont::ALL_IGNORE);
187         font.setEmph(LyXFont::TOGGLE);
188         toggleAndShow(bv, font);
189 }
190
191
192 void bold(BufferView * bv)
193 {
194         LyXFont font(LyXFont::ALL_IGNORE);
195         font.setSeries(LyXFont::BOLD_SERIES);
196         toggleAndShow(bv, font);
197 }
198
199
200 void noun(BufferView * bv)
201 {
202         LyXFont font(LyXFont::ALL_IGNORE);
203         font.setNoun(LyXFont::TOGGLE);
204         toggleAndShow(bv, font);
205 }
206
207
208 void number(BufferView * bv)
209 {
210         LyXFont font(LyXFont::ALL_IGNORE);
211         font.setNumber(LyXFont::TOGGLE);
212         toggleAndShow(bv, font);
213 }
214
215 void lang(BufferView * bv, string const & l)
216 {
217         LyXFont font(LyXFont::ALL_IGNORE);
218         Language const * lang = languages.getLanguage(l);
219         if (!lang)
220                 return;
221
222         font.setLanguage(lang);
223         toggleAndShow(bv, font);
224 }
225
226
227 // Change environment depth.
228 // if decInc >= 0, increment depth
229 // if decInc <  0, decrement depth
230 void changeDepth(BufferView * bv, LyXText * text, int decInc)
231 {
232         if (!bv->available() || !text)
233             return;
234
235         bv->hideCursor();
236         bv->update(BufferView::SELECT);
237         if (decInc >= 0)
238                 text->incDepth();
239         else
240                 text->decDepth();
241         if (text->inset_owner)
242                 bv->updateInset((Inset *)text->inset_owner);
243         bv->update(BufferView::SELECT);
244 }
245
246
247 void code(BufferView * bv)
248 {
249         LyXFont font(LyXFont::ALL_IGNORE);
250         font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
251         toggleAndShow(bv, font);
252 }
253
254
255 void sans(BufferView * bv)
256 {
257         LyXFont font(LyXFont::ALL_IGNORE);
258         font.setFamily(LyXFont::SANS_FAMILY);
259         toggleAndShow(bv, font);
260 }
261
262
263 void roman(BufferView * bv)
264 {
265         LyXFont font(LyXFont::ALL_IGNORE);
266         font.setFamily(LyXFont::ROMAN_FAMILY);
267         toggleAndShow(bv, font);
268 }
269
270
271 void styleReset(BufferView * bv)
272 {
273         LyXFont font(LyXFont::ALL_INHERIT, ignore_language);
274         toggleAndShow(bv, font);
275 }
276
277
278 void underline(BufferView * bv)
279 {
280         LyXFont font(LyXFont::ALL_IGNORE);
281         font.setUnderbar(LyXFont::TOGGLE);
282         toggleAndShow(bv, font);
283 }
284
285
286 void fontSize(BufferView * bv, string const & size)
287 {
288         LyXFont font(LyXFont::ALL_IGNORE);
289         font.setLyXSize(size);
290         toggleAndShow(bv, font);
291 }
292
293
294 // Returns the current font and depth as a message.
295 string const currentState(BufferView * bv)
296 {
297         if (!bv->available())
298                 return string();
299
300         ostringstream state;
301
302         LyXText * text = bv->getLyXText();
303         Buffer * buffer = bv->buffer();
304         LyXCursor const & c(text->cursor);
305
306         bool const show_change = buffer->params.tracking_changes
307                 && c.pos() != c.par()->size()
308                 && c.par()->lookupChange(c.pos()) != Change::UNCHANGED;
309
310         if (show_change) {
311                 Change change(c.par()->lookupChangeFull(c.pos()));
312                 Author const & a(bv->buffer()->authors().get(change.author));
313                 state << _("Change: ") << a.name();
314                 if (!a.email().empty()) {
315                         state << " (" << a.email() << ")";
316                 }
317                 if (change.changetime)
318                         state << _(" at ") << ctime(&change.changetime);
319                 state << " : ";
320         }
321
322         // I think we should only show changes from the default
323         // font. (Asger)
324         LyXFont font = text->real_current_font;
325         LyXFont const & defaultfont =
326                 buffer->params.getLyXTextClass().defaultfont();
327         font.reduce(defaultfont);
328
329 #if USE_BOOST_FORMAT
330         state << boost::format(_("Font: %1$s")) % font.stateText(&buffer->params);
331 #else
332         state << _("Font: ") << font.stateText(&buffer->params);
333 #endif
334
335         // The paragraph depth
336         int depth = text->getDepth();
337         if (depth > 0) {
338 #if USE_BOOST_FORMAT
339                 state << boost::format(_(", Depth: %1$d")) % depth;
340 #else
341                 state << _(", Depth: ") << depth;
342 #endif
343         }
344
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 #endif
376         return STRCONV(state.str());
377 }
378
379
380 /* Does the actual toggle job of the calls above.
381  * Also shows the current font state.
382  */
383 void toggleAndShow(BufferView * bv, LyXFont const & font, bool toggleall)
384 {
385         if (!bv->available())
386                 return;
387
388         if (bv->theLockingInset()) {
389                 bv->theLockingInset()->setFont(bv, font, toggleall);
390                 return;
391         }
392
393         LyXText * text = bv->getLyXText();
394         // FIXME: can this happen ??
395         if (!text)
396                 return;
397
398         bv->hideCursor();
399         bv->update(text, BufferView::SELECT);
400         text->toggleFree(font, toggleall);
401         bv->update(text, BufferView::SELECT);
402
403         if (font.language() != ignore_language ||
404             font.number() != LyXFont::IGNORE) {
405                 LyXCursor & cursor = text->cursor;
406                 text->computeBidiTables(bv->buffer(), cursor.row());
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 }