From 0bddb49db8fca2d691042fe86eaa62992fe5b5c3 Mon Sep 17 00:00:00 2001 From: Gonzalo Odiard Date: Thu, 02 Aug 2012 19:46:49 +0000 Subject: Initial version of port to gtk3 Signed-off-by: Gonzalo Odiard --- (limited to 'widgets.py') diff --git a/widgets.py b/widgets.py index 8547b8b..46c8db2 100644 --- a/widgets.py +++ b/widgets.py @@ -2,15 +2,15 @@ from gettext import gettext as _ -import gtk -import gobject -import cairo +from gi.repository import Gtk +from gi.repository import Gdk +from gi.repository import GObject import math -from sugar.graphics import style -from sugar.graphics.palette import ToolInvoker -from sugar.graphics.colorbutton import _ColorButton -from sugar.graphics.radiotoolbutton import RadioToolButton +from sugar3.graphics import style +from sugar3.graphics.palette import ToolInvoker +from sugar3.graphics.colorbutton import _ColorButton +from sugar3.graphics.radiotoolbutton import RadioToolButton # this strings are here only to enable pootle to translate them # and do not broke the old versions @@ -29,7 +29,7 @@ class BrushButton(_ColorButton): def __init__(self, **kwargs): self._title = _('Choose brush properties') - self._color = gtk.gdk.Color(0, 0, 0) + self._color = Gdk.Color(0, 0, 0) self._has_palette = True self._has_invoker = True self._palette = None @@ -39,16 +39,16 @@ class BrushButton(_ColorButton): self._brush_shape = 'circle' self._alpha = 1.0 self._resized_stamp = None - self._preview = gtk.DrawingArea() + 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) + GObject.GObject.__init__(self, **kwargs) + self._preview.set_events(Gdk.EventMask.BUTTON_PRESS_MASK) self._preview.connect('button_press_event', self.__mouse_down_cb) - self._preview.connect("expose_event", self.expose) + self._preview.connect("draw", self.draw) self.set_image(self._preview) if self._has_palette and self._has_invoker: @@ -57,14 +57,10 @@ class BrushButton(_ColorButton): self._invoker.has_rectangle_gap = lambda: False self._invoker.palette = self._palette - def _setup(self): - if self.get_window() is not None: - self._preview.fill_color = '' - self._preview.show() - 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 _setup(self): +# if self.get_window() is not None: +# self._preview.show() +# self.show_all() def get_brush_size(self): return self._brush_size @@ -73,7 +69,7 @@ class BrushButton(_ColorButton): self._brush_size = brush_size self._preview.queue_draw() - brush_size = gobject.property(type=int, getter=get_brush_size, + brush_size = GObject.property(type=int, getter=get_brush_size, setter=set_brush_size) def get_brush_shape(self): @@ -83,12 +79,12 @@ class BrushButton(_ColorButton): self._brush_shape = brush_shape self._preview.queue_draw() - brush_shape = gobject.property(type=str, getter=get_brush_shape, + brush_shape = GObject.property(type=str, getter=get_brush_shape, setter=set_brush_shape) def set_color(self, color): """ - @ param color gtk.gdk.Color + @ param color Gdk.Color """ self._color = color self._preview.queue_draw() @@ -100,7 +96,7 @@ class BrushButton(_ColorButton): self._stamp_size = stamp_size self._preview.queue_draw() - stamp_size = gobject.property(type=int, getter=get_stamp_size, + stamp_size = GObject.property(type=int, getter=get_stamp_size, setter=set_stamp_size) def set_resized_stamp(self, resized_stamp): @@ -117,16 +113,16 @@ class BrushButton(_ColorButton): self._alpha = alpha self._preview.queue_draw() - def expose(self, widget, event): - if self._ctx is None: - self._setup() + def draw(self, widget, ctx): + #if self._ctx is None: + # self._setup() if self.get_window() is not None: center = style.STANDARD_ICON_SIZE / 2 - self._ctx.rectangle(0, 0, style.STANDARD_ICON_SIZE, + 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() + ctx.set_source_rgb(1.0, 1.0, 1.0) + ctx.fill() if self.is_stamping(): width = self._resized_stamp.get_width() @@ -134,31 +130,26 @@ class BrushButton(_ColorButton): dx = center - width / 2 dy = center - height / 2 - self._ctx.rectangle(dx, dy, width, height) - temp_ctx = gtk.gdk.CairoContext(self._ctx) - temp_ctx.set_source_pixbuf(self._resized_stamp, 0, 0) - self._ctx.paint() + ctx.rectangle(dx, dy, width, height) + Gdk.cairo_set_source_pixbuf(ctx, self._resized_stamp, 0, 0) + ctx.paint() 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_rgba(red, green, blue, self._alpha) + ctx.set_source_rgba(red, green, blue, self._alpha) if self._brush_shape == 'circle': - self._ctx.arc(center, center, self._brush_size / 2, 0., + ctx.arc(center, center, self._brush_size / 2, 0., 2 * math.pi) - self._ctx.fill() + ctx.fill() elif self._brush_shape == 'square': - self._ctx.rectangle(center - self._brush_size / 2, + ctx.rectangle(center - self._brush_size / 2, center - self._brush_size / 2, self._brush_size, self._brush_size) - self._ctx.fill() + 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): @@ -186,11 +177,11 @@ class BrushButton(_ColorButton): return True -class ButtonStrokeColor(gtk.ToolItem): +class ButtonStrokeColor(Gtk.ToolItem): """Class to manage the Stroke Color of a Button""" __gtype_name__ = 'BrushColorToolButton' - __gsignals__ = {'color-set': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, + __gsignals__ = {'color-set': (GObject.SignalFlags.RUN_FIRST, None, tuple())} def __init__(self, activity, **kwargs): @@ -201,9 +192,9 @@ class ButtonStrokeColor(gtk.ToolItem): self._palette_invoker = ToolInvoker() self._palette = None - gobject.GObject.__init__(self, **kwargs) + GObject.GObject.__init__(self, **kwargs) - # The gtk.ToolButton has already added a normal button. + # The Gtk.ToolButton has already added a normal button. # Replace it with a ColorButton self.color_button = BrushButton(has_invoker=False) self.add(self.color_button) @@ -212,7 +203,7 @@ class ButtonStrokeColor(gtk.ToolItem): self.color_button.set_stamp_size(20) # The following is so that the behaviour on the toolbar is correct. - self.color_button.set_relief(gtk.RELIEF_NONE) + self.color_button.set_relief(Gtk.ReliefStyle.NONE) self._palette_invoker.attach_tool(self) @@ -231,51 +222,54 @@ class ButtonStrokeColor(gtk.ToolItem): return True def __notify_change(self, widget, pspec): - new_color = self.alloc_color(self.get_color()) - self.color_button.set_color(new_color) + #new_color = self.alloc_color(self.get_color()) + #self.color_button.set_color(new_color) + self.color_button.set_color(self.get_color()) self.notify(pspec.name) def _color_button_cb(self, widget, pspec): color = self.get_color() self.set_stroke_color(color) - def alloc_color(self, color): - colormap = self._activity.area.get_colormap() - return colormap.alloc_color(color.red, color.green, color.blue) +# def alloc_color(self, color): +# colormap = self._activity.area.get_colormap() +# return colormap.alloc_color(color.red, color.green, color.blue) def create_palette(self): self._palette = self.get_child().create_palette() color_palette_hbox = self._palette._picker_hbox - content_box = gtk.VBox() + content_box = Gtk.VBox() - self._brush_table = gtk.Table(2, 2) + self._brush_table = Gtk.Table(2, 2) self._brush_table.set_col_spacing(0, style.DEFAULT_PADDING) # This is where we set restrictions for size: # Initial value, minimum value, maximum value, step - adj = gtk.Adjustment(self.properties['line size'], 1.0, 100.0, 1.0) - self.size_scale = gtk.HScale(adj) - self.size_scale.set_value_pos(gtk.POS_RIGHT) + adj = Gtk.Adjustment(self.properties['line size'], 1.0, 100.0, 1.0) + self.size_scale = Gtk.HScale() + self.size_scale.set_adjustment(adj) + self.size_scale.set_value_pos(Gtk.PositionType.RIGHT) self.size_scale.set_digits(0) self.size_scale.set_size_request(style.zoom(150), -1) - label = gtk.Label(_('Size')) + label = Gtk.Label(label=_('Size')) row = 0 self._brush_table.attach(label, 0, 1, row, row + 1) self._brush_table.attach(self.size_scale, 1, 2, row, row + 1) - content_box.pack_start(self._brush_table) + content_box.pack_start(self._brush_table, True, True, 0) self.size_scale.connect('value-changed', self._on_value_changed) # Control alpha alpha = self.properties['alpha'] * 100 - adj_alpha = gtk.Adjustment(alpha, 10.0, 100.0, 1.0) - self.alpha_scale = gtk.HScale(adj_alpha) - self.alpha_scale.set_value_pos(gtk.POS_RIGHT) + adj_alpha = Gtk.Adjustment(alpha, 10.0, 100.0, 1.0) + self.alpha_scale = Gtk.HScale() + self.alpha_scale.set_adjustment(adj_alpha) + self.alpha_scale.set_value_pos(Gtk.PositionType.RIGHT) self.alpha_scale.set_digits(0) self.alpha_scale.set_size_request(style.zoom(150), -1) - self.alpha_label = gtk.Label(_('Opacity')) + self.alpha_label = Gtk.Label(label=_('Opacity')) row = row + 1 self._brush_table.attach(self.alpha_label, 0, 1, row, row + 1) self._brush_table.attach(self.alpha_scale, 1, 2, row, row + 1) @@ -283,37 +277,37 @@ class ButtonStrokeColor(gtk.ToolItem): self.alpha_scale.connect('value-changed', self._on_alpha_changed) # User is able to choose Shapes for 'Brush' and 'Eraser' - self.vbox_brush_options = gtk.VBox() - shape_box = gtk.HBox() - content_box.pack_start(self.vbox_brush_options) + self.vbox_brush_options = Gtk.VBox() + shape_box = Gtk.HBox() + content_box.pack_start(self.vbox_brush_options, True, True, 0) item1 = RadioToolButton() item1.set_icon_name('tool-shape-ellipse') - item1.set_group(None) item1.set_active(True) item2 = RadioToolButton() item2.set_icon_name('tool-shape-rectangle') - item2.set_group(item1) + item2.props.group = item1 item1.connect('toggled', self._on_toggled, self.properties, 'circle') item2.connect('toggled', self._on_toggled, self.properties, 'square') - shape_box.pack_start(gtk.Label(_('Shape'))) - shape_box.pack_start(item1) - shape_box.pack_start(item2) + shape_box.pack_start(Gtk.Label(_('Shape')), True, True, 0) + shape_box.pack_start(item1, True, True, 0) + shape_box.pack_start(item2, True, True, 0) - self.vbox_brush_options.pack_start(shape_box) + self.vbox_brush_options.pack_start(shape_box, True, True, 0) - keep_aspect_checkbutton = gtk.CheckButton(_('Keep aspect')) + keep_aspect_checkbutton = Gtk.CheckButton(_('Keep aspect')) ratio = self._activity.area.keep_aspect_ratio keep_aspect_checkbutton.set_active(ratio) keep_aspect_checkbutton.connect('toggled', self._keep_aspect_checkbutton_toggled) - self.vbox_brush_options.pack_start(keep_aspect_checkbutton) + self.vbox_brush_options.pack_start(keep_aspect_checkbutton, True, True, + 0) - color_palette_hbox.pack_start(gtk.VSeparator(), + color_palette_hbox.pack_start(Gtk.VSeparator(), True, True, padding=style.DEFAULT_SPACING) - color_palette_hbox.pack_start(content_box) + color_palette_hbox.pack_start(content_box, True, True, 0) color_palette_hbox.show_all() self._update_palette() return self._palette @@ -326,9 +320,9 @@ class ButtonStrokeColor(gtk.ToolItem): if self.color_button.is_stamping(): # Hide palette color widgets: for ch in palette_children[:4]: - ch.hide_all() + ch.hide() # Hide brush options: - self.vbox_brush_options.hide_all() + self.vbox_brush_options.hide() self.alpha_label.hide() self.alpha_scale.hide() # Change title: @@ -384,7 +378,7 @@ class ButtonStrokeColor(gtk.ToolItem): self._palette_invoker.detach() self._palette_invoker = palette_invoker - palette_invoker = gobject.property( + palette_invoker = GObject.property( type=object, setter=set_palette_invoker, getter=get_palette_invoker) def set_color(self, color): @@ -393,7 +387,7 @@ class ButtonStrokeColor(gtk.ToolItem): def get_color(self): return self.get_child().props.color - color = gobject.property(type=object, getter=get_color, setter=set_color) + color = GObject.property(type=object, getter=get_color, setter=set_color) def set_title(self, title): self.get_child().props.title = title @@ -401,7 +395,7 @@ class ButtonStrokeColor(gtk.ToolItem): def get_title(self): return self.get_child().props.title - title = gobject.property(type=str, getter=get_title, setter=set_title) + title = GObject.property(type=str, getter=get_title, setter=set_title) def do_expose_event(self, event): child = self.get_child() @@ -409,11 +403,11 @@ class ButtonStrokeColor(gtk.ToolItem): if self._palette and self._palette.is_up(): invoker = self._palette.props.invoker invoker.draw_rectangle(event, self._palette) - elif child.state == gtk.STATE_PRELIGHT: - child.style.paint_box(event.window, gtk.STATE_PRELIGHT, - gtk.SHADOW_NONE, event.area, + elif child.state == Gtk.StateType.PRELIGHT: + child.style.paint_box(event.window, Gtk.StateType.PRELIGHT, + Gtk.ShadowType.NONE, event.area, child, 'toolbutton-prelight', allocation.x, allocation.y, allocation.width, allocation.height) - gtk.ToolButton.do_expose_event(self, event) + Gtk.ToolButton.do_expose_event(self, event) -- cgit v0.9.1