]> git.lyx.org Git - lyx.git/blob - src/bufferview_funcs.C
baf340bd027618975e26f2aec2996d8911aee97f
[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 as a message. 
236 string CurrentState(BufferView * bv)
237 {
238         string state;
239         if (bv->available()) { 
240                 // I think we should only show changes from the default
241                 // font. (Asger)
242                 Buffer * buffer = bv->buffer();
243                 LyXFont font = bv->text->real_current_font;
244                 LyXFont const & defaultfont =
245                         textclasslist
246                         .TextClass(buffer->params.textclass)
247                         .defaultfont();
248                 font.reduce(defaultfont);
249                 state = _("Font: ") + font.stateText(&buffer->params);
250                 // The paragraph depth
251                 int depth = bv->text->GetDepth();
252                 if (depth > 0) 
253                         state += string(_(", Depth: ")) + tostr(depth);
254                 // The paragraph spacing, but only if different from
255                 // buffer spacing.
256                 if (!bv->text->cursor.par()->spacing.isDefault()) {
257                         Spacing::Space cur_space =
258                                 bv->text->cursor.par()->spacing.getSpace();
259                         state += _(", Spacing: ");
260                         switch (cur_space) {
261                         case Spacing::Single:
262                                 state += _("Single");
263                                 break;
264                         case Spacing::Onehalf:
265                                 state += _("Onehalf");
266                                 break;
267                         case Spacing::Double:
268                                 state += _("Double");
269                                 break;
270                         case Spacing::Other:
271                                 state += _("Other (");
272                                 state += tostr(bv->text->cursor.par()->spacing.getValue());
273                                 state += ")";
274                                 break;
275                         case Spacing::Default:
276                                 // should never happen, do nothing
277                                 break;
278                         }
279                 }
280         }
281         return state;
282 }
283
284
285 /* -------> Does the actual toggle job of the XxxCB() calls above.
286  * Also shows the current font state.
287  */
288 void ToggleAndShow(BufferView * bv, LyXFont const & font)
289 {
290         if (bv->available()) { 
291                 bv->hideCursor();
292                 bv->update(BufferView::SELECT|BufferView::FITCUR);
293                 if (bv->the_locking_inset)
294                         bv->the_locking_inset->SetFont(bv, font, toggleall);
295                 else
296                         bv->text->ToggleFree(bv, font, toggleall);
297                 bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
298
299                 if (font.language() != ignore_language ||
300                     font.latex() != LyXFont::IGNORE) {
301                         LyXText * text = bv->text;
302                         LyXCursor & cursor = text->cursor;
303                         text->ComputeBidiTables(bv->buffer(), cursor.row());
304                         if (cursor.boundary() != 
305                             text->IsBoundary(bv->buffer(), cursor.par(), cursor.pos(),
306                                              text->real_current_font) )
307                                 text->SetCursor(bv, cursor.par(), cursor.pos(),
308                                                 false, !cursor.boundary());
309                 }
310         }
311 }