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