]> git.lyx.org Git - lyx.git/blob - src/lyxtext.h
explain why ownsClipbiard() does not work on Windows and OS X
[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         ///
98         void toggleFree(LCursor & cur, LyXFont const &, bool toggleall = false);
99
100         /// ???
101         /// FIXME: replace LCursor with DocIterator.
102         docstring getStringToIndex(LCursor const & cur);
103
104         /// insert a character at cursor position
105         /// FIXME: replace LCursor with DocIterator.
106         void insertChar(LCursor & cur, char_type c);
107         /// insert an inset at cursor position
108         /// FIXME: replace LCursor with DocIterator.
109         void insertInset(LCursor & cur, InsetBase * inset);
110
111         /// draw text (only used for insets)
112         void draw(PainterInfo & pi, int x, int y) const;
113         /// draw textselection
114         void drawSelection(PainterInfo & pi, int x, int y) const;
115
116         /// try to handle that request
117         /// FIXME: replace LCursor with DocIterator.
118         void dispatch(LCursor & cur, FuncRequest & cmd);
119         /// do we want to handle this event?
120         bool getStatus(LCursor & cur, FuncRequest const & cmd,
121                 FuncStatus & status) const;
122
123         /// read-only access to individual paragraph
124         Paragraph const & getPar(pit_type pit) const { return pars_[pit]; }
125         /// read-write access to individual paragraph
126         Paragraph & getPar(pit_type pit) { return pars_[pit]; }
127         // Returns the current font and depth as a message.
128         /// FIXME: replace LCursor with DocIterator.
129         docstring currentState(LCursor & cur);
130
131         /** returns row near the specified
132           * y-coordinate in given paragraph (relative to the screen).
133           */
134         /// FIXME: move to TextMetrics.
135         Row const & getRowNearY(BufferView const & bv, int y,
136                 pit_type pit) const;
137
138         /// returns the paragraph number closest to screen y-coordinate.
139         /// This method uses the BufferView CoordCache to locate the
140         /// paragraph. The y-coodinate is allowed to be off-screen and
141         /// the CoordCache will be automatically updated if needed. This is
142         /// the reason why we need a non const BufferView.
143         /// FIXME: move to TextMetrics.
144         pit_type getPitNearY(BufferView & bv, int y) const;
145
146         /** Find the word under \c from in the relative location
147          *  defined by \c word_location.
148          *  @param from return here the start of the word
149          *  @param to return here the end of the word
150          */
151         void getWord(CursorSlice & from, CursorSlice & to, word_location const);
152         /// just selects the word the cursor is in
153         void selectWord(LCursor & cur, word_location loc);
154
155         /// what type of change operation to make 
156         enum ChangeOp {
157                 ACCEPT,
158                 REJECT
159         };
160         /// accept or reject the selected change
161         void acceptOrRejectChanges(LCursor & cur, ChangeOp op);
162         /// accept the changes within the complete LyXText
163         void acceptChanges(BufferParams const & bparams);
164         /// reject the changes within the complete LyXText
165         void rejectChanges(BufferParams const & bparams);
166
167         /// returns true if par was empty and was removed
168         bool setCursor(LCursor & cur, pit_type par, pos_type pos,
169                        bool setfont = true, bool boundary = false);
170         ///
171         void setCursor(CursorSlice &, pit_type par, pos_type pos);
172         ///
173         void setCursorIntern(LCursor & cur, pit_type par,
174                  pos_type pos, bool setfont = true, bool boundary = false);
175         ///
176         void setCurrentFont(LCursor & cur);
177
178         ///
179         void recUndo(LCursor & cur, pit_type first, pit_type last) const;
180         ///
181         void recUndo(LCursor & cur, pit_type first) const;
182
183         /// sets cursor only within this LyXText.
184         /// x,y are screen coordinates
185         void setCursorFromCoordinates(LCursor & cur, int x, int y);
186
187         /// sets cursor recursively descending into nested editable insets
188         /**
189         \return the inset pointer if x,y is covering that inset
190         \param x,y are absolute screen coordinates.
191         \retval inset is non-null if the cursor is positionned inside
192         */
193         /// FIXME: move to TextMetrics.
194         /// FIXME: cleanup to use BufferView::getCoveringInset() and
195         /// setCursorFromCoordinates() instead of checkInsetHit().
196         InsetBase * editXY(LCursor & cur, int x, int y);
197         
198         /// Move cursor one line up.
199         /**
200          * Returns true if an update is needed after the move.
201          */
202         /// FIXME: move to TextMetrics.
203         bool cursorUp(LCursor & cur);
204         /// Move cursor one line down.
205         /**
206          * Returns true if an update is needed after the move.
207          */
208         /// FIXME: move to TextMetrics.
209         bool cursorDown(LCursor & cur);
210         /// Move cursor one position left
211         /**
212          * Returns true if an update is needed after the move.
213          */
214         bool cursorLeft(LCursor & cur);
215         /// Move cursor one position right
216         /**
217          * Returns true if an update is needed after the move.
218          */
219         bool cursorRight(LCursor & cur);
220         ///
221         bool cursorLeftOneWord(LCursor & cur);
222         ///
223         bool cursorRightOneWord(LCursor & cur);
224         ///
225         bool cursorUpParagraph(LCursor & cur);
226         ///
227         bool cursorDownParagraph(LCursor & cur);
228         ///
229         /// FIXME: move to TextMetrics.
230         bool cursorHome(LCursor & cur);
231         ///
232         /// FIXME: move to TextMetrics.
233         bool cursorEnd(LCursor & cur);
234         ///
235         void cursorPrevious(LCursor & cur);
236         ///
237         void cursorNext(LCursor & cur);
238         ///
239         bool cursorTop(LCursor & cur);
240         ///
241         bool cursorBottom(LCursor & cur);
242         /// Erase character at cursor. Honour change tracking
243         /// FIXME: replace LCursor with DocIterator.
244         bool erase(LCursor & cur);
245         /// Delete character before cursor. Honour CT
246         /// FIXME: replace LCursor with DocIterator.
247         bool backspace(LCursor & cur);
248         // Dissolve the inset under cursor
249         /// FIXME: replace LCursor with DocIterator.
250         bool dissolveInset(LCursor & cur);
251         ///
252         bool selectWordWhenUnderCursor(LCursor & cur, word_location);
253         ///
254         enum TextCase {
255                 ///
256                 text_lowercase = 0,
257                 ///
258                 text_capitalization = 1,
259                 ///
260                 text_uppercase = 2
261         };
262         /// Change the case of the word at cursor position.
263         void changeCase(LCursor & cur, TextCase action);
264         /// Transposes the character at the cursor with the one before it
265         void charsTranspose(LCursor & cur);
266
267         /** the DTP switches for paragraphs. LyX will store the top settings
268          always in the first physical paragraph, the bottom settings in the
269          last. When a paragraph is broken, the top settings rest, the bottom
270          settings are given to the new one.
271          */
272         void setParagraph(LCursor & cur,
273                           Spacing const & spacing,
274                           LyXAlignment align,
275                           docstring const & labelwidthstring,
276                           bool noindent);
277
278         /* these things are for search and replace */
279
280         /// needed to insert the selection
281         /// FIXME: replace LCursor with DocIterator.
282         void insertStringAsLines(LCursor & cur, docstring const & str);
283         /// needed to insert the selection
284         /// FIXME: replace LCursor with DocIterator.
285         void insertStringAsParagraphs(LCursor & cur, docstring const & str);
286
287         /// Returns an inset if inset was hit, or 0 if not.
288         /// \warning This method is not recursive! It will return the
289         /// outermost inset within this LyXText.
290         /// \sa BufferView::getCoveringInset() to get the innermost inset.
291         InsetBase * checkInsetHit(BufferView &, int x, int y);
292
293         ///
294         /// FIXME: move to TextMetrics.
295         int singleWidth(Buffer const &, Paragraph const & par,
296                 pos_type pos) const;
297         ///
298         /// FIXME: move to TextMetrics.
299         int singleWidth(Paragraph const & par, pos_type pos, char_type c,
300                 LyXFont const & Font) const;
301
302         /// return the color of the canvas
303         LColor_color backgroundColor() const;
304
305         /**
306          * Returns the left beginning of the text.
307          * This information cannot be taken from the layout object, because
308          * in LaTeX the beginning of the text fits in some cases
309          * (for example sections) exactly the label-width.
310          */
311         /// FIXME: move to TextMetrics.
312         int leftMargin(Buffer const &, int max_width, pit_type pit, pos_type pos) const;
313         int leftMargin(Buffer const &, int max_width, pit_type pit) const;
314
315         /// access to our paragraphs
316         ParagraphList const & paragraphs() const { return pars_; }
317         ParagraphList & paragraphs() { return pars_; }
318         /// return true if this is the main text
319         bool isMainText(Buffer const &) const;
320
321         /// is this row the last in the text?
322         /// FIXME: move to TextMetrics.
323         bool isLastRow(pit_type pit, Row const & row) const;
324         /// is this row the first in the text?
325         /// FIXME: move to TextMetrics.
326         bool isFirstRow(pit_type pit, Row const & row) const;
327
328         ///
329         double spacing(Buffer const & buffer, Paragraph const & par) const;
330         /// make a suggestion for a label
331         /// FIXME: replace LCursor with DocIterator.
332         docstring getPossibleLabel(LCursor & cur) const;
333         /// is this paragraph right-to-left?
334         bool isRTL(Buffer const &, Paragraph const & par) const;
335         ///
336         bool checkAndActivateInset(LCursor & cur, bool front);
337
338         ///
339         void write(Buffer const & buf, std::ostream & os) const;
340         /// returns whether we've seen our usual 'end' marker
341         bool read(Buffer const & buf, LyXLex & lex, ErrorList & errorList);
342
343         ///
344         /// FIXME: move to TextMetrics.
345         int cursorX(BufferView const &, CursorSlice const & cursor,
346                 bool boundary) const;
347         ///
348         /// FIXME: move to TextMetrics.
349         int cursorY(BufferView const & bv, CursorSlice const & cursor,
350                 bool boundary) const;
351
352         /// delete double spaces, leading spaces, and empty paragraphs around old cursor.
353         /// \retval true if a change has happened and we need a redraw.
354         /// FIXME: replace LCursor with DocIterator. This is not possible right
355         /// now because recordUndo() is called which needs a LCursor.
356         static bool deleteEmptyParagraphMechanism(LCursor & cur,
357                 LCursor & old, bool & need_anchor_change);
358
359         /// delete double spaces, leading spaces, and empty paragraphs
360         /// from \first to \last paragraph
361         void deleteEmptyParagraphMechanism(pit_type first, pit_type last, bool trackChanges);
362
363 public:
364         /// the current font settings
365         LyXFont current_font;
366         /// the current font
367         LyXFont real_current_font;
368         ///
369         int background_color_;
370
371         ///
372         mutable Bidi bidi;
373         ///
374         ParagraphList pars_;
375
376         /// our 'outermost' font. This is handed down from the surrounding
377         // inset through the pi/mi parameter (pi.base.font)
378         LyXFont font_;
379
380         ///
381         bool autoBreakRows_;
382 private:
383         /// return past-the-last paragraph influenced by a layout
384         /// change on pit
385         pit_type undoSpan(pit_type pit);
386
387         // fix the cursor `cur' after a characters has been deleted at `where'
388         // position. Called by deleteEmptyParagraphMechanism
389         static void fixCursorAfterDelete(CursorSlice & cur, CursorSlice const & where);
390
391         // At cursor position 0, try to merge the paragraph with the one before it.
392         // Ignore change tracking, i.e., physically remove the end-of-par character
393         bool backspacePos0(LCursor & cur);
394
395         ///
396         void deleteWordForward(LCursor & cur);
397         ///
398         void deleteWordBackward(LCursor & cur);
399         ///
400         void deleteLineForward(LCursor & cur);
401         ///
402         void charInserted();
403         /// set 'number' font property
404         void number(LCursor & cur);
405
406         /// paste plain text at current cursor.
407         /// \param str string to paste
408         /// \param asParagraphs whether to paste as paragraphs or as lines
409         void pasteString(LCursor & cur, docstring const & str,
410                         bool asParagraphs);
411 };
412
413 } // namespace lyx
414
415 #endif // LYXTEXT_H