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