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