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

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
1.1.1.2   root       17:  * GNU General Public License (gpl.txt) for more details.
1.1       root       18:  */
1.1.1.3 ! root       19: const char DlgAlert_rcsid[] = "Hatari $Id: dlgAlert.c,v 1.5 2006/07/03 20:36:28 clafou Exp $";
1.1       root       20: 
                     21: #include <string.h>
                     22: 
                     23: #include "main.h"
                     24: #include "dialog.h"
1.1.1.2   root       25: #include "screen.h"
1.1       root       26: #include "sdlgui.h"
                     27: 
                     28: 
                     29: #define MAX_LINES 4
                     30: 
                     31: static char dlglines[MAX_LINES][50+1];
                     32: 
1.1.1.3 ! root       33: #ifdef ALERT_HOOKS 
        !            34:        // The alert hook functions
        !            35:        extern int HookedAlertNotice(const char* szMessage);    // Must return TRUE if OK clicked, FALSE otherwise
        !            36:        extern int HookedAlertQuery(const char* szMessage);             // Must return TRUE if OK clicked, FALSE otherwise
        !            37: 
        !            38:        // Runtime switch to activate/deactivate alert hooks
        !            39:        BOOL useAlertHooks = FALSE;
        !            40: #endif
1.1       root       41: 
                     42: #define DLGALERT_OK       5
                     43: #define DLGALERT_CANCEL   6
                     44: 
                     45: /* The "Alert"-dialog: */
                     46: static SGOBJ alertdlg[] =
                     47: {
                     48:        { SGBOX, 0, 0, 0,0, 52,7, NULL },
                     49:        { SGTEXT, 0, 0, 1,1, 50,1, dlglines[0] },
                     50:        { SGTEXT, 0, 0, 1,2, 50,1, dlglines[1] },
                     51:        { SGTEXT, 0, 0, 1,3, 50,1, dlglines[2] },
                     52:        { SGTEXT, 0, 0, 1,4, 50,1, dlglines[3] },
                     53:        { SGBUTTON, 0, 0, 5,5, 8,1, "OK" },
                     54:        { SGBUTTON, 0, 0, 24,5, 8,1, "Cancel" },
                     55:        { -1, 0, 0, 0,0, 0,0, NULL }
                     56: };
                     57: 
                     58: 
                     59: /*-----------------------------------------------------------------------*/
                     60: /*
                     61:    Breaks long string to several strings of max_width, divided by '\0'
                     62:    and returns the number of lines you need to display the strings.
                     63: */
                     64: static int DlgAlert_FormatTextToBox(char *text, int max_width)
                     65: {
                     66:        int lines = 1;
                     67:        int txtlen;
                     68:        char *p;                /* pointer to begin of actual line */
                     69:        char *q;                /* pointer to start of next search */
                     70:        char *llb;              /* pointer to last place suitable for breaking the line */
                     71:        char *txtend;   /* pointer to end of the text */
                     72: 
                     73:        txtlen = strlen(text);
                     74: 
                     75:        q = p = text;
                     76:        llb = text-1;   /* pointer to last line break */
                     77:        txtend = text + txtlen;
                     78: 
                     79:        if (txtlen > max_width)
                     80:        {
                     81:                while(q < txtend)                                               /* q was last place suitable for breaking */
                     82:                {
                     83:                        char *r = strpbrk(q, " \t/\\\n");       /* find next suitable place for the break */
                     84:                        if (r == NULL)
                     85:                                r = txtend;                                             /* if there's no place then point to the end */
                     86: 
                     87:                        if ((r-p) < max_width && *r != '\n')    /* '\n' is always used for breaking */
                     88:                        {
                     89:                                llb = r;                                                /* remember new place suitable for breaking */
                     90:                                q++;
                     91:                                continue;                                               /* search again */
                     92:                        }
                     93: 
                     94:                        if ((r-p) > max_width)                          /* too long line already? */
                     95:                        {
                     96:                                if (p > llb)                                    /* bad luck - no place for the delimiter. Let's do it the strong way */
                     97:                                        llb = p + max_width;            /* we loose one character */
                     98:                        }
                     99:                        else
                    100:                                llb = r;                        /* break in this place */
                    101: 
                    102:                        *llb = '\0';                    /* BREAK */
                    103:                        p = q = llb + 1;                /* next line begins here */
                    104:                        lines++;                                /* increment line counter */
                    105:                }
                    106:        }
                    107: 
                    108:        return lines;                                   /* return line counter */
                    109: }
                    110: 
                    111: 
                    112: 
                    113: /*-----------------------------------------------------------------------*/
                    114: /*
                    115:   Show the "alert" dialog. Return TRUE if user pressed "OK".
                    116: */
                    117: static int DlgAlert_ShowDlg(const char *text)
                    118: {
                    119:        char *t = (char *)malloc(strlen(text)+1);
                    120:        char *orig_t = t;
                    121:        static int maxlen = sizeof(dlglines[0])-1;
                    122:        int lines;
                    123:        int i;
                    124:        BOOL bOldMouseVisibility;
                    125: 
                    126:        strcpy(t, text);
                    127:        lines = DlgAlert_FormatTextToBox(t, maxlen);
                    128: 
                    129:        for(i=0; i<MAX_LINES; i++)
                    130:        {
                    131:                if (i < lines)
                    132:                {
                    133:                        strcpy(dlglines[i], t);
                    134:                        t += strlen(t)+1;
                    135:                }
                    136:                else
                    137:                {
                    138:                        dlglines[i][0] = '\0';
                    139:                }
                    140:        }
                    141: 
                    142:        free(orig_t);
                    143: 
1.1.1.2   root      144:        if (SDLGui_SetScreen(sdlscrn))
                    145:                return FALSE;
1.1       root      146:        SDLGui_CenterDlg(alertdlg);
                    147: 
                    148:        bOldMouseVisibility = SDL_ShowCursor(SDL_QUERY);
                    149:        SDL_ShowCursor(SDL_ENABLE);
                    150: 
1.1.1.2   root      151:        i = SDLGui_DoDialog(alertdlg, NULL);
1.1       root      152: 
                    153:        SDL_ShowCursor(bOldMouseVisibility);
                    154: 
                    155:        return (i == DLGALERT_OK);
                    156: }
                    157: 
                    158: 
                    159: /*-----------------------------------------------------------------------*/
                    160: /*
                    161:   Show a "notice" dialog: (only one button)
                    162: */
                    163: int DlgAlert_Notice(const char *text)
                    164: {
1.1.1.3 ! root      165: #ifdef ALERT_HOOKS 
        !           166:        // If activated, used the hooked function instead
        !           167:        if (useAlertHooks)
        !           168:        {
        !           169:                return HookedAlertNotice(text);
        !           170:        }
        !           171: #endif
        !           172: 
1.1       root      173:        /* Hide "cancel" button: */
                    174:        alertdlg[DLGALERT_CANCEL].type = SGTEXT;
                    175:        alertdlg[DLGALERT_CANCEL].txt = "";
                    176:        alertdlg[DLGALERT_CANCEL].w = alertdlg[DLGALERT_CANCEL].w = 0;
                    177: 
                    178:        /* Adjust button position: */
                    179:        alertdlg[DLGALERT_OK].x = (alertdlg[0].w - alertdlg[DLGALERT_OK].w) / 2;
                    180: 
                    181:        return DlgAlert_ShowDlg(text);
                    182: }
                    183: 
                    184: 
                    185: /*-----------------------------------------------------------------------*/
                    186: /*
                    187:   Show a "query" dialog: (two buttons)
                    188: */
                    189: int DlgAlert_Query(const char *text)
                    190: {
1.1.1.3 ! root      191: #ifdef ALERT_HOOKS
        !           192:        // If activated, used the hooked function instead
        !           193:        if (useAlertHooks)
        !           194:        {
        !           195:                return HookedAlertQuery(text);
        !           196:        }
        !           197: #endif
        !           198: 
1.1       root      199:        /* Show "cancel" button: */
                    200:        alertdlg[DLGALERT_CANCEL].type = SGBUTTON;
                    201:        alertdlg[DLGALERT_CANCEL].txt = "Cancel";
                    202:        alertdlg[DLGALERT_CANCEL].w = 8;
                    203:        alertdlg[DLGALERT_CANCEL].h = 1;
                    204: 
                    205:        /* Adjust buttons positions: */
                    206:        alertdlg[DLGALERT_OK].x = (alertdlg[0].w - alertdlg[DLGALERT_OK].w - alertdlg[DLGALERT_CANCEL].w) / 3;
                    207:        alertdlg[DLGALERT_CANCEL].x = alertdlg[DLGALERT_OK].x * 2 + alertdlg[DLGALERT_OK].w;
                    208: 
                    209:        return DlgAlert_ShowDlg(text);
                    210: }

unix.superglobalmegacorp.com

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