#include #include #include "ui.h" #include #include #include #include static int start_lua(GtkApplication* app) { int status; lua_State* L; int ret; GtkWidget *page_window, *scrolled, *pageview; GtkTextBuffer* pageview_buffer; scrolled = gtk_scrolled_window_new(NULL, NULL); pageview_buffer = gtk_text_buffer_new(NULL); pageview = gtk_text_view_new_with_buffer(pageview_buffer); gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(pageview), GTK_WRAP_WORD_CHAR); gtk_text_view_set_editable(pageview, 0); gtk_text_view_set_cursor_visible(pageview, 0); page_window = gtk_application_window_new(app); gtk_window_set_title(GTK_WINDOW(page_window), "Window"); gtk_window_set_default_size(GTK_WINDOW(page_window), 200, 200); gtk_container_add(GTK_CONTAINER(scrolled), GTK_WIDGET(pageview)); gtk_container_add(GTK_CONTAINER(page_window), GTK_WIDGET(scrolled)); gtk_widget_show_all(page_window); L = luaL_newstate(); // open Lua if (!L) { fprintf(stderr, "error: failed to create lua state"); return -1; } printf("lua: ready\n"); int pageview_append_tag(lua_State* L) { const char* tag_type = luaL_checkstring(L, 1); if (strcmp(tag_type, "text") == 0) { printf(tag_type); const char* tag_text = luaL_checkstring(L, 2); GtkTextIter iter; gtk_text_buffer_get_end_iter(pageview_buffer, &iter); gtk_text_buffer_insert(pageview_buffer, &iter, tag_text, -1); } else { return luaL_error(L, "tried to create tag that does not exist"); } return 0; } lua_pushcfunction(L, pageview_append_tag); lua_setglobal(L, "__ext_pageview_append_tag"); luaL_openlibs(L); status = luaL_loadfile(L, "corescripts/_global.lua"); ret = lua_pcall(L, 0, 0, 0); if (ret != 0) { fprintf(stderr, "lua: corescript: error: %s\n", lua_tostring(L, -1)); return 1; } status = luaL_loadfile(L, "pages/test.lua"); ret = lua_pcall(L, 0, 0, 0); if (ret != 0) { fprintf(stderr, "lua: error: %s\n", lua_tostring(L, -1)); return 1; } lua_close(L); return 0; } static void activate(GtkApplication* app, gpointer user_data) { printf("gtk: ready\n"); start_lua(app); } static int start_gtk(int* argc, char*** argv) { int status = 1; struct application_state state = new_application(); g_signal_connect(state.app, "activate", G_CALLBACK(activate), NULL); status = g_application_run(G_APPLICATION(state.app), *argc, *argv); g_object_unref(state.app); return status; } int main(int argc, char** argv) { start_gtk(&argc, &argv); }