]> git.lyx.org Git - lyx.git/blob - src/bufferview_funcs.C
more cursor dispatch
[lyx.git] / src / bufferview_funcs.C
1 /**
2  * \file bufferview_funcs.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Jean-Marc Lasgouttes
8  * \author John Levon
9  * \author Angus Leeming
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #include <config.h>
15
16 #include "bufferview_funcs.h"
17
18 #include "author.h"
19 #include "buffer.h"
20 #include "bufferparams.h"
21 #include "BufferView.h"
22 #include "cursor.h"
23 #include "gettext.h"
24 #include "language.h"
25 #include "LColor.h"
26 #include "lyxlex.h"
27 #include "lyxrow.h"
28 #include "paragraph.h"
29 #include "ParagraphParameters.h"
30 #include "iterators.h"
31
32 #include "frontends/Alert.h"
33 #include "frontends/LyXView.h"
34
35 #include "insets/insettext.h"
36
37 #include "support/tostr.h"
38 #include "support/std_sstream.h"
39
40 using lyx::support::bformat;
41
42 using std::istringstream;
43 using std::ostringstream;
44 using std::string;
45
46
47 namespace bv_funcs {
48
49 // Set data using font and toggle
50 // If successful, returns true
51 bool font2string(LyXFont const & font, bool toggle, string & data)
52 {
53         string lang = "ignore";
54         if (font.language())
55                 lang = font.language()->lang();
56
57         ostringstream os;
58         os << "family " << font.family() << '\n'
59            << "series " << font.series() << '\n'
60            << "shape " << font.shape() << '\n'
61            << "size " << font.size() << '\n'
62            << "emph " << font.emph() << '\n'
63            << "underbar " << font.underbar() << '\n'
64            << "noun " << font.noun() << '\n'
65            << "number " << font.number() << '\n'
66            << "color " << font.color() << '\n'
67            << "language " << lang << '\n'
68            << "toggleall " << tostr(toggle);
69         data = os.str();
70         return true;
71 }
72
73
74 // Set font and toggle using data
75 // If successful, returns true
76 bool string2font(string const & data, LyXFont & font, bool & toggle)
77 {
78         istringstream is(data);
79         LyXLex lex(0,0);
80         lex.setStream(is);
81
82         int nset = 0;
83         while (lex.isOK()) {
84                 string token;
85                 if (lex.next())
86                         token = lex.getString();
87
88                 if (token.empty() || !lex.next())
89                         break;
90
91                 if (token == "family") {
92                         int const next = lex.getInteger();
93                         font.setFamily(LyXFont::FONT_FAMILY(next));
94
95                 } else if (token == "series") {
96                         int const next = lex.getInteger();
97                         font.setSeries(LyXFont::FONT_SERIES(next));
98
99                 } else if (token == "shape") {
100                         int const next = lex.getInteger();
101                         font.setShape(LyXFont::FONT_SHAPE(next));
102
103                 } else if (token == "size") {
104                         int const next = lex.getInteger();
105                         font.setSize(LyXFont::FONT_SIZE(next));
106
107                 } else if (token == "emph" || token == "underbar" ||
108                            token == "noun" || token == "number") {
109
110                         int const next = lex.getInteger();
111                         LyXFont::FONT_MISC_STATE const misc =
112                                 LyXFont::FONT_MISC_STATE(next);
113
114                         if (token == "emph")
115                             font.setEmph(misc);
116                         else if (token == "underbar")
117                                 font.setUnderbar(misc);
118                         else if (token == "noun")
119                                 font.setNoun(misc);
120                         else if (token == "number")
121                                 font.setNumber(misc);
122
123                 } else if (token == "color") {
124                         int const next = lex.getInteger();
125                         font.setColor(LColor::color(next));
126
127                 } else if (token == "language") {
128                         string const next = lex.getString();
129                         if (next == "ignore")
130                                 font.setLanguage(ignore_language);
131                         else
132                                 font.setLanguage(languages.getLanguage(next));
133
134                 } else if (token == "toggleall") {
135                         toggle = lex.getBool();
136
137                 } else {
138                         // Unrecognised token
139                         break;
140                 }
141
142                 ++nset;
143         }
144         return (nset > 0);
145 }
146
147
148 bool changeDepthAllowed(BufferView * bv, LyXText * text, DEPTH_CHANGE type)
149 {
150         return bv->available() && text && text->changeDepthAllowed(type);
151 }
152
153
154 void changeDepth(BufferView * bv, LyXText * text, DEPTH_CHANGE type)
155 {
156         if (bv->available() && text)
157                 text->changeDepth(type);
158 }
159
160
161 // Returns the current font and depth as a message.
162 string const currentState(BufferView * bv)
163 {
164         ostringstream state;
165
166         if (!bv->available())
167                 return string();
168
169         if (bv->cursor().inMathed()) {
170                 bv->cursor().info(state);
171                 return state.str();
172         }
173
174         LyXText * text = bv->getLyXText();
175         Buffer * buffer = bv->buffer();
176         CursorSlice const & c = text->cursor();
177
178         bool const show_change = buffer->params().tracking_changes
179                 && text->cursor().pos() != text->cursorPar()->size()
180                 && text->cursorPar()->lookupChange(c.pos()) != Change::UNCHANGED;
181
182         if (show_change) {
183                 Change change = text->cursorPar()->lookupChangeFull(c.pos());
184                 Author const & a = bv->buffer()->params().authors().get(change.author);
185                 state << _("Change: ") << a.name();
186                 if (!a.email().empty())
187                         state << " (" << a.email() << ")";
188                 if (change.changetime)
189                         state << _(" at ") << ctime(&change.changetime);
190                 state << " : ";
191         }
192
193         // I think we should only show changes from the default
194         // font. (Asger)
195         LyXFont font = text->real_current_font;
196         font.reduce(buffer->params().getLyXTextClass().defaultfont());
197
198         // avoid _(...) re-entrance problem
199         string const s = font.stateText(&buffer->params());
200         state << bformat(_("Font: %1$s"), s);
201
202         // state << bformat(_("Font: %1$s"), font.stateText(&buffer->params));
203
204         // The paragraph depth
205         int depth = text->getDepth();
206         if (depth > 0)
207                 state << bformat(_(", Depth: %1$s"), tostr(depth));
208
209         // The paragraph spacing, but only if different from
210         // buffer spacing.
211         if (!text->cursorPar()->params().spacing().isDefault()) {
212                 Spacing::Space cur_space =
213                         text->cursorPar()->params().spacing().getSpace();
214                 state << _(", Spacing: ");
215
216                 switch (cur_space) {
217                 case Spacing::Single:
218                         state << _("Single");
219                         break;
220                 case Spacing::Onehalf:
221                         state << _("OneHalf");
222                         break;
223                 case Spacing::Double:
224                         state << _("Double");
225                         break;
226                 case Spacing::Other:
227                         state << _("Other (")
228                               << text->cursorPar()->params().spacing().getValue()
229                               << ')';
230                         break;
231                 case Spacing::Default:
232                         // should never happen, do nothing
233                         break;
234                 }
235         }
236 #ifdef DEVEL_VERSION
237         ParagraphList::iterator pit = text->cursorPar();
238         state << _(", Paragraph: ") << pit->id();
239         state << _(", Position: ") << text->cursor().pos();
240         RowList::iterator rit = pit->getRow(text->cursor().pos());
241         state << bformat(_(", Row b:%1$d e:%2$d"), rit->pos(), rit->endpos());
242         state << _(", Inset: ");
243         InsetOld * inset = pit->inInset();
244         if (inset)
245                 state << inset << " owner: " << inset->owner();
246         else
247                 state << -1;
248 #endif
249         return state.str();
250 }
251
252
253 // deletes a selection during an insertion
254 void replaceSelection(LyXText * text)
255 {
256         if (text->bv()->cursor().selection()) {
257                 text->cutSelection(true, false);
258                 text->bv()->update();
259         }
260 }
261
262 } // namespace bv_funcs