forked from tecknicaltom/HT-Editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtextedit.h
409 lines (369 loc) · 11.8 KB
/
textedit.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
* HT Editor
* textedit.h
*
* Copyright (C) 1999-2002 Sebastian Biallas (sb@biallas.net)
* Copyright (C) 1999-2002 Stefan Weyergraf
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef __TEXTEDIT_H__
#define __TEXTEDIT_H__
#include "htobj.h"
#include "textfile.h"
#include "htformat.h"
#define TEXTEDITOPT_NULL 0
#define TEXTEDITOPT_INPUTTABS 1
#define TEXTEDITOPT_UNDO 2
struct text_viewer_pos {
uint line;
uint pofs;
};
union text_search_pos {
FileOfs offset;
};
class ht_text_editor;
#define ATOM_HT_UNDO_DATA_INSERT MAGIC32("UND\x01")
#define ATOM_HT_UNDO_DATA_OVERWRITE MAGIC32("UND\x02")
#define ATOM_HT_UNDO_DATA_DELETE MAGIC32("UND\x03")
#define ATOM_HT_UNDO_DATA_DELETE2 MAGIC32("UND\x04")
#define ATOM_HT_UNDO_DATA_SPLIT_LINE MAGIC32("UND\x05")
#define ATOM_HT_UNDO_DATA_JOIN_LINE MAGIC32("UND\x06")
#define ATOM_HT_UNDO_DATA_INSERT_BLOCK MAGIC32("UND\x07")
#define ATOM_HT_UNDO_DATA_DELETE_BLOCK MAGIC32("UND\x08")
/*
* CLASS ht_undo_data
*/
class ht_undo_data: public Object {
public:
virtual bool combine(ht_undo_data *ud);
virtual uint getsize() = 0;
virtual void gettext(char *text, uint maxlen) = 0;
virtual void apply(ht_text_editor *te) = 0;
virtual void unapply(ht_text_editor *te, bool *goto_only) = 0;
};
/*
* CLASS ht_undo_data_delete_string
*/
class ht_undo_data_delete_string: public ht_undo_data {
text_viewer_pos apos; /* cursor before */
text_viewer_pos bpos; /* cursor after */
void *string;
uint len;
public:
ht_undo_data_delete_string(text_viewer_pos *apos, text_viewer_pos *bpos, void *string, uint len);
~ht_undo_data_delete_string();
virtual bool combine(ht_undo_data *ud);
virtual uint getsize();
virtual void gettext(char *text, uint maxlen);
virtual ObjectID getObjectID() const;
virtual void apply(ht_text_editor *te);
virtual void unapply(ht_text_editor *te, bool *goto_only);
};
/*
* CLASS ht_undo_data_delete_string2
*/
class ht_undo_data_delete_string2: public ht_undo_data {
text_viewer_pos apos; /* cursor before */
text_viewer_pos bpos; /* cursor after */
void *string;
uint len;
public:
ht_undo_data_delete_string2(text_viewer_pos *apos, text_viewer_pos *bpos, void *string, uint len);
~ht_undo_data_delete_string2();
virtual bool combine(ht_undo_data *ud);
virtual uint getsize();
virtual void gettext(char *text, uint maxlen);
virtual ObjectID getObjectID() const;
virtual void apply(ht_text_editor *te);
virtual void unapply(ht_text_editor *te, bool *goto_only);
};
/*
* CLASS ht_undo_data_insert_string
*/
class ht_undo_data_insert_string: public ht_undo_data {
text_viewer_pos apos;
text_viewer_pos bpos;
text_viewer_pos cpos;
void *string;
uint len;
public:
ht_undo_data_insert_string(text_viewer_pos *apos, text_viewer_pos *bpos, void *string, uint len);
~ht_undo_data_insert_string();
virtual bool combine(ht_undo_data *ud);
virtual uint getsize();
virtual void gettext(char *text, uint maxlen);
virtual ObjectID getObjectID() const;
virtual void apply(ht_text_editor *te);
virtual void unapply(ht_text_editor *te, bool *goto_only);
};
/*
* CLASS ht_undo_data_overwrite_string
*/
class ht_undo_data_overwrite_string: public ht_undo_data {
text_viewer_pos apos;
text_viewer_pos bpos;
text_viewer_pos cpos;
void *string;
uint len;
void *string2;
uint len2;
public:
ht_undo_data_overwrite_string(text_viewer_pos *apos, text_viewer_pos *bpos, void *string, uint len, void *string2, uint len2);
~ht_undo_data_overwrite_string();
virtual bool combine(ht_undo_data *ud);
virtual uint getsize();
virtual void gettext(char *text, uint maxlen);
virtual ObjectID getObjectID() const;
virtual void apply(ht_text_editor *te);
virtual void unapply(ht_text_editor *te, bool *goto_only);
};
/*
* CLASS ht_undo_data_split_line
*/
class ht_undo_data_split_line: public ht_undo_data {
text_viewer_pos apos;
text_viewer_pos bpos;
uint indent;
public:
ht_undo_data_split_line(text_viewer_pos *apos, text_viewer_pos *bpos, uint Indent);
virtual uint getsize();
virtual void gettext(char *text, uint maxlen);
virtual ObjectID getObjectID() const;
virtual void apply(ht_text_editor *te);
virtual void unapply(ht_text_editor *te, bool *goto_only);
};
/*
* CLASS ht_undo_data_join_line
*/
class ht_undo_data_join_line: public ht_undo_data {
text_viewer_pos apos;
text_viewer_pos bpos;
text_viewer_pos cpos;
public:
ht_undo_data_join_line(text_viewer_pos *apos, text_viewer_pos *bpos);
virtual uint getsize();
virtual void gettext(char *text, uint maxlen);
virtual ObjectID getObjectID() const;
virtual void apply(ht_text_editor *te);
virtual void unapply(ht_text_editor *te, bool *goto_only);
};
/*
* CLASS ht_undo_data_insert_block
*/
class ht_undo_data_insert_block: public ht_undo_data {
text_viewer_pos apos;
text_viewer_pos bpos;
text_viewer_pos cpos;
text_viewer_pos sel_start;
text_viewer_pos sel_end;
void *block;
uint size;
public:
ht_undo_data_insert_block(text_viewer_pos *apos, text_viewer_pos *bpos, void *block, uint size);
~ht_undo_data_insert_block();
virtual uint getsize();
virtual void gettext(char *text, uint maxlen);
virtual ObjectID getObjectID() const;
virtual void apply(ht_text_editor *te);
virtual void unapply(ht_text_editor *te, bool *goto_only);
};
/*
* CLASS ht_undo_data_delete_block
*/
class ht_undo_data_delete_block: public ht_undo_data {
text_viewer_pos apos;
text_viewer_pos bpos;
text_viewer_pos cpos;
text_viewer_pos sel_start;
text_viewer_pos sel_end;
void *block;
uint size;
public:
ht_undo_data_delete_block(text_viewer_pos *apos, text_viewer_pos *bpos, text_viewer_pos *Sel_start, text_viewer_pos *Sel_end);
~ht_undo_data_delete_block();
virtual uint getsize();
virtual void gettext(char *text, uint maxlen);
virtual ObjectID getObjectID() const;
virtual void apply(ht_text_editor *te);
virtual void unapply(ht_text_editor *te, bool *goto_only);
};
/*
* CLASS ht_text_editor_undo
*/
class ht_text_editor_undo: public Array {
public:
uint size, max_size;
int clean_state;
int current_position;
bool goto_state;
public:
ht_text_editor_undo(uint max_undo_size);
void insert_undo(ht_text_editor *te, ht_undo_data *undo);
bool is_clean();
bool is_clean(int i);
int get_current_position();
void mark_clean();
void undo(ht_text_editor *te, bool place_cursor_first);
void redo(ht_text_editor *te);
};
/*
* CLASS ht_text_viewer
*/
#define cmd_text_viewer_goto HT_COMMAND(601)
#define cmd_text_viewer_change_highlight HT_COMMAND(602)
#define cmd_text_editor_undo HT_COMMAND(620)
#define cmd_text_editor_redo HT_COMMAND(621)
#define cmd_text_editor_protocol HT_COMMAND(622)
#define cmd_text_editor_delete_line HT_COMMAND(623)
class ht_text_viewer: public ht_view {
friend class ht_undo_data;
friend class ht_undo_data_delete_string;
friend class ht_undo_data_delete_string2;
friend class ht_undo_data_insert_string;
friend class ht_undo_data_overwrite_string;
friend class ht_undo_data_split_line;
friend class ht_undo_data_join_line;
friend class ht_undo_data_insert_block;
friend class ht_undo_data_delete_block;
protected:
ht_textfile *textfile;
bool own_textfile;
Container *lexers;
ht_syntax_lexer *lexer;
bool own_lexer;
uint cursorx, cursory;
text_viewer_pos sel_start, sel_end;
uint top_line;
uint xofs;
bool selectcursor;
bool selectmode;
text_viewer_pos selectstart;
char *EOL_string;
char *EOF_string;
bool show_EOL;
bool show_EOF;
bool highlight_wrap;
uint tab_size;
ht_search_request *last_search_request;
FileOfs last_search_end_ofs;
/* new */
int buf_lprint0(int x, int y, int c, int l, char *text);
uint char_vsize(char c, uint x);
void clipboard_copy_cmd();
bool continue_search();
virtual vcp get_bgcolor();
void make_pos_physical(text_viewer_pos *p);
void normalize_selection();
uint physical_cursorx();
void popup_change_highlight();
void render_meta(uint x, uint y, text_viewer_pos *pos, vcp color);
void render_str(int x, int y, vcp color, text_viewer_pos *pos, uint len, char *str, bool multi);
void render_str_color(vcp *color, text_viewer_pos *pos);
ht_search_result *search(ht_search_request *request, text_search_pos *start, text_search_pos *end);
bool show_search_result(ht_search_result *result);
public:
void init(Bounds *b, bool own_textfile, ht_textfile *textfile, Container *lexers);
virtual void done();
/* overwritten */
virtual void config_changed();
virtual void draw();
virtual void handlemsg(htmsg *msg);
virtual void resize(int rw, int rh);
/* new */
virtual const char *func(uint i, bool execute);
uint get_line_length(uint line);
uint get_line_vlength(uint line);
uint get_line_indent(uint line);
void get_selection(text_viewer_pos *start, text_viewer_pos *end);
bool goto_line(uint line);
/* position indicator string */
virtual int get_pindicator_str(char *buf, int max_len);
/* scrollbar pos */
virtual bool get_hscrollbar_pos(int *pstart, int *psize);
virtual bool get_vscrollbar_pos(int *pstart, int *psize);
/* cursor */
virtual CursorMode get_cursor_mode();
virtual void get_cursor_pos(text_viewer_pos *cursor);
/* conversions */
virtual bool pos_to_offset(text_viewer_pos *pos, FileOfs *ofs);
/**/
ht_syntax_lexer *get_lexer();
int ppos_str(char *buf, uint bufsize, text_viewer_pos *ppos);
void set_lexer(ht_syntax_lexer *l, bool own_l);
void set_textfile(ht_textfile *t, bool own_t);
ht_textfile *get_textfile();
/**/
uint cursor_up(uint n);
uint cursor_down(uint n);
uint cursor_left(uint n);
uint cursor_right(uint n);
void cursor_home();
void cursor_end();
void cursor_vput(uint vx);
void cursor_pput(uint px);
void cursor_set(text_viewer_pos *pos);
uint scroll_up(uint n);
uint scroll_down(uint n);
uint scroll_left(uint n);
uint scroll_right(uint n);
void select_add(text_viewer_pos *from, text_viewer_pos *to);
void select_clear();
void select_end();
void select_set(text_viewer_pos *from, text_viewer_pos *to);
void select_start();
};
/*
* CLASS ht_text_editor
*/
class ht_text_editor: public ht_text_viewer {
protected:
uint edit_options;
ht_text_editor_undo *undo_list;
bool auto_indent;
bool overwrite_mode;
/* new */
void clipboard_cut_cmd();
void clipboard_delete_cmd();
void clipboard_paste_cmd();
virtual vcp get_bgcolor();
bool save();
public:
void init(Bounds *b, bool own_textfile, ht_textfile *textfile, Container *lexers, uint edit_options);
virtual void done();
/* overwritten */
virtual void config_changed();
virtual const char *func(uint i, bool execute);
virtual void handlemsg(htmsg *msg);
/* position indicator string */
virtual int get_pindicator_str(char *buf, int max_len);
/* cursor mode */
virtual CursorMode get_cursor_mode();
/* new */
bool concat_lines(uint a);
void delete_chars(uint line, uint ofs, uint count);
void delete_lines(uint line, uint count);
void indent(uint line, uint start, uint size);
void insert_chars(uint line, uint ofs, void *chars, uint len);
void insert_lines(uint line, uint count);
void split_line(uint a, uint pos);
void unindent(uint line, uint start, uint size);
/* undo/redo */
void textoperation_apply(ht_undo_data *ud);
void redo();
void show_protocol();
void undo(bool place_cursor_first);
};
extern int text_viewer_pos_compare(text_viewer_pos *a, text_viewer_pos *b);
#endif /* __TEXTEDIT_H__ */