b5c528d40548d5fb36018ea514750e51bf5287ad
[opencv] / otherlibs / _fltk / include / FL / Fl_Text_Display.H
1 //
2 // "$Id: Fl_Text_Display.H,v 1.2 2002/12/01 15:38:37 neurosurg Exp $"
3 //
4 // Header file for Fl_Text_Display class.
5 //
6 // Copyright 2001-2002 by Bill Spitzak and others.
7 // Original code Copyright Mark Edel.  Permission to distribute under
8 // the LGPL for the FLTK library granted by Mark Edel.
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Library General Public
12 // License as published by the Free Software Foundation; either
13 // version 2 of the License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 // Library General Public License for more details.
19 //
20 // You should have received a copy of the GNU Library General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 // USA.
24 //
25 // Please report all bugs and problems to "fltk-bugs@fltk.org".
26 //
27
28 #ifndef FL_TEXT_DISPLAY_H
29 #define FL_TEXT_DISPLAY_H
30
31 #include "fl_draw.H"
32 #include "Fl_Group.H"
33 #include "Fl_Widget.H"
34 #include "Fl_Scrollbar.H"
35 #include "Fl_Text_Buffer.H"
36
37 class FL_EXPORT Fl_Text_Display: public Fl_Group {
38   public:
39     enum {
40       NORMAL_CURSOR, CARET_CURSOR, DIM_CURSOR,
41       BLOCK_CURSOR, HEAVY_CURSOR
42     };
43
44     enum {
45       CURSOR_POS, CHARACTER_POS
46     };
47
48     // drag types- they match Fl::event_clicks() so that single clicking to
49     // start a collection selects by character, double clicking selects by
50     // word and triple clicking selects by line.
51     enum {
52       DRAG_CHAR = 0, DRAG_WORD = 1, DRAG_LINE = 2
53     };
54     friend void fl_text_drag_me(int pos, Fl_Text_Display* d);
55
56     typedef void (*Unfinished_Style_Cb)(int, void *);
57
58     // style attributes - currently not implemented!
59     enum {
60       ATTR_NONE = 0,
61       ATTR_UNDERLINE = 1,
62       ATTR_HIDDEN = 2
63     };
64
65     struct Style_Table_Entry {
66       Fl_Color  color;
67       Fl_Font   font;
68       int       size;
69       unsigned  attr;
70     };
71
72     Fl_Text_Display(int X, int Y, int W, int H, const char *l = 0);
73     ~Fl_Text_Display();
74
75     virtual int handle(int e);
76     void buffer(Fl_Text_Buffer* buf);
77     void buffer(Fl_Text_Buffer& buf) { buffer(&buf); }
78     Fl_Text_Buffer* buffer() { return mBuffer; }
79     void redisplay_range(int start, int end);
80     void scroll(int topLineNum, int horizOffset);
81     void insert(const char* text);
82     void overstrike(const char* text);
83     void insert_position(int newPos);
84     int insert_position() { return mCursorPos; }
85     int in_selection(int x, int y);
86     void show_insert_position();
87     int move_right();
88     int move_left();
89     int move_up();
90     int move_down();
91     int count_lines(int start, int end, bool start_pos_is_line_start);
92     int line_start(int pos);
93     int line_end(int pos, bool start_pos_is_line_start);
94     int skip_lines(int startPos, int nLines, bool startPosIsLineStart);
95     int rewind_lines(int startPos, int nLines);
96     void next_word(void);
97     void previous_word(void);
98     void show_cursor(int b = 1);
99     void hide_cursor() { show_cursor(0); }
100     void cursor_style(int style);
101     int scrollbar_width() { return scrollbar_width_; }
102     Fl_Align scrollbar_align() { return scrollbar_align_; }
103     void scrollbar_width(int W) { scrollbar_width_ = W; }
104     void scrollbar_align(Fl_Align a) { scrollbar_align_ = a; }
105     int word_start(int pos) { return buffer()->word_start(pos); }
106     int word_end(int pos) { return buffer()->word_end(pos); }
107
108     
109     void highlight_data(Fl_Text_Buffer *styleBuffer,
110                         const Style_Table_Entry *styleTable,
111                         int nStyles, char unfinishedStyle,
112                         Unfinished_Style_Cb unfinishedHighlightCB,
113                         void *cbArg);
114
115     int position_style(int lineStartPos, int lineLen, int lineIndex,
116                        int dispIndex);
117
118     Fl_Font textfont() const {return (Fl_Font)textfont_;}
119     void textfont(uchar s) {textfont_ = s;}
120     uchar textsize() const {return textsize_;}
121     void textsize(uchar s) {textsize_ = s;}
122     Fl_Color textcolor() const {return (Fl_Color)textcolor_;}
123     void textcolor(unsigned n) {textcolor_ = n;}
124
125     int wrapped_column(int row, int column);
126     int wrapped_row(int row);
127     void wrap_mode(int wrap, int wrap_margin);
128
129     virtual void resize(int X, int Y, int W, int H);
130
131   protected:
132     // Most (all?) of this stuff should only be called from resize() or
133     // draw().
134     // Anything with "vline" indicates thats it deals with currently
135     // visible lines.
136
137     virtual void draw();
138     void draw_text(int X, int Y, int W, int H);
139     void draw_range(int start, int end);
140     void draw_cursor(int, int);
141
142     void draw_string(int style, int x, int y, int toX, const char *string,
143                      int nChars);
144
145     void draw_vline(int visLineNum, int leftClip, int rightClip,
146                     int leftCharIndex, int rightCharIndex);
147
148     void draw_line_numbers(bool clearAll);
149
150     void clear_rect(int style, int x, int y, int width, int height);
151     void display_insert();
152
153     void offset_line_starts(int newTopLineNum);
154
155     void calc_line_starts(int startLine, int endLine);
156
157     void update_line_starts(int pos, int charsInserted, int charsDeleted,
158                             int linesInserted, int linesDeleted, int *scrolled);
159
160     void calc_last_char();
161
162     int position_to_line( int pos, int* lineNum );
163     int string_width(const char* string, int length, int style);
164
165     static void buffer_predelete_cb(int pos, int nDeleted, void* cbArg);
166     static void buffer_modified_cb(int pos, int nInserted, int nDeleted,
167                                    int nRestyled, const char* deletedText,
168                                    void* cbArg);
169
170     static void h_scrollbar_cb(Fl_Scrollbar* w, Fl_Text_Display* d);
171     static void v_scrollbar_cb( Fl_Scrollbar* w, Fl_Text_Display* d);
172     void update_v_scrollbar();
173     void update_h_scrollbar();
174     int measure_vline(int visLineNum);
175     int longest_vline();
176     int empty_vlines();
177     int vline_length(int visLineNum);
178     int xy_to_position(int x, int y, int PosType = CHARACTER_POS);
179
180     void xy_to_rowcol(int x, int y, int* row, int* column,
181                       int PosType = CHARACTER_POS);
182
183     int position_to_xy(int pos, int* x, int* y);
184     void maintain_absolute_top_line_number(int state);
185     int get_absolute_top_line_number();
186     void absolute_top_line_number(int oldFirstChar);
187     int maintaining_absolute_top_line_number();
188     void reset_absolute_top_line_number();
189     int position_to_linecol(int pos, int* lineNum, int* column);
190     void scroll_(int topLineNum, int horizOffset);
191
192     void extend_range_for_styles(int* start, int* end);
193
194     void find_wrap_range(const char *deletedText, int pos, int nInserted,
195                            int nDeleted, int *modRangeStart, int *modRangeEnd,
196                            int *linesInserted, int *linesDeleted);
197     void measure_deleted_lines(int pos, int nDeleted);
198     void wrapped_line_counter(Fl_Text_Buffer *buf, int startPos, int maxPos,
199                                int maxLines, bool startPosIsLineStart,
200                                int styleBufOffset, int *retPos, int *retLines,
201                                int *retLineStart, int *retLineEnd,
202                                bool countLastLineMissingNewLine = true);
203     void find_line_end(int pos, bool start_pos_is_line_start, int *lineEnd,
204                          int *nextLineStart);
205     int measure_proportional_character(char c, int colNum, int pos);
206     int wrap_uses_character(int lineEndPos);
207     int range_touches_selection(Fl_Text_Selection *sel, int rangeStart,
208                                  int rangeEnd);
209
210     int damage_range1_start, damage_range1_end;
211     int damage_range2_start, damage_range2_end;
212     int mCursorPos;
213     int mCursorOn;
214     int mCursorOldY;            /* Y pos. of cursor for blanking */
215     int mCursorToHint;          /* Tells the buffer modified callback
216                                    where to move the cursor, to reduce
217                                    the number of redraw calls */
218     int mCursorStyle;           /* One of enum cursorStyles above */
219     int mCursorPreferredCol;    /* Column for vert. cursor movement */
220     int mNVisibleLines;         /* # of visible (displayed) lines */
221     int mNBufferLines;          /* # of newlines in the buffer */
222     Fl_Text_Buffer* mBuffer;    /* Contains text to be displayed */
223     Fl_Text_Buffer* mStyleBuffer; /* Optional parallel buffer containing
224                                      color and font information */
225     int mFirstChar, mLastChar;  /* Buffer positions of first and last
226                                    displayed character (lastChar points
227                                    either to a newline or one character
228                                    beyond the end of the buffer) */
229     int mContinuousWrap;          /* Wrap long lines when displaying */
230     int mWrapMargin;              /* Margin in # of char positions for
231                                            wrapping in continuousWrap mode */
232     int* mLineStarts;
233     int mTopLineNum;            /* Line number of top displayed line
234                                    of file (first line of file is 1) */
235     int mAbsTopLineNum;                 /* In continuous wrap mode, the line
236                                            number of the top line if the text
237                                            were not wrapped (note that this is
238                                            only maintained as needed). */
239     int mNeedAbsTopLineNum;     /* Externally settable flag to continue
240                                            maintaining absTopLineNum even if
241                                            it isn't needed for line # display */
242     int mHorizOffset;           /* Horizontal scroll pos. in pixels */
243     int mTopLineNumHint;        /* Line number of top displayed line
244                                    of file (first line of file is 1) */
245     int mHorizOffsetHint;       /* Horizontal scroll pos. in pixels */
246     int mNStyles;               /* Number of entries in styleTable */
247     const Style_Table_Entry *mStyleTable; /* Table of fonts and colors for
248                                    coloring/syntax-highlighting */
249     char mUnfinishedStyle;      /* Style buffer entry which triggers
250                                    on-the-fly reparsing of region */
251     Unfinished_Style_Cb mUnfinishedHighlightCB; /* Callback to parse "unfinished" */
252                                 /* regions */
253     void* mHighlightCBArg;      /* Arg to unfinishedHighlightCB */
254
255     int mMaxsize;
256
257     int mFixedFontWidth;        /* Font width if all current fonts are
258                                    fixed and match in width, else -1 */
259     int mSuppressResync;                /* Suppress resynchronization of line
260                                            starts during buffer updates */
261     int mNLinesDeleted;                 /* Number of lines deleted during
262                                            buffer modification (only used
263                                            when resynchronization is suppressed) */
264     int mModifyingTabDistance;  /* Whether tab distance is being
265                                            modified */
266
267     Fl_Color mCursor_color;
268
269     Fl_Scrollbar* mHScrollBar;
270     Fl_Scrollbar* mVScrollBar;
271     int scrollbar_width_;
272     Fl_Align scrollbar_align_;
273     int dragPos, dragType, dragging;
274     int display_insert_position_hint;
275     struct { int x, y, w, h; } text_area;
276
277     uchar textfont_;
278     uchar textsize_;
279     unsigned textcolor_;
280
281          // The following are not presently used from the original NEdit code,
282          // but are being put here so that future versions of Fl_Text_Display
283          // can implement line numbers without breaking binary compatibility.
284     int mLineNumLeft, mLineNumWidth;
285                                 /* Line number margin and width */
286 };
287
288 #endif
289
290 //
291 // End of "$Id: Fl_Text_Display.H,v 1.2 2002/12/01 15:38:37 neurosurg Exp $".
292 //