]> git.lyx.org Git - lyx.git/blob - src/bufferview_funcs.C
LyX Drinkers United: patch 2
[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-2001 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 "paragraph.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 #include "ParagraphParameters.h"
30
31 void Emph(BufferView * bv)
32 {
33         LyXFont font(LyXFont::ALL_IGNORE);
34         font.setEmph(LyXFont::TOGGLE);
35         ToggleAndShow(bv, font);
36 }
37
38
39 void Bold(BufferView * bv)
40 {
41         LyXFont font(LyXFont::ALL_IGNORE);
42         font.setSeries(LyXFont::BOLD_SERIES);
43         ToggleAndShow(bv, font);
44 }
45
46
47 void Noun(BufferView * bv)
48 {
49         LyXFont font(LyXFont::ALL_IGNORE);
50         font.setNoun(LyXFont::TOGGLE);
51         ToggleAndShow(bv, font);
52 }
53
54
55 void Number(BufferView * bv)
56 {
57         LyXFont font(LyXFont::ALL_IGNORE);
58         font.setNumber(LyXFont::TOGGLE);
59         ToggleAndShow(bv, font);
60 }
61
62 void Lang(BufferView * bv, string const & l)
63 {
64         LyXFont font(LyXFont::ALL_IGNORE);
65         Language const * lang = languages.getLanguage(l);
66         if (lang) {
67                 font.setLanguage(lang);
68                 ToggleAndShow(bv, font);
69         } else
70                 WriteAlert(_("Error! unknown language"),l);
71 }
72
73
74 #ifndef NO_LATEX
75 void Tex(BufferView * bv)
76 {
77         LyXFont font(LyXFont::ALL_IGNORE);
78         font.setLatex (LyXFont::TOGGLE);
79         ToggleAndShow(bv, font);
80 }
81 #endif
82
83
84 // Change environment depth.
85 // if decInc >= 0, increment depth
86 // if decInc <  0, decrement depth
87 void changeDepth(BufferView * bv, LyXText * text, int decInc)
88 {
89         if (!bv->available() || !text)
90             return;
91         
92         bv->hideCursor();
93         bv->update(bv->text, BufferView::SELECT|BufferView::FITCUR);
94         if (decInc >= 0)
95                 text->incDepth(bv);
96         else
97                 text->decDepth(bv);
98         if (text->inset_owner)
99             bv->updateInset((Inset *)text->inset_owner, true);
100         bv->update(bv->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
101         bv->owner()->message(_("Changed environment depth "
102                                "(in possible range, maybe not)"));
103 }
104
105
106 // How should this actually work? Should it prohibit input in all BufferViews,
107 // or just in the current one? If "just the current one", then it should be
108 // placed in BufferView. If "all BufferViews" then LyXGUI (I think) should
109 // run "ProhibitInput" on all LyXViews which will run prohibitInput on all
110 // BufferViews. Or is it perhaps just the (input in) BufferViews in the
111 // current LyxView that should be prohibited (Lgb) (This applies to
112 // "AllowInput" as well.)
113 void ProhibitInput(BufferView * bv)
114 {
115         bv->hideCursor();
116
117         static Cursor cursor;
118         static bool cursor_undefined = true;
119    
120         if (cursor_undefined){
121                 cursor = XCreateFontCursor(fl_get_display(), XC_watch);
122                 XFlush(fl_get_display());
123                 cursor_undefined = false;
124         }
125    
126         /* set the cursor to the watch for all forms and the canvas */ 
127         XDefineCursor(fl_get_display(), bv->owner()->getForm()->window, 
128                       cursor);
129
130         XFlush(fl_get_display());
131         fl_deactivate_all_forms();
132 }
133
134
135 void AllowInput(BufferView * bv)
136 {
137         /* reset the cursor from the watch for all forms and the canvas */
138    
139         XUndefineCursor(fl_get_display(), bv->owner()->getForm()->window);
140
141         XFlush(fl_get_display());
142         fl_activate_all_forms();
143 }
144
145
146 void Code(BufferView * bv)
147 {
148         LyXFont font(LyXFont::ALL_IGNORE);
149         font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
150         ToggleAndShow(bv, font);
151 }
152
153
154 void Sans(BufferView * bv)
155 {
156         LyXFont font(LyXFont::ALL_IGNORE);
157         font.setFamily(LyXFont::SANS_FAMILY);
158         ToggleAndShow(bv, font);
159 }
160
161
162 void Roman(BufferView * bv)
163 {
164         LyXFont font(LyXFont::ALL_IGNORE);
165         font.setFamily(LyXFont::ROMAN_FAMILY);
166         ToggleAndShow(bv, font);
167 }
168
169
170 void StyleReset(BufferView * bv)
171 {
172         LyXFont font(LyXFont::ALL_INHERIT, ignore_language);
173         ToggleAndShow(bv, font);
174 }
175
176
177 void Underline(BufferView * bv)
178 {
179         LyXFont font(LyXFont::ALL_IGNORE);
180         font.setUnderbar(LyXFont::TOGGLE);
181         ToggleAndShow(bv, font);
182 }
183
184
185 void FontSize(BufferView * bv, string const & size)
186 {
187         LyXFont font(LyXFont::ALL_IGNORE);
188         font.setLyXSize(size);
189         ToggleAndShow(bv, font);
190 }
191
192
193 // Returns the current font and depth as a message. 
194 string const CurrentState(BufferView * bv)
195 {
196         ostringstream state;
197
198         if (bv->available()) { 
199                 // I think we should only show changes from the default
200                 // font. (Asger)
201                 LyXText * text = bv->getLyXText();
202                 Buffer * buffer = bv->buffer();
203                 LyXFont font = text->real_current_font;
204                 LyXFont const & defaultfont =
205                         textclasslist
206                         .TextClass(buffer->params.textclass)
207                         .defaultfont();
208                 font.reduce(defaultfont);
209
210                 state << _("Font:") << ' '
211                       << font.stateText(&buffer->params);
212                 
213                 // The paragraph depth
214                 int depth = text->getDepth();
215                 if (depth > 0)
216                         state << _(", Depth: ") << depth;
217                 
218                 // The paragraph spacing, but only if different from
219                 // buffer spacing.
220                 if (!text->cursor.par()->params().spacing().isDefault()) {
221                         Spacing::Space cur_space =
222                                 text->cursor.par()->params().spacing().getSpace();
223                         state << _(", Spacing: ");
224
225                         switch (cur_space) {
226                         case Spacing::Single:
227                                 state << _("Single");
228                                 
229                                 break;
230                         case Spacing::Onehalf:
231                                 state << _("Onehalf");
232                                 break;
233                         case Spacing::Double:
234                                 state << _("Double");
235                                 break;
236                         case Spacing::Other:
237                                 state << _("Other (")
238                                       << text->cursor.par()->params().spacing().getValue()
239                                       << ")";
240                                 break;
241                         case Spacing::Default:
242                                 // should never happen, do nothing
243                                 break;
244                         }
245                 }
246         }
247         return state.str().c_str();
248 }
249
250
251 /* -------> Does the actual toggle job of the XxxCB() calls above.
252  * Also shows the current font state.
253  */
254 void ToggleAndShow(BufferView * bv, LyXFont const & font, bool toggleall)
255 {
256         if (bv->available()) { 
257                 if (bv->theLockingInset()) {
258                         bv->theLockingInset()->SetFont(bv, font, toggleall);
259                         return;
260                 }
261                 LyXText * text = bv->getLyXText();
262                 if (!text)
263                         return;
264
265                 bv->hideCursor();
266                 bv->update(text, BufferView::SELECT|BufferView::FITCUR);
267                 text->toggleFree(bv, font, toggleall);
268                 bv->update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
269
270                 if (font.language() != ignore_language ||
271 #ifndef NO_LATEX
272                     font.latex() != LyXFont::IGNORE ||
273 #endif
274                     font.number() != LyXFont::IGNORE)
275                 {
276                         LyXCursor & cursor = text->cursor;
277                         text->computeBidiTables(bv->buffer(), cursor.row());
278                         if (cursor.boundary() != 
279                             text->isBoundary(bv->buffer(), cursor.par(), cursor.pos(),
280                                              text->real_current_font) )
281                                 text->setCursor(bv, cursor.par(), cursor.pos(),
282                                                 false, !cursor.boundary());
283                 }
284         }
285 }