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