g_rtext.c 32.3 KB
Newer Older
Miller Puckette's avatar
Miller Puckette committed
1
2
3
4
5
6
7
8
9
10
11
12
13
/* Copyright (c) 1997-1999 Miller Puckette.
* For information on usage and redistribution, and for a DISCLAIMER OF ALL
* WARRANTIES, see the file, "LICENSE.txt," in this distribution.  */

/* changes by Thomas Musil IEM KUG Graz Austria 2001 */
/* have to insert gui-objects into editor-list */
/* all changes are labeled with      iemlib      */

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include "m_pd.h"
14
#include "m_imp.h"
Miller Puckette's avatar
Miller Puckette committed
15
16
#include "s_stuff.h"
#include "g_canvas.h"
17
#include "s_utf8.h"
Miller Puckette's avatar
Miller Puckette committed
18
19
#include "t_tk.h"

Miller Puckette's avatar
Miller Puckette committed
20
21
#define LMARGIN 2
#define RMARGIN 2
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
22
23
/* for some reason, it draws text 1 pixel lower on Mac OS X */
#ifdef __APPLE__
Miller Puckette's avatar
Miller Puckette committed
24
25
#define TMARGIN 3
#define BMARGIN 1
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
26
#else
27
#define TMARGIN 3
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
28
#define BMARGIN 0
Miller Puckette's avatar
Miller Puckette committed
29
#endif
Miller Puckette's avatar
Miller Puckette committed
30
31
32
33
34

#define SEND_FIRST 1
#define SEND_UPDATE 2
#define SEND_CHECK 0

35
36
37
38
39
40
// selection for shift+arrow selecting
// 0 = none;
// 1 = start;
// 2 = end;
static int last_sel = 0;

Miller Puckette's avatar
Miller Puckette committed
41
42
struct _rtext
{
43
44
45
46
    char *x_buf;    /*-- raw byte string, assumed UTF-8 encoded (moo) --*/
    int x_bufsize;  /*-- byte length --*/
    int x_selstart; /*-- byte offset --*/
    int x_selend;   /*-- byte offset --*/
Miller Puckette's avatar
Miller Puckette committed
47
48
49
50
51
52
53
    int x_active;
    int x_dragfrom;
    int x_height;
    int x_drawnwidth;
    int x_drawnheight;
    t_text *x_text;
    t_glist *x_glist;
Mathieu L Bouchard's avatar
Mathieu L Bouchard committed
54
    char x_tag[20];
Miller Puckette's avatar
Miller Puckette committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
    struct _rtext *x_next;
};

t_rtext *rtext_new(t_glist *glist, t_text *who)
{
    t_rtext *x = (t_rtext *)getbytes(sizeof *x);
    x->x_height = -1;
    x->x_text = who;
    x->x_glist = glist;
    x->x_next = glist->gl_editor->e_rtext;
    x->x_selstart = x->x_selend = x->x_active =
        x->x_drawnwidth = x->x_drawnheight = 0;
    binbuf_gettext(who->te_binbuf, &x->x_buf, &x->x_bufsize);
    glist->gl_editor->e_rtext = x;
Mathieu L Bouchard's avatar
Mathieu L Bouchard committed
69
    sprintf(x->x_tag, "x%lx", (long)who);
Miller Puckette's avatar
Miller Puckette committed
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
    return (x);
}

static t_rtext *rtext_entered;

void rtext_free(t_rtext *x)
{
    if (x->x_glist->gl_editor->e_textedfor == x)
        x->x_glist->gl_editor->e_textedfor = 0;
    if (x->x_glist->gl_editor->e_rtext == x)
        x->x_glist->gl_editor->e_rtext = x->x_next;
    else
    {
        t_rtext *e2;
        for (e2 = x->x_glist->gl_editor->e_rtext; e2; e2 = e2->x_next)
            if (e2->x_next == x)
        {
            e2->x_next = x->x_next;
            break;
        }
    }
    if (rtext_entered == x) rtext_entered = 0;
    freebytes(x->x_buf, x->x_bufsize);
    freebytes(x, sizeof *x);
}

char *rtext_gettag(t_rtext *x)
{
    return (x->x_tag);
}

void rtext_gettext(t_rtext *x, char **buf, int *bufsize)
{
    *buf = x->x_buf;
    *bufsize = x->x_bufsize;
}

void rtext_getseltext(t_rtext *x, char **buf, int *bufsize)
{
    *buf = x->x_buf + x->x_selstart;
    *bufsize = x->x_selend - x->x_selstart;
}

113
114
115
116
117
118
119
120
121
122
123
124
125
/* convert t_text te_type symbol for use as a Tk tag */
static t_symbol *rtext_gettype(t_rtext *x)
{
    switch (x->x_text->te_type) 
    {
    case T_TEXT: return gensym("text");
    case T_OBJECT: return gensym("obj");
    case T_MESSAGE: return gensym("msg");
    case T_ATOM: return gensym("atom");
    }
    return (&s_);
}

Miller Puckette's avatar
Miller Puckette committed
126
127
/* LATER deal with tcl-significant characters */

128
129
130
131
132
133
134
135
136
/* firstone(), lastone()
 *  + returns byte offset of (first|last) occurrence of 'c' in 's[0..n-1]', or
 *    -1 if none was found
 *  + 's' is a raw byte string
 *  + 'c' is a byte value
 *  + 'n' is the length (in bytes) of the prefix of 's' to be searched.
 *  + we could make these functions work on logical characters in utf8 strings,
 *    but we don't really need to...
 */
Miller Puckette's avatar
Miller Puckette committed
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
static int firstone(char *s, int c, int n)
{
    char *s2 = s + n;
    int i = 0;
    while (s != s2)
    {
        if (*s == c) return (i);
        i++;
        s++;
    }
    return (-1);
}

static int lastone(char *s, int c, int n)
{
    char *s2 = s + n;
    while (s2 != s)
    {
        s2--;
        n--;
        if (*s2 == c) return (n);
    }
    return (-1);
}

    /* the following routine computes line breaks and carries out
    some action which could be:
        SEND_FIRST - draw the box  for the first time
        SEND_UPDATE - redraw the updated box
        otherwise - don't draw, just calculate.
167
    Called with *widthp and *heightp as coordinates of
Miller Puckette's avatar
Miller Puckette committed
168
169
170
171
172
    a test point, the routine reports the index of the character found
    there in *indexp.  *widthp and *heightp are set to the width and height
    of the entire text in pixels.
    */

173
174
175
176
177
178
179
180
181
182
   /*-- moo: 
    * + some variables from the original version have been renamed
    * + variables with a "_b" suffix are raw byte strings, lengths, or offsets
    * + variables with a "_c" suffix are logical character lengths or offsets
    *   (assuming valid UTF-8 encoded byte string in x->x_buf)
    * + a fair amount of O(n) computations required to convert between raw byte
    *   offsets (needed by the C side) and logical character offsets (needed by
    *   the GUI)
    */

Miller Puckette's avatar
Miller Puckette committed
183
184
185
186
187
188
189
190
191
192
193
194
195
196
    /* LATER get this and sys_vgui to work together properly,
        breaking up messages as needed.  As of now, there's
        a limit of 1950 characters, imposed by sys_vgui(). */
#define UPBUFSIZE 4000
#define BOXWIDTH 60

/* Older (pre-8.3.4) TCL versions handle text selection differently; this
flag is set from the GUI if this happens.  LATER take this out: early 2006? */

extern int sys_oldtclversion;           

static void rtext_senditup(t_rtext *x, int action, int *widthp, int *heightp,
    int *indexp)
{
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
    //fprintf(stderr,"rtext_senditup <%s>\n", x->x_buf);
    if (x)
    {
        t_float dispx, dispy;
        char smallbuf[200], *tempbuf;
        int outchars_b = 0, nlines = 0, ncolumns = 0,
            pixwide, pixhigh, font, fontwidth, fontheight, findx, findy;
        int reportedindex = 0;
        t_canvas *canvas = glist_getcanvas(x->x_glist);
        int widthspec_c = x->x_text->te_width; // width if any specified
        // width limit in chars
        int widthlimit_c = (widthspec_c ? widthspec_c : BOXWIDTH);
        //fprintf(stderr,"senditup widthlimit_c %d %d\n",
        //    widthspec_c, widthlimit_c);
        int inindex_b = 0; // index location in the buffer
        int inindex_c = 0; // index location in the u8 chars
        int selstart_b = 0, selend_b = 0; // selection start and end
        // buffer size in u8 chars
        int x_bufsize_c = u8_charnum(x->x_buf, x->x_bufsize);
            /* if we're a GOP (the new, "goprect" style) borrow the font size
            from the inside to preserve the spacing */
        if (pd_class(&x->x_text->te_pd) == canvas_class &&
            ((t_glist *)(x->x_text))->gl_isgraph &&
            ((t_glist *)(x->x_text))->gl_goprect)
                font =  glist_getfont((t_glist *)(x->x_text));
        else font = glist_getfont(x->x_glist);
        fontwidth = sys_fontwidth(font);
        fontheight = sys_fontheight(font);
        // calculating x and y in pixels
        findx = (*widthp + (fontwidth/2)) / fontwidth;
        findy = *heightp / fontheight;
        if (x->x_bufsize >= 100)
             tempbuf = (char *)t_getbytes(2 * x->x_bufsize + 1);
        else tempbuf = smallbuf;
        while (x_bufsize_c - inindex_c > 0)
        {
            int inchars_b  = x->x_bufsize - inindex_b;
            int inchars_c  = x_bufsize_c  - inindex_c;
            int maxindex_c =
                (inchars_c > widthlimit_c ? widthlimit_c : inchars_c);
            int maxindex_b = u8_offset(x->x_buf + inindex_b, maxindex_c);
            int eatchar = 1;
            int foundit_b  = firstone(x->x_buf + inindex_b, '\n', maxindex_b);
            int foundit_c;
            //following deals with \v replacement for \n in multiline comments
            int foundit_bv  = firstone(x->x_buf + inindex_b, '\v', maxindex_b);
            //fprintf(stderr,"%d %d <%s>\n", foundit_b, foundit_bv, x->x_buf);
            if ((foundit_bv < foundit_b && foundit_bv != -1) ||
                (foundit_b == -1 && foundit_bv != -1))
                foundit_b = foundit_bv;
            if (foundit_b < 0) //if we did not find an \n
            { 
249
                /* too much text to fit in one line? */
250
251
                if (inchars_c > widthlimit_c)
                {
252
253
254
                    /* is there a space to break the line at?  OK if it's even
                    one byte past the end since in this context we know there's
                    more text */
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
                    foundit_b =
                        lastone(x->x_buf + inindex_b, ' ', maxindex_b + 1);
                    if (foundit_b < 0)
                    {
                        foundit_b = maxindex_b;
                        foundit_c = maxindex_c;
                        eatchar = 0;
                    }
                    else
                        foundit_c = u8_charnum(x->x_buf + inindex_b, foundit_b);
                }
                else
                {
                    foundit_b = inchars_b;
                    foundit_c = inchars_c;
                    eatchar = 0;
                }
            }
            else
                foundit_c = u8_charnum(x->x_buf + inindex_b, foundit_b);

            if (nlines == findy)
            {
                int actualx = (findx < 0 ? 0 :
                    (findx > foundit_c ? foundit_c : findx));
                *indexp = inindex_b + u8_offset(x->x_buf + inindex_b, actualx);
                reportedindex = 1;
            }
            //fprintf(stderr,"eatchar %d <%s>\n", eatchar, tempbuf);
            strncpy(tempbuf+outchars_b, x->x_buf + inindex_b, foundit_b);
            if (x->x_selstart >= inindex_b &&
                x->x_selstart <= inindex_b + foundit_b + eatchar)
                    selstart_b = x->x_selstart + outchars_b - inindex_b;
            if (x->x_selend >= inindex_b &&
                x->x_selend <= inindex_b + foundit_b + eatchar)
                    selend_b = x->x_selend + outchars_b - inindex_b;
            outchars_b += foundit_b;
            inindex_b += (foundit_b + eatchar);
            inindex_c += (foundit_c + eatchar);
            if (inindex_b < x->x_bufsize)
                tempbuf[outchars_b++] = '\n';
            // if we found a row that is longer than previous (total width)
            if (foundit_c > ncolumns)
                ncolumns = foundit_c;
            nlines++;
        }
        // append new line in case we end our input with an \n
        if (x->x_buf[x_bufsize_c - 1] == '\n')
        {
            nlines++;
        }
        if (!reportedindex)
            *indexp = outchars_b;
        dispx = text_xpix(x->x_text, x->x_glist);
        dispy = text_ypix(x->x_text, x->x_glist);
        if (nlines < 1) nlines = 1;
        if (!widthspec_c)
        {
            while (ncolumns < (x->x_text->te_type == T_TEXT ? 1 : 3))
            {
                tempbuf[outchars_b++] = ' ';
                ncolumns++;
            }
        }
        else ncolumns = widthspec_c;
        pixwide = ncolumns * fontwidth + (LMARGIN + RMARGIN);
        pixhigh = nlines * fontheight + (TMARGIN + BMARGIN);

        if (action && x->x_text->te_width && x->x_text->te_type != T_ATOM)
        {
                /* if our width is specified but the "natural" width is the
                same as the specified width, set specified width to zero
                so future text editing will automatically change width.
                Except atoms whose content changes at runtime. */
            int widthwas = x->x_text->te_width, newwidth = 0, newheight = 0,
                newindex = 0;
            x->x_text->te_width = 0;
            rtext_senditup(x, 0, &newwidth, &newheight, &newindex);
            if (newwidth/fontwidth != widthwas)
                x->x_text->te_width = widthwas;
            else x->x_text->te_width = 0;
            //fprintf(stderr,"senditup width %d %d %d\n",
            //    newwidth/fontwidth, widthwas, x->x_text->te_width);
        }
        if (action == SEND_FIRST)
        {
            //fprintf(stderr,"canvas=.x%lx %s\n", (t_int)canvas, tempbuf);
            sys_vgui("pdtk_text_new .x%lx.c {%s %s text} %f %f {%.*s} %d %s\n",
                canvas, x->x_tag, rtext_gettype(x)->s_name,
                dispx + LMARGIN, dispy + TMARGIN,
                outchars_b, tempbuf, sys_hostfontsize(font),
                (glist_isselected(x->x_glist,
                    &x->x_glist->gl_gobj)? "$pd_colors(selection)" :
                        "$pd_colors(text)"));
        }
        else if (action == SEND_UPDATE)
        {
            /*fprintf(stderr,
                "SEND_UPDATE canvas_class=%d isgraph=%d goprect=%d\n",
                (pd_class(&x->x_text->te_pd) == canvas_class ? 1 : 0),
                ((t_glist *)(x->x_text))->gl_isgraph,
                ((t_glist *)(x->x_text))->gl_goprect );*/

            sys_vgui("pdtk_text_set .x%lx.c %s {%.*s}\n",
                canvas, x->x_tag, outchars_b, tempbuf);

            /*if ( pd_class(&x->x_text->te_pd) == canvas_class &&
                ((t_glist *)(x->x_text))->gl_isgraph &&
                (((t_glist *)(x->x_text))->gl_goprect) )
            {
                fprintf(stderr, "do not update outlets\n");
            }
            else */

            if (pixwide != x->x_drawnwidth || pixhigh != x->x_drawnheight) 
                text_drawborder(x->x_text, x->x_glist, x->x_tag,
                    pixwide, pixhigh, 0);
            if (x->x_active)
            {
                if (selend_b > selstart_b)
                {
                    sys_vgui(".x%lx.c select from %s %d\n", canvas, 
                        x->x_tag, u8_charnum(tempbuf, selstart_b));
                    sys_vgui(".x%lx.c select to %s %d\n", canvas, 
                        x->x_tag, u8_charnum(tempbuf, selend_b)
                          + (sys_oldtclversion ? 0 : -1));
                    sys_vgui(".x%lx.c focus \"\"\n", canvas);        
                }
                else
                {
                    sys_vgui(".x%lx.c select clear\n", canvas);
                    sys_vgui(".x%lx.c icursor %s %d\n", canvas, x->x_tag,
                        u8_charnum(tempbuf, selstart_b));
                    sys_vgui(".x%lx.c focus %s\n", canvas, x->x_tag);        
                }
            }
        }
        x->x_drawnwidth = pixwide;
        x->x_drawnheight = pixhigh;
        
        *widthp = pixwide;
        *heightp = pixhigh;
        if (tempbuf != smallbuf)
            t_freebytes(tempbuf, 2 * x->x_bufsize + 1);
    }
Miller Puckette's avatar
Miller Puckette committed
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
}

void rtext_retext(t_rtext *x)
{
    int w = 0, h = 0, indx;
    t_text *text = x->x_text;
    t_freebytes(x->x_buf, x->x_bufsize);
    binbuf_gettext(text->te_binbuf, &x->x_buf, &x->x_bufsize);
        /* special case: for number boxes, try to pare the number down
        to the specified width of the box. */
    if (text->te_width > 0 && text->te_type == T_ATOM &&
        x->x_bufsize > text->te_width)
    {
        t_atom *atomp = binbuf_getvec(text->te_binbuf);
        int natom = binbuf_getnatom(text->te_binbuf);
        int bufsize = x->x_bufsize;
        if (natom == 1 && atomp->a_type == A_FLOAT)
        {
                /* try to reduce size by dropping decimal digits */
            int wantreduce = bufsize - text->te_width;
            char *decimal = 0, *nextchar, *ebuf = x->x_buf + bufsize,
                *s1, *s2;
            for (decimal = x->x_buf; decimal < ebuf; decimal++)
                if (*decimal == '.')
                    break;
            if (decimal >= ebuf)
                goto giveup;
            for (nextchar = decimal + 1; nextchar < ebuf; nextchar++)
                if (*nextchar < '0' || *nextchar > '9')
                    break;
            if (nextchar - decimal - 1 < wantreduce)
                goto giveup;
            for (s1 = nextchar - wantreduce, s2 = s1 + wantreduce;
                s2 < ebuf; s1++, s2++)
                    *s1 = *s2;
            x->x_buf = t_resizebytes(x->x_buf, bufsize, text->te_width);
            bufsize = text->te_width;
            goto done;
        giveup:
                /* give up and bash it to "+" or "-" */
            x->x_buf[0] = (atomp->a_w.w_float < 0 ? '-' : '+');
            x->x_buf = t_resizebytes(x->x_buf, bufsize, 1);
            bufsize = 1;
        }
        else if (bufsize > text->te_width)
        {
            x->x_buf[text->te_width - 1] = '>';
            x->x_buf = t_resizebytes(x->x_buf, bufsize, text->te_width);
            bufsize = text->te_width;
        }
    done:
        x->x_bufsize = bufsize;
    }
    rtext_senditup(x, SEND_UPDATE, &w, &h, &indx);
}

/* find the rtext that goes with a text item */
t_rtext *glist_findrtext(t_glist *gl, t_text *who)
{
459
    t_rtext *x=NULL;
Miller Puckette's avatar
test7    
Miller Puckette committed
460
461
    if (!gl->gl_editor)
        canvas_create_editor(gl);
462
463
464
    if (gl->gl_editor->e_rtext)
        for (x = gl->gl_editor->e_rtext; x && x->x_text != who; x = x->x_next)
            ;
Miller Puckette's avatar
Miller Puckette committed
465
466
467
468
    if (!x) bug("glist_findrtext");
    return (x);
}

469
470
471
472
473
474
/* same as above but without error reporting */
t_rtext *glist_tryfindrtext(t_glist *gl, t_text *who)
{
    t_rtext *x=NULL;
    if (!gl->gl_editor)
        canvas_create_editor(gl);
475
476
477
    if (gl->gl_editor->e_rtext)
        for (x = gl->gl_editor->e_rtext; x && x->x_text != who; x = x->x_next)
            ;
478
479
480
    return (x);
}

Miller Puckette's avatar
Miller Puckette committed
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
int rtext_width(t_rtext *x)
{
    int w = 0, h = 0, indx;
    rtext_senditup(x, SEND_CHECK, &w, &h, &indx);
    return (w);
}

int rtext_height(t_rtext *x)
{
    int w = 0, h = 0, indx;
    rtext_senditup(x, SEND_CHECK, &w, &h, &indx);
    return (h);
}

void rtext_draw(t_rtext *x)
{
    int w = 0, h = 0, indx;
    rtext_senditup(x, SEND_FIRST, &w, &h, &indx);
}

void rtext_erase(t_rtext *x)
{
503
504
    if (x && x->x_glist)
        sys_vgui(".x%lx.c delete %s\n", glist_getcanvas(x->x_glist), x->x_tag);
Miller Puckette's avatar
Miller Puckette committed
505
506
507
508
509
510
511
512
513
514
515
516
}

void rtext_displace(t_rtext *x, int dx, int dy)
{
    sys_vgui(".x%lx.c move %s %d %d\n", glist_getcanvas(x->x_glist), 
        x->x_tag, dx, dy);
}

void rtext_select(t_rtext *x, int state)
{
    t_glist *glist = x->x_glist;
    t_canvas *canvas = glist_getcanvas(glist);
517
518
519
520
521
522
523
524
525
526
527
528
    if (glist_istoplevel(glist))
        sys_vgui(".x%lx.c itemconfigure %s -fill %s\n", canvas, 
            x->x_tag, (state? "$pd_colors(selection)" : "$pd_colors(text)"));
    if (x->x_text->te_pd->c_wb && x->x_text->te_pd->c_wb->w_displacefnwtag)
    {
        if (state)
            sys_vgui(".x%lx.c addtag selected withtag %s\n",
                   glist_getcanvas(glist), x->x_tag);
        else
            sys_vgui(".x%lx.c dtag %s selected\n",
                   glist_getcanvas(glist), x->x_tag);
    }
Miller Puckette's avatar
Miller Puckette committed
529
530
531
532
533
    canvas_editing = canvas;
}

void rtext_activate(t_rtext *x, int state)
{
534
    //fprintf(stderr, "rtext_activate\n");
Miller Puckette's avatar
Miller Puckette committed
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
    int w = 0, h = 0, indx;
    t_glist *glist = x->x_glist;
    t_canvas *canvas = glist_getcanvas(glist);
    if (state)
    {
        sys_vgui(".x%lx.c focus %s\n", canvas, x->x_tag);
        glist->gl_editor->e_textedfor = x;
        glist->gl_editor->e_textdirty = 0;
        x->x_dragfrom = x->x_selstart = 0;
        x->x_selend = x->x_bufsize;
        x->x_active = 1;
    }
    else
    {
        sys_vgui("selection clear .x%lx.c\n", canvas);
        sys_vgui(".x%lx.c focus \"\"\n", canvas);
        if (glist->gl_editor->e_textedfor == x)
            glist->gl_editor->e_textedfor = 0;
        x->x_active = 0;
    }
    rtext_senditup(x, SEND_UPDATE, &w, &h, &indx);
}

558
559
560
561
562
// outputs 1 if found one of the special chars
// this function is used with traversal through rtext below
// using ctrl+left/right and similar shortcuts
static int rtext_compare_special_chars(const char c)
{
563
564
565
        if (c != '\n' && c != '\v' && c != ' ')
            return 0;
        return 1;
566
567
}

Miller Puckette's avatar
Miller Puckette committed
568
569
570
void rtext_key(t_rtext *x, int keynum, t_symbol *keysym)
{
    int w = 0, h = 0, indx, i, newsize, ndel;
571
    //fprintf(stderr,"rtext_key keysym=%s\n", keysym->s_name);
Miller Puckette's avatar
Miller Puckette committed
572
573
574
575
576
577
578
579
580
581
582
583
    if (keynum)
    {
        int n = keynum;
        if (n == '\r') n = '\n';
        if (n == '\b')  /* backspace */
        {
                    /* LATER delete the box if all text is selected...
                    this causes reentrancy problems now. */
            /* if ((!x->x_selstart) && (x->x_selend == x->x_bufsize))
            {
                ....
            } */
584
585
            if (x->x_selstart && (x->x_selstart == x->x_selend))
            {
586
                u8_dec(x->x_buf, &x->x_selstart);
587
588
589
590
591
                if (glist_isvisible(glist_getcanvas(x->x_glist)))
                    sys_vgui("pdtk_canvas_getscroll .x%lx.c\n",
                        (t_int)glist_getcanvas(x->x_glist));
            }
            
Miller Puckette's avatar
Miller Puckette committed
592
593
594
595
        }
        else if (n == 127)      /* delete */
        {
            if (x->x_selend < x->x_bufsize && (x->x_selstart == x->x_selend))
596
                u8_inc(x->x_buf, &x->x_selend);
597
598
599
            if (glist_isvisible(glist_getcanvas(x->x_glist)))
                sys_vgui("pdtk_canvas_getscroll .x%lx.c\n",
                    (t_int)glist_getcanvas(x->x_glist));
Miller Puckette's avatar
Miller Puckette committed
600
601
602
603
604
605
606
607
608
609
610
611
        }
        
        ndel = x->x_selend - x->x_selstart;
        for (i = x->x_selend; i < x->x_bufsize; i++)
            x->x_buf[i- ndel] = x->x_buf[i];
        newsize = x->x_bufsize - ndel;
        x->x_buf = resizebytes(x->x_buf, x->x_bufsize, newsize);
        x->x_bufsize = newsize;

/* at Guenter's suggestion, use 'n>31' to test wither a character might
be printable in whatever 8-bit character set we find ourselves. */

612
613
614
615
616
617
618
/*-- moo:
  ... but test with "<" rather than "!=" in order to accomodate unicode
  codepoints for n (which we get since Tk is sending the "%A" substitution
  for bind <Key>), effectively reducing the coverage of this clause to 7
  bits.  Case n>127 is covered by the next clause.
*/
        if (n == '\n' || (n > 31 && n < 127))
Miller Puckette's avatar
Miller Puckette committed
619
        {
620
            //fprintf(stderr,"return or 31-127\n");
Miller Puckette's avatar
Miller Puckette committed
621
622
623
624
625
626
627
            newsize = x->x_bufsize+1;
            x->x_buf = resizebytes(x->x_buf, x->x_bufsize, newsize);
            for (i = x->x_bufsize; i > x->x_selstart; i--)
                x->x_buf[i] = x->x_buf[i-1];
            x->x_buf[x->x_selstart] = n;
            x->x_bufsize = newsize;
            x->x_selstart = x->x_selstart + 1;
628
629
630
            if (glist_isvisible(glist_getcanvas(x->x_glist)))
                sys_vgui("pdtk_canvas_getscroll .x%lx.c\n",
                    (t_int)glist_getcanvas(x->x_glist));
631
        }
632
633
634
635
        /*--moo: check for unicode codepoints beyond 7-bit ASCII --*/
        else if (n > 127)
        {
            //fprintf(stderr,">127\n");
636
637
638
639
640
641
642
643
644
            int ch_nbytes = u8_wc_nbytes(n);
            newsize = x->x_bufsize + ch_nbytes;
            x->x_buf = resizebytes(x->x_buf, x->x_bufsize, newsize);
            for (i = x->x_bufsize; i > x->x_selstart; i--)
                x->x_buf[i] = x->x_buf[i-1];
            x->x_bufsize = newsize;
            /*-- moo: assume canvas_key() has encoded keysym as UTF-8 */
            strncpy(x->x_buf+x->x_selstart, keysym->s_name, ch_nbytes);
            x->x_selstart = x->x_selstart + ch_nbytes;
Miller Puckette's avatar
Miller Puckette committed
645
646
647
648
649
650
651
        }
        x->x_selend = x->x_selstart;
        x->x_glist->gl_editor->e_textdirty = 1;
    }
    else if (!strcmp(keysym->s_name, "Right"))
    {
        if (x->x_selend == x->x_selstart && x->x_selstart < x->x_bufsize)
652
653
654
655
        {
            u8_inc(x->x_buf, &x->x_selstart);
            x->x_selend = x->x_selstart;
        }
Miller Puckette's avatar
Miller Puckette committed
656
657
        else
            x->x_selstart = x->x_selend;
658
        last_sel = 0;        
Miller Puckette's avatar
Miller Puckette committed
659
660
661
662
    }
    else if (!strcmp(keysym->s_name, "Left"))
    {
        if (x->x_selend == x->x_selstart && x->x_selstart > 0)
663
664
665
666
        {
            u8_dec(x->x_buf, &x->x_selstart);
            x->x_selend = x->x_selstart;
        }
Miller Puckette's avatar
Miller Puckette committed
667
668
        else
            x->x_selend = x->x_selstart;
669
        last_sel = 0;
670
671
672
    }
    else if (!strcmp(keysym->s_name, "ShiftRight"))
    {
673
674
675
676
677
678
679
680
681
682
683
684
        if (!last_sel) last_sel = 2;
        if (last_sel == 1 && x->x_selstart < x->x_selend)
        {
            if (x->x_selstart < x->x_bufsize)
                u8_inc(x->x_buf, &x->x_selstart);        
        }
        else
        {
            last_sel = 2;
            if (x->x_selend < x->x_bufsize)
                u8_inc(x->x_buf, &x->x_selend);
        }
685
686
687
    }
    else if (!strcmp(keysym->s_name, "ShiftLeft"))
    {
688
689
690
        if (!last_sel) last_sel = 1;
        if (last_sel == 2 && x->x_selend > x->x_selstart)
        {
691
            x->x_selend = x->x_selend - 1;
692
693
694
695
696
697
698
        }
        else
        {
            last_sel = 1;
            if (x->x_selstart > 0)
                u8_dec(x->x_buf, &x->x_selstart);
        }
Miller Puckette's avatar
Miller Puckette committed
699
    }
700
701
702
703
    else if (!strcmp(keysym->s_name, "Up"))
    {
        if (x->x_selstart)
            u8_dec(x->x_buf, &x->x_selstart);
704
705
706
        while (x->x_selstart > 0 &&
            (x->x_buf[x->x_selstart] != '\n' &&
                x->x_buf[x->x_selstart] != '\v'))
707
708
            u8_dec(x->x_buf, &x->x_selstart);
        x->x_selend = x->x_selstart;
709
        last_sel = 0;
710
711
712
713
714
715
716
717
718
    }
    else if (!strcmp(keysym->s_name, "Down"))
    {
        while (x->x_selend < x->x_bufsize &&
            (x->x_buf[x->x_selend] != '\n' && x->x_buf[x->x_selend] != '\v'))
            u8_inc(x->x_buf, &x->x_selend);
        if (x->x_selend < x->x_bufsize)
            u8_inc(x->x_buf, &x->x_selend);
        x->x_selstart = x->x_selend;
719
        last_sel = 0;
720
721
    }
    else if (!strcmp(keysym->s_name, "Home"))
Miller Puckette's avatar
Miller Puckette committed
722
723
    {
        if (x->x_selstart)
724
            u8_dec(x->x_buf, &x->x_selstart);
Miller Puckette's avatar
Miller Puckette committed
725
        while (x->x_selstart > 0 && x->x_buf[x->x_selstart] != '\n')
726
            u8_dec(x->x_buf, &x->x_selstart);
Miller Puckette's avatar
Miller Puckette committed
727
        x->x_selend = x->x_selstart;
728
        last_sel = 0;
Miller Puckette's avatar
Miller Puckette committed
729
    }
730
    else if (!strcmp(keysym->s_name, "End"))
Miller Puckette's avatar
Miller Puckette committed
731
732
733
    {
        while (x->x_selend < x->x_bufsize &&
            x->x_buf[x->x_selend] != '\n')
734
            u8_inc(x->x_buf, &x->x_selend);
Miller Puckette's avatar
Miller Puckette committed
735
        if (x->x_selend < x->x_bufsize)
736
            u8_inc(x->x_buf, &x->x_selend);
Miller Puckette's avatar
Miller Puckette committed
737
        x->x_selstart = x->x_selend;
738
        last_sel = 0;
Miller Puckette's avatar
Miller Puckette committed
739
    }
740
    else if (!strcmp(keysym->s_name, "CtrlLeft"))
741
    {
742
743
        //fprintf(stderr,"ctrleft\n");
        /* first find first non-space char going back */
744
        while (x->x_selstart > 0 &&
745
746
747
748
749
750
751
752
753
754
755
            rtext_compare_special_chars(x->x_buf[x->x_selstart-1]))
        {
            //fprintf(stderr,"while 1 <%c>\n", x->x_buf[x->x_selstart-1]);
            u8_dec(x->x_buf, &x->x_selstart);
        }
        /* now go back until you find another space or
           the beginning of the buffer */
        while (x->x_selstart > 0 &&
          !rtext_compare_special_chars(x->x_buf[x->x_selstart-1]))
        {
            //fprintf(stderr,"while 2 <%c>\n", x->x_buf[x->x_selstart-1]);
756
            u8_dec(x->x_buf, &x->x_selstart);
757
758
759
760
761
762
763
764
        }
        if (x->x_buf[x->x_selstart+1] == ' ' &&
            x->x_buf[x->x_selstart] == ' ')
        {
            //fprintf(stderr,"go forward\n");
            u8_inc(x->x_buf, &x->x_selstart);
        }
        x->x_selend = x->x_selstart;
765
    }
766
    else if (!strcmp(keysym->s_name, "CtrlRight"))
767
    {
768
769
770
771
        /* now go forward until you find another space
           or the end of the buffer */
        if (x->x_selend < x->x_bufsize - 1)
            u8_inc(x->x_buf, &x->x_selend);
772
        while (x->x_selend < x->x_bufsize &&
773
          !rtext_compare_special_chars(x->x_buf[x->x_selend]))
774
            u8_inc(x->x_buf, &x->x_selend);
775
        /* now skip all the spaces and land before next word */
776
        /*while (x->x_selend < x->x_bufsize &&
777
          x->x_buf[x->x_selend] == ' ')
778
            u8_inc(x->x_buf, &x->x_selend);
779
780
781
        if (x->x_selend > 0 && x->x_buf[x->x_selend-1] == ' ')
            u8_dec(x->x_buf, &x->x_selend);*/
        x->x_selstart = x->x_selend;
782
    }
783
    else if (!strcmp(keysym->s_name, "CtrlShiftLeft"))
784
785
786
787
788
789
790
791
792
793
794
795
796
797
    {
        //fprintf(stderr,"ctrlshiftleft %d %d %d\n",
        //    last_sel, x->x_selstart, x->x_selend);
        int swap = 0;
        int *target;
        if (!last_sel) last_sel = 1;
        if (last_sel == 2 && x->x_selend > x->x_selstart)
            target = &x->x_selend;
        else
        {
            last_sel = 1;
            target = &x->x_selstart;
        }
        /* first find first non-space char going back */
798
        while (*target > 0 &&
799
800
801
802
803
804
805
806
807
808
809
810
            rtext_compare_special_chars(x->x_buf[*target-1]))
        {
            u8_dec(x->x_buf, target);
            //(*target)--;
        }
        //fprintf(stderr,"%d %d\n", x->x_selstart, x->x_selend);
        /* now go back until you find another space or
           the beginning of the buffer */
        while (*target > 0 &&
            !rtext_compare_special_chars(x->x_buf[*target-1]))
        {
            u8_dec(x->x_buf, target);
811
            //(*target)--;
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
        }
        //fprintf(stderr,"%d %d\n", x->x_selstart, x->x_selend);
        if (x->x_buf[*target+1] == ' ' &&
            x->x_buf[x->x_selstart] == ' ')
        {
            u8_inc(x->x_buf, target);
            //(*target)++;
        }
        //fprintf(stderr,"%d %d\n", x->x_selstart, x->x_selend);
        if (x->x_selstart > x->x_selend)
        {
            swap = x->x_selend;
            x->x_selend = x->x_selstart;
            x->x_selstart = swap;
            last_sel = 1;
        }
    }
829
    else if (!strcmp(keysym->s_name, "CtrlShiftRight"))
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
    {
        //fprintf(stderr,"ctrlshiftright %d %d %d\n",
        //    last_sel, x->x_selstart, x->x_selend);
        int swap = 0;
        int *target;
        if (!last_sel) last_sel = 2;
        if (last_sel == 1 && x->x_selstart < x->x_selend)
            target = &x->x_selstart;
        else
        {
            last_sel = 2;
            target = &x->x_selend;
        }
        //fprintf(stderr,"%d %d\n", x->x_selstart, x->x_selend);
        /* now go forward until you find another space or
           the end of the buffer */
        if (*target < x->x_bufsize - 1)
        {
            //fprintf(stderr,"while 1 <%c>\n", x->x_buf[*target]);
            u8_inc(x->x_buf, target);
            //(*target)++;
        }
        //fprintf(stderr,"%d %d\n", x->x_selstart, x->x_selend);
853
        while (*target < x->x_bufsize &&
854
855
856
857
            !rtext_compare_special_chars(x->x_buf[*target]))
        {
            //fprintf(stderr,"while 2 <%c>\n", x->x_buf[*target]);
            u8_inc(x->x_buf, target);
858
            //(*target)++;
859
860
861
        }
        /* now skip all the spaces and land before next word */

862
        /*while (*target < x->x_bufsize &&
863
864
865
            x->x_buf[*target] == ' ')
        {
            u8_inc(x->x_buf, target);
866
            //(*target)++;
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
        }
        if (*target > 0 && x->x_buf[*target-1] == ' ')
        {
            u8_dec(x->x_buf, target);
            //(*target)--;
        }*/

        //fprintf(stderr,"%d %d\n", x->x_selstart, x->x_selend);
        if (x->x_selstart > x->x_selend)
        {
            swap = x->x_selend;
            x->x_selend = x->x_selstart;
            x->x_selstart = swap;
            last_sel = 2;
        }
        //fprintf(stderr,"%d %d\n", x->x_selstart, x->x_selend);
    }
884

Miller Puckette's avatar
Miller Puckette committed
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
    rtext_senditup(x, SEND_UPDATE, &w, &h, &indx);
}

void rtext_mouse(t_rtext *x, int xval, int yval, int flag)
{
    int w = xval, h = yval, indx;
    rtext_senditup(x, SEND_CHECK, &w, &h, &indx);
    if (flag == RTEXT_DOWN)
    {
        x->x_dragfrom = x->x_selstart = x->x_selend = indx;
    }
    else if (flag == RTEXT_DBL)
    {
        int whereseparator, newseparator;
        x->x_dragfrom = -1;
        whereseparator = 0;
        if ((newseparator = lastone(x->x_buf, ' ', indx)) > whereseparator)
            whereseparator = newseparator+1;
        if ((newseparator = lastone(x->x_buf, '\n', indx)) > whereseparator)
            whereseparator = newseparator+1;
        if ((newseparator = lastone(x->x_buf, ';', indx)) > whereseparator)
            whereseparator = newseparator+1;
        if ((newseparator = lastone(x->x_buf, ',', indx)) > whereseparator)
            whereseparator = newseparator+1;
        x->x_selstart = whereseparator;
        
        whereseparator = x->x_bufsize - indx;
        if ((newseparator =
            firstone(x->x_buf+indx, ' ', x->x_bufsize - indx)) >= 0 &&
                newseparator < whereseparator)
                    whereseparator = newseparator;
        if ((newseparator =
            firstone(x->x_buf+indx, '\n', x->x_bufsize - indx)) >= 0 &&
                newseparator < whereseparator)
                    whereseparator = newseparator;
        if ((newseparator =
            firstone(x->x_buf+indx, ';', x->x_bufsize - indx)) >= 0 &&
                newseparator < whereseparator)
                    whereseparator = newseparator;
        if ((newseparator =
            firstone(x->x_buf+indx, ',', x->x_bufsize - indx)) >= 0 &&
                newseparator < whereseparator)
                    whereseparator = newseparator;
        x->x_selend = indx + whereseparator;
    }
    else if (flag == RTEXT_SHIFT)
    {
        if (indx * 2 > x->x_selstart + x->x_selend)
            x->x_dragfrom = x->x_selstart, x->x_selend = indx;
        else
            x->x_dragfrom = x->x_selend, x->x_selstart = indx;
    }
    else if (flag == RTEXT_DRAG)
    {
        if (x->x_dragfrom < 0)
            return;
        x->x_selstart = (x->x_dragfrom < indx ? x->x_dragfrom : indx);
        x->x_selend = (x->x_dragfrom > indx ? x->x_dragfrom : indx);
    }
    rtext_senditup(x, SEND_UPDATE, &w, &h, &indx);
}