x11: Don't hardcode limit on lines of text in message boxes.
authorRyan C. Gordon <icculus@icculus.org>
Mon, 15 Oct 2018 00:46:43 -0400
changeset 12330cf64a70ad02a
parent 12329 b7243b9f8fd5
child 12331 43d46306c3ba
x11: Don't hardcode limit on lines of text in message boxes.

Plus other text parsing fixes.

Fixes Bugzilla #4306.
src/video/x11/SDL_x11messagebox.c
     1.1 --- a/src/video/x11/SDL_x11messagebox.c	Sun Oct 14 23:56:56 2018 +0300
     1.2 +++ b/src/video/x11/SDL_x11messagebox.c	Mon Oct 15 00:46:43 2018 -0400
     1.3 @@ -44,7 +44,6 @@
     1.4  #endif
     1.5  
     1.6  #define MAX_BUTTONS             8       /* Maximum number of buttons supported */
     1.7 -#define MAX_TEXT_LINES          32      /* Maximum number of text lines supported */
     1.8  #define MIN_BUTTON_WIDTH        64      /* Minimum button width */
     1.9  #define MIN_DIALOG_WIDTH        200     /* Minimum dialog width */
    1.10  #define MIN_DIALOG_HEIGHT       100     /* Minimum dialog height */
    1.11 @@ -101,7 +100,7 @@
    1.12      int xtext, ytext;                   /* Text position to start drawing at. */
    1.13      int numlines;                       /* Count of Text lines. */
    1.14      int text_height;                    /* Height for text lines. */
    1.15 -    TextLineData linedata[ MAX_TEXT_LINES ];
    1.16 +    TextLineData *linedata;
    1.17  
    1.18      int *pbuttonid;                     /* Pointer to user return buttonid value. */
    1.19  
    1.20 @@ -223,6 +222,18 @@
    1.21      return 0;
    1.22  }
    1.23  
    1.24 +static int
    1.25 +CountLinesOfText(const char *text)
    1.26 +{
    1.27 +    int retval = 0;
    1.28 +    while (text && *text) {
    1.29 +        const char *lf = SDL_strchr(text, '\n');
    1.30 +        retval++;  /* even without an endline, this counts as a line. */
    1.31 +        text = lf ? lf + 1 : NULL;
    1.32 +    }
    1.33 +    return retval;
    1.34 +}
    1.35 +
    1.36  /* Calculate and initialize text and button locations. */
    1.37  static int
    1.38  X11_MessageBoxInitPositions( SDL_MessageBoxDataX11 *data )
    1.39 @@ -237,29 +248,35 @@
    1.40      /* Go over text and break linefeeds into separate lines. */
    1.41      if ( messageboxdata->message && messageboxdata->message[ 0 ] ) {
    1.42          const char *text = messageboxdata->message;
    1.43 -        TextLineData *plinedata = data->linedata;
    1.44 +        const int linecount = CountLinesOfText(text);
    1.45 +        TextLineData *plinedata = (TextLineData *) SDL_malloc(sizeof (TextLineData) * linecount);
    1.46  
    1.47 -        for ( i = 0; i < MAX_TEXT_LINES; i++, plinedata++ ) {
    1.48 +        if (!plinedata) {
    1.49 +            return SDL_OutOfMemory();
    1.50 +        }
    1.51 +
    1.52 +        data->linedata = plinedata;
    1.53 +        data->numlines = linecount;
    1.54 +
    1.55 +        for ( i = 0; i < linecount; i++, plinedata++ ) {
    1.56 +            const char *lf = SDL_strchr( text, '\n' );
    1.57 +            const int length = lf ? ( lf - text ) : SDL_strlen( text );
    1.58              int height;
    1.59 -            char *lf = SDL_strchr( ( char * )text, '\n' );
    1.60  
    1.61 -            data->numlines++;
    1.62 -
    1.63 -            /* Only grab length up to lf if it exists and isn't the last line. */
    1.64 -            plinedata->length = ( lf && ( i < MAX_TEXT_LINES - 1 ) ) ? ( lf - text ) : SDL_strlen( text );
    1.65              plinedata->text = text;
    1.66  
    1.67 -            GetTextWidthHeight( data, text, plinedata->length, &plinedata->width, &height );
    1.68 +            GetTextWidthHeight( data, text, length, &plinedata->width, &height );
    1.69  
    1.70              /* Text and widths are the largest we've ever seen. */
    1.71              data->text_height = IntMax( data->text_height, height );
    1.72              text_width_max = IntMax( text_width_max, plinedata->width );
    1.73  
    1.74 +            plinedata->length = length;
    1.75              if (lf && (lf > text) && (lf[-1] == '\r')) {
    1.76                  plinedata->length--;
    1.77              }
    1.78  
    1.79 -            text += plinedata->length + 1;
    1.80 +            text += length + 1;
    1.81  
    1.82              /* Break if there are no more linefeeds. */
    1.83              if ( !lf )
    1.84 @@ -369,6 +386,8 @@
    1.85          X11_XCloseDisplay( data->display );
    1.86          data->display = NULL;
    1.87      }
    1.88 +
    1.89 +    SDL_free(data->linedata);
    1.90  }
    1.91  
    1.92  /* Create and set up our X11 dialog box indow. */