{
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;
{
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);
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);
cairo_restore (cr);
}
+static const int text_offset_left = 10;
+
static void
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);
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;
}
}
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);
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 {
}
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);
}
}
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);