swk

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

commit a704775a268f1c3ff36430d778e72ca8ff63eac3
parent d85fee7cf0f1899b192d4b79b3774567ed6f42b6
Author: pancake <pancake@nopcode.org>
Date:   Wed, 28 Apr 2010 02:14:08 +0200

Added keybindings and functions to scroll and change font size
  ^+ , ^- to increase/decrease font size
  ^j, ^k  to scroll down and up the vertical filled area
No more overlapped widgets
Scrolling is possible when using vertical filler
Diffstat:
config.def.h | 11+++++++++++
gi_sdl.c | 48++++++++++++++++++++++++++++--------------------
swk.c | 65+++++++++++++++++++++++++++++++++++++++++++++++++++--------------
swk.h | 6++++++
test.c | 11+++++++++--
5 files changed, 105 insertions(+), 36 deletions(-)

diff --git a/config.def.h b/config.def.h @@ -1,5 +1,12 @@ /* See LICENSE file for copyright and license details. */ +//#define N900 +#ifdef N900 +#define FONTSIZE 32 +#else +#define FONTSIZE 14 +#endif + /* appearance */ #define FONTSIZE 14 #define FONTBOLD 1 @@ -29,5 +36,9 @@ static SwkKeyBind keys[] = { { 0 , KDown, swk_focus_next }, { 0 , 13 , swk_focus_activate }, { Ctrl, 12 , swk_focus_activate }, + { Ctrl, 10, swk_scroll_up }, + { Ctrl, 11, swk_scroll_down }, + { Ctrl, '+', swk_fontsize_increase }, + { Ctrl, '-', swk_fontsize_decrease }, { 0 } }; diff --git a/gi_sdl.c b/gi_sdl.c @@ -6,10 +6,10 @@ #include "config.h" #define FONTNAME "Inconsolata.otf" -#define FS FONTSIZE #define BPP 32 #define SDLFLAGS SDL_DOUBLEBUF|SDL_RESIZABLE +static int fs = FONTSIZE; static Uint32 pal[ColorLast]; static SDL_Color fontcolor = { TFCOLOR }; static SDL_Color bgcolor = { BGCOLOR }; @@ -47,6 +47,19 @@ static void putpixel(int x, int y, Uint32 pixel) { } int +swk_gi_fontsize(int sz) { + fs += sz*2; + font = TTF_OpenFont(FONTNAME, fs); + if(font == NULL) { + fprintf(stderr, "Cannot open font '%s'\n", FONTNAME); + return 0; + } else + if (FONTBOLD) + TTF_SetFontStyle(font, TTF_STYLE_BOLD); + return 1; +} + +int swk_gi_init(SwkWindow *w) { if(SDL_Init(SDL_INIT_VIDEO)) { fprintf(stderr, "Cannot initialize SDL\n"); @@ -64,14 +77,7 @@ swk_gi_init(SwkWindow *w) { pal[ColorFG] = SDL_MapRGB(screen->format, FGCOLOR); pal[ColorBG] = SDL_MapRGB(screen->format, BGCOLOR); pal[ColorHI] = SDL_MapRGB(screen->format, HICOLOR); - font = TTF_OpenFont(FONTNAME, FS); - if(font == NULL) { - fprintf(stderr, "Cannot open font '%s'\n", FONTNAME); - return 0; - } else - if (FONTBOLD) - TTF_SetFontStyle(font, TTF_STYLE_BOLD); - return 1; + return swk_gi_fontsize(0); } int @@ -79,8 +85,8 @@ swk_gi_update(SwkWindow *w) { screen = SDL_GetVideoSurface(); if (screen == NULL) return 0; - w->r.w = (screen->w / FS)-1; - w->r.h = (screen->h / FS)-1; + w->r.w = (screen->w / fs)-1; + w->r.h = (screen->h / fs)-1; return 1; } @@ -118,16 +124,16 @@ swk_gi_event(SwkWindow *w, int dowait) { break; case SDL_MOUSEMOTION: ret->type = EMotion; - ret->data.motion.x = event.motion.x / FS; - ret->data.motion.y = event.motion.y / FS; + ret->data.motion.x = event.motion.x / fs; + ret->data.motion.y = event.motion.y / fs; // fprintf(stderr, "event: motion %d %d\n", // event.motion.x, event.motion.y); break; case SDL_MOUSEBUTTONDOWN: 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; + ret->data.click.point.x = event.button.x / fs; + ret->data.click.point.y = event.button.y / fs; fprintf(stderr, "event: click %d\n", event.button.button); break; case SDL_KEYDOWN: @@ -148,9 +154,11 @@ swk_gi_event(SwkWindow *w, int dowait) { } else { // TODO key aliases defined in config.h switch(event.key.keysym.sym) { + case 1073741906: // n900 up key case 273: ret->data.key.keycode = KUp; break; + case 1073741912: // n900 down key case 274: ret->data.key.keycode = KDown; break; @@ -185,8 +193,8 @@ swk_gi_flip() { void swk_gi_line(int x1, int y1, int x2, int y2, int color) { int i; - x1 *= FS; y1 *= FS; - x2 *= FS; y2 *= FS; + 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]); @@ -194,9 +202,9 @@ swk_gi_line(int x1, int y1, int x2, int y2, int color) { void swk_gi_fill(Rect r, int color, int lil) { - SDL_Rect area = { r.x*FS, r.y*FS, r.w*FS, r.h*FS }; + SDL_Rect area = { r.x*fs, r.y*fs, r.w*fs, r.h*fs }; if (lil) { - const int s = FS/4; + const int s = fs/4; area.x += s; area.y += s; area.w -= (s*2); @@ -226,7 +234,7 @@ swk_gi_text(Rect r, const char *text) { if(*text) { SDL_Surface *ts = TTF_RenderText_Shaded(font, text, fontcolor, bgcolor); if(ts) { - SDL_Rect to = { (r.x)*FS, r.y*FS, ts->w, ts->h }; + SDL_Rect to = { (r.x)*fs, r.y*fs, ts->w, ts->h }; SDL_BlitSurface(ts, NULL, screen, &to); SDL_FreeSurface(ts); } else fprintf(stderr, "Cannot render string (%s)\n", text); diff --git a/swk.c b/swk.c @@ -23,14 +23,21 @@ swk_init(SwkWindow *w) { void swk_update(SwkWindow *w) { + int roy, oy, skip = 0; w->_e.type = EExpose; if(swk_gi_update(w)) { SwkBox *b = w->boxes; swk_fit(w); swk_gi_clear(); + roy = oy = 0; for(;b->cb; b++) { w->_e.box = b; - b->cb(&w->_e); + if (b->r.w==-1 && b->r.h==-1 && ((int)(size_t)b->data)<0) + roy = oy+1; + if (roy && b->r.y < roy) + swk_gi_line(0, roy, w->r.w, 0, ColorHI); + else b->cb(&w->_e); + oy = b->r.y; } swk_gi_flip(); } else w->running = 0; @@ -50,6 +57,40 @@ swk_loop(SwkWindow *w) { } while(!e || e->type != EQuit); } +void +swk_fontsize_increase(SwkWindow *w) { + swk_gi_fontsize(1); + swk_update(w); +} + +void +swk_fontsize_decrease(SwkWindow *w) { + swk_gi_fontsize(-1); + swk_update(w); +} + +void +swk_scroll_up(SwkWindow *w) { + SwkBox *b = w->boxes; + for(; b->cb; b++) + if (b->r.w==-1 && b->r.h==-1 && ((int)(size_t)b->data)<0) { + b->scroll++; + return; + } + fprintf(stderr, "Cannot scroll. no vfiller\n"); +} + +void +swk_scroll_down(SwkWindow *w) { + SwkBox *b = w->boxes; + for(; b->cb; b++) + if (b->r.w==-1 && b->r.h==-1 && ((int)(size_t)b->data)<0) { + b->scroll--; + return; + } + fprintf(stderr, "Cannot scroll. no vfiller\n"); +} + static void swk_fit_row(SwkWindow *w, SwkBox *a, SwkBox *b, int y) { int count, x = 0; SwkBox *btmp; @@ -76,28 +117,24 @@ countrows(SwkBox *b) { row += (int)(size_t)b->data; else row += b->r.h; } - return row; + return (1+row) * 0.7; // hacky } void swk_fit(SwkWindow *w) { - int x, yi, y = 0; + int skip = 0; + int x, y = 0; SwkBox *b, *b2; for(b=b2=w->boxes; b->cb; b++) { if(b->r.w==-1 && b->r.h==-1) { x = (int)(size_t)b->data; - if (x>0) { - swk_fit_row(w, b2, b, y); - y += (int)(size_t)b->data; - b2 = b+1; - } else { - swk_fit_row(w, b2, b, y); - b2 = b+1; - yi = (w->r.h-countrows(b2)); - if (yi<2) y += 2; - else y += yi; - } + swk_fit_row(w, b2, b, y); + y+=x-skip+b->scroll; + // vertical align // + if(x<0) y+=(w->r.h-countrows(b2)); + b2 = b+1; } + // printf ("%d %d\n", y, b->scroll); } swk_fit_row(w, b2, b, y); } diff --git a/swk.h b/swk.h @@ -62,6 +62,7 @@ struct SwkBox { SwkEventCallback cb; char *text; void *data; + int scroll; }; struct SwkWindow { @@ -87,6 +88,10 @@ 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_scroll_up(SwkWindow *w); +void swk_scroll_down(SwkWindow *w); +void swk_fontsize_increase(SwkWindow *w); +void swk_fontsize_decrease(SwkWindow *w); void swk_button(SwkEvent *e); void swk_label(SwkEvent *e); @@ -103,6 +108,7 @@ void swk_gi_exit(); SwkEvent *swk_gi_event(SwkWindow *w, int dowait); int swk_gi_update(SwkWindow *w); int swk_gi_has_event(SwkWindow *w); +int swk_gi_fontsize(int sz); /* FIXME: don't these need SwkWindow *w state, to avoid static'ness? */ void swk_gi_clear(); diff --git a/test.c b/test.c @@ -26,7 +26,14 @@ static SwkBox about[] = { { .cb=swk_label, .text="About this program...", }, SWK_BOX_NEWLINE(1), { .cb=swk_separator }, - { .cb=swk_label, .text="This program aims to be\nfor hackers\nand developers\n" }, + SWK_BOX_NEWLINE(2), + { .cb=swk_label, .text="This is a test program for swk" }, +#if 0 + SWK_BOX_NEWLINE(1), + { .cb=swk_label, .text=" ... a simple widget kit " }, + SWK_BOX_NEWLINE(1), + { .cb=swk_label, .text=" ... from the suckless.org project" }, +#endif SWK_BOX_NEWLINE(-1), { .cb=swk_filler }, { .cb=mybutton_about_ok, .text="Ok" }, @@ -70,7 +77,7 @@ static SwkBox helloworld[] = { // { .cb=swk_option, .text="null" }, SWK_BOX_NEWLINE(1), { .cb=swk_option, .text="pasta barata", .data=&opt }, - SWK_BOX_NEWLINE(5), + SWK_BOX_NEWLINE(2), { .cb=swk_label, .text="--swktest", }, { .cb=mybutton_about, .text="about" }, { .cb=NULL }