]> git.lyx.org Git - lyx.git/blob - src/lyxtext.h
merge from the string-switch branch and ready for a prelease.
[lyx.git] / src / lyxtext.h
1 // -*- C++ -*-
2 /* This file is part of
3  * ======================================================
4  * 
5  *           LyX, The Document Processor
6  *       
7  *          Copyright (C) 1995 Matthias Ettrich
8  *
9  *======================================================*/
10
11 #ifndef _LYXTEXT_H
12 #define _LYXTEXT_H
13
14 #ifdef __GNUG__
15 #pragma interface
16 #endif
17
18 #include "definitions.h"
19 #include "layout.h"
20 #include "lyxfont.h"
21 #include "lyxrow.h"
22 #include "undo.h"
23 #include "lyxcursor.h"
24
25 class Buffer;
26 class BufferParams;
27 class LyXScreen;
28
29 /**
30   This class holds the mapping between buffer paragraphs and screen rows.
31   */
32 class LyXText {
33 public:
34         ///
35         enum text_status {
36                 ///
37                 UNCHANGED,
38                 ///
39                 NEED_LITTLE_REFRESH,
40                 ///
41                 NEED_MORE_REFRESH,
42                 ///
43                 NEED_VERY_LITTLE_REFRESH
44         };
45
46         /// points to Buffer.params
47         BufferParams *parameters;
48         /// points to Buffer
49         Buffer *params;
50         ///
51         int number_of_rows;
52         ///
53         long height;
54         /// the current font settings
55         LyXFont current_font;
56         /// the current font
57         LyXFont real_current_font;
58
59         /// Constructor
60         LyXText(int paperwidth, Buffer *);
61    
62         /// Destructor
63         ~LyXText();
64    
65         ///
66         LyXFont GetFont(LyXParagraph* par, int pos);
67         ///
68         void SetCharFont(LyXParagraph *par, int pos, LyXFont font);
69    
70         /// returns a pointer to the very first LyXParagraph
71         LyXParagraph *FirstParagraph();
72   
73         /// what you expect when pressing <enter> at cursor position
74         void BreakParagraph(char keep_layout = 0);
75
76         /** set layout over selection and make a total rebreak of
77           those paragraphs
78           */
79         void SetLayout(char layout);
80         
81         /// used in setlayout
82         void MakeFontEntriesLayoutSpecific(LyXParagraph *par);
83         
84         /** increment depth over selection and make a total rebreak of those 
85           paragraphs
86           */
87         void IncDepth();
88         
89         /** decrement depth over selection and make a total rebreak of those  
90           paragraphs */
91         void DecDepth();
92
93         /** Get the depth at current cursor position
94          */
95         int GetDepth() { return cursor.par->GetDepth(); }
96         
97         /** set font over selection and make a total rebreak of those
98           paragraphs.
99           toggleall defaults to false.
100           */
101         void SetFont(LyXFont font, bool toggleall=false);
102         
103         /** deletes and inserts again all paragaphs between the cursor
104           and the specified par .The Cursor is needed to set the refreshing
105           parameters. 
106           This function is needed after SetLayout and SetFont etc.
107           */
108         void RedoParagraphs(LyXCursor cursor, LyXParagraph *end_par);
109         ///
110         void RedoParagraph();
111         
112         ///
113         void ToggleFree(LyXFont font,bool toggleall=false);
114         
115         /** recalculates the heights of all previous rows of the
116             specified paragraph.  needed, if the last characters font
117             has changed.  
118             */
119         void RedoHeightOfParagraph(LyXCursor cursor);
120         
121         /** forces the redrawing of a paragraph. Needed when manipulating a 
122             right address box
123             */ 
124         void RedoDrawingOfParagraph(LyXCursor cursor);
125         
126         /** insert a character, moves all the following breaks in the 
127           same Paragraph one to the right and make a little rebreak
128           */
129         void InsertChar(char c);
130         ///
131         void InsertInset(Inset *inset);
132    
133         /// completes the insertion with a full rebreak
134         int FullRebreak();
135    
136         /// may be important for the menu
137         char* GetLayout(int row);
138         ///
139         LyXParagraph::footnote_flag GetFootnoteFlag(int row);
140         ///
141         Row* need_break_row;
142         ///
143         long refresh_y;
144         ///
145         int refresh_height;
146         ///
147         int refresh_width;
148         ///
149         int refresh_x;
150         ///
151         Row *refresh_row;
152         ///
153         int refresh_pos;
154         
155         /** wether the screen needs a refresh,
156            starting with refresh_y
157            */
158         text_status status;
159         
160         /** returns a pointer to the row near the specified y-coordinate
161           (relative to the whole text). y is set to the real beginning
162           of this row
163           */ 
164         Row* GetRowNearY(long &y);
165         
166         /** returns the column near the specified x-coordinate of the row 
167          x is set to the real beginning of this column
168          */ 
169         int GetColumnNearX(Row *row, int& x);
170         
171         /** returns a pointer to a specified row. y is set to the beginning
172          of the row
173          */
174         Row* GetRow(LyXParagraph *par, int pos, long &y);
175         
176         /** returns the height of a default row, needed  for scrollbar
177          */
178         int DefaultHeight();
179    
180         /** The cursor.
181           Later this variable has to be removed. There should be now internal
182           cursor in a text (and thus not in a buffer). By keeping this it is
183           (I think) impossible to have several views with the same buffer, but
184           the cursor placed at different places.
185           */
186         LyXCursor cursor;
187    
188         /* the selection cursor */
189         /// 
190         bool selection;
191         ///
192         bool mark_set;
193
194         ///
195         LyXCursor sel_cursor;
196         ///
197         LyXCursor sel_start_cursor;
198         ///
199         LyXCursor sel_end_cursor;
200         /// needed for the toggling
201         LyXCursor last_sel_cursor;
202         ///
203         LyXCursor toggle_cursor;
204         ///
205         LyXCursor toggle_end_cursor;
206    
207         /// need the selection cursor:
208         void SetSelection();
209         ///
210         void ClearSelection();
211
212         /// just selects the word the cursor is in
213         void SelectWord();
214
215         /** 'selects" the next word, where the cursor is not in 
216          and returns this word as string. THe cursor will be moved 
217          to the beginning of this word. 
218          With SelectSelectedWord can this be highlighted really
219          */ 
220         char* SelectNextWord(float &value);
221         ///
222         void SelectSelectedWord();
223         ///
224         void SetCursor(LyXParagraph *par, int pos);
225         ///
226         void SetCursorIntern(LyXParagraph *par, int pos);
227         ///
228         void SetCursorFromCoordinates(int x, long y);
229         ///
230         void CursorUp();
231         ///
232         void CursorDown();
233         ///
234         void CursorLeft();
235         ///
236         void CursorRight();
237         ///
238         void CursorLeftOneWord();
239         ///
240         void CursorRightOneWord();
241         ///
242         void CursorUpParagraph();
243         ///
244         void CursorDownParagraph();
245         ///
246         void CursorHome();
247         ///
248         void CursorEnd();
249         ///
250         void CursorTab();
251         ///
252         void CursorTop();
253         ///
254         void CursorBottom();
255         ///
256         void Delete();
257         ///
258         void Backspace();
259         ///
260         void DeleteWordForward();
261         ///
262         void DeleteWordBackward();
263         ///
264         void DeleteLineForward();
265         ///
266         int SelectWordWhenUnderCursor();
267         /// Change the case of the word at cursor position
268         /** action is 0 for lower case, 1 for capitalization and 2 for
269             uppercase. 
270          */
271         void ChangeWordCase(int action);
272
273         /** returns a printed row in a pixmap. The y value is needed to
274           decide, wether it is selected text or not. This is a strange
275           solution but faster.
276          */ 
277         void GetVisibleRow(LyXScreen &scr, int offset, 
278                            Row *row_ptr, long y);
279                                            
280         /* footnotes: */
281         ///
282         void ToggleFootnote();
283         ///
284         void OpenStuff();
285         ///
286         void OpenFootnotes();
287         ///
288         void OpenFootnote();
289         ///
290         void CloseFootnotes();
291         ///
292         void CloseFootnote();
293
294         /** turn the selection into a new environment. If there is no
295           selection, create an empty environment
296          */ 
297         void InsertFootnoteEnvironment(LyXParagraph::footnote_kind kind);
298         ///
299         void MeltFootnoteEnvironment();
300         ///
301         void CutSelection(bool = true);
302         ///
303         void CopySelection();
304         ///
305         void PasteSelection();
306         ///
307         void copyEnvironmentType();
308         ///
309         void pasteEnvironmentType();
310         ///
311         void InsertFootnote();
312         ///
313         void InsertMarginpar();
314         ///
315         void InsertFigure();
316         ///
317         void InsertTabular();
318
319         /** the DTP switches for paragraphs. LyX will store the top settings
320          always in the first physical paragraph, the bottom settings in the
321          last. When a paragraph is broken, the top settings rest, the bottom 
322          settings are given to the new one. So I can make shure, they do not
323          duplicate themself (and you cannnot make dirty things with them! )
324          */ 
325         void SetParagraph(bool line_top, bool line_bottom,
326                           bool pagebreak_top, bool pagebreak_bottom,
327                           VSpace space_top, VSpace space_bottom,
328                           char align, 
329                           string labelwidthstring,
330                           bool noindent);
331         void SetParagraphExtraOpt(int type,
332                                   const char *width,
333                                   const char *widthp,
334                                   int alignment, bool hfill,
335                                   bool start_minipage);
336
337         /* these things are for search and replace */
338
339         /** returns true if the specified string is at the specified
340           position
341           */
342         bool IsStringInText(LyXParagraph *par, int pos, char const* string);
343
344         /** sets the selection over the number of characters of string,
345           no check!!
346           */
347         void SetSelectionOverString(char const* string);
348
349         /** simple replacing. The font of the first selected character
350           is used
351           */
352         void ReplaceSelectionWithString(char const* string);
353
354         /** if the string can be found: return true and set the cursor to
355           the new position */
356         bool SearchForward(char const* string);
357         bool SearchBackward(char const* string);
358  
359         /// needed to insert the selection
360         void InsertStringA(char* string);
361         /// needed to insert the selection
362         void InsertStringB(char* string);
363    
364         /// usefull when texing from within LyX
365         bool GotoNextError();
366
367         /// just another feature :)
368         bool GotoNextNote();
369
370         /** needed to switch between different classes this works
371           for a list of paragraphs beginning with the specified par 
372           return value is the number of wrong conversions
373           */ 
374         int SwitchLayoutsBetweenClasses(char class1, char class2, LyXParagraph *par);
375
376         /* for the greater insets */
377   
378         /// returns 0 if inset wasn't found
379         int UpdateInset(Inset* inset);
380         ///
381         void CheckParagraph(LyXParagraph* par, int pos);
382
383         /* table stuff -- begin*/
384
385         /** all table features of the text-class are put together in
386           this function. Possible values of feature are defined in table.h
387           */
388         void TableFeatures(int feature,string val);
389         ///
390         void TableFeatures(int feature);
391         ///
392         int NumberOfCell(LyXParagraph *par, int pos);
393
394         /** pos points at the beginning of the next cell (if there is one)
395          */
396         int WidthOfCell(LyXParagraph *par, int &pos);
397         ///
398         void InsertCharInTable(char c);
399         ///
400         void BackspaceInTable();
401         ///
402         void CheckParagraphInTable(LyXParagraph* par, int pos);
403         ///
404         char HitInTable(Row* row, int x);
405         ///
406         bool MouseHitInTable(int x, long y);
407         /* table stuff -- end*/
408         ///
409         LyXParagraph* GetParFromID(int id);
410
411         // undo functions
412         /// returns false if no undo possible
413         bool  TextUndo();
414         /// returns false if no redo possible
415         bool  TextRedo();
416         /// used by TextUndo/TextRedo
417         bool TextHandleUndo(Undo* undo);
418         /// makes sure the next operation will be stored
419         void FinishUndo();
420         /// this is dangerous and for internal use only
421         void FreezeUndo();
422         /// this is dangerous and for internal use only
423         void UnFreezeUndo();
424         /// the flag used by FinishUndo();
425         bool undo_finished;
426         /// a flag
427         bool undo_frozen;
428         ///
429         void SetUndo(Undo::undo_kind kind, LyXParagraph *before, LyXParagraph *end);
430         ///
431         void SetRedo(Undo::undo_kind kind, LyXParagraph *before, LyXParagraph *end);
432         ///
433         Undo *CreateUndo(Undo::undo_kind kind, LyXParagraph *before, LyXParagraph *end);
434         /// for external use in lyx_cb.C
435         void SetCursorParUndo();
436         ///
437         void CursorLeftIntern();
438         ///
439         void CursorRightIntern();
440         ///
441         void RemoveTableRow(LyXCursor *cursor);
442         ///
443         bool IsEmptyTableRow(LyXCursor *cursor);
444         ///
445         bool IsEmptyTableCell();
446         ///
447         void toggleAppendix();
448
449 private:
450         ///
451         Row* firstrow;
452         ///
453         Row* lastrow;
454
455         /** Copybuffer for copy environment type
456           Asger has learned that this should be a buffer-property instead
457           Lgb has learned that 'char' is a lousy type for non-characters
458           */
459         char copylayouttype;
460
461         /// the currentrow is needed to access rows faster*/ 
462         Row* currentrow;                       /* pointer to the current row  */
463         /// position in the text 
464         long  currentrow_y;
465         /// width of the paper
466         unsigned short  paperwidth;
467    
468         /** inserts a new row behind the specified row, increments
469          * the touched counters */
470         void InsertRow(Row *row, LyXParagraph *par, int pos );
471
472         /** removes the row and reset the touched counters */
473         void RemoveRow(Row *row);
474
475         /** remove all following rows of the paragraph of the specified row. */
476         void RemoveParagraph(Row *row);
477
478         /** insert the specified paragraph behind the specified row */
479         void InsertParagraph(LyXParagraph *par, Row *row);
480
481         /** appends  the implizit specified paragraph behind the specified row,
482          * start at the implizit given position */
483         void AppendParagraph(Row *row);
484    
485         ///
486         void BreakAgain(Row *row);
487         ///
488         void BreakAgainOneRow(Row *row);
489         ///
490         void SetHeightOfRow(Row *row_ptr); /* calculate and set the height 
491                                             * of the row */
492
493         /** this calculates the specified parameters. needed when setting
494          * the cursor and when creating a visible row */ 
495         void PrepareToPrint(Row *row, float &x, float &fill_separator, 
496                             float &fill_hfill, float &fill_label_hfill);
497         ///
498         void DeleteEmptyParagraphMechanism(LyXCursor old_cursor);
499
500         /** Updates all counters starting BEHIND the row. Changed paragraphs
501          * with a dynamic left margin will be rebroken. */ 
502         void UpdateCounters(Row *row);
503         ///
504         void SetCounter(LyXParagraph *par);
505    
506         /*
507          * some low level functions
508          */
509         
510         ///
511         int SingleWidth(LyXParagraph *par, int pos);
512         ///
513         int SingleWidth(LyXParagraph *par, int pos, char c);
514         ///
515         void Draw(Row *row, int &pos, LyXScreen &scr, int offset, float &x);
516         /// get the next breakpoint in a given paragraph
517         int NextBreakPoint(Row* row, int width);
518         /// returns the minimum space a row needs on the screen in pixel
519         int Fill(Row *row, int paperwidth);
520         
521         /** returns the minimum space a manual label needs on the
522           screen in pixel */ 
523         int LabelFill(Row *row);
524         
525         ///
526         int BeginningOfMainBody(LyXParagraph *par);
527         
528         /** Returns the left beginning of the text.
529           This information cannot be taken from the layouts-objekt, because
530           in LaTeX the beginning of the text fits in some cases
531           (for example sections) exactly the label-width.
532           */
533         int LeftMargin(Row *row);
534         ///
535         int RightMargin(Row *row);
536         ///
537         int LabelEnd (Row *row);
538
539         /** returns the number of separators in the specified row.
540           The separator on the very last column doesnt count
541           */ 
542         int NumberOfSeparators(Row *row);
543
544         /** returns the number of hfills in the specified row. The
545           LyX-Hfill is a LaTeX \hfill so that the hfills at the
546           beginning and at the end were ignored. This is {\em MUCH}
547           more usefull than not to ignore!
548           */
549         int NumberOfHfills(Row *row);
550    
551         /// like NumberOfHfills, but only those in the manual label!
552         int NumberOfLabelHfills(Row *row);
553
554         /** returns true, if a expansion is needed. Rules are given by 
555           LaTeX
556           */ 
557         bool HfillExpansion(Row *row_ptr, int pos);
558    
559         /** returns the paragraph position of the last character in the 
560           specified row
561           */
562         int RowLast(Row *row);
563 };
564
565 #endif