拥有与 Gtk.TreeView 中默认拖动图标相同颜色和字体设置的拖动图标

Own drag icon with same color and font settings as the default drag icon in a Gtk.TreeView

Gtk.TreeView实现了默认的拖动图标。它使用 TreeView 的背景颜色、字体和完整的行内容作为字符串。

我想要相同的(背景颜色、字体、字体大小、字体颜色) 字符串更短(只有三列中的第二列)。

在下面的示例中创建我自己的 cairo.Surface 来创建这样一个图标。但是颜色和字体是个问题。我不知道如何设置它们或(更重要的是)向 TreeView 或 Gtk 本身询问当前颜色和字体值。

TreeView 如何获取这些值?

#!/usr/bin/env python3
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import Gdk
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]

        # dummy surface/context
        surface = cairo.ImageSurface(cairo.Format.RGB24, 0, 0)
        cr = cairo.Context(surface)

        # calculate text size
        txtext = cr.text_extents(text)
        width = int(txtext.width)
        height = int(txtext.height)
        offset = 10

        # creal surface/context
        surface = cairo.ImageSurface(cairo.Format.RGB24,
                                     width + (offset*2),
                                     height + (offset*2))
        cr = cairo.Context(surface)
        cr.set_source_rgb(1, 1, 1) # text color: white
        cr.move_to(0+offset, height+offset)
        cr.show_text(text)

        # use the surface as drag icon
        Gtk.drag_set_icon_surface(context, surface)

win = MainWindow()
win.show_all()
Gtk.main()

我尝试(但没有奏效)的是 cairo.Surface.create_similar()',cairo.Surface.create_similar_image()andGtk.TreeView.create_row_drag_icon()`。

此答案基于 foreign mailing list posting

widget 有一个 Gtk.StyleContext. A Pango.Layout 用于根据 Gtk.StyleContext 中的样式信息呈现文本。

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_rec, log_rect = pl.get_pixel_extents()
    padding = 5 

    # create surface/context
    surface = cairo.ImageSurface(cairo.Format.RGB24,
                                 log_rect.width + (padding*2),
                                 log_rect.height + (padding*2))
    cr = cairo.Context(surface)
    Gtk.render_background(stylecontext, cr, 0, 0,
                          log_rect.width + (padding*2),
                          log_rect.height + (padding*2))
    Gtk.render_layout(stylecontext, cr, padding, padding, pl)

    # border
    line_width = cr.get_line_width()
    cr.rectangle(-1+line_width, -1+line_width,
                 log_rect.width+(padding*2)-line_width,
                 log_rect.height+(padding*2)-line_width)
    cr.stroke()

    # use the surface as drag icon
    Gtk.drag_set_icon_surface(context, surface)