]> git.lyx.org Git - lyx.git/blob - src/bufferview_funcs.C
new alert code
[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 "frontends/Alert.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                 Alert::alert(_("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 #ifndef INHERIT_LANG
124         LyXFont font(LyXFont::ALL_INHERIT, ignore_language);
125 #else 
126         LyXFont font(LyXFont::ALL_INHERIT);
127 #endif
128         toggleAndShow(bv, font);
129 }
130
131
132 void underline(BufferView * bv)
133 {
134         LyXFont font(LyXFont::ALL_IGNORE);
135         font.setUnderbar(LyXFont::TOGGLE);
136         toggleAndShow(bv, font);
137 }
138
139
140 void fontSize(BufferView * bv, string const & size)
141 {
142         LyXFont font(LyXFont::ALL_IGNORE);
143         font.setLyXSize(size);
144         toggleAndShow(bv, font);
145 }
146
147
148 // Returns the current font and depth as a message. 
149 string const currentState(BufferView * bv)
150 {
151         ostringstream state;
152
153         if (bv->available()) { 
154                 // I think we should only show changes from the default
155                 // font. (Asger)
156                 LyXText * text = bv->getLyXText();
157                 Buffer * buffer = bv->buffer();
158                 LyXFont font = text->real_current_font;
159                 LyXFont const & defaultfont =
160                         textclasslist
161                         .TextClass(buffer->params.textclass)
162                         .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 }