| 1 |
import gi |
| 2 |
|
| 3 |
gi.require_version("Gdk", "4.0") |
| 4 |
gi.require_version("Gtk", "4.0") |
| 5 |
from gi.repository import Gdk, GObject, Gtk |
| 6 |
|
| 7 |
|
| 8 |
class DragDropWindow(Gtk.ApplicationWindow): |
| 9 |
def __init__(self, *args, **kargs): |
| 10 |
super().__init__(*args, **kargs, title="Drag and Drop Example") |
| 11 |
|
| 12 |
self.set_default_size(500, 400) |
| 13 |
|
| 14 |
views_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL) |
| 15 |
views_box.props.vexpand = True |
| 16 |
self.set_child(views_box) |
| 17 |
|
| 18 |
flow_box = Gtk.FlowBox() |
| 19 |
views_box.append(flow_box) |
| 20 |
flow_box.props.selection_mode = Gtk.SelectionMode.NONE |
| 21 |
flow_box.append(SourceFlowBoxChild("Item 1", "image-missing")) |
| 22 |
flow_box.append(SourceFlowBoxChild("Item 2", "help-about")) |
| 23 |
flow_box.append(SourceFlowBoxChild("Item 3", "edit-copy")) |
| 24 |
|
| 25 |
views_box.append(Gtk.Separator()) |
| 26 |
|
| 27 |
self.target_view = TargetView(vexpand=True) |
| 28 |
views_box.append(self.target_view) |
| 29 |
|
| 30 |
|
| 31 |
class SourceFlowBoxChild(Gtk.FlowBoxChild): |
| 32 |
def __init__(self, name, icon_name): |
| 33 |
super().__init__() |
| 34 |
|
| 35 |
self.name = name |
| 36 |
self.icon_name = icon_name |
| 37 |
|
| 38 |
box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6) |
| 39 |
self.set_child(box) |
| 40 |
|
| 41 |
icon = Gtk.Image(icon_name=self.icon_name) |
| 42 |
label = Gtk.Label(label=self.name) |
| 43 |
|
| 44 |
box.append(icon) |
| 45 |
box.append(label) |
| 46 |
|
| 47 |
drag_controller = Gtk.DragSource() |
| 48 |
drag_controller.connect("prepare", self.on_drag_prepare) |
| 49 |
drag_controller.connect("drag-begin", self.on_drag_begin) |
| 50 |
self.add_controller(drag_controller) |
| 51 |
|
| 52 |
def on_drag_prepare(self, _ctrl, _x, _y): |
| 53 |
item = Gdk.ContentProvider.new_for_value(self) |
| 54 |
string = Gdk.ContentProvider.new_for_value(self.name) |
| 55 |
return Gdk.ContentProvider.new_union([item, string]) |
| 56 |
|
| 57 |
def on_drag_begin(self, ctrl, _drag): |
| 58 |
icon = Gtk.WidgetPaintable.new(self) |
| 59 |
ctrl.set_icon(icon, 0, 0) |
| 60 |
|
| 61 |
|
| 62 |
class TargetView(Gtk.Box): |
| 63 |
def __init__(self, **kargs): |
| 64 |
super().__init__(**kargs) |
| 65 |
|
| 66 |
self.stack = Gtk.Stack(hexpand=True) |
| 67 |
self.append(self.stack) |
| 68 |
|
| 69 |
empty_label = Gtk.Label(label="Drag some item, text, or files here.") |
| 70 |
self.stack.add_named(empty_label, "empty") |
| 71 |
self.stack.set_visible_child_name("empty") |
| 72 |
|
| 73 |
box = Gtk.Box( |
| 74 |
orientation=Gtk.Orientation.VERTICAL, |
| 75 |
vexpand=True, |
| 76 |
valign=Gtk.Align.CENTER, |
| 77 |
) |
| 78 |
self.stack.add_named(box, "item") |
| 79 |
|
| 80 |
self.icon = Gtk.Image() |
| 81 |
box.append(self.icon) |
| 82 |
self.label = Gtk.Label() |
| 83 |
box.append(self.label) |
| 84 |
|
| 85 |
self.text = Gtk.Label() |
| 86 |
self.stack.add_named(self.text, "other") |
| 87 |
|
| 88 |
drop_controller = Gtk.DropTarget.new( |
| 89 |
type=GObject.TYPE_NONE, actions=Gdk.DragAction.COPY |
| 90 |
) |
| 91 |
drop_controller.set_gtypes([SourceFlowBoxChild, Gdk.FileList, str]) |
| 92 |
drop_controller.connect("drop", self.on_drop) |
| 93 |
self.add_controller(drop_controller) |
| 94 |
|
| 95 |
def on_drop(self, _ctrl, value, _x, _y): |
| 96 |
if isinstance(value, SourceFlowBoxChild): |
| 97 |
self.label.props.label = value.name |
| 98 |
self.icon.props.icon_name = value.icon_name |
| 99 |
self.stack.set_visible_child_name("item") |
| 100 |
|
| 101 |
elif isinstance(value, Gdk.FileList): |
| 102 |
files = value.get_files() |
| 103 |
names = "" |
| 104 |
for file in files: |
| 105 |
names += f"Loaded file {file.get_basename()}\n" |
| 106 |
self.text.props.label = names |
| 107 |
self.stack.set_visible_child_name("other") |
| 108 |
|
| 109 |
elif isinstance(value, str): |
| 110 |
self.text.props.label = value |
| 111 |
self.stack.set_visible_child_name("other") |
| 112 |
|
| 113 |
|
| 114 |
def on_activate(app): |
| 115 |
win = DragDropWindow(application=app) |
| 116 |
win.present() |
| 117 |
|
| 118 |
|
| 119 |
app = Gtk.Application(application_id="com.example.App") |
| 120 |
app.connect("activate", on_activate) |
| 121 |
|
| 122 |
app.run(None) |