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