1 /*
2 
3 Copyright (C) 2015-2018 Night Dive Studios, LLC.
4 
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 
18 */
19 #ifndef __TEXTTOOL_H
20 #define __TEXTTOOL_H
21 
22 // defines
23 #include "lg_types.h"
24 #include "rect.h"
25 
26 // These are all stolen from kbcook.h, for purposes of independence, emancipation, and the New World Order
27 // **HOWEVER** make sure they stay up to date or perhaps you will be hosed!
28 
29 #define TEXTTOOL_CNV_CTRL     (1<<(1+8)) // KB_FLAG_CTRL can be set
30 #define TEXTTOOL_CNV_ALT      (1<<(2+8)) // KB_FLAG_SHIFT can be set
31 #define TEXTTOOL_CNV_SPECIAL  (1<<(3+8)) // KB_FLAG_SPECIAL is set
32 #define TEXTTOOL_CNV_SHIFT    (1<<(4+8)) // KB_FLAG_SHIFT can be set
33 #define TEXTTOOL_CNV_2ND      (1<<(5+8)) // KB_FLAG_2ND is set
34 #define TEXTTOOL_CNV_NUMLOCK  (1<<(6+8)) // affected by numlock
35 #define TEXTTOOL_CNV_CAPSLOCK (1<<(7+8)) // affected by capslock
36 
37 #define TEXTTOOL_CNV_CAPSLOCK_SHF (7+8)
38 
39 
40 #define TEXTTOOL_KB_FLAG_DOWN    (1<<(0+8))
41 #define TEXTTOOL_KB_FLAG_CTRL    TEXTTOOL_CNV_CTRL
42 #define TEXTTOOL_KB_FLAG_ALT     TEXTTOOL_CNV_ALT
43 #define TEXTTOOL_KB_FLAG_SPECIAL TEXTTOOL_CNV_SPECIAL
44 #define TEXTTOOL_KB_FLAG_SHIFT   TEXTTOOL_CNV_SHIFT
45 #define TEXTTOOL_KB_FLAG_2ND     TEXTTOOL_CNV_2ND
46 
47 
48 #define TEXTTOOL_KB_DOWN_SHF     8
49 #define TEXTTOOL_KB_CTRL_SHF     9
50 #define TEXTTOOL_KB_ALT_SHF     10
51 #define TEXTTOOL_KB_SPECIAL_SHF 11
52 #define TEXTTOOL_KB_SHIFT_SHF   12
53 #define TEXTTOOL_KB_2ND_SHF     13
54 
55 // okay, real text tool defines now...
56 #define TTF_REPLACE    0x01    // replace any existing text (overwrite)
57 #define TTF_INSFRONT   0x02    // insert text at front of line
58 #define TTF_INSEND     0x04    // at end of line
59 #define TTF_INSWHOLE   0x08    // insert the text as a whole new line
60 
61 #define TTFI_FIXED     0x01    // For TextToolFontInfo
62 #define TTFI_SPACE     0x02
63 #define TTFI_PROP      0x04
64 #define TTFI_FREE      0x80    // should tt_toast free the TTFI
65 
66 #define TTS_WRAP       0x0001  // For EditState Mode
67 #define TTS_OVER       0x0002  // overstrike/insert mode toggle
68 #define TTS_CGROW      0x0004
69 #define TTS_COLUMN     0x0008  // column vs. wrap blocks
70 #define TTS_COMMAND    0x0010  // waiting for a command character
71 #define TTS_FULL       0x1000  // full screen editing
72 #define TTS_SINGLE     0x2000  // One line editing
73 #define TTS_LINES      0x4000  // a set of lines, final resting one is return
74 #define TTS_READONLY   0x8000  // is it, or isnt it
75 
76 #define TTS_MODE       0xF000
77 
78 // internal defines
79 #define TTC_REBUILD    (-2)    // For TextToolCheats
80 #define TTC_NOTHING    (-1)
81 
82 #define TTC_FLG_RET    0x01    // return at EOL
83 
84 #define TTL_INIT       0x02    // Initial size of a line
85 #define TTL_BASE       0x10    // Amount to add w/realloc
86 
87 #define TTPC_
88 #define TTPC_SAME      (-3)    // stay in the same place
89 #define TTPC_BOL       (0)     // beginning of line
90 #define TTPC_EOL       (-1)    // end of line, as you probably could tell
91 
92 #define TTWL_FIRST     (1)
93 #define TTWL_LAST      (2)
94 #define TTWL_CUR       (3)
95 
96 #define TTCHG_NOCHANGE 0x00
97 #define TTCHG_CURSOR   0x01
98 #define TTCHG_REDRAW   0x02
99 
100 // the event masks
101 #define _FULL_M        TTS_FULL
102 #define _SNGL_M        TTS_SINGLE
103 #define _LINE_M        TTS_LINES
104 #define _RO_M          TTS_READONLY
105 #define _ALL_M         (_SNGL_M|_FULL_M|_LINE_M|_RO_M)
106 #define _NOSNGL_M      (_ALL_M&(~_SNGL_M))
107 #define _NORO_M        (_ALL_M&(~_RO_M))
108 
109 // the event codes
110 #define TTEV_NULL      (0x0000)
111 #define TTEV_F_CHAR    (0x01|_ALL_M)
112 #define TTEV_B_CHAR    (0x02|_ALL_M)
113 #define TTEV_F_WORD    (0x03|_ALL_M)
114 #define TTEV_B_WORD    (0x04|_ALL_M)
115 #define TTEV_F_LINE    (0x05|_NOSNGL_M)
116 #define TTEV_B_LINE    (0x06|_NOSNGL_M)
117 #define TTEV_HOME      (0x07|_ALL_M)
118 #define TTEV_END       (0x08|_ALL_M)
119 #define TTEV_CENTER    (0x09|_NOSNGL_M)
120 #define TTEV_REPEAT    (0x0A|_ALL_M)
121 #define TTEV_OVER      (0x0B|_ALL_M)
122 #define TTEV_INS       (0x0C|_ALL_M)
123 #define TTEV_DEL       (0x0D|_NORO_M)
124 #define TTEV_PGDN      (0x0E|_NOSNGL_M)
125 #define TTEV_PGUP      (0x0F|_NOSNGL_M)
126 #define TTEV_CUT       (0x10|_NORO_M)
127 #define TTEV_YANK      (0x11|_NORO_M)
128 #define TTEV_RET       (0x12|_ALL_M)
129 #define TTEV_COMMAND   (0x13|_ALL_M)
130 #define TTEV_TOP       (0x14|_NOSNGL_M)
131 #define TTEV_BACKSP    (0x15|_NORO_M)
132 #define TTEV_MACRO_ST  (0x16|_ALL_M)
133 #define TTEV_MACRO_END (0x17|_ALL_M)
134 #define TTEV_MACRO_PLY (0x18|_ALL_M)
135 #define TTEV_2_HOME    (0x19|_ALL_M)
136 #define TTEV_2_END     (0x1A|_ALL_M)
137 #define TTEV_3_HOME    (0x1B|_ALL_M)
138 #define TTEV_3_END     (0x1C|_ALL_M)
139 #define TTEV_EOL       (0x1D|_ALL_M)
140 #define TTEV_BOL       (0x1E|_ALL_M)
141 #define TTEV_KILL      (0x1F|_NORO_M)
142 #define TTEV_WRAP      (0x20|_FULL_M)
143 
144 //#define TTEV_
145 
146 //#define TTEV_UNDO
147 //#define TTEV_REDO
148 
149 // structs
150 typedef struct {
151    int (*s_wid)(char *s), (*s_draw)(char *s, int x, int y), (*c_wid)(char c), (*c_draw)(char c, int x, int y);
152    int (*s_clr)(char *s, int x, int y), (*l_clr)(int len, int x, int y), (*c_clr)(char c, int x, int y);
153    int (*cursor)(int x, int y);
154    int height, base_w;
155    uchar type;
156 } TTFontInfo;
157 
158 typedef struct {
159    long  left_m, right_m;      // current margins
160    long  max_w;                // maximum width for a line, or 0 for none
161    ulong mode;                 // mode of editing
162    int   r_cnt;                // current repeat count
163    int   last_ev;              // last event (for multi-event stuff, like triple home)
164 } TTState;
165 
166 typedef struct {
167    long wid, stl;              // malloced width of line, length of string
168    long chr[2];                // l/r edge of screen char off
169    long pix[2];                // actual pix width there
170    uchar flg;                  // various text flags
171 } TTCheats;
172 
173 typedef union {
174    int c[2];                   // coord x,y
175    struct {
176 	   int x, y;
177    } pt;
178 } TTPoint;
179 
180 typedef struct {
181    TTPoint crn;
182    int w, h;
183 } TTRect;
184 
185 typedef struct {
186    long max_w, max_h;          // max w/h in characters
187    long cur_w, cur_h;          // cursor w/h in characters
188    long disp_x, disp_y;        // x(abspix)/y(lines) of display top
189    int  disp_rows;             // number of rows we really can fit on the screen
190    int  cur_x, cur_y;          // x/y in scr pixels of cursor
191    TTRect scr_loc;             // screen offset + size of the texttool
192    void (*display_func)(void *, LGRect *);
193    void *output_data;          // some appropriate output data
194    char **lines;               // the lines of actual text
195    TTCheats *line_info;        // offsets for current lines
196    TTFontInfo *lfont;          // the font being used
197    TTState es;
198 } TextTool;
199 
200 // prototypes
201 TextTool *tt_full_build(TTRect *pos, TTState *es, TTFontInfo *ttf, void *output_data, char *keymap, void (*d_func)(void *, LGRect *));
202 #define tt_easy_build() tt_full_build(NULL,NULL,NULL,NULL,NULL)
203 uchar tt_toast(TextTool *old_tt);
204 uchar tt_set(TextTool *def_tt);
205 long tt_parse_char(TextTool *tt, ushort key_code);
206 long tt_parse_string(TextTool *tt, char *st);
207 char *tt_get(TextTool *tt, long line_num);
208 void tt_dump(TextTool *tt);
209 void tt_display_all(TextTool *tt, LGRect *r);
210 void tt_show_all(TextTool *tt);
211 void tt_move(TextTool *tt, int xoff, int yoff);
212 void tt_fill_line(TextTool *tt, int how, long line_num, char *s);
213 #endif
214