commit 3d689358f91b3dc593652bd2a9cc0a014a592b5b
parent 41c901a65ffa93711e27e2484f1d7b01cfdd5be7
Author: pancake <pancake@nopcode.org>
Date: Thu, 19 Aug 2010 00:38:17 +0200
add calculator sample application
Diffstat:
t/Makefile | | | 5 | ++++- |
t/calc.c | | | 82 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 files changed, 86 insertions(+), 1 deletion(-)
diff --git a/t/Makefile b/t/Makefile
@@ -2,7 +2,7 @@ include ../swk.mk
CFLAGS=-I.. -Wall
-all: test tlock ui
+all: test calc tlock ui
test: test.o
${CC} ${SWKLIBS} test.o -o test ../libswk.a
@@ -10,6 +10,9 @@ test: test.o
tlock: tlock.o
${CC} ${SWKLIBS} tlock.o -o tlock ../libswk.a
+calc: calc.o
+ ${CC} ${SWKLIBS} calc.o -o calc ../libswk.a
+
ui: ui.o
${CC} ${SWKLIBS} ui.o -o ui ../libswk.a
diff --git a/t/calc.c b/t/calc.c
@@ -0,0 +1,82 @@
+#include <stdio.h>
+#include <string.h>
+#include <swk.h>
+
+static int bufferi = 0;
+static char buffer[256];
+
+static void button(SwkEvent *e) {
+ if(e->type==EClick) {
+ int key = *e->box->text;
+ switch(key) {
+ case 'C':
+ bufferi = buffer[0] = 0;
+ break;
+ case '<':
+ if(bufferi>0)
+ buffer[--bufferi] = 0;
+ break;
+ case '=':
+ {
+ FILE *pd;
+ static char buffer2[sizeof(buffer)+32];
+ snprintf(buffer2, sizeof(buffer2), "echo '%s' | bc -q", buffer);
+ pd = popen(buffer2, "r");
+ fgets(buffer, sizeof(buffer),pd);
+ bufferi=strlen(buffer)-1;
+ pclose(pd);
+ }
+ break;
+ default:
+ if(bufferi<sizeof(buffer)) {
+ buffer[bufferi++] = key;
+ buffer[bufferi] = 0;
+ }
+ break;
+ }
+ }
+ return swk_bigbutton(e);
+}
+
+static SwkBox contents[] = {
+ SWK_BOX_NEWLINE(1),
+ { .cb=swk_label, .text=buffer },
+ { .cb=button, .text="<" },
+ SWK_BOX_NEWLINE(2),
+ { .cb=button, .text="1" },
+ { .cb=button, .text="2" },
+ { .cb=button, .text="3" },
+ { .cb=button, .text="C" },
+ SWK_BOX_NEWLINE(1),
+ { .cb=button, .text="4" },
+ { .cb=button, .text="5" },
+ { .cb=button, .text="6" },
+ { .cb=button, .text="+" },
+ SWK_BOX_NEWLINE(1),
+ { .cb=button, .text="7" },
+ { .cb=button, .text="8" },
+ { .cb=button, .text="9" },
+ { .cb=button, .text="-" },
+ SWK_BOX_NEWLINE(1),
+ { .cb=button, .text="%" },
+ { .cb=button, .text="0" },
+ { .cb=button, .text="/" },
+ { .cb=button, .text="*" },
+ SWK_BOX_NEWLINE(1),
+ { .cb=swk_separator },
+ { .cb=button, .text="=" },
+ { .cb=NULL }
+};
+
+int main() {
+ SwkWindow w = {
+ .title="Calculator",
+ .boxes={contents,NULL},
+ .box=contents,
+ };
+ if(!swk_use(&w))
+ return 1;
+ *buffer = 0;
+ swk_loop();
+ return 0;
+}