Annotation of hatari/src/gui-sdl/dlgAlert.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  * Hatari - dlgAlert.c - AES-like AlertBox 
                      3:  *
                      4:  * Based on dlgAlert.cpp from the emulator ARAnyM,
                      5:  * Copyright (c) 2004 Petr Stehlik of ARAnyM dev team
                      6:  *
                      7:  * Adaptation to Hatari by Thomas Huth.
                      8:  *
                      9:  * This file is free software; you can redistribute it and/or modify
                     10:  * it under the terms of the GNU General Public License as published by
                     11:  * the Free Software Foundation; either version 2 of the License, or
                     12:  * (at your option) any later version.
                     13:  *
                     14:  * This file is distributed in the hope that it will be useful,
                     15:  * but WITHOUT ANY WARRANTY; without even the implied warranty of
                     16:  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     17:  * GNU General Public License for more details.
                     18:  *
                     19:  * You should have received a copy of the GNU General Public License
                     20:  * along with ARAnyM; if not, write to the Free Software
                     21:  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     22:  */
                     23: char DlgAlert_rcsid[] = "Hatari $Id: dlgAlert.c,v 1.1 2004/10/31 17:32:50 thothy Exp $";
                     24: 
                     25: #include <string.h>
                     26: 
                     27: #include "main.h"
                     28: #include "dialog.h"
                     29: #include "sdlgui.h"
                     30: 
                     31: 
                     32: #define MAX_LINES 4
                     33: 
                     34: static char dlglines[MAX_LINES][50+1];
                     35: 
                     36: 
                     37: #define DLGALERT_OK       5
                     38: #define DLGALERT_CANCEL   6
                     39: 
                     40: /* The "Alert"-dialog: */
                     41: static SGOBJ alertdlg[] =
                     42: {
                     43:        { SGBOX, 0, 0, 0,0, 52,7, NULL },
                     44:        { SGTEXT, 0, 0, 1,1, 50,1, dlglines[0] },
                     45:        { SGTEXT, 0, 0, 1,2, 50,1, dlglines[1] },
                     46:        { SGTEXT, 0, 0, 1,3, 50,1, dlglines[2] },
                     47:        { SGTEXT, 0, 0, 1,4, 50,1, dlglines[3] },
                     48:        { SGBUTTON, 0, 0, 5,5, 8,1, "OK" },
                     49:        { SGBUTTON, 0, 0, 24,5, 8,1, "Cancel" },
                     50:        { -1, 0, 0, 0,0, 0,0, NULL }
                     51: };
                     52: 
                     53: 
                     54: /*-----------------------------------------------------------------------*/
                     55: /*
                     56:    Breaks long string to several strings of max_width, divided by '\0'
                     57:    and returns the number of lines you need to display the strings.
                     58: */
                     59: static int DlgAlert_FormatTextToBox(char *text, int max_width)
                     60: {
                     61:        int lines = 1;
                     62:        int txtlen;
                     63:        char *p;                /* pointer to begin of actual line */
                     64:        char *q;                /* pointer to start of next search */
                     65:        char *llb;              /* pointer to last place suitable for breaking the line */
                     66:        char *txtend;   /* pointer to end of the text */
                     67: 
                     68:        txtlen = strlen(text);
                     69: 
                     70:        q = p = text;
                     71:        llb = text-1;   /* pointer to last line break */
                     72:        txtend = text + txtlen;
                     73: 
                     74:        if (txtlen > max_width)
                     75:        {
                     76:                while(q < txtend)                                               /* q was last place suitable for breaking */
                     77:                {
                     78:                        char *r = strpbrk(q, " \t/\\\n");       /* find next suitable place for the break */
                     79:                        if (r == NULL)
                     80:                                r = txtend;                                             /* if there's no place then point to the end */
                     81: 
                     82:                        if ((r-p) < max_width && *r != '\n')    /* '\n' is always used for breaking */
                     83:                        {
                     84:                                llb = r;                                                /* remember new place suitable for breaking */
                     85:                                q++;
                     86:                                continue;                                               /* search again */
                     87:                        }
                     88: 
                     89:                        if ((r-p) > max_width)                          /* too long line already? */
                     90:                        {
                     91:                                if (p > llb)                                    /* bad luck - no place for the delimiter. Let's do it the strong way */
                     92:                                        llb = p + max_width;            /* we loose one character */
                     93:                        }
                     94:                        else
                     95:                                llb = r;                        /* break in this place */
                     96: 
                     97:                        *llb = '\0';                    /* BREAK */
                     98:                        p = q = llb + 1;                /* next line begins here */
                     99:                        lines++;                                /* increment line counter */
                    100:                }
                    101:        }
                    102: 
                    103:        return lines;                                   /* return line counter */
                    104: }
                    105: 
                    106: 
                    107: 
                    108: /*-----------------------------------------------------------------------*/
                    109: /*
                    110:   Show the "alert" dialog. Return TRUE if user pressed "OK".
                    111: */
                    112: static int DlgAlert_ShowDlg(const char *text)
                    113: {
                    114:        char *t = (char *)malloc(strlen(text)+1);
                    115:        char *orig_t = t;
                    116:        static int maxlen = sizeof(dlglines[0])-1;
                    117:        int lines;
                    118:        int i;
                    119:        BOOL bOldMouseVisibility;
                    120: 
                    121:        strcpy(t, text);
                    122:        lines = DlgAlert_FormatTextToBox(t, maxlen);
                    123: 
                    124:        for(i=0; i<MAX_LINES; i++)
                    125:        {
                    126:                if (i < lines)
                    127:                {
                    128:                        strcpy(dlglines[i], t);
                    129:                        t += strlen(t)+1;
                    130:                }
                    131:                else
                    132:                {
                    133:                        dlglines[i][0] = '\0';
                    134:                }
                    135:        }
                    136: 
                    137:        free(orig_t);
                    138: 
                    139:        SDLGui_PrepareFont();
                    140:        SDLGui_CenterDlg(alertdlg);
                    141: 
                    142:        bOldMouseVisibility = SDL_ShowCursor(SDL_QUERY);
                    143:        SDL_ShowCursor(SDL_ENABLE);
                    144: 
                    145:        i = SDLGui_DoDialog(alertdlg);
                    146: 
                    147:        SDL_ShowCursor(bOldMouseVisibility);
                    148: 
                    149:        return (i == DLGALERT_OK);
                    150: }
                    151: 
                    152: 
                    153: /*-----------------------------------------------------------------------*/
                    154: /*
                    155:   Show a "notice" dialog: (only one button)
                    156: */
                    157: int DlgAlert_Notice(const char *text)
                    158: {
                    159:        /* Hide "cancel" button: */
                    160:        alertdlg[DLGALERT_CANCEL].type = SGTEXT;
                    161:        alertdlg[DLGALERT_CANCEL].txt = "";
                    162:        alertdlg[DLGALERT_CANCEL].w = alertdlg[DLGALERT_CANCEL].w = 0;
                    163: 
                    164:        /* Adjust button position: */
                    165:        alertdlg[DLGALERT_OK].x = (alertdlg[0].w - alertdlg[DLGALERT_OK].w) / 2;
                    166: 
                    167:        return DlgAlert_ShowDlg(text);
                    168: }
                    169: 
                    170: 
                    171: /*-----------------------------------------------------------------------*/
                    172: /*
                    173:   Show a "query" dialog: (two buttons)
                    174: */
                    175: int DlgAlert_Query(const char *text)
                    176: {
                    177:        /* Show "cancel" button: */
                    178:        alertdlg[DLGALERT_CANCEL].type = SGBUTTON;
                    179:        alertdlg[DLGALERT_CANCEL].txt = "Cancel";
                    180:        alertdlg[DLGALERT_CANCEL].w = 8;
                    181:        alertdlg[DLGALERT_CANCEL].h = 1;
                    182: 
                    183:        /* Adjust buttons positions: */
                    184:        alertdlg[DLGALERT_OK].x = (alertdlg[0].w - alertdlg[DLGALERT_OK].w - alertdlg[DLGALERT_CANCEL].w) / 3;
                    185:        alertdlg[DLGALERT_CANCEL].x = alertdlg[DLGALERT_OK].x * 2 + alertdlg[DLGALERT_OK].w;
                    186: 
                    187:        return DlgAlert_ShowDlg(text);
                    188: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.