#include #include #include Point wordwrap(char* m, Font *font, Point pi, int margin) { /* Strategy for word wrapping: * Copy the message so we don't stomp on the original (which we need for redraw). * Split on newlines with getfields(3); do the following for each field: * split on space or newline, get the next word. * use stringsize(3) to see if it fits in our rectangle; if so, print it and a space. * if not, if we're at the beginning of the box, print it anyway. * otherwise, move down a line and then print it. * repeat. * Return the next point we'd try to draw at if we had more. */ int i, j, nlines, nwords; char *buf, *lines[100], *words[500], *word; Point p; buf = strdup(m); p = pi; nlines = getfields(buf, lines, nelem(lines), 1, "\n"); for(i=0; i1) word = strcat(strdup(words[j]), " "); else word = strdup(words[j]); if(ptinrect(addpt(p, Pt(stringwidth(font, word)+margin, 0)), screen->r) != 1) { p = Pt(screen->r.min.x+margin, p.y + stringsize(font, " ").y); } p = string(screen, p, display->white, ZP, font, word); } if(i+1 < nlines) p = Pt(screen->r.min.x+margin, p.y + stringsize(font, " ").y); } return p; }