]> git.lyx.org Git - lyx.git/blob - src/Text.h
Forgot to restore this before committing 18494. This fixes bug 3725.
[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                 std::string const & layout);
85         /// Set given layout to current cursor position.
86         /// FIXME: replace Cursor with DocIterator.
87         void setLayout(Cursor & cur, std::string 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 line up.
212         /**
213          * Returns true if an update is needed after the move.
214          */
215         /// FIXME: move to TextMetrics.
216         bool cursorUp(Cursor & cur);
217         /// Move cursor one line down.
218         /**
219          * Returns true if an update is needed after the move.
220          */
221         /// FIXME: move to TextMetrics.
222         bool cursorDown(Cursor & cur);
223         /// Move cursor one position left
224         /**
225          * Returns true if an update is needed after the move.
226          */
227         bool cursorLeft(Cursor & cur);
228         /// Move cursor one position right
229         /**
230          * Returns true if an update is needed after the move.
231          */
232         bool cursorRight(Cursor & cur);
233         ///
234         bool cursorLeftOneWord(Cursor & cur);
235         ///
236         bool cursorRightOneWord(Cursor & cur);
237         ///
238         bool cursorUpParagraph(Cursor & cur);
239         ///
240         bool cursorDownParagraph(Cursor & cur);
241         ///
242         /// FIXME: move to TextMetrics.
243         bool cursorHome(Cursor & cur);
244         ///
245         /// FIXME: move to TextMetrics.
246         bool cursorEnd(Cursor & cur);
247         ///
248         void cursorPrevious(Cursor & cur);
249         ///
250         void cursorNext(Cursor & cur);
251         ///
252         bool cursorTop(Cursor & cur);
253         ///
254         bool cursorBottom(Cursor & cur);
255         /// Erase character at cursor. Honour change tracking
256         /// FIXME: replace Cursor with DocIterator.
257         bool erase(Cursor & cur);
258         /// Delete character before cursor. Honour CT
259         /// FIXME: replace Cursor with DocIterator.
260         bool backspace(Cursor & cur);
261         // Dissolve the inset under cursor
262         /// FIXME: replace Cursor with DocIterator.
263         bool dissolveInset(Cursor & cur);
264         ///
265         bool selectWordWhenUnderCursor(Cursor & cur, word_location);
266         ///
267         enum TextCase {
268                 ///
269                 text_lowercase = 0,
270                 ///
271                 text_capitalization = 1,
272                 ///
273                 text_uppercase = 2
274         };
275         /// Change the case of the word at cursor position.
276         void changeCase(Cursor & cur, TextCase action);
277         /// Transposes the character at the cursor with the one before it
278         void charsTranspose(Cursor & cur);
279
280         /** the DTP switches for paragraphs. LyX will store the top settings
281          always in the first physical paragraph, the bottom settings in the
282          last. When a paragraph is broken, the top settings rest, the bottom
283          settings are given to the new one.
284          */
285         void setParagraph(Cursor & cur,
286                           Spacing const & spacing,
287                           LyXAlignment align,
288                           docstring const & labelwidthstring,
289                           bool noindent);
290
291         /* these things are for search and replace */
292
293         /// needed to insert the selection
294         /// FIXME: replace Cursor with DocIterator.
295         void insertStringAsLines(Cursor & cur, docstring const & str);
296         /// needed to insert the selection
297         /// FIXME: replace Cursor with DocIterator.
298         void insertStringAsParagraphs(Cursor & cur, docstring const & str);
299
300         /// Returns an inset if inset was hit, or 0 if not.
301         /// \warning This method is not recursive! It will return the
302         /// outermost inset within this Text.
303         /// \sa BufferView::getCoveringInset() to get the innermost inset.
304         Inset * checkInsetHit(BufferView &, int x, int y);
305
306         ///
307         /// FIXME: move to TextMetrics.
308         int singleWidth(Buffer const &, Paragraph const & par,
309                 pos_type pos) const;
310         ///
311         /// FIXME: move to TextMetrics.
312         int singleWidth(Paragraph const & par, pos_type pos, char_type c,
313                 Font const & Font) const;
314
315         /// return the color of the canvas
316         Color_color backgroundColor() const;
317
318         /**
319          * Returns the left beginning of the text.
320          * This information cannot be taken from the layout object, because
321          * in LaTeX the beginning of the text fits in some cases
322          * (for example sections) exactly the label-width.
323          */
324         /// FIXME: move to TextMetrics.
325         int leftMargin(Buffer const &, int max_width, pit_type pit, pos_type pos) const;
326         int leftMargin(Buffer const &, int max_width, pit_type pit) const;
327
328         /// access to our paragraphs
329         ParagraphList const & paragraphs() const { return pars_; }
330         ParagraphList & paragraphs() { return pars_; }
331         /// return true if this is the main text
332         bool isMainText(Buffer const &) const;
333
334         /// is this row the last in the text?
335         /// FIXME: move to TextMetrics.
336         bool isLastRow(pit_type pit, Row const & row) const;
337         /// is this row the first in the text?
338         /// FIXME: move to TextMetrics.
339         bool isFirstRow(pit_type pit, Row const & row) const;
340
341         ///
342         double spacing(Buffer const & buffer, Paragraph const & par) const;
343         /// make a suggestion for a label
344         /// FIXME: replace Cursor with DocIterator.
345         docstring getPossibleLabel(Cursor & cur) const;
346         /// is this paragraph right-to-left?
347         bool isRTL(Buffer const &, Paragraph const & par) const;
348         ///
349         bool checkAndActivateInset(Cursor & cur, bool front);
350
351         ///
352         void write(Buffer const & buf, std::ostream & os) const;
353         /// returns whether we've seen our usual 'end' marker
354         bool read(Buffer const & buf, Lexer & lex, ErrorList & errorList);
355
356         ///
357         /// FIXME: move to TextMetrics.
358         int cursorX(BufferView const &, CursorSlice const & cursor,
359                 bool boundary) const;
360         ///
361         /// FIXME: move to TextMetrics.
362         int cursorY(BufferView const & bv, CursorSlice const & cursor,
363                 bool boundary) const;
364
365         /// delete double spaces, leading spaces, and empty paragraphs around old cursor.
366         /// \retval true if a change has happened and we need a redraw.
367         /// FIXME: replace Cursor with DocIterator. This is not possible right
368         /// now because recordUndo() is called which needs a Cursor.
369         static bool deleteEmptyParagraphMechanism(Cursor & cur,
370                 Cursor & old, bool & need_anchor_change);
371
372         /// delete double spaces, leading spaces, and empty paragraphs
373         /// from \first to \last paragraph
374         void deleteEmptyParagraphMechanism(pit_type first, pit_type last, bool trackChanges);
375
376 public:
377         /// the current font settings
378         Font current_font;
379         /// the current font
380         Font real_current_font;
381         ///
382         int background_color_;
383
384         ///
385         mutable Bidi bidi;
386         ///
387         ParagraphList pars_;
388
389         /// our 'outermost' font. This is handed down from the surrounding
390         // inset through the pi/mi parameter (pi.base.font)
391         Font font_;
392
393         ///
394         bool autoBreakRows_;
395 private:
396         /// return past-the-last paragraph influenced by a layout
397         /// change on pit
398         pit_type undoSpan(pit_type pit);
399
400         // fix the cursor `cur' after a characters has been deleted at `where'
401         // position. Called by deleteEmptyParagraphMechanism
402         static void fixCursorAfterDelete(CursorSlice & cur, CursorSlice const & where);
403
404         // At cursor position 0, try to merge the paragraph with the one before it.
405         // Ignore change tracking, i.e., physically remove the end-of-par character
406         bool backspacePos0(Cursor & cur);
407         /// handle the case where bibitems were deleted
408         bool handleBibitems(Cursor & cur);
409
410         ///
411         void deleteWordForward(Cursor & cur);
412         ///
413         void deleteWordBackward(Cursor & cur);
414         ///
415         void deleteLineForward(Cursor & cur);
416         ///
417         void charInserted();
418         /// set 'number' font property
419         void number(Cursor & cur);
420
421         /// paste plain text at current cursor.
422         /// \param str string to paste
423         /// \param asParagraphs whether to paste as paragraphs or as lines
424         void pasteString(Cursor & cur, docstring const & str,
425                         bool asParagraphs);
426 };
427
428 } // namespace lyx
429
430 #endif // LYXTEXT_H