如何防止 Gtk.SourceView 获取文件的 URI

如何防止 Gtk.SourceView 获取文件的 URI

我正在 Vala 中使用 GTK3 制作一个文本编辑器。我需要拖动一个文件,当我将其放入应用程序窗口时,应用程序会打开该文件。我正在使用Gtk.Notebook包含 的Gtk.SourceView。当我将文件放入空的 时,它可以工作,但是当文件上Gtk.Notebook至少附加了一个 时,会抓取文件的 URI,显示它,窗口无法处理文件的打开。Gtk.SourceViewSourceView

Gtk.SourceView在这种情况下,当我将文件放入时,我该怎么做才能防止抓取文件的 URI Gtk.SourceView

附言:drag_dest_unset()我尝试在派生类中使用SourceView。成功了,SourceView没有获取 URI,窗口可以打开文件,但应用程序在运行时显示了如下消息:

Gtk-WARNING **: Can't set a target list on a widget until you've called gtk_drag_dest_set() to make the widget into a drag destination

答案1

为了停止Gtk.SourceView抓取 uri,我使用了以下代码片段:

public class MySourceView: Gtk.SourceView {
    public MySourceView() {
        Gtk.TargetEntry[] targets;
        targets = new Gtk.TargetEntry[1];
        targets[0].target = "text/uri-list";
        targets[0].flags = 0;
        targets[0].info = 0;

        Gtk.drag_dest_set(this, Gtk.DestDefaults.ALL, targets, Gdk.DragAction.COPY);
        this.drag_data_received.connect(this.drag_data_received_cb);
    }

    private void drag_data_received_cb(Gtk.Widget sender,
                    Gdk.DragContext drag_context,
                    int x, int y,
                    Gtk.SelectionData data,
                    uint info, uint time) {
        print("Drag data received!\n");
        // Do something

        Gtk.drag_finish (drag_context, true, false, time);
    }
}

这样,我放入源视图的文件的 uri 就不会粘贴在其中,并且我可以在另一个选项卡中打开该文件。

答案2

这听起来就像您试图将文件拖入 SourceView 小部件,并希望打开一个新选项卡,而不是在源视图小部件中打开该文件。

您需要做的不是禁用拖动事件,而是重定向它。事件将进入 sourceview 小部件,然后您连接到它并将其传递给处理打开新选项卡的代码(在您的窗口中?)。

这有意义吗?您需要代码示例吗?

相关内容