add new textarea (GTK) newapi

Sun, 13 Oct 2024 14:34:12 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 13 Oct 2024 14:34:12 +0200
branch
newapi
changeset 335
91d4f0391282
parent 334
00be546d3b38
child 336
56c12f44c2d3

add new textarea (GTK)

application/main.c file | annotate | diff | comparison | revisions
ui/common/context.c file | annotate | diff | comparison | revisions
ui/gtk/text.c file | annotate | diff | comparison | revisions
ui/ui/text.h file | annotate | diff | comparison | revisions
--- a/application/main.c	Sun Oct 13 14:02:43 2024 +0200
+++ b/application/main.c	Sun Oct 13 14:34:12 2024 +0200
@@ -37,6 +37,7 @@
     UiString *str1;
     UiString *str2;
     UiString *path;
+    UiText *text;
     UiDouble *progress;
     UiList *list;
     UiList *menulist;
@@ -88,6 +89,7 @@
     ui_list_append(doc->list, "test3");
     doc->radio = ui_int_new(docctx, "radio");
     doc->tabview = ui_int_new(docctx, "tabview");
+    //doc->text = ui_text_new(docctx, "text");
     return doc;
 }
 
@@ -192,17 +194,19 @@
         ui_tab(obj, "Tab 2") {
             ui_button(obj, .label = "Button", .onclick=action_tab2_button);
         }
-        ui_tab(obj, "Tab 3         x") {
+        ui_tab(obj, "Tab 3") {
             UiTabViewArgs args = {0};
-            tabview = ui_tabview_create(obj, args);
-            
-            UiObject *tab1 = ui_tabview_add(tabview, "Sub 1", -1);
-            ui_button(tab1, .label = "Button 1");
-            
-            
-            UiObject *tab2 = ui_tabview_add(tabview, "Sub 2", -1);
-            ui_button(tab2, .label = "Button 2");
-            
+            UI_CTN(obj, tabview=ui_tabview_create(obj, args)) {
+                UiObject *tab1 = ui_tabview_add(tabview, "Sub 1", -1);
+                ui_button(tab1, .label = "Button 1");
+
+
+                UiObject *tab2 = ui_tabview_add(tabview, "Sub 2", -1);
+                ui_button(tab2, .label = "Button 2");
+            }
+        }
+        ui_tab(obj, "Tab 4") {
+            ui_textarea(obj, .varname = "text");
         }
     }
     
--- a/ui/common/context.c	Sun Oct 13 14:02:43 2024 +0200
+++ b/ui/common/context.c	Sun Oct 13 14:34:12 2024 +0200
@@ -162,7 +162,7 @@
 
 static UiVar* ctx_getvar(UiContext *ctx, CxHashKey key) {
     UiVar *var = cxMapGet(ctx->vars, key);
-    if(!var) {
+    if(!var && ctx->documents) {
         CxIterator i = cxListIterator(ctx->documents);
         cx_foreach(void *, doc, i) {
             UiContext *subctx = ui_document_context(doc);
--- a/ui/gtk/text.c	Sun Oct 13 14:02:43 2024 +0200
+++ b/ui/gtk/text.c	Sun Oct 13 14:34:12 2024 +0200
@@ -62,8 +62,13 @@
     }
 }
 
-UIWIDGET ui_textarea_var(UiObject *obj, UiVar *var) {
+UIWIDGET ui_textarea_create(UiObject *obj, UiTextAreaArgs args) {
+    UiObject* current = uic_current_obj(obj);
+    UiVar* var = uic_widget_var(obj->ctx, current->ctx, args.value, args.varname, UI_VAR_TEXT);
+    
     GtkWidget *text_area = gtk_text_view_new();
+    ui_set_name_and_style(text_area, args.name, args.style_class);
+    
     gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text_area), GTK_WRAP_WORD_CHAR);
     g_signal_connect(
             text_area,
@@ -90,17 +95,17 @@
     SCROLLEDWINDOW_SET_CHILD(scroll_area, text_area);
     
     // font and padding
-    PangoFontDescription *font;
-    font = pango_font_description_from_string("Monospace");
+    //PangoFontDescription *font;
+    //font = pango_font_description_from_string("Monospace");
     //gtk_widget_modify_font(text_area, font); // TODO
-    pango_font_description_free(font);
+    //pango_font_description_free(font);
     
     gtk_text_view_set_left_margin(GTK_TEXT_VIEW(text_area), 2);
     gtk_text_view_set_right_margin(GTK_TEXT_VIEW(text_area), 2);
     
     // add
-    UiContainer *ct = uic_get_current_container(obj);
-    ct->add(ct, scroll_area, TRUE);
+    UI_APPLY_LAYOUT1(current, args);
+    current->container->add(current->container, scroll_area, TRUE);
     
     // bind value
     UiText *value = var->value;
@@ -160,25 +165,6 @@
     free(textarea);
 }
 
-UIWIDGET ui_textarea(UiObject *obj, UiText *value) {
-    UiVar *var = malloc(sizeof(UiVar));
-    var->value = value;
-    var->type = UI_VAR_SPECIAL;
-    var->from = NULL;
-    var->from_ctx = NULL;
-    return ui_textarea_var(obj, var);
-}
-
-UIWIDGET ui_textarea_nv(UiObject *obj, char *varname) {
-    UiVar *var = uic_create_var(obj->ctx, varname, UI_VAR_TEXT);
-    if(var) {
-        return ui_textarea_var(obj, var);
-    } else {
-        // TODO: error
-    }
-    return NULL;
-}
-
 UIWIDGET ui_textarea_gettextwidget(UIWIDGET textarea) {
     return SCROLLEDWINDOW_GET_CHILD(textarea);
 }
@@ -571,8 +557,8 @@
         gtk_entry_set_visibility(GTK_ENTRY(textfield), FALSE);
     }
     
-    UiContainer *ct = uic_get_current_container(obj);
-    ct->add(ct, textfield, FALSE);
+    UI_APPLY_LAYOUT1(current, args);
+    current->container->add(current->container, textfield, FALSE);
     
     if(var) {
         UiString *value = var->value;
--- a/ui/ui/text.h	Sun Oct 13 14:02:43 2024 +0200
+++ b/ui/ui/text.h	Sun Oct 13 14:34:12 2024 +0200
@@ -35,6 +35,22 @@
 extern "C" {
 #endif
 
+typedef struct UiTextAreaArgs {
+    UiTri fill;
+    UiBool hexpand;
+    UiBool vexpand;
+    int colspan;
+    int rowspan;
+    int width;
+    const char *name;
+    const char *style_class;
+
+    UiText *value;
+    const char *varname;
+    ui_callback onchange;
+    void *onchangedata;
+} UiTextAreaArgs;
+    
 typedef struct UiTextFieldArgs {
     UiTri fill;
     UiBool hexpand;
@@ -46,19 +62,19 @@
     const char *style_class;
 
     UiString* value;
-    const char* varname;
+    const char *varname;
     ui_callback onchange;
-    void* onchangedata;
+    void *onchangedata;
 } UiTextFieldArgs;
 
 typedef struct UiPathElmRet {
-    char* name;
+    char *name;
     size_t name_len;
-    char* path;
+    char *path;
     size_t path_len;
 } UiPathElm;
 
-typedef UiPathElm*(*ui_pathelm_func)(const char *full_path, size_t len, size_t *ret_nelm, void* data);
+typedef UiPathElm*(*ui_pathelm_func)(const char *full_path, size_t len, size_t *ret_nelm, void *data);
 
 
 
@@ -72,24 +88,25 @@
     const char *style_class;
 
     UiString *value;
-    const char* varname;
+    const char *varname;
 
     ui_pathelm_func getpathelm;
-    void* getpathelmdata;
+    void *getpathelmdata;
 
     ui_callback onactivate;
-    void* onactivatedata;
+    void *onactivatedata;
     
     ui_callback ondragstart;
-    void* ondragstartdata;
+    void *ondragstartdata;
     ui_callback ondragcomplete;
-    void* ondragcompletedata;
+    void *ondragcompletedata;
     ui_callback ondrop;
-    void* ondropsdata;
+    void *ondropsdata;
 } UiPathTextFieldArgs;
 
-UIWIDGET ui_textarea_deprecated(UiObject *obj, UiText *value);
-UIWIDGET ui_textarea_nv_deprecated(UiObject *obj, char *varname);
+#define ui_textarea(obj, ...) ui_textarea_create(obj, (UiTextAreaArgs) { __VA_ARGS__ })
+
+UIWIDGET ui_textarea_create(UiObject *obj, UiTextAreaArgs args);
 
 UIWIDGET ui_textarea_gettextwidget(UIWIDGET textarea);
 

mercurial