]> git.lyx.org Git - lyx.git/blob - src/bufferview_funcs.C
e3f9f0a73422101a54d68cc99e5b6b2e565773cf
[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 static
21 void SetXtermCursor(Window win)
22 {
23         static Cursor cursor;
24         static bool cursor_undefined = true;
25         if (cursor_undefined){
26                 cursor = XCreateFontCursor(fl_display, XC_xterm);
27                 XFlush(fl_display);
28                 cursor_undefined = false;
29         }
30         XDefineCursor(fl_display, win, cursor);
31         XFlush(fl_display);
32 }
33
34
35 void Foot(BufferView * bv)
36 {
37         if (!bv->available()) 
38                 return;
39         
40         bv->owner()->getMiniBuffer()
41                 ->Set(_("Inserting Footnote..."));
42         bv->hideCursor();
43         bv->update(-2);
44         bv->text->InsertFootnoteEnvironment(LyXParagraph::FOOTNOTE);
45         bv->update(1);
46 }
47
48
49 void Emph(BufferView * bv)
50 {
51         LyXFont font(LyXFont::ALL_IGNORE);
52         font.setEmph(LyXFont::TOGGLE);
53         ToggleAndShow(bv, font);
54 }
55
56
57 void Bold(BufferView * bv)
58 {
59         LyXFont font(LyXFont::ALL_IGNORE);
60         font.setSeries(LyXFont::BOLD_SERIES);
61         ToggleAndShow(bv, font);
62 }
63
64
65 void Noun(BufferView * bv)
66 {
67         LyXFont font(LyXFont::ALL_IGNORE);
68         font.setNoun(LyXFont::TOGGLE);
69         ToggleAndShow(bv, font);
70 }
71
72
73 void Margin(BufferView * bv)
74 {
75         if (bv->available()) {
76                 bv->owner()->getMiniBuffer()->Set(_("Inserting margin note..."));
77                 bv->hideCursor();
78                 bv->update(-2);
79                 bv->text->InsertFootnoteEnvironment(LyXParagraph::MARGIN);
80                 bv->update(1);
81         }
82 }
83
84
85 void Lang(BufferView * bv, string const & l)
86 {
87         LyXFont font(LyXFont::ALL_IGNORE);
88         Languages::iterator lit = languages.find(l);
89         if (lit != languages.end()) {
90                 font.setLanguage(&(*lit).second);
91                 ToggleAndShow(bv, font);
92         } else
93                 WriteAlert(_("Error! unknown language"),l);
94 }
95
96
97 void Melt(BufferView * bv)
98 {
99         if (!bv->available()) return;
100         
101         bv->owner()->getMiniBuffer()->Set(_("Melt"));
102         bv->hideCursor();
103         bv->beforeChange();
104         bv->update(-2);
105         bv->text->MeltFootnoteEnvironment();
106         bv->update(1);
107 }
108
109
110 void Tex(BufferView * bv)
111 {
112         LyXFont font(LyXFont::ALL_IGNORE);
113         font.setLatex (LyXFont::TOGGLE);
114         ToggleAndShow(bv, font);
115 }
116
117
118 // Change environment depth.
119 // if decInc >= 0, increment depth
120 // if decInc <  0, decrement depth
121 void changeDepth(BufferView * bv, int decInc)
122 {
123         if (!bv->available()) return;
124         
125         bv->hideCursor();
126         bv->update(-2);
127         if (decInc >= 0)
128                 bv->text->IncDepth();
129         else
130                 bv->text->DecDepth();
131         bv->update(1);
132         bv->owner()->getMiniBuffer()
133                 ->Set(_("Changed environment depth"
134                         " (in possible range, maybe not)"));
135 }
136
137
138 void Free(BufferView * bv)
139 {
140         ToggleAndShow(bv, UserFreeFont());
141 }
142
143
144 // How should this actually work? Should it prohibit input in all BufferViews,
145 // or just in the current one? If "just the current one", then it should be
146 // placed in BufferView. If "all BufferViews" then LyXGUI (I think) should
147 // run "ProhibitInput" on all LyXViews which will run prohibitInput on all
148 // BufferViews. Or is it perhaps just the (input in) BufferViews in the
149 // current LyxView that should be prohibited (Lgb) (This applies to
150 // "AllowInput" as well.)
151 void ProhibitInput(BufferView * bv)
152 {
153         bv->hideCursor();
154
155         static Cursor cursor;
156         static bool cursor_undefined = true;
157    
158         if (cursor_undefined){
159                 cursor = XCreateFontCursor(fl_display, XC_watch);
160                 XFlush(fl_display);
161                 cursor_undefined = false;
162         }
163    
164         /* set the cursor to the watch for all forms and the canvas */ 
165         XDefineCursor(fl_display, bv->owner()->getForm()->window, 
166                       cursor);
167         if (fd_form_paragraph->form_paragraph->visible)
168                 XDefineCursor(fl_display,
169                               fd_form_paragraph->form_paragraph->window,
170                               cursor);
171         if (fd_form_character->form_character->visible)
172                 XDefineCursor(fl_display,
173                               fd_form_character->form_character->window,
174                               cursor);
175
176         XFlush(fl_display);
177         fl_deactivate_all_forms();
178 }
179
180
181 void AllowInput(BufferView * bv)
182 {
183         /* reset the cursor from the watch for all forms and the canvas */
184    
185         XUndefineCursor(fl_display, bv->owner()->getForm()->window);
186         if (fd_form_paragraph->form_paragraph->visible)
187                 XUndefineCursor(fl_display,
188                                 fd_form_paragraph->form_paragraph->window);
189         if (fd_form_character->form_character->visible)
190                 XUndefineCursor(fl_display,
191                                 fd_form_character->form_character->window);
192
193         // What to do about this? (Lgb)
194         if (bv->belowMouse())
195                 SetXtermCursor(bv->owner()->getForm()->window);
196
197         XFlush(fl_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.setGUISize(size);
245         ToggleAndShow(bv, font);
246 }
247
248
249 /* -------> Returns the current font and depth by printing a message. In the
250  * future perhaps we could try to implement a callback to the button-bar.
251  * That is, `light' the bold button when the font is currently bold, etc.
252  */
253 string 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                 Buffer * buffer = bv->buffer();
260                 LyXFont font = bv->text->real_current_font;
261                 LyXFont defaultfont = textclasslist.TextClass(buffer->
262                                                               params.textclass).defaultfont();
263                 font.reduce(defaultfont);
264                 state = _("Font: ") + font.stateText();
265                 // The paragraph depth
266                 int depth = bv->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 (!bv->text->cursor.par->spacing.isDefault()) {
272                         Spacing::Space cur_space =
273                                 bv->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(bv->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 // candidate for move to BufferView
301 /* -------> Does the actual toggle job of the XxxCB() calls above.
302  * Also shows the current font state.
303  */
304 void ToggleAndShow(BufferView * bv, LyXFont const & font)
305 {
306         if (bv->available()) { 
307                 bv->hideCursor();
308                 bv->update(-2);
309                 if (bv->the_locking_inset)
310                         bv->the_locking_inset->SetFont(bv, font, toggleall);
311                 else
312                         bv->text->ToggleFree(font, toggleall);
313                 bv->update(1);
314         }
315 }