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