Skip to content

Commit

Permalink
on #383 - keypress/keyup/keydown events
Browse files Browse the repository at this point in the history
* not working yet since there can be slot handlers and they need
  to be sent to non-native widget (svg,plot,image...)
  Much like click does
* Just checking in the code.
  • Loading branch information
Cecil committed Dec 10, 2017
1 parent 4ac54ab commit bdc03c0
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 22 deletions.
9 changes: 5 additions & 4 deletions Tests/events/event4.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ def click(btn,x,y,mods)
@tw = test_widget {|btn,x,y,mods| @eb.append "User Widget: #{btn} at #{x},#{y} with #{mods}\n"}
end
@eb = edit_box width: 500, height: 200
stroke blue
strokewidth 4
fill black
oval 540, 590, 50
# draw a shape
stroke blue
strokewidth 4
fill black
oval 540, 590, 50
end

event do |evt|
Expand Down
41 changes: 41 additions & 0 deletions Tests/events/event6.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
Shoes.app do

event do |evt|
# do not trigger new events here unless you can handle them recursively
# which is harder than you think.
case evt.type
when :click
$stderr.puts "click handler called: #{evt.type} #{evt.button}, #{evt.x} #{evt.y} #{evt.modifiers}"
evt.accept = true
when :keypress
$stderr.puts "keypress: #{evt.key}"
evt.accept = true
when :keydown
$stderr.puts "keydown for #{evt.key}"
evt.accept = $ck.checked?
when :keyup
$stderr.puts "keyup for #{evt.key}"
evt.accept = $ck.checked?
else
puts "Other: #{evt.type.inspect}"
evt.accept = true
end
end

stack do
para "Key Tests"
flow do
$ck = check checked: true; para "Enable up/down"
end
@eb = edit_box width: 500, height: 350
end
keypress do |key|
@eb.append "press: #{key}\n"
end
keyup do |key|
@eb.append "up: #{key}\n"
end
keydown do |key|
@eb.append "down: #{key}\n"
end
end
80 changes: 67 additions & 13 deletions shoes/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -590,9 +590,7 @@ shoes_code shoes_app_click(shoes_app *app, int button, int x, int y, int mods) {
rb_ary_push(modifiers, rb_str_new_cstr("meta"));
*/
if (app->use_event_handler) {
//fprintf(stderr, "have event_handler, creating event...\n");
VALUE evt = shoes_event_create_event(app, s_click, button, x, y, modifiers);
//VALUE evt = shoes_event_new(cShoesEvent, s_click, Qnil, x, y, button, modifiers);
VALUE evt = shoes_event_create_event(app, s_click, button, x, y, modifiers, Qnil);
shoes_canvas *canvas;
Data_Get_Struct(app->canvas, shoes_canvas, canvas);
VALUE event = ATTR(canvas->attr, event);
Expand Down Expand Up @@ -643,15 +641,8 @@ shoes_code shoes_app_wheel(shoes_app *app, ID dir, int x, int y, int mods) {
return SHOES_OK;
}

shoes_code shoes_app_keydown(shoes_app *app, VALUE key) {
if (!RTEST(rb_hash_aref(app->keypresses, key))) {
rb_hash_aset(app->keypresses, key, Qtrue);
shoes_canvas_send_keydown(app->canvas, key);
}
return SHOES_OK;
}

shoes_code shoes_app_keypress(shoes_app *app, VALUE key) {
VALUE sendevt = Qtrue;
if (key == symAltSlash)
rb_eval_string("Shoes.show_log");
else if (key == symAltQuest)
Expand All @@ -662,14 +653,77 @@ shoes_code shoes_app_keypress(shoes_app *app, VALUE key) {
rb_eval_string("Shoes.show_irb");
else if (key == symAltSemiColon)
rb_eval_string("Shoes.remote_debug");
else
shoes_canvas_send_keypress(app->canvas, key);
else {
if (app->use_event_handler) {
VALUE evt = shoes_event_create_event(app, s_click, button, x, y, modifiers, key);
VALUE evt = shoes_event_new_key(cShoesEvent, s_keypress, key);
shoes_canvas *canvas;
Data_Get_Struct(app->canvas, shoes_canvas, canvas);
VALUE event = ATTR(canvas->attr, event);
if (! NIL_P(event)) {
shoes_safe_block(app->canvas, event, rb_ary_new3(1, evt));
shoes_event *tevent;
Data_Get_Struct(evt, shoes_event, tevent);
sendevt = (tevent->accept == 1) ? Qtrue : Qfalse;
} else {
fprintf(stderr, "keypress: doen't have event - but should - dump hash\n");
shoes_hash_debug(canvas->attr);
}
}
if (sendevt == Qtrue)
shoes_canvas_send_keypress(app->canvas, key);
}
return SHOES_OK;
}

shoes_code shoes_app_keydown(shoes_app *app, VALUE key) {
VALUE sendevt = Qtrue;
if (!RTEST(rb_hash_aref(app->keypresses, key))) {
rb_hash_aset(app->keypresses, key, Qtrue);
if (app->use_event_handler) {
VALUE evt = shoes_event_new_key(cShoesEvent, s_keydown, key);
shoes_canvas *canvas;
Data_Get_Struct(app->canvas, shoes_canvas, canvas);
VALUE event = ATTR(canvas->attr, event);
if (! NIL_P(event)) {
shoes_safe_block(app->canvas, event, rb_ary_new3(1, evt));
shoes_event *tevent;
Data_Get_Struct(evt, shoes_event, tevent);
sendevt = (tevent->accept == 1) ? Qtrue : Qfalse;
} else {
fprintf(stderr, "keydown: doen't have event - but should - dump hash\n");
shoes_hash_debug(canvas->attr);
}
}
if (sendevt == Qtrue) {
shoes_canvas_send_keydown(app->canvas, key);
}
}
return SHOES_OK;
}

shoes_code shoes_app_keyup(shoes_app *app, VALUE key) {
VALUE sendevt = Qtrue;
rb_hash_aset(app->keypresses, key, Qfalse);
shoes_canvas_send_keyup(app->canvas, key);
if (app->use_event_handler) {
VALUE evt = shoes_event_new_key(cShoesEvent, s_keyup, key);
shoes_canvas *canvas;
Data_Get_Struct(app->canvas, shoes_canvas, canvas);
VALUE event = ATTR(canvas->attr, event);
if (! NIL_P(event)) {
shoes_safe_block(app->canvas, event, rb_ary_new3(1, evt));
shoes_event *tevent;
Data_Get_Struct(evt, shoes_event, tevent);
sendevt = (tevent->accept == 1) ? Qtrue : Qfalse;
} else {
fprintf(stderr, "keyup: doen't have event - but should - dump hash\n");
shoes_hash_debug(canvas->attr);
}
}
if (sendevt == Qtrue) {
shoes_canvas_send_keyup(app->canvas, key);
}
return SHOES_OK;
}

Expand Down
3 changes: 2 additions & 1 deletion shoes/native/gtk.c
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,8 @@ static gboolean shoes_app_gtk_keypress(GtkWidget *widget, GdkEventKey *event, gp
chbuf[len] = '\0';

v = ID2SYM(rb_intern(chbuf));
if (modifiers & GDK_SHIFT_MASK) modifiers ^= GDK_SHIFT_MASK;
if (modifiers & GDK_SHIFT_MASK)
modifiers ^= GDK_SHIFT_MASK;
} else {
if (event->string[0] == '\r' && event->length == 1)
v = rb_str_new2("\n");
Expand Down
25 changes: 22 additions & 3 deletions shoes/types/event.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,23 @@ VALUE shoes_event_new_widget(VALUE klass, ID type, VALUE widget, int btn, int x,
return obj;
}

VALUE shoes_event_new_key(VALUE klass, ID type, VALUE key) {
shoes_event *event;
VALUE obj = shoes_event_alloc(klass);
Data_Get_Struct(obj, shoes_event, event);
event->accept = 1;
event->type = type;
event->object = Qnil;
event->btn = 0;
event->x = 0;
event->y = 0;
event->width = 0;
event->height = 0;
event->key = key;
event->modifiers = Qnil;
return obj;
}

VALUE shoes_event_alloc(VALUE klass) {
VALUE obj;
shoes_event *event = SHOE_ALLOC(shoes_event);
Expand Down Expand Up @@ -118,15 +135,15 @@ VALUE shoes_event_create_event(shoes_app *app, ID etype, int button, int x, int
VALUE ps_widget = shoes_event_find_psuedo (app->canvas, x, y, &nobj);
VALUE evt;
if (NIL_P(ps_widget)) {
evt = shoes_event_new(cShoesEvent, s_click, Qnil, x, y, button, modifiers);
evt = shoes_event_new(cShoesEvent, etype, Qnil, x, y, button, modifiers);
} else {
// TODO: ASSUME all the ps_widget have the canvas place
int w,h;
shoes_canvas *cvs;
Data_Get_Struct(nobj, shoes_canvas, cvs);
w = cvs->place.w;
h = cvs->place.h;
evt = shoes_event_new_widget(cShoesEvent, s_click, nobj, button, x, y, w, h, modifiers);
evt = shoes_event_new_widget(cShoesEvent, etype, nobj, button, x, y, w, h, modifiers, );
}
return evt;
}
Expand Down Expand Up @@ -196,6 +213,8 @@ VALUE shoes_event_find_psuedo (VALUE self, int x, int y, VALUE *hitobj) {
return Qnil;
}

// helper for creating key events



VALUE shoes_event_type(VALUE self) {
Expand Down Expand Up @@ -252,7 +271,7 @@ VALUE shoes_event_width(VALUE self) {
VALUE shoes_event_key(VALUE self) {
shoes_event *event;
Data_Get_Struct(self, shoes_event, event);
return INT2NUM(event->key);
return event->key;
}
VALUE shoes_event_set_key(VALUE self, VALUE key) {
shoes_event *event;
Expand Down
3 changes: 2 additions & 1 deletion shoes/types/event.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ VALUE shoes_event_alloc(VALUE klass);
VALUE shoes_event_create_event(shoes_app *app, ID action, int button, int x, int y, VALUE modifiers);
VALUE shoes_canvas_shoesevent(int argc, VALUE *argv, VALUE self);
VALUE shoes_event_new_widget(VALUE klass, ID type, VALUE widget, int btn, int x, \
int y, int w, int h, VALUE modifiers);
int y, int w, int h, VALUE modifiers, VALUE key);
VALUE shoes_event_find_psuedo (VALUE self, int x, int y, VALUE *pswidget);
VALUE shoes_event_new_key(VALUE klass, ID type, VALUE key);
#endif

0 comments on commit bdc03c0

Please sign in to comment.