// -*- coding: utf-8 -*- // // Copyright 2021 Michael Büsch // // Licensed under the Apache License version 2.0 // or the MIT license, at your option. // SPDX-License-Identifier: Apache-2.0 OR MIT // mod drawing_area; use drawing_area::DrawingArea; use crate::gsignal_connect_to; use crate::gtk_helpers::*; use crate::print::Print; use anyhow as ah; use expect_exit::exit_unwind; use std::cell::RefCell; use std::rc::Rc; pub struct MainWindow { appwindow: gtk::ApplicationWindow, #[allow(dead_code)] drawing_area: Rc>, } impl MainWindow { pub fn new(app: >k::Application, map_name: &str) -> ah::Result>> { let glade_source = include_str!("main_window.glade"); let builder = gtk::Builder::from_string(glade_source); let appwindow: gtk::ApplicationWindow = builder.object("main_window").unwrap(); appwindow.set_application(Some(app)); appwindow.set_title("Voxel-engine"); let drawing_area = DrawingArea::new(builder.object("drawing_area").unwrap(), map_name)?; let drawing_area2 = Rc::clone(&drawing_area); let mainwnd = Rc::new(RefCell::new(MainWindow { appwindow, drawing_area, })); let mainwnd2 = Rc::clone(&mainwnd); builder.connect_signals(move |_builder, handler_name| { let mainwnd2 = Rc::clone(&mainwnd2); if let Some(h) = DrawingArea::connect_signals(Rc::clone(&drawing_area2), handler_name) { return h; } match handler_name { "handler_quit" => gsignal_connect_to!(mainwnd2, gsignal_quit, None), name => { Print::error(&format!("Unhandled signal: {}", name)); Box::new(|_| None) } } }); Ok(mainwnd) } pub fn main_window(&self) -> gtk::ApplicationWindow { self.appwindow.clone() } fn gsignal_quit(&self, _param: &[glib::Value]) -> Option { exit_unwind(0); } } // vim: ts=4 sw=4 expandtab