]> git.lyx.org Git - lyx.git/blob - src/bufferview_funcs.C
character2.diff.gz
[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
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()->getMiniBuffer()->Set(_("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()->getMiniBuffer()->Set(_("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()->getMiniBuffer()
143                 ->Set(_("Changed environment depth"
144                         " (in possible range, maybe not)"));
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_get_display(), XC_watch);
164                 XFlush(fl_get_display());
165                 cursor_undefined = false;
166         }
167    
168         /* set the cursor to the watch for all forms and the canvas */ 
169         XDefineCursor(fl_get_display(), bv->owner()->getForm()->window, 
170                       cursor);
171
172         XFlush(fl_get_display());
173         fl_deactivate_all_forms();
174 }
175
176
177 void AllowInput(BufferView * bv)
178 {
179         /* reset the cursor from the watch for all forms and the canvas */
180    
181         XUndefineCursor(fl_get_display(), bv->owner()->getForm()->window);
182
183         XFlush(fl_get_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.setLyXSize(size);
231         ToggleAndShow(bv, font);
232 }
233
234
235 // Returns the current font and depth as a message. 
236 string const 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                 LyXText * text = bv->getLyXText();
243                 Buffer * buffer = bv->buffer();
244                 LyXFont font = text->real_current_font;
245                 LyXFont const & defaultfont =
246                         textclasslist
247                         .TextClass(buffer->params.textclass)
248                         .defaultfont();
249                 font.reduce(defaultfont);
250                 state = _("Font: ") + font.stateText(&buffer->params);
251                 // The paragraph depth
252                 int depth = text->GetDepth();
253                 if (depth > 0) 
254                         state += string(_(", Depth: ")) + tostr(depth);
255                 // The paragraph spacing, but only if different from
256                 // buffer spacing.
257                 if (!text->cursor.par()->spacing.isDefault()) {
258                         Spacing::Space cur_space =
259                                 text->cursor.par()->spacing.getSpace();
260                         state += _(", Spacing: ");
261                         switch (cur_space) {
262                         case Spacing::Single:
263                                 state += _("Single");
264                                 break;
265                         case Spacing::Onehalf:
266                                 state += _("Onehalf");
267                                 break;
268                         case Spacing::Double:
269                                 state += _("Double");
270                                 break;
271                         case Spacing::Other:
272                                 state += _("Other (");
273                                 state += tostr(text->cursor.par()->spacing.getValue());
274                                 state += ")";
275                                 break;
276                         case Spacing::Default:
277                                 // should never happen, do nothing
278                                 break;
279                         }
280                 }
281         }
282         return state;
283 }
284
285
286 /* -------> Does the actual toggle job of the XxxCB() calls above.
287  * Also shows the current font state.
288  */
289 void ToggleAndShow(BufferView * bv, LyXFont const & font, bool toggleall=true)
290 {
291         if (bv->available()) { 
292                 LyXText * text = bv->getLyXText();
293
294                 bv->hideCursor();
295                 bv->update(text, BufferView::SELECT|BufferView::FITCUR);
296                 text->ToggleFree(bv, font, toggleall);
297                 bv->update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
298
299                 if (font.language() != ignore_language ||
300                     font.latex() != LyXFont::IGNORE ||
301                     font.number() != LyXFont::IGNORE) {
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 }