swk

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

commit a6dd2b8e9b5b6a81c0fc49ae14a55863f9842baa
parent c0c4fcebb32193be96cc7e8f9d9ac0dcf323d233
Author: pancake <pancake@nopcode.org>
Date:   Wed,  5 May 2010 11:12:02 +0200

* Fix click/scroll event handling
* Use constants in putpixel (reduce LOC and complexity)
  - Remove useless space chars
* Minor cleanup for config.def.h
  - remove N900 stuff, drop bold fonts
Diffstat:
README | 13++++++++-----
config.def.h | 38++++++++++++++++----------------------
gi_sdl.c | 51+++++++++++++++++++++++++++------------------------
3 files changed, 51 insertions(+), 51 deletions(-)

diff --git a/README b/README @@ -1,8 +1,11 @@ SWK - simple widget kit ======================= - +On ArchLinunx: pacman -S sdl sdl_ttf sdl_image +On Debian/Ubuntu: + apt-get install libsdl-dev libsdl-ttf libsdl-image + Usability guideline =================== Default keybindings are defined in config.h @@ -12,10 +15,10 @@ TODO * support for clipboard (implemented in gi_ backend) * simple way to define callback for buttons instead of reimplementing widget * scroll on focus -* mouse bindings -* allow widgets to define their boundaries to force size -* add support to resize images -* only focus buttons and entries +* mouse bindings in config.h +* allow widgets to define to force size (height, width) +* add support to resize images (scaling) +* only focus buttons and entries (input widgets) * add function to click button 1, 2, 3, ... * allow to load images from memory instead of files * default image path diff --git a/config.def.h b/config.def.h @@ -1,14 +1,8 @@ /* See LICENSE file for copyright and license details. */ -//#define N900 -#ifdef N900 -#define FONTSIZE 32 -#else -#define FONTSIZE 28 -#endif - /* appearance */ -#define FONTBOLD 1 +#define FONTSIZE 28 +#define FONTBOLD 0 #define WINWIDTH 640 #define WINHEIGHT 480 // SDL @@ -23,22 +17,22 @@ /* 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, 225, swk_focus_activate }, // n900 enter - { 0 , KUp, swk_focus_prev }, + { 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, 225 , swk_focus_activate }, // n900 return + { 0 , KUp, swk_focus_prev }, { 0 , KDown, swk_focus_next }, - { 0 , 13 , swk_focus_activate }, - { Ctrl, 12 , swk_focus_activate }, + { 0 , 13 , swk_focus_activate }, + { Ctrl, 12 , swk_focus_activate }, { Ctrl|Shift, 10, swk_scroll_down }, { Ctrl|Shift, 11, swk_scroll_up }, - { Ctrl, '+', swk_fontsize_increase }, - { Ctrl, '-', swk_fontsize_decrease }, + { Ctrl, '+', swk_fontsize_increase }, + { Ctrl, '-', swk_fontsize_decrease }, { 0 } }; diff --git a/gi_sdl.c b/gi_sdl.c @@ -16,34 +16,31 @@ static SDL_Color fontcolor = { TFCOLOR }; static SDL_Color bgcolor = { BGCOLOR }; static SDL_Surface *screen = NULL; static TTF_Font *font = NULL; -/* FIXME: put ugly statics into void *aux of SwkWindow */ +/* FIXME: put ugly statics into void *aux of SwkWindow ? */ static int has_event = 0; static SDL_Event lastev = { .type=-1 }; static void putpixel(SDL_Surface *scr, int x, int y, Uint32 pixel) { - Uint8 *p, *pend; - int delta, bpp = scr->format->BytesPerPixel; - delta = y * scr->pitch + x * bpp; - p = (Uint8 *)scr->pixels + delta; - pend = (Uint8 *)scr->pixels + (scr->h*scr->w*bpp); + Uint8 *p = (Uint8 *)scr->pixels + (y*scr->pitch+x*(BPP/8)); + Uint8 *pend = (Uint8 *)scr->pixels + (scr->h*scr->w*(BPP/8)); if((p<((Uint8 *)scr->pixels)) || (p>=pend)) return; #if BPP == 8 - *p = pixel; + *p = pixel; #elif BPP == 16 *(Uint16 *)p = pixel; #elif BPP == 24 # if SDL_BYTEORDER == SDL_BIG_ENDIAN - p[0] = (pixel >> 16) & 0xff; - p[1] = (pixel >> 8) & 0xff; - p[2] = pixel & 0xff; + p[0] = (pixel >> 16) & 0xff; + p[1] = (pixel >> 8) & 0xff; + p[2] = pixel & 0xff; # else - p[0] = pixel & 0xff; - p[1] = (pixel >> 8) & 0xff; - p[2] = (pixel >> 16) & 0xff; + p[0] = pixel & 0xff; + p[1] = (pixel >> 8) & 0xff; + p[2] = (pixel >> 16) & 0xff; # endif #elif BPP == 32 - *(Uint32 *)p = pixel; + *(Uint32 *)p = pixel; #endif } @@ -108,6 +105,7 @@ swk_gi_has_event(SwkWindow *w) { SwkEvent * swk_gi_event(SwkWindow *w, int dowait) { static int mousedowny, mousedown = 0; + static int mousemoved = 0; SDL_Event event; SwkEvent *ret = &w->_e; @@ -138,6 +136,9 @@ swk_gi_event(SwkWindow *w, int dowait) { swk_scroll_down(w); swk_scroll_down(w); } + ret->type = EExpose; + ret->data.expose.x = ret->data.expose.y = \ + ret->data.expose.w = ret->data.expose.h = 0; } else { ret->type = EMotion; ret->data.motion.x = event.motion.x / fs; @@ -146,15 +147,18 @@ swk_gi_event(SwkWindow *w, int dowait) { break; case SDL_MOUSEBUTTONUP: mousedown = 0; + if(!mousemoved) { + fprintf(stderr, "event: click %d\n", event.button.button); + ret->type = EClick; + ret->data.click.button = event.button.button; + ret->data.click.point.x = event.button.x / fs; + ret->data.click.point.y = event.button.y / fs; + } break; case SDL_MOUSEBUTTONDOWN: + mousemoved = 0; mousedown = 1; mousedowny = event.button.y; - fprintf(stderr, "event: click %d\n", event.button.button); - ret->type = EClick; - ret->data.click.button = event.button.button; - ret->data.click.point.x = event.button.x / fs; - ret->data.click.point.y = event.button.y / fs; break; case SDL_KEYDOWN: ret->data.key.modmask = 0; @@ -173,7 +177,7 @@ swk_gi_event(SwkWindow *w, int dowait) { ret->data.key.modmask, ret->data.key.keycode); } else { // TODO key aliases defined in config.h - switch(event.key.keysym.sym) { + switch((int)event.key.keysym.sym) { case 1073741906: // n900 up key case 273: ret->data.key.keycode = KUp; @@ -189,7 +193,7 @@ swk_gi_event(SwkWindow *w, int dowait) { } break; case SDL_QUIT: - ret->type = ret->type = EQuit; + ret->type = EQuit; break; } has_event = 0; @@ -210,7 +214,6 @@ swk_gi_flip() { } /* -- drawing primitives -- */ - void swk_gi_line(int x1, int y1, int x2, int y2, int color) { int i; @@ -268,7 +271,7 @@ void swk_gi_img(Rect r, void *img) { SDL_Surface *s = (SDL_Surface*)img; SDL_Rect area = { r.x*fs, r.y*fs, r.w*fs, r.h*fs }; - if (s) SDL_BlitSurface(s, NULL, screen, &area); + if(s) SDL_BlitSurface(s, NULL, screen, &area); } void* @@ -284,7 +287,7 @@ swk_gi_img_free(void *s) { void swk_gi_img_set(void *img, int x, int y, int color) { SDL_Surface *s = (SDL_Surface*)img; - if (s) putpixel(s, x, y, color); + if(s) putpixel(s, x, y, color); } int