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