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