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

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.7   root       19: const char DlgAlert_fileid[] = "Hatari dlgAlert.c : " __DATE__ " " __TIME__;
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
1.1.1.7   root       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
1.1.1.3   root       37: #endif
1.1       root       38: 
                     39: #define DLGALERT_OK       5
                     40: #define DLGALERT_CANCEL   6
                     41: 
                     42: /* The "Alert"-dialog: */
                     43: static SGOBJ alertdlg[] =
                     44: {
                     45:        { SGBOX, 0, 0, 0,0, 52,7, NULL },
                     46:        { SGTEXT, 0, 0, 1,1, 50,1, dlglines[0] },
                     47:        { SGTEXT, 0, 0, 1,2, 50,1, dlglines[1] },
                     48:        { SGTEXT, 0, 0, 1,3, 50,1, dlglines[2] },
                     49:        { SGTEXT, 0, 0, 1,4, 50,1, dlglines[3] },
1.1.1.4   root       50:        { SGBUTTON, SG_DEFAULT, 0, 5,5, 8,1, "OK" },
1.1.1.12! root       51:        { SGBUTTON, SG_CANCEL, 0, 24,5, 8,1, NULL },
        !            52:        { SGSTOP, 0, 0, 0,0, 0,0, NULL }
1.1       root       53: };
                     54: 
                     55: 
                     56: /*-----------------------------------------------------------------------*/
1.1.1.8   root       57: /**
                     58:  * Breaks long string to several strings of max_width, divided by '\0',
                     59:  * sets text_width to the longest line width and returns the number of lines
                     60:  * you need to display the strings.
                     61:  */
1.1.1.4   root       62: static int DlgAlert_FormatTextToBox(char *text, int max_width, int *text_width)
1.1       root       63: {
1.1.1.4   root       64:        int columns = 0;
1.1       root       65:        int lines = 1;
                     66:        int txtlen;
1.1.1.4   root       67:        char *p;            /* pointer to begin of actual line */
                     68:        char *q;            /* pointer to start of next search */
                     69:        char *llb;          /* pointer to last place suitable for breaking the line */
                     70:        char *txtend;       /* pointer to end of the text */
1.1       root       71: 
                     72:        txtlen = strlen(text);
                     73: 
                     74:        q = p = text;
1.1.1.4   root       75:        llb = text-1;       /* pointer to last line break */
1.1       root       76:        txtend = text + txtlen;
                     77: 
1.1.1.4   root       78:        if (txtlen <= max_width)
1.1       root       79:        {
1.1.1.4   root       80:                *text_width = txtlen;
                     81:                return lines;
                     82:        }
                     83: 
                     84:        while(q < txtend)                             /* q was last place suitable for breaking */
                     85:        {
                     86:                char *r = strpbrk(q, " \t/\\\n");     /* find next suitable place for the break */
                     87:                if (r == NULL)
                     88:                        r = txtend;                   /* if there's no place then point to the end */
                     89: 
                     90:                if ((r-p) <= max_width && *r != '\n') /* '\n' is always used for breaking */
                     91:                {
                     92:                        llb = r;                      /* remember new place suitable for breaking */
                     93:                        q++;
                     94:                        if ((r-p) > columns)
                     95:                                columns = r - p;
                     96:                        continue;                     /* search again */
                     97:                }
                     98: 
                     99:                if ((r-p) > max_width)                /* too long line already? */
1.1       root      100:                {
1.1.1.4   root      101:                        if (p > llb)                  /* bad luck - no place for the delimiter. Let's do it the strong way */
                    102:                                llb = p + max_width;  /* we loose one character */
1.1       root      103:                }
1.1.1.4   root      104:                else
                    105:                        llb = r;                /* break from previous delimiter */
                    106: 
                    107:                *llb = '\0';                    /* BREAK */
                    108:                if ((llb-p) > columns)
                    109:                        columns = llb - p;      /* longest line so far */
                    110:                p = q = llb + 1;                /* next line begins here */
                    111:                lines++;                        /* increment line counter */
1.1       root      112:        }
                    113: 
1.1.1.4   root      114:        *text_width = columns;
                    115: 
                    116:        return lines;                           /* return line counter */
1.1       root      117: }
                    118: 
                    119: 
                    120: 
                    121: /*-----------------------------------------------------------------------*/
1.1.1.8   root      122: /**
                    123:  * Show the "alert" dialog. Return true if user pressed "OK".
                    124:  */
1.1       root      125: static int DlgAlert_ShowDlg(const char *text)
                    126: {
1.1.1.4   root      127:        static int maxlen = sizeof(dlglines[0])-1;
1.1       root      128:        char *t = (char *)malloc(strlen(text)+1);
                    129:        char *orig_t = t;
1.1.1.4   root      130:        int lines, i, len, offset;
1.1.1.5   root      131:        bool bOldMouseVisibility;
1.1.1.4   root      132:        int nOldMouseX, nOldMouseY;
1.1.1.12! root      133:        bool bWasEmuActive;
1.1       root      134: 
1.1.1.11  root      135: #if WITH_SDL2
                    136:        bool bOldMouseMode = SDL_GetRelativeMouseMode();
                    137:        SDL_SetRelativeMouseMode(SDL_FALSE);
                    138: #endif
1.1       root      139:        strcpy(t, text);
1.1.1.4   root      140:        lines = DlgAlert_FormatTextToBox(t, maxlen, &len);
                    141:        offset = (maxlen-len)/2;
1.1       root      142: 
                    143:        for(i=0; i<MAX_LINES; i++)
                    144:        {
                    145:                if (i < lines)
                    146:                {
1.1.1.4   root      147:                        /* center text to current dlgline */
                    148:                        memset(dlglines[i], ' ', offset);
                    149:                        strcpy(dlglines[i] + offset, t);
1.1       root      150:                        t += strlen(t)+1;
                    151:                }
                    152:                else
                    153:                {
                    154:                        dlglines[i][0] = '\0';
                    155:                }
                    156:        }
                    157: 
                    158:        free(orig_t);
                    159: 
1.1.1.2   root      160:        if (SDLGui_SetScreen(sdlscrn))
1.1.1.7   root      161:                return false;
1.1       root      162:        SDLGui_CenterDlg(alertdlg);
                    163: 
1.1.1.12! root      164:        bWasEmuActive = Main_PauseEmulation(true);
        !           165: 
1.1.1.4   root      166:        SDL_GetMouseState(&nOldMouseX, &nOldMouseY);
1.1       root      167:        bOldMouseVisibility = SDL_ShowCursor(SDL_QUERY);
                    168:        SDL_ShowCursor(SDL_ENABLE);
                    169: 
1.1.1.11  root      170:        i = SDLGui_DoDialog(alertdlg, NULL, false);
1.1       root      171: 
1.1.1.4   root      172:        SDL_UpdateRect(sdlscrn, 0,0, 0,0);
1.1       root      173:        SDL_ShowCursor(bOldMouseVisibility);
1.1.1.11  root      174:        Main_WarpMouse(nOldMouseX, nOldMouseY, true);
                    175: 
                    176: #if WITH_SDL2
                    177:        SDL_SetRelativeMouseMode(bOldMouseMode);
                    178: #endif
1.1       root      179: 
1.1.1.12! root      180:        if (bWasEmuActive)
        !           181:                Main_UnPauseEmulation();
        !           182: 
1.1       root      183:        return (i == DLGALERT_OK);
                    184: }
                    185: 
                    186: 
                    187: /*-----------------------------------------------------------------------*/
1.1.1.8   root      188: /**
                    189:  * Show a "notice" dialog: (only one button)
                    190:  */
1.1       root      191: int DlgAlert_Notice(const char *text)
                    192: {
1.1.1.10  root      193: #ifdef ALERT_HOOKS
                    194:        if (!Main_UnPauseEmulation())
                    195:                Main_PauseEmulation(true);
                    196:        if(!bInFullScreen)
                    197:                return HookedAlertNotice(text);
1.1.1.3   root      198: #endif
                    199: 
1.1       root      200:        /* Hide "cancel" button: */
                    201:        alertdlg[DLGALERT_CANCEL].type = SGTEXT;
                    202:        alertdlg[DLGALERT_CANCEL].txt = "";
1.1.1.9   root      203:        alertdlg[DLGALERT_CANCEL].w = 0;
                    204:        alertdlg[DLGALERT_CANCEL].h = 0;
1.1       root      205: 
                    206:        /* Adjust button position: */
                    207:        alertdlg[DLGALERT_OK].x = (alertdlg[0].w - alertdlg[DLGALERT_OK].w) / 2;
                    208: 
                    209:        return DlgAlert_ShowDlg(text);
                    210: }
                    211: 
                    212: 
                    213: /*-----------------------------------------------------------------------*/
1.1.1.8   root      214: /**
                    215:  * Show a "query" dialog: (two buttons), return true for OK
                    216:  */
1.1       root      217: int DlgAlert_Query(const char *text)
                    218: {
1.1.1.3   root      219: #ifdef ALERT_HOOKS
1.1.1.10  root      220:        if(!bInFullScreen)
                    221:                return HookedAlertQuery(text);
1.1.1.3   root      222: #endif
                    223: 
1.1       root      224:        /* Show "cancel" button: */
                    225:        alertdlg[DLGALERT_CANCEL].type = SGBUTTON;
                    226:        alertdlg[DLGALERT_CANCEL].txt = "Cancel";
                    227:        alertdlg[DLGALERT_CANCEL].w = 8;
                    228:        alertdlg[DLGALERT_CANCEL].h = 1;
                    229: 
                    230:        /* Adjust buttons positions: */
                    231:        alertdlg[DLGALERT_OK].x = (alertdlg[0].w - alertdlg[DLGALERT_OK].w - alertdlg[DLGALERT_CANCEL].w) / 3;
                    232:        alertdlg[DLGALERT_CANCEL].x = alertdlg[DLGALERT_OK].x * 2 + alertdlg[DLGALERT_OK].w;
                    233: 
                    234:        return DlgAlert_ShowDlg(text);
                    235: }

unix.superglobalmegacorp.com

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