class ClickableImageButton(CustomButton): """Image that can send clicked events. If max_width and/or max_height are specified, resizes the image proportionally such that all constraints are met. """ def __init__(self, image_path, max_width=None, max_height=None): CustomButton.__init__(self) self.max_width = max_width self.max_height = max_height self.image = None self._width, self._height = None, None if image_path: self.set_path(image_path) self.set_cursor(widgetconst.CURSOR_POINTING_HAND) def set_path(self, path): image = Image(path) if self.max_width: image = image.resize_for_space(self.max_width, self.max_height) self.image = ImageSurface(image) self._width, self._height = image.width, image.height def size_request(self, layout): w = self._width h = self._height if not w: w = self.max_width if not h: h = self.max_height return w, h def draw(self, context, layout): if self.image: self.image.draw(context, 0, 0, self._width, self._height) w = self._width h = self._height if not w: w = self.max_width if not h: h = self.max_height w = min(context.width, w) h = min(context.height, h) context.rectangle(0, 0, w, h) context.set_color((0, 0, 0)) # black context.set_line_width(1) context.stroke()
def set_path(self, path): image = Image(path) if self.max_width: image = image.resize_for_space(self.max_width, self.max_height) self.image = ImageSurface(image) self._width, self._height = image.width, image.height