]> git.lyx.org Git - lyx.git/blob - src/bufferview_funcs.C
layout as string
[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[buffer->params.textclass].defaultfont();
163                 font.reduce(defaultfont);
164
165                 state << _("Font:") << ' '
166                       << font.stateText(&buffer->params);
167                 
168                 // The paragraph depth
169                 int depth = text->getDepth();
170                 if (depth > 0)
171                         state << _(", Depth: ") << depth;
172                 
173                 // The paragraph spacing, but only if different from
174                 // buffer spacing.
175                 if (!text->cursor.par()->params().spacing().isDefault()) {
176                         Spacing::Space cur_space =
177                                 text->cursor.par()->params().spacing().getSpace();
178                         state << _(", Spacing: ");
179
180                         switch (cur_space) {
181                         case Spacing::Single:
182                                 state << _("Single");
183                                 
184                                 break;
185                         case Spacing::Onehalf:
186                                 state << _("Onehalf");
187                                 break;
188                         case Spacing::Double:
189                                 state << _("Double");
190                                 break;
191                         case Spacing::Other:
192                                 state << _("Other (")
193                                       << text->cursor.par()->params().spacing().getValue()
194                                       << ")";
195                                 break;
196                         case Spacing::Default:
197                                 // should never happen, do nothing
198                                 break;
199                         }
200                 }
201 #if 1
202                 state << _(", Paragraph: ") << text->cursor.par()->id();
203 #endif
204         }
205         return state.str().c_str();
206 }
207
208
209 /* -------> Does the actual toggle job of the XxxCB() calls above.
210  * Also shows the current font state.
211  */
212 void toggleAndShow(BufferView * bv, LyXFont const & font, bool toggleall)
213 {
214         if (bv->available()) { 
215                 if (bv->theLockingInset()) {
216                         bv->theLockingInset()->setFont(bv, font, toggleall);
217                         return;
218                 }
219                 LyXText * text = bv->getLyXText();
220                 if (!text)
221                         return;
222
223                 bv->hideCursor();
224                 bv->update(text, BufferView::SELECT|BufferView::FITCUR);
225                 text->toggleFree(bv, font, toggleall);
226                 bv->update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
227
228                 if (font.language() != ignore_language ||
229                     font.number() != LyXFont::IGNORE) {
230                         LyXCursor & cursor = text->cursor;
231                         text->computeBidiTables(bv->buffer(), cursor.row());
232                         if (cursor.boundary() != 
233                             text->isBoundary(bv->buffer(), cursor.par(), cursor.pos(),
234                                              text->real_current_font))
235                                 text->setCursor(bv, cursor.par(), cursor.pos(),
236                                                 false, !cursor.boundary());
237                 }
238         }
239 }