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