[hackers] [swk] swk_entry initial implementation || pancake

From: <hg_AT_suckless.org>
Date: Wed, 21 Apr 2010 10:29:08 +0000 (UTC)

changeset: 12:477047c05136
tag: tip
user: pancake <pancake_AT_nopcode.org>
date: Wed Apr 21 12:22:34 2010 +0200
files: gi_sdl.c swk.c swk.h test.c
description:
swk_entry initial implementation
focus_{prev|next} iterates only over non swk_fillers
highlight label widgets
extended test example

diff -r 25840162d26b -r 477047c05136 gi_sdl.c
--- a/gi_sdl.c Wed Apr 21 03:35:01 2010 +0200
+++ b/gi_sdl.c Wed Apr 21 12:22:34 2010 +0200
@@ -13,7 +13,6 @@
 #define BPP 32
 /* --- */
 
-
 static Uint32 pal[ColorLast];
 static SDL_Color fontcolor = { TFCOLOR };
 static SDL_Surface *screen = NULL;
diff -r 25840162d26b -r 477047c05136 swk.c
--- a/swk.c Wed Apr 21 03:35:01 2010 +0200
+++ b/swk.c Wed Apr 21 12:22:34 2010 +0200
@@ -1,5 +1,7 @@
 /* See LICENSE file for copyright and license details. */
 #include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
 #include "swk.h"
 
 static int running = 0;
@@ -94,23 +96,25 @@
         SwkBox *b;
         switch(e->type) {
         case EKey:
- if (e->data.key.keycode == 9) {
- /* not working */
- if (e->data.key.modmask&Ctrl)
+ if (e->data.key.keycode == 9) { // TAB
+ if (e->data.key.modmask)
                                 swk_focus_prev();
                         else swk_focus_next();
                         swk_update();
+ } else
+ if (e->data.key.keycode == 13) { // ENTER
+ e->box = w->box;
+ e->type = EClick;
                 }
                 // send key to focused box
                 e->box = w->box;
                 if (w->box)
                         w->box->cb(e);
+ swk_update();
                 break;
         case EMotion:
- for(b=w->boxes;b->cb;b++) {
- Point p = e->data.motion;
- if (p.x>=b->r.x && p.x<=(b->r.x+b->r.w)
- && p.y>=b->r.y && p.y<=(b->r.y+b->r.h)) {
+ for(b=w->boxes; b->cb; b++) {
+ if (SWK_HIT(b->r, e->data.click.point)) {
                                 w->box = e->box = b;
                                 b->cb(e);
                                 swk_update();
@@ -119,10 +123,8 @@
                 }
                 break;
         case EClick:
- for(b=w->boxes;b->cb;b++) {
- Point p = e->data.click.point;
- if (p.x>=b->r.x && p.x<=(b->r.x+b->r.w)
- && p.y>=b->r.y && p.y<=(b->r.y+b->r.h)) {
+ for(b=w->boxes; b->cb; b++) {
+ if (SWK_HIT(b->r, e->data.click.point)) {
                                 e->box = w->box = b;
                                 e->box->cb(e);
                                 swk_update();
@@ -145,6 +147,10 @@
         w->box++;
         if (w->box->cb == NULL)
                 w->box = w->boxes;
+ while(w->box->cb == swk_filler)
+ w->box++;
+ if(w->box->cb == NULL)
+ w->box = w->boxes;
 }
 
 void
@@ -153,7 +159,17 @@
                 while(w->box->cb)
                         w->box++;
                 w->box--;
- } else w->box--;
+ } else {
+ w->box--;
+ while (w->box->cb == swk_filler) {
+ w->box--;
+ if (w->box < w->boxes) {
+ w->box = w->boxes;
+ swk_focus_prev();
+ return;
+ }
+ }
+ }
 }
 
 /* widgets */
@@ -163,6 +179,8 @@
         switch(e->type) {
         case EExpose:
                 r = e->box->r;
+ if (w->box == e->box)
+ swk_gi_line(r.x, r.y+1, r.w, 0, ColorHI);
                 swk_gi_text(r.x, r.y, e->box->text);
                 break;
         default:
@@ -172,6 +190,30 @@
 
 void
 swk_entry(SwkEvent *e) {
+ int len, key;
+ char *ptr;
+ switch(e->type) {
+ case EKey:
+ key = e->data.key.keycode;
+ if (key==8) {
+ ptr = strdup (e->box->text);
+ if(e->box->data)
+ free(e->box->text);
+ if((len = strlen (ptr))>0)
+ ptr[len-1] = '\0';
+ e->box->text = e->box->data = ptr;
+ } else {
+ ptr = (char*)malloc(strlen(e->box->text)+2);
+ sprintf(ptr, "%s%c", e->box->text, e->data.key.keycode);
+ if(e->box->data)
+ free(e->box->text);
+ e->box->text = e->box->data = ptr;
+ }
+ break;
+ default:
+ swk_label(e);
+ break;
+ }
 }
 
 void
diff -r 25840162d26b -r 477047c05136 swk.h
--- a/swk.h Wed Apr 21 03:35:01 2010 +0200
+++ b/swk.h Wed Apr 21 12:22:34 2010 +0200
@@ -1,6 +1,7 @@
 /* See LICENSE file for copyright and license details. */
 
 #define SWK_NEWLINE(x) .data=(void*)(size_t)x, .r.w=-1, .r.h=-1, .cb = swk_filler
+#define SWK_HIT(r,p) (p.x>=r.x && p.x<(r.x+r.w) && p.y>=r.y && p.y<(r.y+r.h))
 
 typedef enum { EVoid, EClick, EMotion, EKey, EExpose, EQuit, ELast } SwkEventType;
 typedef enum { Shift=1, Ctrl=2, Alt=4, Meta=8 } SwkKeyMod;
diff -r 25840162d26b -r 477047c05136 test.c
--- a/test.c Wed Apr 21 03:35:01 2010 +0200
+++ b/test.c Wed Apr 21 12:22:34 2010 +0200
@@ -20,6 +20,14 @@
         { SWK_NEWLINE(1) },
         { .cb=swk_label, .text="Press a button", },
         { SWK_NEWLINE(2) },
+ { .cb=swk_label, .text="Username:", },
+ { .cb=swk_entry, .text="____", },
+ { .cb=swk_filler, },
+ { SWK_NEWLINE(1) },
+ { .cb=swk_label, .text="Password:", },
+ { .cb=swk_entry, .text="****", },
+ { .cb=swk_filler, },
+ { SWK_NEWLINE(2) },
         { .cb=mybutton, .text="yes" },
         { .cb=mybutton, .text="no" },
         { .cb=swk_filler, },
Received on Wed Apr 21 2010 - 10:29:08 UTC

This archive was generated by hypermail 2.2.0 : Wed Apr 21 2010 - 10:36:04 UTC