]> git.lyx.org Git - lyx.git/blob - src/bufferview_funcs.C
Fixed various "missing features" in the tabular/textinset 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-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, LyXText * text, int decInc)
131 {
132         if (!bv->available() || !text)
133             return;
134         
135         bv->hideCursor();
136         bv->update(BufferView::SELECT|BufferView::FITCUR);
137         if (decInc >= 0)
138                 text->IncDepth(bv);
139         else
140                 text->DecDepth(bv);
141         if (text->inset_owner)
142             bv->updateInset((Inset *)text->inset_owner, true);
143         bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
144         bv->owner()->getMiniBuffer()
145                 ->Set(_("Changed environment depth"
146                         " (in possible range, maybe not)"));
147 }
148
149
150 void Free(BufferView * bv)
151 {
152         ToggleAndShow(bv, UserFreeFont(bv->buffer()->params));
153 }
154
155
156 // How should this actually work? Should it prohibit input in all BufferViews,
157 // or just in the current one? If "just the current one", then it should be
158 // placed in BufferView. If "all BufferViews" then LyXGUI (I think) should
159 // run "ProhibitInput" on all LyXViews which will run prohibitInput on all
160 // BufferViews. Or is it perhaps just the (input in) BufferViews in the
161 // current LyxView that should be prohibited (Lgb) (This applies to
162 // "AllowInput" as well.)
163 void ProhibitInput(BufferView * bv)
164 {
165         bv->hideCursor();
166
167         static Cursor cursor;
168         static bool cursor_undefined = true;
169    
170         if (cursor_undefined){
171                 cursor = XCreateFontCursor(fl_get_display(), XC_watch);
172                 XFlush(fl_get_display());
173                 cursor_undefined = false;
174         }
175    
176         /* set the cursor to the watch for all forms and the canvas */ 
177         XDefineCursor(fl_get_display(), bv->owner()->getForm()->window, 
178                       cursor);
179
180         if (fd_form_character->form_character->visible)
181                 XDefineCursor(fl_get_display(),
182                               fd_form_character->form_character->window,
183                               cursor);
184
185         XFlush(fl_get_display());
186         fl_deactivate_all_forms();
187 }
188
189
190 void AllowInput(BufferView * bv)
191 {
192         /* reset the cursor from the watch for all forms and the canvas */
193    
194         XUndefineCursor(fl_get_display(), bv->owner()->getForm()->window);
195
196         if (fd_form_character->form_character->visible)
197                 XUndefineCursor(fl_get_display(),
198                                 fd_form_character->form_character->window);
199
200         XFlush(fl_get_display());
201         fl_activate_all_forms();
202 }
203
204
205 void Code(BufferView * bv)
206 {
207         LyXFont font(LyXFont::ALL_IGNORE);
208         font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
209         ToggleAndShow(bv, font);
210 }
211
212
213 void Sans(BufferView * bv)
214 {
215         LyXFont font(LyXFont::ALL_IGNORE);
216         font.setFamily(LyXFont::SANS_FAMILY);
217         ToggleAndShow(bv, font);
218 }
219
220
221 void Roman(BufferView * bv)
222 {
223         LyXFont font(LyXFont::ALL_IGNORE);
224         font.setFamily(LyXFont::ROMAN_FAMILY);
225         ToggleAndShow(bv, font);
226 }
227
228
229 void StyleReset(BufferView * bv)
230 {
231         LyXFont font(LyXFont::ALL_INHERIT, ignore_language);
232         ToggleAndShow(bv, font);
233 }
234
235
236 void Underline(BufferView * bv)
237 {
238         LyXFont font(LyXFont::ALL_IGNORE);
239         font.setUnderbar(LyXFont::TOGGLE);
240         ToggleAndShow(bv, font);
241 }
242
243
244 void FontSize(BufferView * bv, string const & size)
245 {
246         LyXFont font(LyXFont::ALL_IGNORE);
247         font.setLyXSize(size);
248         ToggleAndShow(bv, font);
249 }
250
251
252 // Returns the current font and depth as a message. 
253 string const CurrentState(BufferView * bv)
254 {
255         string state;
256         if (bv->available()) { 
257                 // I think we should only show changes from the default
258                 // font. (Asger)
259                 LyXText * text = bv->getLyXText();
260                 Buffer * buffer = bv->buffer();
261                 LyXFont font = text->real_current_font;
262                 LyXFont const & defaultfont =
263                         textclasslist
264                         .TextClass(buffer->params.textclass)
265                         .defaultfont();
266                 font.reduce(defaultfont);
267                 state = _("Font: ") + font.stateText(&buffer->params);
268                 // The paragraph depth
269                 int depth = text->GetDepth();
270                 if (depth > 0) 
271                         state += string(_(", Depth: ")) + tostr(depth);
272                 // The paragraph spacing, but only if different from
273                 // buffer spacing.
274                 if (!text->cursor.par()->spacing.isDefault()) {
275                         Spacing::Space cur_space =
276                                 text->cursor.par()->spacing.getSpace();
277                         state += _(", Spacing: ");
278                         switch (cur_space) {
279                         case Spacing::Single:
280                                 state += _("Single");
281                                 break;
282                         case Spacing::Onehalf:
283                                 state += _("Onehalf");
284                                 break;
285                         case Spacing::Double:
286                                 state += _("Double");
287                                 break;
288                         case Spacing::Other:
289                                 state += _("Other (");
290                                 state += tostr(text->cursor.par()->spacing.getValue());
291                                 state += ")";
292                                 break;
293                         case Spacing::Default:
294                                 // should never happen, do nothing
295                                 break;
296                         }
297                 }
298         }
299         return state;
300 }
301
302
303 /* -------> Does the actual toggle job of the XxxCB() calls above.
304  * Also shows the current font state.
305  */
306 void ToggleAndShow(BufferView * bv, LyXFont const & font)
307 {
308         if (bv->available()) { 
309                 bv->hideCursor();
310                 bv->update(BufferView::SELECT|BufferView::FITCUR);
311                 if (bv->theLockingInset())
312                         bv->theLockingInset()->SetFont(bv, font, toggleall);
313                 else
314                         bv->text->ToggleFree(bv, font, toggleall);
315                 bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
316
317                 if (font.language() != ignore_language ||
318                     font.latex() != LyXFont::IGNORE ||
319                     font.number() != LyXFont::IGNORE) {
320                         LyXText * text = bv->getLyXText();
321                         LyXCursor & cursor = text->cursor;
322                         text->ComputeBidiTables(bv->buffer(), cursor.row());
323                         if (cursor.boundary() != 
324                             text->IsBoundary(bv->buffer(), cursor.par(), cursor.pos(),
325                                              text->real_current_font) )
326                                 text->SetCursor(bv, cursor.par(), cursor.pos(),
327                                                 false, !cursor.boundary());
328                 }
329         }
330 }