From: Peep P. <so...@us...> - 2004-08-03 14:48:55
|
Update of /cvsroot/agd/client In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv1156 Modified Files: gui.c Log Message: xmalloc; textbox Index: gui.c =================================================================== RCS file: /cvsroot/agd/client/gui.c,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -u -d -r1.1.1.1 -r1.2 --- gui.c 9 Jan 2004 20:52:07 -0000 1.1.1.1 +++ gui.c 3 Aug 2004 14:48:46 -0000 1.2 @@ -1,9 +1,12 @@ #include <allegro.h> -#include "gui.h" #include <string.h> +#include <stdarg.h> +#include "gui.h" +#include "sys.h" static int numfonts, fontsalloced; -static gui_font_t *fonts; +/*static*/ gui_font_t *fonts; +int cursor_y; void gui_init(void) { @@ -15,7 +18,7 @@ void gui_load_font(char *filename) { if(numfonts >= fontsalloced) { - fonts = realloc(fonts, sizeof(BITMAP*) * (fontsalloced *= 2)); + fonts = xrealloc(fonts, sizeof(BITMAP*) * (fontsalloced *= 2)); } fonts[numfonts].b = load_bitmap(filename, NULL); fonts[numfonts].char_w = fonts[numfonts].b->w / 94; @@ -24,19 +27,22 @@ gui_window_t *gui_new_window(int w, int h, int x, int y, int properties) { - gui_window_t *win = malloc(sizeof(gui_window_t)); + gui_window_t *win = xmalloc(sizeof(gui_window_t)); win->w = w; win->h = h; win->visible = 1; win->x = x; win->y = y; win->properties = properties; win->title = NULL; - init_array(&win->widgets); + + win->numwidg = 0; + win->widgets = NULL; return win; } -gui_button_t *gui_new_button(int w, int h, char *caption, void (*callback)(void)) +gui_button_t *gui_new_button(int w, int h, char *caption, + void (*callback)(void)) { - gui_button_t *b = malloc(sizeof(gui_button_t)); + gui_button_t *b = xmalloc(sizeof(gui_button_t)); b->w = w; b->h = h; b->caption = caption; @@ -47,28 +53,41 @@ gui_label_t *gui_new_label(int w, int h, char *text) { - gui_label_t *l = malloc(sizeof(gui_label_t)); + gui_label_t *l = xmalloc(sizeof(gui_label_t)); l->w = w; l->h = h; l->text = text; return l; } +gui_textbox_t *gui_new_textbox(int w, int h) +{ + gui_textbox_t *tb = xmalloc(sizeof(gui_textbox_t)); + tb->w = w; + tb->h = h; + tb->value = NULL; + return tb; +} + void gui_add_widget(gui_window_t *win, void *widg, int type, int x, int y) { - subwidget_t *sw = malloc(sizeof(subwidget_t)); + subwidget_t *sw; + + win->numwidg++; + win->widgets = xrealloc(win->widgets, sizeof(subwidget_t) * win->numwidg); + sw = &win->widgets[win->numwidg-1]; sw->x = x; sw->y = y; sw->type = type; sw->widg = widg; - array_push(&win->widgets, sw); } void gui_events(gui_window_t *win) { int i; - for(i=0;i<win->widgets.length;i++) { - subwidget_t *sw = win->widgets.data[i]; + + for(i=0;i<win->numwidg;i++) { + subwidget_t *sw = &win->widgets[i]; gui_widget_t *widg = sw->widg; if(mouse_x >= win->x + sw->x && mouse_x <= win->x + sw->x + widg->w && mouse_y >= win->y + sw->y && mouse_y <= win->y + sw->y + widg->h) { @@ -130,11 +149,11 @@ } /* Contents of window */ - for(i=0;i<win->widgets.length;i++) { + for(i=0;i<win->numwidg;i++) { subwidget_t *sw; int loc_x, loc_y; - sw = win->widgets.data[i]; + sw = &win->widgets[i]; loc_x = x + 1 + sw->x; loc_y = y + titleh + 1 + sw->y; switch(sw->type) { @@ -149,6 +168,11 @@ if(loc_x + l->w < x + win->w && loc_y + l->h < y + win->h + titleh) gui_label(bmp, 0, l->text, loc_x, loc_y, l->w, l->h); } break; + case GUI_SWT_TEXTBOX: { + gui_textbox_t *tb = sw->widg; + if(loc_x + tb->w < x + win->w && loc_y + tb->h < y + win->h + titleh) + gui_textbox(bmp, 0, tb->value, loc_x, loc_y, tb->w, tb->h); + } break; } } } @@ -197,69 +221,44 @@ return 0; } -/* in sys.c or something? */ -int count_lines(char *str) -{ - int i, count; - i = count = 0; - while(str[i]) if(str[i++] == '\n') count++; - return count; -} - -char *strdup(char *text); void gui_label(BITMAP *bmp, int font, char *text, int x, int y, int w, int h) { - char *str = strdup(text); - int numlines = count_lines(str) + 1; - char *oldstr = NULL; - char **strings; - char *marker = NULL; + exploded_t *ex; int i, yy; - int numstr; - - strings = malloc(sizeof(char*)*numlines); - numstr = 0; - - strings[0] = str; - for(i=1;i<numlines;i++) - strings[i] = NULL; - - marker = str; - for(i=0;str[i];i++) { - if(str[i] == '\n') { - str[i] = '\0'; - if(marker && *marker) { - strings[numstr] = marker; - marker = NULL; - } - marker = str+i+1; - ++numstr; - } else if(str[i] == '\t') { - char *str2; - str[i] = '\0'; - str2 = malloc(strlen(marker) + 5 + strlen(str+i+1)); /* 4 for tab */ - strcpy(str2, marker); - strcat(str2, " "); - strcat(str2, str+i+1); - oldstr = str; - str = str2; - i = 0; - } - } - if(str) - strings[numstr++] = str; + + ex = explode(text, "\n"); - for(i=0,yy=y;i<numlines;i++,yy += fonts[font].b->h) { - if(strings[i]) - while(gui_text(bmp, font, strings[i], x, yy, w)) { - strings[i] += w / fonts[font].char_w; + yy = y; + for(i=0;i<ex->num;i++,yy+=fonts[font].b->h) { + if(ex->str[i]) + while(gui_text(bmp, font, ex->str[i], x, yy, w)) { + ex->str[i] += w / fonts[font].char_w; yy += fonts[font].b->h; - } } - if(oldstr) free(oldstr); - free(str); - free(strings); +} + +void gui_textbox(BITMAP *bmp, int font, char *value, int x, int y, int w, int h) +{ + int text_w, text_h; + int col_border, col_contents; + + text_w = fonts[font].char_w * strlen(value); + text_h = fonts[font].b->h; + + col_border = makecol(20, 20, 20); + col_contents = makecol(55, 55, 55); + + if(w < text_w + 4) w = text_w + 4; + if(h < text_h + 4) h = text_h + 4; + + line(bmp, x, y, x+w, y, col_border); + line(bmp, x, y, x, y+h, col_border); + line(bmp, x, y+h, x+w, y+h, col_border); + line(bmp, x+w, y, x+w, y+h, col_border); + rectfill(bmp, x+1, y+1, x+w-1, y+h-1, col_contents); +/* gui_text(bmp, font, value, x+(w/2-text_w/2)+1+pressed, y+(h/2-text_h/2)+1+pressed, w);*/ + gui_text(bmp, font, value, x + 1, y + ((h - text_h)/ 2) + 1, w); } #if 0 |