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

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Gtk+ - Apps Dev mailing list
I have a stackoverflow question with example code open with this:
https://stackoverflow.com/q/52339893/4865723

The Gtk.TreeView implements a default drag icon. It use the background
color of the TreeView, it's font and the complete row-content as string.

I want the same (background-color, font-face, font-size, font-color)
but with a shorter string (only the second of three columns).

How does the TreeView get this values?
_______________________________________________
gtk-app-devel-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Reply | Threaded
Open this post in threaded view
|

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

Gtk+ - Apps Dev mailing list
Op 15-09-18 om 00:24 schreef c.buhtz--- via gtk-app-devel-list:
> I want the same (background-color, font-face, font-size, font-color)
> but with a shorter string (only the second of three columns).

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().

~infirit
_______________________________________________
gtk-app-devel-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Reply | Threaded
Open this post in threaded view
|

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

Gtk+ - Apps Dev mailing list
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()
_______________________________________________
gtk-app-devel-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list