Update snapshot
[profile/ivi/weston.git] / clients / editor.c
index b6a1742..d185137 100644 (file)
@@ -153,18 +153,26 @@ text_layout_xy_to_index(struct text_layout *layout, double x, double y)
 {
        cairo_text_extents_t extents;
        int i;
+       double d;
+
+       if (layout->num_glyphs == 0)
+               return 0;
 
        cairo_scaled_font_glyph_extents(layout->font,
                                        layout->glyphs, layout->num_glyphs,
                                        &extents);
 
-       for (i = 1; i < layout->num_glyphs; i++) {
-               if (layout->glyphs[i].x >= x) {
-                       return i - 1;
-               }
+       if (x < 0)
+               return 0;
+
+       for (i = 0; i < layout->num_glyphs - 1; ++i) {
+               d = layout->glyphs[i + 1].x - layout->glyphs[i].x;
+               if (x < layout->glyphs[i].x + d/2)
+                       return i;
        }
 
-       if (x >= layout->glyphs[layout->num_glyphs - 1].x && x < extents.width)
+       d = extents.width - layout->glyphs[layout->num_glyphs - 1].x;
+       if (x < layout->glyphs[layout->num_glyphs - 1].x + d/2)
                return layout->num_glyphs - 1;
 
        return layout->num_glyphs;
@@ -212,6 +220,9 @@ static void text_entry_insert_at_cursor(struct text_entry *entry, const char *te
 static void text_entry_set_preedit(struct text_entry *entry,
                                   const char *preedit_text,
                                   int preedit_cursor);
+static void text_entry_delete_text(struct text_entry *entry,
+                                  uint32_t index, uint32_t length);
+static void text_entry_delete_selected_text(struct text_entry *entry);
 
 static void
 text_model_commit_string(void *data,
@@ -221,11 +232,10 @@ text_model_commit_string(void *data,
 {
        struct text_entry *entry = data;
 
-       if (index > strlen(text)) {
+       if (index > strlen(text))
                fprintf(stderr, "Invalid cursor index %d\n", index);
-               index = strlen(text);
-       }
 
+       text_entry_delete_selected_text(entry);
        text_entry_insert_at_cursor(entry, text);
 
        widget_schedule_redraw(entry->widget);
@@ -244,12 +254,38 @@ text_model_preedit_string(void *data,
                index = strlen(text);
        }
 
+       text_entry_delete_selected_text(entry);
        text_entry_set_preedit(entry, text, index);
 
        widget_schedule_redraw(entry->widget);
 }
 
 static void
+text_model_delete_surrounding_text(void *data,
+                                  struct text_model *text_model,
+                                  int32_t index,
+                                  uint32_t length)
+{
+       struct text_entry *entry = data;
+       uint32_t cursor_index = index + entry->cursor;
+
+       if (cursor_index > strlen(entry->text)) {
+               fprintf(stderr, "Invalid cursor index %d\n", index);
+               return;
+       }
+
+       if (cursor_index + length > strlen(entry->text)) {
+               fprintf(stderr, "Invalid length %d\n", length);
+               return;
+       }
+
+       if (length == 0)
+               return;
+
+       text_entry_delete_text(entry, cursor_index, length);
+}
+
+static void
 text_model_preedit_styling(void *data,
                           struct text_model *text_model)
 {
@@ -257,8 +293,44 @@ text_model_preedit_styling(void *data,
 
 static void
 text_model_key(void *data,
-              struct text_model *text_model)
+              struct text_model *text_model,
+               uint32_t key,
+               uint32_t state)
 {
+       struct text_entry *entry = data;
+       const char *state_label;
+       const char *key_label = "released";
+
+       if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
+               state_label = "pressed";
+       }
+
+       switch (key) {
+               case XKB_KEY_Tab:
+                       key_label = "Tab";
+                       break;
+               case XKB_KEY_KP_Enter:
+                       key_label = "Enter";
+                       break;
+               case XKB_KEY_Left:
+                       if (entry->cursor > 0) {
+                               entry->cursor--;
+                               entry->anchor = entry->cursor;
+                               widget_schedule_redraw(entry->widget);
+                       }
+                       break;
+               case XKB_KEY_Right:
+                       if (entry->cursor < strlen(entry->text)) {
+                               entry->cursor++;
+                               entry->anchor = entry->cursor;
+                               widget_schedule_redraw(entry->widget);
+                       }
+                       break;
+               default:
+                       key_label = "Unknown";
+       }
+
+       fprintf(stderr, "%s key was %s.\n", key_label, state_label);
 }
 
 static void
@@ -280,19 +352,23 @@ text_model_locale(void *data,
 }
 
 static void
-text_model_activated(void *data,
-                    struct text_model *text_model)
+text_model_enter(void *data,
+                struct text_model *text_model,
+                struct wl_surface *surface)
 {
        struct text_entry *entry = data;
 
+       if (surface != window_get_wl_surface(entry->window))
+               return;
+
        entry->active = 1;
 
        widget_schedule_redraw(entry->widget);
 }
 
 static void
-text_model_deactivated(void *data,
-                      struct text_model *text_model)
+text_model_leave(void *data,
+                struct text_model *text_model)
 {
        struct text_entry *entry = data;
 
@@ -304,13 +380,14 @@ text_model_deactivated(void *data,
 static const struct text_model_listener text_model_listener = {
        text_model_commit_string,
        text_model_preedit_string,
+       text_model_delete_surrounding_text,
        text_model_preedit_styling,
        text_model_key,
        text_model_selection_replacement,
        text_model_direction,
        text_model_locale,
-       text_model_activated,
-       text_model_deactivated
+       text_model_enter,
+       text_model_leave
 };
 
 static struct text_entry*
@@ -429,7 +506,8 @@ text_entry_update_layout(struct text_entry *entry)
 {
        char *text;
 
-       assert(((unsigned int)entry->cursor) <= strlen(entry->text));
+       assert(((unsigned int)entry->cursor) <= strlen(entry->text) +
+              (entry->preedit_text ? strlen(entry->preedit_text) : 0));
 
        if (!entry->preedit_text) {
                text_layout_set_text(entry->layout, entry->text);
@@ -497,6 +575,8 @@ text_entry_set_cursor_position(struct text_entry *entry,
 {
        entry->cursor = text_layout_xy_to_index(entry->layout, x, y);
 
+       text_model_reset(entry->model);
+
        if (entry->cursor >= entry->preedit_cursor) {
                entry->cursor -= entry->preedit_cursor;
        }
@@ -516,6 +596,37 @@ text_entry_set_anchor_position(struct text_entry *entry,
 }
 
 static void
+text_entry_delete_text(struct text_entry *entry,
+                      uint32_t index, uint32_t length)
+{
+       if (entry->cursor > index)
+               entry->cursor -= length;
+
+       entry->anchor = entry->cursor;
+
+       entry->text[index] = '\0';
+       strcat(entry->text, entry->text + index + length);
+
+       text_entry_update_layout(entry);
+
+       widget_schedule_redraw(entry->widget);
+}
+
+static void
+text_entry_delete_selected_text(struct text_entry *entry)
+{
+       uint32_t start_index = entry->anchor < entry->cursor ? entry->anchor : entry->cursor;
+       uint32_t end_index = entry->anchor < entry->cursor ? entry->cursor : entry->anchor;
+
+       if (entry->anchor == entry->cursor)
+               return;
+
+       text_entry_delete_text(entry, start_index, end_index - start_index);
+
+       entry->anchor = entry->cursor;
+}
+
+static void
 text_entry_draw_selection(struct text_entry *entry, cairo_t *cr)
 {
        cairo_text_extents_t extents;
@@ -534,7 +645,7 @@ text_entry_draw_selection(struct text_entry *entry, cairo_t *cr)
 
        cairo_save (cr);
 
-       cairo_set_source_rgba(cr, 0.0, 0.0, 1.0, 1.0);
+       cairo_set_source_rgba(cr, 0.3, 0.3, 1.0, 0.5);
        cairo_rectangle(cr,
                        start.x, extents.y_bearing + extents.height + 2,
                        end.x - start.x, -extents.height - 4);
@@ -595,6 +706,8 @@ text_entry_draw_preedit(struct text_entry *entry, cairo_t *cr)
        cairo_restore (cr);
 }
 
+static const int text_offset_left = 10;
+
 static void
 text_entry_redraw_handler(struct widget *widget, void *data)
 {
@@ -630,7 +743,7 @@ text_entry_redraw_handler(struct widget *widget, void *data)
 
        cairo_set_source_rgba(cr, 0, 0, 0, 1);
 
-       cairo_translate(cr, 10, allocation.height / 2);
+       cairo_translate(cr, text_offset_left, allocation.height / 2);
        text_layout_draw(entry->layout, cr);
 
        text_entry_draw_selection(entry, cr);
@@ -657,8 +770,8 @@ text_entry_motion_handler(struct widget *widget,
        widget_get_allocation(entry->widget, &allocation);
 
        text_entry_set_cursor_position(entry,
-                                      x - allocation.x,
-                                      y - allocation.y);
+                                      x - allocation.x - text_offset_left,
+                                      y - allocation.y - text_offset_left);
 
        return CURSOR_IBEAM;
 }
@@ -681,8 +794,8 @@ text_entry_button_handler(struct widget *widget,
        }
 
        text_entry_set_cursor_position(entry,
-                                      x - allocation.x,
-                                      y - allocation.y);
+                                      x - allocation.x - text_offset_left,
+                                      y - allocation.y - text_offset_left);
 
        if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
                struct wl_seat *seat = input_get_seat(input);
@@ -690,8 +803,8 @@ text_entry_button_handler(struct widget *widget,
                text_entry_activate(entry, seat);
 
                text_entry_set_anchor_position(entry,
-                                              x - allocation.x,
-                                              y - allocation.y);
+                                              x - allocation.x - text_offset_left,
+                                              y - allocation.y - text_offset_left);
 
                widget_set_motion_handler(entry->widget, text_entry_motion_handler);
        } else {
@@ -720,14 +833,15 @@ editor_button_handler(struct widget *widget,
 }
 
 static void
-global_handler(struct wl_display *display, uint32_t id,
+global_handler(struct display *display, uint32_t name,
               const char *interface, uint32_t version, void *data)
 {
        struct editor *editor = data;
 
        if (!strcmp(interface, "text_model_factory")) {
-               editor->text_model_factory = wl_display_bind(display, id,
-                                                            &text_model_factory_interface);
+               editor->text_model_factory =
+                       display_bind(display, name,
+                                    &text_model_factory_interface, 1);
        }
 }
 
@@ -741,9 +855,9 @@ main(int argc, char *argv[])
                fprintf(stderr, "failed to create display: %m\n");
                return -1;
        }
-       wl_display_add_global_listener(display_get_display(editor.display),
-                                      global_handler, &editor);
 
+       display_set_user_data(editor.display, &editor);
+       display_set_global_handler(editor.display, global_handler);
 
        editor.window = window_create(editor.display);
        editor.widget = frame_create(editor.window, &editor);