Thursday, May 2, 2024
 Popular · Latest · Hot · Upcoming
2
rated 0 times [  2] [ 0]  / answers: 1 / hits: 561  / 1 Year ago, sun, november 20, 2022, 12:29:46

I'm making a text editor using GTK3 in Vala. I need to drag a file and, when I drop it into the application window, the application opens that file. I'm using a Gtk.Notebook that contains the Gtk.SourceView's. It works when I drop a file into an empty Gtk.Notebook, but when there is at least one Gtk.SourceView appended on it, the SourceView grabs the URI of the file, shows it and the window can't handle the opening of the file.



In this case, what can I do to prevent a Gtk.SourceView from grabbing the URI of a file when I drop a file into a Gtk.SourceView.



PS: I tried to use the drag_dest_unset() inside the SourceView derived class. It worked, the SourceView didn't grab the URI and the window could open the file, but the application showed a message like this, in runtime:



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

More From » gtk

 Answers
7

To make the Gtk.SourceView stop grabbing the uri, I used the following code snippet:



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!
");
// Do something

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


This way, the uri of the file that I drop in the source view is not pasted in it and I can open the file in another tab.


[#41947] Sunday, November 20, 2022, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
herriail

Total Points: 11
Total Questions: 122
Total Answers: 118

Location: Peru
Member since Tue, Mar 16, 2021
3 Years ago
;