]> git.lyx.org Git - lyx.git/blob - src/bufferview_funcs.C
some visual feedback for extra vertical space
[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 // Change environment depth.
76 // if decInc >= 0, increment depth
77 // if decInc <  0, decrement depth
78 void changeDepth(BufferView * bv, LyXText * text, int decInc)
79 {
80         if (!bv->available() || !text)
81             return;
82         
83         bv->hideCursor();
84         bv->update(bv->text, BufferView::SELECT|BufferView::FITCUR);
85         if (decInc >= 0)
86                 text->incDepth(bv);
87         else
88                 text->decDepth(bv);
89         if (text->inset_owner)
90             bv->updateInset((Inset *)text->inset_owner, true);
91         bv->update(bv->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
92         bv->owner()->message(_("Changed environment depth "
93                                "(in possible range, maybe not)"));
94 }
95
96
97 void code(BufferView * bv)
98 {
99         LyXFont font(LyXFont::ALL_IGNORE);
100         font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
101         toggleAndShow(bv, font);
102 }
103
104
105 void sans(BufferView * bv)
106 {
107         LyXFont font(LyXFont::ALL_IGNORE);
108         font.setFamily(LyXFont::SANS_FAMILY);
109         toggleAndShow(bv, font);
110 }
111
112
113 void roman(BufferView * bv)
114 {
115         LyXFont font(LyXFont::ALL_IGNORE);
116         font.setFamily(LyXFont::ROMAN_FAMILY);
117         toggleAndShow(bv, font);
118 }
119
120
121 void styleReset(BufferView * bv)
122 {
123         LyXFont font(LyXFont::ALL_INHERIT);
124         toggleAndShow(bv, font);
125 }
126
127
128 void underline(BufferView * bv)
129 {
130         LyXFont font(LyXFont::ALL_IGNORE);
131         font.setUnderbar(LyXFont::TOGGLE);
132         toggleAndShow(bv, font);
133 }
134
135
136 void fontSize(BufferView * bv, string const & size)
137 {
138         LyXFont font(LyXFont::ALL_IGNORE);
139         font.setLyXSize(size);
140         toggleAndShow(bv, font);
141 }
142
143
144 // Returns the current font and depth as a message. 
145 string const currentState(BufferView * bv)
146 {
147         ostringstream state;
148
149         if (bv->available()) { 
150                 // I think we should only show changes from the default
151                 // font. (Asger)
152                 LyXText * text = bv->getLyXText();
153                 Buffer * buffer = bv->buffer();
154                 LyXFont font = text->real_current_font;
155                 LyXFont const & defaultfont =
156                         textclasslist
157                         .TextClass(buffer->params.textclass)
158                         .defaultfont();
159                 font.reduce(defaultfont);
160
161                 state << _("Font:") << ' '
162                       << font.stateText(&buffer->params);
163                 
164                 // The paragraph depth
165                 int depth = text->getDepth();
166                 if (depth > 0)
167                         state << _(", Depth: ") << depth;
168                 
169                 // The paragraph spacing, but only if different from
170                 // buffer spacing.
171                 if (!text->cursor.par()->params().spacing().isDefault()) {
172                         Spacing::Space cur_space =
173                                 text->cursor.par()->params().spacing().getSpace();
174                         state << _(", Spacing: ");
175
176                         switch (cur_space) {
177                         case Spacing::Single:
178                                 state << _("Single");
179                                 
180                                 break;
181                         case Spacing::Onehalf:
182                                 state << _("Onehalf");
183                                 break;
184                         case Spacing::Double:
185                                 state << _("Double");
186                                 break;
187                         case Spacing::Other:
188                                 state << _("Other (")
189                                       << text->cursor.par()->params().spacing().getValue()
190                                       << ")";
191                                 break;
192                         case Spacing::Default:
193                                 // should never happen, do nothing
194                                 break;
195                         }
196                 }
197 #if 1
198                 state << _(", Paragraph: ") << text->cursor.par()->id();
199 #endif
200         }
201         return state.str().c_str();
202 }
203
204
205 /* -------> Does the actual toggle job of the XxxCB() calls above.
206  * Also shows the current font state.
207  */
208 void toggleAndShow(BufferView * bv, LyXFont const & font, bool toggleall)
209 {
210         if (bv->available()) { 
211                 if (bv->theLockingInset()) {
212                         bv->theLockingInset()->setFont(bv, font, toggleall);
213                         return;
214                 }
215                 LyXText * text = bv->getLyXText();
216                 if (!text)
217                         return;
218
219                 bv->hideCursor();
220                 bv->update(text, BufferView::SELECT|BufferView::FITCUR);
221                 text->toggleFree(bv, font, toggleall);
222                 bv->update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
223
224                 if (font.language() != ignore_language ||
225                     font.number() != LyXFont::IGNORE)
226                 {
227                         LyXCursor & cursor = text->cursor;
228                         text->computeBidiTables(bv->buffer(), cursor.row());
229                         if (cursor.boundary() != 
230                             text->isBoundary(bv->buffer(), cursor.par(), cursor.pos(),
231                                              text->real_current_font) )
232                                 text->setCursor(bv, cursor.par(), cursor.pos(),
233                                                 false, !cursor.boundary());
234                 }
235         }
236 }