Re: XForms: Word wrapping in text class

From: Angus Leeming (angus.leeming@btopenworld.com)
Date: Mon Apr 07 2003 - 19:36:29 EDT

  • Next message: Angus Leeming: "Re: XForms: Drawing in the browser prehandler (bug?)"

    # To subscribers of the xforms list from Angus Leeming <angus.leeming@btopenworld.com> :

    On Monday 07 April 2003 9:52 pm, jac@casurgica.com wrote:
    > # To subscribers of the xforms list from jac@casurgica.com :
    >
    > Is there an easier way to enable word wrapping in a text object than to go
    > through the string, keep track of the width, and insert line breaks at the
    > appropriate places before setting the text object's label?

    I'm afraid that's what we had to do in LyX. Here's the function so that you
    don't have to re-invent the wheel.

    Regards,
    Angus

    // Take a string and add breaks so that it fits into a desired label width, w
    string formatted(string const & sin, int w, int size, int style)
    {
            string sout;
            if (sin.empty()) return sout;

            string::size_type curpos = 0;
            string line;
            for(;;) {
                    string::size_type const nxtpos1 = sin.find(' ', curpos);
                    string::size_type const nxtpos2 = sin.find('\n', curpos);
                    string::size_type const nxtpos = std::min(nxtpos1, nxtpos2);

                    string const word = nxtpos == string::npos ?
                            sin.substr(curpos) : sin.substr(curpos, nxtpos-curpos);

                    bool const newline = (nxtpos2 != string::npos &&
                                          nxtpos2 < nxtpos1);

                    string const line_plus_word =
                            line.empty() ? word : line + ' ' + word;

                    int const length =
                            fl_get_string_width(style, size,
                                                line_plus_word.c_str(),
                                                int(line_plus_word.length()));

                    if (length >= w) {
                            sout += line + '\n';
                            if (newline) {
                                    sout += word + '\n';
                                    line.erase();
                            } else {
                                    line = word;
                            }

                    } else if (newline) {
                            sout += line_plus_word + '\n';
                            line.erase();

                    } else {
                            if (!line.empty())
                                    line += ' ';
                            line += word;
                    }

                    if (nxtpos == string::npos) {
                            if (!line.empty())
                                    sout += line;
                            break;
                    }

                    curpos = nxtpos+1;
            }

            return sout;
    }

    _________________________________________________
    To unsubscribe, send the message "unsubscribe" to
    xforms-request@bob.usuhs.mil or see
    http://bob.usuhs.mil/mailserv/xforms.html
    XForms Home Page: http://world.std.com/~xforms
    List Archive: http://bob.usuhs.mil/mailserv/list-archives/
    Development: http://savannah.nongnu.org/files/?group=xforms



    This archive was generated by hypermail 2b29 : Mon Apr 07 2003 - 18:33:32 EDT