]> git.lyx.org Git - lyx.git/blob - src/bufferview_funcs.C
f32ba3254ab17d0588e5f88e3730af6f6a89b8e4
[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 "LyXView.h"
19 #include "BufferView.h"
20 #include "paragraph.h"
21 #include "lyxfont.h"
22 #include "lyx_gui_misc.h"
23 #include "lyxtext.h"
24 #include "buffer.h"
25 #include "support/lstrings.h"
26 #include "lyx_cb.h"
27 #include "language.h"
28 #include "gettext.h"
29 #include "ParagraphParameters.h"
30
31
32 void emph(BufferView * bv)
33 {
34         LyXFont font(LyXFont::ALL_IGNORE);
35         font.setEmph(LyXFont::TOGGLE);
36         toggleAndShow(bv, font);
37 }
38
39
40 void bold(BufferView * bv)
41 {
42         LyXFont font(LyXFont::ALL_IGNORE);
43         font.setSeries(LyXFont::BOLD_SERIES);
44         toggleAndShow(bv, font);
45 }
46
47
48 void noun(BufferView * bv)
49 {
50         LyXFont font(LyXFont::ALL_IGNORE);
51         font.setNoun(LyXFont::TOGGLE);
52         toggleAndShow(bv, font);
53 }
54
55
56 void number(BufferView * bv)
57 {
58         LyXFont font(LyXFont::ALL_IGNORE);
59         font.setNumber(LyXFont::TOGGLE);
60         toggleAndShow(bv, font);
61 }
62
63 void lang(BufferView * bv, string const & l)
64 {
65         LyXFont font(LyXFont::ALL_IGNORE);
66         Language const * lang = languages.getLanguage(l);
67         if (lang) {
68                 font.setLanguage(lang);
69                 toggleAndShow(bv, font);
70         } else
71                 WriteAlert(_("Error! unknown language"),l);
72 }
73
74
75 #ifndef NO_LATEX
76 void tex(BufferView * bv)
77 {
78         LyXFont font(LyXFont::ALL_IGNORE);
79         font.setLatex (LyXFont::TOGGLE);
80         toggleAndShow(bv, font);
81 }
82 #endif
83
84
85 // Change environment depth.
86 // if decInc >= 0, increment depth
87 // if decInc <  0, decrement depth
88 void changeDepth(BufferView * bv, LyXText * text, int decInc)
89 {
90         if (!bv->available() || !text)
91             return;
92         
93         bv->hideCursor();
94         bv->update(bv->text, BufferView::SELECT|BufferView::FITCUR);
95         if (decInc >= 0)
96                 text->incDepth(bv);
97         else
98                 text->decDepth(bv);
99         if (text->inset_owner)
100             bv->updateInset((Inset *)text->inset_owner, true);
101         bv->update(bv->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
102         bv->owner()->message(_("Changed environment depth "
103                                "(in possible range, maybe not)"));
104 }
105
106
107 void code(BufferView * bv)
108 {
109         LyXFont font(LyXFont::ALL_IGNORE);
110         font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
111         toggleAndShow(bv, font);
112 }
113
114
115 void sans(BufferView * bv)
116 {
117         LyXFont font(LyXFont::ALL_IGNORE);
118         font.setFamily(LyXFont::SANS_FAMILY);
119         toggleAndShow(bv, font);
120 }
121
122
123 void roman(BufferView * bv)
124 {
125         LyXFont font(LyXFont::ALL_IGNORE);
126         font.setFamily(LyXFont::ROMAN_FAMILY);
127         toggleAndShow(bv, font);
128 }
129
130
131 void styleReset(BufferView * bv)
132 {
133         LyXFont font(LyXFont::ALL_INHERIT);
134         toggleAndShow(bv, font);
135 }
136
137
138 void underline(BufferView * bv)
139 {
140         LyXFont font(LyXFont::ALL_IGNORE);
141         font.setUnderbar(LyXFont::TOGGLE);
142         toggleAndShow(bv, font);
143 }
144
145
146 void fontSize(BufferView * bv, string const & size)
147 {
148         LyXFont font(LyXFont::ALL_IGNORE);
149         font.setLyXSize(size);
150         toggleAndShow(bv, font);
151 }
152
153
154 // Returns the current font and depth as a message. 
155 string const currentState(BufferView * bv)
156 {
157         ostringstream state;
158
159         if (bv->available()) { 
160                 // I think we should only show changes from the default
161                 // font. (Asger)
162                 LyXText * text = bv->getLyXText();
163                 Buffer * buffer = bv->buffer();
164                 LyXFont font = text->real_current_font;
165                 LyXFont const & defaultfont =
166                         textclasslist
167                         .TextClass(buffer->params.textclass)
168                         .defaultfont();
169                 font.reduce(defaultfont);
170
171                 state << _("Font:") << ' '
172                       << font.stateText(&buffer->params);
173                 
174                 // The paragraph depth
175                 int depth = text->getDepth();
176                 if (depth > 0)
177                         state << _(", Depth: ") << depth;
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                                 
190                                 break;
191                         case Spacing::Onehalf:
192                                 state << _("Onehalf");
193                                 break;
194                         case Spacing::Double:
195                                 state << _("Double");
196                                 break;
197                         case Spacing::Other:
198                                 state << _("Other (")
199                                       << text->cursor.par()->params().spacing().getValue()
200                                       << ")";
201                                 break;
202                         case Spacing::Default:
203                                 // should never happen, do nothing
204                                 break;
205                         }
206                 }
207         }
208         return state.str().c_str();
209 }
210
211
212 /* -------> Does the actual toggle job of the XxxCB() calls above.
213  * Also shows the current font state.
214  */
215 void toggleAndShow(BufferView * bv, LyXFont const & font, bool toggleall)
216 {
217         if (bv->available()) { 
218                 if (bv->theLockingInset()) {
219                         bv->theLockingInset()->setFont(bv, font, toggleall);
220                         return;
221                 }
222                 LyXText * text = bv->getLyXText();
223                 if (!text)
224                         return;
225
226                 bv->hideCursor();
227                 bv->update(text, BufferView::SELECT|BufferView::FITCUR);
228                 text->toggleFree(bv, font, toggleall);
229                 bv->update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
230
231                 if (font.language() != ignore_language ||
232 #ifndef NO_LATEX
233                     font.latex() != LyXFont::IGNORE ||
234 #endif
235                     font.number() != LyXFont::IGNORE)
236                 {
237                         LyXCursor & cursor = text->cursor;
238                         text->computeBidiTables(bv->buffer(), cursor.row());
239                         if (cursor.boundary() != 
240                             text->isBoundary(bv->buffer(), cursor.par(), cursor.pos(),
241                                              text->real_current_font) )
242                                 text->setCursor(bv, cursor.par(), cursor.pos(),
243                                                 false, !cursor.boundary());
244                 }
245         }
246 }