def do_realize(self): self.set_flags(self.flags() | gtk.REALIZED) self.window = gdk.Window( self.get_parent_window(), width=self.allocation.width, height=self.allocation.height, window_type=gdk.WINDOW_CHILD, wclass=gdk.INPUT_OUTPUT, event_mask=self.get_events() | gdk.EXPOSURE_MASK) colormap = gtk.gdk.colormap_get_system() self.black = colormap.alloc_color(0, 0, 0) self.green = colormap.alloc_color(0, 65535, 0) self.orange = colormap.alloc_color(65535, 32768, 0) self.red = colormap.alloc_color(65535, 0, 0) self.yellow = colormap.alloc_color(65535, 65535, 0) self.black_gc = gdk.GC(self.window, foreground=self.black) self.green_gc = gdk.GC(self.window, foreground=self.green) self.orange_gc = gdk.GC(self.window, foreground=self.orange) self.red_gc = gdk.GC(self.window, foreground=self.red) self.yellow_gc = gdk.GC(self.window, foreground=self.yellow) self.window.set_user_data(self) self.style.attach(self.window) self.style.set_background(self.window, gtk.STATE_NORMAL)
def do_realize(self): gtk.Layout.do_realize(self) # Use the same gdk.window (from gtk.Layout) to capture these events. self.window.set_events(self.get_events() | gdk.BUTTON_PRESS_MASK | gdk.BUTTON_RELEASE_MASK | gdk.KEY_PRESS_MASK | gdk.KEY_RELEASE_MASK | gdk.ENTER_NOTIFY_MASK | gdk.LEAVE_NOTIFY_MASK | gdk.POINTER_MOTION_MASK) self.modify_bg(gtk.STATE_NORMAL, gdk.color_parse('white')) gc = gdk.GC(self.window, line_style=gdk.LINE_ON_OFF_DASH, line_width=2) gc.set_rgb_fg_color(gdk.color_parse('blue')) self._selection_gc = gc gc = gdk.GC(self.window) gc.set_rgb_fg_color(gdk.color_parse('grey80')) self._grid_gc = gc gc = gdk.GC(self.window) gc.set_rgb_fg_color(gdk.color_parse('black')) self._border_gc = gc gc = gdk.GC(self.window) gc.set_rgb_fg_color(gdk.color_parse('grey40')) self._field_border_gc = gc
def do_button_press_event(self, event): light_gc = self.style.light_gc[gtk.STATE_NORMAL] dark_gc = self.style.dark_gc[gtk.STATE_NORMAL] w, h = event.window.get_size() # # These lines make the Placeholder looks like a button event.window.draw_line(light_gc, 0, 0, w - 1, 0) event.window.draw_line(light_gc, 0, 0, 0, h - 1) event.window.draw_line(dark_gc, 0, h - 1, w - 1, h - 1) event.window.draw_line(dark_gc, w - 1, 0, w - 1, h - 1) x, y, w, h = self.allocation x, y, width, height = self.allocation border = 5 gc = gdk.GC(self.window, line_width=3) window = self.window window.draw_rectangle(gc, True, x, y, SELECTION_NODE_SIZE, SELECTION_NODE_SIZE) window.draw_rectangle(gc, True, x, y + height - SELECTION_NODE_SIZE, SELECTION_NODE_SIZE, SELECTION_NODE_SIZE) window.draw_rectangle(gc, True, x + width - SELECTION_NODE_SIZE, y, SELECTION_NODE_SIZE, SELECTION_NODE_SIZE) window.draw_rectangle(gc, True, x + width - SELECTION_NODE_SIZE, y + height - SELECTION_NODE_SIZE, SELECTION_NODE_SIZE, SELECTION_NODE_SIZE) window.draw_rectangle(gc, False, x, y, width - 1, height - 1)
def _create_shape(self, width, height): black = gdk.Color(red=0, green=0, blue=0, pixel=1) white = gdk.Color(red=255, green=255, blue=255, pixel=0) pm = gdk.Pixmap(self.window, width, height, 1) gc = gdk.GC(pm) gc.set_background(white) gc.set_foreground(white) pm.draw_rectangle(gc, True, 0, 0, width, height) gc.set_foreground(black) pm.draw_rectangle(gc, False, 0, 0, width - 1, height - 1) pm.draw_rectangle(gc, False, 1, 1, width - 3, height - 3) self.shape_combine_mask(pm, 0, 0)
def do_expose_event(self, event): window = event.window gc = gdk.GC(window) gc.set_rgb_fg_color(self.style.text[self.state]) self.propagate_expose(self._evbox, event) for c in self._children: window.draw_rectangle(self.style.base_gc[self.state], True, c.decoration_x, c.decoration_y, c.decoration_width, c.decoration_height) window.draw_rectangle(gc, False, c.decoration_x, c.decoration_y, c.decoration_width, c.decoration_height) self.propagate_expose(c, event)
def do_realize(self): self.set_flags(self.flags() | gtk.REALIZED) self.window = gdk.Window(self.get_parent_window(), width=self.allocation.width, height=self.allocation.height, window_type=gdk.WINDOW_CHILD, wclass=gdk.INPUT_OUTPUT, event_mask=self.get_events() | gdk.EXPOSURE_MASK) if not hasattr(self.window, "cairo_create"): self.draw_gc = gdk.GC(self.window, line_width=5, line_style=gdk.SOLID, join_style=gdk.JOIN_ROUND) self.window.set_user_data(self) self.style.attach(self.window) self.style.set_background(self.window, gtk.STATE_NORMAL) self.window.move_resize(*self.allocation)
def draw_nodes(self, x, y, width, height): gc = self._draw_gc window = self._selected.window gc = gdk.GC(window, line_width=self._selection_width) if width > SELECTION_NODE_SIZE and height > SELECTION_NODE_SIZE: window.draw_rectangle(gc, True, x, y, SELECTION_NODE_SIZE, SELECTION_NODE_SIZE) window.draw_rectangle(gc, True, x, y + height - SELECTION_NODE_SIZE, SELECTION_NODE_SIZE, SELECTION_NODE_SIZE) window.draw_rectangle(gc, True, x + width - SELECTION_NODE_SIZE, y, SELECTION_NODE_SIZE, SELECTION_NODE_SIZE) window.draw_rectangle(gc, True, x + width - SELECTION_NODE_SIZE, y + height - SELECTION_NODE_SIZE, SELECTION_NODE_SIZE, SELECTION_NODE_SIZE) window.draw_rectangle(gc, False, x, y, width - 1, height - 1)
def do_realize(self): assert not (self.flags() & gtk.NO_WINDOW) self.set_flags(self.flags() | gtk.REALIZED) self.window = gdk.Window(self.get_parent_window(), width=self.allocation.width, height=self.allocation.height, window_type=gdk.WINDOW_CHILD, wclass=gdk.INPUT_OUTPUT, event_mask=(self.get_events() | gdk.EXPOSURE_MASK | gdk.BUTTON_PRESS_MASK)) self.window.set_user_data(self) self.style.attach(self.window) self.style.set_background(self.window, gtk.STATE_NORMAL) self._draw_gc = gdk.GC(self.window, line_width=self._selection_width, line_style=gdk.SOLID, foreground=self.style.bg[gtk.STATE_SELECTED])
def set_background_color(self, r, g, b): """ Set background color. @type r: int @param r: red @type g: int @param g: green @type b: int @param b: blue """ self._background_color = (r, g, b) if self._background_gc: # This part can only be called after the widget is visible color = gdk.Color(red=r, green=g, blue=b) self._background_gc = gdk.GC(self.window) self._gc_set_foreground(self._background_gc, color) self.refresh(force_draw=True)
def _init_gc(self): if not self._handwriting_line_gc: color = gdk.Color(red=0x0000, blue=0x0000, green=0x0000) self._handwriting_line_gc = gdk.GC(self.window) self._gc_set_foreground(self._handwriting_line_gc, color) self._handwriting_line_gc.set_line_attributes( 4, gdk.LINE_SOLID, gdk.CAP_ROUND, gdk.JOIN_ROUND) if not self._stroke_gc: color = gdk.Color(red=0xff00, blue=0x0000, green=0x0000) self._stroke_gc = gdk.GC(self.window) self._gc_set_foreground(self._stroke_gc, color) self._stroke_gc.set_line_attributes(4, gdk.LINE_SOLID, gdk.CAP_ROUND, gdk.JOIN_ROUND) if not self._background_writing_gc: color = gdk.Color(red=0xcccc, blue=0xcccc, green=0xcccc) self._background_writing_gc = gdk.GC(self.window) self._gc_set_foreground(self._background_writing_gc, color) self._background_writing_gc.set_line_attributes( 4, gdk.LINE_SOLID, gdk.CAP_ROUND, gdk.JOIN_ROUND) if not self._annotation_gc: color = gdk.Color(red=0x8000, blue=0x0000, green=0x0000) self._annotation_gc = gdk.GC(self.window) self._gc_set_foreground(self._annotation_gc, color) if not self._axis_gc: color = gdk.Color(red=0x8000, blue=0x8000, green=0x8000) self._axis_gc = gdk.GC(self.window) self._gc_set_foreground(self._axis_gc, color) self._axis_gc.set_line_attributes(1, gdk.LINE_ON_OFF_DASH, gdk.CAP_BUTT, gdk.JOIN_ROUND) if not self._background_gc: color = gdk.Color(*self._background_color) self._background_gc = gdk.GC(self.window) self._gc_set_foreground(self._background_gc, color)
def do_realize(self): """ Realize the widget This creates the server resources (gdk window, cursors, pixbufs, etc) and sets up drag and drop handlers """ self.set_flags(self.flags() | gtk.REALIZED) self.window = gdk.Window(self.get_parent_window(), width=self.allocation.width, height=self.allocation.height, window_type=gdk.WINDOW_CHILD, wclass=gdk.INPUT_OUTPUT, event_mask=self.get_events() | gdk.EXPOSURE_MASK | gdk.BUTTON1_MOTION_MASK | gdk.BUTTON_PRESS_MASK | gdk.BUTTON_RELEASE_MASK | gdk.POINTER_MOTION_MASK | gdk.POINTER_MOTION_HINT_MASK) try: pbuf = gdk.pixbuf_new_from_file( os.path.join(self.icon_path, "magnify.png")) self.cursors['magnify'] = gdk.Cursor(self.window.get_display(), pbuf, 6, 6) except glib.GError: self.cursors['magnify'] = None try: pbuf = gdk.pixbuf_new_from_file( os.path.join(self.icon_path, "measure.png")) self.cursors['measure'] = gdk.Cursor(self.window.get_display(), pbuf, 6, 6) except glib.GError: self.cursors['measure'] = None #self.set_cursor('magnify') self.pixbuf_width = int(self.allocation.width) self.pixbuf_height = int(self.allocation.height) self.raw_width = int(self.allocation.width / self.zoom) self.raw_height = int(self.allocation.height / self.zoom) self.window.set_user_data(self) self.style.attach(self.window) self.style.set_background(self.window, gtk.STATE_NORMAL) self.window.move_resize(*self.allocation) self.gc = self.style.fg_gc[gtk.STATE_NORMAL] self.measure_gc = gdk.GC(self.window) self.measure_gc.set_foreground( self.measure_gc.get_colormap().alloc_color("#fff")) self.measure_gc.set_background( self.measure_gc.get_colormap().alloc_color("#000")) self.measure_gc.set_dashes(0, (4, 4)) self.measure_gc.set_line_attributes(1, gdk.LINE_DOUBLE_DASH, gdk.CAP_BUTT, gdk.JOIN_MITER) self.grid_gc = gdk.GC(self.window) self.grid_gc.set_foreground( self.measure_gc.get_colormap().alloc_color("#777")) self.connect("motion-notify-event", self.cb_motion_notify) self.connect("button-press-event", self.cb_button_press) self.connect("button-release-event", self.cb_button_release) self.connect("scroll-event", self.cb_scroll) self.pixbuf = gdk.Pixbuf(gdk.COLORSPACE_RGB, False, 8, self.pixbuf_width, self.pixbuf_height) self.raw_pixbuf = gdk.Pixbuf(gdk.COLORSPACE_RGB, False, 8, self.raw_width, self.raw_height) target_list = gtk.target_list_add_image_targets( None, self.TARGET_TYPE_IMAGE, True) self.drag_source_set(gtk.gdk.BUTTON1_MASK, target_list, gtk.gdk.ACTION_COPY) self.connect("drag-data-get", self.cb_drag_data_get)
def do_button_press_event(self, event): gc = gdk.GC(self.window, line_width=5, line_style = gtk.gdk.SOLID, foreground=self.style.bg[gtk.STATE_SELECTED]) x,y,h,w = self.allocation self.window.draw_rectangle(gc, True, x,y, w, h)
def osd(text, bgcolor, fgcolor, fontdesc, use_markup=False, alignment=pango.ALIGN_CENTER, fake_translucent_bg=False, drop_shadow=False, max_width=None, debug_frame=False, ellipsize=pango.ELLIPSIZE_NONE): assert isinstance(fontdesc, pango.FontDescription) win = gtk.Window(gtk.WINDOW_POPUP) win.add_events(gtk.gdk.ENTER_NOTIFY_MASK) darea = gtk.DrawingArea() win.add(darea) darea.show() if use_markup: layout = win.create_pango_layout('') layout.set_markup(text) else: layout = win.create_pango_layout(text) try: layout.set_ellipsize(ellipsize) except AttributeError: print "ellipsize attribute not supported, ignored" layout.set_justify(False) layout.set_alignment(alignment) layout.set_font_description(fontdesc) if max_width is None: MAX_WIDTH = gdk.screen_width() - 8 else: MAX_WIDTH = max_width - 8 layout.set_width(pango.SCALE * MAX_WIDTH) if ellipsize != pango.ELLIPSIZE_NONE: layout.set_wrap(pango.WRAP_WORD) width, height = layout.get_pixel_size() off_x = BORDER_WIDTH * 2 off_y = BORDER_WIDTH * 2 if alignment == pango.ALIGN_CENTER: off_x -= MAX_WIDTH / 2 - width / 2 elif alignment == pango.ALIGN_RIGHT: off_x -= MAX_WIDTH - width width += BORDER_WIDTH * 4 height += BORDER_WIDTH * 4 if drop_shadow: drop_shadow_distance = max( 2, int(fontdesc.get_size() / pango.SCALE * 0.1)) width += drop_shadow_distance height += drop_shadow_distance darea.set_size_request(width, height) darea.realize() pixmap = gtk.gdk.Pixmap(darea.window, width, height) pixmap.set_colormap(darea.window.get_colormap()) fg_gc = gdk.GC(pixmap) fg_gc.copy(darea.style.fg_gc[gtk.STATE_NORMAL]) bg_gc = gdk.GC(pixmap) bg_gc.copy(darea.style.fg_gc[gtk.STATE_NORMAL]) fg_gc.set_foreground(darea.get_colormap().alloc_color(fgcolor)) bg_gc.set_background(darea.get_colormap().alloc_color(bgcolor)) pixmap.draw_rectangle(bg_gc, True, 0, 0, width, height) pixmap.draw_layout(fg_gc, off_x, off_y, layout) if debug_frame: pixmap.draw_rectangle(fg_gc, False, 0, 0, width - 1, height - 1) bitmap = gtk.gdk.Pixmap(darea.window, width, height, 1) bitmap.set_colormap(darea.window.get_colormap()) fg_gc = gdk.GC(bitmap) bg_gc = gdk.GC(bitmap) fg_gc.set_foreground(gdk.Color(pixel=-1)) bg_gc.set_background(gdk.Color(pixel=0)) if fake_translucent_bg: w, h = 2, 2 stipple = gtk.gdk.Pixmap(None, w, h, 1) stipple.draw_rectangle(bg_gc, True, 0, 0, w, h) stipple.draw_point(fg_gc, 0, 0) stipple.draw_point(fg_gc, 1, 1) fg_gc.set_stipple(stipple) bitmap.draw_rectangle(bg_gc, True, 0, 0, width, height) fg_gc.set_fill(gtk.gdk.STIPPLED) bitmap.draw_rectangle(fg_gc, True, 0, 0, width, height) fg_gc.set_fill(gtk.gdk.SOLID) # draw corners corner_size = int(fontdesc.get_size() / pango.SCALE * 0.5) fg_gc.set_function(gtk.gdk.AND) gc = gdk.GC(bitmap) corner = gtk.gdk.Pixmap(bitmap, corner_size, corner_size) def draw_corner(angle1, x, y, arc_x0, arc_y0): gc.set_foreground(gdk.Color(pixel=0)) corner.draw_rectangle(gc, True, 0, 0, corner_size, corner_size) gc.set_foreground(gdk.Color(pixel=1)) corner.draw_arc(gc, True, arc_x0, arc_y0, corner_size * 2, corner_size * 2, angle1 * 64, 90 * 64) bitmap.draw_drawable(fg_gc, corner, 0, 0, x, y, corner_size, corner_size) # top-left draw_corner(90, 0, 0, 0, 0) # bottom-left draw_corner(180, 0, height - corner_size, 0, -corner_size) # bottom-right draw_corner(270, width - corner_size, height - corner_size, -corner_size, -corner_size) # top-right draw_corner(0, width - corner_size, 0, -corner_size, 0) fg_gc.set_function(gtk.gdk.SET) else: bitmap.draw_rectangle(bg_gc, True, 0, 0, width, height) bitmap.draw_layout(fg_gc, off_x, off_y, layout) bitmap.draw_layout(fg_gc, off_x + BORDER_WIDTH, off_y, layout) bitmap.draw_layout(fg_gc, off_x + BORDER_WIDTH, off_y + BORDER_WIDTH, layout) bitmap.draw_layout(fg_gc, off_x, off_y + BORDER_WIDTH, layout) bitmap.draw_layout(fg_gc, off_x - BORDER_WIDTH, off_y + BORDER_WIDTH, layout) bitmap.draw_layout(fg_gc, off_x - BORDER_WIDTH, off_y, layout) bitmap.draw_layout(fg_gc, off_x - BORDER_WIDTH, off_y - BORDER_WIDTH, layout) bitmap.draw_layout(fg_gc, off_x, off_y - BORDER_WIDTH, layout) bitmap.draw_layout(fg_gc, off_x + BORDER_WIDTH, off_y - BORDER_WIDTH, layout) if drop_shadow: bitmap.draw_layout(fg_gc, off_x + drop_shadow_distance, off_y + drop_shadow_distance, layout) if debug_frame: bitmap.draw_rectangle(fg_gc, False, 0, 0, width - 1, height - 1) darea.window.set_back_pixmap(pixmap, False) win.window.shape_combine_mask(bitmap, 0, 0) win.width = width win.height = height # Set the overlay mode of the window pixmap = gdk.Pixmap(None, width, height, 1) win.input_shape_combine_mask(pixmap, 0, 0) return win