From d2476dac0aa7eefbd89136c4df0880d3873f54fa Mon Sep 17 00:00:00 2001 From: hippoz Date: Sun, 2 May 2021 15:07:43 +0300 Subject: [PATCH] basic lua interaction with text buffer --- Makefile | 4 +- README.md | 3 ++ corescripts/_global.lua | 29 ++++++++++++ main.c | 97 +++++++++++++++++++++++++++++++++++++++-- pages/test.lua | 8 ++++ ui.c | 11 +++++ ui.h | 18 ++++++++ 7 files changed, 165 insertions(+), 5 deletions(-) create mode 100644 corescripts/_global.lua create mode 100644 pages/test.lua create mode 100644 ui.c create mode 100644 ui.h diff --git a/Makefile b/Makefile index e92feb4..cb8d846 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ CC = gcc -main.out: main.c - $(CC) -o main.out main.c +main.out: main.c ui.c + $(CC) main.c ui.c -o main.out $(shell pkg-config --cflags --libs gtk+-3.0) $(shell pkg-config --cflags --libs luajit) clean: @rm main.out \ No newline at end of file diff --git a/README.md b/README.md index 3a0a2e9..ca5d655 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,5 @@ # browser-thing +Dependencies: + - luajit + - gtk3 \ No newline at end of file diff --git a/corescripts/_global.lua b/corescripts/_global.lua new file mode 100644 index 0000000..fd55845 --- /dev/null +++ b/corescripts/_global.lua @@ -0,0 +1,29 @@ +--[[ utility functions ]]-- + +function _el(name) + return function(args) + return { name, args } + end +end + +h1 = _el("h1") +t = _el("text") +b = _el("b") +input = _el("input") + + +--[[ page module ]]-- + +page = {} + +function page.Commit(content) + --__ext_pageview_clear() + for i, v in pairs(content) do + -- tag_name = v[1] + -- tag_props = v[2] + + print(v[1], v[2]) + + __ext_pageview_append_tag(v[1], v[2]) + end +end \ No newline at end of file diff --git a/main.c b/main.c index 797d9d8..6af785b 100644 --- a/main.c +++ b/main.c @@ -1,7 +1,98 @@ #include +#include +#include "ui.h" -int main() { - printf("hello"); +#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; -} \ No newline at end of file +} + +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); +} diff --git a/pages/test.lua b/pages/test.lua new file mode 100644 index 0000000..a1bc9df --- /dev/null +++ b/pages/test.lua @@ -0,0 +1,8 @@ +function Home() + return { + t("Hello"), + t(" wow") + } +end + +page.Commit(Home()) \ No newline at end of file diff --git a/ui.c b/ui.c new file mode 100644 index 0000000..65b2e6e --- /dev/null +++ b/ui.c @@ -0,0 +1,11 @@ +#include "ui.h" + +struct application_state new_application() { + GtkApplication* gapp; + + gapp = gtk_application_new("xyz.hippoz.nettle", G_APPLICATION_FLAGS_NONE); + + struct application_state state = {.app = gapp}; + + return state; +} \ No newline at end of file diff --git a/ui.h b/ui.h new file mode 100644 index 0000000..e3578af --- /dev/null +++ b/ui.h @@ -0,0 +1,18 @@ +#ifndef __UI_H +#define __UI_H + +#include + +enum ElementType { + Heading1, + Text +}; + +struct application_state { + GtkApplication* app; +}; + +struct application_state new_application(); +void add_element(enum ElementType elementType); + +#endif \ No newline at end of file