void shoes_canvas_clear(VALUE self) { shoes_canvas *canvas; Data_Get_Struct(self, shoes_canvas, canvas); canvas->cr = NULL; canvas->attr = rb_hash_new(); ATTRSET(canvas->attr, cap, Qnil); ATTRSET(canvas->attr, strokewidth, rb_float_new(1.)); ATTRSET(canvas->attr, stroke, shoes_color_new(0, 0, 0, 0xFF)); ATTRSET(canvas->attr, fill, shoes_color_new(0, 0, 0, 0xFF)); canvas->parent = Qnil; canvas->stl = 0; canvas->stt = 0; shoes_canvas_reset_transform(canvas); shoes_canvas_empty(canvas, TRUE); canvas->contents = rb_ary_new(); canvas->place.x = canvas->place.y = 0; canvas->place.dx = canvas->place.dy = 0; canvas->place.ix = canvas->place.iy = 0; canvas->hover = 0; canvas->cx = 0; canvas->cy = 0; canvas->endy = 0; canvas->endx = 0; canvas->topy = 0; canvas->fully = 0; shoes_group_clear(&canvas->group); }
VALUE shoes_canvas_shape(int argc, VALUE *argv, VALUE self) { int x; double x1, y1, x2, y2; cairo_t *shape = NULL; cairo_path_t *line = NULL; SETUP_SHAPE(); shape = canvas->shape; VALUE attr = shoes_shape_attr(argc, argv, 2, s_left, s_top); canvas->shape = cairo_create(cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 1, 1)); cairo_move_to(canvas->shape, 0, 0); if (rb_block_given_p()) rb_funcall(rb_block_proc(), s_call, 0); #if CAIRO_VERSION_MAJOR == 1 && CAIRO_VERSION_MINOR <= 4 cairo_fill_extents(canvas->shape, &x1, &y1, &x2, &y2); #else cairo_path_extents(canvas->shape, &x1, &y1, &x2, &y2); #endif x = x2 - x1; ATTRSET(attr, width, INT2NUM(x)); x = y2 - y1; ATTRSET(attr, height, INT2NUM(x)); line = cairo_copy_path(canvas->shape); canvas->shape = shape; return shoes_add_shape(self, s_shape, attr, line); }
VALUE shoes_canvas_displace(VALUE self, VALUE dx, VALUE dy) { SETUP_CANVAS(); ATTRSET(canvas->attr, displace_left, dx); ATTRSET(canvas->attr, displace_top, dy); shoes_canvas_repaint_all(canvas->parent); return self; }
VALUE shoes_canvas_move(VALUE self, VALUE x, VALUE y) { SETUP_CANVAS(); ATTRSET(canvas->attr, left, x); ATTRSET(canvas->attr, top, y); shoes_canvas_repaint_all(canvas->parent); return self; }
VALUE shoes_canvas_nofill(VALUE self) { SETUP_BASIC(); ATTRSET(basic->attr, fill, Qnil); return self; }
VALUE shoes_canvas_cap(VALUE self, VALUE cap) { SETUP_BASIC(); ATTRSET(basic->attr, cap, cap); return self; }
VALUE shoes_canvas_strokewidth(VALUE self, VALUE w) { SETUP_BASIC(); ATTRSET(basic->attr, strokewidth, w); return self; }
VALUE shoes_canvas_nostroke(VALUE self) { SETUP_BASIC(); ATTRSET(basic->attr, stroke, Qnil); return self; }
VALUE shoes_canvas_image(int argc, VALUE *argv, VALUE self) { VALUE path, attr, _w, _h, image, block; if (argc == 0 || (argc == 1 && rb_obj_is_kind_of(argv[0], rb_cHash))) { rb_scan_args(argc, argv, "01&", &attr, &block); if (NIL_P(attr)) attr = rb_hash_new(); _w = ATTR(attr, width); _h = ATTR(attr, height); } else rb_scan_args(argc, argv, "12&", &_w, &_h, &attr, &block); if (NIL_P(_w) || FIXNUM_P(_w)) { path = Qnil; if (FIXNUM_P(_w)) ATTRSET(attr, width, _w); if (FIXNUM_P(_h)) ATTRSET(attr, height, _h); ATTRSET(attr, draw, block); } else { rb_scan_args(argc, argv, "11&", &path, &attr, &block); if (!NIL_P(block)) { if (NIL_P(attr)) attr = rb_hash_new(); rb_hash_aset(attr, ID2SYM(s_click), block); } } if (rb_obj_is_kind_of(self, cImage)) { shoes_image_image(self, path, attr); return self; } SETUP(); image = shoes_image_new(cImage, path, attr, self, canvas->st); shoes_add_ele(canvas, image); return image; }
VALUE shoes_canvas_image(int argc, VALUE *argv, VALUE self) { rb_arg_list args; VALUE path = Qnil, attr = Qnil, _w, _h, image; switch (rb_parse_args(argc, argv, "ii|h,s|h,|h", &args)) { case 1: _w = args.a[0]; _h = args.a[1]; attr = args.a[2]; ATTRSET(attr, width, _w); ATTRSET(attr, height, _h); if (rb_block_given_p()) ATTRSET(attr, draw, rb_block_proc()); break; case 2: path = args.a[0]; attr = args.a[1]; if (rb_block_given_p()) ATTRSET(attr, click, rb_block_proc()); break; case 3: attr = args.a[0]; if (rb_block_given_p()) ATTRSET(attr, draw, rb_block_proc()); break; } if (rb_obj_is_kind_of(self, cImage)) { shoes_image_image(self, path, attr); return self; } SETUP(); image = shoes_image_new(cImage, path, attr, self, canvas->st); shoes_add_ele(canvas, image); return image; }
VALUE shoes_canvas_fill(int argc, VALUE *argv, VALUE self) { VALUE pat; SETUP_BASIC(); if (argc == 1 && rb_respond_to(argv[0], s_to_pattern)) pat = argv[0]; else pat = shoes_pattern_args(argc, argv, self); if (!rb_obj_is_kind_of(pat, cColor)) pat = rb_funcall(pat, s_to_pattern, 0); ATTRSET(basic->attr, fill, pat); return pat; }
VALUE shoes_canvas_shape(int argc, VALUE *argv, VALUE self) { int x; double x1, y1, x2, y2; cairo_t *shape = NULL; cairo_path_t *line = NULL; SETUP_SHAPE(); shape = canvas->shape; attr = shoes_shape_attr(argc, argv, 2, s_left, s_top); canvas->shape = cairo_create(cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 1, 1)); cairo_move_to(canvas->shape, 0, 0); if (rb_block_given_p()) rb_yield(Qnil); cairo_path_extents(canvas->shape, &x1, &y1, &x2, &y2); x = x2 - x1; ATTRSET(attr, width, INT2NUM(x)); x = y2 - y1; ATTRSET(attr, height, INT2NUM(x)); line = cairo_copy_path(canvas->shape); canvas->shape = shape; return shoes_add_shape(self, s_shape, attr, line); }
// canvas - Shoes usage: menu "Help" VALUE shoes_canvas_menu(int argc, VALUE *argv, VALUE self) { rb_arg_list args; VALUE text = Qnil, attr = Qnil, menu; switch (rb_parse_args(argc, argv, "s|h,|h", &args)) { case 1: text = args.a[0]; attr = args.a[1]; break; case 2: attr = args.a[0]; break; } if (!NIL_P(text)) ATTRSET(attr, text, text); menu = shoes_menu_new(text); return menu; }
void shoes_place_decide(shoes_place *place, VALUE c, VALUE attr, int dw, int dh, unsigned char rel, int padded) { shoes_canvas *canvas = NULL; if (!NIL_P(c)) Data_Get_Struct(c, shoes_canvas, canvas); VALUE ck = rb_obj_class(c); VALUE stuck = ATTR(attr, attach); // for image : we want to scale the image, given only one attribute :width or :height // get dw and dh, set width or height if (REL_FLAGS(rel) & REL_SCALE) { // 8 VALUE rw = ATTR(attr, width), rh = ATTR(attr, height); if (NIL_P(rw) && !NIL_P(rh)) { // we have height // fetch height in pixels whatever the input (string, float, positive/negative int) int spx = shoes_px(rh, dh, CPH(canvas), 1); // compute width with image aspect ratio [(dh == dw) means a square ] dw = (dh == dw) ? spx : ROUND(((dh * 1.) / dw) * spx); dh = spx; // now re-init 'dh' for next calculations ATTRSET(attr, width, INT2NUM(dw)); // set calculated width } else if (NIL_P(rh) && !NIL_P(rw)) { int spx = shoes_px(rw, dw, CPW(canvas), 1); dh = (dh == dw) ? spx : ROUND(((dh * 1.) / dw) * spx); dw = spx; ATTRSET(attr, height, INT2NUM(dh)); } } ATTR_MARGINS(attr, 0, canvas); if (padded || dh == 0) dh += tmargin + bmargin; if (padded || dw == 0) dw += lmargin + rmargin; int testw = dw; if (testw == 0) testw = lmargin + 1 + rmargin; if (!NIL_P(stuck)) { if (stuck == cShoesWindow) rel = REL_FLAGS(rel) | REL_WINDOW; else if (stuck == cMouse) rel = REL_FLAGS(rel) | REL_CURSOR; else rel = REL_FLAGS(rel) | REL_STICKY; } place->flags = rel; place->dx = place->dy = 0; if (canvas == NULL) { place->ix = place->x = 0; place->iy = place->y = 0; place->iw = place->w = dw; place->ih = place->h = dh; } else { int cx, cy, ox, oy, tw = dw, th = dh; switch (REL_COORDS(rel)) { case REL_WINDOW: cx = 0; cy = 0; ox = 0; oy = canvas->slot->scrolly; break; case REL_CANVAS: cx = canvas->cx - CPX(canvas); cy = canvas->cy - CPY(canvas); ox = CPX(canvas); oy = CPY(canvas); break; case REL_CURSOR: cx = 0; cy = 0; ox = canvas->app->mousex; oy = canvas->app->mousey; break; case REL_TILE: cx = 0; cy = 0; ox = CPX(canvas); oy = CPY(canvas); testw = dw = CPW(canvas); dh = max(canvas->height, CPH(canvas)); // Fix #2 ? //dh = (max(canvas->height, canvas->fully - CPB(canvas)) - CPY(canvas)); break; default: cx = 0; cy = 0; ox = canvas->cx; oy = canvas->cy; if ((REL_COORDS(rel) & REL_STICKY) && shoes_is_element(stuck)) { shoes_element *element; Data_Get_Struct(stuck, shoes_element, element); ox = element->place.x; oy = element->place.y; } break; } place->w = PX(attr, width, testw, CPW(canvas)); if (dw == 0 && place->w + (int)canvas->cx > canvas->place.iw) { canvas->cx = canvas->endx = CPX(canvas); canvas->cy = canvas->endy; place->w = canvas->place.iw; } place->h = PX(attr, height, dh, CPH(canvas)); if (REL_COORDS(rel) != REL_TILE) { tw = place->w; th = place->h; } place->x = PX2(attr, left, right, cx, tw, canvas->place.iw) + ox; place->y = PX2(attr, top, bottom, cy, th, ORIGIN(canvas->place) ? canvas->height : canvas->fully) + oy; if (!ORIGIN(canvas->place)) { place->dx = canvas->place.dx; place->dy = canvas->place.dy; } place->dx += PXN(attr, displace_left, 0, CPW(canvas)); place->dy += PXN(attr, displace_top, 0, CPH(canvas)); place->flags |= NIL_P(ATTR(attr, left)) && NIL_P(ATTR(attr, right)) ? 0 : FLAG_ABSX; place->flags |= NIL_P(ATTR(attr, top)) && NIL_P(ATTR(attr, bottom)) ? 0 : FLAG_ABSY; if (REL_COORDS(rel) != REL_TILE && ABSY(*place) == 0 && (ck == cStack || place->x + place->w > CPX(canvas) + canvas->place.iw)) { canvas->cx = place->x = CPX(canvas); canvas->cy = place->y = canvas->endy; } } place->ix = place->x + lmargin; place->iy = place->y + tmargin; place->iw = place->w - (lmargin + rmargin); //place->iw = (RTEST(ATTR(attr, width))) ? place->w : place->w - (lmargin + rmargin); if (place->iw < 0) place->iw = 0; place->ih = place->h - (tmargin + bmargin); //place->ih = (RTEST(ATTR(attr, height))) ? place->h : place->h - (tmargin + bmargin); if (place->ih < 0) place->ih = 0; INFO("PLACE: (%d, %d), (%d: %d, %d: %d) [%d, %d] %x\n", place->x, place->y, place->w, place->iw, place->h, place->ih, ABSX(*place), ABSY(*place), place->flags); }