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