swk

static widget kit
git clone git://git.suckless.org/swk
Log | Files | Refs | README | LICENSE

commit ed732914f2977b61230b569e9aed9ecbe29bc69e
parent ba11625044577e8dd8126a48b7d0c01eb3862b59
Author: pancake <pancake@nopcode.org>
Date:   Mon, 26 Apr 2010 00:13:21 +0200

added config.h to configure colors, font size and keys
implied simplification in gi_sdl
Diffstat:
Makefile | 9+++++++--
config.def.h | 30++++++++++++++++++++++++++++++
gi_sdl.c | 10+++++++---
swk.c | 52+++++++++++++++++-----------------------------------
swk.h | 16++++++++++++----
test.c | 3++-
6 files changed, 75 insertions(+), 45 deletions(-)

diff --git a/Makefile b/Makefile @@ -14,7 +14,10 @@ GI_OBJS=gi_${GI}.o all: static test -test: test.o libswk.a +config.h: + cp config.def.h config.h + +test: config.h test.o libswk.a ${CC} test.o -o test libswk.a ${GI_LIBS} clean: @@ -30,7 +33,9 @@ install: static: libswk.a -libswk.a: swk.o ${GI_OBJS} +swk.o: config.h + +libswk.a: config.h swk.o ${GI_OBJS} rm -f libswk.a ar qcvf libswk.a swk.o ${GI_OBJS} echo CFLAGS+=-I${PREFIX}/include > swk.mk diff --git a/config.def.h b/config.def.h @@ -0,0 +1,30 @@ +/* See LICENSE file for copyright and license details. */ + +/* appearance */ +#define FONTSIZE 14 +// SDL +#define SWK_COLOR(r,g,b) 0x##r,0x##g,0x##b +// X11 +//#define SWK_COLOR(r,g,b) r##g##b + +#define HICOLOR SWK_COLOR(0,66,ff) +#define FGCOLOR SWK_COLOR(ff,ff,ff) +#define BGCOLOR SWK_COLOR(00,00,00) +#define TFCOLOR SWK_COLOR(cc,cc,cc) + +/* key bindings */ +static SwkKeyBind keys[] = { + { Ctrl, 'j', swk_focus_next }, + { Ctrl, 'k', swk_focus_prev }, + { Ctrl, 8 , swk_focus_first }, + { Ctrl, 9 , swk_focus_prev }, + { 0 , 9 , swk_focus_next }, + { Ctrl, 10 , swk_focus_next }, + { Ctrl, 11 , swk_focus_prev }, + { Ctrl, 12 , swk_focus_activate }, + { 0 , KUp, swk_focus_prev }, + { 0 , KDown, swk_focus_next }, + { 0 , 13 , swk_focus_activate }, + { Ctrl, 12 , swk_focus_activate }, + { 0 } +}; diff --git a/gi_sdl.c b/gi_sdl.c @@ -2,17 +2,22 @@ #include <SDL/SDL.h> #include <SDL/SDL_ttf.h> #include "swk.h" +#include "config.h" +//#define SWK_COLOR(r,g,b) 0x##r,0x##g,0x##b + +#if 0 #define HICOLOR 0x00,0x66,0xff #define FGCOLOR 0xff,0xff,0xff #define BGCOLOR 0x00,0x00,0x00 #define TFCOLOR 0xcc,0xcc,0xcc +#endif + +/* --- */ #define FONTNAME "Inconsolata.otf" -#define FONTSIZE 14 #define FS FONTSIZE #define BPP 32 #define SDLFLAGS SDL_DOUBLEBUF|SDL_RESIZABLE -/* --- */ static Uint32 pal[ColorLast]; static SDL_Color fontcolor = { TFCOLOR }; @@ -181,7 +186,6 @@ swk_gi_line(int x1, int y1, int x2, int y2, int color) { int i; x1 *= FS; y1 *= FS; x2 *= FS; y2 *= FS; - if(x2==0) for(i=0;i<y2;i++) putpixel(x1, y1+i, pal[color]); else if(y2==0) for(i=0;i<x2;i++) putpixel(x1+i, y1, pal[color]); diff --git a/swk.c b/swk.c @@ -3,6 +3,7 @@ #include <string.h> #include <stdlib.h> #include "swk.h" +#include "config.h" // move into SwkWindow* ? static int running = 0; @@ -10,7 +11,8 @@ static int running = 0; int swk_init(SwkWindow *w) { w->_e.win = w; - swk_focus_first(w); + if (w->box == NULL) + swk_focus_first(w); if(w->r.w == 0 || w->r.h == 0) { w->r.w = 640; w->r.h = 480; @@ -88,6 +90,12 @@ swk_has_event(SwkWindow *w) { return swk_gi_has_event(w); } +void +swk_focus_activate(SwkWindow *w) { + w->_e.box = w->box; + w->_e.type = EClick; +} + SwkEvent * swk_next_event(SwkWindow *w) { if(running) @@ -99,45 +107,19 @@ swk_next_event(SwkWindow *w) { void swk_handle_event(SwkEvent *e) { + int i; SwkBox *b; switch(e->type) { case EKey: - // TODO: ^F ullscreen? handle ^Y and ^P to copypasta box->text - // ^A focus first widget, ^E focus last widget ? - if(e->data.key.modmask == 2) { - switch(e->data.key.keycode) { - case 8: - swk_focus_first(e->win); - break; - case 10: - swk_focus_next(e->win); - break; - case 11: - swk_focus_prev(e->win); - break; - case 12: - e->type = EClick; + for(i=0; keys[i].cb; i++) { + if (e->data.key.modmask == keys[i].modmask + && e->data.key.keycode == keys[i].keycode) { + keys[i].cb(e->win); break; } - } else - switch(e->data.key.keycode) { - case KUp: - swk_focus_prev(e->win); - break; - case KDown: - swk_focus_next(e->win); - break; - case 9: // TAB - if(e->data.key.modmask) - swk_focus_prev(e->win); - else swk_focus_next(e->win); - swk_update(e->win); - break; - case 13: // ENTER - e->box = e->win->box; - e->type = EClick; - break; - case 27: // ESC + } + /* XXX: this must be implemented in app? */ + if (e->data.key.keycode==27) { e->box = e->win->box; e->type = EQuit; swk_exit(); diff --git a/swk.h b/swk.h @@ -9,7 +9,10 @@ typedef enum { ColorFG, ColorBG, ColorHI, ColorLast } Palete; typedef enum { KUp=0xe0, KDown=0xe1, KLeft=0xe2, KRight=0xe3 } SwkKeyCode; typedef struct SwkBox SwkBox; +typedef struct SwkEvent SwkEvent; typedef struct SwkWindow SwkWindow; +typedef void (*SwkEventCallback)(SwkEvent *e); +typedef void (*SwkKeyCallback)(SwkWindow *w); typedef struct { int x; @@ -30,11 +33,17 @@ typedef struct { } Click; typedef struct { - int keycode; int modmask; + int keycode; } Key; typedef struct { + int modmask; + int keycode; + SwkKeyCallback cb; +} SwkKeyBind; + +struct SwkEvent { SwkEventType type; SwkBox *box; SwkWindow *win; @@ -45,9 +54,7 @@ typedef struct { Rect expose; int rows; } data; -} SwkEvent; - -typedef void (*SwkEventCallback)(SwkEvent *e); +}; struct SwkBox { Rect r; @@ -77,6 +84,7 @@ void swk_handle_event(SwkEvent *e); void swk_focus_first(SwkWindow *w); void swk_focus_next(SwkWindow *w); void swk_focus_prev(SwkWindow *w); +void swk_focus_activate(SwkWindow *w); void swk_button(SwkEvent *e); void swk_label(SwkEvent *e); diff --git a/test.c b/test.c @@ -38,7 +38,8 @@ int main() { SwkWindow w = { .title="Hello World", - .boxes=helloworld + .boxes=helloworld, + .box=helloworld+10 }; if(!swk_init(&w)) return 1;