Skip to content
This repository has been archived by the owner on Jun 8, 2021. It is now read-only.

Commit

Permalink
Add WidgetExt::add_tick_callback binding
Browse files Browse the repository at this point in the history
  • Loading branch information
fengalin committed Dec 28, 2017
1 parent 342f69e commit 78d9383
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion src/widget.rs
Expand Up @@ -2,19 +2,23 @@
// See the COPYRIGHT file at the top-level directory of this distribution.
// Licensed under the MIT license, see the LICENSE file or <http://opensource.org/licenses/MIT>

use std::cell::RefCell;
use std::mem::transmute;
use std::ptr;

use glib::object::{Downcast, IsA};
use glib::signal::{SignalHandlerId, connect};
use glib::translate::*;
use glib_ffi::gboolean;
use glib_ffi::{gboolean, gpointer};
use gdk::{DragAction, Event, ModifierType};
#[cfg(any(feature = "v3_8", feature = "dox"))]
use gdk::FrameClock;
use gdk_ffi;
use pango;
use ffi;

use {
Continue,
DestDefaults,
Inhibit,
Object,
Expand All @@ -32,6 +36,11 @@ pub trait WidgetExtManual {

fn override_font(&self, font: &pango::FontDescription);

#[cfg(any(feature = "v3_8", feature = "dox"))]
fn add_tick_callback<F>(&self, func: F) -> u32
where
F: FnMut(&Self, &FrameClock) -> Continue + 'static;

fn connect_map_event<F: Fn(&Self, &Event) -> Inhibit + 'static>(&self, f: F) -> SignalHandlerId;

fn connect_unmap_event<F: Fn(&Self, &Event) -> Inhibit + 'static>(&self, f: F) -> SignalHandlerId;
Expand Down Expand Up @@ -98,6 +107,23 @@ impl<O: IsA<Widget> + IsA<Object>> WidgetExtManual for O {
}
}

#[cfg(any(feature = "v3_8", feature = "dox"))]
fn add_tick_callback<F>(&self, func: F) -> u32
where
F: FnMut(&Self, &FrameClock) -> Continue + 'static,
{
unsafe {
let func: Box<RefCell<Box<FnMut(&Self, &FrameClock) -> Continue + 'static>>> =
Box::new(RefCell::new(Box::new(func)));
ffi::gtk_widget_add_tick_callback(
self.to_glib_none().0,
Some(trampoline_add_tick_callback),
Box::into_raw(func) as gpointer,
Some(destroy_closure),
)
}
}

fn connect_map_event<F: Fn(&Self, &Event) -> Inhibit + 'static>(&self, f: F) -> SignalHandlerId {
unsafe {
let f: Box<Box<Fn(&Self, &Event) -> Inhibit + 'static>> = Box::new(Box::new(f));
Expand All @@ -115,6 +141,24 @@ impl<O: IsA<Widget> + IsA<Object>> WidgetExtManual for O {
}
}

#[cfg(any(feature = "v3_8", feature = "dox"))]
unsafe extern "C" fn trampoline_add_tick_callback(
widget: *mut ffi::GtkWidget,
frame_clock: *mut gdk_ffi::GdkFrameClock,
func: gpointer,
) -> gboolean {
callback_guard!();
let func: &RefCell<Box<FnMut(&Widget, &FrameClock) -> Continue + 'static>> = transmute(func);

(&mut *func.borrow_mut())(&from_glib_borrow(widget), &from_glib_borrow(frame_clock)).to_glib()
}

#[cfg(any(feature = "v3_8", feature = "dox"))]
unsafe extern "C" fn destroy_closure(ptr: gpointer) {
callback_guard!();
Box::<RefCell<Box<FnMut(&Widget, &FrameClock) -> Continue + 'static>>>::from_raw(ptr as *mut _);
}

unsafe extern "C" fn event_any_trampoline<T>(this: *mut ffi::GtkWidget,
event: *mut gdk_ffi::GdkEventAny,
f: &&(Fn(&T, &Event) -> Inhibit + 'static)) -> gboolean
Expand Down

0 comments on commit 78d9383

Please sign in to comment.