Skip to content

Instantly share code, notes, and snippets.

@fxff
Created December 9, 2019 09:49
Show Gist options
  • Save fxff/6bdec8d20b303cbce05983d33f72dbde to your computer and use it in GitHub Desktop.
Save fxff/6bdec8d20b303cbce05983d33f72dbde to your computer and use it in GitHub Desktop.
GTK method patching
// gtk-method-patching.c: an example of patching GTK methods.
// gcc `pkg-config --libs --cflags gtk+-3.0` gtk-method-patching.c
#include <gtk/gtk.h>
typedef void (* get_preferred_width) (GtkWidget *, gint *, gint *);
get_preferred_width original; // A place to store real function
void patched_gpw (GtkWidget *w, gint *min_w, gint *nat_w)
{
original (w, min_w, nat_w);
g_message ("Patched: %p %i %i", w, min_w ? *min_w : 0, nat_w ? *nat_w : 0);
}
int
main (int argc, char **argv)
{
GtkWidget *window;
GtkWidget *box, *box2;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
box2 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
// Patching
{
GtkWidgetClass *widget_class = GTK_WIDGET_GET_CLASS (box);
original = widget_class->get_preferred_width;
widget_class->get_preferred_width = patched_gpw;
}
gtk_container_add (GTK_CONTAINER (box), gtk_button_new_with_label ("hello"));
gtk_container_add (GTK_CONTAINER (box2), gtk_button_new_with_label ("world"));
gtk_container_add (GTK_CONTAINER (box), box2);
gtk_container_add (GTK_CONTAINER (window), box);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment