|
|
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.2 ! root 19: char DlgAlert_rcsid[] = "Hatari $Id: dlgAlert.c,v 1.3 2005/06/05 17:29:05 thothy 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:
33:
34: #define DLGALERT_OK 5
35: #define DLGALERT_CANCEL 6
36:
37: /* The "Alert"-dialog: */
38: static SGOBJ alertdlg[] =
39: {
40: { SGBOX, 0, 0, 0,0, 52,7, NULL },
41: { SGTEXT, 0, 0, 1,1, 50,1, dlglines[0] },
42: { SGTEXT, 0, 0, 1,2, 50,1, dlglines[1] },
43: { SGTEXT, 0, 0, 1,3, 50,1, dlglines[2] },
44: { SGTEXT, 0, 0, 1,4, 50,1, dlglines[3] },
45: { SGBUTTON, 0, 0, 5,5, 8,1, "OK" },
46: { SGBUTTON, 0, 0, 24,5, 8,1, "Cancel" },
47: { -1, 0, 0, 0,0, 0,0, NULL }
48: };
49:
50:
51: /*-----------------------------------------------------------------------*/
52: /*
53: Breaks long string to several strings of max_width, divided by '\0'
54: and returns the number of lines you need to display the strings.
55: */
56: static int DlgAlert_FormatTextToBox(char *text, int max_width)
57: {
58: int lines = 1;
59: int txtlen;
60: char *p; /* pointer to begin of actual line */
61: char *q; /* pointer to start of next search */
62: char *llb; /* pointer to last place suitable for breaking the line */
63: char *txtend; /* pointer to end of the text */
64:
65: txtlen = strlen(text);
66:
67: q = p = text;
68: llb = text-1; /* pointer to last line break */
69: txtend = text + txtlen;
70:
71: if (txtlen > max_width)
72: {
73: while(q < txtend) /* q was last place suitable for breaking */
74: {
75: char *r = strpbrk(q, " \t/\\\n"); /* find next suitable place for the break */
76: if (r == NULL)
77: r = txtend; /* if there's no place then point to the end */
78:
79: if ((r-p) < max_width && *r != '\n') /* '\n' is always used for breaking */
80: {
81: llb = r; /* remember new place suitable for breaking */
82: q++;
83: continue; /* search again */
84: }
85:
86: if ((r-p) > max_width) /* too long line already? */
87: {
88: if (p > llb) /* bad luck - no place for the delimiter. Let's do it the strong way */
89: llb = p + max_width; /* we loose one character */
90: }
91: else
92: llb = r; /* break in this place */
93:
94: *llb = '\0'; /* BREAK */
95: p = q = llb + 1; /* next line begins here */
96: lines++; /* increment line counter */
97: }
98: }
99:
100: return lines; /* return line counter */
101: }
102:
103:
104:
105: /*-----------------------------------------------------------------------*/
106: /*
107: Show the "alert" dialog. Return TRUE if user pressed "OK".
108: */
109: static int DlgAlert_ShowDlg(const char *text)
110: {
111: char *t = (char *)malloc(strlen(text)+1);
112: char *orig_t = t;
113: static int maxlen = sizeof(dlglines[0])-1;
114: int lines;
115: int i;
116: BOOL bOldMouseVisibility;
117:
118: strcpy(t, text);
119: lines = DlgAlert_FormatTextToBox(t, maxlen);
120:
121: for(i=0; i<MAX_LINES; i++)
122: {
123: if (i < lines)
124: {
125: strcpy(dlglines[i], t);
126: t += strlen(t)+1;
127: }
128: else
129: {
130: dlglines[i][0] = '\0';
131: }
132: }
133:
134: free(orig_t);
135:
1.1.1.2 ! root 136: if (SDLGui_SetScreen(sdlscrn))
! 137: return FALSE;
1.1 root 138: SDLGui_CenterDlg(alertdlg);
139:
140: bOldMouseVisibility = SDL_ShowCursor(SDL_QUERY);
141: SDL_ShowCursor(SDL_ENABLE);
142:
1.1.1.2 ! root 143: i = SDLGui_DoDialog(alertdlg, NULL);
1.1 root 144:
145: SDL_ShowCursor(bOldMouseVisibility);
146:
147: return (i == DLGALERT_OK);
148: }
149:
150:
151: /*-----------------------------------------------------------------------*/
152: /*
153: Show a "notice" dialog: (only one button)
154: */
155: int DlgAlert_Notice(const char *text)
156: {
157: /* Hide "cancel" button: */
158: alertdlg[DLGALERT_CANCEL].type = SGTEXT;
159: alertdlg[DLGALERT_CANCEL].txt = "";
160: alertdlg[DLGALERT_CANCEL].w = alertdlg[DLGALERT_CANCEL].w = 0;
161:
162: /* Adjust button position: */
163: alertdlg[DLGALERT_OK].x = (alertdlg[0].w - alertdlg[DLGALERT_OK].w) / 2;
164:
165: return DlgAlert_ShowDlg(text);
166: }
167:
168:
169: /*-----------------------------------------------------------------------*/
170: /*
171: Show a "query" dialog: (two buttons)
172: */
173: int DlgAlert_Query(const char *text)
174: {
175: /* Show "cancel" button: */
176: alertdlg[DLGALERT_CANCEL].type = SGBUTTON;
177: alertdlg[DLGALERT_CANCEL].txt = "Cancel";
178: alertdlg[DLGALERT_CANCEL].w = 8;
179: alertdlg[DLGALERT_CANCEL].h = 1;
180:
181: /* Adjust buttons positions: */
182: alertdlg[DLGALERT_OK].x = (alertdlg[0].w - alertdlg[DLGALERT_OK].w - alertdlg[DLGALERT_CANCEL].w) / 3;
183: alertdlg[DLGALERT_CANCEL].x = alertdlg[DLGALERT_OK].x * 2 + alertdlg[DLGALERT_OK].w;
184:
185: return DlgAlert_ShowDlg(text);
186: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.