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