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