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