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