]> git.lyx.org Git - lyx.git/blob - src/bufferview_funcs.C
66fc4c0a24cff883d815a59548bd26f2350c8f68
[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_paragraph * fd_form_paragraph;
31 extern FD_form_character * fd_form_character;
32
33 #ifndef NEW_INSETS
34 void Foot(BufferView * bv)
35 {
36         if (!bv->available()) 
37                 return;
38         
39         bv->owner()->getMiniBuffer()
40                 ->Set(_("Inserting Footnote..."));
41         bv->hideCursor();
42         bv->update(BufferView::SELECT|BufferView::FITCUR);
43         bv->text->InsertFootnoteEnvironment(bv, LyXParagraph::FOOTNOTE);
44         bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
45 }
46 #endif
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 #ifndef NEW_INSETS
74 void Margin(BufferView * bv)
75 {
76         if (bv->available()) {
77                 bv->owner()->getMiniBuffer()->Set(_("Inserting margin note..."));
78                 bv->hideCursor();
79                 bv->update(BufferView::SELECT|BufferView::FITCUR);
80                 bv->text->InsertFootnoteEnvironment(bv, LyXParagraph::MARGIN);
81                 bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
82         }
83 }
84 #endif
85
86
87 void Lang(BufferView * bv, string const & l)
88 {
89         LyXFont font(LyXFont::ALL_IGNORE);
90         Languages::iterator lit = languages.find(l);
91         if (lit != languages.end()) {
92                 font.setLanguage(&(*lit).second);
93                 ToggleAndShow(bv, font);
94         } else
95                 WriteAlert(_("Error! unknown language"),l);
96 }
97
98
99 #ifndef NEW_INSETS
100 void Melt(BufferView * bv)
101 {
102         if (!bv->available()) return;
103         
104         bv->owner()->getMiniBuffer()->Set(_("Melt"));
105         bv->hideCursor();
106         bv->beforeChange();
107         bv->update(BufferView::SELECT|BufferView::FITCUR);
108         bv->text->MeltFootnoteEnvironment(bv);
109         bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
110 }
111 #endif
112
113
114 void Tex(BufferView * bv)
115 {
116         LyXFont font(LyXFont::ALL_IGNORE);
117         font.setLatex (LyXFont::TOGGLE);
118         ToggleAndShow(bv, font);
119 }
120
121
122 // Change environment depth.
123 // if decInc >= 0, increment depth
124 // if decInc <  0, decrement depth
125 void changeDepth(BufferView * bv, int decInc)
126 {
127         if (!bv->available()) return;
128         
129         bv->hideCursor();
130         bv->update(BufferView::SELECT|BufferView::FITCUR);
131         if (decInc >= 0)
132                 bv->text->IncDepth(bv);
133         else
134                 bv->text->DecDepth(bv);
135         bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
136         bv->owner()->getMiniBuffer()
137                 ->Set(_("Changed environment depth"
138                         " (in possible range, maybe not)"));
139 }
140
141
142 void Free(BufferView * bv)
143 {
144         ToggleAndShow(bv, UserFreeFont(bv->buffer()->params));
145 }
146
147
148 // How should this actually work? Should it prohibit input in all BufferViews,
149 // or just in the current one? If "just the current one", then it should be
150 // placed in BufferView. If "all BufferViews" then LyXGUI (I think) should
151 // run "ProhibitInput" on all LyXViews which will run prohibitInput on all
152 // BufferViews. Or is it perhaps just the (input in) BufferViews in the
153 // current LyxView that should be prohibited (Lgb) (This applies to
154 // "AllowInput" as well.)
155 void ProhibitInput(BufferView * bv)
156 {
157         bv->hideCursor();
158
159         static Cursor cursor;
160         static bool cursor_undefined = true;
161    
162         if (cursor_undefined){
163                 cursor = XCreateFontCursor(fl_display, XC_watch);
164                 XFlush(fl_display);
165                 cursor_undefined = false;
166         }
167    
168         /* set the cursor to the watch for all forms and the canvas */ 
169         XDefineCursor(fl_display, bv->owner()->getForm()->window, 
170                       cursor);
171         if (fd_form_paragraph->form_paragraph->visible)
172                 XDefineCursor(fl_display,
173                               fd_form_paragraph->form_paragraph->window,
174                               cursor);
175         if (fd_form_character->form_character->visible)
176                 XDefineCursor(fl_display,
177                               fd_form_character->form_character->window,
178                               cursor);
179
180         XFlush(fl_display);
181         fl_deactivate_all_forms();
182 }
183
184
185 void AllowInput(BufferView * bv)
186 {
187         /* reset the cursor from the watch for all forms and the canvas */
188    
189         XUndefineCursor(fl_display, bv->owner()->getForm()->window);
190         if (fd_form_paragraph->form_paragraph->visible)
191                 XUndefineCursor(fl_display,
192                                 fd_form_paragraph->form_paragraph->window);
193         if (fd_form_character->form_character->visible)
194                 XUndefineCursor(fl_display,
195                                 fd_form_character->form_character->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 as a message. 
250 string const CurrentState(BufferView * bv)
251 {
252         string state;
253         if (bv->available()) { 
254                 // I think we should only show changes from the default
255                 // font. (Asger)
256                 LyXText *text = 0;
257
258                 if (bv->the_locking_inset)
259                     text = bv->the_locking_inset->getLyXText(bv);
260                 if (!text)
261                     text = bv->text;
262
263                 Buffer * buffer = bv->buffer();
264                 LyXFont font = text->real_current_font;
265                 LyXFont const & defaultfont =
266                         textclasslist
267                         .TextClass(buffer->params.textclass)
268                         .defaultfont();
269                 font.reduce(defaultfont);
270                 state = _("Font: ") + font.stateText(&buffer->params);
271                 // The paragraph depth
272                 int depth = text->GetDepth();
273                 if (depth > 0) 
274                         state += string(_(", Depth: ")) + tostr(depth);
275                 // The paragraph spacing, but only if different from
276                 // buffer spacing.
277                 if (!text->cursor.par()->spacing.isDefault()) {
278                         Spacing::Space cur_space =
279                                 text->cursor.par()->spacing.getSpace();
280                         state += _(", Spacing: ");
281                         switch (cur_space) {
282                         case Spacing::Single:
283                                 state += _("Single");
284                                 break;
285                         case Spacing::Onehalf:
286                                 state += _("Onehalf");
287                                 break;
288                         case Spacing::Double:
289                                 state += _("Double");
290                                 break;
291                         case Spacing::Other:
292                                 state += _("Other (");
293                                 state += tostr(text->cursor.par()->spacing.getValue());
294                                 state += ")";
295                                 break;
296                         case Spacing::Default:
297                                 // should never happen, do nothing
298                                 break;
299                         }
300                 }
301         }
302         return state;
303 }
304
305
306 /* -------> Does the actual toggle job of the XxxCB() calls above.
307  * Also shows the current font state.
308  */
309 void ToggleAndShow(BufferView * bv, LyXFont const & font)
310 {
311         if (bv->available()) { 
312                 bv->hideCursor();
313                 bv->update(BufferView::SELECT|BufferView::FITCUR);
314                 if (bv->the_locking_inset)
315                         bv->the_locking_inset->SetFont(bv, font, toggleall);
316                 else
317                         bv->text->ToggleFree(bv, font, toggleall);
318                 bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
319
320                 if (font.language() != ignore_language ||
321                     font.latex() != LyXFont::IGNORE) {
322                         LyXText * text = bv->text;
323                         LyXCursor & cursor = text->cursor;
324                         text->ComputeBidiTables(bv->buffer(), cursor.row());
325                         if (cursor.boundary() != 
326                             text->IsBoundary(bv->buffer(), cursor.par(), cursor.pos(),
327                                              text->real_current_font) )
328                                 text->SetCursor(bv, cursor.par(), cursor.pos(),
329                                                 false, !cursor.boundary());
330                 }
331         }
332 }