[hackers] [swk] add calculator sample application || pancake

From: <hg_AT_suckless.org>
Date: Wed, 18 Aug 2010 22:41:48 +0000 (UTC)

changeset: 56:83288bf8f040
tag: tip
user: pancake <pancake_AT_nopcode.org>
date: Thu Aug 19 00:38:17 2010 +0200
files: t/Makefile t/calc.c
description:
add calculator sample application

diff -r dc5cdef1af18 -r 83288bf8f040 t/Makefile
--- a/t/Makefile Wed Aug 18 23:30:10 2010 +0200
+++ b/t/Makefile Thu Aug 19 00:38:17 2010 +0200
@@ -2,7 +2,7 @@
 
 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 @@
 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 -r dc5cdef1af18 -r 83288bf8f040 t/calc.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/t/calc.c Thu Aug 19 00:38:17 2010 +0200
@@ -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;
+}
Received on Thu Aug 19 2010 - 00:41:48 CEST

This archive was generated by hypermail 2.2.0 : Thu Aug 19 2010 - 00:48:04 CEST