diff options
author | Jordan Halase <jordan@halase.me> | 2020-03-09 13:40:51 -0500 |
---|---|---|
committer | Jordan Halase <jordan@halase.me> | 2020-03-09 13:40:51 -0500 |
commit | 760fb45fab539d3b228076f64c3d6480e9e57fd7 (patch) | |
tree | 3860c2b5023c82eadf3d27358679b218930e8478 /main.c |
Diffstat (limited to 'main.c')
-rw-r--r-- | main.c | 86 |
1 files changed, 86 insertions, 0 deletions
@@ -0,0 +1,86 @@ +/* + * Copyright (C) 2020 Jordan Halase <jordan@halase.me> + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION + * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN + * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <stdio.h> +#include <stdlib.h> + +#include <python3.7/Python.h> +#include <pygobject-3.0/pygobject.h> +#include <gtk/gtk.h> + +/* Name of the Python GTK Widget class */ +#define WIDGET_NAME "MainWidget" + +/* Name of the Python module */ +#define MODULE_NAME ("ui." WIDGET_NAME) + +/** Initialize a Python interpreter and load the main module */ +static PyObject *init_python_module(const char *module_name) +{ + Py_Initialize(); + + PyObject *sys_path = PySys_GetObject("path"); + PyList_Append(sys_path, PyUnicode_FromString(".")); + + PyObject *module = PyImport_ImportModule(module_name); + if (!module) { + PyErr_Print(); + } + + return module; +} + +/** Construct a new Python widget from a loaded module */ +static GtkWidget *new_python_widget(PyObject *module) +{ + PyObject *widget_py = PyObject_CallMethod(module, WIDGET_NAME, ""); + if (!widget_py) { + PyErr_Print(); + return NULL; + } + + GObject *observed = pygobject_get(widget_py); + if (!observed || !G_IS_OBJECT(observed)) { + fprintf(stderr, WIDGET_NAME " not a GObject\n"); + } + return GTK_WIDGET(observed); +} + +static void on_activate(GtkApplication *app) +{ + PyObject *module = init_python_module(MODULE_NAME); + + GtkWidget *win = gtk_application_window_new(app); + gtk_window_set_title(GTK_WINDOW(win), "PyGObject Widget from C"); + + GtkWidget *widget = new_python_widget(module); + gtk_container_add(GTK_CONTAINER(win), widget); + + g_signal_connect_swapped(win, "destroy", G_CALLBACK(gtk_widget_destroy), win); + + gtk_widget_show_all(win); +} + +int main(int argc, char **argv) +{ + GtkApplication *app = gtk_application_new("com.example.GtkApplication", + G_APPLICATION_FLAGS_NONE); + g_signal_connect(app, "activate", G_CALLBACK(on_activate), NULL); + + g_application_run(G_APPLICATION(app), argc, argv); + + return Py_FinalizeEx(); +} |