]> git.lyx.org Git - lyx.git/blob - src/bufferview_funcs.C
handle USE_BOOST_FORMAT
[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
29 #include "frontends/Alert.h"
30
31 #include "support/lstrings.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         ostringstream state;
149
150         if (!bv->available())
151                 return "";
152
153         // I think we should only show changes from the default
154         // font. (Asger)
155         LyXText * text = bv->getLyXText();
156         Buffer * buffer = bv->buffer();
157         LyXFont font = text->real_current_font;
158         LyXFont const & defaultfont =
159                 buffer->params.getLyXTextClass().defaultfont();
160         font.reduce(defaultfont);
161
162 #if USE_BOOST_FORMAT
163         state << boost::format(_("Font: %1$s")) % font.stateText(&buffer->params);
164 #else
165         state << _("Font: ") << font.stateText(&buffer->params);
166 #endif
167
168         // The paragraph depth
169         int depth = text->getDepth();
170         if (depth > 0) {
171 #if USE_BOOST_FORMAT
172                 state << boost::format(_(", Depth: %1$d")) % depth;
173 #else
174                 state << _(", Depth: ") << depth;
175 #endif
176         }
177
178
179         // The paragraph spacing, but only if different from
180         // buffer spacing.
181         if (!text->cursor.par()->params().spacing().isDefault()) {
182                 Spacing::Space cur_space =
183                         text->cursor.par()->params().spacing().getSpace();
184                 state << _(", Spacing: ");
185
186                 switch (cur_space) {
187                 case Spacing::Single:
188                         state << _("Single");
189                         break;
190                 case Spacing::Onehalf:
191                         state << _("Onehalf");
192                         break;
193                 case Spacing::Double:
194                         state << _("Double");
195                         break;
196                 case Spacing::Other:
197                         state << _("Other (")
198                               << text->cursor.par()->params().spacing().getValue()
199                               << ")";
200                         break;
201                 case Spacing::Default:
202                         // should never happen, do nothing
203                         break;
204                 }
205         }
206 #ifdef DEVEL_VERSION
207         state << _(", Paragraph: ") << text->cursor.par()->id();
208 #endif
209         return STRCONV(state.str());
210 }
211
212
213 /* Does the actual toggle job of the calls above.
214  * Also shows the current font state.
215  */
216 void toggleAndShow(BufferView * bv, LyXFont const & font, bool toggleall)
217 {
218         if (!bv->available())
219                 return;
220
221         if (bv->theLockingInset()) {
222                 bv->theLockingInset()->setFont(bv, font, toggleall);
223                 return;
224         }
225
226         LyXText * text = bv->getLyXText();
227         // FIXME: can this happen ??
228         if (!text)
229                 return;
230
231         bv->hideCursor();
232         bv->update(text, BufferView::SELECT | BufferView::FITCUR);
233         text->toggleFree(bv, font, toggleall);
234         bv->update(text, BufferView::SELECT | BufferView::FITCUR | BufferView::CHANGE);
235
236         if (font.language() != ignore_language ||
237             font.number() != LyXFont::IGNORE) {
238                 LyXCursor & cursor = text->cursor;
239                 text->computeBidiTables(bv->buffer(), cursor.row());
240                 if (cursor.boundary() !=
241                     text->isBoundary(bv->buffer(), cursor.par(), cursor.pos(),
242                                      text->real_current_font))
243                         text->setCursor(bv, cursor.par(), cursor.pos(),
244                                         false, !cursor.boundary());
245         }
246 }