X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=clients%2Feditor.c;h=d185137fce5b3dcc0d51073761bf0c2483506b65;hb=b6eae9faf70e3d113fd379eb44229928a80ee3b4;hp=0ed217f2238715c708068a8f531f82791b5f4537;hpb=e202bae9d3736341dc2d0c832797aaf45e1ef0b0;p=profile%2Fivi%2Fweston.git diff --git a/clients/editor.c b/clients/editor.c index 0ed217f..d185137 100644 --- a/clients/editor.c +++ b/clients/editor.c @@ -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; @@ -214,6 +222,7 @@ static void text_entry_set_preedit(struct text_entry *entry, 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, @@ -223,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); @@ -246,6 +254,7 @@ 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); @@ -284,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 @@ -307,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; @@ -337,8 +386,8 @@ static const struct text_model_listener text_model_listener = { 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* @@ -457,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); @@ -525,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; } @@ -550,6 +602,8 @@ text_entry_delete_text(struct text_entry *entry, if (entry->cursor > index) entry->cursor -= length; + entry->anchor = entry->cursor; + entry->text[index] = '\0'; strcat(entry->text, entry->text + index + length); @@ -559,6 +613,20 @@ text_entry_delete_text(struct text_entry *entry, } 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; @@ -577,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); @@ -638,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) { @@ -673,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); @@ -700,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; } @@ -724,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); @@ -733,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 { @@ -763,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); } } @@ -784,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);