# HG changeset patch # User Thinker K.F. Li # Date 1219476407 -28800 # Node ID dd813dcc232cb0d9183689881502635c63caafc8 # Parent 8dfebf76cb5984d26fd1dbc4508b1451a1784b28 New example, calculator. diff -r 8dfebf76cb59 -r dd813dcc232c examples/calculator/Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/calculator/Makefile Sat Aug 23 15:26:47 2008 +0800 @@ -0,0 +1,35 @@ +SVG=calculator_scr.svg +TOOLSDIR=../../tools +INCS=-I../../src +CFLAGS+=`pkg-config --cflags cairo` $(INCS) -Wall +LDFLAGS=-L../../src/ `pkg-config --libs cairo` +LIBS=-lmbfly +BINS= calc + +all: $(BINS) + +calc: main.o $(SVG:C/.svg/.o/) + $(CC) $(LDFLAGS) -o $@ $(.ALLSRC) $(LIBS) + +main.o: main.c $(SVG:C/.svg/.h/) + $(CC) $(CFLAGS) -c -o $@ main.c + +$(SVG:C/.svg/.o/): $(SVG:C/.svg/.c/) $(SVG:C/.svg/.h/) + $(CC) -c $(CFLAGS) -o $@ $(SVG:C/.svg/.c/) + +$(SVG:C/.svg/.mb/): $(SVG) + $(TOOLSDIR)/svg2code.py $(.ALLSRC) $@ + +$(SVG:C/.svg/.c/): $(SVG:C/.svg/.mb/) + m4 -I $(TOOLSDIR) mb_c_source.m4 $(.ALLSRC) > $@ + +$(SVG:C/.svg/.h/): $(SVG:C/.svg/.mb/) + m4 -I $(TOOLSDIR) mb_c_header.m4 $(.ALLSRC) > $@ + +clean: + for i in *.mb *.o *~ $(SVG:C/.svg/.c/) $(SVG:C/.svg/.h/) $(BINS); do \ + if [ -e "$$i" ]; then \ + echo "delete $$i"; \ + rm -f "$$i"; \ + fi; \ + done \ No newline at end of file diff -r 8dfebf76cb59 -r dd813dcc232c examples/calculator/calculator_scr.svg --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/calculator/calculator_scr.svg Sat Aug 23 15:26:47 2008 +0800 @@ -0,0 +1,512 @@ + + + + + + + + + + + + + + + + + + + + + + + + + image/svg+xml + + + + + + + + + + 0 + + + + 1 + + + + 2 + + + + 3 + + + + 4 + + + + 5 + + + + 6 + + + + 7 + + + + 8 + + + + 9 + + + + + + + + + - + + + + * + + + + / + + + + = + + + + clr + + + 0 + + diff -r 8dfebf76cb59 -r dd813dcc232c examples/calculator/main.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/calculator/main.c Sat Aug 23 15:26:47 2008 +0800 @@ -0,0 +1,199 @@ +#include +#include +#include +#include +#include +#include "calculator_scr.h" + +typedef struct _ex_rt ex_rt_t; +struct _ex_rt { + X_MB_runtime_t *rt; + calculator_scr_t *code; +}; + +static struct { + int c; + int off; +} tgt_list[] = { + { 0, OFFSET(calculator_scr_t, but_0) }, + { 1, OFFSET(calculator_scr_t, but_1) }, + { 2, OFFSET(calculator_scr_t, but_2) }, + { 3, OFFSET(calculator_scr_t, but_3) }, + { 4, OFFSET(calculator_scr_t, but_4) }, + { 5, OFFSET(calculator_scr_t, but_5) }, + { 6, OFFSET(calculator_scr_t, but_6) }, + { 7, OFFSET(calculator_scr_t, but_7) }, + { 8, OFFSET(calculator_scr_t, but_8) }, + { 9, OFFSET(calculator_scr_t, but_9) }, + { '+', OFFSET(calculator_scr_t, but_add) }, + { '-', OFFSET(calculator_scr_t, but_minus) }, + { '*', OFFSET(calculator_scr_t, but_mul) }, + { '/', OFFSET(calculator_scr_t, but_div) }, + { '=', OFFSET(calculator_scr_t, but_eq) }, + { 'c', OFFSET(calculator_scr_t, but_clr) } +}; + +static int real_compute(int op, int v1, int v2) { + int r = v2; + + switch(op) { + case '+': + r = v1 + v2; + break; + case '-': + r = v1 - v2; + break; + case '*': + r = v1 * v2; + break; + case '/': + r = v1 / v2; + break; + } + + return r; +} + +static void show_text(ex_rt_t *ex_rt, int num) { + char buf[20]; + + sprintf(buf, "%d", num); + sh_text_set_text(ex_rt->code->screen_text, buf); + rdman_shape_changed(ex_rt->rt->rdman, ex_rt->code->screen_text); +} + +static void compute(ex_rt_t *ex_rt, coord_t *tgt) { + int i; + coord_t **coord_p; + static int num = 0; + static int saved = 0; + static int op = 0; + + for(i = 0; i < 16; i++) { + coord_p = (coord_t **)((void *)ex_rt->code + tgt_list[i].off); + if(*coord_p == (void *)tgt) + break; + } + if(i >= 16) return; + + if(i < 10) { + num = num * 10 + i; + show_text(ex_rt, num); + } else { + switch(tgt_list[i].c) { + case 'c': + saved = num = 0; + show_text(ex_rt, 0); + break; + + case '+': + saved = real_compute(op, saved, num); + show_text(ex_rt, saved); + op = '+'; + num = 0; + break; + + case '-': + saved = real_compute(op, saved, num); + show_text(ex_rt, saved); + op = '-'; + num = 0; + break; + + case '*': + saved = real_compute(op, saved, num); + show_text(ex_rt, saved); + op = '*'; + num = 0; + break; + + case '/': + saved = real_compute(op, saved, num); + show_text(ex_rt, saved); + op = '/'; + num = 0; + break; + + case '=': + saved = real_compute(op, saved, num); + show_text(ex_rt, saved); + num = 0; + break; + } + } + rdman_redraw_changed(ex_rt->rt->rdman); +} + +static void buttons_handler(event_t *evt, void *arg) { + ex_rt_t *ex_rt = (ex_rt_t *)arg; + + switch(evt->type) { + case EVT_MOUSE_BUT_PRESS: + compute(ex_rt, (coord_t *)evt->cur_tgt); + break; + } +} + +static void setup_observers(ex_rt_t *ex_rt) { + calculator_scr_t *calculator_scr; + ob_factory_t *factory; + subject_t *subject; + + calculator_scr = ex_rt->code; + factory = rdman_get_ob_factory(ex_rt->rt->rdman); + + subject = coord_get_mouse_event(calculator_scr->but_0); + subject_add_observer(factory, subject, buttons_handler, ex_rt); + subject = coord_get_mouse_event(calculator_scr->but_1); + subject_add_observer(factory, subject, buttons_handler, ex_rt); + subject = coord_get_mouse_event(calculator_scr->but_2); + subject_add_observer(factory, subject, buttons_handler, ex_rt); + subject = coord_get_mouse_event(calculator_scr->but_3); + subject_add_observer(factory, subject, buttons_handler, ex_rt); + subject = coord_get_mouse_event(calculator_scr->but_4); + subject_add_observer(factory, subject, buttons_handler, ex_rt); + subject = coord_get_mouse_event(calculator_scr->but_5); + subject_add_observer(factory, subject, buttons_handler, ex_rt); + subject = coord_get_mouse_event(calculator_scr->but_6); + subject_add_observer(factory, subject, buttons_handler, ex_rt); + subject = coord_get_mouse_event(calculator_scr->but_7); + subject_add_observer(factory, subject, buttons_handler, ex_rt); + subject = coord_get_mouse_event(calculator_scr->but_8); + subject_add_observer(factory, subject, buttons_handler, ex_rt); + subject = coord_get_mouse_event(calculator_scr->but_9); + subject_add_observer(factory, subject, buttons_handler, ex_rt); + subject = coord_get_mouse_event(calculator_scr->but_add); + subject_add_observer(factory, subject, buttons_handler, ex_rt); + subject = coord_get_mouse_event(calculator_scr->but_minus); + subject_add_observer(factory, subject, buttons_handler, ex_rt); + subject = coord_get_mouse_event(calculator_scr->but_mul); + subject_add_observer(factory, subject, buttons_handler, ex_rt); + subject = coord_get_mouse_event(calculator_scr->but_div); + subject_add_observer(factory, subject, buttons_handler, ex_rt); + subject = coord_get_mouse_event(calculator_scr->but_eq); + subject_add_observer(factory, subject, buttons_handler, ex_rt); + subject = coord_get_mouse_event(calculator_scr->but_clr); + subject_add_observer(factory, subject, buttons_handler, ex_rt); +} + +int main(int argc, char * const argv[]) { + X_MB_runtime_t rt; + calculator_scr_t *calculator_scr; + ex_rt_t ex_rt; + int r; + + r = X_MB_init(":0.0", 300, 400, &rt); + + calculator_scr = calculator_scr_new(rt.rdman); + + ex_rt.rt = &rt; + ex_rt.code = calculator_scr; + setup_observers(&ex_rt); + + X_MB_handle_connection(&rt); + + calculator_scr_free(calculator_scr); + X_MB_destroy(&rt); + + return 0; +} diff -r 8dfebf76cb59 -r dd813dcc232c src/shape_text.c --- a/src/shape_text.c Fri Aug 22 16:41:11 2008 +0800 +++ b/src/shape_text.c Sat Aug 23 15:26:47 2008 +0800 @@ -58,6 +58,15 @@ return (shape_t *)text; } +extern void sh_text_set_text(shape_t *shape, const char *txt) { + sh_text_t *text = (sh_text_t *)shape; + char *buf; + + buf = strdup(txt); + if(text->data) free(text->data); + text->data = buf; +} + static int get_extents(sh_text_t *text, cairo_text_extents_t *extents) { cairo_matrix_t fmatrix; cairo_matrix_t ctm; diff -r 8dfebf76cb59 -r dd813dcc232c src/shapes.h --- a/src/shapes.h Fri Aug 22 16:41:11 2008 +0800 +++ b/src/shapes.h Sat Aug 23 15:26:47 2008 +0800 @@ -27,6 +27,7 @@ extern shape_t *sh_text_new(const char *txt, co_aix x, co_aix y, co_aix font_size, cairo_font_face_t *face); +extern void sh_text_set_text(shape_t *shape, const char *txt); extern void sh_text_transform(shape_t *shape); extern void sh_text_draw(shape_t *shape, cairo_t *cr);