]> git.lyx.org Git - lyx.git/blob - src/bufferview_funcs.C
reverse last change
[lyx.git] / src / bufferview_funcs.C
1 /* This file is part of
2  * ======================================================
3  *
4  *           LyX, The Document Processor
5  *
6  *           Copyright 1995 Matthias Ettrich
7  *           Copyright 1995-2001 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "bufferview_funcs.h"
18 #include "frontends/LyXView.h"
19 #include "BufferView.h"
20 #include "paragraph.h"
21 #include "lyxfont.h"
22 #include "lyxtext.h"
23 #include "buffer.h"
24 #include "lyx_cb.h"
25 #include "language.h"
26 #include "gettext.h"
27 #include "ParagraphParameters.h"
28 #include "author.h"
29 #include "changes.h"
30
31 #include "frontends/Alert.h"
32
33 #include "support/lstrings.h"
34
35 #include "BoostFormat.h"
36
37 void emph(BufferView * bv)
38 {
39         LyXFont font(LyXFont::ALL_IGNORE);
40         font.setEmph(LyXFont::TOGGLE);
41         toggleAndShow(bv, font);
42 }
43
44
45 void bold(BufferView * bv)
46 {
47         LyXFont font(LyXFont::ALL_IGNORE);
48         font.setSeries(LyXFont::BOLD_SERIES);
49         toggleAndShow(bv, font);
50 }
51
52
53 void noun(BufferView * bv)
54 {
55         LyXFont font(LyXFont::ALL_IGNORE);
56         font.setNoun(LyXFont::TOGGLE);
57         toggleAndShow(bv, font);
58 }
59
60
61 void number(BufferView * bv)
62 {
63         LyXFont font(LyXFont::ALL_IGNORE);
64         font.setNumber(LyXFont::TOGGLE);
65         toggleAndShow(bv, font);
66 }
67
68 void lang(BufferView * bv, string const & l)
69 {
70         LyXFont font(LyXFont::ALL_IGNORE);
71         Language const * lang = languages.getLanguage(l);
72         if (lang) {
73                 font.setLanguage(lang);
74                 toggleAndShow(bv, font);
75         } else
76                 Alert::alert(_("Error! unknown language"),l);
77 }
78
79
80 // Change environment depth.
81 // if decInc >= 0, increment depth
82 // if decInc <  0, decrement depth
83 void changeDepth(BufferView * bv, LyXText * text, int decInc)
84 {
85         if (!bv->available() || !text)
86             return;
87
88         bv->hideCursor();
89         bv->update(bv->text, BufferView::SELECT|BufferView::FITCUR);
90         if (decInc >= 0)
91                 text->incDepth(bv);
92         else
93                 text->decDepth(bv);
94         if (text->inset_owner)
95                 bv->updateInset((Inset *)text->inset_owner, true);
96         bv->update(bv->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
97 }
98
99
100 void code(BufferView * bv)
101 {
102         LyXFont font(LyXFont::ALL_IGNORE);
103         font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
104         toggleAndShow(bv, font);
105 }
106
107
108 void sans(BufferView * bv)
109 {
110         LyXFont font(LyXFont::ALL_IGNORE);
111         font.setFamily(LyXFont::SANS_FAMILY);
112         toggleAndShow(bv, font);
113 }
114
115
116 void roman(BufferView * bv)
117 {
118         LyXFont font(LyXFont::ALL_IGNORE);
119         font.setFamily(LyXFont::ROMAN_FAMILY);
120         toggleAndShow(bv, font);
121 }
122
123
124 void styleReset(BufferView * bv)
125 {
126         LyXFont font(LyXFont::ALL_INHERIT, ignore_language);
127         toggleAndShow(bv, font);
128 }
129
130
131 void underline(BufferView * bv)
132 {
133         LyXFont font(LyXFont::ALL_IGNORE);
134         font.setUnderbar(LyXFont::TOGGLE);
135         toggleAndShow(bv, font);
136 }
137
138
139 void fontSize(BufferView * bv, string const & size)
140 {
141         LyXFont font(LyXFont::ALL_IGNORE);
142         font.setLyXSize(size);
143         toggleAndShow(bv, font);
144 }
145
146
147 // Returns the current font and depth as a message.
148 string const currentState(BufferView * bv)
149 {
150         ostringstream state;
151
152         if (!bv->available())
153                 return string();
154
155         LyXText * text = bv->getLyXText();
156         Buffer * buffer = bv->buffer();
157         LyXCursor const & c(text->cursor);
158
159         bool const show_change = buffer->params.tracking_changes
160                 && c.pos() != c.par()->size()
161                 && c.par()->lookupChange(c.pos()) != Change::UNCHANGED;
162
163         if (show_change) {
164                 Change change(c.par()->lookupChangeFull(c.pos()));
165                 Author const & a(bv->buffer()->authors().get(change.author));
166                 state << _("Change: ") << a.name();
167                 if (!a.email().empty()) {
168                         state << " (" << a.email() << ")";
169                 }
170                 if (change.changetime)
171                         state << _(" at ") << ctime(&change.changetime);
172                 state << " : ";
173         }
174  
175         // I think we should only show changes from the default
176         // font. (Asger)
177         LyXFont font = text->real_current_font;
178         LyXFont const & defaultfont =
179                 buffer->params.getLyXTextClass().defaultfont();
180         font.reduce(defaultfont);
181
182 #if USE_BOOST_FORMAT
183         state << boost::format(_("Font: %1$s")) % font.stateText(&buffer->params);
184 #else
185         state << _("Font: ") << font.stateText(&buffer->params);
186 #endif
187
188         // The paragraph depth
189         int depth = text->getDepth();
190         if (depth > 0) {
191 #if USE_BOOST_FORMAT
192                 state << boost::format(_(", Depth: %1$d")) % depth;
193 #else
194                 state << _(", Depth: ") << depth;
195 #endif
196         }
197
198
199         // The paragraph spacing, but only if different from
200         // buffer spacing.
201         if (!text->cursor.par()->params().spacing().isDefault()) {
202                 Spacing::Space cur_space =
203                         text->cursor.par()->params().spacing().getSpace();
204                 state << _(", Spacing: ");
205
206                 switch (cur_space) {
207                 case Spacing::Single:
208                         state << _("Single");
209                         break;
210                 case Spacing::Onehalf:
211                         state << _("OneHalf");
212                         break;
213                 case Spacing::Double:
214                         state << _("Double");
215                         break;
216                 case Spacing::Other:
217                         state << _("Other (")
218                               << text->cursor.par()->params().spacing().getValue()
219                               << ')';
220                         break;
221                 case Spacing::Default:
222                         // should never happen, do nothing
223                         break;
224                 }
225         }
226 #ifdef DEVEL_VERSION
227         state << _(", Paragraph: ") << text->cursor.par()->id();
228 #endif
229         return STRCONV(state.str());
230 }
231
232
233 /* Does the actual toggle job of the calls above.
234  * Also shows the current font state.
235  */
236 void toggleAndShow(BufferView * bv, LyXFont const & font, bool toggleall)
237 {
238         if (!bv->available())
239                 return;
240
241         if (bv->theLockingInset()) {
242                 bv->theLockingInset()->setFont(bv, font, toggleall);
243                 return;
244         }
245
246         LyXText * text = bv->getLyXText();
247         // FIXME: can this happen ??
248         if (!text)
249                 return;
250
251         bv->hideCursor();
252         bv->update(text, BufferView::SELECT | BufferView::FITCUR);
253         text->toggleFree(bv, font, toggleall);
254         bv->update(text, BufferView::SELECT | BufferView::FITCUR | BufferView::CHANGE);
255
256         if (font.language() != ignore_language ||
257             font.number() != LyXFont::IGNORE) {
258                 LyXCursor & cursor = text->cursor;
259                 text->computeBidiTables(bv->buffer(), cursor.row());
260                 if (cursor.boundary() !=
261                     text->isBoundary(bv->buffer(), cursor.par(), cursor.pos(),
262                                      text->real_current_font))
263                         text->setCursor(bv, cursor.par(), cursor.pos(),
264                                         false, !cursor.boundary());
265         }
266 }