]> git.lyx.org Git - lyx.git/blob - src/bufferview_funcs.C
small patch from Dekel, begin introducing the real boost framework, get rid of the...
[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_character * fd_form_character;
31
32 #ifndef NEW_INSETS
33 void Foot(BufferView * bv)
34 {
35         if (!bv->available()) 
36                 return;
37         
38         bv->owner()->getMiniBuffer()
39                 ->Set(_("Inserting Footnote..."));
40         bv->hideCursor();
41         bv->update(BufferView::SELECT|BufferView::FITCUR);
42         bv->text->InsertFootnoteEnvironment(bv, LyXParagraph::FOOTNOTE);
43         bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
44 }
45 #endif
46
47
48 void Emph(BufferView * bv)
49 {
50         LyXFont font(LyXFont::ALL_IGNORE);
51         font.setEmph(LyXFont::TOGGLE);
52         ToggleAndShow(bv, font);
53 }
54
55
56 void Bold(BufferView * bv)
57 {
58         LyXFont font(LyXFont::ALL_IGNORE);
59         font.setSeries(LyXFont::BOLD_SERIES);
60         ToggleAndShow(bv, font);
61 }
62
63
64 void Noun(BufferView * bv)
65 {
66         LyXFont font(LyXFont::ALL_IGNORE);
67         font.setNoun(LyXFont::TOGGLE);
68         ToggleAndShow(bv, font);
69 }
70
71
72 #ifndef NEW_INSETS
73 void Margin(BufferView * bv)
74 {
75         if (bv->available()) {
76                 bv->owner()->getMiniBuffer()->Set(_("Inserting margin note..."));
77                 bv->hideCursor();
78                 bv->update(BufferView::SELECT|BufferView::FITCUR);
79                 bv->text->InsertFootnoteEnvironment(bv, LyXParagraph::MARGIN);
80                 bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
81         }
82 }
83 #endif
84
85
86 void Lang(BufferView * bv, string const & l)
87 {
88         LyXFont font(LyXFont::ALL_IGNORE);
89         Languages::iterator lit = languages.find(l);
90         if (lit != languages.end()) {
91                 font.setLanguage(&(*lit).second);
92                 ToggleAndShow(bv, font);
93         } else
94                 WriteAlert(_("Error! unknown language"),l);
95 }
96
97
98 #ifndef NEW_INSETS
99 void Melt(BufferView * bv)
100 {
101         if (!bv->available()) return;
102         
103         bv->owner()->getMiniBuffer()->Set(_("Melt"));
104         bv->hideCursor();
105         bv->beforeChange();
106         bv->update(BufferView::SELECT|BufferView::FITCUR);
107         bv->text->MeltFootnoteEnvironment(bv);
108         bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
109 }
110 #endif
111
112
113 void Tex(BufferView * bv)
114 {
115         LyXFont font(LyXFont::ALL_IGNORE);
116         font.setLatex (LyXFont::TOGGLE);
117         ToggleAndShow(bv, font);
118 }
119
120
121 // Change environment depth.
122 // if decInc >= 0, increment depth
123 // if decInc <  0, decrement depth
124 void changeDepth(BufferView * bv, int decInc)
125 {
126         if (!bv->available()) return;
127         
128         bv->hideCursor();
129         bv->update(BufferView::SELECT|BufferView::FITCUR);
130         if (decInc >= 0)
131                 bv->text->IncDepth(bv);
132         else
133                 bv->text->DecDepth(bv);
134         bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
135         bv->owner()->getMiniBuffer()
136                 ->Set(_("Changed environment depth"
137                         " (in possible range, maybe not)"));
138 }
139
140
141 void Free(BufferView * bv)
142 {
143         ToggleAndShow(bv, UserFreeFont(bv->buffer()->params));
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_display, XC_watch);
163                 XFlush(fl_display);
164                 cursor_undefined = false;
165         }
166    
167         /* set the cursor to the watch for all forms and the canvas */ 
168         XDefineCursor(fl_display, bv->owner()->getForm()->window, 
169                       cursor);
170
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
187         if (fd_form_character->form_character->visible)
188                 XUndefineCursor(fl_display,
189                                 fd_form_character->form_character->window);
190
191         XFlush(fl_display);
192         fl_activate_all_forms();
193 }
194
195
196 void Code(BufferView * bv)
197 {
198         LyXFont font(LyXFont::ALL_IGNORE);
199         font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
200         ToggleAndShow(bv, font);
201 }
202
203
204 void Sans(BufferView * bv)
205 {
206         LyXFont font(LyXFont::ALL_IGNORE);
207         font.setFamily(LyXFont::SANS_FAMILY);
208         ToggleAndShow(bv, font);
209 }
210
211
212 void Roman(BufferView * bv)
213 {
214         LyXFont font(LyXFont::ALL_IGNORE);
215         font.setFamily(LyXFont::ROMAN_FAMILY);
216         ToggleAndShow(bv, font);
217 }
218
219
220 void StyleReset(BufferView * bv)
221 {
222         LyXFont font(LyXFont::ALL_INHERIT, ignore_language);
223         ToggleAndShow(bv, font);
224 }
225
226
227 void Underline(BufferView * bv)
228 {
229         LyXFont font(LyXFont::ALL_IGNORE);
230         font.setUnderbar(LyXFont::TOGGLE);
231         ToggleAndShow(bv, font);
232 }
233
234
235 void FontSize(BufferView * bv, string const & size)
236 {
237         LyXFont font(LyXFont::ALL_IGNORE);
238         font.setGUISize(size);
239         ToggleAndShow(bv, font);
240 }
241
242
243 // Returns the current font and depth as a message. 
244 string const CurrentState(BufferView * bv)
245 {
246         string state;
247         if (bv->available()) { 
248                 // I think we should only show changes from the default
249                 // font. (Asger)
250                 LyXText *text = 0;
251
252                 if (bv->the_locking_inset)
253                     text = bv->the_locking_inset->getLyXText(bv);
254                 if (!text)
255                     text = bv->text;
256
257                 Buffer * buffer = bv->buffer();
258                 LyXFont font = text->real_current_font;
259                 LyXFont const & defaultfont =
260                         textclasslist
261                         .TextClass(buffer->params.textclass)
262                         .defaultfont();
263                 font.reduce(defaultfont);
264                 state = _("Font: ") + font.stateText(&buffer->params);
265                 // The paragraph depth
266                 int depth = 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 (!text->cursor.par()->spacing.isDefault()) {
272                         Spacing::Space cur_space =
273                                 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(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 /* -------> Does the actual toggle job of the XxxCB() calls above.
301  * Also shows the current font state.
302  */
303 void ToggleAndShow(BufferView * bv, LyXFont const & font)
304 {
305         if (bv->available()) { 
306                 bv->hideCursor();
307                 bv->update(BufferView::SELECT|BufferView::FITCUR);
308                 if (bv->the_locking_inset)
309                         bv->the_locking_inset->SetFont(bv, font, toggleall);
310                 else
311                         bv->text->ToggleFree(bv, font, toggleall);
312                 bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
313
314                 if (font.language() != ignore_language ||
315                     font.latex() != LyXFont::IGNORE) {
316                         LyXText * text = bv->text;
317                         LyXCursor & cursor = text->cursor;
318                         text->ComputeBidiTables(bv->buffer(), cursor.row());
319                         if (cursor.boundary() != 
320                             text->IsBoundary(bv->buffer(), cursor.par(), cursor.pos(),
321                                              text->real_current_font) )
322                                 text->SetCursor(bv, cursor.par(), cursor.pos(),
323                                                 false, !cursor.boundary());
324                 }
325         }
326 }