src/video/x11/SDL_x11messagebox.c
changeset 12330 cf64a70ad02a
parent 11811 5d94cb6b24d3
child 12503 806492103856
equal deleted inserted replaced
12329:b7243b9f8fd5 12330:cf64a70ad02a
    42 #include <unistd.h>
    42 #include <unistd.h>
    43 #include <errno.h>
    43 #include <errno.h>
    44 #endif
    44 #endif
    45 
    45 
    46 #define MAX_BUTTONS             8       /* Maximum number of buttons supported */
    46 #define MAX_BUTTONS             8       /* Maximum number of buttons supported */
    47 #define MAX_TEXT_LINES          32      /* Maximum number of text lines supported */
       
    48 #define MIN_BUTTON_WIDTH        64      /* Minimum button width */
    47 #define MIN_BUTTON_WIDTH        64      /* Minimum button width */
    49 #define MIN_DIALOG_WIDTH        200     /* Minimum dialog width */
    48 #define MIN_DIALOG_WIDTH        200     /* Minimum dialog width */
    50 #define MIN_DIALOG_HEIGHT       100     /* Minimum dialog height */
    49 #define MIN_DIALOG_HEIGHT       100     /* Minimum dialog height */
    51 
    50 
    52 static const char g_MessageBoxFontLatin1[] = "-*-*-medium-r-normal--0-120-*-*-p-0-iso8859-1";
    51 static const char g_MessageBoxFontLatin1[] = "-*-*-medium-r-normal--0-120-*-*-p-0-iso8859-1";
    99     XFontSet font_set;                  /* for UTF-8 systems */
    98     XFontSet font_set;                  /* for UTF-8 systems */
   100     XFontStruct *font_struct;           /* Latin1 (ASCII) fallback. */
    99     XFontStruct *font_struct;           /* Latin1 (ASCII) fallback. */
   101     int xtext, ytext;                   /* Text position to start drawing at. */
   100     int xtext, ytext;                   /* Text position to start drawing at. */
   102     int numlines;                       /* Count of Text lines. */
   101     int numlines;                       /* Count of Text lines. */
   103     int text_height;                    /* Height for text lines. */
   102     int text_height;                    /* Height for text lines. */
   104     TextLineData linedata[ MAX_TEXT_LINES ];
   103     TextLineData *linedata;
   105 
   104 
   106     int *pbuttonid;                     /* Pointer to user return buttonid value. */
   105     int *pbuttonid;                     /* Pointer to user return buttonid value. */
   107 
   106 
   108     int button_press_index;             /* Index into buttondata/buttonpos for button which is pressed (or -1). */
   107     int button_press_index;             /* Index into buttondata/buttonpos for button which is pressed (or -1). */
   109     int mouse_over_index;               /* Index into buttondata/buttonpos for button mouse is over (or -1). */
   108     int mouse_over_index;               /* Index into buttondata/buttonpos for button mouse is over (or -1). */
   221     }
   220     }
   222 
   221 
   223     return 0;
   222     return 0;
   224 }
   223 }
   225 
   224 
       
   225 static int
       
   226 CountLinesOfText(const char *text)
       
   227 {
       
   228     int retval = 0;
       
   229     while (text && *text) {
       
   230         const char *lf = SDL_strchr(text, '\n');
       
   231         retval++;  /* even without an endline, this counts as a line. */
       
   232         text = lf ? lf + 1 : NULL;
       
   233     }
       
   234     return retval;
       
   235 }
       
   236 
   226 /* Calculate and initialize text and button locations. */
   237 /* Calculate and initialize text and button locations. */
   227 static int
   238 static int
   228 X11_MessageBoxInitPositions( SDL_MessageBoxDataX11 *data )
   239 X11_MessageBoxInitPositions( SDL_MessageBoxDataX11 *data )
   229 {
   240 {
   230     int i;
   241     int i;
   235     const SDL_MessageBoxData *messageboxdata = data->messageboxdata;
   246     const SDL_MessageBoxData *messageboxdata = data->messageboxdata;
   236 
   247 
   237     /* Go over text and break linefeeds into separate lines. */
   248     /* Go over text and break linefeeds into separate lines. */
   238     if ( messageboxdata->message && messageboxdata->message[ 0 ] ) {
   249     if ( messageboxdata->message && messageboxdata->message[ 0 ] ) {
   239         const char *text = messageboxdata->message;
   250         const char *text = messageboxdata->message;
   240         TextLineData *plinedata = data->linedata;
   251         const int linecount = CountLinesOfText(text);
   241 
   252         TextLineData *plinedata = (TextLineData *) SDL_malloc(sizeof (TextLineData) * linecount);
   242         for ( i = 0; i < MAX_TEXT_LINES; i++, plinedata++ ) {
   253 
       
   254         if (!plinedata) {
       
   255             return SDL_OutOfMemory();
       
   256         }
       
   257 
       
   258         data->linedata = plinedata;
       
   259         data->numlines = linecount;
       
   260 
       
   261         for ( i = 0; i < linecount; i++, plinedata++ ) {
       
   262             const char *lf = SDL_strchr( text, '\n' );
       
   263             const int length = lf ? ( lf - text ) : SDL_strlen( text );
   243             int height;
   264             int height;
   244             char *lf = SDL_strchr( ( char * )text, '\n' );
   265 
   245 
       
   246             data->numlines++;
       
   247 
       
   248             /* Only grab length up to lf if it exists and isn't the last line. */
       
   249             plinedata->length = ( lf && ( i < MAX_TEXT_LINES - 1 ) ) ? ( lf - text ) : SDL_strlen( text );
       
   250             plinedata->text = text;
   266             plinedata->text = text;
   251 
   267 
   252             GetTextWidthHeight( data, text, plinedata->length, &plinedata->width, &height );
   268             GetTextWidthHeight( data, text, length, &plinedata->width, &height );
   253 
   269 
   254             /* Text and widths are the largest we've ever seen. */
   270             /* Text and widths are the largest we've ever seen. */
   255             data->text_height = IntMax( data->text_height, height );
   271             data->text_height = IntMax( data->text_height, height );
   256             text_width_max = IntMax( text_width_max, plinedata->width );
   272             text_width_max = IntMax( text_width_max, plinedata->width );
   257 
   273 
       
   274             plinedata->length = length;
   258             if (lf && (lf > text) && (lf[-1] == '\r')) {
   275             if (lf && (lf > text) && (lf[-1] == '\r')) {
   259                 plinedata->length--;
   276                 plinedata->length--;
   260             }
   277             }
   261 
   278 
   262             text += plinedata->length + 1;
   279             text += length + 1;
   263 
   280 
   264             /* Break if there are no more linefeeds. */
   281             /* Break if there are no more linefeeds. */
   265             if ( !lf )
   282             if ( !lf )
   266                 break;
   283                 break;
   267         }
   284         }
   367         }
   384         }
   368 
   385 
   369         X11_XCloseDisplay( data->display );
   386         X11_XCloseDisplay( data->display );
   370         data->display = NULL;
   387         data->display = NULL;
   371     }
   388     }
       
   389 
       
   390     SDL_free(data->linedata);
   372 }
   391 }
   373 
   392 
   374 /* Create and set up our X11 dialog box indow. */
   393 /* Create and set up our X11 dialog box indow. */
   375 static int
   394 static int
   376 X11_MessageBoxCreateWindow( SDL_MessageBoxDataX11 *data )
   395 X11_MessageBoxCreateWindow( SDL_MessageBoxDataX11 *data )