]> git.lyx.org Git - lyx.git/blob - src/bufferview_funcs.C
fixed two consecutive spaces handling
[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-2000 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 "lyxparagraph.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 "lyxfunc.h"
30
31 #ifndef NEW_INSETS
32 void Foot(BufferView * bv)
33 {
34         if (!bv->available()) 
35                 return;
36         
37         bv->owner()->getMiniBuffer()
38                 ->Set(_("Inserting Footnote..."));
39         bv->hideCursor();
40         bv->update(bv->text, BufferView::SELECT|BufferView::FITCUR);
41         bv->text->InsertFootnoteEnvironment(bv, LyXParagraph::FOOTNOTE);
42         bv->update(bv->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
43 }
44 #endif
45
46
47 void Emph(BufferView * bv)
48 {
49         LyXFont font(LyXFont::ALL_IGNORE);
50         font.setEmph(LyXFont::TOGGLE);
51         ToggleAndShow(bv, font);
52 }
53
54
55 void Bold(BufferView * bv)
56 {
57         LyXFont font(LyXFont::ALL_IGNORE);
58         font.setSeries(LyXFont::BOLD_SERIES);
59         ToggleAndShow(bv, font);
60 }
61
62
63 void Noun(BufferView * bv)
64 {
65         LyXFont font(LyXFont::ALL_IGNORE);
66         font.setNoun(LyXFont::TOGGLE);
67         ToggleAndShow(bv, font);
68 }
69
70
71 #ifndef NEW_INSETS
72 void Margin(BufferView * bv)
73 {
74         if (bv->available()) {
75                 bv->owner()->getLyXFunc()->Dispatch(LFUN_MESSAGE, _("Inserting margin note...");
76                 bv->hideCursor();
77                 bv->update(bv->text, BufferView::SELECT|BufferView::FITCUR);
78                 bv->text->InsertFootnoteEnvironment(bv, LyXParagraph::MARGIN);
79                 bv->update(bv->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
80         }
81 }
82 #endif
83
84 void Number(BufferView * bv)
85 {
86         LyXFont font(LyXFont::ALL_IGNORE);
87         font.setNumber(LyXFont::TOGGLE);
88         ToggleAndShow(bv, font);
89 }
90
91 void Lang(BufferView * bv, string const & l)
92 {
93         LyXFont font(LyXFont::ALL_IGNORE);
94         Language const * lang = languages.getLanguage(l);
95         if (lang) {
96                 font.setLanguage(lang);
97                 ToggleAndShow(bv, font);
98         } else
99                 WriteAlert(_("Error! unknown language"),l);
100 }
101
102
103 #ifndef NEW_INSETS
104 void Melt(BufferView * bv)
105 {
106         if (!bv->available()) return;
107
108         bv->owner()->getLyXFunc()->Dispatch(LFUN_MESSAGE, _("Melt"));
109         bv->hideCursor();
110         bv->beforeChange(bv->text);
111         bv->update(bv->text, BufferView::SELECT|BufferView::FITCUR);
112         bv->text->MeltFootnoteEnvironment(bv);
113         bv->update(bv->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
114 }
115 #endif
116
117
118 void Tex(BufferView * bv)
119 {
120         LyXFont font(LyXFont::ALL_IGNORE);
121         font.setLatex (LyXFont::TOGGLE);
122         ToggleAndShow(bv, font);
123 }
124
125
126 // Change environment depth.
127 // if decInc >= 0, increment depth
128 // if decInc <  0, decrement depth
129 void changeDepth(BufferView * bv, LyXText * text, int decInc)
130 {
131         if (!bv->available() || !text)
132             return;
133         
134         bv->hideCursor();
135         bv->update(bv->text, BufferView::SELECT|BufferView::FITCUR);
136         if (decInc >= 0)
137                 text->IncDepth(bv);
138         else
139                 text->DecDepth(bv);
140         if (text->inset_owner)
141             bv->updateInset((Inset *)text->inset_owner, true);
142         bv->update(bv->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
143         bv->owner()->getLyXFunc()
144                 ->Dispatch(LFUN_MESSAGE,
145                            _("Changed environment depth "
146                              "(in possible range, maybe not)"));
147 }
148
149
150 // How should this actually work? Should it prohibit input in all BufferViews,
151 // or just in the current one? If "just the current one", then it should be
152 // placed in BufferView. If "all BufferViews" then LyXGUI (I think) should
153 // run "ProhibitInput" on all LyXViews which will run prohibitInput on all
154 // BufferViews. Or is it perhaps just the (input in) BufferViews in the
155 // current LyxView that should be prohibited (Lgb) (This applies to
156 // "AllowInput" as well.)
157 void ProhibitInput(BufferView * bv)
158 {
159         bv->hideCursor();
160
161         static Cursor cursor;
162         static bool cursor_undefined = true;
163    
164         if (cursor_undefined){
165                 cursor = XCreateFontCursor(fl_get_display(), XC_watch);
166                 XFlush(fl_get_display());
167                 cursor_undefined = false;
168         }
169    
170         /* set the cursor to the watch for all forms and the canvas */ 
171         XDefineCursor(fl_get_display(), bv->owner()->getForm()->window, 
172                       cursor);
173
174         XFlush(fl_get_display());
175         fl_deactivate_all_forms();
176 }
177
178
179 void AllowInput(BufferView * bv)
180 {
181         /* reset the cursor from the watch for all forms and the canvas */
182    
183         XUndefineCursor(fl_get_display(), bv->owner()->getForm()->window);
184
185         XFlush(fl_get_display());
186         fl_activate_all_forms();
187 }
188
189
190 void Code(BufferView * bv)
191 {
192         LyXFont font(LyXFont::ALL_IGNORE);
193         font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
194         ToggleAndShow(bv, font);
195 }
196
197
198 void Sans(BufferView * bv)
199 {
200         LyXFont font(LyXFont::ALL_IGNORE);
201         font.setFamily(LyXFont::SANS_FAMILY);
202         ToggleAndShow(bv, font);
203 }
204
205
206 void Roman(BufferView * bv)
207 {
208         LyXFont font(LyXFont::ALL_IGNORE);
209         font.setFamily(LyXFont::ROMAN_FAMILY);
210         ToggleAndShow(bv, font);
211 }
212
213
214 void StyleReset(BufferView * bv)
215 {
216         LyXFont font(LyXFont::ALL_INHERIT, ignore_language);
217         ToggleAndShow(bv, font);
218 }
219
220
221 void Underline(BufferView * bv)
222 {
223         LyXFont font(LyXFont::ALL_IGNORE);
224         font.setUnderbar(LyXFont::TOGGLE);
225         ToggleAndShow(bv, font);
226 }
227
228
229 void FontSize(BufferView * bv, string const & size)
230 {
231         LyXFont font(LyXFont::ALL_IGNORE);
232         font.setLyXSize(size);
233         ToggleAndShow(bv, font);
234 }
235
236
237 // Returns the current font and depth as a message. 
238 string const CurrentState(BufferView * bv)
239 {
240         string state;
241         if (bv->available()) { 
242                 // I think we should only show changes from the default
243                 // font. (Asger)
244                 LyXText * text = bv->getLyXText();
245                 Buffer * buffer = bv->buffer();
246                 LyXFont font = text->real_current_font;
247                 LyXFont const & defaultfont =
248                         textclasslist
249                         .TextClass(buffer->params.textclass)
250                         .defaultfont();
251                 font.reduce(defaultfont);
252                 state = _("Font: ") + font.stateText(&buffer->params);
253                 // The paragraph depth
254                 int depth = text->GetDepth();
255                 if (depth > 0) 
256                         state += string(_(", Depth: ")) + tostr(depth);
257                 // The paragraph spacing, but only if different from
258                 // buffer spacing.
259                 if (!text->cursor.par()->params.spacing().isDefault()) {
260                         Spacing::Space cur_space =
261                                 text->cursor.par()->params.spacing().getSpace();
262                         state += _(", Spacing: ");
263                         switch (cur_space) {
264                         case Spacing::Single:
265                                 state += _("Single");
266                                 break;
267                         case Spacing::Onehalf:
268                                 state += _("Onehalf");
269                                 break;
270                         case Spacing::Double:
271                                 state += _("Double");
272                                 break;
273                         case Spacing::Other:
274                                 state += _("Other (");
275                                 state += tostr(text->cursor.par()->params.spacing().getValue());
276                                 state += ")";
277                                 break;
278                         case Spacing::Default:
279                                 // should never happen, do nothing
280                                 break;
281                         }
282                 }
283         }
284         return state;
285 }
286
287
288 /* -------> Does the actual toggle job of the XxxCB() calls above.
289  * Also shows the current font state.
290  */
291 void ToggleAndShow(BufferView * bv, LyXFont const & font, bool toggleall)
292 {
293         if (bv->available()) { 
294                 LyXText * text = bv->getLyXText();
295
296                 bv->hideCursor();
297                 bv->update(text, BufferView::SELECT|BufferView::FITCUR);
298                 text->ToggleFree(bv, font, toggleall);
299                 bv->update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
300
301                 if (font.language() != ignore_language ||
302                     font.latex() != LyXFont::IGNORE ||
303                     font.number() != LyXFont::IGNORE) {
304                         LyXCursor & cursor = text->cursor;
305                         text->ComputeBidiTables(bv->buffer(), cursor.row());
306                         if (cursor.boundary() != 
307                             text->IsBoundary(bv->buffer(), cursor.par(), cursor.pos(),
308                                              text->real_current_font) )
309                                 text->SetCursor(bv, cursor.par(), cursor.pos(),
310                                                 false, !cursor.boundary());
311                 }
312         }
313 }