main.rs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. use gtk::{
  2. Application,
  3. ApplicationWindow,
  4. Button,
  5. GtkWindowExt,
  6. WidgetExt,
  7. ContainerExt,
  8. ButtonExt,
  9. BoxExt,
  10. };
  11. use gio::prelude::{
  12. ApplicationExt,
  13. ApplicationExtManual,
  14. };
  15. use std::rc::Rc;
  16. use std::cell::RefCell;
  17. struct Legend {
  18. symbol: char,
  19. color: (f64, f64, f64),
  20. }
  21. impl Legend {
  22. fn draw_to_file(&self) {
  23. let f = std::fs::File::create("samp.svg").unwrap();
  24. let surf = cairo::SvgSurface::for_stream(
  25. 100.0,
  26. 100.0,
  27. f,
  28. ).unwrap();
  29. self.render(&cairo::Context::new(&surf));
  30. surf.finish_output_stream().unwrap();
  31. }
  32. fn render(&self, ctx: &cairo::Context) -> () {
  33. ctx.set_source_rgb(self.color.0, self.color.1, self.color.2);
  34. ctx.paint();
  35. ctx.set_source_rgb(0., 0., 0.);
  36. ctx.move_to(0.0, -23.0);
  37. let layout = pangocairo::functions::create_layout(&ctx).unwrap();
  38. layout.set_width(100 * 1024);
  39. layout.set_alignment(pango::Alignment::Center);
  40. let font = pango::FontDescription::from_string("Fira Sans 92");
  41. layout.set_font_description(Some(&font));
  42. layout.set_text(&self.symbol.to_string());
  43. pangocairo::functions::show_layout(&ctx, &layout);
  44. }
  45. }
  46. struct AppData {
  47. legend: Legend,
  48. }
  49. #[derive(Clone)]
  50. struct App {
  51. data: Rc<RefCell<AppData>>,
  52. }
  53. impl App {
  54. fn new() -> App {
  55. let legend = Legend {
  56. symbol: 'X',
  57. color: (0.5, 0.5, 0.5),
  58. };
  59. let data = Rc::new(RefCell::new(AppData { legend }));
  60. App { data }
  61. }
  62. }
  63. fn mk_app(app: &gtk::Application) {
  64. let window = ApplicationWindow::new(app);
  65. let app = App::new();
  66. window.set_title("I Am Legend");
  67. let container = gtk::Box::new(gtk::Orientation::Vertical, 4);
  68. container.pack_start(&mk_icon_choice('A', app.clone()), false, true, 0);
  69. container.pack_start(&mk_icon_choice('B', app.clone()), false, true, 0);
  70. container.pack_start(&mk_icon_choice('C', app.clone()), false, true, 0);
  71. container.pack_start(&mk_icon_choice('D', app.clone()), false, true, 0);
  72. let flow = gtk::Box::new(gtk::Orientation::Horizontal, 2);
  73. flow.pack_start(&container, false, true, 0);
  74. let button = Button::with_label("Click me!");
  75. flow.pack_start(&button, false, true, 0);
  76. let canvas = gtk::DrawingArea::new();
  77. let copy = app.clone();
  78. canvas.connect_draw(move |cv, ctx| {
  79. copy.data.borrow_mut().legend.render(ctx);
  80. gtk::Inhibit(false)
  81. });
  82. flow.pack_start(&canvas, true, true, 0);
  83. button.connect_clicked(move |_| {
  84. app.data.borrow_mut().legend.draw_to_file();
  85. });
  86. window.add(&flow);
  87. window.show_all();
  88. }
  89. fn mk_icon_choice(choice: char, cell: App) -> gtk::Button {
  90. let button = gtk::Button::with_label(&choice.to_string());
  91. button.connect_clicked(move |_| {
  92. cell.data.borrow_mut().legend.symbol = choice;
  93. println!("Choosing '{}'", choice);
  94. });
  95. button
  96. }
  97. fn main() {
  98. let application = Application::new(
  99. Some("com.github.gtk-rs.examples.basic"),
  100. Default::default(),
  101. ).expect("failed to initialize GTK application");
  102. application.connect_activate(|app| {
  103. mk_app(app);
  104. });
  105. application.run(&[]);
  106. }