]> git.lyx.org Git - lyx.git/blob - src/Text.h
Fixed some lines that were too long. It compiled afterwards.
[lyx.git] / src / Text.h
1 // -*- C++ -*-
2 /**
3  * \file Text.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 TEXT_H
15 #define TEXT_H
16
17 #include "Bidi.h"
18 #include "DispatchResult.h"
19 #include "Font.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 Inset;
38 class Color_color;
39 class Cursor;
40 class PainterInfo;
41 class Row;
42 class RowMetrics;
43 class Spacing;
44
45
46 /// This class encapsulates the main text data and operations in LyX
47 class Text {
48 public:
49         /// constructor
50         explicit Text();
51
52         ///
53         Font getFont(Buffer const & buffer, Paragraph const & par,
54                 pos_type pos) const;
55         ///
56         void applyOuterFont(Buffer const & buffer, Font &) const;
57         ///
58         Font getLayoutFont(Buffer const & buffer, pit_type pit) const;
59         ///
60         Font getLabelFont(Buffer const & buffer,
61                 Paragraph const & par) const;
62         /** Set font of character at position \p pos in paragraph \p pit.
63          *  Must not be called if \p pos denotes an inset with text contents,
64          *  and the inset is not allowed inside a font change (see below).
65          */
66         void setCharFont(Buffer const & buffer, pit_type pit, pos_type pos,
67                 Font const & font);
68
69         /** Needed to propagate font changes to all text cells of insets
70          *  that are not allowed inside a font change (bug 1973).
71          *  Must not be called if \p pos denotes an ordinary character or an
72          *  inset that is alowed inside a font change.
73          *  FIXME: This should be removed, see documentation of noFontChange
74          *  in insetbase.h
75          */
76         void setInsetFont(Buffer const & buffer, pit_type pit, pos_type pos,
77                 Font const & font, bool toggleall = false);
78
79         /// what you expect when pressing \<enter\> at cursor position
80         void breakParagraph(Cursor & cur, bool keep_layout = false);
81
82         /// set layout over selection
83         void setLayout(Buffer const & buffer, pit_type start, pit_type end,
84                 docstring const & layout);
85         /// Set given layout to current cursor position.
86         /// FIXME: replace Cursor with DocIterator.
87         void setLayout(Cursor & cur, docstring const & layout);
88
89         /// what type of depth change to make
90         enum DEPTH_CHANGE {
91                 INC_DEPTH,
92                 DEC_DEPTH
93         };
94         /// Increase or decrease the nesting depth of the selected paragraph(s)
95         /// FIXME: replace Cursor with DocIterator.
96         void changeDepth(Cursor & cur, DEPTH_CHANGE type);
97
98         /// Returns whether something would be changed by changeDepth
99         /// FIXME: replace Cursor with DocIterator.
100         bool changeDepthAllowed(Cursor & cur, DEPTH_CHANGE type) const;
101
102         /// Set font over selection paragraphs and rebreak.
103         /// FIXME: replace Cursor with DocIterator.
104         void setFont(Cursor & cur, Font const &, bool toggleall = false);
105         /// Set font from \p begin to \p end and rebreak.
106         void setFont(Buffer const & buffer, DocIterator const & begin,
107                 DocIterator const & end, Font const &,
108                 bool toggleall = false);
109
110         ///
111         void toggleFree(Cursor & cur, Font const &, bool toggleall = false);
112
113         /// ???
114         /// FIXME: replace Cursor with DocIterator.
115         docstring getStringToIndex(Cursor const & cur);
116
117         /// insert a character at cursor position
118         /// FIXME: replace Cursor with DocIterator.
119         void insertChar(Cursor & cur, char_type c);
120         /// insert an inset at cursor position
121         /// FIXME: replace Cursor with DocIterator.
122         void insertInset(Cursor & cur, Inset * inset);
123
124         /// draw text (only used for insets)
125         void draw(PainterInfo & pi, int x, int y) const;
126         /// draw textselection
127         void drawSelection(PainterInfo & pi, int x, int y) const;
128
129         /// try to handle that request
130         /// FIXME: replace Cursor with DocIterator.
131         void dispatch(Cursor & cur, FuncRequest & cmd);
132         /// do we want to handle this event?
133         bool getStatus(Cursor & cur, FuncRequest const & cmd,
134                 FuncStatus & status) const;
135
136         /// read-only access to individual paragraph
137         Paragraph const & getPar(pit_type pit) const { return pars_[pit]; }
138         /// read-write access to individual paragraph
139         Paragraph & getPar(pit_type pit) { return pars_[pit]; }
140         // Returns the current font and depth as a message.
141         /// FIXME: replace Cursor with DocIterator.
142         docstring currentState(Cursor & cur);
143
144         /** returns row near the specified
145           * y-coordinate in given paragraph (relative to the screen).
146           */
147         /// FIXME: move to TextMetrics.
148         Row const & getRowNearY(BufferView const & bv, int y,
149                 pit_type pit) const;
150
151         /// returns the paragraph number closest to screen y-coordinate.
152         /// This method uses the BufferView CoordCache to locate the
153         /// paragraph. The y-coodinate is allowed to be off-screen and
154         /// the CoordCache will be automatically updated if needed. This is
155         /// the reason why we need a non const BufferView.
156         /// FIXME: move to TextMetrics.
157         pit_type getPitNearY(BufferView & bv, int y) 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(Cursor & cur, word_location loc);
167
168         /// what type of change operation to make
169         enum ChangeOp {
170                 ACCEPT,
171                 REJECT
172         };
173         /// accept or reject the selected change
174         void acceptOrRejectChanges(Cursor & cur, ChangeOp op);
175         /// accept the changes within the complete Text
176         void acceptChanges(BufferParams const & bparams);
177         /// reject the changes within the complete Text
178         void rejectChanges(BufferParams const & bparams);
179
180         /// returns true if par was empty and was removed
181         bool setCursor(Cursor & cur, pit_type par, pos_type pos,
182                        bool setfont = true, bool boundary = false);
183         ///
184         void setCursor(CursorSlice &, pit_type par, pos_type pos);
185         ///
186         void setCursorIntern(Cursor & cur, pit_type par,
187                  pos_type pos, bool setfont = true, bool boundary = false);
188         ///
189         void setCurrentFont(Cursor & cur);
190
191         ///
192         void recUndo(Cursor & cur, pit_type first, pit_type last) const;
193         ///
194         void recUndo(Cursor & cur, pit_type first) const;
195
196         /// sets cursor only within this Text.
197         /// x,y are screen coordinates
198         void setCursorFromCoordinates(Cursor & cur, int x, int y);
199
200         /// sets cursor recursively descending into nested editable insets
201         /**
202         \return the inset pointer if x,y is covering that inset
203         \param x,y are absolute screen coordinates.
204         \retval inset is non-null if the cursor is positionned inside
205         */
206         /// FIXME: move to TextMetrics.
207         /// FIXME: cleanup to use BufferView::getCoveringInset() and
208         /// setCursorFromCoordinates() instead of checkInsetHit().
209         Inset * editXY(Cursor & cur, int x, int y);
210
211         /// Move cursor one position left
212         /**
213          * Returns true if an update is needed after the move.
214          */
215         bool cursorLeft(Cursor & cur);
216         /// Move cursor one position right
217         /**
218          * Returns true if an update is needed after the move.
219          */
220         bool cursorRight(Cursor & cur);
221         ///
222         bool cursorLeftOneWord(Cursor & cur);
223         ///
224         bool cursorRightOneWord(Cursor & cur);
225         ///
226         bool cursorUpParagraph(Cursor & cur);
227         ///
228         bool cursorDownParagraph(Cursor & cur);
229         ///
230         /// FIXME: move to TextMetrics.
231         bool cursorHome(Cursor & cur);
232         ///
233         /// FIXME: move to TextMetrics.
234         bool cursorEnd(Cursor & cur);
235         ///
236         void cursorPrevious(Cursor & cur);
237         ///
238         void cursorNext(Cursor & cur);
239         ///
240         bool cursorTop(Cursor & cur);
241         ///
242         bool cursorBottom(Cursor & cur);
243         /// Erase character at cursor. Honour change tracking
244         /// FIXME: replace Cursor with DocIterator.
245         bool erase(Cursor & cur);
246         /// Delete character before cursor. Honour CT
247         /// FIXME: replace Cursor with DocIterator.
248         bool backspace(Cursor & cur);
249         // Dissolve the inset under cursor
250         /// FIXME: replace Cursor with DocIterator.
251         bool dissolveInset(Cursor & cur);
252         ///
253         bool selectWordWhenUnderCursor(Cursor & cur, word_location);
254         ///
255         enum TextCase {
256                 ///
257                 text_lowercase = 0,
258                 ///
259                 text_capitalization = 1,
260                 ///
261                 text_uppercase = 2
262         };
263         /// Change the case of the word at cursor position.
264         void changeCase(Cursor & cur, TextCase action);
265         /// Transposes the character at the cursor with the one before it
266         void charsTranspose(Cursor & cur);
267
268         /** the DTP switches for paragraphs. LyX will store the top settings
269          always in the first physical paragraph, the bottom settings in the
270          last. When a paragraph is broken, the top settings rest, the bottom
271          settings are given to the new one.
272          */
273         void setParagraph(Cursor & cur,
274                           Spacing const & spacing,
275                           LyXAlignment align,
276                           docstring const & labelwidthstring,
277                           bool noindent);
278
279         /* these things are for search and replace */
280
281         /// needed to insert the selection
282         /// FIXME: replace Cursor with DocIterator.
283         void insertStringAsLines(Cursor & cur, docstring const & str);
284         /// needed to insert the selection
285         /// FIXME: replace Cursor with DocIterator.
286         void insertStringAsParagraphs(Cursor & cur, docstring const & str);
287
288         /// Returns an inset if inset was hit, or 0 if not.
289         /// \warning This method is not recursive! It will return the
290         /// outermost inset within this Text.
291         /// \sa BufferView::getCoveringInset() to get the innermost inset.
292         Inset * checkInsetHit(BufferView &, int x, int y);
293
294         ///
295         /// FIXME: move to TextMetrics.
296         int singleWidth(Buffer const &, Paragraph const & par,
297                 pos_type pos) const;
298         ///
299         /// FIXME: move to TextMetrics.
300         int singleWidth(Paragraph const & par, pos_type pos, char_type c,
301                 Font const & Font) const;
302
303         /// return the color of the canvas
304         Color_color backgroundColor() const;
305
306         /**
307          * Returns the left beginning of the text.
308          * This information cannot be taken from the layout object, because
309          * in LaTeX the beginning of the text fits in some cases
310          * (for example sections) exactly the label-width.
311          */
312         /// FIXME: move to TextMetrics.
313         int leftMargin(Buffer const &, int max_width, pit_type pit, pos_type pos) const;
314         int leftMargin(Buffer const &, int max_width, pit_type pit) const;
315
316         /// access to our paragraphs
317         ParagraphList const & paragraphs() const { return pars_; }
318         ParagraphList & paragraphs() { return pars_; }
319         /// return true if this is the main text
320         bool isMainText(Buffer const &) const;
321
322         /// is this row the last in the text?
323         /// FIXME: move to TextMetrics.
324         bool isLastRow(pit_type pit, Row const & row) const;
325         /// is this row the first in the text?
326         /// FIXME: move to TextMetrics.
327         bool isFirstRow(pit_type pit, Row const & row) const;
328
329         ///
330         double spacing(Buffer const & buffer, Paragraph const & par) const;
331         /// make a suggestion for a label
332         /// FIXME: replace Cursor with DocIterator.
333         docstring getPossibleLabel(Cursor & cur) const;
334         /// is this paragraph right-to-left?
335         bool isRTL(Buffer const &, Paragraph const & par) const;
336         /// is this position in the paragraph right-to-left?
337         bool isRTL(Buffer const & buffer, CursorSlice const & sl, bool boundary) const;
338         /// is between pos-1 and pos an RTL<->LTR boundary?
339         bool isRTLBoundary(Buffer const & buffer, Paragraph const & par,
340           pos_type pos) const;
341         /// would be a RTL<->LTR boundary between pos and the given font?
342         bool isRTLBoundary(Buffer const & buffer, Paragraph const & par,
343           pos_type pos, Font const & font) const;
344
345         ///
346         bool checkAndActivateInset(Cursor & cur, bool front);
347
348         ///
349         void write(Buffer const & buf, std::ostream & os) const;
350         /// returns whether we've seen our usual 'end' marker
351         bool read(Buffer const & buf, Lexer & lex, ErrorList & errorList);
352
353         ///
354         /// FIXME: move to TextMetrics.
355         int cursorX(BufferView const &, CursorSlice const & cursor,
356                 bool boundary) const;
357         ///
358         /// FIXME: move to TextMetrics.
359         int cursorY(BufferView const & bv, CursorSlice const & cursor,
360                 bool boundary) const;
361
362         /// delete double spaces, leading spaces, and empty paragraphs around old cursor.
363         /// \retval true if a change has happened and we need a redraw.
364         /// FIXME: replace Cursor with DocIterator. This is not possible right
365         /// now because recordUndo() is called which needs a Cursor.
366         static bool deleteEmptyParagraphMechanism(Cursor & cur,
367                 Cursor & old, bool & need_anchor_change);
368
369         /// delete double spaces, leading spaces, and empty paragraphs
370         /// from \first to \last paragraph
371         void deleteEmptyParagraphMechanism(pit_type first, pit_type last, bool trackChanges);
372
373 public:
374         /// the current font settings
375         Font current_font;
376         /// the current font
377         Font real_current_font;
378         ///
379         int background_color_;
380
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         Font 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         static 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(Cursor & cur);
402         /// handle the case where bibitems were deleted
403         bool handleBibitems(Cursor & cur);
404
405         ///
406         void deleteWordForward(Cursor & cur);
407         ///
408         void deleteWordBackward(Cursor & cur);
409         ///
410         void deleteLineForward(Cursor & cur);
411         ///
412         void charInserted();
413         /// set 'number' font property
414         void number(Cursor & cur);
415         /// draw selection for a single row
416         void drawRowSelection(PainterInfo & pi, int x, Row const & row,
417                 DocIterator const & beg, DocIterator const & end, 
418                 bool drawOnBegMargin, bool drawOnEndMargin) const;
419
420         /// paste plain text at current cursor.
421         /// \param str string to paste
422         /// \param asParagraphs whether to paste as paragraphs or as lines
423         void pasteString(Cursor & cur, docstring const & str,
424                         bool asParagraphs);
425 };
426
427 } // namespace lyx
428
429 #endif // LYXTEXT_H