persistent textarea cursor position

Tue, 17 Feb 2015 20:12:22 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 17 Feb 2015 20:12:22 +0100
changeset 90
2019fdbaadfd
parent 89
9a7e4a335b2b
child 91
c13e344fa55f

persistent textarea cursor position

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/gtk/text.h file | annotate | diff | comparison | revisions
ui/gtk/toolbar.c file | annotate | diff | comparison | revisions
ui/motif/text.c file | annotate | diff | comparison | revisions
ui/motif/text.h file | annotate | diff | comparison | revisions
ui/ui/toolkit.h file | annotate | diff | comparison | revisions
--- a/application/main.c	Sun Feb 15 15:44:24 2015 +0100
+++ b/application/main.c	Tue Feb 17 20:12:22 2015 +0100
@@ -33,66 +33,6 @@
 #include <ucx/buffer.h>
 #include <ucx/utils.h>
 
-/*
-typedef struct Person {
-    char *name;
-    char *mail;
-} Person;
-
-typedef struct Document {
-    UiText text;
-} Document;
-
-void action_close(UiEvent *event, void *data) {
-    ui_close(event->obj);
-}
-
-char* person_getvalue(Person *p, int column) {
-    switch(column) {
-        case 0: return p->name;
-        case 1: return p->mail;
-    }
-    return NULL;
-}
-
-UiString name;
-UiString mail;
-
-UIWIDGET tabview = NULL;
-
-void action_activate(UiEvent *event, void *data) {
-    UiListSelection *selection = event->eventdata;
-    printf("activate: %d\n", event->intval);
-}
-
-void action_select(UiEvent *event, void *data) {
-    UiListSelection *selection = event->eventdata;
-    printf("selection[%d]: ", selection->count);
-    for(int i=0;i<selection->count;i++) {
-        printf("%d ", selection->rows[i]);
-    }
-    printf("\n");
-}
-
-void action_button(UiEvent *event, void *data) {
-    printf("button: %d\n", event->intval);
-    //char *s = ui_gettext(event->obj, "text");
-    char *s = ui_getval(name);
-    printf("{%s}\n", s);
-    //printf("name: {%s}\n", ui_getval(name));
-    //printf("mail: {%s}\n", ui_getval(mail));
-    //ui_select_tab(tabview, 1);
-}
-
-void action_check(UiEvent *event, void *data) {
-    printf("check: %d\n", event->intval);
-}
-
-void action_test(UiEvent *event, void *data) {
-    //ui_select_tab(tabview, 0);
-}
-*/
-
 void action_menu(UiEvent *event, void *data) {
     printf("action_menu test: {%s}\n", data);
     printf("text: {%s}\n", ui_gettext(event->obj, "text"));
@@ -123,69 +63,19 @@
     ui_toolbar_add_default("button2");
     
     UiObject *obj = ui_window("Test", NULL);
-    ui_textarea_nv(obj, "text");
+    
+    UiTabbedPane *docs = ui_tabbed_document_view(obj);
+    UiObject *tab;
+    tab = ui_document_tab(docs);
+    ui_textarea(tab, NULL);
+    tab = ui_document_tab(docs);
+    ui_textarea(tab, NULL);
+    tab = ui_document_tab(docs);
+    ui_textarea(tab, NULL);
+    tab = ui_document_tab(docs);
+    ui_textarea(tab, NULL);
     ui_show(obj);
     ui_main();
-    /*
-	ui_locales_dir("/opt/app1/locales");
-    ui_load_lang_def(NULL, "en_EN");
-    //ui_openfilefunc(action_new, NULL);
     
-    
-    
-    ui_menu("File");
-    ui_menuitem_st(UI_STOCK_CLOSE, action_close, NULL);
-    ui_checkitem("Test", action_check, NULL);
-    //ui_checkitem("Check", action_button, NULL);
-    
-    ui_toolitem_st("button", UI_STOCK_GO_BACK, action_button, NULL);
-    ui_toolbar_add_default("button");
-    
-    printf("create window\n");
-    UiObject *window = ui_window("Mod0", NULL);
-    
-    UiModelInfo *model = ui_model_info(window->ctx, UI_STRING, "Name", UI_STRING, "Email", -1);
-    model->getvalue = (ui_model_getvalue_f)person_getvalue;
-    model->activate = action_activate;
-    model->selection = action_select;
-    UiList *list = ui_list_new();
-    Person *p1 = ui_malloc(window->ctx, sizeof(Person));
-    Person *p2 = ui_malloc(window->ctx, sizeof(Person));
-    Person *p3 = ui_malloc(window->ctx, sizeof(Person));
-    Person *p4 = ui_malloc(window->ctx, sizeof(Person));
-    p1->name = "Some Näme";
-    p1->mail = "mail@host.com";
-    p2->name = "押井守";
-    p2->mail = "other.person@provider.com";
-    p3->name = "My Self";
-    p3->mail = "my@self.org";
-    p4->name = "Gregory House";
-    p4->mail = "greg@pp";
-    ui_list_append(list, p1);
-    ui_list_append(list, p2);
-    ui_list_append(list, p3);
-    ui_list_append(list, p4);
-    
-    ui_layout_fill(window, FALSE);
-    ui_grid(window);
-    ui_button(window, "Test", NULL, NULL);
-    ui_button(window, "Test", NULL, NULL);
-    ui_button(window, "Test", NULL, NULL);
-    ui_newline(window);
-    ui_textfield(window, NULL);
-    ui_end(window);
-    ui_listview(window, list, (ui_model_getvalue_f)person_getvalue, action_activate, NULL);
-    ui_contextmenu(window);
-    ui_widget_menuitem(window, "OK", action_test, NULL);
-    ui_widget_menuitem(window, "1", action_test, NULL);
-    ui_widget_menuitem(window, "2", action_test, NULL);
-    ui_widget_menuitem(window, "3", action_test, NULL);
-    ui_widget_menuitem(window, "4", action_test, NULL);
-    ui_widget_menuitem_st(window, UI_STOCK_CLOSE, action_test, NULL);
-    
-    
-    ui_show(window);
-    ui_main();
-    */
     return (EXIT_SUCCESS);
 }
--- a/ui/common/context.c	Sun Feb 15 15:44:24 2015 +0100
+++ b/ui/common/context.c	Tue Feb 17 20:12:22 2015 +0100
@@ -174,7 +174,7 @@
             return var;
         }
     } else {
-        // create an empty value and add it first to the window variables
+        // create an empty value and add it to the window variables
         // it can be moved to the document vars later
         void *value = uic_create_value(ctx->mempool->allocator, type);
         if(!value) {
@@ -217,11 +217,14 @@
             UiText *f = from->value;
             UiText *t = to->value;
             char *tvalue = t->value;
+            int tpos = t->pos;
             memcpy(t, f, sizeof(UiText));
             if(set && tvalue) {
                 t->set(t, tvalue);
+                t->setposition(t, tpos);
             } else {
                 f->value = f->get(f);
+                f->pos = f->position(f);
             }
             break;
         }
--- a/ui/gtk/text.c	Sun Feb 15 15:44:24 2015 +0100
+++ b/ui/gtk/text.c	Tue Feb 17 20:12:22 2015 +0100
@@ -110,6 +110,7 @@
         value->set = ui_textarea_set;
         value->getsubstr = ui_textarea_getsubstr;
         value->insert = ui_textarea_insert;
+        value->setposition = ui_textarea_setposition;
         value->position = ui_textarea_position;
         value->selection = ui_textarea_selection;
         value->length = ui_textarea_length;
@@ -198,11 +199,18 @@
     gtk_text_buffer_insert(text->obj, &offset, str, -1);
 }
 
+void ui_textarea_setposition(UiText *text, int pos) {
+    GtkTextIter iter;
+    gtk_text_buffer_get_iter_at_offset(text->obj, &iter, pos);
+    gtk_text_buffer_place_cursor(text->obj, &iter);
+}
+
 int ui_textarea_position(UiText *text) {
     GtkTextIter begin;
     GtkTextIter end;
     gtk_text_buffer_get_selection_bounds(text->obj, &begin, &end);
-    return gtk_text_iter_get_offset(&begin);
+    text->pos = gtk_text_iter_get_offset(&begin);
+    return text->pos;
 }
 
 void ui_textarea_selection(UiText *text, int *begin, int *end) {
--- a/ui/gtk/text.h	Sun Feb 15 15:44:24 2015 +0100
+++ b/ui/gtk/text.h	Tue Feb 17 20:12:22 2015 +0100
@@ -63,6 +63,7 @@
 void ui_textarea_set(UiText *text, char *str);
 char* ui_textarea_getsubstr(UiText *text, int begin, int end);
 void ui_textarea_insert(UiText *text, int pos, char *str);
+void ui_textarea_setposition(UiText *text, int pos);
 int ui_textarea_position(UiText *text);
 void ui_textarea_selection(UiText *text, int *begin, int *end);
 int ui_textarea_length(UiText *text);
--- a/ui/gtk/toolbar.c	Sun Feb 15 15:44:24 2015 +0100
+++ b/ui/gtk/toolbar.c	Tue Feb 17 20:12:22 2015 +0100
@@ -238,6 +238,8 @@
         GdkPixbuf *pixbuf = ui_get_image(item->image);
         GtkWidget *image = gtk_image_new_from_pixbuf(pixbuf);
         gtk_tool_button_set_icon_widget(GTK_TOOL_BUTTON(button), image);
+    } else {
+        gtk_tool_item_set_is_important(button, TRUE);
     }
     
     if(item->callback) {
--- a/ui/motif/text.c	Sun Feb 15 15:44:24 2015 +0100
+++ b/ui/motif/text.c	Tue Feb 17 20:12:22 2015 +0100
@@ -69,6 +69,7 @@
         value->get = ui_textarea_get;
         value->getsubstr = ui_textarea_getsubstr;
         value->insert = ui_textarea_insert;
+        value->setposition = ui_textarea_setposition;
         value->position = ui_textarea_position;
         value->selection = ui_textarea_selection;
         value->length = ui_textarea_length;
@@ -136,11 +137,16 @@
     XmTextInsert(text->obj, pos, str);
 }
 
+void ui_textarea_setposition(UiText *text, int pos) {
+    XmTextSetInsertionPosition(text->obj, pos);
+}
+
 int ui_textarea_position(UiText *text) {
     long begin;
     long end;
     XmTextGetSelectionPosition(text->obj, &begin, &end);
-    return begin;
+    text->pos = begin;
+    return text->pos;
 }
 
 void ui_textarea_selection(UiText *text, int *begin, int *end) {
--- a/ui/motif/text.h	Sun Feb 15 15:44:24 2015 +0100
+++ b/ui/motif/text.h	Tue Feb 17 20:12:22 2015 +0100
@@ -63,6 +63,7 @@
 void ui_textarea_set(UiText *text, char *str);
 char* ui_textarea_getsubstr(UiText *text, int begin, int end);
 void ui_textarea_insert(UiText *text, int pos, char *str);
+void ui_textarea_setposition(UiText *text, int pos);
 int ui_textarea_position(UiText *text);
 void ui_textarea_selection(UiText *text, int *begin, int *end);
 int ui_textarea_length(UiText *text);
--- a/ui/ui/toolkit.h	Sun Feb 15 15:44:24 2015 +0100
+++ b/ui/ui/toolkit.h	Tue Feb 17 20:12:22 2015 +0100
@@ -73,23 +73,23 @@
 extern "C" {
 #endif
 
-#define UI_GROUP_SELECTION     20000
+#define UI_GROUP_SELECTION  20000
     
 /* public types */
 typedef int UiBool;
 
-typedef struct UiObject    UiObject;
-typedef struct UiEvent     UiEvent;
-typedef struct UiObserver  UiObserver;
+typedef struct UiObject     UiObject;
+typedef struct UiEvent      UiEvent;
+typedef struct UiObserver   UiObserver;
 
-typedef struct UiInteger   UiInteger;
-typedef struct UiString    UiString;
-typedef struct UiText      UiText;
-typedef struct UiList      UiList;
+typedef struct UiInteger    UiInteger;
+typedef struct UiString     UiString;
+typedef struct UiText       UiText;
+typedef struct UiList       UiList;
 
 /* private types */
-typedef struct UiContext   UiContext;
-typedef struct UiContainer UiContainer;
+typedef struct UiContext    UiContext;
+typedef struct UiContainer  UiContainer;
 
 typedef struct UiTabbedPane UiTabbedPane;
 
@@ -186,11 +186,13 @@
     char* (*get)(UiText*);
     char* (*getsubstr)(UiText*, int, int); // text, begin, end
     void  (*insert)(UiText*, int, char*);
+    void  (*setposition)(UiText*,int);
     int   (*position)(UiText*);
     void  (*selection)(UiText*, int*, int*); // text, begin, end
     int   (*length)(UiText*);
     void  (*remove)(UiText*, int, int); // text, begin, end
     char  *value;
+    int   pos;
     void  *obj;
     void  *undomgr;
     // TODO: replace, ...

mercurial