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