]> git.lyx.org Git - lyx.git/blob - src/bufferview_funcs.C
NEW_INSETS changes, + some small things in insettabular.C
[lyx.git] / src / bufferview_funcs.C
1 #include <config.h>
2
3 #include "bufferview_funcs.h"
4 #include "LyXView.h"
5 #include "BufferView.h"
6 #include "lyxparagraph.h"
7 #include "lyxfont.h"
8 #include "lyx_gui_misc.h"
9 #include "lyxtext.h"
10 #include "minibuffer.h"
11 #include "buffer.h"
12 #include "support/lstrings.h"
13 #include "lyx_cb.h"
14 #include "layout_forms.h"
15
16 extern FD_form_paragraph * fd_form_paragraph;
17 extern FD_form_character * fd_form_character;
18
19 #ifndef NEW_INSETS
20 void Foot(BufferView * bv)
21 {
22         if (!bv->available()) 
23                 return;
24         
25         bv->owner()->getMiniBuffer()
26                 ->Set(_("Inserting Footnote..."));
27         bv->hideCursor();
28         bv->update(BufferView::SELECT|BufferView::FITCUR);
29         bv->text->InsertFootnoteEnvironment(bv, LyXParagraph::FOOTNOTE);
30         bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
31 }
32 #endif
33
34
35 void Emph(BufferView * bv)
36 {
37         LyXFont font(LyXFont::ALL_IGNORE);
38         font.setEmph(LyXFont::TOGGLE);
39         ToggleAndShow(bv, font);
40 }
41
42
43 void Bold(BufferView * bv)
44 {
45         LyXFont font(LyXFont::ALL_IGNORE);
46         font.setSeries(LyXFont::BOLD_SERIES);
47         ToggleAndShow(bv, font);
48 }
49
50
51 void Noun(BufferView * bv)
52 {
53         LyXFont font(LyXFont::ALL_IGNORE);
54         font.setNoun(LyXFont::TOGGLE);
55         ToggleAndShow(bv, font);
56 }
57
58
59 #ifndef NEW_INSETS
60 void Margin(BufferView * bv)
61 {
62         if (bv->available()) {
63                 bv->owner()->getMiniBuffer()->Set(_("Inserting margin note..."));
64                 bv->hideCursor();
65                 bv->update(BufferView::SELECT|BufferView::FITCUR);
66                 bv->text->InsertFootnoteEnvironment(bv, LyXParagraph::MARGIN);
67                 bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
68         }
69 }
70 #endif
71
72
73 void Lang(BufferView * bv, string const & l)
74 {
75         LyXFont font(LyXFont::ALL_IGNORE);
76         Languages::iterator lit = languages.find(l);
77         if (lit != languages.end()) {
78                 font.setLanguage(&(*lit).second);
79                 ToggleAndShow(bv, font);
80         } else
81                 WriteAlert(_("Error! unknown language"),l);
82 }
83
84
85 #ifndef NEW_INSETS
86 void Melt(BufferView * bv)
87 {
88         if (!bv->available()) return;
89         
90         bv->owner()->getMiniBuffer()->Set(_("Melt"));
91         bv->hideCursor();
92         bv->beforeChange();
93         bv->update(BufferView::SELECT|BufferView::FITCUR);
94         bv->text->MeltFootnoteEnvironment(bv);
95         bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
96 }
97 #endif
98
99
100 void Tex(BufferView * bv)
101 {
102         LyXFont font(LyXFont::ALL_IGNORE);
103         font.setLatex (LyXFont::TOGGLE);
104         ToggleAndShow(bv, font);
105 }
106
107
108 // Change environment depth.
109 // if decInc >= 0, increment depth
110 // if decInc <  0, decrement depth
111 void changeDepth(BufferView * bv, int decInc)
112 {
113         if (!bv->available()) return;
114         
115         bv->hideCursor();
116         bv->update(BufferView::SELECT|BufferView::FITCUR);
117         if (decInc >= 0)
118                 bv->text->IncDepth(bv);
119         else
120                 bv->text->DecDepth(bv);
121         bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
122         bv->owner()->getMiniBuffer()
123                 ->Set(_("Changed environment depth"
124                         " (in possible range, maybe not)"));
125 }
126
127
128 void Free(BufferView * bv)
129 {
130         ToggleAndShow(bv, UserFreeFont(bv->buffer()->params));
131 }
132
133
134 // How should this actually work? Should it prohibit input in all BufferViews,
135 // or just in the current one? If "just the current one", then it should be
136 // placed in BufferView. If "all BufferViews" then LyXGUI (I think) should
137 // run "ProhibitInput" on all LyXViews which will run prohibitInput on all
138 // BufferViews. Or is it perhaps just the (input in) BufferViews in the
139 // current LyxView that should be prohibited (Lgb) (This applies to
140 // "AllowInput" as well.)
141 void ProhibitInput(BufferView * bv)
142 {
143         bv->hideCursor();
144
145         static Cursor cursor;
146         static bool cursor_undefined = true;
147    
148         if (cursor_undefined){
149                 cursor = XCreateFontCursor(fl_display, XC_watch);
150                 XFlush(fl_display);
151                 cursor_undefined = false;
152         }
153    
154         /* set the cursor to the watch for all forms and the canvas */ 
155         XDefineCursor(fl_display, bv->owner()->getForm()->window, 
156                       cursor);
157         if (fd_form_paragraph->form_paragraph->visible)
158                 XDefineCursor(fl_display,
159                               fd_form_paragraph->form_paragraph->window,
160                               cursor);
161         if (fd_form_character->form_character->visible)
162                 XDefineCursor(fl_display,
163                               fd_form_character->form_character->window,
164                               cursor);
165
166         XFlush(fl_display);
167         fl_deactivate_all_forms();
168 }
169
170
171 void AllowInput(BufferView * bv)
172 {
173         /* reset the cursor from the watch for all forms and the canvas */
174    
175         XUndefineCursor(fl_display, bv->owner()->getForm()->window);
176         if (fd_form_paragraph->form_paragraph->visible)
177                 XUndefineCursor(fl_display,
178                                 fd_form_paragraph->form_paragraph->window);
179         if (fd_form_character->form_character->visible)
180                 XUndefineCursor(fl_display,
181                                 fd_form_character->form_character->window);
182
183         XFlush(fl_display);
184         fl_activate_all_forms();
185 }
186
187
188 void Code(BufferView * bv)
189 {
190         LyXFont font(LyXFont::ALL_IGNORE);
191         font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
192         ToggleAndShow(bv, font);
193 }
194
195
196 void Sans(BufferView * bv)
197 {
198         LyXFont font(LyXFont::ALL_IGNORE);
199         font.setFamily(LyXFont::SANS_FAMILY);
200         ToggleAndShow(bv, font);
201 }
202
203
204 void Roman(BufferView * bv)
205 {
206         LyXFont font(LyXFont::ALL_IGNORE);
207         font.setFamily(LyXFont::ROMAN_FAMILY);
208         ToggleAndShow(bv, font);
209 }
210
211
212 void StyleReset(BufferView * bv)
213 {
214         LyXFont font(LyXFont::ALL_INHERIT, ignore_language);
215         ToggleAndShow(bv, font);
216 }
217
218
219 void Underline(BufferView * bv)
220 {
221         LyXFont font(LyXFont::ALL_IGNORE);
222         font.setUnderbar(LyXFont::TOGGLE);
223         ToggleAndShow(bv, font);
224 }
225
226
227 void FontSize(BufferView * bv, string const & size)
228 {
229         LyXFont font(LyXFont::ALL_IGNORE);
230         font.setGUISize(size);
231         ToggleAndShow(bv, font);
232 }
233
234
235 /* -------> Returns the current font and depth by printing a message. In the
236  * future perhaps we could try to implement a callback to the button-bar.
237  * That is, `light' the bold button when the font is currently bold, etc.
238  */
239 string CurrentState(BufferView * bv)
240 {
241         string state;
242         if (bv->available()) { 
243                 // I think we should only show changes from the default
244                 // font. (Asger)
245                 Buffer * buffer = bv->buffer();
246                 LyXFont font = bv->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 = bv->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 (!bv->text->cursor.par()->spacing.isDefault()) {
260                         Spacing::Space cur_space =
261                                 bv->text->cursor.par()->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(bv->text->cursor.par()->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)
292 {
293         if (bv->available()) { 
294                 bv->hideCursor();
295                 bv->update(BufferView::SELECT|BufferView::FITCUR);
296                 if (bv->the_locking_inset)
297                         bv->the_locking_inset->SetFont(bv, font, toggleall);
298                 else
299                         bv->text->ToggleFree(bv, font, toggleall);
300                 bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
301
302                 if (font.language() != ignore_language ||
303                     font.latex() != LyXFont::IGNORE) {
304                         LyXText * text = bv->text;
305                         LyXCursor & cursor = text->cursor;
306                         text->ComputeBidiTables(bv->buffer(), cursor.row());
307                         if (cursor.boundary() != 
308                             text->IsBoundary(bv->buffer(), cursor.par(), cursor.pos(),
309                                              text->real_current_font) )
310                                 text->SetCursor(bv, cursor.par(), cursor.pos(),
311                                                 false, !cursor.boundary());
312                 }
313         }
314 }