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