diff options
author | Gonzalo Odiard <godiard@gmail.com> | 2012-07-24 15:54:36 (GMT) |
---|---|---|
committer | Gonzalo Odiard <godiard@gmail.com> | 2012-07-24 15:54:36 (GMT) |
commit | 3c36f5a9db120a84d2bbde14522045910b4db5e7 (patch) | |
tree | 7c74d24af73c486b002f1477d012edcc6a7031ae | |
parent | 0adf3883200fcdfda91989547a3a514fb4702473 (diff) |
Use cairo to pick_color and draw the brush preview widget
Is pending the part of the stamp display in the brush button
Signed-off-by: Gonzalo Odiard <gonzalo@laptop.org>
-rw-r--r-- | Area.py | 22 | ||||
-rw-r--r-- | widgets.py | 53 |
2 files changed, 46 insertions, 29 deletions
@@ -681,14 +681,24 @@ class Area(gtk.DrawingArea): self.window.set_cursor(cursor) def pick_color(self, x, y): - gdk_image = self.pixmap.get_image(x, y, 1, 1) - pixel = gdk_image.get_pixel(0, 0) - cmap = gdk_image.get_colormap() - gdk_color = cmap.query_color(pixel) + # create a new 1x1 cairo surface + cairo_surface = cairo.ImageSurface(cairo.FORMAT_RGB24, 1, 1) + cairo_context = cairo.Context(cairo_surface) + # translate xlib_surface so that target pixel is at 0, 0 + cairo_context.set_source_surface(self.drawing_canvas, -x, -y) + cairo_context.rectangle(0, 0, 1, 1) + cairo_context.set_operator(cairo.OPERATOR_SOURCE) + cairo_context.fill() + cairo_surface.flush() + # Read the pixel + pixels = cairo_surface.get_data() + red = ord(pixels[2]) * 256 + green = ord(pixels[1]) * 256 + blue = ord(pixels[0]) * 256 + + stroke_color = gtk.gdk.Color(red, green, blue) # set in the area - pixmap_cmap = self.pixmap.get_colormap() - stroke_color = pixmap_cmap.alloc_color(gdk_color) self.tool['stroke color'] = stroke_color self.set_stroke_color(self.tool['stroke color']) @@ -4,6 +4,8 @@ from gettext import gettext as _ import gtk import gobject +import cairo +import math from sugar.graphics import style from sugar.graphics.palette import ToolInvoker @@ -34,6 +36,7 @@ class BrushButton(_ColorButton): self._preview = gtk.DrawingArea() self._preview.set_size_request(style.STANDARD_ICON_SIZE, style.STANDARD_ICON_SIZE) + self._ctx = None gobject.GObject.__init__(self, **kwargs) self._preview.set_events(gtk.gdk.BUTTON_PRESS_MASK) @@ -42,8 +45,6 @@ class BrushButton(_ColorButton): self._preview.connect("expose_event", self.expose) self.set_image(self._preview) - self._gc = None - if self._has_palette and self._has_invoker: self._invoker = WidgetInvoker(self) # FIXME: This is a hack. @@ -54,10 +55,9 @@ class BrushButton(_ColorButton): if self.get_window() is not None: self._preview.fill_color = '' self._preview.show() - self.pixmap = gtk.gdk.Pixmap(self.get_window(), - style.STANDARD_ICON_SIZE, - style.STANDARD_ICON_SIZE, -1) - self._gc = self.get_window().new_gc() + self._surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, + style.STANDARD_ICON_SIZE, style.STANDARD_ICON_SIZE) + self._ctx = cairo.Context(self._surface) self.show_all() def get_brush_size(self): @@ -81,7 +81,9 @@ class BrushButton(_ColorButton): setter=set_brush_shape) def set_color(self, color): - # receive gtk.gdk.Color + """ + @ param color gtk.gdk.Color + """ self._color = color self._preview.queue_draw() @@ -106,14 +108,15 @@ class BrushButton(_ColorButton): return self._resized_stamp != None def expose(self, widget, event): - if self._gc is None: + if self._ctx is None: self._setup() if self.get_window() is not None: center = style.STANDARD_ICON_SIZE / 2 - self.pixmap.draw_rectangle(self._preview.get_style().white_gc, - True, 0, 0, style.STANDARD_ICON_SIZE, style.STANDARD_ICON_SIZE) - self._gc.set_foreground(self._color) + self._ctx.rectangle(0, 0, style.STANDARD_ICON_SIZE, + style.STANDARD_ICON_SIZE) + self._ctx.set_source_rgb(1.0, 1.0, 1.0) + self._ctx.fill() if self.is_stamping(): width = self._resized_stamp.get_width() @@ -124,21 +127,25 @@ class BrushButton(_ColorButton): 0, 0, dx, dy, width, height) else: + red = float(self._color.red) / 65535.0 + green = float(self._color.green) / 65535.0 + blue = float(self._color.blue) / 65535.0 + self._ctx.set_source_rgb(red, green, blue) if self._brush_shape == 'circle': - self.pixmap.draw_arc(self._gc, True, - center - self._brush_size / 2, - center - self._brush_size / 2, - self._brush_size, self._brush_size, 0, 360 * 64) + self._ctx.arc(center, center, self._brush_size / 2, 0., + 2 * math.pi) + self._ctx.fill() elif self._brush_shape == 'square': - self.pixmap.draw_rectangle(self._gc, True, - center - self._brush_size / 2, - center - self._brush_size / 2, - self._brush_size, self._brush_size) - - area = event.area - widget.window.draw_drawable(self._gc, self.pixmap, - area[0], area[1], area[0], area[1], area[2], area[3]) + self._ctx.rectangle(center - self._brush_size / 2, + center - self._brush_size / 2, self._brush_size, + self._brush_size) + self._ctx.fill() + + allocation = widget.get_allocation() + context = widget.window.cairo_create() + context.set_source_surface(self._surface) + context.paint() return False def do_style_set(self, previous_style): |