Re: [AD] Tab and Newline in alert |
[ Thread Index |
Date Index
| More lists.liballeg.org/allegro-developers Archives
]
--- Evert Glebbeek wrote:
> > P.S. The cvs server is down, so I made a patch off
> of
> > 4.2-rc2. Also I'm bad at this patch thing, i did
> diff
> > src/gui.c src/guinew.c > alert.diff. Are there
> more
> > options I should have passed cause my diff isn't
> > looking like other people's do.
>
> diff -u
> I'll try to have a peek at your code later; not sure
> if this is 4.2
> material though...
I added the functionality to already existing
functions so it could fit into 4.2 instead of making
alert_ex.
Heres the -u version.
--- src/gui.c 2005-08-21 09:23:28.000000000 -0700
+++ src/guinew.c 2005-10-19 03:54:31.000000000 -0700
@@ -23,8 +23,8 @@
* See readme.txt for copyright information.
*/
-
#include <limits.h>
+#include <string.h>
#include "allegro.h"
#include "allegro/internal/aintern.h"
@@ -2260,31 +2260,175 @@
return ret;
}
+#define BG 0
+#define B1 1
+#define B2 2
+#define B3 3
+#define YIELD 4
+#define SIZE 5
-
-static DIALOG alert_dialog[] =
+int _alert3(char *s1, AL_CONST char *b1, AL_CONST char *b2, AL_CONST char *b3, int c1, int c2, int c3)
{
- /* (dialog proc) (x) (y) (w) (h) (fg) (bg) (key) (flags) (d1) (d2) (dp) (dp2) (dp3) */
- { _gui_shadow_box_proc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL },
- { _gui_ctext_proc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL },
- { _gui_ctext_proc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL },
- { _gui_ctext_proc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL },
- { _gui_button_proc, 0, 0, 0, 0, 0, 0, 0, D_EXIT, 0, 0, NULL, NULL, NULL },
- { _gui_button_proc, 0, 0, 0, 0, 0, 0, 0, D_EXIT, 0, 0, NULL, NULL, NULL },
- { _gui_button_proc, 0, 0, 0, 0, 0, 0, 0, D_EXIT, 0, 0, NULL, NULL, NULL },
- { d_yield_proc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL },
- { NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL }
-};
-
-
-#define A_S1 1
-#define A_S2 2
-#define A_S3 3
-#define A_B1 4
-#define A_B2 5
-#define A_B3 6
-
-
+ int ret;
+ int lines = 1; /* Number of lines */
+ int width = 0; /* Width / length of longest line */
+ int widthcmp = 0; /* Temporary value for comparing with width */
+ int buttons = 0; /* Number of buttons */
+ const int pad = 5; /* Padding between lines */
+ const int bpad = 10; /* Padding between buttons */
+ const int bh = text_height(font) + 8; /* Height of buttons */
+ char *lptr; /* Pointer for iterating lines */
+ DIALOG *d;
+ int i;
+
+ /* Calculate number of buttons */
+ if(b1)
+ buttons++;
+
+ if(b2)
+ buttons++;
+
+ if(b3)
+ buttons++;
+
+ lptr = s1;
+
+ /* Count number of lines replacing newlines with NULL */
+ while((lptr = strchr(lptr, '\n')) != NULL) {
+
+ lines++;
+ *(lptr++) = 0;
+ }
+
+ lptr = s1;
+
+ /* Calculate longest line length (width) */
+ for(i = 0; i < lines; i++) {
+
+ if(text_length(font, lptr) > width)
+ width = text_length(font, lptr);
+
+ lptr += strlen(lptr) + 1;
+ }
+
+ d = _al_malloc(sizeof(DIALOG) * (lines + SIZE + 1));
+ memset(d, 0, sizeof(DIALOG) * (lines + SIZE + 1));
+
+ if(!d) {
+
+ *allegro_errno = ENOMEM;
+ return -1;
+ }
+
+ /* Rotate button strings down so b1 will be filled if any are */
+ if(!b2 && b3) {
+
+ b2 = b3;
+ b3 = 0;
+ }
+
+ if(!b1 && b2) {
+
+ b1 = b2;
+ b2 = 0;
+
+ if(b3) {
+
+ b2 = b3;
+ b3 = 0;
+ }
+ }
+
+ /* Set up preliminary button stuff to calculate width of alert box */
+ d[B1].dp = (void*)(b1 ? b1 : "");
+ d[B1].w = text_length(font, d[B1].dp) + 8;
+ d[B1].x = SCREEN_W / 2 - d[B1].w / 2;
+
+ d[B2].dp = (void*)(b2 ? b2 : "");
+ d[B2].w = text_length(font, d[B2].dp) + 8;
+ d[B2].x = SCREEN_W / 2 - d[B2].w / 2;
+
+ d[B3].dp = (void*)(b3 ? b3 : "");
+ d[B3].w = text_length(font, d[B3].dp) + 8;
+ d[B3].x = SCREEN_W / 2 - d[B3].w / 2;
+
+ widthcmp = d[B1].w;
+
+ if(d[B2].w < widthcmp)
+ widthcmp = d[B2].w;
+
+ if(d[B3].w < widthcmp)
+ widthcmp = d[B3].w;
+
+ widthcmp += bpad * 2;
+
+ if(widthcmp > width)
+ width = widthcmp;
+
+ /* Set up background box */
+ d[BG].proc = d_shadow_box_proc;
+ d[BG].w = width + 8;
+ d[BG].h = lines * text_height(font) + lines * pad + bh * 2 + 8;
+ d[BG].x = SCREEN_W / 2 - d[BG].w / 2;
+ d[BG].y = SCREEN_H / 2 - d[BG].h / 2 + 8;
+ d[BG].fg = gui_fg_color;
+ d[BG].bg = gui_bg_color;
+
+ /* Set up button */
+ d[B1].proc = d_button_proc;
+ d[B1].h = bh;
+ d[B1].y = d[BG].y + d[BG].h - bh - 8;
+ d[B1].fg = gui_fg_color;
+ d[B1].bg = gui_bg_color;
+ d[B1].key = c1;
+ d[B1].flags = D_EXIT | (b1 ? 0 : D_HIDDEN | D_DISABLED);
+
+ /* Set up button */
+ d[B2].proc = d_button_proc;
+ d[B2].h = bh;
+ d[B2].y = d[BG].y + d[BG].h - bh - 8;
+ d[B2].fg = gui_fg_color;
+ d[B2].bg = gui_bg_color;
+ d[B2].key = c2;
+ d[B2].flags = D_EXIT | (b2 ? 0 : D_HIDDEN | D_DISABLED);
+
+ /* Set up button */
+ d[B3].proc = d_button_proc;
+ d[B3].h = bh;
+ d[B3].y = d[BG].y + d[BG].h - bh - 8;
+ d[B3].fg = gui_fg_color;
+ d[B3].bg = gui_bg_color;
+ d[B3].key = c3;
+ d[B3].flags = D_EXIT | (b3 ? 0 : D_HIDDEN | D_DISABLED);
+
+ /* Make sure to yeild */
+ d[YIELD].proc = d_yield_proc;
+
+ lptr = s1;
+
+ /* Put lines onto dialog */
+ for(i = 0; i < lines; i++) {
+
+ d[SIZE + i].proc = d_ctext_proc;
+ d[SIZE + i].x = SCREEN_W / 2;
+ d[SIZE + i].y = SCREEN_H / 2 + text_height(font) * i + i * pad - lines * text_height(font) / 2 - lines * pad / 2;
+ d[SIZE + i].dp = lptr;
+ d[SIZE + i].dp2 = font;
+ d[SIZE + i].fg = gui_fg_color;
+ d[SIZE + i].bg = gui_bg_color;
+
+ lptr += strlen(lptr) + 1;
+ }
+
+ /* Set the final DIALOG proc in the array to null */
+ d[SIZE + i].proc = NULL;
+
+ ret = popup_dialog(d, B1);
+
+ free(d);
+
+ return ret;
+}
/* alert3:
* Displays a simple alert box, containing three lines of text (s1-s3),
@@ -2295,110 +2439,121 @@
*/
int alert3(AL_CONST char *s1, AL_CONST char *s2, AL_CONST char *s3, AL_CONST char *b1, AL_CONST char *b2, AL_CONST char *b3, int c1, int c2, int c3)
{
- char tmp[16];
- int avg_w, avg_h;
- int len1, len2, len3;
- int maxlen = 0;
- int buttons = 0;
- int b[3];
- int c;
-
- #define SORT_OUT_BUTTON(x) { \
- if (b##x) { \
- alert_dialog[A_B##x].flags &= ~D_HIDDEN; \
- alert_dialog[A_B##x].key = c##x; \
- alert_dialog[A_B##x].dp = (char *)b##x; \
- len##x = gui_strlen(b##x); \
- b[buttons++] = A_B##x; \
- } \
- else { \
- alert_dialog[A_B##x].flags |= D_HIDDEN; \
- len##x = 0; \
- } \
- }
-
- usetc(tmp+usetc(tmp, ' '), 0);
-
- avg_w = text_length(font, tmp);
- avg_h = text_height(font);
-
- alert_dialog[A_S1].dp = alert_dialog[A_S2].dp = alert_dialog[A_S3].dp =
- alert_dialog[A_B1].dp = alert_dialog[A_B2].dp = empty_string;
-
- if (s1) {
- alert_dialog[A_S1].dp = (char *)s1;
- maxlen = text_length(font, s1);
- }
-
- if (s2) {
- alert_dialog[A_S2].dp = (char *)s2;
- len1 = text_length(font, s2);
- if (len1 > maxlen)
- maxlen = len1;
- }
-
- if (s3) {
- alert_dialog[A_S3].dp = (char *)s3;
- len1 = text_length(font, s3);
- if (len1 > maxlen)
- maxlen = len1;
- }
-
- SORT_OUT_BUTTON(1);
- SORT_OUT_BUTTON(2);
- SORT_OUT_BUTTON(3);
-
- len1 = MAX(len1, MAX(len2, len3)) + avg_w*3;
- if (len1*buttons > maxlen)
- maxlen = len1*buttons;
-
- maxlen += avg_w*4;
- alert_dialog[0].w = maxlen;
- alert_dialog[A_S1].w = alert_dialog[A_S2].w = alert_dialog[A_S3].w = maxlen - avg_w*2;
- alert_dialog[A_S1].x = alert_dialog[A_S2].x = alert_dialog[A_S3].x =
- alert_dialog[0].x + avg_w;
-
- alert_dialog[A_B1].w = alert_dialog[A_B2].w = alert_dialog[A_B3].w = len1;
-
- alert_dialog[A_B1].x = alert_dialog[A_B2].x = alert_dialog[A_B3].x =
- alert_dialog[0].x + maxlen/2 - len1/2;
-
- if (buttons == 3) {
- alert_dialog[b[0]].x = alert_dialog[0].x + maxlen/2 - len1*3/2 - avg_w;
- alert_dialog[b[2]].x = alert_dialog[0].x + maxlen/2 + len1/2 + avg_w;
- }
- else if (buttons == 2) {
- alert_dialog[b[0]].x = alert_dialog[0].x + maxlen/2 - len1 - avg_w;
- alert_dialog[b[1]].x = alert_dialog[0].x + maxlen/2 + avg_w;
- }
-
- alert_dialog[0].h = avg_h*8;
- alert_dialog[A_S1].y = alert_dialog[0].y + avg_h;
- alert_dialog[A_S2].y = alert_dialog[0].y + avg_h*2;
- alert_dialog[A_S3].y = alert_dialog[0].y + avg_h*3;
- alert_dialog[A_S1].h = alert_dialog[A_S2].h = alert_dialog[A_S3].h = avg_h;
- alert_dialog[A_B1].y = alert_dialog[A_B2].y = alert_dialog[A_B3].y = alert_dialog[0].y + avg_h*5;
- alert_dialog[A_B1].h = alert_dialog[A_B2].h = alert_dialog[A_B3].h = avg_h*2;
-
- centre_dialog(alert_dialog);
- set_dialog_color(alert_dialog, gui_fg_color, gui_bg_color);
- for (c = 0; alert_dialog[c].proc; c++)
- if (alert_dialog[c].proc == _gui_ctext_proc)
- alert_dialog[c].bg = -1;
-
- clear_keybuf();
-
- do {
- } while (gui_mouse_b());
-
- c = popup_dialog(alert_dialog, A_B1);
-
- if (c == A_B1)
- return 1;
- else if (c == A_B2)
- return 2;
- else
- return 3;
+ int ret;
+ char *msg; /* New string containing s* seperated by newlines */
+ char *lptr; /* For iterating */
+ const char *cptr; /* For const iterating */
+ int len = 0; /* Length of new string */
+ int tabs = 0; /* Number of tabs in new string */
+ int lines = 0; /* How many valid s# strings were passed */
+ const int tab = 4; /* Spaces per tab */
+
+ /* Count tabs */
+ cptr = s1;
+
+ if(cptr) {
+
+ while((cptr = strchr(cptr, '\t')) != NULL) {
+
+ tabs++;
+ cptr++;
+ }
+ }
+
+ cptr = s2;
+
+ if(cptr) {
+
+ while((cptr = strchr(cptr, '\t')) != NULL) {
+
+ tabs++;
+ cptr++;
+ }
+ }
+
+ cptr = s3;
+
+ if(cptr) {
+
+ while((cptr = strchr(cptr, '\t')) != NULL) {
+
+ tabs++;
+ cptr++;
+ }
+ }
+
+ /* Calculate len and lines */
+ if(s1) {
+
+ len += strlen(s1);
+ lines++;
+ }
+
+ if(s2) {
+
+ len += strlen(s2);
+ lines++;
+ }
+
+ if(s3) {
+
+ len += strlen(s3);
+ lines++;
+ }
+
+ msg = _al_malloc(len + lines + tabs * (tab - 1));
+
+ if(!msg) {
+
+ *allegro_errno = ENOMEM;
+ return -1;
+ }
+
+ lptr = msg;
+
+ /* Copy string to msg and postfix newline if nessicary */
+ if(s1) {
+
+ strcpy(lptr, s1);
+ lptr += strlen(lptr);
+
+ if(--lines) {
+
+ *lptr++ = '\n';
+ }
+ }
+
+ /* Copy string to msg and postfix newline if nessicary */
+ if(s2) {
+
+ strcpy(lptr, s2);
+ lptr += strlen(lptr);
+
+ if(--lines) {
+
+ *lptr++ = '\n';
+ }
+ }
+
+ /* Copy string to msg */
+ if(s3)
+ strcpy(lptr, s3);
+
+ /* Replace tabs with spaces */
+ lptr = msg;
+
+ while((lptr = strchr(lptr, '\t')) != NULL) {
+
+ memmove(lptr + tab, lptr + 1, strlen(lptr + 1) + 1);
+
+ memset(lptr, ' ', tab);
+ }
+
+ ret = _alert3(msg, b1, b2, b3, c1, c2, c3);
+
+ free(msg);
+
+ return ret;
}