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