libt3widget
textline.h
1 /* Copyright (C) 2011-2012 G.P. Halkes
2  This program is free software: you can redistribute it and/or modify
3  it under the terms of the GNU General Public License version 3, as
4  published by the Free Software Foundation.
5 
6  This program is distributed in the hope that it will be useful,
7  but WITHOUT ANY WARRANTY; without even the implied warranty of
8  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  GNU General Public License for more details.
10 
11  You should have received a copy of the GNU General Public License
12  along with this program. If not, see <http://www.gnu.org/licenses/>.
13 */
14 #ifndef T3_WIDGET_TEXTLINE_H
15 #define T3_WIDGET_TEXTLINE_H
16 
17 #define BUFFERSIZE 64
18 #define BUFFERINC 16
19 
20 #include <sys/types.h>
21 #include <stdio.h>
22 #include <string>
23 #include <t3window/window.h>
24 
25 #include <t3widget/widget_api.h>
26 #include <t3widget/key.h>
27 
28 namespace t3_widget {
29 
30 class undo_t;
31 
32 #define _T3_MAX_TAB 80
33 
34 class text_line_factory_t;
35 
36 class T3_WIDGET_API text_line_t {
37  public:
38  enum {
39  BREAK = (1<<0),
40  PARTIAL_CHAR = (1<<1),
41  SPACECLEAR = (1<<2),
42  TAB_AS_CONTROL = (1<<3),
43  EXTEND_SELECTION = (1<<4),
44  DEBUG_LINE = (1<<5),
45  SHOW_TABS = (1<<6)
46  };
47 
48  struct T3_WIDGET_API paint_info_t {
49  int start; // Byte position of the start of the line (0 unless line wrapping is in effect)
50  int leftcol; // First cell to draw
51 
52  int max; // Last position of the line (INT_MAX unless line wrapping is in effect)
53  int size; // Maximum number of characters to draw
54 
55  int tabsize; // Length of a tab, or 0 to force TAB_AS_CONTROL flag
56  int flags; // See above for valid flags
57  int selection_start; // Start of selected text in bytes
58  int selection_end; // End of selected text in bytes
59  int cursor; // Location of cursor in bytes
60  t3_attr_t normal_attr, selected_attr; // Attributes to be used for normal an selected texts
61  //string highlighting;
62  };
63 
64  struct T3_WIDGET_API break_pos_t {
65  int pos;
66  int flags;
67  };
68 
69  private:
70  std::string buffer;
71  bool starts_with_combining;
72 
73  protected:
74  text_line_factory_t *factory;
75 
76  private:
77  static char spaces[_T3_MAX_TAB];
78  static char dashes[_T3_MAX_TAB];
79  static char dots[16];
80  static const char *control_map;
81  static const char *wrap_symbol;
82 
83  static void paint_part(t3_window_t *win, const char *paint_buffer, bool is_print, int todo, t3_attr_t selection_attr);
84  static int key_width(key_t key);
85 
86  t3_attr_t get_draw_attrs(int i, const text_line_t::paint_info_t *info);
87 
88  void fill_line(const char *_buffer, int length);
89  bool check_boundaries(int match_start, int match_end) const;
90  void update_meta_buffer(int start_pos = 0);
91  char get_char_meta(int pos) const;
92 
93  void insert_bytes(int pos, const char *bytes, int space);
94  void reserve(int size);
95  int byte_width_from_first(int pos) const;
96 
97  public:
98  text_line_t(int buffersize = BUFFERSIZE, text_line_factory_t *_factory = NULL);
99  text_line_t(const char *_buffer, text_line_factory_t *_factory = NULL);
100  text_line_t(const char *_buffer, int length, text_line_factory_t *_factory = NULL);
101  text_line_t(const std::string *str, text_line_factory_t *_factory = NULL);
102  virtual ~text_line_t(void);
103 
104  void set_text(const char *_buffer);
105  void set_text(const char *_buffer, size_t length);
106  void set_text(const std::string *str);
107 
108  void merge(text_line_t *other);
109  text_line_t *break_line(int pos);
110  text_line_t *cut_line(int start, int end);
111  text_line_t *clone(int start, int end);
112  text_line_t *break_on_nl(int *start_from);
113  void insert(text_line_t *other, int pos);
114 
115  void minimize(void);
116 
117  int calculate_screen_width(int start, int pos, int tabsize) const;
118  int calculate_line_pos(int start, int max, int pos, int tabsize) const;
119 
120  void paint_line(t3_window_t *win, const paint_info_t *info);
121 
122  break_pos_t find_next_break_pos(int start, int length, int tabsize) const;
123  int get_next_word(int start) const;
124  int get_previous_word(int start) const;
125 
126  bool insert_char(int pos, key_t c, undo_t *undo);
127  bool overwrite_char(int pos, key_t c, undo_t *undo);
128  bool delete_char(int pos, undo_t *undo);
129  bool append_char(key_t c, undo_t *undo);
130  bool backspace_char(int pos, undo_t *undo);
131 
132  int adjust_position(int pos, int adjust) const;
133 
134  int get_length(void) const;
135  int width_at(int pos) const;
136  bool is_print(int pos) const;
137  bool is_space(int pos) const;
138  bool is_alnum(int pos) const;
139  bool is_bad_draw(int pos) const;
140 
141  const std::string *get_data(void) const;
142 
143  int get_next_word_boundary(int start) const;
144  int get_previous_word_boundary(int start) const;
145 
146  static void init(void);
147 
148  protected:
149  virtual t3_attr_t get_base_attr(int i, const paint_info_t *info);
150 };
151 
152 class T3_WIDGET_API text_line_factory_t {
153  public:
154  text_line_factory_t(void);
155  virtual ~text_line_factory_t(void);
156  virtual text_line_t *new_text_line_t(int buffersize = BUFFERSIZE);
157  virtual text_line_t *new_text_line_t(const char *_buffer);
158  virtual text_line_t *new_text_line_t(const char *_buffer, int length);
159  virtual text_line_t *new_text_line_t(const std::string *str);
160 };
161 
162 T3_WIDGET_API extern text_line_factory_t default_text_line_factory;
163 
164 }; // namespace
165 #endif
Definition: textline.h:152
The t3_widget namespace is contains all classes, functions and global variables in the libt3widget li...
Definition: autocompleter.cc:18
Definition: textline.h:64
long key_t
Integer type holding a single key symbol.
Definition: key.h:24
Definition: textline.h:36
Definition: textline.h:48
complex_error_t init(const init_parameters_t *params)
Initialize the libt3widget library.
Definition: main.cc:253
Definition: undo.h:85