]> git.lyx.org Git - lyx.git/blob - src/bufferview_funcs.C
Yesterday's character dialog patch
[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->buffer()->markDirty();
181         bv->owner()->message(_("Character set"));
182 }
183
184
185 void emph(BufferView * bv)
186 {
187         LyXFont font(LyXFont::ALL_IGNORE);
188         font.setEmph(LyXFont::TOGGLE);
189         toggleAndShow(bv, font);
190 }
191
192
193 void bold(BufferView * bv)
194 {
195         LyXFont font(LyXFont::ALL_IGNORE);
196         font.setSeries(LyXFont::BOLD_SERIES);
197         toggleAndShow(bv, font);
198 }
199
200
201 void noun(BufferView * bv)
202 {
203         LyXFont font(LyXFont::ALL_IGNORE);
204         font.setNoun(LyXFont::TOGGLE);
205         toggleAndShow(bv, font);
206 }
207
208
209 void number(BufferView * bv)
210 {
211         LyXFont font(LyXFont::ALL_IGNORE);
212         font.setNumber(LyXFont::TOGGLE);
213         toggleAndShow(bv, font);
214 }
215
216 void lang(BufferView * bv, string const & l)
217 {
218         LyXFont font(LyXFont::ALL_IGNORE);
219         Language const * lang = languages.getLanguage(l);
220         if (lang) {
221                 font.setLanguage(lang);
222                 toggleAndShow(bv, font);
223         } else
224                 Alert::alert(_("Error! unknown language"),l);
225 }
226
227
228 // Change environment depth.
229 // if decInc >= 0, increment depth
230 // if decInc <  0, decrement depth
231 void changeDepth(BufferView * bv, LyXText * text, int decInc)
232 {
233         if (!bv->available() || !text)
234             return;
235
236         bv->hideCursor();
237         bv->update(bv->text, BufferView::SELECT|BufferView::FITCUR);
238         if (decInc >= 0)
239                 text->incDepth(bv);
240         else
241                 text->decDepth(bv);
242         if (text->inset_owner)
243                 bv->updateInset((Inset *)text->inset_owner, true);
244         bv->update(bv->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
245 }
246
247
248 void code(BufferView * bv)
249 {
250         LyXFont font(LyXFont::ALL_IGNORE);
251         font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
252         toggleAndShow(bv, font);
253 }
254
255
256 void sans(BufferView * bv)
257 {
258         LyXFont font(LyXFont::ALL_IGNORE);
259         font.setFamily(LyXFont::SANS_FAMILY);
260         toggleAndShow(bv, font);
261 }
262
263
264 void roman(BufferView * bv)
265 {
266         LyXFont font(LyXFont::ALL_IGNORE);
267         font.setFamily(LyXFont::ROMAN_FAMILY);
268         toggleAndShow(bv, font);
269 }
270
271
272 void styleReset(BufferView * bv)
273 {
274         LyXFont font(LyXFont::ALL_INHERIT, ignore_language);
275         toggleAndShow(bv, font);
276 }
277
278
279 void underline(BufferView * bv)
280 {
281         LyXFont font(LyXFont::ALL_IGNORE);
282         font.setUnderbar(LyXFont::TOGGLE);
283         toggleAndShow(bv, font);
284 }
285
286
287 void fontSize(BufferView * bv, string const & size)
288 {
289         LyXFont font(LyXFont::ALL_IGNORE);
290         font.setLyXSize(size);
291         toggleAndShow(bv, font);
292 }
293
294
295 // Returns the current font and depth as a message.
296 string const currentState(BufferView * bv)
297 {
298         if (!bv->available())
299                 return string();
300
301         ostringstream state;
302
303         LyXText * text = bv->getLyXText();
304         Buffer * buffer = bv->buffer();
305         LyXCursor const & c(text->cursor);
306
307         bool const show_change = buffer->params.tracking_changes
308                 && c.pos() != c.par()->size()
309                 && c.par()->lookupChange(c.pos()) != Change::UNCHANGED;
310
311         if (show_change) {
312                 Change change(c.par()->lookupChangeFull(c.pos()));
313                 Author const & a(bv->buffer()->authors().get(change.author));
314                 state << _("Change: ") << a.name();
315                 if (!a.email().empty()) {
316                         state << " (" << a.email() << ")";
317                 }
318                 if (change.changetime)
319                         state << _(" at ") << ctime(&change.changetime);
320                 state << " : ";
321         }
322
323         // I think we should only show changes from the default
324         // font. (Asger)
325         LyXFont font = text->real_current_font;
326         LyXFont const & defaultfont =
327                 buffer->params.getLyXTextClass().defaultfont();
328         font.reduce(defaultfont);
329
330 #if USE_BOOST_FORMAT
331         state << boost::format(_("Font: %1$s")) % font.stateText(&buffer->params);
332 #else
333         state << _("Font: ") << font.stateText(&buffer->params);
334 #endif
335
336         // The paragraph depth
337         int depth = text->getDepth();
338         if (depth > 0) {
339 #if USE_BOOST_FORMAT
340                 state << boost::format(_(", Depth: %1$d")) % depth;
341 #else
342                 state << _(", Depth: ") << depth;
343 #endif
344         }
345
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 #endif
377         return STRCONV(state.str());
378 }
379
380
381 /* Does the actual toggle job of the calls above.
382  * Also shows the current font state.
383  */
384 void toggleAndShow(BufferView * bv, LyXFont const & font, bool toggleall)
385 {
386         if (!bv->available())
387                 return;
388
389         if (bv->theLockingInset()) {
390                 bv->theLockingInset()->setFont(bv, font, toggleall);
391                 return;
392         }
393
394         LyXText * text = bv->getLyXText();
395         // FIXME: can this happen ??
396         if (!text)
397                 return;
398
399         bv->hideCursor();
400         bv->update(text, BufferView::SELECT | BufferView::FITCUR);
401         text->toggleFree(bv, font, toggleall);
402         bv->update(text, BufferView::SELECT | BufferView::FITCUR | BufferView::CHANGE);
403
404         if (font.language() != ignore_language ||
405             font.number() != LyXFont::IGNORE) {
406                 LyXCursor & cursor = text->cursor;
407                 text->computeBidiTables(bv->buffer(), cursor.row());
408                 if (cursor.boundary() !=
409                     text->isBoundary(bv->buffer(), cursor.par(), cursor.pos(),
410                                      text->real_current_font))
411                         text->setCursor(bv, cursor.par(), cursor.pos(),
412                                         false, !cursor.boundary());
413         }
414 }