]> git.lyx.org Git - lyx.git/blob - src/lyxtext.h
Sanitize the DEPM!
[lyx.git] / src / lyxtext.h
1 // -*- C++ -*-
2 /**
3  * \file lyxtext.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author unknown
8  * \author Lars Gullik Bjønnes
9  * \author John Levon
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #ifndef LYXTEXT_H
15 #define LYXTEXT_H
16
17 #include "Bidi.h"
18 #include "dispatchresult.h"
19 #include "lyxfont.h"
20 #include "layout.h"
21 #include "lyxlayout_ptr_fwd.h"
22 #include "ParagraphList.h"
23
24 #include <iosfwd>
25
26
27 namespace lyx {
28
29 class Buffer;
30 class BufferParams;
31 class BufferView;
32 class CursorSlice;
33 class DocIterator;
34 class ErrorList;
35 class FuncRequest;
36 class FuncStatus;
37 class InsetBase;
38 class LColor_color;
39 class LCursor;
40 class LyXTextClass;
41 class PainterInfo;
42 class Row;
43 class RowMetrics;
44 class Spacing;
45
46
47 /// This class encapsulates the main text data and operations in LyX
48 class LyXText {
49 public:
50         /// constructor
51         explicit LyXText();
52
53         ///
54         LyXFont getFont(Buffer const & buffer, Paragraph const & par,
55                 pos_type pos) const;
56         ///
57         void applyOuterFont(Buffer const & buffer, LyXFont &) const;
58         ///
59         LyXFont getLayoutFont(Buffer const & buffer, pit_type pit) const;
60         ///
61         LyXFont getLabelFont(Buffer const & buffer,
62                 Paragraph const & par) const;
63         ///
64         void setCharFont(Buffer const & buffer, pit_type pit, pos_type pos,
65                 LyXFont const & font);
66         ///
67         void setCharFont(Buffer const & buffer, pit_type pit, pos_type pos,
68                 LyXFont const & font, bool toggleall);
69
70         /// what you expect when pressing <enter> at cursor position
71         void breakParagraph(LCursor & cur, bool keep_layout = false);
72
73         /// set layout over selection
74         void setLayout(Buffer const & buffer, pit_type start, pit_type end,
75                 std::string const & layout);
76         /// Set given layout to current cursor position.
77         /// FIXME: replace LCursor with DocIterator.
78         void setLayout(LCursor & cur, std::string const & layout);
79
80         /// what type of depth change to make
81         enum DEPTH_CHANGE {
82                 INC_DEPTH,
83                 DEC_DEPTH
84         };
85         /// Increase or decrease the nesting depth of the selected paragraph(s)
86         /// FIXME: replace LCursor with DocIterator.
87         void changeDepth(LCursor & cur, DEPTH_CHANGE type);
88
89         /// Returns whether something would be changed by changeDepth
90         /// FIXME: replace LCursor with DocIterator.
91         bool changeDepthAllowed(LCursor & cur, DEPTH_CHANGE type) const;
92
93         /// Set font over selection paragraphs and rebreak.
94         /// FIXME: replace LCursor with DocIterator.
95         void setFont(LCursor & cur, LyXFont const &, bool toggleall = false);
96
97         /// returns pos in given par at given x coord.
98         /// FIXME: move to TextMetrics.
99         pos_type x2pos(BufferView const &, pit_type pit, int row, int x) const;
100         int pos2x(pit_type pit, pos_type pos) const;
101
102         ///
103         void toggleFree(LCursor & cur, LyXFont const &, bool toggleall = false);
104
105         /// ???
106         /// FIXME: replace LCursor with DocIterator.
107         docstring getStringToIndex(LCursor const & cur);
108
109         /// insert a character at cursor position
110         /// FIXME: replace LCursor with DocIterator.
111         void insertChar(LCursor & cur, char_type c);
112         /// insert an inset at cursor position
113         /// FIXME: replace LCursor with DocIterator.
114         void insertInset(LCursor & cur, InsetBase * inset);
115
116         /// draw text (only used for insets)
117         void draw(PainterInfo & pi, int x, int y) const;
118         /// draw textselection
119         void drawSelection(PainterInfo & pi, int x, int y) const;
120
121         /// try to handle that request
122         /// FIXME: replace LCursor with DocIterator.
123         void dispatch(LCursor & cur, FuncRequest & cmd);
124         /// do we want to handle this event?
125         bool getStatus(LCursor & cur, FuncRequest const & cmd,
126                 FuncStatus & status) const;
127
128         /// read-only access to individual paragraph
129         Paragraph const & getPar(pit_type pit) const { return pars_[pit]; }
130         /// read-write access to individual paragraph
131         Paragraph & getPar(pit_type pit) { return pars_[pit]; }
132         // Returns the current font and depth as a message.
133         /// FIXME: replace LCursor with DocIterator.
134         docstring currentState(LCursor & cur);
135
136         /** returns row near the specified
137           * y-coordinate in given paragraph (relative to the screen).
138           */
139         /// FIXME: move to TextMetrics.
140         Row const & getRowNearY(BufferView const & bv, int y,
141                 pit_type pit) const;
142
143         /// returns the paragraph number closest to screen y-coordinate.
144         /// This method uses the BufferView CoordCache to locate the
145         /// paragraph. The y-coodinate is allowed to be off-screen and
146         /// the CoordCache will be automatically updated if needed. This is
147         /// the reason why we need a non const BufferView and why this
148         /// method is non-const.
149         /// FIXME: move to TextMetrics.
150         pit_type getPitNearY(BufferView & bv, int y);
151
152         /** returns the column near the specified x-coordinate of the row
153          x is set to the real beginning of this column
154          */
155         /// FIXME: move to TextMetrics.
156         pos_type getColumnNearX(BufferView const & bv, int right_margin,
157                 pit_type pit, Row const & row, int & x, bool & boundary) const;
158
159         /** Find the word under \c from in the relative location
160          *  defined by \c word_location.
161          *  @param from return here the start of the word
162          *  @param to return here the end of the word
163          */
164         void getWord(CursorSlice & from, CursorSlice & to, word_location const);
165         /// just selects the word the cursor is in
166         void selectWord(LCursor & cur, word_location loc);
167
168         /// accept selected change
169         void acceptChange(LCursor & cur);
170         /// reject selected change
171         void rejectChange(LCursor & cur);
172
173         /// returns true if par was empty and was removed
174         bool setCursor(LCursor & cur, pit_type par, pos_type pos,
175                        bool setfont = true, bool boundary = false);
176         ///
177         void setCursor(CursorSlice &, pit_type par, pos_type pos);
178         ///
179         void setCursorIntern(LCursor & cur, pit_type par,
180                  pos_type pos, bool setfont = true, bool boundary = false);
181         ///
182         void setCurrentFont(LCursor & cur);
183
184         ///
185         void recUndo(LCursor & cur, pit_type first, pit_type last) const;
186         ///
187         void recUndo(LCursor & cur, pit_type first) const;
188         /// returns true if par was empty and was removed
189         bool setCursorFromCoordinates(LCursor & cur, int x, int y);
190
191         /// sets cursor recursively descending into nested editable insets
192         /**
193         \return the inset pointer if x,y is covering that inset
194         \param x,y are absolute screen coordinates.
195         \retval inset is non-null if the cursor is positionned inside
196         */
197         /// FIXME: move to TextMetrics.
198         InsetBase * editXY(LCursor & cur, int x, int y);
199         
200         /// Move cursor one line up.
201         /**
202          * Returns true if an update is needed after the move.
203          */
204         /// FIXME: move to TextMetrics.
205         bool cursorUp(LCursor & cur);
206         /// Move cursor one line down.
207         /**
208          * Returns true if an update is needed after the move.
209          */
210         /// FIXME: move to TextMetrics.
211         bool cursorDown(LCursor & cur);
212         /// Move cursor one position left
213         /**
214          * Returns true if an update is needed after the move.
215          */
216         bool cursorLeft(LCursor & cur);
217         /// Move cursor one position right
218         /**
219          * Returns true if an update is needed after the move.
220          */
221         bool cursorRight(LCursor & cur);
222         ///
223         bool cursorLeftOneWord(LCursor & cur);
224         ///
225         bool cursorRightOneWord(LCursor & cur);
226         ///
227         bool cursorUpParagraph(LCursor & cur);
228         ///
229         bool cursorDownParagraph(LCursor & cur);
230         ///
231         /// FIXME: move to TextMetrics.
232         bool cursorHome(LCursor & cur);
233         ///
234         /// FIXME: move to TextMetrics.
235         bool cursorEnd(LCursor & cur);
236         ///
237         void cursorPrevious(LCursor & cur);
238         ///
239         void cursorNext(LCursor & cur);
240         ///
241         bool cursorTop(LCursor & cur);
242         ///
243         bool cursorBottom(LCursor & cur);
244         /// Erase character at cursor. Honour change tracking
245         /// FIXME: replace LCursor with DocIterator.
246         bool erase(LCursor & cur);
247         /// Delete character before cursor. Honour CT
248         /// FIXME: replace LCursor with DocIterator.
249         bool backspace(LCursor & cur);
250         // Dissolve the inset under cursor
251         /// FIXME: replace LCursor with DocIterator.
252         bool dissolveInset(LCursor & cur);
253         ///
254         bool selectWordWhenUnderCursor(LCursor & cur, word_location);
255         ///
256         enum TextCase {
257                 ///
258                 text_lowercase = 0,
259                 ///
260                 text_capitalization = 1,
261                 ///
262                 text_uppercase = 2
263         };
264         /// Change the case of the word at cursor position.
265         void changeCase(LCursor & cur, TextCase action);
266         /// Transposes the character at the cursor with the one before it
267         void charsTranspose(LCursor & cur);
268
269         /** the DTP switches for paragraphs. LyX will store the top settings
270          always in the first physical paragraph, the bottom settings in the
271          last. When a paragraph is broken, the top settings rest, the bottom
272          settings are given to the new one.
273          */
274         void setParagraph(LCursor & cur,
275                           Spacing const & spacing,
276                           LyXAlignment align,
277                           docstring const & labelwidthstring,
278                           bool noindent);
279
280         /* these things are for search and replace */
281
282         /// needed to insert the selection
283         /// FIXME: replace LCursor with DocIterator.
284         void insertStringAsLines(LCursor & cur, docstring const & str);
285         /// needed to insert the selection
286         /// FIXME: replace LCursor with DocIterator.
287         void insertStringAsParagraphs(LCursor & cur, docstring const & str);
288
289         /// Returns an inset if inset was hit, or 0 if not.
290         InsetBase * checkInsetHit(BufferView &, int x, int y);
291
292         ///
293         /// FIXME: move to TextMetrics.
294         int singleWidth(Buffer const &, Paragraph const & par,
295                 pos_type pos) const;
296         ///
297         /// FIXME: move to TextMetrics.
298         int singleWidth(Paragraph const & par, pos_type pos, char_type c,
299                 LyXFont const & Font) const;
300
301         /// return the color of the canvas
302         LColor_color backgroundColor() const;
303
304         /**
305          * Returns the left beginning of the text.
306          * This information cannot be taken from the layout object, because
307          * in LaTeX the beginning of the text fits in some cases
308          * (for example sections) exactly the label-width.
309          */
310         /// FIXME: move to TextMetrics.
311         int leftMargin(Buffer const &, int max_width, pit_type pit, pos_type pos) const;
312         int leftMargin(Buffer const &, int max_width, pit_type pit) const;
313
314         /// access to our paragraphs
315         ParagraphList const & paragraphs() const { return pars_; }
316         ParagraphList & paragraphs() { return pars_; }
317         /// return true if this is the main text
318         bool isMainText(Buffer const &) const;
319
320         /// is this row the last in the text?
321         /// FIXME: move to TextMetrics.
322         bool isLastRow(pit_type pit, Row const & row) const;
323         /// is this row the first in the text?
324         /// FIXME: move to TextMetrics.
325         bool isFirstRow(pit_type pit, Row const & row) const;
326
327         ///
328         double spacing(Buffer const & buffer, Paragraph const & par) const;
329         /// make a suggestion for a label
330         /// FIXME: replace LCursor with DocIterator.
331         docstring getPossibleLabel(LCursor & cur) const;
332         /// is this paragraph right-to-left?
333         bool isRTL(Buffer const &, Paragraph const & par) const;
334         ///
335         bool checkAndActivateInset(LCursor & cur, bool front);
336
337         ///
338         void write(Buffer const & buf, std::ostream & os) const;
339         /// returns whether we've seen our usual 'end' marker
340         bool read(Buffer const & buf, LyXLex & lex, ErrorList & errorList);
341
342         ///
343         /// FIXME: move to TextMetrics.
344         int cursorX(BufferView const &, CursorSlice const & cursor,
345                 bool boundary) const;
346         ///
347         /// FIXME: move to TextMetrics.
348         int cursorY(BufferView const & bv, CursorSlice const & cursor,
349                 bool boundary) const;
350
351         /// delete double space or empty paragraphs around old cursor
352         /// FIXME: replace LCursor with DocIterator. This is not possible right
353         /// now because recordUndo() is called which needs a LCursor.
354         bool deleteEmptyParagraphMechanism(LCursor & cur,
355                 LCursor & old, bool & need_anchor_change);
356
357         /// sets row.end to the pos value *after* which a row should break.
358         /// for example, the pos after which isNewLine(pos) == true
359         /// FIXME: move to TextMetrics.
360         void rowBreakPoint(Buffer const &, int right_margin, int max_width, pit_type pit,
361                 Row & row) const;
362         /// sets row.width to the minimum space a row needs on the screen in pixel
363         /// FIXME: move to TextMetrics.
364         void setRowWidth(Buffer const &, int right_margin, int max_width, pit_type pit,
365                 Row & row) const;
366
367         /// Calculate and set the height of the row
368         /// FIXME: move to TextMetrics.
369         void setHeightOfRow(BufferView const &, pit_type, Row & row);
370
371 public:
372         /// the current font settings
373         LyXFont current_font;
374         /// the current font
375         LyXFont real_current_font;
376         ///
377         int background_color_;
378
379         ///
380         mutable Bidi bidi;
381         ///
382         ParagraphList pars_;
383
384         /// our 'outermost' font. This is handed down from the surrounding
385         // inset through the pi/mi parameter (pi.base.font)
386         LyXFont font_;
387
388         ///
389         bool autoBreakRows_;
390 private:
391         /// return past-the-last paragraph influenced by a layout
392         /// change on pit
393         pit_type undoSpan(pit_type pit);
394
395         // fix the cursor `cur' after a characters has been deleted at `where'
396         // position. Called by deleteEmptyParagraphMechanism
397         void fixCursorAfterDelete(CursorSlice & cur, CursorSlice const & where);
398
399         // At cursor position 0, try to merge the paragraph with the one before it.
400         // Ignore change tracking, i.e., physically remove the end-of-par character
401         bool backspacePos0(LCursor & cur);
402
403         ///
404         void deleteWordForward(LCursor & cur);
405         ///
406         void deleteWordBackward(LCursor & cur);
407         ///
408         void deleteLineForward(LCursor & cur);
409
410         /// FIXME
411         int labelEnd(Buffer const &, int max_width, pit_type pit) const;
412
413         ///
414         void charInserted();
415         /// set 'number' font property
416         void number(LCursor & cur);
417 };
418
419 } // namespace lyx
420
421 #endif // LYXTEXT_H