libt3widget
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups
textbuffer.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_TEXTBUFFER_H
15 #define T3_WIDGET_TEXTBUFFER_H
16 
17 #include <cstdio>
18 #include <vector>
19 #include <string>
20 
21 #include <t3widget/key.h>
22 #include <t3widget/textline.h>
23 #include <t3widget/undo.h>
24 #include <t3widget/interfaces.h>
25 
26 namespace t3_widget {
27 
28 typedef std::vector<text_line_t *> lines_t;
29 
30 struct find_result_t;
31 class finder_t;
32 class wrap_info_t;
33 
34 class T3_WIDGET_API text_buffer_t {
35  friend class wrap_info_t;
36  private:
37  struct T3_WIDGET_LOCAL implementation_t {
38  lines_t lines;
39  text_coordinate_t selection_start;
40  text_coordinate_t selection_end;
41  selection_mode_t selection_mode;
42 
43  undo_list_t undo_list;
44  text_coordinate_t last_undo_position;
45  undo_type_t last_undo_type;
46  undo_t *last_undo;
47 
48  text_line_factory_t *line_factory;
49 
50  implementation_t(text_line_factory_t *_line_factory) : selection_start(-1, 0), selection_end(-1, 0),
51  selection_mode(selection_mode_t::NONE), last_undo_type(UNDO_NONE), last_undo(NULL),
52  line_factory(_line_factory == NULL ? &default_text_line_factory : _line_factory)
53  {}
54  };
55  pimpl_ptr<implementation_t>::t impl;
56 
57  protected:
58  undo_t *get_undo(undo_type_t type);
59  undo_t *get_undo(undo_type_t type, text_coordinate_t coord);
60  undo_t *get_undo(undo_type_t type, text_coordinate_t start, text_coordinate_t end);
61 
62  void set_undo_mark(void);
63 
64  void locate_pos(void);
65  void locate_pos(text_coordinate_t *coord) const;
66  void delete_block_internal(text_coordinate_t start, text_coordinate_t end, undo_t *undo);
67  bool insert_block_internal(text_coordinate_t insert_at, text_line_t *block);
68  int apply_undo_redo(undo_type_t type, undo_t *current);
69  bool merge_internal(int line);
70  bool break_line_internal(const std::string *indent = NULL);
71 
72  bool undo_indent_selection(undo_t *undo, undo_type_t type);
73 
74  text_line_t *get_line_data_nonconst(int idx);
75  text_line_factory_t *get_line_factory(void);
76 
77  virtual void prepare_paint_line(int line);
78 
79  public:
80  text_buffer_t(text_line_factory_t *_line_factory = NULL);
81  virtual ~text_buffer_t(void);
82 
83  int size(void) const;
84  const text_line_t *get_line_data(int idx) const;
85 
86  bool insert_char(key_t c);
87  bool overwrite_char(key_t c);
88  bool delete_char(void);
89  bool backspace_char(void);
90  bool merge(bool backspace);
91  bool break_line(const std::string *indent = NULL);
92  bool insert_block(const std::string *block);
93 
94  bool append_text(const char *text);
95  bool append_text(const char *text, size_t _size);
96  bool append_text(const std::string *text);
97 
98  int get_line_max(int line) const;
99  void adjust_position(int adjust);
100  int width_at_cursor(void) const;
101 
102  void paint_line(t3_window_t *win, int line, const text_line_t::paint_info_t *info);
103  void goto_next_word(void);
104  void goto_previous_word(void);
105 
106  int calculate_screen_pos(const text_coordinate_t *where, int tabsize) const;
107  int calculate_line_pos(int line, int pos, int tabsize) const;
108 
109  text_coordinate_t get_selection_start(void) const;
110  text_coordinate_t get_selection_end(void) const;
111  void set_selection_end(bool update_primary = true);
112  void set_selection_mode(selection_mode_t mode);
113  selection_mode_t get_selection_mode(void) const;
114  bool selection_empty(void) const;
115  void delete_block(text_coordinate_t start, text_coordinate_t end);
116  bool replace_block(text_coordinate_t start, text_coordinate_t end, const std::string *block);
117  bool indent_selection(int tabsize, bool tab_spaces);
118  bool indent_block(text_coordinate_t &start, text_coordinate_t &end, int tabsize, bool tab_spaces);
119  bool unindent_selection(int tabsize);
120  bool unindent_block(text_coordinate_t &start, text_coordinate_t &end, int tabsize);
121  bool unindent_line(int tabsize);
122  void set_selection_from_find(find_result_t *result);
123 
134  bool find(finder_t *finder, find_result_t *result, bool reverse = false) const;
135  bool find_limited(finder_t *finder, text_coordinate_t start, text_coordinate_t end, find_result_t *result) const;
136  void replace(finder_t *finder, find_result_t *result);
137 
138  bool is_modified(void) const;
139  std::string *convert_block(text_coordinate_t start, text_coordinate_t end);
140  int apply_undo(void);
141  int apply_redo(void);
142  void start_undo_block(void);
143  void end_undo_block(void);
144 
145  void goto_next_word_boundary(void);
146  void goto_previous_word_boundary(void);
147 
148  //FIXME: make these members private again
149  text_coordinate_t cursor;
150 
151  T3_WIDGET_SIGNAL(rewrap_required, void, rewrap_type_t, int, int);
152 };
153 
154 }; // namespace
155 #endif