Skip to content

Commit

Permalink
Add password and app_data support for C Bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
malisipi committed Jan 21, 2023
1 parent e540cfe commit fe0248e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
9 changes: 6 additions & 3 deletions c_bindings/mui.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ typedef struct mui_window mui_window;
typedef struct mui_event_details mui_event_details;
typedef struct mui_parsed_event_details mui_parsed_event_details;
typedef struct v_string v_string;
typedef struct mui_object mui_object;

struct v_string {
int* str;
Expand All @@ -39,25 +40,27 @@ struct mui_parsed_event_details {
};

struct mui_window {};
struct mui_object {};

void mui_init(int argc, char** argv);
mui_window* mui_create(char* config);
mui_window* mui_create(char* config, void**);
void mui_button(mui_window* window, char* config, void (*onclk)(mui_event_details, mui_window*, void**));
void mui_label(mui_window* window, char* config, void (*onclk)(mui_event_details, mui_window*, void**));
void mui_textbox(mui_window* window, char* config, void (*onchg)(mui_event_details, mui_window*, void**));
void mui_password(mui_window* window, char* config, void (*onchg)(mui_event_details, mui_window*, void**));
void mui_run(mui_window* window);
void mui_change_object_property(mui_window* window, char* id, char* config);
void mui_change_object_property(mui_window* window, struct mui_object* object, char* config);
struct mui_parsed_event_details mui_parse_event_details(mui_event_details);
void mui_empty_fn(mui_event_details details, mui_window* window, void** app_data){}
void mui_destroy(mui_window* window);
int mui_messagebox(char* title, char* message, char* typ, char* icon);
void mui_beep();
char* mui_inputbox(char* title, char* text, char* default_text);
struct mui_object* mui_get_object_by_id(mui_window* window, char* id);

/*
### Not Implemented Yet ###
void mui_get_object_by_id();
void mui_sort_widgets_with_zindex();
void mui_notifypopup();
void mui_openfiledialog();
Expand Down
22 changes: 18 additions & 4 deletions c_bindings/mui.v
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ fn mui_init(argc int, argv &&char){
}

[export: "mui_create"]
fn mui_create(pconf &char) &mui.Window {
fn mui_create(pconf &char, app_data voidptr) &mui.Window {
unsafe {
jconf := json.decode(map[string]IntString, pconf.vstring()) or {println("Crashed -> json") exit(0)}
mut wconf := mui.WindowConfig{}
mut wconf := mui.WindowConfig{app_data: app_data}
if jconf["title"].type_name() == "string" { wconf.title = jconf["title"] as string }
if jconf["width"].type_name() == "int" { wconf.width = jconf["width"] as int }
if jconf["height"].type_name() == "int" { wconf.height = jconf["height"] as int }
Expand All @@ -23,12 +23,12 @@ fn mui_create(pconf &char) &mui.Window {
}

[export: "mui_change_object_property"]
fn mui_change_object_property(mut window &mui.Window, id &char, pconf &char){
fn mui_change_object_property(mut window &mui.Window, object &map[string]mui.WindowData, pconf &char){
unsafe {
jconf := json.decode(map[string]string, pconf.vstring()) or {println("Crashed -> json") exit(0)}
property := jconf["property"]
value := jconf["value"]
window.get_object_by_id(id.vstring())[0][property] = mui.WindowData{str: value}
object[property] = mui.WindowData{str: value}
}
}

Expand All @@ -54,6 +54,8 @@ fn mui_widget(_type string, mut window &mui.Window, pconf &char, onclk mui.OnEve
window.label(wconf)
} "textbox" {
window.textbox(wconf)
} "password" {
window.password(wconf)
} else {
println(":: Invalid/Unsupported widget type")
}
Expand All @@ -77,6 +79,18 @@ fn mui_textbox(mut window &mui.Window, pconf &char, onchg mui.OnEvent){
mui_widget("textbox", mut window, pconf, mui.empty_fn, onchg, mui.empty_fn)
}

[export: "mui_password"]
fn mui_password(mut window &mui.Window, pconf &char, onchg mui.OnEvent){
mui_widget("password", mut window, pconf, mui.empty_fn, onchg, mui.empty_fn)
}

[export: "mui_get_object_by_id"]
fn mui_get_object_by_id(mut window &mui.Window, id &char) voidptr {
unsafe {
return &window.get_object_by_id(id.vstring())[0]
}
}

pub struct ParsedEventDetails{
pub mut:
event &char // click, value_change, unclick, keypress, files_drop, resize
Expand Down
17 changes: 14 additions & 3 deletions c_bindings/test.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#include "mui.h"
#include <string.h>
#include <stdio.h>

struct AppData {
int count;
};

void button_event_handler(mui_event_details details, mui_window* window, void** app_data){
mui_messagebox("Goodbye!",
Expand All @@ -9,16 +14,22 @@ void button_event_handler(mui_event_details details, mui_window* window, void**
}

void textbox_event_handler(mui_event_details details, mui_window* window, void** app_data){
printf("%d\n",++((struct AppData*)app_data)->count);
struct mui_parsed_event_details parsed_details = mui_parse_event_details(details);
char str[1024] = "{\"property\":\"text\",\"value\":\"";
char str[256] = "{\"property\":\"text\",\"value\":\"";
strcat(str, parsed_details.value);
strcat(str, "\"}");
mui_change_object_property(window, "label1", str);
mui_change_object_property(window,
mui_get_object_by_id(window, "label1"),
str);
}

void main(int argc, char** argv){
struct AppData app_data;
app_data.count = 0;

mui_init(argc, argv);
mui_window* window = mui_create("{\"title\":\"Hello World\", \"width\":450, \"height\":250}");
mui_window* window = mui_create("{\"title\":\"Hello World\", \"width\":450, \"height\":250}", (void*)&app_data);
mui_button(window, "{\"id\":\"button1\", \"x\":\"10%x\", \"y\":\"10\", \"width\":\"80%x\", \"text\": \"Destroy the window\"}", *button_event_handler);
mui_label(window, "{\"id\":\"label1\", \"x\":\"10%x\", \"y\":\"50\", \"width\":\"80%x\", \"text\": \"This is a label\"}", *mui_empty_fn);
mui_textbox(window, "{\"id\":\"textbox1\", \"x\":\"10%x\", \"y\":\"90\", \"width\":\"80%x\", \"text\": \"This is a textbox\"}", *textbox_event_handler);
Expand Down

0 comments on commit fe0248e

Please sign in to comment.