# HG changeset patch # User John Tsiombikas # Date 1304130239 -10800 # Node ID c7a7ddbe7714273a34773b4c759bb5a11882f8ab # Parent 11da537aeba65f21673b460b7355cdc76416c7a3 half-arsed automatic layout diff -r 11da537aeba6 -r c7a7ddbe7714 Makefile --- a/Makefile Tue Apr 26 22:53:21 2011 +0300 +++ b/Makefile Sat Apr 30 05:23:59 2011 +0300 @@ -1,9 +1,13 @@ -src = $(wildcard *.c src/*.c) +src = $(wildcard src/*.c) obj = $(src:.c=.o) depfiles = $(obj:.o=.d) +lib_a = libimtk.a bin = test +PREFIX = /usr/local + CC = gcc +AR = ar CFLAGS = -pedantic -Wall -g -Isrc LDFLAGS = $(libgl) -lm @@ -13,8 +17,11 @@ libgl = -lGL -lGLU -lglut endif -$(bin): $(obj) - $(CC) -o $@ $(obj) $(LDFLAGS) +$(bin): $(lib_a) test.o + $(CC) -o $@ test.o $(lib_a) $(LDFLAGS) + +$(lib_a): $(obj) + $(AR) rcs $@ $(obj) -include $(depfiles) @@ -23,8 +30,19 @@ .PHONY: clean clean: - rm -f $(obj) $(bin) + rm -f $(obj) $(bin) $(lib_a) test.o .PHONY: cleandep cleandep: rm -f $(depfiles) + +.PHONY: install +install: $(lib_a) + mkdir -p $(PREFIX)/include $(PREFIX)/lib + cp $(lib_a) $(PREFIX)/lib/$(lib_a) + cp src/imtk.h $(PREFIX)/include/imtk.h + +.PHONY: uninstall +uninstall: + rm -f $(PREFIX)/lib/$(lib_a) + rm -f $(PREFIX)/include/imtk.h diff -r 11da537aeba6 -r c7a7ddbe7714 src/button.c --- a/src/button.c Tue Apr 26 22:53:21 2011 +0300 +++ b/src/button.c Sat Apr 30 05:23:59 2011 +0300 @@ -14,6 +14,10 @@ assert(id >= 0); + if(x == IMTK_AUTO || y == IMTK_AUTO) { + imtk_layout_get_pos(&x, &y); + } + calc_button_size(label, &w, &h); if(imtk_hit_test(x, y, w, h)) { @@ -35,6 +39,7 @@ } draw_button(id, label, x, y, over); + imtk_layout_advance(w, h); return res; } diff -r 11da537aeba6 -r c7a7ddbe7714 src/checkbox.c --- a/src/checkbox.c Tue Apr 26 22:53:21 2011 +0300 +++ b/src/checkbox.c Sat Apr 30 05:23:59 2011 +0300 @@ -13,11 +13,16 @@ int imtk_checkbox(int id, const char *label, int x, int y, int state) { int sz = CHECKBOX_SIZE; - int over = 0; + int full_size, over = 0; assert(id >= 0); - if(imtk_hit_test(x, y, sz + imtk_string_size(label) + 5, sz)) { + if(x == IMTK_AUTO || y == IMTK_AUTO) { + imtk_layout_get_pos(&x, &y); + } + + full_size = sz + imtk_string_size(label) + 5; + if(imtk_hit_test(x, y, full_size, sz)) { imtk_set_hot(id); over = 1; } @@ -36,6 +41,7 @@ } draw_checkbox(id, label, x, y, state, over); + imtk_layout_advance(full_size, sz); return state; } diff -r 11da537aeba6 -r c7a7ddbe7714 src/imtk.c --- a/src/imtk.c Tue Apr 26 22:53:21 2011 +0300 +++ b/src/imtk.c Sat Apr 30 05:23:59 2011 +0300 @@ -22,6 +22,20 @@ { int width, height; + glPushAttrib(GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT | GL_TRANSFORM_BIT); + + glDisable(GL_DEPTH_TEST); + glDisable(GL_STENCIL_TEST); + glDisable(GL_ALPHA_TEST); + glDisable(GL_TEXTURE_1D); + glDisable(GL_TEXTURE_2D); + glDisable(GL_CULL_FACE); + glDisable(GL_SCISSOR_TEST); + glDisable(GL_LIGHTING); + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + + imtk_get_viewport(&width, &height); glMatrixMode(GL_PROJECTION); @@ -33,42 +47,88 @@ glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity(); - - glPushAttrib(GL_ENABLE_BIT); - glDisable(GL_DEPTH_TEST); - glDisable(GL_CULL_FACE); - glDisable(GL_LIGHTING); - glEnable(GL_BLEND); - - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); } void imtk_end(void) { - glPopAttrib(); - glMatrixMode(GL_PROJECTION); glPopMatrix(); glMatrixMode(GL_MODELVIEW); glPopMatrix(); + + glPopAttrib(); } void imtk_label(const char *str, int x, int y) { + if(x == IMTK_AUTO || y == IMTK_AUTO) { + imtk_layout_get_pos(&x, &y); + } + glColor4fv(imtk_get_color(IMTK_TEXT_COLOR)); - imtk_draw_string(x, y, str); + imtk_draw_string(x, y + 15, str); + imtk_layout_advance(imtk_string_size(str), 12); } -/* -int imtk_combobox(int id, char *textbuf, size_t buf_sz, const char *list, int sel, int x, int y) +static int box[4], span[4]; +static int spacing; +static int layout; + +void imtk_layout_start(int x, int y, int sp, int dir) { - imtk_textbox(id + 1, textbuf, buf_sz, x, y); - imtk_button(id + 3, "V", x + TEXTBOX_SIZE, y); + box[0] = span[0] = x; + box[1] = span[1] = y; + box[2] = box[3] = span[2] = span[3] = 0; + spacing = sp; + layout = dir; +} - if(prev_active == id + 3) { - sel = imtk_listbox(id + 5, list, sel, x, y + 20); +void imtk_layout_dir(int dir) +{ + layout = dir; + if(dir == IMTK_VERTICAL) { + imtk_layout_newline(); } - return sel; } -*/ + +void imtk_layout_advance(int width, int height) +{ + int max_span_y, max_box_y; + + box[2] += width + spacing; + span[2] += width + spacing; + + if(height > span[3]) { + span[3] = height; + } + + max_span_y = span[1] + span[3]; + max_box_y = box[1] + box[3]; + + if(max_span_y > max_box_y) { + box[3] = max_span_y - box[1]; + } + + if(layout == IMTK_VERTICAL) { + imtk_layout_newline(); + } +} + +void imtk_layout_newline(void) +{ + span[0] = box[0]; + span[1] = box[1] + box[3] + spacing; + span[2] = span[3] = 0; +} + +void imtk_layout_get_pos(int *x, int *y) +{ + *x = span[0] + span[2]; + *y = span[1]; +} + +void imtk_layout_get_bounds(int *bbox) +{ + memcpy(bbox, box, sizeof box); +} diff -r 11da537aeba6 -r c7a7ddbe7714 src/imtk.h --- a/src/imtk.h Tue Apr 26 22:53:21 2011 +0300 +++ b/src/imtk.h Sat Apr 30 05:23:59 2011 +0300 @@ -2,7 +2,7 @@ #define IMTK_H_ #include - +#include #define IMUID (__LINE__ << 10) #define IMUID_IDX(i) ((__LINE__ << 10) + ((i) << 1)) @@ -31,10 +31,16 @@ IMTK_CHECK_COLOR }; +enum { + IMTK_HORIZONTAL, + IMTK_VERTICAL +}; + #define IMTK_FOCUS_BIT 0x100 #define IMTK_PRESS_BIT 0x200 #define IMTK_SEL_BIT 0x400 +#define IMTK_AUTO INT_MIN #ifdef __cplusplus extern "C" { @@ -62,10 +68,18 @@ int imtk_listbox(int id, const char *list, int sel, int x, int y); int imtk_radiogroup(int id, const char *list, int sel, int x, int y); -/* helper functions to create and destroy item lists for listboxes and comboboxes */ +/* helper functions to create and destroy item lists for listboxes */ char *imtk_create_list(const char *first, ...); void imtk_free_list(char *list); +/* automatic layout */ +void imtk_layout_start(int x, int y, int spacing, int dir); +void imtk_layout_dir(int dir); +void imtk_layout_advance(int width, int height); +void imtk_layout_newline(void); +void imtk_layout_get_pos(int *x, int *y); +void imtk_layout_get_bounds(int *bbox); + /* defined in draw.c */ void imtk_set_color(unsigned int col, float r, float g, float b, float a); float *imtk_get_color(unsigned int col); diff -r 11da537aeba6 -r c7a7ddbe7714 src/listbox.c --- a/src/listbox.c Tue Apr 26 22:53:21 2011 +0300 +++ b/src/listbox.c Sat Apr 30 05:23:59 2011 +0300 @@ -29,6 +29,10 @@ assert(id >= 0); + if(x == IMTK_AUTO || y == IMTK_AUTO) { + imtk_layout_get_pos(&x, &y); + } + max_width = 0; over = 0; @@ -61,6 +65,7 @@ } draw(id, list, sel, x, y, max_width, nitems, over); + imtk_layout_advance(max_width, ITEM_HEIGHT * nitems); return sel; } diff -r 11da537aeba6 -r c7a7ddbe7714 src/progress.c --- a/src/progress.c Tue Apr 26 22:53:21 2011 +0300 +++ b/src/progress.c Sat Apr 30 05:23:59 2011 +0300 @@ -9,7 +9,12 @@ void imtk_progress(int id, float pos, int x, int y) { + if(x == IMTK_AUTO || y == IMTK_AUTO) { + imtk_layout_get_pos(&x, &y); + } + draw_progress(id, pos, x, y); + imtk_layout_advance(PROGR_SIZE, PROGR_HEIGHT); } static void draw_progress(int id, float pos, int x, int y) diff -r 11da537aeba6 -r c7a7ddbe7714 src/slider.c --- a/src/slider.c Tue Apr 26 22:53:21 2011 +0300 +++ b/src/slider.c Sat Apr 30 05:23:59 2011 +0300 @@ -18,6 +18,11 @@ assert(id >= 0); + if(x == IMTK_AUTO || y == IMTK_AUTO) { + imtk_layout_get_pos(&x, &y); + } + y += THUMB_HEIGHT / 2; + imtk_get_mouse(&mousex, &mousey); pos = (pos - min) / range; @@ -60,6 +65,7 @@ } draw_slider(id, pos, min, max, x, y, over); + imtk_layout_advance(SLIDER_SIZE + THUMB_WIDTH, THUMB_HEIGHT); return pos * range + min; } diff -r 11da537aeba6 -r c7a7ddbe7714 src/textbox.c --- a/src/textbox.c Tue Apr 26 22:53:21 2011 +0300 +++ b/src/textbox.c Sat Apr 30 05:23:59 2011 +0300 @@ -6,6 +6,7 @@ #include "draw.h" #define TEXTBOX_SIZE 100 +#define TEXTBOX_HEIGHT 20 static void draw_textbox(int id, char *text, int cursor, int x, int y, int over); @@ -16,6 +17,10 @@ assert(id >= 0); + if(x == IMTK_AUTO || y == IMTK_AUTO) { + imtk_layout_get_pos(&x, &y); + } + len = strlen(textbuf); /* HACK! using last element of textbuf for saving cursor position */ @@ -23,7 +28,7 @@ cursor = len; } - if(imtk_hit_test(x, y, TEXTBOX_SIZE, 20)) { + if(imtk_hit_test(x, y, TEXTBOX_SIZE, TEXTBOX_HEIGHT)) { imtk_set_hot(id); over = 1; } @@ -106,6 +111,7 @@ textbuf[buf_sz - 1] = cursor; draw_textbox(id, textbuf, cursor, x, y, over); + imtk_layout_advance(TEXTBOX_SIZE, TEXTBOX_HEIGHT); } @@ -120,7 +126,7 @@ memcpy(tcol, imtk_get_color(IMTK_BOTTOM_COLOR | attr), sizeof tcol); memcpy(bcol, imtk_get_color(IMTK_TOP_COLOR | attr), sizeof bcol); - imtk_draw_rect(x, y, TEXTBOX_SIZE, 20, tcol, bcol); + imtk_draw_rect(x, y, TEXTBOX_SIZE, TEXTBOX_HEIGHT, tcol, bcol); if(imtk_has_focus(id)) { int strsz; @@ -143,6 +149,6 @@ glColor4fv(imtk_get_color(IMTK_TEXT_COLOR)); imtk_draw_string(x + 2, y + 15, text); - imtk_draw_frame(x, y, TEXTBOX_SIZE, 20, FRAME_INSET); + imtk_draw_frame(x, y, TEXTBOX_SIZE, TEXTBOX_HEIGHT, FRAME_INSET); } diff -r 11da537aeba6 -r c7a7ddbe7714 test.c --- a/test.c Tue Apr 26 22:53:21 2011 +0300 +++ b/test.c Sat Apr 30 05:23:59 2011 +0300 @@ -125,6 +125,7 @@ char *itemlist; imtk_begin(); + imtk_layout_start(30, 50, 10, IMTK_VERTICAL); /*glBegin(GL_QUADS); glColor3f(0.6, 0.6, 0.6); @@ -134,47 +135,49 @@ glVertex2f(0, glutGet(GLUT_WINDOW_HEIGHT)); glEnd();*/ - if(imtk_button(IMUID, "red", 30, 50)) { + if(imtk_button(IMUID, "red", IMTK_AUTO, IMTK_AUTO)) { float color[] = {1, 0.4, 0.3, 1}; glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color); glutPostRedisplay(); } - if(imtk_button(IMUID, "blue", 30, 80)) { + if(imtk_button(IMUID, "blue", IMTK_AUTO, IMTK_AUTO)) { float color[] = {0.3, 0.4, 1, 1}; glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color); glutPostRedisplay(); } itemlist = imtk_create_list("teapot", "torus", "sphere", NULL); - if((objsel = imtk_radiogroup(IMUID, itemlist, prev_sel, 30, 120)) != prev_sel) { + if((objsel = imtk_radiogroup(IMUID, itemlist, prev_sel, IMTK_AUTO, IMTK_AUTO)) != prev_sel) { prev_sel = objsel; glutPostRedisplay(); } imtk_free_list(itemlist); - imtk_textbox(IMUID, textbuf, sizeof textbuf, 30, 200); - imtk_textbox(IMUID, textbuf2, sizeof textbuf2, 30, 250); + imtk_textbox(IMUID, textbuf, sizeof textbuf, IMTK_AUTO, IMTK_AUTO); + imtk_textbox(IMUID, textbuf2, sizeof textbuf2, IMTK_AUTO, IMTK_AUTO); - if((bnshow = imtk_checkbox(IMUID, "show hidden button", 30, 300, bnshow))) { - if(imtk_button(IMUID, "yellow", 50, 340)) { + if((bnshow = imtk_checkbox(IMUID, "show hidden button", IMTK_AUTO, IMTK_AUTO, bnshow))) { + if(imtk_button(IMUID, "yellow", IMTK_AUTO, IMTK_AUTO)) { float color[] = {0.8, 0.75, 0.3, 1}; glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color); glutPostRedisplay(); } } - val = imtk_slider(IMUID, angle, 0.0, 360.0, 30, 390); + val = imtk_slider(IMUID, angle, 0.0, 360.0, IMTK_AUTO, IMTK_AUTO); if(val != angle) { angle = val; glutPostRedisplay(); } - imtk_progress(IMUID, val / 360.0, 30, 420); + imtk_progress(IMUID, val / 360.0, IMTK_AUTO, IMTK_AUTO); - imtk_label("alpha:", 24, 473); - imtk_set_alpha(imtk_slider(IMUID, imtk_get_alpha(), 0.0, 1.0, 60, 470)); + imtk_layout_dir(IMTK_HORIZONTAL); + imtk_label("alpha:", IMTK_AUTO, IMTK_AUTO); + imtk_set_alpha(imtk_slider(IMUID, imtk_get_alpha(), 0.0, 1.0, IMTK_AUTO, IMTK_AUTO)); + imtk_layout_dir(IMTK_VERTICAL); - if(imtk_button(IMUID, "Quit", 30, 500)) { + if(imtk_button(IMUID, "Quit", IMTK_AUTO, IMTK_AUTO)) { exit(0); }