commit ba11625044577e8dd8126a48b7d0c01eb3862b59
parent aa3d8ecb8e9c3aa770a8474bcfaef03f43a336f1
Author: pancake <pancake@nopcode.org>
Date: Thu, 22 Apr 2010 15:01:14 +0200
handle up/down arrow keys
implement and use swk_password widget
Diffstat:
gi_sdl.c | | | 32 | ++++++++++++++++++++++---------- |
swk.c | | | 45 | ++++++++++++++++++++++++++++++++++++++++----- |
swk.h | | | 2 | ++ |
test.c | | | 2 | +- |
4 files changed, 65 insertions(+), 16 deletions(-)
diff --git a/gi_sdl.c b/gi_sdl.c
@@ -126,20 +126,32 @@ swk_gi_event(SwkWindow *w, int dowait) {
fprintf(stderr, "event: click %d\n", event.button.button);
break;
case SDL_KEYDOWN:
+ ret->data.key.modmask = 0;
+ ret->type = EKey;
+ if(event.key.keysym.mod & KMOD_CTRL)
+ ret->data.key.modmask |= Ctrl;
+ if(event.key.keysym.mod & KMOD_SHIFT)
+ ret->data.key.modmask |= Shift;
+ if(event.key.keysym.mod & KMOD_ALT)
+ ret->data.key.modmask |= Alt;
+ if(event.key.keysym.mod & KMOD_META)
+ ret->data.key.modmask |= Meta;
if(ret->data.key.keycode != 0 && event.key.keysym.unicode != 0) {
- ret->type = EKey;
ret->data.key.keycode = event.key.keysym.unicode;
- ret->data.key.modmask = 0;
- if(event.key.keysym.mod & KMOD_CTRL)
- ret->data.key.modmask |= Ctrl;
- if(event.key.keysym.mod & KMOD_SHIFT)
- ret->data.key.modmask |= Shift;
- if(event.key.keysym.mod & KMOD_ALT)
- ret->data.key.modmask |= Alt;
- if(event.key.keysym.mod & KMOD_META)
- ret->data.key.modmask |= Meta;
fprintf(stderr, "event: key %d %d\n",
ret->data.key.modmask, ret->data.key.keycode);
+ } else {
+ switch(event.key.keysym.sym) {
+ case 273:
+ ret->data.key.keycode = KUp;
+ break;
+ case 274:
+ ret->data.key.keycode = KDown;
+ break;
+ default:
+ ret->type = -1;
+ break;
+ }
}
break;
case SDL_QUIT:
diff --git a/swk.c b/swk.c
@@ -120,20 +120,28 @@ swk_handle_event(SwkEvent *e) {
break;
}
} else
- if(e->data.key.keycode == 9) { // TAB
+ switch(e->data.key.keycode) {
+ case KUp:
+ swk_focus_prev(e->win);
+ break;
+ case KDown:
+ swk_focus_next(e->win);
+ break;
+ case 9: // TAB
if(e->data.key.modmask)
swk_focus_prev(e->win);
else swk_focus_next(e->win);
swk_update(e->win);
- } else
- if(e->data.key.keycode == 13) { // ENTER
+ break;
+ case 13: // ENTER
e->box = e->win->box;
e->type = EClick;
- } else
- if(e->data.key.keycode == 27) { // ESC
+ break;
+ case 27: // ESC
e->box = e->win->box;
e->type = EQuit;
swk_exit();
+ break;
}
// send key to focused box
e->box = e->win->box;
@@ -227,6 +235,33 @@ swk_label(SwkEvent *e) {
}
void
+swk_password(SwkEvent *e) {
+ int len;
+ Rect r;
+ char *str, *ptr;
+ switch(e->type) {
+ case EExpose:
+ r = e->box->r;
+ if(e->win->box == e->box)
+ swk_gi_line(r.x, r.y+1, r.w, 0, ColorHI);
+ len = strlen(e->box->text);
+ if (len>0) {
+ ptr = str = malloc(len+1);
+ for(;len--;ptr++)
+ *ptr='*';
+ *ptr='\0';
+ swk_gi_text(r, str);
+ free(str);
+ }
+ break;
+ case EClick:
+ printf("password: %s\n", e->box->text);
+ default:
+ swk_entry(e);
+ }
+}
+
+void
swk_entry(SwkEvent *e) {
int len, key;
char *ptr;
diff --git a/swk.h b/swk.h
@@ -6,6 +6,7 @@
typedef enum { EVoid, EClick, EMotion, EKey, EExpose, EQuit, ELast } SwkEventType;
typedef enum { Shift=1, Ctrl=2, Alt=4, Meta=8 } SwkKeyMod;
typedef enum { ColorFG, ColorBG, ColorHI, ColorLast } Palete;
+typedef enum { KUp=0xe0, KDown=0xe1, KLeft=0xe2, KRight=0xe3 } SwkKeyCode;
typedef struct SwkBox SwkBox;
typedef struct SwkWindow SwkWindow;
@@ -80,6 +81,7 @@ void swk_focus_prev(SwkWindow *w);
void swk_button(SwkEvent *e);
void swk_label(SwkEvent *e);
void swk_entry(SwkEvent *e);
+void swk_password(SwkEvent *e);
void swk_filler(SwkEvent *e);
/* graphic backend */
diff --git a/test.c b/test.c
@@ -23,7 +23,7 @@ static SwkBox helloworld[] = {
{ .cb=swk_filler, },
SWK_BOX_NEWLINE(1),
{ .cb=swk_label, .text="Password:", },
- { .cb=swk_entry, .text="****", },
+ { .cb=swk_password, .text="1234", },
{ .cb=swk_filler, },
SWK_BOX_NEWLINE(2),
{ .cb=mybutton, .text="yes" },