simplify frontend code

gui-branch
an 2019-03-29 15:27:16 -04:00
parent 1a3145b5b6
commit 392e20ae65
1 changed files with 70 additions and 64 deletions

View File

@ -10,6 +10,16 @@ use gtk_sys::*;
use maraiah::{c_str, durandal::ffi};
use std::{cell::RefCell, marker::PhantomData, rc::Rc};
const ACTIVATE: ffi::NT = c_str!("activate");
const APP_ID: ffi::NT = c_str!("net.greyserv.maraiah.tycho");
const DELETE_EVENT: ffi::NT = c_str!("delete-event");
const DESTROY: ffi::NT = c_str!("destroy");
const DRAW: ffi::NT = c_str!("draw");
const IM_ABOUT: ffi::NT = c_str!("/net/greyserv/maraiah/tycho/tycho2.png");
const IM_NOMAP: ffi::NT = c_str!("/net/greyserv/maraiah/tycho/tycho1.png");
const PATH_BUILDER: ffi::NT = c_str!("/net/greyserv/maraiah/tycho/ui");
const PATH_CSS: ffi::NT = c_str!("/net/greyserv/maraiah/tycho/css");
/// Called when the application activates in order to set everything up.
unsafe extern "C" fn app_activate(app: *mut GtkApplication, edit: gpointer)
{
@ -19,8 +29,7 @@ unsafe extern "C" fn app_activate(app: *mut GtkApplication, edit: gpointer)
setup_css();
let path = c_str!("/net/greyserv/maraiah/tycho/ui");
let b = Refc::new(gtk_builder_new_from_resource(path));
let b = Refc::new(gtk_builder_new_from_resource(PATH_BUILDER));
setup_draw_area(&b, edit.clone());
setup_win_map_view(&b);
@ -54,8 +63,7 @@ unsafe fn setup_draw_area(b: &Refc<GtkBuilder>, edit: Rc<MapEditorRef>)
rend: gpointer)
-> gboolean
{
let rend = rend as *mut RenderState;
let rend = &mut *rend;
let rend = &mut *(rend as *mut RenderState);
let w = f64::from(gtk_widget_get_allocated_width(wid));
let h = f64::from(gtk_widget_get_allocated_height(wid));
@ -66,7 +74,7 @@ unsafe fn setup_draw_area(b: &Refc<GtkBuilder>, edit: Rc<MapEditorRef>)
gtk_adjustment_set_lower(*rend.ay, 0.0);
gtk_adjustment_set_upper(*rend.ay, h);
let im = CrImage(*rend.im_nomap);
let im = CrImage(*rend.im_nomap);
let mut dr = CrDrawArea::new(ctx, w, h);
rend.edit.borrow().draw(&mut dr, &im);
@ -74,55 +82,54 @@ unsafe fn setup_draw_area(b: &Refc<GtkBuilder>, edit: Rc<MapEditorRef>)
1
}
let wid: *mut GtkDrawingArea = get_obj(b, c_str!("draw-area"));
let wid = get_obj::<GtkDrawingArea>(b, c_str!("draw-area"));
edit.borrow_mut().set_draw(wid as _);
// get all of the necessary state and related objects
let ax = Refc::<GtkAdjustment>::new(get_obj(b, c_str!("adj-map-horz")));
let ay = Refc::<GtkAdjustment>::new(get_obj(b, c_str!("adj-map-vert")));
let ax = Refc::new(get_obj::<GtkAdjustment>(b, c_str!("adj-map-horz")));
let ay = Refc::new(get_obj::<GtkAdjustment>(b, c_str!("adj-map-vert")));
g_object_ref(*ax as _);
g_object_ref(*ay as _);
let im_nomap = load_img(c_str!("/net/greyserv/maraiah/tycho/tycho1.png"));
let im_nomap = Refc::new(im_nomap);
let im_nomap = Refc::new(load_img(IM_NOMAP));
let rend = RenderState{im_nomap, ax, ay, edit};
let rend = Box::into_raw(Box::new(rend));
connect(wid as _, c_str!("destroy"), c_done as _, rend as _);
connect(wid as _, c_str!("draw"), c_draw as _, rend as _);
connect(wid, DESTROY, c_done as _, rend);
connect(wid, DRAW, c_draw as _, rend);
}
/// Sets up the map view window.
unsafe fn setup_win_map_view(b: &Refc<GtkBuilder>)
{
let win: *mut GtkWindow = get_obj(b, c_str!("win-map-view"));
let btn: *mut GtkMenuItem = get_obj(b, c_str!("btn-show-map-view"));
let win = get_obj::<GtkWindow >(b, c_str!("win-map-view"));
let btn = get_obj::<GtkMenuItem>(b, c_str!("btn-show-map-view"));
connect_hide(win as _);
connect_show(btn as _, win as _);
connect_hide(win);
connect_show(btn, win);
}
/// Sets up the map tools window.
unsafe fn setup_win_map_tools(b: &Refc<GtkBuilder>)
{
let win: *mut GtkWindow = get_obj(b, c_str!("win-map-tools"));
let btn: *mut GtkMenuItem = get_obj(b, c_str!("btn-show-map-tools"));
let win = get_obj::<GtkWindow >(b, c_str!("win-map-tools"));
let btn = get_obj::<GtkMenuItem>(b, c_str!("btn-show-map-tools"));
connect_hide(win as _);
connect_show(btn as _, win as _);
connect_hide(win);
connect_show(btn, win);
}
/// Sets up the map properties window.
unsafe fn setup_win_map_prop(b: &Refc<GtkBuilder>)
{
let win: *mut GtkWindow = get_obj(b, c_str!("win-map-prop"));
let btn: *mut GtkMenuItem = get_obj(b, c_str!("btn-show-map-prop"));
let win = get_obj::<GtkWindow >(b, c_str!("win-map-prop"));
let btn = get_obj::<GtkMenuItem>(b, c_str!("btn-show-map-prop"));
connect_hide(win as _);
connect_show(btn as _, win as _);
connect_hide(win);
connect_show(btn, win);
}
/// Sets up the about dialogue.
@ -136,21 +143,20 @@ unsafe fn setup_about_dlg(b: &Refc<GtkBuilder>)
gtk_widget_hide(dlg as _);
}
let dlg: *mut GtkAboutDialog = get_obj(b, c_str!("dlg-about"));
let btn: *mut GtkMenuItem = get_obj(b, c_str!("btn-about"));
let dlg = get_obj::<GtkAboutDialog>(b, c_str!("dlg-about"));
let btn = get_obj::<GtkMenuItem >(b, c_str!("btn-about"));
let it = env!("CARGO_PKG_AUTHORS").split(';');
let mut v = ffi::CStringVec::new_from_iter(it).unwrap();
let img = load_img(c_str!("/net/greyserv/maraiah/tycho/tycho2.png"));
let img = Refc::new(img);
let img = Refc::new(load_img(IM_ABOUT));
gtk_about_dialog_set_authors(dlg, v.as_mut_ptr());
gtk_about_dialog_set_version(dlg, c_str!(env!("CARGO_PKG_VERSION")));
gtk_about_dialog_set_website(dlg, c_str!(env!("CARGO_PKG_HOMEPAGE")));
gtk_about_dialog_set_logo(dlg, *img);
connect_hide(dlg as _);
connect(btn as _, c_str!("activate"), c_show_act as _, dlg as _);
connect_hide(dlg);
connect(btn, ACTIVATE, c_show_act as _, dlg);
}
/// Sets up explicit window finalization for the main window.
@ -202,7 +208,7 @@ unsafe fn setup_explicit_drop(b: &Refc<GtkBuilder>, win: *mut GtkWindow)
let exp_del = Box::into_raw(Box::new(exp_del));
connect(win as _, c_str!("destroy"), c_done as _, exp_del as _);
connect(win, DESTROY, c_done as _, exp_del);
}
/// Sets up the main menu window.
@ -219,8 +225,7 @@ unsafe fn setup_win_main(b: &Refc<GtkBuilder>,
/// Callback to create a new map when the "New" button is pressed.
unsafe extern "C" fn c_new_act(_: *mut GtkWidget, edit: gpointer)
{
let edit = edit as *const MapEditorRef;
let edit = &*edit;
let edit = &*(edit as *const MapEditorRef);
let mut edit = edit.borrow_mut();
if edit.is_opened() {
@ -237,15 +242,8 @@ unsafe fn setup_win_main(b: &Refc<GtkBuilder>,
edit.cause_update();
}
/// Callback to finalize the editor state reference.
unsafe extern "C" fn c_new_done(_: *mut GtkWidget, edit: gpointer)
{
let edit = edit as *const MapEditorRef;
Rc::from_raw(edit);
}
// set up main window
let win: *mut GtkWindow = get_obj(b, c_str!("win-main"));
let win = get_obj::<GtkWindow>(b, c_str!("win-main"));
setup_explicit_drop(b, win);
@ -253,24 +251,18 @@ unsafe fn setup_win_main(b: &Refc<GtkBuilder>,
gtk_widget_show_all(win as _);
// set up buttons
let btn: *mut GtkMenuItem = get_obj(b, c_str!("btn-quit"));
let btn = get_obj::<GtkMenuItem>(b, c_str!("btn-quit"));
connect(btn, ACTIVATE, c_quit_act as _, win);
connect(btn as _, c_str!("activate"), c_quit_act as _, win as _);
let btn: *mut GtkMenuItem = get_obj(b, c_str!("btn-new"));
let eptr = Rc::into_raw(edit.clone());
connect(btn as _, c_str!("activate"), c_new_act as _, eptr as _);
connect(btn as _, c_str!("destroy"), c_new_done as _, eptr as _);
let btn = get_obj::<GtkMenuItem>(b, c_str!("btn-new"));
connect(btn, ACTIVATE, c_new_act as _, connect_ref(btn, edit.clone()));
}
/// Sets up the CSS styling providers.
unsafe fn setup_css()
{
let path = c_str!("/net/greyserv/maraiah/tycho/css");
let css = Refc::new(gtk_css_provider_new());
gtk_css_provider_load_from_resource(*css, path);
gtk_css_provider_load_from_resource(*css, PATH_CSS);
let scr = gdk_screen_get_default();
let pri = GTK_STYLE_PROVIDER_PRIORITY_APPLICATION as u32;
@ -288,7 +280,7 @@ unsafe fn run_ok_cancel_dlg(title: ffi::NT, text: ffi::NT) -> bool
GTK_RESPONSE_ACCEPT,
c_str!("_Cancel"),
GTK_RESPONSE_REJECT,
ffi::null_mut::<gpointer>());
ffi::null_mut_void());
let area = gtk_dialog_get_content_area(dlg as _);
let labl = gtk_label_new(text);
@ -307,7 +299,7 @@ unsafe fn run_ok_cancel_dlg(title: ffi::NT, text: ffi::NT) -> bool
}
/// Connects a handler that hides a toplevel widget when deleted.
unsafe fn connect_hide(wid: *mut GtkWidget)
unsafe fn connect_hide<T>(wid: *mut T)
{
/// Callback to hide the widget.
unsafe extern "C" fn c_hide_del(wid: *mut GtkWidget,
@ -317,11 +309,11 @@ unsafe fn connect_hide(wid: *mut GtkWidget)
gtk_widget_hide(wid);
}
connect(wid as _, c_str!("delete-event"), c_hide_del as _, ffi::null_mut());
connect(wid, DELETE_EVENT, c_hide_del as _, ffi::null_void());
}
/// Connects a handler that shows a widget when activated.
unsafe fn connect_show(btn: *mut GtkWidget, wid: *mut GtkWidget)
unsafe fn connect_show<T, U>(btn: *mut T, wid: *mut U)
{
/// Callback to show the widget.
unsafe extern "C" fn c_show_act(_: *mut GtkWidget, wid: gpointer)
@ -329,21 +321,36 @@ unsafe fn connect_show(btn: *mut GtkWidget, wid: *mut GtkWidget)
gtk_widget_show_all(wid as _);
}
connect(btn as _, c_str!("activate"), c_show_act as _, wid as _);
connect(btn, ACTIVATE, c_show_act as _, wid);
}
/// Connects a reference-counted object to a widget.
unsafe fn connect_ref<T, U>(obj: *mut T, rc: Rc<U>) -> *const U
{
/// Callback to finalize the reference.
unsafe extern "C" fn c_done(_: *mut GtkWidget, edit: gpointer)
{
Rc::from_raw(edit as *const MapEditorRef);
}
let ptr = Rc::into_raw(rc);
connect(obj, DESTROY, c_done as _, ptr);
ptr
}
/// Gets an object from a `GtkBuilder`.
unsafe fn get_obj<T>(b: &Refc<GtkBuilder>, name: ffi::NT) -> *mut T
{
let obj = gtk_builder_get_object(**b, name);
obj as _
gtk_builder_get_object(**b, name) as _
}
/// Connects a signal handler.
unsafe fn connect(obj: *mut GObject, name: ffi::NT, cb: gpointer, d: gpointer)
unsafe fn connect<T, U>(obj: *mut T, name: ffi::NT, cb: gpointer, d: *const U)
{
let cb = std::mem::transmute(cb);
g_signal_connect_data(obj, name, cb, d, None, 0);
g_signal_connect_data(obj as _, name, cb, d as _, None, 0);
}
/// Loads a `Pixbuf` from a resource.
@ -377,10 +384,9 @@ fn main()
let eptr = Rc::into_raw(edit.clone());
// create and run the app
let name = c_str!("net.greyserv.maraiah.tycho");
let app = Refc::new(gtk_application_new(name, 0));
let app = Refc::new(gtk_application_new(APP_ID, 0));
connect(*app as _, c_str!("activate"), app_activate as _, eptr as _);
connect(*app, ACTIVATE, app_activate as _, eptr);
g_application_run(*app as _, 0, ffi::null_mut());