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