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