2017-07-04 19:15:11 +00:00
|
|
|
use errors::*;
|
|
|
|
use gdk_pixbuf;
|
|
|
|
use gdk_pixbuf_sys;
|
|
|
|
use glib::translate::FromGlibPtrFull;
|
|
|
|
use glib::translate::ToGlibPtr;
|
|
|
|
use gtk::prelude::*;
|
|
|
|
use gtk;
|
|
|
|
use std::path::*;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn copy_pixbuf(pixbuf: &gdk_pixbuf::Pixbuf) -> gdk_pixbuf::Pixbuf {
|
|
|
|
|
|
|
|
let new_pixbuf = unsafe {
|
|
|
|
let gdk_pixbuf = pixbuf.to_glib_full();
|
|
|
|
let copy = gdk_pixbuf_sys::gdk_pixbuf_copy(gdk_pixbuf);
|
|
|
|
FromGlibPtrFull::from_glib_full(copy)
|
|
|
|
};
|
|
|
|
|
|
|
|
return new_pixbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-07 20:10:45 +00:00
|
|
|
pub fn pixbuf_new_from_theme(icon_name: &str,
|
|
|
|
size: i32,
|
|
|
|
theme: >k::IconTheme)
|
|
|
|
-> Result<gdk_pixbuf::Pixbuf> {
|
|
|
|
|
|
|
|
let icon_info =
|
|
|
|
theme.lookup_icon(icon_name, size, gtk::IconLookupFlags::empty())
|
|
|
|
.ok_or(format!("Couldn't find icon {}", icon_name))?;
|
|
|
|
|
|
|
|
debug!("Loading stock icon {} from {:?}",
|
|
|
|
icon_name,
|
|
|
|
icon_info.get_filename().unwrap_or(PathBuf::new()));
|
2017-07-04 19:15:11 +00:00
|
|
|
|
|
|
|
// TODO: propagate error
|
|
|
|
let pixbuf = icon_info.load_icon().unwrap();
|
|
|
|
|
|
|
|
return Ok(pixbuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn pixbuf_new_from_file(filename: &str) -> Result<gdk_pixbuf::Pixbuf> {
|
|
|
|
ensure!(!filename.is_empty(), "Filename is empty");
|
2017-07-08 22:14:49 +00:00
|
|
|
let mut syspath = String::new();
|
|
|
|
let sysdir = option_env!("PIXMAPSDIR").map(|s|{
|
|
|
|
syspath = format!("{}/{}", s, filename);
|
|
|
|
Path::new(syspath.as_str())
|
|
|
|
});
|
|
|
|
let cargopath = format!("./data/pixmaps/{}",
|
|
|
|
filename);
|
|
|
|
let cargodir = Path::new(cargopath.as_str());
|
|
|
|
|
|
|
|
// prefer local dir
|
|
|
|
let final_dir = {
|
|
|
|
if cargodir.exists() {
|
|
|
|
cargodir
|
|
|
|
} else if sysdir.is_some() && sysdir.unwrap().exists() {
|
|
|
|
sysdir.unwrap()
|
|
|
|
} else {
|
|
|
|
bail!("No valid path found")
|
|
|
|
}
|
|
|
|
};
|
2017-07-04 19:15:11 +00:00
|
|
|
|
2017-07-08 22:14:49 +00:00
|
|
|
let str_path = final_dir.to_str().ok_or("Path is not valid unicode")?;
|
|
|
|
debug!("Loading icon from {}", str_path);
|
|
|
|
// TODO: propagate error
|
|
|
|
return Ok(gdk_pixbuf::Pixbuf::new_from_file(str_path).unwrap());
|
2017-07-04 19:15:11 +00:00
|
|
|
}
|