forked from gtk-rs/gtk3-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
71 lines (60 loc) · 2.68 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use gtk::prelude::*;
use gtk::{gdk, gio};
use gtk::{DestDefaults, TargetFlags};
fn main() {
let application = gtk::Application::new(
Some("com.github.gtk-rs.examples.drag_and_drop_textview"),
Default::default(),
);
application.connect_activate(build_ui);
application.run();
}
fn build_ui(application: >k::Application) {
let window = gtk::ApplicationWindow::new(application);
window.set_title("Drag and Drop Example with a TextView");
// Give a nice text description for the user
let label = gtk::Label::new(Some("Drag files and/or folders onto the TextView below."));
// Create scrollable text view as our drag target
let text_view = gtk::TextView::new();
text_view.set_wrap_mode(gtk::WrapMode::Word);
text_view.set_cursor_visible(false);
let scrolled_text_view = gtk::ScrolledWindow::new(gtk::Adjustment::NONE, gtk::Adjustment::NONE);
scrolled_text_view.set_policy(gtk::PolicyType::Automatic, gtk::PolicyType::Automatic);
scrolled_text_view.add(&text_view);
// Configure the text view to accept URI lists from other applications. This allows
// dragging files & folders from a file browser program onto the textview.
let targets = vec![gtk::TargetEntry::new(
"text/uri-list",
TargetFlags::OTHER_APP,
0,
)];
text_view.drag_dest_set(DestDefaults::HIGHLIGHT, &targets, gdk::DragAction::COPY);
// Process any `drag-data-received` events received by the textview. These events include
// the URL list we're looking for.
text_view.connect_drag_data_received(|w, _, _, _, d, _, _| {
// Get the text buffer for the TextView and clear it to make it ready to accept new text.
let buffer = w.buffer().unwrap();
buffer.set_text("");
// Since we only accept `text/uri-list`s here, we don't need to check first, we can simply
// iterate through all of the accepted URIs.
for file in d.uris() {
let file = gio::File::for_uri(&file);
let display_name = if file.is_native() {
file.path().unwrap().display().to_string()
} else {
file.uri().into()
};
let bulleted_file_path = format!(" • {}\n", &display_name);
// We make sure to always insert this at the end of the text buffer so they're in
// order.
buffer.insert_at_cursor(&bulleted_file_path);
}
});
// Pack widgets vertically.
let vbox = gtk::Box::new(gtk::Orientation::Vertical, 0);
vbox.pack_start(&label, false, false, 0);
vbox.pack_start(&scrolled_text_view, true, true, 0);
// Create a new window
window.add(&vbox);
window.show_all();
}