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