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