]> git.lyx.org Git - lyx.git/blob - src/bufferview_funcs.C
updateCounters fix + changeDepth simplification
[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 "PosIterator.h"
31 #include "iterators.h"
32
33 #include "frontends/Alert.h"
34 #include "frontends/LyXView.h"
35
36 #include "insets/insettext.h"
37
38 #include "mathed/math_cursor.h"
39
40 #include "support/tostr.h"
41
42 #include "support/std_sstream.h"
43
44 using lyx::support::bformat;
45
46 using std::istringstream;
47 using std::ostringstream;
48 using std::string;
49
50
51 namespace bv_funcs {
52
53 // Set data using font and toggle
54 // If successful, returns true
55 bool font2string(LyXFont const & font, bool toggle, string & data)
56 {
57         string lang = "ignore";
58         if (font.language())
59                 lang = font.language()->lang();
60
61         ostringstream os;
62         os << "family " << font.family() << '\n'
63            << "series " << font.series() << '\n'
64            << "shape " << font.shape() << '\n'
65            << "size " << font.size() << '\n'
66            << "emph " << font.emph() << '\n'
67            << "underbar " << font.underbar() << '\n'
68            << "noun " << font.noun() << '\n'
69            << "number " << font.number() << '\n'
70            << "color " << font.color() << '\n'
71            << "language " << lang << '\n'
72            << "toggleall " << tostr(toggle);
73         data = os.str();
74         return true;
75 }
76
77
78 // Set font and toggle using data
79 // If successful, returns true
80 bool string2font(string const & data, LyXFont & font, bool & toggle)
81 {
82         istringstream is(data);
83         LyXLex lex(0,0);
84         lex.setStream(is);
85
86         int nset = 0;
87         while (lex.isOK()) {
88                 string token;
89                 if (lex.next())
90                         token = lex.getString();
91
92                 if (token.empty() || !lex.next())
93                         break;
94
95                 if (token == "family") {
96                         int const next = lex.getInteger();
97                         font.setFamily(LyXFont::FONT_FAMILY(next));
98
99                 } else if (token == "series") {
100                         int const next = lex.getInteger();
101                         font.setSeries(LyXFont::FONT_SERIES(next));
102
103                 } else if (token == "shape") {
104                         int const next = lex.getInteger();
105                         font.setShape(LyXFont::FONT_SHAPE(next));
106
107                 } else if (token == "size") {
108                         int const next = lex.getInteger();
109                         font.setSize(LyXFont::FONT_SIZE(next));
110
111                 } else if (token == "emph" || token == "underbar" ||
112                            token == "noun" || token == "number") {
113
114                         int const next = lex.getInteger();
115                         LyXFont::FONT_MISC_STATE const misc =
116                                 LyXFont::FONT_MISC_STATE(next);
117
118                         if (token == "emph")
119                             font.setEmph(misc);
120                         else if (token == "underbar")
121                                 font.setUnderbar(misc);
122                         else if (token == "noun")
123                                 font.setNoun(misc);
124                         else if (token == "number")
125                                 font.setNumber(misc);
126
127                 } else if (token == "color") {
128                         int const next = lex.getInteger();
129                         font.setColor(LColor::color(next));
130
131                 } else if (token == "language") {
132                         string const next = lex.getString();
133                         if (next == "ignore")
134                                 font.setLanguage(ignore_language);
135                         else
136                                 font.setLanguage(languages.getLanguage(next));
137
138                 } else if (token == "toggleall") {
139                         toggle = lex.getBool();
140
141                 } else {
142                         // Unrecognised token
143                         break;
144                 }
145
146                 ++nset;
147         }
148         return (nset > 0);
149 }
150
151
152 bool changeDepthAllowed(BufferView * bv, LyXText * text, DEPTH_CHANGE type)
153 {
154         if (!bv->available() || !text)
155                 return false;
156
157         return text->changeDepthAllowed(type);
158 }
159
160
161 void changeDepth(BufferView * bv, LyXText * text, DEPTH_CHANGE type)
162 {
163         if (!bv->available() || !text)
164                 return;
165         text->changeDepth(type);
166 }
167
168
169 // Returns the current font and depth as a message.
170 string const currentState(BufferView * bv)
171 {
172         if (!bv->available())
173                 return string();
174
175         if (mathcursor)
176                 return mathcursor->info();
177
178         ostringstream state;
179
180         LyXText * text = bv->getLyXText();
181         Buffer * buffer = bv->buffer();
182         LyXCursor const & c = text->cursor;
183
184         bool const show_change = buffer->params().tracking_changes
185                 && text->cursor.pos() != text->cursorPar()->size()
186                 && text->cursorPar()->lookupChange(c.pos()) != Change::UNCHANGED;
187
188         if (show_change) {
189                 Change change = text->cursorPar()->lookupChangeFull(c.pos());
190                 Author const & a = bv->buffer()->params().authors().get(change.author);
191                 state << _("Change: ") << a.name();
192                 if (!a.email().empty())
193                         state << " (" << a.email() << ")";
194                 if (change.changetime)
195                         state << _(" at ") << ctime(&change.changetime);
196                 state << " : ";
197         }
198
199         // I think we should only show changes from the default
200         // font. (Asger)
201         LyXFont font = text->real_current_font;
202         font.reduce(buffer->params().getLyXTextClass().defaultfont());
203
204         // avoid _(...) re-entrance problem
205         string const s = font.stateText(&buffer->params());
206         state << bformat(_("Font: %1$s"), s);
207
208         // state << bformat(_("Font: %1$s"), font.stateText(&buffer->params));
209
210         // The paragraph depth
211         int depth = text->getDepth();
212         if (depth > 0)
213                 state << bformat(_(", Depth: %1$s"), tostr(depth));
214
215         // The paragraph spacing, but only if different from
216         // buffer spacing.
217         if (!text->cursorPar()->params().spacing().isDefault()) {
218                 Spacing::Space cur_space =
219                         text->cursorPar()->params().spacing().getSpace();
220                 state << _(", Spacing: ");
221
222                 switch (cur_space) {
223                 case Spacing::Single:
224                         state << _("Single");
225                         break;
226                 case Spacing::Onehalf:
227                         state << _("OneHalf");
228                         break;
229                 case Spacing::Double:
230                         state << _("Double");
231                         break;
232                 case Spacing::Other:
233                         state << _("Other (")
234                               << text->cursorPar()->params().spacing().getValue()
235                               << ')';
236                         break;
237                 case Spacing::Default:
238                         // should never happen, do nothing
239                         break;
240                 }
241         }
242 #ifdef DEVEL_VERSION
243         ParagraphList::iterator pit = text->cursorPar();
244         state << _(", Paragraph: ") << pit->id();
245         state << _(", Position: ") << text->cursor.pos();
246         RowList::iterator rit = pit->getRow(text->cursor.pos());
247         state << bformat(_(", Row b:%1$d e:%2$d"), rit->pos(), rit->endpos());
248         state << _(", Inset: ");
249         InsetOld * inset = pit->inInset();
250         if (inset)
251                 state << inset << " owner: " << inset->owner();
252         else
253                 state << -1;
254 #endif
255         return state.str();
256 }
257
258
259
260 // deletes a selection during an insertion
261 void replaceSelection(LyXText * text)
262 {
263         if (text->selection.set()) {
264                 text->cutSelection(true, false);
265                 text->bv()->update();
266         }
267 }
268
269
270 /*
271 if the fitCursor call refers to some point in never-explored-land, then we
272 don't have y information in insets there, then we cannot even do an update
273 to get it (because we need the y infomation for setting top_y first). So
274 this is solved in put_selection_at with:
275
276 - setting top_y to the y of the outerPar (that has good info)
277 - calling update
278 - calling cursor().updatePos()
279 - then call fitCursor()
280
281 Ab.
282 */
283
284 void put_selection_at(BufferView * bv, PosIterator const & cur,
285                       int length, bool backwards)
286 {
287         ParIterator par(cur);
288         
289         bv->getLyXText()->clearSelection();
290
291         LyXText * text = par.text(bv);
292         par.lockPath(bv);
293         //hack for the chicken and egg problem
294         if (par.inset())
295                 bv->top_y(par.outerPar()->y);
296         bv->update();
297         text->setCursor(cur.pit(), cur.pos());
298         bv->cursor().updatePos();
299
300         if (length) {
301                 text->setSelectionRange(length);
302                 text->setSelection();
303                 if (backwards)
304                         text->cursor = text->selection.start;
305         }
306
307         bv->fitCursor();
308         bv->update();
309 }
310
311
312 }; // namespace bv_funcs