2 * Copyright © 2012 Openismus GmbH
3 * Copyright © 2012 Intel Corporation
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
29 #include <linux/input.h>
33 #include "text-client-protocol.h"
35 static const char *font_name = "sans-serif";
36 static int font_size = 14;
39 cairo_glyph_t *glyphs;
41 cairo_text_cluster_t *clusters;
43 cairo_text_cluster_flags_t cluster_flags;
44 cairo_scaled_font_t *font;
48 struct widget *widget;
49 struct window *window;
55 uint32_t preedit_cursor;
56 struct text_model *model;
57 struct text_layout *layout;
61 struct text_model_factory *text_model_factory;
62 struct display *display;
63 struct window *window;
64 struct widget *widget;
65 struct text_entry *entry;
66 struct text_entry *editor;
69 static struct text_layout *
70 text_layout_create(void)
72 struct text_layout *layout;
73 cairo_surface_t *surface;
76 layout = malloc(sizeof *layout);
80 layout->glyphs = NULL;
81 layout->num_glyphs = 0;
83 layout->clusters = NULL;
84 layout->num_clusters = 0;
86 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
87 cr = cairo_create(surface);
88 cairo_set_font_size(cr, font_size);
89 cairo_select_font_face(cr, font_name, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
90 layout->font = cairo_get_scaled_font(cr);
91 cairo_scaled_font_reference(layout->font);
94 cairo_surface_destroy(surface);
100 text_layout_destroy(struct text_layout *layout)
103 cairo_glyph_free(layout->glyphs);
105 if (layout->clusters)
106 cairo_text_cluster_free(layout->clusters);
108 cairo_scaled_font_destroy(layout->font);
114 text_layout_set_text(struct text_layout *layout,
118 cairo_glyph_free(layout->glyphs);
120 if (layout->clusters)
121 cairo_text_cluster_free(layout->clusters);
123 layout->glyphs = NULL;
124 layout->num_glyphs = 0;
125 layout->clusters = NULL;
126 layout->num_clusters = 0;
128 cairo_scaled_font_text_to_glyphs(layout->font, 0, 0, text, -1,
129 &layout->glyphs, &layout->num_glyphs,
130 &layout->clusters, &layout->num_clusters,
131 &layout->cluster_flags);
135 text_layout_draw(struct text_layout *layout, cairo_t *cr)
138 cairo_set_scaled_font(cr, layout->font);
139 cairo_show_glyphs(cr, layout->glyphs, layout->num_glyphs);
144 text_layout_extents(struct text_layout *layout, cairo_text_extents_t *extents)
146 cairo_scaled_font_glyph_extents(layout->font,
147 layout->glyphs, layout->num_glyphs,
152 text_layout_xy_to_index(struct text_layout *layout, double x, double y)
154 cairo_text_extents_t extents;
158 if (layout->num_glyphs == 0)
161 cairo_scaled_font_glyph_extents(layout->font,
162 layout->glyphs, layout->num_glyphs,
168 for (i = 0; i < layout->num_glyphs - 1; ++i) {
169 d = layout->glyphs[i + 1].x - layout->glyphs[i].x;
170 if (x < layout->glyphs[i].x + d/2)
174 d = extents.width - layout->glyphs[layout->num_glyphs - 1].x;
175 if (x < layout->glyphs[layout->num_glyphs - 1].x + d/2)
176 return layout->num_glyphs - 1;
178 return layout->num_glyphs;
182 text_layout_index_to_pos(struct text_layout *layout, uint32_t index, cairo_rectangle_t *pos)
184 cairo_text_extents_t extents;
189 cairo_scaled_font_glyph_extents(layout->font,
190 layout->glyphs, layout->num_glyphs,
193 if ((int)index >= layout->num_glyphs) {
194 pos->x = extents.x_advance;
195 pos->y = layout->num_glyphs ? layout->glyphs[layout->num_glyphs - 1].y : 0;
197 pos->height = extents.height;
201 pos->x = layout->glyphs[index].x;
202 pos->y = layout->glyphs[index].y;
203 pos->width = (int)index < layout->num_glyphs - 1 ? layout->glyphs[index + 1].x : extents.x_advance - pos->x;
204 pos->height = extents.height;
208 text_layout_get_cursor_pos(struct text_layout *layout, int index, cairo_rectangle_t *pos)
210 text_layout_index_to_pos(layout, index, pos);
214 static void text_entry_redraw_handler(struct widget *widget, void *data);
215 static void text_entry_button_handler(struct widget *widget,
216 struct input *input, uint32_t time,
218 enum wl_pointer_button_state state, void *data);
219 static void text_entry_insert_at_cursor(struct text_entry *entry, const char *text);
220 static void text_entry_set_preedit(struct text_entry *entry,
221 const char *preedit_text,
223 static void text_entry_delete_text(struct text_entry *entry,
224 uint32_t index, uint32_t length);
225 static void text_entry_delete_selected_text(struct text_entry *entry);
228 text_model_commit_string(void *data,
229 struct text_model *text_model,
233 struct text_entry *entry = data;
235 if (index > strlen(text))
236 fprintf(stderr, "Invalid cursor index %d\n", index);
238 text_entry_delete_selected_text(entry);
239 text_entry_insert_at_cursor(entry, text);
241 widget_schedule_redraw(entry->widget);
245 text_model_preedit_string(void *data,
246 struct text_model *text_model,
250 struct text_entry *entry = data;
252 if (index > strlen(text)) {
253 fprintf(stderr, "Invalid cursor index %d\n", index);
254 index = strlen(text);
257 text_entry_delete_selected_text(entry);
258 text_entry_set_preedit(entry, text, index);
260 widget_schedule_redraw(entry->widget);
264 text_model_delete_surrounding_text(void *data,
265 struct text_model *text_model,
269 struct text_entry *entry = data;
270 uint32_t cursor_index = index + entry->cursor;
272 if (cursor_index > strlen(entry->text)) {
273 fprintf(stderr, "Invalid cursor index %d\n", index);
277 if (cursor_index + length > strlen(entry->text)) {
278 fprintf(stderr, "Invalid length %d\n", length);
285 text_entry_delete_text(entry, cursor_index, length);
289 text_model_preedit_styling(void *data,
290 struct text_model *text_model)
295 text_model_key(void *data,
296 struct text_model *text_model,
300 struct text_entry *entry = data;
301 const char *state_label;
302 const char *key_label = "released";
304 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
305 state_label = "pressed";
312 case XKB_KEY_KP_Enter:
316 if (entry->cursor > 0) {
318 entry->anchor = entry->cursor;
319 widget_schedule_redraw(entry->widget);
323 if (entry->cursor < strlen(entry->text)) {
325 entry->anchor = entry->cursor;
326 widget_schedule_redraw(entry->widget);
330 key_label = "Unknown";
333 fprintf(stderr, "%s key was %s.\n", key_label, state_label);
337 text_model_selection_replacement(void *data,
338 struct text_model *text_model)
343 text_model_direction(void *data,
344 struct text_model *text_model)
349 text_model_locale(void *data,
350 struct text_model *text_model)
355 text_model_enter(void *data,
356 struct text_model *text_model,
357 struct wl_surface *surface)
359 struct text_entry *entry = data;
361 if (surface != window_get_wl_surface(entry->window))
366 widget_schedule_redraw(entry->widget);
370 text_model_leave(void *data,
371 struct text_model *text_model)
373 struct text_entry *entry = data;
377 widget_schedule_redraw(entry->widget);
380 static const struct text_model_listener text_model_listener = {
381 text_model_commit_string,
382 text_model_preedit_string,
383 text_model_delete_surrounding_text,
384 text_model_preedit_styling,
386 text_model_selection_replacement,
387 text_model_direction,
393 static struct text_entry*
394 text_entry_create(struct editor *editor, const char *text)
396 struct text_entry *entry;
398 entry = malloc(sizeof *entry);
400 entry->widget = widget_add_widget(editor->widget, entry);
401 entry->window = editor->window;
402 entry->text = strdup(text);
404 entry->cursor = strlen(text);
405 entry->anchor = entry->cursor;
406 entry->preedit_text = NULL;
407 entry->preedit_cursor = 0;
408 entry->model = text_model_factory_create_text_model(editor->text_model_factory);
409 text_model_add_listener(entry->model, &text_model_listener, entry);
411 entry->layout = text_layout_create();
412 text_layout_set_text(entry->layout, entry->text);
414 widget_set_redraw_handler(entry->widget, text_entry_redraw_handler);
415 widget_set_button_handler(entry->widget, text_entry_button_handler);
421 text_entry_destroy(struct text_entry *entry)
423 widget_destroy(entry->widget);
424 text_model_destroy(entry->model);
425 text_layout_destroy(entry->layout);
431 redraw_handler(struct widget *widget, void *data)
433 struct editor *editor = data;
434 cairo_surface_t *surface;
435 struct rectangle allocation;
438 surface = window_get_surface(editor->window);
439 widget_get_allocation(editor->widget, &allocation);
441 cr = cairo_create(surface);
442 cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
445 cairo_translate(cr, allocation.x, allocation.y);
447 /* Draw background */
448 cairo_push_group(cr);
449 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
450 cairo_set_source_rgba(cr, 1, 1, 1, 1);
451 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
454 cairo_pop_group_to_source(cr);
458 cairo_surface_destroy(surface);
462 text_entry_allocate(struct text_entry *entry, int32_t x, int32_t y,
463 int32_t width, int32_t height)
465 widget_set_allocation(entry->widget, x, y, width, height);
469 resize_handler(struct widget *widget,
470 int32_t width, int32_t height, void *data)
472 struct editor *editor = data;
473 struct rectangle allocation;
475 widget_get_allocation(editor->widget, &allocation);
477 text_entry_allocate(editor->entry,
478 allocation.x + 20, allocation.y + 20,
479 width - 40, height / 2 - 40);
480 text_entry_allocate(editor->editor,
481 allocation.x + 20, allocation.y + height / 2 + 20,
482 width - 40, height / 2 - 40);
486 text_entry_activate(struct text_entry *entry,
487 struct wl_seat *seat)
489 struct wl_surface *surface = window_get_wl_surface(entry->window);
491 text_model_activate(entry->model,
497 text_entry_deactivate(struct text_entry *entry,
498 struct wl_seat *seat)
500 text_model_deactivate(entry->model,
505 text_entry_update_layout(struct text_entry *entry)
509 assert(((unsigned int)entry->cursor) <= strlen(entry->text) +
510 (entry->preedit_text ? strlen(entry->preedit_text) : 0));
512 if (!entry->preedit_text) {
513 text_layout_set_text(entry->layout, entry->text);
517 text = malloc(strlen(entry->text) + strlen(entry->preedit_text) + 1);
518 strncpy(text, entry->text, entry->cursor);
519 strcpy(text + entry->cursor, entry->preedit_text);
520 strcpy(text + entry->cursor + strlen(entry->preedit_text),
521 entry->text + entry->cursor);
523 text_layout_set_text(entry->layout, text);
526 widget_schedule_redraw(entry->widget);
528 text_model_set_surrounding_text(entry->model,
535 text_entry_insert_at_cursor(struct text_entry *entry, const char *text)
537 char *new_text = malloc(strlen(entry->text) + strlen(text) + 1);
539 strncpy(new_text, entry->text, entry->cursor);
540 strcpy(new_text + entry->cursor, text);
541 strcpy(new_text + entry->cursor + strlen(text),
542 entry->text + entry->cursor);
545 entry->text = new_text;
546 entry->cursor += strlen(text);
547 entry->anchor += strlen(text);
549 text_entry_update_layout(entry);
553 text_entry_set_preedit(struct text_entry *entry,
554 const char *preedit_text,
557 if (entry->preedit_text) {
558 free(entry->preedit_text);
559 entry->preedit_text = NULL;
560 entry->preedit_cursor = 0;
566 entry->preedit_text = strdup(preedit_text);
567 entry->preedit_cursor = preedit_cursor;
569 text_entry_update_layout(entry);
573 text_entry_set_cursor_position(struct text_entry *entry,
574 int32_t x, int32_t y)
576 entry->cursor = text_layout_xy_to_index(entry->layout, x, y);
578 text_model_reset(entry->model);
580 if (entry->cursor >= entry->preedit_cursor) {
581 entry->cursor -= entry->preedit_cursor;
584 text_entry_update_layout(entry);
586 widget_schedule_redraw(entry->widget);
590 text_entry_set_anchor_position(struct text_entry *entry,
591 int32_t x, int32_t y)
593 entry->anchor = text_layout_xy_to_index(entry->layout, x, y);
595 widget_schedule_redraw(entry->widget);
599 text_entry_delete_text(struct text_entry *entry,
600 uint32_t index, uint32_t length)
602 if (entry->cursor > index)
603 entry->cursor -= length;
605 entry->anchor = entry->cursor;
607 entry->text[index] = '\0';
608 strcat(entry->text, entry->text + index + length);
610 text_entry_update_layout(entry);
612 widget_schedule_redraw(entry->widget);
616 text_entry_delete_selected_text(struct text_entry *entry)
618 uint32_t start_index = entry->anchor < entry->cursor ? entry->anchor : entry->cursor;
619 uint32_t end_index = entry->anchor < entry->cursor ? entry->cursor : entry->anchor;
621 if (entry->anchor == entry->cursor)
624 text_entry_delete_text(entry, start_index, end_index - start_index);
626 entry->anchor = entry->cursor;
630 text_entry_draw_selection(struct text_entry *entry, cairo_t *cr)
632 cairo_text_extents_t extents;
633 uint32_t start_index = entry->anchor < entry->cursor ? entry->anchor : entry->cursor;
634 uint32_t end_index = entry->anchor < entry->cursor ? entry->cursor : entry->anchor;
635 cairo_rectangle_t start;
636 cairo_rectangle_t end;
638 if (entry->anchor == entry->cursor)
641 text_layout_extents(entry->layout, &extents);
643 text_layout_index_to_pos(entry->layout, start_index, &start);
644 text_layout_index_to_pos(entry->layout, end_index, &end);
648 cairo_set_source_rgba(cr, 0.3, 0.3, 1.0, 0.5);
650 start.x, extents.y_bearing + extents.height + 2,
651 end.x - start.x, -extents.height - 4);
655 start.x, extents.y_bearing + extents.height,
656 end.x - start.x, -extents.height);
658 cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0);
659 text_layout_draw(entry->layout, cr);
665 text_entry_draw_cursor(struct text_entry *entry, cairo_t *cr)
667 cairo_text_extents_t extents;
668 cairo_rectangle_t cursor_pos;
670 text_layout_extents(entry->layout, &extents);
671 text_layout_get_cursor_pos(entry->layout,
672 entry->cursor + entry->preedit_cursor,
675 cairo_set_line_width(cr, 1.0);
676 cairo_move_to(cr, cursor_pos.x, extents.y_bearing + extents.height + 2);
677 cairo_line_to(cr, cursor_pos.x, extents.y_bearing - 2);
682 text_entry_draw_preedit(struct text_entry *entry, cairo_t *cr)
684 cairo_text_extents_t extents;
685 cairo_rectangle_t start;
686 cairo_rectangle_t end;
688 if (!entry->preedit_text)
691 text_layout_extents(entry->layout, &extents);
693 text_layout_index_to_pos(entry->layout, entry->cursor, &start);
694 text_layout_index_to_pos(entry->layout,
695 entry->cursor + strlen(entry->preedit_text),
700 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
709 static const int text_offset_left = 10;
712 text_entry_redraw_handler(struct widget *widget, void *data)
714 struct text_entry *entry = data;
715 cairo_surface_t *surface;
716 struct rectangle allocation;
719 surface = window_get_surface(entry->window);
720 widget_get_allocation(entry->widget, &allocation);
722 cr = cairo_create(surface);
723 cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
726 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
728 cairo_push_group(cr);
729 cairo_translate(cr, allocation.x, allocation.y);
731 cairo_set_source_rgba(cr, 1, 1, 1, 1);
732 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
735 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
738 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
739 cairo_set_line_width (cr, 3);
740 cairo_set_source_rgba(cr, 0, 0, 1, 1.0);
744 cairo_set_source_rgba(cr, 0, 0, 0, 1);
746 cairo_translate(cr, text_offset_left, allocation.height / 2);
747 text_layout_draw(entry->layout, cr);
749 text_entry_draw_selection(entry, cr);
751 text_entry_draw_cursor(entry, cr);
753 text_entry_draw_preedit(entry, cr);
755 cairo_pop_group_to_source(cr);
759 cairo_surface_destroy(surface);
763 text_entry_motion_handler(struct widget *widget,
764 struct input *input, uint32_t time,
765 float x, float y, void *data)
767 struct text_entry *entry = data;
768 struct rectangle allocation;
770 widget_get_allocation(entry->widget, &allocation);
772 text_entry_set_cursor_position(entry,
773 x - allocation.x - text_offset_left,
774 y - allocation.y - text_offset_left);
780 text_entry_button_handler(struct widget *widget,
781 struct input *input, uint32_t time,
783 enum wl_pointer_button_state state, void *data)
785 struct text_entry *entry = data;
786 struct rectangle allocation;
789 widget_get_allocation(entry->widget, &allocation);
790 input_get_position(input, &x, &y);
792 if (button != BTN_LEFT) {
796 text_entry_set_cursor_position(entry,
797 x - allocation.x - text_offset_left,
798 y - allocation.y - text_offset_left);
800 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
801 struct wl_seat *seat = input_get_seat(input);
803 text_entry_activate(entry, seat);
805 text_entry_set_anchor_position(entry,
806 x - allocation.x - text_offset_left,
807 y - allocation.y - text_offset_left);
809 widget_set_motion_handler(entry->widget, text_entry_motion_handler);
811 widget_set_motion_handler(entry->widget, NULL);
816 editor_button_handler(struct widget *widget,
817 struct input *input, uint32_t time,
819 enum wl_pointer_button_state state, void *data)
821 struct editor *editor = data;
823 if (button != BTN_LEFT) {
827 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
828 struct wl_seat *seat = input_get_seat(input);
830 text_entry_deactivate(editor->entry, seat);
831 text_entry_deactivate(editor->editor, seat);
836 global_handler(struct display *display, uint32_t name,
837 const char *interface, uint32_t version, void *data)
839 struct editor *editor = data;
841 if (!strcmp(interface, "text_model_factory")) {
842 editor->text_model_factory =
843 display_bind(display, name,
844 &text_model_factory_interface, 1);
849 main(int argc, char *argv[])
851 struct editor editor;
853 editor.display = display_create(argc, argv);
854 if (editor.display == NULL) {
855 fprintf(stderr, "failed to create display: %m\n");
859 display_set_user_data(editor.display, &editor);
860 display_set_global_handler(editor.display, global_handler);
862 editor.window = window_create(editor.display);
863 editor.widget = frame_create(editor.window, &editor);
865 editor.entry = text_entry_create(&editor, "Entry");
866 editor.editor = text_entry_create(&editor, "Editor");
867 text_entry_set_preedit(editor.editor, "preedit", strlen("preedit"));
869 window_set_title(editor.window, "Text Editor");
871 widget_set_redraw_handler(editor.widget, redraw_handler);
872 widget_set_resize_handler(editor.widget, resize_handler);
873 widget_set_button_handler(editor.widget, editor_button_handler);
875 window_schedule_resize(editor.window, 500, 400);
877 display_run(editor.display);
879 text_entry_destroy(editor.entry);
880 text_entry_destroy(editor.editor);