commit b5353a87ec56edadf1eb4a9f88812d7ce902d1d1
parent 2a8c188e51547e3dea8dc80465bec44b8a26a153
Author: pancake <pancake@nopcode.org>
Date: Wed, 21 Apr 2010 12:22:34 +0200
swk_entry initial implementation
focus_{prev|next} iterates only over non swk_fillers
highlight label widgets
extended test example
Diffstat:
gi_sdl.c | | | 1 | - |
swk.c | | | 66 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++------------ |
swk.h | | | 1 | + |
test.c | | | 8 | ++++++++ |
4 files changed, 63 insertions(+), 13 deletions(-)
diff --git a/gi_sdl.c b/gi_sdl.c
@@ -13,7 +13,6 @@
#define BPP 32
/* --- */
-
static Uint32 pal[ColorLast];
static SDL_Color fontcolor = { TFCOLOR };
static SDL_Surface *screen = NULL;
diff --git a/swk.c b/swk.c
@@ -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 @@ swk_event_handle(SwkEvent *e) {
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 @@ swk_event_handle(SwkEvent *e) {
}
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 @@ swk_focus_next() {
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 @@ swk_focus_prev() {
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 @@ swk_label(SwkEvent *e) {
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 @@ swk_label(SwkEvent *e) {
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 --git a/swk.h b/swk.h
@@ -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 --git a/test.c b/test.c
@@ -20,6 +20,14 @@ static SwkBox helloworld[] = {
{ 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, },