[hackers] [swk] Added keybindings and functions to scroll and change font size || pancake

From: <hg_AT_suckless.org>
Date: Wed, 28 Apr 2010 00:20:34 +0000 (UTC)

changeset: 22:4a5d00a8a540
tag: tip
user: pancake <pancake_AT_nopcode.org>
date: Wed Apr 28 02:14:08 2010 +0200
files: config.def.h gi_sdl.c swk.c swk.h test.c
description:
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

diff -r 60dd05d78919 -r 4a5d00a8a540 config.def.h
--- a/config.def.h Tue Apr 27 02:09:26 2010 +0200
+++ b/config.def.h Wed Apr 28 02:14:08 2010 +0200
@@ -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 @@
         { 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 -r 60dd05d78919 -r 4a5d00a8a540 gi_sdl.c
--- a/gi_sdl.c Tue Apr 27 02:09:26 2010 +0200
+++ b/gi_sdl.c Wed Apr 28 02:14:08 2010 +0200
@@ -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 @@
 }
 
 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 @@
         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 @@
         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 @@
                 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 @@
                 } 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 @@
 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 @@
 
 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 @@
         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 -r 60dd05d78919 -r 4a5d00a8a540 swk.c
--- a/swk.c Tue Apr 27 02:09:26 2010 +0200
+++ b/swk.c Wed Apr 28 02:14:08 2010 +0200
@@ -23,14 +23,21 @@
 
 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 @@
         } 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 @@
                         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 -r 60dd05d78919 -r 4a5d00a8a540 swk.h
--- a/swk.h Tue Apr 27 02:09:26 2010 +0200
+++ b/swk.h Wed Apr 28 02:14:08 2010 +0200
@@ -62,6 +62,7 @@
         SwkEventCallback cb;
         char *text;
         void *data;
+ int scroll;
 };
 
 struct SwkWindow {
@@ -87,6 +88,10 @@
 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 @@
 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 -r 60dd05d78919 -r 4a5d00a8a540 test.c
--- a/test.c Tue Apr 27 02:09:26 2010 +0200
+++ b/test.c Wed Apr 28 02:14:08 2010 +0200
@@ -26,7 +26,14 @@
         { .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 @@
 // { .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 }
Received on Wed Apr 28 2010 - 00:20:34 UTC

This archive was generated by hypermail 2.2.0 : Wed Apr 28 2010 - 00:24:03 UTC