rem: if (tb && !tb->graphic) { WMFont *f = tb->d.font; len = tb->sections[s].end - tb->sections[s].begin; text = &(tb->text[tb->sections[s].begin]);  _w = x - tb->sections[s].x; pos = 0;  while (pos<len && WMWidthOfString(f, text, pos+1) <  _w) pos++;  tPtr->cursor.x = tb->sections[s].x + (pos? WMWidthOfString(f, text, pos) : 0);  pos += tb->sections[s].begin;
add: if (tb) { if(tb->graphic) { int gw = (tb->object ? WMWidgetWidth(tb->d.widget) : tb->d.pixmap->width);  tPtr->cursor.x = tb->sections[0].x;  if(x > tPtr->cursor.x + gw/2) { pos = 1; printf("here x%d:  %d\n", x, tPtr->cursor.x + gw/2); tPtr->cursor.x += gw; } else pos = 0;  s = 0; goto _doneH;  } else { WMFont *f = tb->d.font; len = tb->sections[s].end - tb->sections[s].begin; text = &(tb->text[tb->sections[s].begin]);  _w = x - tb->sections[s].x; pos = 0;  while (pos<len && WMWidthOfString(f, text, pos+1) <  _w) pos++;  tPtr->cursor.x = tb->sections[s].x + (pos? WMWidthOfString(f, text, pos) : 0);  pos += tb->sections[s].begin; } }
context: cursorToTextPosition(Text *tPtr, int x, int y){    TextBlock *tb = NULL;    int done=False, s, pos, len, _w, _y, dir=1; /* 1 == "down" */    char *text;    if(tPtr->flags.needsLayOut)        layOutDocument(tPtr);    y += (tPtr->vpos - tPtr->visible.y);    if (y<0)         y = 0;    x -= (tPtr->visible.x - 2);     if (x<0)         x=0;    /* clicked is relative to document, not window... */    tPtr->clicked.x = x;    tPtr->clicked.y = y;    if (! (tb = tPtr->currentTextBlock)) {        if (! (tb = tPtr->firstTextBlock)) {            tPtr->tpos = 0;            tPtr->cursor.h = tPtr->dFont->height;            tPtr->cursor.y = 2;            tPtr->cursor.x = 2;            return;        }    }    /* first, which direction? Most likely, newly clicked         position will be close to previous */    if(!updateStartForCurrentTextBlock(tPtr, x, y, &dir, tb))        return;    s = (dir? 0 : tb->nsections-1);    if ( y >= tb->sections[s]._y         && y <= tb->sections[s]._y + tb->sections[s].h) {         goto _doneV;    }    /* get the first (or last) section of the TextBlock that        lies about the vertical click point */    done = False;    while (!done && tb) {         if (tPtr->flags.monoFont && tb->graphic) {            if( (dir?tb->next:tb->prior))                tb = (dir?tb->next:tb->prior);            continue;        }        s = (dir? 0 : tb->nsections-1);        while (!done && (dir? (s<tb->nsections) : (s>=0) )) {            if ( (dir?  (y <= tb->sections[s]._y + tb->sections[s].h) :                    ( y >= tb->sections[s]._y ) ) ) {                    done = True;                } else {                    dir? s++ : s--;                }        }                            if (!done) {            if ( (dir? tb->next : tb->prior))  {                tb = (dir ? tb->next : tb->prior);            } else {                pos = tb->used;                break; /* goto _doneH; */            }        }    }        if (s<0 || s>=tb->nsections) {        s = (dir? tb->nsections-1 : 0);    }_doneV:    /* we have the line, which TextBlock on that line is it? */    pos = (dir?0:tb->sections[s].begin);    if (tPtr->flags.monoFont && tb->graphic) {        TextBlock *hold = tb;        tb = getFirstNonGraphicBlockFor(hold, dir);        if(!tb) {            tPtr->tpos = 0;            tb = hold;            s = 0;            goto _doNothing;        }    }            if(tb->blank)         _w = 0;    _y = tb->sections[s]._y;    while (tb) {         if (tPtr->flags.monoFont && tb->graphic) {            tb = (dir ? tb->next : tb->prior);            continue;        }        if (dir) {            if (tb->graphic) {                if(tb->object)                    _w = WMWidgetWidth(tb->d.widget)-5;                else                     _w = tb->d.pixmap->width-5;            } else {                text = &(tb->text[tb->sections[s].begin]);                len = tb->sections[s].end - tb->sections[s].begin;                _w = WMWidthOfString(tb->d.font, text, len);                if (tb->sections[s].x + _w >= x)                    break;                            }        }  else {             if (tb->sections[s].x  <= x)                break;        }        if ((dir? tb->next : tb->prior)) {            TextBlock *nxt = (dir? tb->next : tb->prior);            if (tPtr->flags.monoFont && nxt->graphic) {                nxt = getFirstNonGraphicBlockFor(nxt, dir);                if (!nxt) {                    pos = (dir?0:tb->sections[s].begin);                    tPtr->cursor.x = tb->sections[s].x;                    goto _doneH;                }            }            if (_y != nxt->sections[dir?0:nxt->nsections-1]._y) {                /* this must be the last/first on this line. stop */                pos = (dir? tb->sections[s].end : 0);                tPtr->cursor.x = tb->sections[s].x;                if (!tb->blank) {                     if (tb->graphic) {                        if(tb->object)                            tPtr->cursor.x += WMWidgetWidth(tb->d.widget);                        else                             tPtr->cursor.x += tb->d.pixmap->width;                    } else if (pos > tb->sections[s].begin) {                        tPtr->cursor.x +=                         WMWidthOfString(tb->d.font,                             &(tb->text[tb->sections[s].begin]),                            pos - tb->sections[s].begin);                    }                }                goto _doneH;            }        }        if ( (dir? tb->next : tb->prior))  {            tb = (dir ? tb->next : tb->prior);        } else {             done = True;            break;        }        if (tb)             s = (dir? 0 : tb->nsections-1);    }            /* we have said TextBlock, now where within it? */    if (tb && !tb->graphic) {        WMFont *f = tb->d.font;        len = tb->sections[s].end - tb->sections[s].begin;        text = &(tb->text[tb->sections[s].begin]);        _w = x - tb->sections[s].x;        pos = 0;        while (pos<len && WMWidthOfString(f, text, pos+1) <  _w)            pos++;        tPtr->cursor.x = tb->sections[s].x +             (pos? WMWidthOfString(f, text, pos) : 0);        pos += tb->sections[s].begin;_doneH:          tPtr->tpos = (pos<tb->used)? pos : tb->used;    }        _doNothing:    if (!tb)        printf("...for this app will surely crash :-)\n");    tPtr->currentTextBlock = tb;    tPtr->cursor.h = tb->sections[s].h;    tPtr->cursor.y = tb->sections[s]._y;        /* scroll the bars if the cursor is not visible */    if(tPtr->flags.editable && tPtr->cursor.x != -23) {         if(tPtr->cursor.y+tPtr->cursor.h             > tPtr->vpos+tPtr->visible.y+tPtr->visible.h) {             tPtr->vpos +=                 (tPtr->cursor.y+tPtr->cursor.h+10                - (tPtr->vpos+tPtr->visible.y+tPtr->visible.h));            updateScrollers(tPtr);        } else if(tPtr->cursor.y < tPtr->vpos+tPtr->visible.y) {             tPtr->vpos -= (tPtr->vpos+tPtr->visible.y-tPtr->cursor.y);            updateScrollers(tPtr);        }                }}