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