Re: Own drag icon with same color and font settings as the default drag icon in a Gtk.TreeView
- From: infirit <infirit gmail com>
- To: gtk-app-devel-list gnome org
- Subject: Re: Own drag icon with same color and font settings as the default drag icon in a Gtk.TreeView
- Date: Sat, 15 Sep 2018 15:37:00 +0200
Op 15-09-18 om 14:13 schreef infirit:
You use the Gtk.render_* range of functions. There is one for for every
type of content you will want to render onto a cairo surface.
If you want to render text put it in a Pango.Layout and use
Gtk.render_layout().
Below is a modified version based of your stackoverflow example.
~infirit
#!/usr/bin/env python3
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
import cairo
class MainWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="TreeView Drag and Drop")
self.connect("delete-event", Gtk.main_quit)
self.box = Gtk.Box()
self.add(self.box)
# "model" with dummy data
self.store = Gtk.TreeStore(int, str, int)
for i in range(5):
self.store.append(None, [i, 'Item {}'.format(i), i]) # treeview
self.tree = Gtk.TreeView(model=self.store)
self.box.pack_start(self.tree, True, True, 0)
# build columns
colA = Gtk.TreeViewColumn('Col A', Gtk.CellRendererText(), text=0)
self.tree.append_column(colA)
colB = Gtk.TreeViewColumn('Col B', Gtk.CellRendererText(), text=1)
self.tree.append_column(colB)
colC = Gtk.TreeViewColumn('Col C', Gtk.CellRendererText(), text=2)
self.tree.append_column(colC)
# enable default drag and drop
self.tree.set_reorderable(True)
# DnD events
self.tree.connect_after("drag-begin", self.drag_begin)
def drag_begin(self, widget, context):
model, path = widget.get_selection().get_selected_rows()
text = model[path][1]
stylecontext = widget.get_style_context()
# new pango layout
pl = widget.create_pango_layout(text)
ink_rect, log_rect = pl.get_pixel_extents()
padding = 10
# creal surface/context
surface = cairo.ImageSurface(cairo.Format.RGB24,
log_rect.width + 2 * padding,
log_rect.height + 2 * padding)
cr = cairo.Context(surface)
Gtk.render_background(stylecontext, cr, 0, 0,
log_rect.width + 2 * padding,
log_rect.height + 2 * padding)
Gtk.render_layout(stylecontext, cr, 10, 10, pl)
Gtk.drag_set_icon_surface(context, surface)
win = MainWindow()
win.show_all()
Gtk.main()
[
Date Prev][
Date Next] [
Thread Prev][Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]