]> git.lyx.org Git - lyx.git/blob - src/lyxtext.h
redocursor removal
[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 "bufferview_funcs.h"
18 #include "Bidi.h"
19 #include "layout.h"
20 #include "lyxfont.h"
21 #include "lyxtextclass.h"
22 #include "ParagraphList_fwd.h"
23 #include "RowList_fwd.h"
24 #include "textcursor.h"
25
26 #include "insets/inset.h"
27
28 #include <iosfwd>
29
30 class Buffer;
31 class BufferParams;
32 class BufferView;
33 class Dimension;
34 class LColor_color;
35 class LyXCursor;
36 class MetricsInfo;
37 class Paragraph;
38 class Row;
39 class Spacing;
40 class UpdatableInset;
41 class VSpace;
42
43
44 /**
45   This class used to hold the mapping between buffer paragraphs and
46         screen rows. Nowadays, the Paragraphs take care of their rows
47   themselves and this contains just most of the code for manipulating
48   them and interaction with the Cursor.
49   */
50
51 // The inheritance from TextCursor should go. It's just there to ease
52 // transition...
53 class LyXText : public TextCursor {
54         // Public Functions
55 public:
56         /// Constructor
57         LyXText(BufferView *, bool ininset);
58         ///
59         void init(BufferView *);
60
61
62         /// update y coordinate cache of all paragraphs
63         void updateParPositions();
64         ///
65         LyXFont getFont(ParagraphList::iterator pit, lyx::pos_type pos) const;
66         ///
67         LyXFont getLayoutFont(ParagraphList::iterator pit) const;
68         ///
69         LyXFont getLabelFont(ParagraphList::iterator pit) const;
70         ///
71         void setCharFont(ParagraphList::iterator pit,
72                          lyx::pos_type pos, LyXFont const & font);
73         void setCharFont(ParagraphList::iterator pit,
74                          lyx::pos_type pos,
75                          LyXFont const & font, bool toggleall);
76
77         /// what you expect when pressing <enter> at cursor position
78         void breakParagraph(ParagraphList & paragraphs, char keep_layout = 0);
79
80         /** set layout over selection and make a total rebreak of
81           those paragraphs
82           */
83         ParagraphList::iterator
84         setLayout(ParagraphList::iterator start,
85                   ParagraphList::iterator end,
86                   std::string const & layout);
87         ///
88         void setLayout(std::string const & layout);
89
90         /// Increase or decrease the nesting depth of the selected paragraph(s)
91         void changeDepth(bv_funcs::DEPTH_CHANGE type);
92
93         /// Returns whether something would be changed by changeDepth
94         bool changeDepthAllowed(bv_funcs::DEPTH_CHANGE type);
95
96         /// get the depth at current cursor position
97         int getDepth() const;
98
99         /** set font over selection and make a total rebreak of those
100           paragraphs.
101           toggleall defaults to false.
102           */
103         void setFont(LyXFont const &, bool toggleall = false);
104
105         /// rebreaks all paragaphs between the given pars.
106         void redoParagraphs(ParagraphList::iterator begin,
107                             ParagraphList::iterator end);
108         /// rebreaks the given par
109         void redoParagraph(ParagraphList::iterator pit);
110
111         /// rebreaks the cursor par
112         void redoParagraph();
113
114         ///
115         void toggleFree(LyXFont const &, bool toggleall = false);
116
117         ///
118         std::string getStringToIndex();
119
120         /** insert a character, moves all the following breaks in the
121           same Paragraph one to the right and make a little rebreak
122           */
123         void insertChar(char c);
124         ///
125         void insertInset(InsetOld * inset);
126
127         /// a full rebreak of the whole text
128         void fullRebreak();
129         /// compute text metrics
130         void metrics(MetricsInfo & mi, Dimension & dim);
131         /// draw text (only used for insets)
132         void draw(PainterInfo & pi, int x, int y) const;
133
134         /// try to handle that request
135         DispatchResult dispatch(FuncRequest const & cmd);
136
137         BufferView * bv();
138
139         BufferView * bv() const;
140
141         friend class LyXScreen;
142
143         /// returns an iterator pointing to a cursor paragraph
144         ParagraphList::iterator getPar(LyXCursor const & cursor) const;
145         ///
146         ParagraphList::iterator getPar(lyx::paroffset_type par) const;
147         ///
148         int parOffset(ParagraphList::iterator pit) const;
149         /// convenience
150         ParagraphList::iterator cursorPar() const;
151         ///
152         RowList::iterator cursorRow() const;
153
154         /** returns an iterator pointing to the row near the specified
155           * y-coordinate (relative to the whole text). y is set to the
156           * real beginning of this row
157           */
158         RowList::iterator getRowNearY(int y,
159                 ParagraphList::iterator & pit) const;
160
161         /** returns the column near the specified x-coordinate of the row
162          x is set to the real beginning of this column
163          */
164         lyx::pos_type getColumnNearX(ParagraphList::iterator pit,
165                 Row const & row, int & x, bool & boundary) const;
166
167         /// need the selection cursor:
168         void setSelection();
169         ///
170         void clearSelection();
171
172         /// select the word we need depending on word_location
173         void getWord(LyXCursor & from, LyXCursor & to, lyx::word_location const);
174         /// just selects the word the cursor is in
175         void selectWord(lyx::word_location loc);
176         /// returns the inset at cursor (if it exists), 0 otherwise
177         InsetOld * getInset() const;
178
179         /// accept selected change
180         void acceptChange();
181
182         /// reject selected change
183         void rejectChange();
184
185         ///
186         void setCursor(ParagraphList::iterator pit, lyx::pos_type pos);
187         /// returns true if par was empty and was removed
188         bool setCursor(lyx::paroffset_type par, lyx::pos_type pos,
189                        bool setfont = true, bool boundary = false);
190         ///
191         void setCursor(LyXCursor &, lyx::paroffset_type par,
192                        lyx::pos_type pos, bool boundary = false);
193         ///
194         void setCursorIntern(lyx::paroffset_type par, lyx::pos_type pos,
195                              bool setfont = true, bool boundary = false);
196         ///
197         void setCurrentFont();
198
199         ///
200         void recUndo(lyx::paroffset_type first, lyx::paroffset_type last) const;
201         ///
202         void recUndo(lyx::paroffset_type first) const;
203         ///
204         void setCursorFromCoordinates(int x, int y);
205         ///
206         void setCursorFromCoordinates(LyXCursor &, int x, int y);
207         ///
208         void cursorUp(bool selecting = false);
209         ///
210         void cursorDown(bool selecting = false);
211         ///
212         bool cursorLeft(bool internal = true);
213         ///
214         bool cursorRight(bool internal = true);
215         ///
216         void cursorLeftOneWord();
217         ///
218         void cursorRightOneWord();
219         ///
220         void cursorUpParagraph();
221         ///
222         void cursorDownParagraph();
223         ///
224         void cursorHome();
225         ///
226         void cursorEnd();
227         ///
228         void cursorPrevious();
229         ///
230         void cursorNext();
231         ///
232         void cursorTop();
233         ///
234         void cursorBottom();
235         ///
236         void Delete();
237         ///
238         void backspace();
239         ///
240         bool selectWordWhenUnderCursor(lyx::word_location);
241         ///
242         enum TextCase {
243                 ///
244                 text_lowercase = 0,
245                 ///
246                 text_capitalization = 1,
247                 ///
248                 text_uppercase = 2
249         };
250         /// Change the case of the word at cursor position.
251         void changeCase(TextCase action);
252
253         /// returns success
254         bool toggleInset();
255         ///
256         void cutSelection(bool doclear = true, bool realcut = true);
257         ///
258         void copySelection();
259         ///
260         void pasteSelection(size_t sel_index = 0);
261
262         /** the DTP switches for paragraphs. LyX will store the top settings
263          always in the first physical paragraph, the bottom settings in the
264          last. When a paragraph is broken, the top settings rest, the bottom
265          settings are given to the new one.
266          */
267         void setParagraph(
268                           Spacing const & spacing,
269                           LyXAlignment align,
270                           std::string const & labelwidthstring,
271                           bool noindent);
272
273         /* these things are for search and replace */
274
275         /**
276          * Sets the selection from the current cursor position to length
277          * characters to the right. No safety checks.
278          */
279         void setSelectionRange(lyx::pos_type length);
280
281         /** simple replacing. The font of the first selected character
282           is used
283           */
284         void replaceSelectionWithString(std::string const & str);
285
286         /// needed to insert the selection
287         void insertStringAsLines(std::string const & str);
288         /// needed to insert the selection
289         void insertStringAsParagraphs(std::string const & str);
290
291         /// Find next inset of some specified type.
292         bool gotoNextInset(std::vector<InsetOld::Code> const & codes,
293                            std::string const & contents = std::string());
294         ///
295         void gotoInset(std::vector<InsetOld::Code> const & codes,
296                        bool same_content);
297         ///
298         void gotoInset(InsetOld::Code code, bool same_content);
299
300         /// current max text width
301         int textWidth() const;
302
303         /// updates all counters
304         void updateCounters();
305         /// Returns an inset if inset was hit, or 0 if not.
306         InsetOld * checkInsetHit(int x, int y);
307
308         ///
309         int singleWidth(ParagraphList::iterator pit, lyx::pos_type pos) const;
310         ///
311         int singleWidth(ParagraphList::iterator pit,
312                 lyx::pos_type pos, char c, LyXFont const & Font) const;
313
314         /// return the color of the canvas
315         LColor_color backgroundColor() const;
316
317         /**
318          * Returns the left beginning of the text.
319          * This information cannot be taken from the layout object, because
320          * in LaTeX the beginning of the text fits in some cases
321          * (for example sections) exactly the label-width.
322          */
323         int leftMargin(ParagraphList::iterator pit, lyx::pos_type pos) const;
324         int leftMargin(ParagraphList::iterator pit) const;
325         ///
326         int rightMargin(Paragraph const & par) const;
327
328         /** this calculates the specified parameters. needed when setting
329          * the cursor and when creating a visible row */
330         void prepareToPrint(ParagraphList::iterator pit, Row & row) const;
331
332         //
333         // special owner functions
334         ///
335         ParagraphList & paragraphs() const;
336
337         /// return true if this is owned by an inset.
338         bool isInInset() const;
339
340         ///
341         ParagraphList::iterator firstPar() const;
342         ///
343         ParagraphList::iterator lastPar() const;
344         ///
345         ParagraphList::iterator endPar() const;
346         
347         /// return first row of text
348         RowList::iterator firstRow() const;
349         /// return last row of text
350         RowList::iterator lastRow() const;
351         /// return row "behind" last row of text
352         RowList::iterator endRow() const;
353         /// return next row crossing paragraph boundaries
354         void nextRow(ParagraphList::iterator & pit,
355                 RowList::iterator & rit) const;
356         /// return previous row crossing paragraph boundaries
357         void previousRow(ParagraphList::iterator & pit,
358                 RowList::iterator & rit) const;
359
360         /// is this row the last in the text?
361         bool isLastRow(ParagraphList::iterator pit, Row const & row) const;
362         /// is this row the first in the text?
363         bool isFirstRow(ParagraphList::iterator pit, Row const & row) const;
364
365         ///
366         std::string selectionAsString(Buffer const & buffer, bool label) const;
367         ///
368         double spacing(Paragraph const &) const;
369         ///
370         void cursorLeftOneWord(LyXCursor &);
371         ///
372         void cursorRightOneWord(LyXCursor &);
373
374         ///
375         DispatchResult moveRight();
376         ///
377         DispatchResult moveLeft();
378         ///
379         DispatchResult moveRightIntern(bool front,
380                 bool activate_inset, bool selecting);
381         ///
382         DispatchResult moveLeftIntern(bool front,
383                 bool activate_inset, bool selecting);
384         ///
385         DispatchResult moveUp();
386         ///
387         DispatchResult moveDown();
388         ///
389         bool checkAndActivateInset(bool front);
390
391         ///
392         void write(Buffer const & buf, std::ostream & os) const;
393         /// returns whether we've seen our usual 'end' marker
394         bool read(Buffer const & buf, LyXLex & lex);
395
396         ///
397         int ascent() const;
398         ///
399         int descent() const;
400         ///
401         int cursorX() const;
402         ///
403         int cursorY() const;
404         ///
405         int cursorX(LyXCursor const & cursor) const;
406         ///
407         int cursorY(LyXCursor const & cursor) const;
408
409 public:
410         ///
411         int height;
412         ///
413         unsigned int width;
414         ///
415         int textwidth_;
416         /// the current font settings
417         LyXFont current_font;
418         /// the current font
419         LyXFont real_current_font;
420         /// our buffer's default layout font
421         LyXFont defaultfont_;
422         ///
423         int background_color_;
424
425         /// only the top-level LyXText has this non-zero
426         BufferView * bv_owner;
427
428         ///
429         mutable Bidi bidi;
430         ///
431         bool in_inset_;
432         ///
433         ParagraphList paragraphs_;
434
435         /// absolute document pixel coordinates of this LyXText
436         mutable int xo_;
437         mutable int yo_;
438
439         /// our 'outermost' Font
440         LyXFont font_;
441
442
443 private:
444         /// rebreaks the given par
445         void redoParagraphInternal(ParagraphList::iterator pit);
446         /// used in setlayout
447         void makeFontEntriesLayoutSpecific(BufferParams const &, Paragraph & par);
448
449         /// Calculate and set the height of the row
450         void setHeightOfRow(ParagraphList::iterator, Row & row);
451
452         // fix the cursor `cur' after a characters has been deleted at `where'
453         // position. Called by deleteEmptyParagraphMechanism
454         void fixCursorAfterDelete(LyXCursor & cur, LyXCursor const & where);
455
456         /// delete double space (false) or empty paragraphs (true) around old_cursor
457         bool deleteEmptyParagraphMechanism(LyXCursor const & old_cursor);
458
459         ///
460         void setCounter(Buffer const &, ParagraphList::iterator pit);
461         ///
462         void deleteWordForward();
463         ///
464         void deleteWordBackward();
465         ///
466         void deleteLineForward();
467
468         /// sets row.end to the pos value *after* which a row should break.
469         /// for example, the pos after which isNewLine(pos) == true
470         void rowBreakPoint(ParagraphList::iterator pit, Row & row) const;
471
472         /// sets row.witdh to the minimum space a row needs on the screen in pixel
473         void fill(ParagraphList::iterator pit, Row & row, int workwidth) const;
474
475         /**
476          * returns the minimum space a manual label needs on the
477          * screen in pixels
478          */
479         int labelFill(ParagraphList::iterator pit, Row const & row) const;
480
481         /// FIXME
482         int labelEnd(ParagraphList::iterator pit) const;
483
484         ///
485         void charInserted();
486         /// set 'number' font property
487         void number();
488         /// is the cursor paragraph right-to-left?
489         bool rtl() const;
490 };
491
492 /// return the default height of a row in pixels, considering font zoom
493 extern int defaultRowHeight();
494
495 ///
496 std::string expandLabel(LyXTextClass const & textclass,
497                 LyXLayout_ptr const & layout, bool appendix);
498
499 #endif // LYXTEXT_H