--- hatari/src/gui-sdl/dlgAlert.c 2019/04/01 07:13:17 1.1.1.3 +++ hatari/src/gui-sdl/dlgAlert.c 2019/04/01 07:13:51 1.1.1.4 @@ -16,7 +16,7 @@ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License (gpl.txt) for more details. */ -const char DlgAlert_rcsid[] = "Hatari $Id: dlgAlert.c,v 1.1.1.3 2019/04/01 07:13:17 root Exp $"; +const char DlgAlert_rcsid[] = "Hatari $Id: dlgAlert.c,v 1.1.1.4 2019/04/01 07:13:51 root Exp $"; #include @@ -50,62 +50,73 @@ static SGOBJ alertdlg[] = { SGTEXT, 0, 0, 1,2, 50,1, dlglines[1] }, { SGTEXT, 0, 0, 1,3, 50,1, dlglines[2] }, { SGTEXT, 0, 0, 1,4, 50,1, dlglines[3] }, - { SGBUTTON, 0, 0, 5,5, 8,1, "OK" }, - { SGBUTTON, 0, 0, 24,5, 8,1, "Cancel" }, + { SGBUTTON, SG_DEFAULT, 0, 5,5, 8,1, "OK" }, + { SGBUTTON, SG_CANCEL, 0, 24,5, 8,1, "Cancel" }, { -1, 0, 0, 0,0, 0,0, NULL } }; /*-----------------------------------------------------------------------*/ /* - Breaks long string to several strings of max_width, divided by '\0' - and returns the number of lines you need to display the strings. + Breaks long string to several strings of max_width, divided by '\0', + sets text_width to the longest line width and returns the number of lines + you need to display the strings. */ -static int DlgAlert_FormatTextToBox(char *text, int max_width) +static int DlgAlert_FormatTextToBox(char *text, int max_width, int *text_width) { + int columns = 0; int lines = 1; int txtlen; - char *p; /* pointer to begin of actual line */ - char *q; /* pointer to start of next search */ - char *llb; /* pointer to last place suitable for breaking the line */ - char *txtend; /* pointer to end of the text */ + char *p; /* pointer to begin of actual line */ + char *q; /* pointer to start of next search */ + char *llb; /* pointer to last place suitable for breaking the line */ + char *txtend; /* pointer to end of the text */ txtlen = strlen(text); q = p = text; - llb = text-1; /* pointer to last line break */ + llb = text-1; /* pointer to last line break */ txtend = text + txtlen; - if (txtlen > max_width) + if (txtlen <= max_width) { - while(q < txtend) /* q was last place suitable for breaking */ + *text_width = txtlen; + return lines; + } + + while(q < txtend) /* q was last place suitable for breaking */ + { + char *r = strpbrk(q, " \t/\\\n"); /* find next suitable place for the break */ + if (r == NULL) + r = txtend; /* if there's no place then point to the end */ + + if ((r-p) <= max_width && *r != '\n') /* '\n' is always used for breaking */ + { + llb = r; /* remember new place suitable for breaking */ + q++; + if ((r-p) > columns) + columns = r - p; + continue; /* search again */ + } + + if ((r-p) > max_width) /* too long line already? */ { - char *r = strpbrk(q, " \t/\\\n"); /* find next suitable place for the break */ - if (r == NULL) - r = txtend; /* if there's no place then point to the end */ - - if ((r-p) < max_width && *r != '\n') /* '\n' is always used for breaking */ - { - llb = r; /* remember new place suitable for breaking */ - q++; - continue; /* search again */ - } - - if ((r-p) > max_width) /* too long line already? */ - { - if (p > llb) /* bad luck - no place for the delimiter. Let's do it the strong way */ - llb = p + max_width; /* we loose one character */ - } - else - llb = r; /* break in this place */ - - *llb = '\0'; /* BREAK */ - p = q = llb + 1; /* next line begins here */ - lines++; /* increment line counter */ + if (p > llb) /* bad luck - no place for the delimiter. Let's do it the strong way */ + llb = p + max_width; /* we loose one character */ } + else + llb = r; /* break from previous delimiter */ + + *llb = '\0'; /* BREAK */ + if ((llb-p) > columns) + columns = llb - p; /* longest line so far */ + p = q = llb + 1; /* next line begins here */ + lines++; /* increment line counter */ } - return lines; /* return line counter */ + *text_width = columns; + + return lines; /* return line counter */ } @@ -116,21 +127,24 @@ static int DlgAlert_FormatTextToBox(char */ static int DlgAlert_ShowDlg(const char *text) { + static int maxlen = sizeof(dlglines[0])-1; char *t = (char *)malloc(strlen(text)+1); char *orig_t = t; - static int maxlen = sizeof(dlglines[0])-1; - int lines; - int i; + int lines, i, len, offset; BOOL bOldMouseVisibility; + int nOldMouseX, nOldMouseY; strcpy(t, text); - lines = DlgAlert_FormatTextToBox(t, maxlen); + lines = DlgAlert_FormatTextToBox(t, maxlen, &len); + offset = (maxlen-len)/2; for(i=0; i