Skip to content

Instantly share code, notes, and snippets.

@barafael
Created May 17, 2017 15:37
Show Gist options
  • Save barafael/2e500f500b0e9ebf7d86bc842fa460c1 to your computer and use it in GitHub Desktop.
Save barafael/2e500f500b0e9ebf7d86bc842fa460c1 to your computer and use it in GitHub Desktop.
//! # Toolbar, Scrollable Text View and File Chooser, Cairo Widget
extern crate gtk;
extern crate cairo;
use std::io::prelude::*;
use std::io::BufReader;
use std::fs::File;
use gtk::DrawingArea;
use cairo::Context;
use gtk::prelude::*;
use gtk::Builder;
pub fn main() {
if gtk::init().is_err() {
println!("Failed to initialize GTK.");
return;
}
let glade_src = include_str!("graph_and_text_viewer.glade");
let builder = Builder::new();
builder.add_from_string(glade_src).unwrap();
let window: gtk::Window = builder.get_object("window").unwrap();
let open_button: gtk::ToolButton = builder.get_object("open_button").unwrap();
let print_button: gtk::ToolButton = builder.get_object("log_to_console").unwrap();
let graph_area: gtk::DrawingArea = builder.get_object("graph_area").unwrap();
let text_view: gtk::TextView = builder.get_object("text_view").unwrap();
// Do I really have to clone to move this? How do I know the data is still linked after cloning?
let window1 = window.clone();
let text_view1 = text_view.clone();
open_button.connect_clicked(move |_| {
// TODO move this to a impl?
let file_chooser = gtk::FileChooserDialog::new(
Some("Open File"), Some(&window1), gtk::FileChooserAction::Open);
file_chooser.add_buttons(&[
("Open", gtk::ResponseType::Ok.into()),
("Cancel", gtk::ResponseType::Cancel.into()),
]);
if file_chooser.run() == gtk::ResponseType::Ok.into() {
let filename = file_chooser.get_filename().unwrap();
let file = File::open(&filename).unwrap();
let mut reader = BufReader::new(file);
let mut contents = String::new();
let _ = reader.read_to_string(&mut contents);
let buffer = text_view1.get_buffer().unwrap();
buffer.set_text(&contents);
}
file_chooser.destroy();
});
print_button.connect_clicked(move |_| {
// Is that really the best way to get the text in the buffer?
let buffer = text_view.get_buffer().unwrap();
let mut end_iter = buffer.get_start_iter();
end_iter.forward_to_end();
let string = buffer.get_text(&buffer.get_start_iter(), &end_iter, false).unwrap();
println!("{}", string);
});
drawable(graph_area, |_, cr| {
let values: Vec<u8> = vec![1, 2, 6, 4, 5, 4, 1, 2, 7];
cr.set_dash(&[3., 2., 1.], 1.);
assert_eq!(cr.get_dash(), (vec![3., 2., 1.], 1.));
cr.scale(255f64, 255f64);
cr.set_source_rgb(250.0/255.0, 224.0/255.0, 55.0/255.0);
cr.paint();
cr.set_line_width(0.01);
cr.set_source_rgb(0.0, 0.0, 0.0);
cr.move_to( 0.0, 0.0 );
let mut initial = 0.1;
for y in &values {
cr.line_to(initial, (*y as f64)/10.0);
initial += 0.1;
}
cr.stroke();
Inhibit(false)
});
window.connect_delete_event(|_, _| {
gtk::main_quit();
Inhibit(false)
});
window.show_all();
gtk::main();
}
pub fn drawable<F>(drawing_area: DrawingArea, draw_fn: F)
where F: Fn(&DrawingArea, &Context) -> Inhibit + 'static {
drawing_area.connect_draw(draw_fn);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment