Esempio n. 1
0
static void rtext_senditup(t_rtext *x, int action, int *widthp, int *heightp,
    int *indexp)
{
    //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
            { 
                /* too much text to fit in one line? */
                if (inchars_c > widthlimit_c)
                {
                    /* 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 */
                    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);
    }
}
Esempio n. 2
0
File: g_rtext.c Progetto: cviejo/mPD
static void rtext_senditup(t_rtext *x, int action, int *widthp, int *heightp,
    int *indexp)
{
    //fprintf(stderr,"rtext_senditup <%s>\n", x->x_buf);
    if (x)
    {
        t_float dispx, dispy;
        char smallbuf[200] = { '\0' }, *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);
        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
        //fprintf(stderr,"buf = <%s> | last 2 chars = %d %d\n", x->x_buf, x->x_buf[x->x_bufsize-1], x->x_buf[x->x_bufsize]);
        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;
            //fprintf(stderr, "firstone <%s> inindex_b=%d maxindex_b=%d\n", x->x_buf + inindex_b, inindex_b, maxindex_b);
            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);
            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
            { 
                /* too much text to fit in one line? */
                if (inchars_c > widthlimit_c)
                {
                    /* 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 */
                    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;
            }
            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_bufsize_c > 0 && (x->x_buf[x_bufsize_c - 1] == '\n' || x->x_buf[x_bufsize_c - 1] == '\v'))
        {
            nlines++;
            tempbuf[outchars_b++] = '\n';
            //tempbuf[outchars_b] = '\0';
            //outchars_b++;
        }
        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;

        // add a null character at the end of the string --for u8_charnum
        // _and_ for the new gui_vmess calls which don't use the %.*s syntax.
        // Because of the way binbuf_gettext works, we should always have
        // a space before this null character. But I'm not 100% sure this
        // space is guaranteed to be there, so we'll just filter it out on
        // the GUI side for now.
        tempbuf[outchars_b++] = '\0';

        pixwide = ncolumns * fontwidth + (LMARGIN + RMARGIN);
        pixhigh = nlines * fontheight + (TMARGIN + BMARGIN);
        //printf("outchars_b=%d bufsize=%d %d\n", outchars_b, x->x_bufsize, x->x_buf[outchars_b]);

        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;
        }
        if (action == SEND_FIRST)
        {
            //fprintf(stderr,"send_first rtext=%lx t_text=%lx\n", x, x->x_text);
            gui_vmess("gui_text_new", "xssiiisi",
                canvas, x->x_tag, rtext_gettype(x)->s_name,
                glist_isselected(x->x_glist, ((t_gobj*)x->x_text)),
                LMARGIN,
                fontheight,
                tempbuf,
                sys_hostfontsize(font));
               
        }
        else if (action == SEND_UPDATE)
        {
            gui_vmess("gui_text_set", "xss", canvas, x->x_tag, tempbuf);

            // We add the check for T_MESSAGE below so that the box border
            // gets resized correctly using our interim event handling in
            // pd_canvas.html.  I could remove the conditional, but
            // this part of Pd is convoluted enough that I'm not sure
            // if there'd be any side effects.
            if (glist_isvisible(x->x_glist) && (pixwide != x->x_drawnwidth ||
                pixhigh != x->x_drawnheight ||
                x->x_text->te_type == T_MESSAGE)) 
                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);
    }
    //printf("END\n");
}