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