Update snapshot
[profile/ivi/weston.git] / clients / editor.c
index 6b67dd3..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;
@@ -224,10 +232,8 @@ 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);
@@ -639,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);
@@ -700,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)
 {
@@ -735,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);
@@ -762,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;
 }
@@ -786,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);
@@ -795,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 {
@@ -825,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);
        }
 }
 
@@ -846,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);