]> git.lyx.org Git - lyx.git/blob - src/bufferview_funcs.C
to much stuff for my liking...
[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 "lyxtext.h"
23 #include "buffer.h"
24 #include "lyx_cb.h"
25 #include "language.h"
26 #include "gettext.h"
27 #include "ParagraphParameters.h"
28 #include "lyxtextclasslist.h"
29
30 #include "frontends/Alert.h"
31
32 #include "support/lstrings.h"
33
34 void emph(BufferView * bv)
35 {
36         LyXFont font(LyXFont::ALL_IGNORE);
37         font.setEmph(LyXFont::TOGGLE);
38         toggleAndShow(bv, font);
39 }
40
41
42 void bold(BufferView * bv)
43 {
44         LyXFont font(LyXFont::ALL_IGNORE);
45         font.setSeries(LyXFont::BOLD_SERIES);
46         toggleAndShow(bv, font);
47 }
48
49
50 void noun(BufferView * bv)
51 {
52         LyXFont font(LyXFont::ALL_IGNORE);
53         font.setNoun(LyXFont::TOGGLE);
54         toggleAndShow(bv, font);
55 }
56
57
58 void number(BufferView * bv)
59 {
60         LyXFont font(LyXFont::ALL_IGNORE);
61         font.setNumber(LyXFont::TOGGLE);
62         toggleAndShow(bv, font);
63 }
64
65 void lang(BufferView * bv, string const & l)
66 {
67         LyXFont font(LyXFont::ALL_IGNORE);
68         Language const * lang = languages.getLanguage(l);
69         if (lang) {
70                 font.setLanguage(lang);
71                 toggleAndShow(bv, font);
72         } else
73                 Alert::alert(_("Error! unknown language"),l);
74 }
75
76
77 // Change environment depth.
78 // if decInc >= 0, increment depth
79 // if decInc <  0, decrement depth
80 void changeDepth(BufferView * bv, LyXText * text, int decInc)
81 {
82         if (!bv->available() || !text)
83             return;
84         
85         bv->hideCursor();
86         bv->update(bv->text, BufferView::SELECT|BufferView::FITCUR);
87         if (decInc >= 0)
88                 text->incDepth(bv);
89         else
90                 text->decDepth(bv);
91         if (text->inset_owner)
92             bv->updateInset((Inset *)text->inset_owner, true);
93         bv->update(bv->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
94         bv->owner()->message(_("Changed environment depth "
95                                "(in possible range, maybe not)"));
96 }
97
98
99 void code(BufferView * bv)
100 {
101         LyXFont font(LyXFont::ALL_IGNORE);
102         font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
103         toggleAndShow(bv, font);
104 }
105
106
107 void sans(BufferView * bv)
108 {
109         LyXFont font(LyXFont::ALL_IGNORE);
110         font.setFamily(LyXFont::SANS_FAMILY);
111         toggleAndShow(bv, font);
112 }
113
114
115 void roman(BufferView * bv)
116 {
117         LyXFont font(LyXFont::ALL_IGNORE);
118         font.setFamily(LyXFont::ROMAN_FAMILY);
119         toggleAndShow(bv, font);
120 }
121
122
123 void styleReset(BufferView * bv)
124 {
125 #ifndef INHERIT_LANG
126         LyXFont font(LyXFont::ALL_INHERIT, ignore_language);
127 #else 
128         LyXFont font(LyXFont::ALL_INHERIT);
129 #endif
130         toggleAndShow(bv, font);
131 }
132
133
134 void underline(BufferView * bv)
135 {
136         LyXFont font(LyXFont::ALL_IGNORE);
137         font.setUnderbar(LyXFont::TOGGLE);
138         toggleAndShow(bv, font);
139 }
140
141
142 void fontSize(BufferView * bv, string const & size)
143 {
144         LyXFont font(LyXFont::ALL_IGNORE);
145         font.setLyXSize(size);
146         toggleAndShow(bv, font);
147 }
148
149
150 // Returns the current font and depth as a message. 
151 string const currentState(BufferView * bv)
152 {
153         ostringstream state;
154
155         if (bv->available()) { 
156                 // I think we should only show changes from the default
157                 // font. (Asger)
158                 LyXText * text = bv->getLyXText();
159                 Buffer * buffer = bv->buffer();
160                 LyXFont font = text->real_current_font;
161                 LyXFont const & defaultfont =
162                         textclasslist
163                         .TextClass(buffer->params.textclass)
164                         .defaultfont();
165                 font.reduce(defaultfont);
166
167                 state << _("Font:") << ' '
168                       << font.stateText(&buffer->params);
169                 
170                 // The paragraph depth
171                 int depth = text->getDepth();
172                 if (depth > 0)
173                         state << _(", Depth: ") << depth;
174                 
175                 // The paragraph spacing, but only if different from
176                 // buffer spacing.
177                 if (!text->cursor.par()->params().spacing().isDefault()) {
178                         Spacing::Space cur_space =
179                                 text->cursor.par()->params().spacing().getSpace();
180                         state << _(", Spacing: ");
181
182                         switch (cur_space) {
183                         case Spacing::Single:
184                                 state << _("Single");
185                                 
186                                 break;
187                         case Spacing::Onehalf:
188                                 state << _("Onehalf");
189                                 break;
190                         case Spacing::Double:
191                                 state << _("Double");
192                                 break;
193                         case Spacing::Other:
194                                 state << _("Other (")
195                                       << text->cursor.par()->params().spacing().getValue()
196                                       << ")";
197                                 break;
198                         case Spacing::Default:
199                                 // should never happen, do nothing
200                                 break;
201                         }
202                 }
203 #if 1
204                 state << _(", Paragraph: ") << text->cursor.par()->id();
205 #endif
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                     font.number() != LyXFont::IGNORE) {
232                         LyXCursor & cursor = text->cursor;
233                         text->computeBidiTables(bv->buffer(), cursor.row());
234                         if (cursor.boundary() != 
235                             text->isBoundary(bv->buffer(), cursor.par(), cursor.pos(),
236                                              text->real_current_font))
237                                 text->setCursor(bv, cursor.par(), cursor.pos(),
238                                                 false, !cursor.boundary());
239                 }
240         }
241 }