Skip to content

Commit

Permalink
branch event, issue #383 - non working code for inspection
Browse files Browse the repository at this point in the history
  • Loading branch information
Cecil committed Nov 10, 2017
1 parent e26774a commit 8af62a8
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 28 deletions.
6 changes: 5 additions & 1 deletion Tests/events/event1.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
Shoes.app do
app.events = proc { puts "hello event handler"}
event do |evt|
stderr.puts "event handler called"
true
end
app.events = true
end
45 changes: 23 additions & 22 deletions shoes/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ static void shoes_app_mark(shoes_app *app) {
rb_gc_mark_maybe(app->styles);
rb_gc_mark_maybe(app->groups);
rb_gc_mark_maybe(app->owner);
rb_gc_mark_maybe(app->event_handler);
}

static void shoes_app_free(shoes_app *app) {
Expand Down Expand Up @@ -62,7 +61,7 @@ VALUE shoes_app_alloc(VALUE klass) {
app->decorated = TRUE;
app->opacity = 1.0;
app->cursor = s_arrow;
app->event_handler = Qnil;
app->use_event_handler = 0;
app->scratch = cairo_create(cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 1, 1));
app->self = Data_Wrap_Struct(klass, shoes_app_mark, shoes_app_free, app);
rb_extend_object(app->self, cTypes);
Expand Down Expand Up @@ -495,6 +494,21 @@ shoes_code shoes_app_paint(shoes_app *app) {
return SHOES_OK;
}

/* ------ GUI events ------ */
VALUE shoes_app_set_event_handler(VALUE self, VALUE opt) {
shoes_app *app;
Data_Get_Struct(self, shoes_app, app);
if (TYPE(opt) == T_TRUE) {
fprintf(stderr, "set app event handler\n");
app->use_event_handler = 1;
return Qtrue;
} else {
app->use_event_handler = 0;
}
return Qnil;
}


shoes_code shoes_app_motion(shoes_app *app, int x, int y) {
app->mousex = x;
app->mousey = y;
Expand All @@ -504,10 +518,15 @@ shoes_code shoes_app_motion(shoes_app *app, int x, int y) {

shoes_code shoes_app_click(shoes_app *app, int button, int x, int y) {
app->mouseb = button;
if (! NIL_P(app->event_handler)) {
VALUE sendevt = Qtrue;
if (app->use_event_handler) {
fprintf(stderr, "have event_handler, invoking...\n");
VALUE ary = rb_ary_new_from_args(4, ID2SYM(s_click), INT2NUM(button),
INT2NUM(x), INT2NUM(y));
sendevt = rb_funcall(app->canvas, rb_intern("event"), 1, ary);
}
shoes_canvas_send_click(app->canvas, button, x, y);
if (! NIL_P(sendevt))
shoes_canvas_send_click(app->canvas, button, x, y);
return SHOES_OK;
}

Expand Down Expand Up @@ -732,21 +751,3 @@ VALUE shoes_app_terminal(int argc, VALUE *argv, VALUE self) {
return shoes_global_terminal ? Qtrue : Qfalse;
}

VALUE shoes_app_set_event_handler(VALUE self, VALUE blk) {
shoes_app *app;
Data_Get_Struct(self, shoes_app, app);
if (rb_obj_is_kind_of(blk, rb_cProc)) {
fprintf(stderr, "setting app event handler\n");
app->event_handler = blk;
return Qtrue;
} else {
rb_raise(rb_eArgError, "events must be be a proc");
}
return Qnil;
}
VALUE shoes_app_playback(VALUE self, VALUE evt) {
// much TODO:
shoes_app *app;
Data_Get_Struct(self, shoes_app, app);
return Qtrue;
}
6 changes: 3 additions & 3 deletions shoes/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ typedef struct _shoes_app {
VALUE title;
VALUE location;
VALUE owner;
VALUE event_handler; // must be a proc
int use_event_handler;
} shoes_app;

//
Expand Down Expand Up @@ -102,8 +102,8 @@ VALUE shoes_app_set_resizable(VALUE, VALUE);
VALUE shoes_app_set_cache(VALUE app, VALUE setting);
VALUE shoes_app_get_cache(VALUE app);
VALUE shoes_app_clear_cache(VALUE app, VALUE opts);
VALUE shoes_app_set_event_handler(VALUE app, VALUE blk);
VALUE shoes_app_playback(VALUE app, VALUE evt);
VALUE shoes_app_get_handler(VALUE app);
VALUE shoes_app_set_event_handler(VALUE app, VALUE opt);
// global var for image cache - declared in types/image.c
extern int shoes_cache_setting;
// global var for console up and running
Expand Down
9 changes: 9 additions & 0 deletions shoes/canvas.c
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,15 @@ EVENT_HANDLER(keypress);
EVENT_HANDLER(keyup);
//EVENT_HANDLER(start);
EVENT_HANDLER(finish);
//EVENT_HANDLER(event);
// dont't use macro for event
VALUE shoes_canvas_event(int argc, VALUE *argv, VALUE self) {
VALUE val, block;
SETUP_CANVAS();
rb_scan_args(argc, argv, "01&", &val, &block);
ATTRSET(canvas->attr, event, NIL_P(block) ? val : block);
return self;
}

VALUE shoes_canvas_start(int argc, VALUE *argv, VALUE self) {
VALUE val, block;
Expand Down
2 changes: 2 additions & 0 deletions shoes/canvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,8 @@ VALUE shoes_canvas_motion(int, VALUE *, VALUE);
VALUE shoes_canvas_keydown(int, VALUE *, VALUE);
VALUE shoes_canvas_keypress(int, VALUE *, VALUE);
VALUE shoes_canvas_keyup(int, VALUE *, VALUE);
VALUE shoes_canvas_event(int, VALUE *, VALUE);

int shoes_canvas_independent(shoes_canvas *);
VALUE shoes_find_canvas(VALUE);
VALUE shoes_canvas_get_app(VALUE);
Expand Down
2 changes: 1 addition & 1 deletion shoes/ruby.c
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,7 @@ void shoes_ruby_init() {
rb_define_method(cApp, "cache=", CASTHOOK(shoes_app_set_cache), 1);
rb_define_method(cApp, "cache_clear", CASTHOOK(shoes_app_clear_cache), 1);
rb_define_method(cApp, "events=", CASTHOOK(shoes_app_set_event_handler), 1);
rb_define_method(cApp, "playback", CASTHOOK(shoes_app_playback), 1);


cDialog = rb_define_class_under(cTypes, "Dialog", cApp);

Expand Down
3 changes: 2 additions & 1 deletion shoes/ruby.h
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ void shoes_cairo_arc(cairo_t *, double, double, double, double, double, double);
f(shadow); f(arc); f(rect); f(oval); f(line); f(star); f(project); f(round); \
f(square); f(undercolor); f(underline); f(variant); f(weight); f(wrap); \
f(dash); f(nodot); f(onedot); f(donekey); f(volume); f(bg_color); \
f(decorated); f(opacity); f(cache)
f(decorated); f(opacity); f(cache); f(event)
#define SYMBOL_INTERN(name) s_##name = rb_intern("" # name)
#define SYMBOL_ID(name) ID s_##name
#define SYMBOL_EXTERN(name) extern ID s_##name
Expand Down Expand Up @@ -589,6 +589,7 @@ SYMBOL_EXTERN(link);
f(".keydown", keydown, -1); \
f(".keypress", keypress, -1); \
f(".keyup", keyup, -1); \
f(".event", event, -1); \
f("+clear", clear_contents, -1); \
f(".visit", goto, 1); \
f(".mouse", mouse, 0); \
Expand Down

0 comments on commit 8af62a8

Please sign in to comment.