]> git.lyx.org Git - lyx.git/blob - src/lyxtext.h
61a8cbbdc42e543303009901ef6f060c68401a39
[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 "dispatchresult.h"
20 #include "lyxfont.h"
21 #include "layout.h"
22 #include "lyxlayout_ptr_fwd.h"
23 #include "ParagraphList_fwd.h"
24 #include "RowList_fwd.h"
25
26 #include <iosfwd>
27
28 class Buffer;
29 class BufferParams;
30 class BufferView;
31 class CursorSlice;
32 class Dimension;
33 class InsetBase;
34 class InsetOld_code;
35 class FuncRequest;
36 class LColor_color;
37 class LCursor;
38 class LyXTextClass;
39 class MetricsInfo;
40 class PainterInfo;
41 class Paragraph;
42 class Row;
43 class Spacing;
44 class UpdatableInset;
45 class VSpace;
46
47
48 /// This class encapsulates the main text data and operations in LyX
49 class LyXText {
50 public:
51         /// constructor
52         LyXText(BufferView *, bool ininset);
53         ///
54         void init(BufferView *);
55
56         /// update y coordinate cache of all paragraphs
57         void updateParPositions();
58         ///
59         LyXFont getFont(ParagraphList::iterator pit, lyx::pos_type pos) const;
60         ///
61         LyXFont getLayoutFont(ParagraphList::iterator pit) const;
62         ///
63         LyXFont getLabelFont(ParagraphList::iterator pit) const;
64         ///
65         void setCharFont(ParagraphList::iterator pit,
66                          lyx::pos_type pos, LyXFont const & font);
67         void setCharFont(ParagraphList::iterator pit,
68                          lyx::pos_type pos,
69                          LyXFont const & font, bool toggleall);
70
71         /// what you expect when pressing <enter> at cursor position
72         void breakParagraph(ParagraphList & paragraphs, char keep_layout = 0);
73
74         /** set layout over selection and make a total rebreak of
75           those paragraphs
76           */
77         ParagraphList::iterator
78         setLayout(ParagraphList::iterator start,
79                   ParagraphList::iterator end,
80                   std::string const & layout);
81         ///
82         void setLayout(std::string const & layout);
83
84         /// Increase or decrease the nesting depth of the selected paragraph(s)
85         void changeDepth(bv_funcs::DEPTH_CHANGE type);
86
87         /// Returns whether something would be changed by changeDepth
88         bool changeDepthAllowed(bv_funcs::DEPTH_CHANGE type);
89
90         /// get the depth at current cursor position
91         int getDepth() const;
92
93         /** set font over selection and make a total rebreak of those
94           paragraphs.
95           toggleall defaults to false.
96           */
97         void setFont(LyXFont const &, bool toggleall = false);
98
99         /// rebreaks all paragaphs between the given pars.
100         void redoParagraphs(ParagraphList::iterator begin,
101                             ParagraphList::iterator end);
102         /// rebreaks the given par
103         void redoParagraph(ParagraphList::iterator pit);
104         /// rebreaks the cursor par
105         void redoParagraph();
106
107         ///
108         void toggleFree(LyXFont const &, bool toggleall = false);
109
110         ///
111         std::string getStringToIndex();
112
113         /// insert a character at cursor position
114         void insertChar(char c);
115         /// insert an inset at cursor position
116         void insertInset(InsetBase * 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(LCursor & cur, FuncRequest const & cmd);
127         /// access to out BufferView. This should go...
128         BufferView * bv();
129         /// access to out BufferView. This should go...
130         BufferView * bv() const;
131
132         /// returns an iterator pointing to a cursor paragraph
133         ParagraphList::iterator getPar(CursorSlice const & cursor) const;
134         ///
135         ParagraphList::iterator getPar(lyx::paroffset_type par) const;
136         ///
137         int parOffset(ParagraphList::iterator pit) const;
138         /// convenience
139         ParagraphList::iterator cursorPar() const;
140         ///
141         RowList::iterator cursorRow() const;
142
143         /** returns an iterator pointing to the row near the specified
144           * y-coordinate (relative to the whole text). y is set to the
145           * real beginning of this row
146           */
147         RowList::iterator getRowNearY(int y,
148                 ParagraphList::iterator & pit) const;
149
150         /** returns the column near the specified x-coordinate of the row
151          x is set to the real beginning of this column
152          */
153         lyx::pos_type getColumnNearX(ParagraphList::iterator pit,
154                 Row const & row, int & x, bool & boundary) const;
155
156         /** Find the word under \c from in the relative location
157          *  defined by \c word_location.
158          *  @param from return here the start of the word
159          *  @param to return here the end of the word
160          */
161         void getWord(CursorSlice & from, CursorSlice & to, lyx::word_location const);
162         /// just selects the word the cursor is in
163         void selectWord(lyx::word_location loc);
164         /// returns the inset at cursor (if it exists), 0 otherwise
165         InsetBase * getInset() const;
166
167         /// accept selected change
168         void acceptChange();
169
170         /// reject selected change
171         void rejectChange();
172
173         ///
174         void setCursor(ParagraphList::iterator pit, lyx::pos_type pos);
175         /// returns true if par was empty and was removed
176         bool setCursor(lyx::paroffset_type par, lyx::pos_type pos,
177                        bool setfont = true, bool boundary = false);
178         ///
179         void setCursor(CursorSlice &, lyx::paroffset_type par,
180                        lyx::pos_type pos, bool boundary = false);
181         ///
182         void setCursorIntern(lyx::paroffset_type par, lyx::pos_type pos,
183                              bool setfont = true, bool boundary = false);
184         ///
185         void setCurrentFont();
186
187         ///
188         void recUndo(lyx::paroffset_type first, lyx::paroffset_type last) const;
189         ///
190         void recUndo(lyx::paroffset_type first) const;
191         ///
192         void setCursorFromCoordinates(int x, int y);
193         ///
194         void setCursorFromCoordinates(CursorSlice &, int x, int y);
195         ///
196         void edit(LCursor & cur, 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         /** 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         InsetBase * 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         /// access to our paragraphs
322         ParagraphList & paragraphs() const;
323         /// return true if this is owned by an inset.
324         bool isInInset() const;
325         /// the first of our paragraphs
326         ParagraphList::iterator firstPar() const;
327         /// the last of our paragraphs
328         ParagraphList::iterator lastPar() const;
329         /// one past the last of our paragraphs
330         ParagraphList::iterator endPar() const;
331         
332         /// return first row of text
333         RowList::iterator firstRow() const;
334         /// return last row of text
335         RowList::iterator lastRow() const;
336         /// return row "behind" last row of text
337         RowList::iterator endRow() const;
338         /// return next row crossing paragraph boundaries
339         void nextRow(ParagraphList::iterator & pit,
340                 RowList::iterator & rit) const;
341         /// return previous row crossing paragraph boundaries
342         void previousRow(ParagraphList::iterator & pit,
343                 RowList::iterator & rit) const;
344
345         /// is this row the last in the text?
346         bool isLastRow(ParagraphList::iterator pit, Row const & row) const;
347         /// is this row the first in the text?
348         bool isFirstRow(ParagraphList::iterator pit, Row const & row) const;
349
350         ///
351         std::string selectionAsString(Buffer const & buffer, bool label) const;
352         ///
353         double spacing(Paragraph const &) const;
354         ///
355         void cursorLeftOneWord(CursorSlice &);
356         ///
357         void cursorRightOneWord(CursorSlice &);
358
359         ///
360         DispatchResult moveRight();
361         ///
362         DispatchResult moveLeft();
363         ///
364         DispatchResult moveRightIntern(bool front,
365                 bool activate_inset, bool selecting);
366         ///
367         DispatchResult moveLeftIntern(bool front,
368                 bool activate_inset, bool selecting);
369         ///
370         DispatchResult moveUp();
371         ///
372         DispatchResult moveDown();
373         ///
374         bool checkAndActivateInset(bool front);
375
376         ///
377         void write(Buffer const & buf, std::ostream & os) const;
378         /// returns whether we've seen our usual 'end' marker
379         bool read(Buffer const & buf, LyXLex & lex);
380
381         ///
382         int ascent() const;
383         ///
384         int descent() const;
385         ///
386         int cursorX(CursorSlice const & cursor) const;
387         ///
388         int cursorY(CursorSlice const & cursor) const;
389
390         /// the topmost cursor slice
391         CursorSlice & cursor();
392         /// the topmost cursor slice
393         CursorSlice const & cursor() const;
394         /// access to the selection anchor
395         CursorSlice & anchor();
396         /// access to the selection anchor
397         CursorSlice const & anchor() const;
398
399         friend class LyXScreen;
400
401 public:
402         ///
403         int height;
404         ///
405         unsigned int width;
406         ///
407         int textwidth_;
408         /// the current font settings
409         LyXFont current_font;
410         /// the current font
411         LyXFont real_current_font;
412         /// our buffer's default layout font
413         LyXFont defaultfont_;
414         ///
415         int background_color_;
416
417         /// only the top-level LyXText has this non-zero
418         BufferView * bv_owner;
419
420         ///
421         mutable Bidi bidi;
422         ///
423         bool in_inset_;
424         ///
425         ParagraphList paragraphs_;
426
427         /// absolute document pixel coordinates of this LyXText
428         mutable int xo_;
429         mutable int yo_;
430
431         /// our 'outermost' Font
432         LyXFont font_;
433
434
435 private:
436         /// return past-the-last paragraph influenced by a layout
437         /// change on pit
438         ParagraphList::iterator undoSpan(ParagraphList::iterator pit);
439         
440         /// rebreaks the given par
441         void redoParagraphInternal(ParagraphList::iterator pit);
442         /// used in setlayout
443         void makeFontEntriesLayoutSpecific(BufferParams const &, Paragraph & par);
444
445         /// Calculate and set the height of the row
446         void setHeightOfRow(ParagraphList::iterator, Row & row);
447
448         // fix the cursor `cur' after a characters has been deleted at `where'
449         // position. Called by deleteEmptyParagraphMechanism
450         void fixCursorAfterDelete(CursorSlice & cur, CursorSlice const & where);
451
452         /// delete double space (false) or empty paragraphs (true) around old_cursor
453         bool deleteEmptyParagraphMechanism(CursorSlice const & old_cursor);
454
455         ///
456         void setCounter(Buffer const &, ParagraphList::iterator pit);
457         ///
458         void deleteWordForward();
459         ///
460         void deleteWordBackward();
461         ///
462         void deleteLineForward();
463
464         /// sets row.end to the pos value *after* which a row should break.
465         /// for example, the pos after which isNewLine(pos) == true
466         void rowBreakPoint(ParagraphList::iterator pit, Row & row) const;
467
468         /// sets row.witdh to the minimum space a row needs on the screen in pixel
469         void fill(ParagraphList::iterator pit, Row & row, int workwidth) const;
470
471         /**
472          * returns the minimum space a manual label needs on the
473          * screen in pixels
474          */
475         int labelFill(ParagraphList::iterator pit, Row const & row) const;
476
477         /// FIXME
478         int labelEnd(ParagraphList::iterator pit) const;
479
480         ///
481         void charInserted();
482         /// set 'number' font property
483         void number();
484         /// is the cursor paragraph right-to-left?
485         bool rtl() const;
486 };
487
488 /// return the default height of a row in pixels, considering font zoom
489 extern int defaultRowHeight();
490
491 ///
492 std::string expandLabel(LyXTextClass const & textclass,
493                 LyXLayout_ptr const & layout, bool appendix);
494
495 #endif // LYXTEXT_H