diff options
-rw-r--r-- | Area.py | 286 | ||||
-rw-r--r-- | Desenho.py | 33 | ||||
-rw-r--r-- | OficinaActivity.py | 37 | ||||
-rw-r--r-- | fontcombobox.py | 29 | ||||
-rwxr-xr-x | setup.py | 2 | ||||
-rw-r--r-- | toolbox.py | 166 | ||||
-rw-r--r-- | widgets.py | 166 |
7 files changed, 355 insertions, 364 deletions
@@ -62,20 +62,23 @@ Walter Bender (walter@laptop.org) """ -import gtk -import gobject +from gi.repository import Gtk +from gi.repository import Gdk +from gi.repository import GdkPixbuf +from gi.repository import GObject +from gi.repository import Pango + import logging import os import tempfile import math -import pango import cairo import StringIO import array from Desenho import Desenho from urlparse import urlparse -from sugar.graphics.style import zoom +from sugar3.graphics.style import zoom FALLBACK_FILL = True @@ -92,35 +95,35 @@ TARGET_URI = 0 MAX_UNDO_STEPS = 12 -class Area(gtk.DrawingArea): +class Area(Gtk.DrawingArea): __gsignals__ = { - 'undo': (gobject.SIGNAL_ACTION, gobject.TYPE_NONE, ([])), - 'redo': (gobject.SIGNAL_ACTION, gobject.TYPE_NONE, ([])), - 'action-saved': (gobject.SIGNAL_ACTION, gobject.TYPE_NONE, ([])), - 'select': (gobject.SIGNAL_ACTION, gobject.TYPE_NONE, ([])), + 'undo': (GObject.SignalFlags.ACTION, None, ([])), + 'redo': (GObject.SignalFlags.ACTION, None, ([])), + 'action-saved': (GObject.SignalFlags.ACTION, None, ([])), + 'select': (GObject.SignalFlags.ACTION, None, ([])), } def __init__(self, activity): """ Initialize the object from class Area which is derived - from gtk.DrawingArea. + from Gtk.DrawingArea. @param self -- the Area object (GtkDrawingArea) @param activity -- the parent window """ - gtk.DrawingArea.__init__(self) - - self.set_events(gtk.gdk.POINTER_MOTION_MASK | - gtk.gdk.POINTER_MOTION_HINT_MASK | - gtk.gdk.BUTTON_PRESS_MASK | - gtk.gdk.BUTTON_RELEASE_MASK | - gtk.gdk.EXPOSURE_MASK | - gtk.gdk.LEAVE_NOTIFY_MASK | - gtk.gdk.ENTER_NOTIFY_MASK | - gtk.gdk.KEY_PRESS_MASK) - - self.connect("expose_event", self.expose) + GObject.GObject.__init__(self) + + self.set_events(Gdk.EventMask.POINTER_MOTION_MASK | + Gdk.EventMask.POINTER_MOTION_HINT_MASK | + Gdk.EventMask.BUTTON_PRESS_MASK | + Gdk.EventMask.BUTTON_RELEASE_MASK | + Gdk.EventMask.EXPOSURE_MASK | + Gdk.EventMask.LEAVE_NOTIFY_MASK | + Gdk.EventMask.ENTER_NOTIFY_MASK | + Gdk.EventMask.KEY_PRESS_MASK) + + self.connect("draw", self.draw) self.connect("motion_notify_event", self.mousemove) self.connect("button_press_event", self.mousedown) self.connect("button_release_event", self.mouseup) @@ -128,23 +131,22 @@ class Area(gtk.DrawingArea): self.connect("leave_notify_event", self.mouseleave) self.connect("enter_notify_event", self.mouseenter) - target = [('text/uri-list', 0, TARGET_URI)] - self.drag_dest_set(gtk.DEST_DEFAULT_ALL, target, - gtk.gdk.ACTION_COPY | gtk.gdk.ACTION_MOVE) + target = [Gtk.TargetEntry.new('text/uri-list', 0, TARGET_URI)] + self.drag_dest_set(Gtk.DestDefaults.ALL, target, + Gdk.DragAction.COPY | Gdk.DragAction.MOVE) self.connect('drag_data_received', self.drag_data_received) self.set_can_focus(True) self.grab_focus() + # TODO gtk3 + # self.set_extension_events(Gdk.EXTENSION_EVENTS_CURSOR) - self.set_extension_events(gtk.gdk.EXTENSION_EVENTS_CURSOR) ## Define which tool is been used. ## It is now described as a dictionnary, ## with the following keys: ## - 'name' : a string ## - 'line size' : a integer ## - 'stamp size' : a integer - ## - 'fill color' : a gtk.gdk.Color object - ## - 'stroke color' : a gtk.gdk.Color object ## - 'line shape' : a string - 'circle' or 'square', for now ## - 'fill' : a Boolean value ## - 'vertices' : a integer @@ -153,8 +155,6 @@ class Area(gtk.DrawingArea): 'name': 'brush', 'line size': 4, 'stamp size': self._get_stamp_size(), - 'fill color': None, - 'stroke color': None, 'line shape': 'circle', 'fill': True, 'cairo_stroke_color': (0.0, 0.0, 0.0, 1.0), @@ -178,7 +178,7 @@ class Area(gtk.DrawingArea): self._font_description = None self.set_font_description( - pango.FontDescription(self.tool['font_description'])) + Pango.FontDescription(self.tool['font_description'])) # selection properties self.clear_selection() @@ -196,10 +196,10 @@ class Area(gtk.DrawingArea): def set_font_description(self, fd): self._font_description = fd self.activity.textview.modify_font(fd) - self.tool['font_description'] = str(fd) + self.tool['font_description'] = fd.to_string() def get_font_description(self): - return self._font_description + return Pango.FontDescription(self.tool['font_description']) def _get_stamp_size(self): """Set the stamp initial size, based on the display DPI.""" @@ -241,11 +241,15 @@ class Area(gtk.DrawingArea): return True + def get_size(self): + rect = self.get_allocation() + return rect.width, rect.height + def _init_temp_canvas(self, area=None): #logging.error('init_temp_canvas.') #self.drawing_canvas.flush() if area == None: - width, height = self.get_window().get_size() + width, height = self.get_size() self.temp_ctx.rectangle(0, 0, width, height) else: self.temp_ctx.rectangle(area.x, area.y, area.width, area.height) @@ -279,7 +283,7 @@ class Area(gtk.DrawingArea): """ self.drawing_ctx.set_line_width(size) - def expose(self, widget, event): + def draw(self, widget, context): """ This function define which canvas will be showed to the user. Show up the Area object (GtkDrawingArea). @@ -288,11 +292,11 @@ class Area(gtk.DrawingArea): @param event -- GdkEvent """ - area = event.area +# area = event.area - context = self.get_window().cairo_create() - context.rectangle(area.x, area.y, area.width, area.height) - context.clip() +# context = self.get_window().cairo_create() +# context.rectangle(area.x, area.y, area.width, area.height) +# context.clip() if self.desenha: #logging.error('Expose use temp canvas area %s', area) @@ -304,7 +308,8 @@ class Area(gtk.DrawingArea): context.set_source_surface(self.drawing_canvas) context.paint() self.show_tool_shape(context) - self._init_temp_canvas(area) + # TODO: gtk3 how get the area to avoid redrawing all ? + self._init_temp_canvas() # area) self.display_selection_border(context) def show_tool_shape(self, context): @@ -350,7 +355,7 @@ class Area(gtk.DrawingArea): @param event -- GdkEvent """ - width, height = self.get_window().get_size() + width, height = self.get_size() coords = int(event.x), int(event.y) # text @@ -363,13 +368,13 @@ class Area(gtk.DrawingArea): elif self.text_in_progress: design_mode = False try: - # This works for a gtk.Entry + # This works for a Gtk.Entry text = self.activity.textview.get_text() except AttributeError: - # This works for a gtk.TextView + # This works for a Gtk.TextView buf = self.activity.textview.get_buffer() start, end = buf.get_bounds() - text = buf.get_text(start, end) + text = buf.get_text(start, end, True) if text is not None: self.d.text(widget, event) @@ -378,12 +383,14 @@ class Area(gtk.DrawingArea): self.oldx, self.oldy = coords - x, y, state = event.window.get_pointer() + # TODO: get_pointer is deprecated +# http://developer.gnome.org/gtk3/3.4/GtkWidget.html#gtk-widget-get-pointer + _pointer, x, y, state = event.window.get_pointer() if self.tool['name'] == 'picker': self.pick_color(x, y) - if state & gtk.gdk.BUTTON1_MASK: + if state & Gdk.ModifierType.BUTTON1_MASK: #Handle with the left button click event. if self.tool['name'] == 'eraser': self.last = [] @@ -469,20 +476,20 @@ class Area(gtk.DrawingArea): """ x = event.x y = event.y - state = event.state + state = event.get_state() self.x_cursor, self.y_cursor = int(x), int(y) coords = int(x), int(y) if self.tool['name'] in ['rectangle', 'ellipse', 'line']: - if (state & gtk.gdk.SHIFT_MASK) or \ + if (state & Gdk.ModifierType.SHIFT_MASK) or \ self.keep_shape_ratio: if self.tool['name'] in ['rectangle', 'ellipse']: coords = self._keep_selection_ratio(coords) elif self.tool['name'] == 'line': coords = self._keep_line_ratio(coords) - if state & gtk.gdk.BUTTON1_MASK: + if state & Gdk.ModifierType.BUTTON1_MASK: if self.tool['name'] == 'eraser': self.d.eraser(widget, coords, self.last) self.last = coords @@ -517,7 +524,7 @@ class Area(gtk.DrawingArea): self.d.move_selection(widget, coords) else: # create a selected area - if (state & gtk.gdk.CONTROL_MASK) or \ + if (state & Gdk.ModifierType.CONTROL_MASK) or \ self.keep_aspect_ratio: coords = self._keep_selection_ratio(coords) self.d.selection(widget, coords) @@ -563,9 +570,11 @@ class Area(gtk.DrawingArea): # show appropiate cursor if (coords[0] < sel_x) or (coords[0] > sel_x + sel_width) or \ (coords[1] < sel_y) or (coords[1] > sel_y + sel_height): - self.get_window().set_cursor(gtk.gdk.Cursor(gtk.gdk.CROSS)) + self.get_window().set_cursor(Gdk.Cursor.new( + Gdk.CursorType.CROSS)) else: - self.get_window().set_cursor(gtk.gdk.Cursor(gtk.gdk.FLEUR)) + self.get_window().set_cursor(Gdk.Cursor.new( + Gdk.CursorType.FLEUR)) elif self.tool['name'] == 'freeform': self.desenha = True @@ -573,7 +582,7 @@ class Area(gtk.DrawingArea): self.d.freeform(widget, coords, True, self.tool['fill'], "moving") - gtk.gdk.event_request_motions(event) + Gdk.event_request_motions(event) def mouseup(self, widget, event): """Make the Area object (GtkDrawingArea) @@ -585,14 +594,14 @@ class Area(gtk.DrawingArea): """ coords = int(event.x), int(event.y) if self.tool['name'] in ['rectangle', 'ellipse', 'line']: - if (event.state & gtk.gdk.SHIFT_MASK) or \ + if (event.get_state() & Gdk.ModifierType.SHIFT_MASK) or \ self.keep_shape_ratio: if self.tool['name'] in ['rectangle', 'ellipse']: coords = self._keep_selection_ratio(coords) if self.tool['name'] == 'line': coords = self._keep_line_ratio(coords) - width, height = self.get_window().get_size() + width, height = self.get_size() private_undo = False if self.desenha: @@ -618,12 +627,14 @@ class Area(gtk.DrawingArea): elif self.tool['name'] == 'bucket': if FALLBACK_FILL: - self.get_window().set_cursor(gtk.gdk.Cursor(gtk.gdk.WATCH)) - gobject.idle_add(self.flood_fill, coords[0], coords[1]) + self.get_window().set_cursor(Gdk.Cursor.new( + Gdk.CursorType.WATCH)) + GObject.idle_add(self.flood_fill, coords[0], coords[1]) else: - width, height = self.get_window().get_size() - self.get_window().set_cursor(gtk.gdk.Cursor(gtk.gdk.WATCH)) - gobject.idle_add(self.fast_flood_fill, widget, coords[0], + width, height = self.get_size() + self.get_window().set_cursor(Gdk.Cursor.new( + Gdk.CursorType.WATCH)) + GObject.idle_add(self.fast_flood_fill, widget, coords[0], coords[1], width, height) elif self.tool['name'] == 'triangle': @@ -658,7 +669,7 @@ class Area(gtk.DrawingArea): if not private_undo and self.tool['name'] != 'bucket': # We have to avoid saving an undo state if the bucket tool # is selected because this undo state is called before the - # gobject.idle_add (with the fill_flood function) finishes + # GObject.idle_add (with the fill_flood function) finishes # and an unconsistent undo state is saved self.enable_undo() widget.queue_draw() @@ -669,8 +680,8 @@ class Area(gtk.DrawingArea): height, self.gc_line.foreground.pixel) widget.queue_draw() self.enable_undo() - display = gtk.gdk.display_get_default() - cursor = gtk.gdk.cursor_new_from_name(display, 'paint-bucket') + display = Gdk.Display.get_default() + cursor = Gdk.Cursor.new_from_name(display, 'paint-bucket') self.get_window().set_cursor(cursor) def flood_fill(self, x, y): @@ -733,8 +744,8 @@ class Area(gtk.DrawingArea): self.queue_draw() self.enable_undo() - display = gtk.gdk.display_get_default() - cursor = gtk.gdk.cursor_new_from_name(display, 'paint-bucket') + display = Gdk.Display.get_default() + cursor = Gdk.Cursor.new_from_name(display, 'paint-bucket') self.get_window().set_cursor(cursor) def pick_color(self, x, y): @@ -753,13 +764,12 @@ class Area(gtk.DrawingArea): green = ord(pixels[1]) * 256 blue = ord(pixels[0]) * 256 - stroke_color = gtk.gdk.Color(red, green, blue) + stroke_color = Gdk.Color(red, green, blue) # set in the area - self.tool['stroke color'] = stroke_color - self.set_stroke_color(self.tool['stroke color']) + self.set_stroke_color(stroke_color) - # update the stroke color button + # update the stroke_color in the button self.activity.get_toolbar_box().brush_button.set_color(stroke_color) self.activity.get_toolbar_box().brush_button.stop_stamping() @@ -788,7 +798,7 @@ class Area(gtk.DrawingArea): # Change stamp, get it from selection: pixbuf_data = StringIO.StringIO() self.get_selection().write_to_png(pixbuf_data) - pxb_loader = gtk.gdk.PixbufLoader(image_type='png') + pxb_loader = GdkPixbuf.PixbufLoader.new_with_type('png') pxb_loader.write(pixbuf_data.getvalue()) self.pixbuf_stamp = pxb_loader.get_pixbuf() @@ -818,7 +828,7 @@ class Area(gtk.DrawingArea): wr, hr = int(stamp_size * w * 1.0 / h), stamp_size self.stamp_dimentions = wr, hr self.resized_stamp = self.pixbuf_stamp.scale_simple(wr, hr, - gtk.gdk.INTERP_HYPER) + GdkPixbuf.InterpType.HYPER) # Remove selected area self.getout() @@ -913,7 +923,7 @@ class Area(gtk.DrawingArea): @param self -- the Area object (GtkDrawingArea) """ - clipBoard = gtk.Clipboard() + clipBoard = Gtk.Clipboard() if 'SUGAR_ACTIVITY_ROOT' in os.environ: temp_dir = os.path.join(os.environ.get('SUGAR_ACTIVITY_ROOT'), 'instance') @@ -937,7 +947,7 @@ class Area(gtk.DrawingArea): """ Determine type data to put in clipboard @param self -- the Area object (GtkDrawingArea) - @param clipboard -- a gtk.Clipboard object + @param clipboard -- a Gtk.Clipboard object @param selection_data -- data of selection @param info -- the app assigned integer associated with a target @param data -- user data (tempPath) @@ -951,7 +961,7 @@ class Area(gtk.DrawingArea): """ Clear the clipboard @param self -- the Area object (GtkDrawingArea) - @param clipboard -- a gtk.Clipboard object + @param clipboard -- a Gtk.Clipboard object @param data -- user data (tempPath) """ if (data != None): @@ -972,12 +982,12 @@ class Area(gtk.DrawingArea): @param self -- the Area object (GtkDrawingArea) """ - width, height = self.get_window().get_size() + width, height = self.get_size() tempPath = os.path.join("/tmp", "tempFile") tempPath = os.path.abspath(tempPath) - clipBoard = gtk.Clipboard() + clipBoard = Gtk.Clipboard() if clipBoard.wait_is_image_available(): logging.debug('Area.paste(self): Wait is image available') @@ -986,8 +996,7 @@ class Area(gtk.DrawingArea): width, height = pixbuf_sel.get_width(), pixbuf_sel.get_height() self.temp_ctx.rectangle(0, 0, width, height) - temp_ctx = gtk.gdk.CairoContext(self.temp_ctx) - temp_ctx.set_source_pixbuf(pixbuf_sel) + Gdk.cairo_set_source_pixbuf(self.temp_ctx, pixbuf_sel, 0, 0) self.temp_ctx.paint() self.set_selection_bounds(0, 0, width, height) self.desenha = True @@ -1010,24 +1019,28 @@ class Area(gtk.DrawingArea): """Set fill color. @param self -- the Area object (GtkDrawingArea) - @param color -- a gdk.Color object + @param color -- a Gdk.Color object """ alpha = self.tool['alpha'] - self.tool['cairo_fill_color'] = (color.red_float, - color.green_float, color.blue_float, alpha) + red = color.red / 65535.0 + green = color.green / 65535.0 + blue = color.blue / 65535.0 + self.tool['cairo_fill_color'] = (red, green, blue, alpha) def set_stroke_color(self, color): - """Set stroke color. + """Set cairo_stroke_color. @param self -- the Area object (GtkDrawingArea) - @param color -- a gdk.Color object + @param color -- a Gdk.Color object """ alpha = self.tool['alpha'] - self.tool['cairo_stroke_color'] = (color.red_float, - color.green_float, color.blue_float, alpha) - self.activity.textview.modify_text(gtk.STATE_NORMAL, color) + red = color.red / 65535.0 + green = color.green / 65535.0 + blue = color.blue / 65535.0 + self.tool['cairo_stroke_color'] = (red, green, blue, alpha) + self.activity.textview.modify_text(Gtk.StateType.NORMAL, color) def set_alpha(self, alpha): """ @@ -1081,8 +1094,8 @@ class Area(gtk.DrawingArea): pix_manip = numpy.ones(pix_manip2.shape, dtype=numpy.uint8) \ * 255 pix_manip2 = pix_manip - pix_manip2 - temp_pix = gtk.gdk.pixbuf_new_from_array(pix_manip2, - gtk.gdk.COLORSPACE_RGB, 8) + temp_pix = GdkPixbuf.Pixbuf.new_from_array(pix_manip2, + GdkPixbuf.Colorspace.RGB, 8) except (ImportError, ImportWarning): import string a = temp_pix.get_pixels() @@ -1090,7 +1103,7 @@ class Area(gtk.DrawingArea): for i in range(len(a)): b[i] = chr(255 - ord(a[i])) buff = string.join(b, '') - temp_pix = gtk.gdk.pixbuf_new_from_data(buff, + temp_pix = GdkPixbuf.Pixbuf.new_from_data(buff, temp_pix.get_colorspace(), temp_pix.get_has_alpha(), temp_pix.get_bits_per_sample(), @@ -1117,25 +1130,24 @@ class Area(gtk.DrawingArea): self._do_process(widget, proc_mirror) def _do_process(self, widget, apply_process): - self.get_window().set_cursor(gtk.gdk.Cursor(gtk.gdk.WATCH)) - gobject.idle_add(self._do_process_internal, widget, apply_process) + self.get_window().set_cursor(Gdk.Cursor.new(Gdk.CursorType.WATCH)) + GObject.idle_add(self._do_process_internal, widget, apply_process) def _surface_to_pixbuf(self, surface): # copy from the surface to the pixbuf pixbuf_data = StringIO.StringIO() surface.write_to_png(pixbuf_data) - pxb_loader = gtk.gdk.PixbufLoader(image_type='png') + pxb_loader = GdkPixbuf.PixbufLoader.new_with_type('png') pxb_loader.write(pixbuf_data.getvalue()) return pxb_loader.get_pixbuf() def _pixbuf_to_context(self, pixbuf, context, x=0, y=0): # copy from the pixbuf to the drawing context - draw_gdk_ctx = gtk.gdk.CairoContext(context) - draw_gdk_ctx.save() - draw_gdk_ctx.translate(x, y) - draw_gdk_ctx.set_source_pixbuf(pixbuf, 0, 0) - draw_gdk_ctx.paint() - draw_gdk_ctx.restore() + context.save() + context.translate(x, y) + Gdk.cairo_set_source_pixbuf(context, pixbuf, 0, 0) + context.paint() + context.restore() def _do_process_internal(self, widget, apply_process): @@ -1167,8 +1179,8 @@ class Area(gtk.DrawingArea): @param self -- the Area object (GtkDrawingArea) @param widget -- the Area object (GtkDrawingArea) """ - self.get_window().set_cursor(gtk.gdk.Cursor(gtk.gdk.WATCH)) - gobject.idle_add(self._rotate, widget, 90) + self.get_window().set_cursor(Gdk.Cursor.new(Gdk.CursorType.WATCH)) + GObject.idle_add(self._rotate, widget, 90) def rotate_right(self, widget): """Rotate the image. @@ -1176,8 +1188,8 @@ class Area(gtk.DrawingArea): @param self -- the Area object (GtkDrawingArea) @param widget -- the Area object (GtkDrawingArea) """ - self.get_window().set_cursor(gtk.gdk.Cursor(gtk.gdk.WATCH)) - gobject.idle_add(self._rotate, widget, 270) + self.get_window().set_cursor(Gdk.Cursor.new(Gdk.CursorType.WATCH)) + GObject.idle_add(self._rotate, widget, 270) def _rotate(self, widget, angle): """Rotate the image. @@ -1185,14 +1197,14 @@ class Area(gtk.DrawingArea): @param self -- the Area object (GtkDrawingArea) @param widget -- the Area object (GtkDrawingArea) """ - self.get_window().set_cursor(gtk.gdk.Cursor(gtk.gdk.WATCH)) + self.get_window().set_cursor(Gdk.Cursor.new(Gdk.CursorType.WATCH)) if self.is_selected(): x, y, width, height = self.get_selection_bounds() surface = self.get_selection() else: x, y = 0, 0 - width, height = self.get_window().get_size() + width, height = self.get_size() surface = self.drawing_canvas temp_pix = self._surface_to_pixbuf(surface) @@ -1318,7 +1330,7 @@ class Area(gtk.DrawingArea): """ logging.debug('Area.load_image Loading file %s', name) - pixbuf = gtk.gdk.pixbuf_new_from_file(name) + pixbuf = GdkPixbuf.Pixbuf.new_from_file(name) width, height = (int)(pixbuf.get_width()), (int)(pixbuf.get_height()) logging.debug('image size %d x %d', width, height) @@ -1361,14 +1373,7 @@ class Area(gtk.DrawingArea): ''' Method to configure all tools. - @param - tool: a dictionary with the following keys: - 'name': a string - 'line size': a integer - 'fill color': a gtk.gdk.Color object - 'stroke color': a gtk.gdk.Color object - 'line shape': a string - 'circle' or 'square', for now - 'fill': a Boolean value - 'vertices': a integer + @param - tool: a dictionary with the tool keys ''' # logging.debug('Area.set_tool %s', tool) self.tool = tool @@ -1376,17 +1381,17 @@ class Area(gtk.DrawingArea): if self.tool['line size'] is not None: self.configure_line(self.tool['line size']) - if self.tool['fill color'] is not None: - self.set_fill_color(self.tool['fill color']) - else: - # use black - self.set_fill_color(self.black) +# if self.tool['fill color'] is not None: +# self.set_fill_color(self.tool['fill color']) +# else: +# # use black +# self.set_fill_color(self.black) - if self.tool['stroke color'] is not None: - self.set_stroke_color(self.tool['stroke color']) - else: - # use black - self.set_stroke_color(self.black) +# if self.tool['stroke color'] is not None: +# self.set_stroke_color(self.tool['stroke color']) +# else: +# # use black +# self.set_stroke_color(self.black) except AttributeError: pass @@ -1401,15 +1406,15 @@ class Area(gtk.DrawingArea): 'eraser': 'eraser', 'bucket': 'paint-bucket'} - display = gtk.gdk.display_get_default() + display = Gdk.Display.get_default() if self.tool['name'] in cursors: name = cursors[self.tool['name']] - cursor = gtk.gdk.cursor_new_from_name(display, name) + cursor = Gdk.Cursor.new_from_name(display, name) elif self.tool['name'] == 'marquee-rectangular': - cursor = gtk.gdk.Cursor(gtk.gdk.CROSS) + cursor = Gdk.Cursor.new(Gdk.CursorType.CROSS) else: filename = os.path.join('images', self.tool['name'] + '.png') - pixbuf = gtk.gdk.pixbuf_new_from_file(filename) + pixbuf = GdkPixbuf.Pixbuf.new_from_file(filename) # Decide which is the cursor hot spot offset: if self.tool['name'] == 'stamp': @@ -1422,8 +1427,9 @@ class Area(gtk.DrawingArea): else: hotspot_x, hotspot_y = 0, 0 - cursor = gtk.gdk.Cursor(display, pixbuf, hotspot_x, hotspot_y) - except gobject.GError: + cursor = Gdk.Cursor.new_from_pixbuf(display, pixbuf, + hotspot_x, hotspot_y) + except GObject.GError: cursor = None self.get_window().set_cursor(cursor) @@ -1457,34 +1463,38 @@ class Area(gtk.DrawingArea): logging.debug('Unexpected error: %s', message) def key_press(self, widget, event): - if event.keyval == gtk.keysyms.BackSpace: + if event.keyval == Gdk.KEY_BackSpace: if self.is_selected(): # Remove selection # TODO if self.tool['name'] == 'marquee-rectangular': - self.get_window().set_cursor(gtk.gdk.Cursor(gtk.gdk.CROSS)) + self.get_window().set_cursor(Gdk.Cursor.new( + Gdk.CursorType.CROSS)) widget.queue_draw() self.enable_undo() - elif event.keyval == gtk.keysyms.a and gtk.gdk.CONTROL_MASK: + elif event.keyval == Gdk.KEY_a and Gdk.ModifierType.CONTROL_MASK: if self.is_selected(): self.getout() - width, height = self.get_window().get_size() + width, height = self.get_size() if self.tool['name'] == 'marquee-rectangular': - self.get_window().set_cursor(gtk.gdk.Cursor(gtk.gdk.FLEUR)) + self.get_window().set_cursor(Gdk.Cursor.new( + Gdk.CursorTypeFLEUR)) self.set_selection_bounds(0, 0, width - 1, height - 1) self.emit('select') widget.queue_draw() - elif event.keyval == gtk.keysyms.d and gtk.gdk.CONTROL_MASK: + elif event.keyval == Gdk.KEY_d and Gdk.ModifierType.CONTROL_MASK: if self.is_selected(): self.getout(True) if self.tool['name'] == 'marquee-rectangular': - self.get_window().set_cursor(gtk.gdk.Cursor(gtk.gdk.CROSS)) + self.get_window().set_cursor(Gdk.Cursor.new( + Gdk.CursorType.CROSS)) widget.queue_draw() - elif event.keyval == gtk.keysyms.Return: + elif event.keyval == Gdk.KEY_Return: self.getout(True) if self.tool['name'] == 'marquee-rectangular': - self.get_window().set_cursor(gtk.gdk.Cursor(gtk.gdk.CROSS)) + self.get_window().set_cursor(Gdk.Cursor.new( + Gdk.CursorType.CROSS)) widget.queue_draw() def change_line_size(self, delta): @@ -61,12 +61,12 @@ Walter Bender (walter@laptop.org) """ -import gtk +from gi.repository import Gdk +from gi.repository import GObject +from gi.repository import PangoCairo import logging import math -import gobject import cairo -import pangocairo RESIZE_DELAY = 500 # The time to wait for the resize operation to be # executed, after the resize controls are pressed. @@ -172,8 +172,8 @@ class Desenho: widget.drawing_ctx.save() widget.drawing_ctx.translate(dx, dy) widget.drawing_ctx.rectangle(dx, dy, width, height) - temp_ctx = gtk.gdk.CairoContext(widget.drawing_ctx) - temp_ctx.set_source_pixbuf(widget.resized_stamp, 0, 0) + Gdk.cairo_set_source_pixbuf(widget.drawing_ctx, widget.resized_stamp, + 0, 0) widget.drawing_ctx.paint() widget.drawing_ctx.restore() @@ -192,7 +192,7 @@ class Desenho: """ _color_str = self._rainbow_color_list[self._rainbow_counter] - _color = gtk.gdk.color_parse(_color_str) + _color = Gdk.color_parse(_color_str) self._rainbow_counter += 1 if self._rainbow_counter > 11: self._rainbow_counter = 0 @@ -564,7 +564,7 @@ class Desenho: widget.textos = [] x, y = 0, 0 - width, height = widget.window.get_size() + width, height = widget.get_size() # try to clear a selected area first if widget.is_selected(): x, y, width, height = widget.get_selection_bounds() @@ -595,7 +595,7 @@ class Desenho: buf = widget.activity.textview.get_buffer() start, end = buf.get_bounds() - text = buf.get_text(start, end) + text = buf.get_text(start, end, True) textview = widget.activity.textview tv_layout = textview.create_pango_layout(text) @@ -604,16 +604,15 @@ class Desenho: ctx.save() ctx.new_path() - pango_context = pangocairo.CairoContext(ctx) - pango_context.set_source_rgba(*widget.tool['cairo_stroke_color']) + ctx.set_source_rgba(*widget.tool['cairo_stroke_color']) - pango_layout = pango_context.create_layout() + pango_layout = PangoCairo.create_layout(ctx) pango_layout.set_font_description(widget.get_font_description()) - pango_layout.set_text(unicode(text)) + pango_layout.set_text(unicode(text), len(unicode(text))) tv_alloc = textview.get_allocation() - pango_context.move_to(tv_alloc.x, tv_alloc.y) - pango_context.show_layout(pango_layout) + ctx.move_to(tv_alloc.x, tv_alloc.y) + PangoCairo.show_layout(ctx, pango_layout) ctx.stroke() ctx.restore() @@ -692,8 +691,8 @@ class Desenho: """ # Add a timer for resize or update it if there is one already: if self._resize_timer is not None: - gobject.source_remove(self._resize_timer) - self._resize_timer = gobject.timeout_add(RESIZE_DELAY, + GObject.source_remove(self._resize_timer) + self._resize_timer = GObject.timeout_add(RESIZE_DELAY, self._do_resize, widget, width_percent, height_percent) def _do_resize(self, widget, width_percent, height_percent): @@ -756,7 +755,7 @@ class Desenho: self.clear_control_points() def adjust(self, widget, coords, locked=False): - width, height = widget.window.get_size() + width, height = widget.get_size() if widget.oldx > int(coords[0]): xi = int(coords[0]) xf = widget.oldx diff --git a/OficinaActivity.py b/OficinaActivity.py index 59cb28d..29cb2d3 100644 --- a/OficinaActivity.py +++ b/OficinaActivity.py @@ -61,11 +61,11 @@ Walter Bender (walter@laptop.org) """ -import gtk +from gi.repository import Gtk import logging -from sugar.activity import activity -from sugar.graphics import style +from sugar3.activity import activity +from sugar3.graphics import style from Area import Area from toolbox import DrawToolbarBox @@ -85,12 +85,12 @@ class OficinaActivity(activity.Activity): logging.debug('Starting Paint activity (Oficina)') - self.fixed = gtk.Fixed() + self.fixed = Gtk.Fixed() self.fixed.show() - self.fixed.modify_bg(gtk.STATE_NORMAL, + self.fixed.modify_bg(Gtk.StateType.NORMAL, style.COLOR_WHITE.get_gdk_color()) - self.textview = gtk.TextView() + self.textview = Gtk.TextView() self.fixed.put(self.textview, 0, 0) # These attributes are used in other classes, so they should be public @@ -98,10 +98,10 @@ class OficinaActivity(activity.Activity): self.area.show() self.fixed.put(self.area, 0, 0) - sw = gtk.ScrolledWindow() - sw.show() - sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) - self.set_canvas(sw) + self._sw = Gtk.ScrolledWindow() + self._sw.show() + self._sw.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC) + self.set_canvas(self._sw) toolbar_box = DrawToolbarBox(self) @@ -121,9 +121,9 @@ class OficinaActivity(activity.Activity): self.canvas.add_with_viewport(self.fixed) # to remove the border, we need set the shadowtype # in the viewport child of the scrolledwindow - self.canvas.get_children()[0].set_shadow_type(gtk.SHADOW_NONE) + self.canvas.get_children()[0].set_shadow_type(Gtk.ShadowType.NONE) self.disconnect(self._setup_handle) - self._setup_handle = self.fixed.connect('size_allocate', + self._setup_handle = self._sw.connect('size_allocate', size_allocate_cb) self._setup_handle = self.connect('map', map_cp) @@ -152,17 +152,18 @@ class OficinaActivity(activity.Activity): # of the canvas when the toolbars popup and the scrolls # keep visible. if height > allocation.height or width > allocation.width: - self.canvas.set_policy(gtk.POLICY_AUTOMATIC, - gtk.POLICY_AUTOMATIC) + self.canvas.set_policy(Gtk.PolicyType.AUTOMATIC, + Gtk.PolicyType.AUTOMATIC) else: - self.canvas.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC) + self.canvas.set_policy(Gtk.PolicyType.NEVER, + Gtk.PolicyType.AUTOMATIC) self.center_area() self.canvas.add_with_viewport(self.fixed) # to remove the border, we need set the shadowtype # in the viewport child of the scrolledwindow - self.canvas.get_children()[0].set_shadow_type(gtk.SHADOW_NONE) + self.canvas.get_children()[0].set_shadow_type(Gtk.ShadowType.NONE) self.canvas.get_children()[0].set_border_width(0) self.disconnect(self._setup_handle) @@ -196,8 +197,8 @@ class OficinaActivity(activity.Activity): the drawing area and center it on the canvas. """ - canvas_width = self.canvas.allocation.width - canvas_height = self.canvas.allocation.height + canvas_width = self.canvas.get_allocation().width + canvas_height = self.canvas.get_allocation().height area_width, area_height = self.area.get_size_request() # Avoid 'x' and 'y' to be outside the screen diff --git a/fontcombobox.py b/fontcombobox.py index 58f9140..9de5e34 100644 --- a/fontcombobox.py +++ b/fontcombobox.py @@ -15,21 +15,22 @@ # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -import gtk +from gi.repository import Gtk +from gi.repository import GObject FONT_BLACKLIST = ['cmex10', 'cmmi10', 'cmr10', 'cmsy10', 'esint10', 'eufm10', 'msam10', 'msbm10', 'rsfs10', 'wasy10'] -class FontComboBox(gtk.ComboBox): +class FontComboBox(Gtk.ComboBox): def __init__(self): - gtk.ComboBox.__init__(self) - font_renderer = gtk.CellRendererText() - self.pack_start(font_renderer) + GObject.GObject.__init__(self) + font_renderer = Gtk.CellRendererText() + self.pack_start(font_renderer, True) self.add_attribute(font_renderer, 'text', 0) self.add_attribute(font_renderer, 'font', 0) - font_model = gtk.ListStore(str) + font_model = Gtk.ListStore(str) context = self.get_pango_context() font_index = 0 @@ -39,15 +40,15 @@ class FontComboBox(gtk.ComboBox): name = family.get_name() if name not in FONT_BLACKLIST: font_model.append([name]) - font_faces = [] - for face in family.list_faces(): - face_name = face.get_face_name() - font_faces.append(face_name) - self.faces[name] = font_faces + # TODO gtk3 +# font_faces = [] +# for face in family.list_faces(): +# face_name = face.get_face_name() +# font_faces.append(face_name) +# self.faces[name] = font_faces - sorter = gtk.TreeModelSort(font_model) - sorter.set_sort_column_id(0, gtk.SORT_ASCENDING) - self.set_model(sorter) + font_model.set_sort_column_id(0, Gtk.SortType.ASCENDING) + self.set_model(font_model) self.show() def set_font_name(self, font_name): @@ -16,6 +16,6 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -from sugar.activity import bundlebuilder +from sugar3.activity import bundlebuilder if __name__ == "__main__": bundlebuilder.start() @@ -63,24 +63,26 @@ Walter Bender (walter@laptop.org) from gettext import gettext as _ -import gtk -import pango +from gi.repository import Gtk +from gi.repository import Gdk +from gi.repository import GObject +from gi.repository import Pango import logging -from sugar.activity.activity import EditToolbar -from sugar.graphics.toolcombobox import ToolComboBox -from sugar.graphics.toolbutton import ToolButton -from sugar.graphics.radiotoolbutton import RadioToolButton -from sugar.graphics.toggletoolbutton import ToggleToolButton -from sugar.graphics.objectchooser import ObjectChooser +from sugar3.activity.widgets import EditToolbar +from sugar3.graphics.toolcombobox import ToolComboBox +from sugar3.graphics.toolbutton import ToolButton +from sugar3.graphics.radiotoolbutton import RadioToolButton +from sugar3.graphics.toggletoolbutton import ToggleToolButton +from sugar3.graphics.objectchooser import ObjectChooser from widgets import ButtonStrokeColor -from sugar.graphics.colorbutton import ColorToolButton +from sugar3.graphics.colorbutton import ColorToolButton -from sugar.graphics import style +from sugar3.graphics import style -from sugar.activity.widgets import ActivityToolbarButton -from sugar.graphics.toolbarbox import ToolbarButton, ToolbarBox -from sugar.activity.widgets import StopButton +from sugar3.activity.widgets import ActivityToolbarButton +from sugar3.graphics.toolbarbox import ToolbarButton, ToolbarBox +from sugar3.activity.widgets import StopButton from fontcombobox import FontComboBox @@ -125,7 +127,7 @@ class DrawToolbarBox(ToolbarBox): image_button.props.label = _('Image') self.toolbar.insert(image_button, -1) - separator = gtk.SeparatorToolItem() + separator = Gtk.SeparatorToolItem() separator.props.draw = False separator.set_size_request(0, -1) separator.set_expand(True) @@ -142,8 +144,15 @@ class DrawToolbarBox(ToolbarBox): self.brush_button.set_brush_shape(area.tool['line shape']) self.brush_button.set_brush_size(area.tool['line size']) self.brush_button.set_stamp_size(area.tool['stamp size']) - if self._activity.area.tool['stroke color'] is not None: - self.brush_button.set_color(area.tool['stroke color']) + + # init the color + cairo_stroke_color = area.tool['cairo_stroke_color'] + red = cairo_stroke_color[0] * 65535 + green = cairo_stroke_color[1] * 65535 + blue = cairo_stroke_color[2] * 65535 + + stroke_color = Gdk.Color(red, green, blue) + self.brush_button.set_color(stroke_color) ##Make the Edit Toolbar @@ -159,7 +168,7 @@ class DrawEditToolbar(EditToolbar): self.copy.set_tooltip(_('Copy')) self.paste.set_tooltip(_('Paste')) - separator = gtk.SeparatorToolItem() + separator = Gtk.SeparatorToolItem() separator.set_draw(True) self.insert(separator, -1) @@ -248,11 +257,11 @@ class ToolsToolbarBuilder(): #self._stroke_color.set_icon_name('icon-stroke') self._stroke_color.set_title(_('Brush properties')) self._stroke_color.connect('notify::color', self._color_button_cb) - item = gtk.ToolItem() + item = Gtk.ToolItem() item.add(self._stroke_color) toolbar.insert(item, -1) - separator = gtk.SeparatorToolItem() + separator = Gtk.SeparatorToolItem() separator.set_draw(True) toolbar.insert(separator, -1) @@ -290,7 +299,7 @@ class ToolsToolbarBuilder(): activity.tool_group, _('Select Area')) toolbar.insert(self._tool_marquee_rectangular, -1) - separator = gtk.SeparatorToolItem() + separator = Gtk.SeparatorToolItem() separator.set_draw(True) toolbar.insert(separator, -1) @@ -315,7 +324,7 @@ class ToolsToolbarBuilder(): """ Set tool to the Area object. Configures tool's color and size. - @param self -- gtk.Toolbar + @param self -- Gtk.Toolbar @param widget -- The connected widget, if any; necessary in case this method is used in a connect() @param tool_name --The name of the selected tool @@ -332,15 +341,8 @@ class ToolsToolbarBuilder(): def _color_button_cb(self, widget, pspec): logging.error('ToolsToolbarBuilder._color_button_cb') - new_color = widget.alloc_color(widget.get_color()) + new_color = widget.get_color() self._activity.area.set_stroke_color(new_color) - self.properties['stroke color'] = new_color - - if isinstance(new_color, unicode): - new_color = gtk.gdk.Color(new_color) - self.properties['cairo_stroke_color'] = (new_color.red_float, - new_color.green_float, - new_color.blue_float, 0.3) def _on_signal_undo_cb(self, widget, data=None): self._verify_sensitive_buttons() @@ -375,67 +377,55 @@ class ButtonFillColor(ColorToolButton): color = self.get_color() self.set_fill_color(color) - def alloc_color(self, color): - colormap = self._activity.area.get_colormap() - return colormap.alloc_color(color.red, color.green, color.blue) - def set_fill_color(self, color): - new_color = self.alloc_color(color) - self._activity.area.set_fill_color(new_color) - self.properties['fill color'] = new_color - - if isinstance(new_color, unicode): - new_color = gtk.gdk.Color(new_color) - self.properties['cairo_fill_color'] = (new_color.red_float, - new_color.green_float, - new_color.blue_float, 0.3) + self._activity.area.set_fill_color(color) 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() # Fill option - fill_checkbutton = gtk.CheckButton(_('Fill')) + fill_checkbutton = Gtk.CheckButton(_('Fill')) fill_checkbutton.set_active(self.properties['fill']) fill_checkbutton.connect('toggled', self._on_fill_checkbutton_toggled) - content_box.pack_start(fill_checkbutton) + content_box.pack_start(fill_checkbutton, True, True, 0) - keep_aspect_checkbutton = gtk.CheckButton(_('Keep aspect')) + keep_aspect_checkbutton = Gtk.CheckButton(_('Keep aspect')) logging.error('Create palette : tool name %s', self.properties['name']) ratio = self._activity.area.keep_shape_ratio keep_aspect_checkbutton.set_active(ratio) keep_aspect_checkbutton.connect('toggled', self._on_keep_aspect_checkbutton_toggled) - content_box.pack_start(keep_aspect_checkbutton) + content_box.pack_start(keep_aspect_checkbutton, True, True, 0) # We want choose the number of sides to our polygon - spin = gtk.SpinButton() + spin = Gtk.SpinButton() # This is where we set restrictions for sides in Regular Polygon: # Initial value, minimum value, maximum value, step - adj = gtk.Adjustment(self.properties['vertices'], 3.0, 50.0, 1.0) + adj = Gtk.Adjustment(self.properties['vertices'], 3.0, 50.0, 1.0) spin.set_adjustment(adj) spin.set_numeric(True) - label = gtk.Label(_('Sides: ')) + label = Gtk.Label(label=_('Sides: ')) #For stars - #label = gtk.Label(_('Points: ')) + #label = Gtk.Label(label=_('Points: ')) - hbox = gtk.HBox() + hbox = Gtk.HBox() hbox.show_all() - hbox.pack_start(label) - hbox.pack_start(spin) + hbox.pack_start(label, True, True, 0) + hbox.pack_start(spin, True, True, 0) - content_box.pack_start(hbox) + content_box.pack_start(hbox, True, True, 0) hbox.show_all() spin.connect('value-changed', self._on_vertices_value_changed) - 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() return self._palette @@ -451,7 +441,7 @@ class ButtonFillColor(ColorToolButton): ##Make the Shapes Toolbar -class ShapesToolbar(gtk.Toolbar): +class ShapesToolbar(Gtk.Toolbar): _SHAPE_ARROW_NAME = 'arrow' _SHAPE_CURVE_NAME = 'curve' @@ -468,7 +458,7 @@ class ShapesToolbar(gtk.Toolbar): ##The Constructor def __init__(self, activity): - gtk.Toolbar.__init__(self) + GObject.GObject.__init__(self) self._activity = activity self.properties = self._activity.area.tool @@ -476,11 +466,11 @@ class ShapesToolbar(gtk.Toolbar): self._fill_color = ButtonFillColor(activity) self._fill_color.set_icon_name('icon-fill') self._fill_color.set_title(_('Shapes properties')) - item = gtk.ToolItem() + item = Gtk.ToolItem() item.add(self._fill_color) self.insert(item, -1) - separator = gtk.SeparatorToolItem() + separator = Gtk.SeparatorToolItem() separator.set_draw(True) self.insert(separator, -1) @@ -556,20 +546,16 @@ class ShapesToolbar(gtk.Toolbar): def set_tool(self, widget, tool, tool_name): tool['name'] = tool_name - - fill_color = self._fill_color.get_color() - tool['fill color'] = self._fill_color.alloc_color(fill_color) - self._activity.area.set_tool(tool) ##Make the Text Toolbar -class TextToolbar(gtk.Toolbar): +class TextToolbar(Gtk.Toolbar): _ACTION_TEXT_NAME = 'text' def __init__(self, activity): - gtk.Toolbar.__init__(self) + GObject.GObject.__init__(self) self._activity = activity self.properties = self._activity.area.tool @@ -578,7 +564,7 @@ class TextToolbar(gtk.Toolbar): self.insert(self._text, -1) self._text.connect('clicked', self.set_tool, self._ACTION_TEXT_NAME) - separator = gtk.SeparatorToolItem() + separator = Gtk.SeparatorToolItem() separator.set_draw(True) self.insert(separator, -1) @@ -592,20 +578,20 @@ class TextToolbar(gtk.Toolbar): self._italic.show() self._italic.connect('clicked', self.__italic_bt_cb) - separator = gtk.SeparatorToolItem() + separator = Gtk.SeparatorToolItem() separator.set_draw(True) self.insert(separator, -1) fd = activity.area.get_font_description() - self._font_size_combo = gtk.combo_box_new_text() + self._font_size_combo = Gtk.ComboBoxText() self._font_sizes = ['8', '10', '12', '14', '16', '20', '22', '24', '26', '28', '36', '48', '72'] self._font_size_changed_id = self._font_size_combo.connect('changed', self.__font_size_changed_cb) for i, s in enumerate(self._font_sizes): self._font_size_combo.append_text(s) - if int(s) == fd.get_size(): + if int(s) == (fd.get_size() /Pango.SCALE): self._font_size_combo.set_active(i) tool_item = ToolComboBox(self._font_size_combo) @@ -623,23 +609,23 @@ class TextToolbar(gtk.Toolbar): def __bold_bt_cb(self, button): fd = self._activity.area.get_font_description() if button.get_active(): - fd.set_weight(pango.WEIGHT_BOLD) + fd.set_weight(Pango.Weight.BOLD) else: - fd.set_weight(pango.WEIGHT_NORMAL) + fd.set_weight(Pango.Weight.NORMAL) self._activity.area.set_font_description(fd) def __italic_bt_cb(self, button): fd = self._activity.area.get_font_description() if button.get_active(): - fd.set_style(pango.STYLE_ITALIC) + fd.set_style(Pango.Style.ITALIC) else: - fd.set_style(pango.STYLE_NORMAL) + fd.set_style(Pango.Style.NORMAL) self._activity.area.set_font_description(fd) def __font_size_changed_cb(self, combo): fd = self._activity.area.get_font_description() value = self.get_active_text(combo) - fd.set_size(int(value) * pango.SCALE) + fd.set_size(int(value) * Pango.SCALE) self._activity.area.set_font_description(fd) def __font_changed_cb(self, combo): @@ -661,12 +647,12 @@ class TextToolbar(gtk.Toolbar): ##Make the Images Toolbar -class ImageToolbar(gtk.Toolbar): +class ImageToolbar(Gtk.Toolbar): _EFFECT_RAINBOW_NAME = 'rainbow' def __init__(self, activity): - gtk.Toolbar.__init__(self) + GObject.GObject.__init__(self) self._activity = activity self.properties = self._activity.area.tool @@ -674,7 +660,7 @@ class ImageToolbar(gtk.Toolbar): self.insert(self._object_insert, -1) self._object_insert.set_tooltip(_('Insert Image')) - separator = gtk.SeparatorToolItem() + separator = Gtk.SeparatorToolItem() separator.set_draw(True) self.insert(separator, -1) @@ -708,7 +694,7 @@ class ImageToolbar(gtk.Toolbar): self.height_spinButton = self._create_spinButton(self._object_height, 'object-height', activity) - item = gtk.ToolItem() + item = Gtk.ToolItem() item.add(self.height_spinButton) self.insert(item, -1) @@ -719,11 +705,11 @@ class ImageToolbar(gtk.Toolbar): self.width_spinButton = self._create_spinButton(self._object_width, 'object-width', activity) - item = gtk.ToolItem() + item = Gtk.ToolItem() item.add(self.width_spinButton) self.insert(item, -1) - separator = gtk.SeparatorToolItem() + separator = Gtk.SeparatorToolItem() separator.set_draw(True) self.insert(separator, -1) @@ -786,7 +772,7 @@ class ImageToolbar(gtk.Toolbar): def _create_spinButton(self, widget, tool, activity): """Set palette for a tool - width or height - @param self -- gtk.Toolbar + @param self -- Gtk.Toolbar @param widget - the widget which Palette will be set, a ToolButton object @param tool @@ -794,13 +780,13 @@ class ImageToolbar(gtk.Toolbar): """ logging.debug('setting a spinButton for %s', tool) - spin = gtk.SpinButton() + spin = Gtk.SpinButton() spin.show() # This is where we set restrictions for Resizing: # Initial value, minimum value, maximum value, step initial = float(100) - adj = gtk.Adjustment(initial, 10.0, 500.0, 1.0) + adj = Gtk.Adjustment(initial, 10.0, 500.0, 1.0) spin.set_adjustment(adj) spin.set_numeric(True) @@ -813,15 +799,15 @@ class ImageToolbar(gtk.Toolbar): def insertImage(self, widget, activity): try: chooser = ObjectChooser(_('Choose image'), - self._activity, gtk.DIALOG_MODAL | - gtk.DIALOG_DESTROY_WITH_PARENT, what_filter='Image') + self._activity, Gtk.DialogFlags.MODAL | + Gtk.DialogFlags.DESTROY_WITH_PARENT, what_filter='Image') except: chooser = ObjectChooser(_('Choose image'), - self._activity, gtk.DIALOG_MODAL | - gtk.DIALOG_DESTROY_WITH_PARENT) + self._activity, Gtk.DialogFlags.MODAL | + Gtk.DialogFlags.DESTROY_WITH_PARENT) try: result = chooser.run() - if result == gtk.RESPONSE_ACCEPT: + if result == Gtk.ResponseType.ACCEPT: logging.debug('ObjectChooser: %r', chooser.get_selected_object()) jobject = chooser.get_selected_object() @@ -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) |