]> git.lyx.org Git - lyx.git/blob - src/bufferview_funcs.C
This should clean up the language stuff a bit and a small new check for
[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 void Emph(BufferView * bv)
32 {
33         LyXFont font(LyXFont::ALL_IGNORE);
34         font.setEmph(LyXFont::TOGGLE);
35         ToggleAndShow(bv, font);
36 }
37
38
39 void Bold(BufferView * bv)
40 {
41         LyXFont font(LyXFont::ALL_IGNORE);
42         font.setSeries(LyXFont::BOLD_SERIES);
43         ToggleAndShow(bv, font);
44 }
45
46
47 void Noun(BufferView * bv)
48 {
49         LyXFont font(LyXFont::ALL_IGNORE);
50         font.setNoun(LyXFont::TOGGLE);
51         ToggleAndShow(bv, font);
52 }
53
54
55 void Number(BufferView * bv)
56 {
57         LyXFont font(LyXFont::ALL_IGNORE);
58         font.setNumber(LyXFont::TOGGLE);
59         ToggleAndShow(bv, font);
60 }
61
62 void Lang(BufferView * bv, string const & l)
63 {
64         LyXFont font(LyXFont::ALL_IGNORE);
65         Language const * lang = languages.getLanguage(l);
66         if (lang) {
67                 font.setLanguage(lang);
68                 ToggleAndShow(bv, font);
69         } else
70                 WriteAlert(_("Error! unknown language"),l);
71 }
72
73
74 #ifndef NO_LATEX
75 void Tex(BufferView * bv)
76 {
77         LyXFont font(LyXFont::ALL_IGNORE);
78         font.setLatex (LyXFont::TOGGLE);
79         ToggleAndShow(bv, font);
80 }
81 #endif
82
83
84 // Change environment depth.
85 // if decInc >= 0, increment depth
86 // if decInc <  0, decrement depth
87 void changeDepth(BufferView * bv, LyXText * text, int decInc)
88 {
89         if (!bv->available() || !text)
90             return;
91         
92         bv->hideCursor();
93         bv->update(bv->text, BufferView::SELECT|BufferView::FITCUR);
94         if (decInc >= 0)
95                 text->incDepth(bv);
96         else
97                 text->decDepth(bv);
98         if (text->inset_owner)
99             bv->updateInset((Inset *)text->inset_owner, true);
100         bv->update(bv->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
101         bv->owner()->message(_("Changed environment depth "
102                                "(in possible range, maybe not)"));
103 }
104
105
106 void Code(BufferView * bv)
107 {
108         LyXFont font(LyXFont::ALL_IGNORE);
109         font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
110         ToggleAndShow(bv, font);
111 }
112
113
114 void Sans(BufferView * bv)
115 {
116         LyXFont font(LyXFont::ALL_IGNORE);
117         font.setFamily(LyXFont::SANS_FAMILY);
118         ToggleAndShow(bv, font);
119 }
120
121
122 void Roman(BufferView * bv)
123 {
124         LyXFont font(LyXFont::ALL_IGNORE);
125         font.setFamily(LyXFont::ROMAN_FAMILY);
126         ToggleAndShow(bv, font);
127 }
128
129
130 void StyleReset(BufferView * bv)
131 {
132         LyXFont font(LyXFont::ALL_INHERIT);
133         ToggleAndShow(bv, font);
134 }
135
136
137 void Underline(BufferView * bv)
138 {
139         LyXFont font(LyXFont::ALL_IGNORE);
140         font.setUnderbar(LyXFont::TOGGLE);
141         ToggleAndShow(bv, font);
142 }
143
144
145 void FontSize(BufferView * bv, string const & size)
146 {
147         LyXFont font(LyXFont::ALL_IGNORE);
148         font.setLyXSize(size);
149         ToggleAndShow(bv, font);
150 }
151
152
153 // Returns the current font and depth as a message. 
154 string const CurrentState(BufferView * bv)
155 {
156         ostringstream state;
157
158         if (bv->available()) { 
159                 // I think we should only show changes from the default
160                 // font. (Asger)
161                 LyXText * text = bv->getLyXText();
162                 Buffer * buffer = bv->buffer();
163                 LyXFont font = text->real_current_font;
164                 LyXFont const & defaultfont =
165                         textclasslist
166                         .TextClass(buffer->params.textclass)
167                         .defaultfont();
168                 font.reduce(defaultfont);
169
170                 state << _("Font:") << ' '
171                       << font.stateText(&buffer->params);
172                 
173                 // The paragraph depth
174                 int depth = text->getDepth();
175                 if (depth > 0)
176                         state << _(", Depth: ") << depth;
177                 
178                 // The paragraph spacing, but only if different from
179                 // buffer spacing.
180                 if (!text->cursor.par()->params().spacing().isDefault()) {
181                         Spacing::Space cur_space =
182                                 text->cursor.par()->params().spacing().getSpace();
183                         state << _(", Spacing: ");
184
185                         switch (cur_space) {
186                         case Spacing::Single:
187                                 state << _("Single");
188                                 
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         }
207         return state.str().c_str();
208 }
209
210
211 /* -------> Does the actual toggle job of the XxxCB() calls above.
212  * Also shows the current font state.
213  */
214 void ToggleAndShow(BufferView * bv, LyXFont const & font, bool toggleall)
215 {
216         if (bv->available()) { 
217                 if (bv->theLockingInset()) {
218                         bv->theLockingInset()->setFont(bv, font, toggleall);
219                         return;
220                 }
221                 LyXText * text = bv->getLyXText();
222                 if (!text)
223                         return;
224
225                 bv->hideCursor();
226                 bv->update(text, BufferView::SELECT|BufferView::FITCUR);
227                 text->toggleFree(bv, font, toggleall);
228                 bv->update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
229
230                 if (font.language() != ignore_language ||
231 #ifndef NO_LATEX
232                     font.latex() != LyXFont::IGNORE ||
233 #endif
234                     font.number() != LyXFont::IGNORE)
235                 {
236                         LyXCursor & cursor = text->cursor;
237                         text->computeBidiTables(bv->buffer(), cursor.row());
238                         if (cursor.boundary() != 
239                             text->isBoundary(bv->buffer(), cursor.par(), cursor.pos(),
240                                              text->real_current_font) )
241                                 text->setCursor(bv, cursor.par(), cursor.pos(),
242                                                 false, !cursor.boundary());
243                 }
244         }
245 }