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);
237 index = strlen(text);
240 text_entry_delete_selected_text(entry);
241 text_entry_insert_at_cursor(entry, text);
243 widget_schedule_redraw(entry->widget);
247 text_model_preedit_string(void *data,
248 struct text_model *text_model,
252 struct text_entry *entry = data;
254 if (index > strlen(text)) {
255 fprintf(stderr, "Invalid cursor index %d\n", index);
256 index = strlen(text);
259 text_entry_delete_selected_text(entry);
260 text_entry_set_preedit(entry, text, index);
262 widget_schedule_redraw(entry->widget);
266 text_model_delete_surrounding_text(void *data,
267 struct text_model *text_model,
271 struct text_entry *entry = data;
272 uint32_t cursor_index = index + entry->cursor;
274 if (cursor_index > strlen(entry->text)) {
275 fprintf(stderr, "Invalid cursor index %d\n", index);
279 if (cursor_index + length > strlen(entry->text)) {
280 fprintf(stderr, "Invalid length %d\n", length);
287 text_entry_delete_text(entry, cursor_index, length);
291 text_model_preedit_styling(void *data,
292 struct text_model *text_model)
297 text_model_key(void *data,
298 struct text_model *text_model,
302 struct text_entry *entry = data;
303 const char *state_label;
304 const char *key_label = "released";
306 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
307 state_label = "pressed";
314 case XKB_KEY_KP_Enter:
318 if (entry->cursor > 0) {
320 entry->anchor = entry->cursor;
321 widget_schedule_redraw(entry->widget);
325 if (entry->cursor < strlen(entry->text)) {
327 entry->anchor = entry->cursor;
328 widget_schedule_redraw(entry->widget);
332 key_label = "Unknown";
335 fprintf(stderr, "%s key was %s.\n", key_label, state_label);
339 text_model_selection_replacement(void *data,
340 struct text_model *text_model)
345 text_model_direction(void *data,
346 struct text_model *text_model)
351 text_model_locale(void *data,
352 struct text_model *text_model)
357 text_model_enter(void *data,
358 struct text_model *text_model,
359 struct wl_surface *surface)
361 struct text_entry *entry = data;
363 if (surface != window_get_wl_surface(entry->window))
368 widget_schedule_redraw(entry->widget);
372 text_model_leave(void *data,
373 struct text_model *text_model)
375 struct text_entry *entry = data;
379 widget_schedule_redraw(entry->widget);
382 static const struct text_model_listener text_model_listener = {
383 text_model_commit_string,
384 text_model_preedit_string,
385 text_model_delete_surrounding_text,
386 text_model_preedit_styling,
388 text_model_selection_replacement,
389 text_model_direction,
395 static struct text_entry*
396 text_entry_create(struct editor *editor, const char *text)
398 struct text_entry *entry;
400 entry = malloc(sizeof *entry);
402 entry->widget = widget_add_widget(editor->widget, entry);
403 entry->window = editor->window;
404 entry->text = strdup(text);
406 entry->cursor = strlen(text);
407 entry->anchor = entry->cursor;
408 entry->preedit_text = NULL;
409 entry->preedit_cursor = 0;
410 entry->model = text_model_factory_create_text_model(editor->text_model_factory);
411 text_model_add_listener(entry->model, &text_model_listener, entry);
413 entry->layout = text_layout_create();
414 text_layout_set_text(entry->layout, entry->text);
416 widget_set_redraw_handler(entry->widget, text_entry_redraw_handler);
417 widget_set_button_handler(entry->widget, text_entry_button_handler);
423 text_entry_destroy(struct text_entry *entry)
425 widget_destroy(entry->widget);
426 text_model_destroy(entry->model);
427 text_layout_destroy(entry->layout);
433 redraw_handler(struct widget *widget, void *data)
435 struct editor *editor = data;
436 cairo_surface_t *surface;
437 struct rectangle allocation;
440 surface = window_get_surface(editor->window);
441 widget_get_allocation(editor->widget, &allocation);
443 cr = cairo_create(surface);
444 cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
447 cairo_translate(cr, allocation.x, allocation.y);
449 /* Draw background */
450 cairo_push_group(cr);
451 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
452 cairo_set_source_rgba(cr, 1, 1, 1, 1);
453 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
456 cairo_pop_group_to_source(cr);
460 cairo_surface_destroy(surface);
464 text_entry_allocate(struct text_entry *entry, int32_t x, int32_t y,
465 int32_t width, int32_t height)
467 widget_set_allocation(entry->widget, x, y, width, height);
471 resize_handler(struct widget *widget,
472 int32_t width, int32_t height, void *data)
474 struct editor *editor = data;
475 struct rectangle allocation;
477 widget_get_allocation(editor->widget, &allocation);
479 text_entry_allocate(editor->entry,
480 allocation.x + 20, allocation.y + 20,
481 width - 40, height / 2 - 40);
482 text_entry_allocate(editor->editor,
483 allocation.x + 20, allocation.y + height / 2 + 20,
484 width - 40, height / 2 - 40);
488 text_entry_activate(struct text_entry *entry,
489 struct wl_seat *seat)
491 struct wl_surface *surface = window_get_wl_surface(entry->window);
493 text_model_activate(entry->model,
499 text_entry_deactivate(struct text_entry *entry,
500 struct wl_seat *seat)
502 text_model_deactivate(entry->model,
507 text_entry_update_layout(struct text_entry *entry)
511 assert(((unsigned int)entry->cursor) <= strlen(entry->text) +
512 (entry->preedit_text ? strlen(entry->preedit_text) : 0));
514 if (!entry->preedit_text) {
515 text_layout_set_text(entry->layout, entry->text);
519 text = malloc(strlen(entry->text) + strlen(entry->preedit_text) + 1);
520 strncpy(text, entry->text, entry->cursor);
521 strcpy(text + entry->cursor, entry->preedit_text);
522 strcpy(text + entry->cursor + strlen(entry->preedit_text),
523 entry->text + entry->cursor);
525 text_layout_set_text(entry->layout, text);
528 widget_schedule_redraw(entry->widget);
530 text_model_set_surrounding_text(entry->model,
537 text_entry_insert_at_cursor(struct text_entry *entry, const char *text)
539 char *new_text = malloc(strlen(entry->text) + strlen(text) + 1);
541 strncpy(new_text, entry->text, entry->cursor);
542 strcpy(new_text + entry->cursor, text);
543 strcpy(new_text + entry->cursor + strlen(text),
544 entry->text + entry->cursor);
547 entry->text = new_text;
548 entry->cursor += strlen(text);
549 entry->anchor += strlen(text);
551 text_entry_update_layout(entry);
555 text_entry_set_preedit(struct text_entry *entry,
556 const char *preedit_text,
559 if (entry->preedit_text) {
560 free(entry->preedit_text);
561 entry->preedit_text = NULL;
562 entry->preedit_cursor = 0;
568 entry->preedit_text = strdup(preedit_text);
569 entry->preedit_cursor = preedit_cursor;
571 text_entry_update_layout(entry);
575 text_entry_set_cursor_position(struct text_entry *entry,
576 int32_t x, int32_t y)
578 entry->cursor = text_layout_xy_to_index(entry->layout, x, y);
580 text_model_reset(entry->model);
582 if (entry->cursor >= entry->preedit_cursor) {
583 entry->cursor -= entry->preedit_cursor;
586 text_entry_update_layout(entry);
588 widget_schedule_redraw(entry->widget);
592 text_entry_set_anchor_position(struct text_entry *entry,
593 int32_t x, int32_t y)
595 entry->anchor = text_layout_xy_to_index(entry->layout, x, y);
597 widget_schedule_redraw(entry->widget);
601 text_entry_delete_text(struct text_entry *entry,
602 uint32_t index, uint32_t length)
604 if (entry->cursor > index)
605 entry->cursor -= length;
607 entry->anchor = entry->cursor;
609 entry->text[index] = '\0';
610 strcat(entry->text, entry->text + index + length);
612 text_entry_update_layout(entry);
614 widget_schedule_redraw(entry->widget);
618 text_entry_delete_selected_text(struct text_entry *entry)
620 uint32_t start_index = entry->anchor < entry->cursor ? entry->anchor : entry->cursor;
621 uint32_t end_index = entry->anchor < entry->cursor ? entry->cursor : entry->anchor;
623 if (entry->anchor == entry->cursor)
626 text_entry_delete_text(entry, start_index, end_index - start_index);
628 entry->anchor = entry->cursor;
632 text_entry_draw_selection(struct text_entry *entry, cairo_t *cr)
634 cairo_text_extents_t extents;
635 uint32_t start_index = entry->anchor < entry->cursor ? entry->anchor : entry->cursor;
636 uint32_t end_index = entry->anchor < entry->cursor ? entry->cursor : entry->anchor;
637 cairo_rectangle_t start;
638 cairo_rectangle_t end;
640 if (entry->anchor == entry->cursor)
643 text_layout_extents(entry->layout, &extents);
645 text_layout_index_to_pos(entry->layout, start_index, &start);
646 text_layout_index_to_pos(entry->layout, end_index, &end);
650 cairo_set_source_rgba(cr, 0.3, 0.3, 1.0, 0.5);
652 start.x, extents.y_bearing + extents.height + 2,
653 end.x - start.x, -extents.height - 4);
657 start.x, extents.y_bearing + extents.height,
658 end.x - start.x, -extents.height);
660 cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0);
661 text_layout_draw(entry->layout, cr);
667 text_entry_draw_cursor(struct text_entry *entry, cairo_t *cr)
669 cairo_text_extents_t extents;
670 cairo_rectangle_t cursor_pos;
672 text_layout_extents(entry->layout, &extents);
673 text_layout_get_cursor_pos(entry->layout,
674 entry->cursor + entry->preedit_cursor,
677 cairo_set_line_width(cr, 1.0);
678 cairo_move_to(cr, cursor_pos.x, extents.y_bearing + extents.height + 2);
679 cairo_line_to(cr, cursor_pos.x, extents.y_bearing - 2);
684 text_entry_draw_preedit(struct text_entry *entry, cairo_t *cr)
686 cairo_text_extents_t extents;
687 cairo_rectangle_t start;
688 cairo_rectangle_t end;
690 if (!entry->preedit_text)
693 text_layout_extents(entry->layout, &extents);
695 text_layout_index_to_pos(entry->layout, entry->cursor, &start);
696 text_layout_index_to_pos(entry->layout,
697 entry->cursor + strlen(entry->preedit_text),
702 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
711 static const int text_offset_left = 10;
714 text_entry_redraw_handler(struct widget *widget, void *data)
716 struct text_entry *entry = data;
717 cairo_surface_t *surface;
718 struct rectangle allocation;
721 surface = window_get_surface(entry->window);
722 widget_get_allocation(entry->widget, &allocation);
724 cr = cairo_create(surface);
725 cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
728 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
730 cairo_push_group(cr);
731 cairo_translate(cr, allocation.x, allocation.y);
733 cairo_set_source_rgba(cr, 1, 1, 1, 1);
734 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
737 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
740 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
741 cairo_set_line_width (cr, 3);
742 cairo_set_source_rgba(cr, 0, 0, 1, 1.0);
746 cairo_set_source_rgba(cr, 0, 0, 0, 1);
748 cairo_translate(cr, text_offset_left, allocation.height / 2);
749 text_layout_draw(entry->layout, cr);
751 text_entry_draw_selection(entry, cr);
753 text_entry_draw_cursor(entry, cr);
755 text_entry_draw_preedit(entry, cr);
757 cairo_pop_group_to_source(cr);
761 cairo_surface_destroy(surface);
765 text_entry_motion_handler(struct widget *widget,
766 struct input *input, uint32_t time,
767 float x, float y, void *data)
769 struct text_entry *entry = data;
770 struct rectangle allocation;
772 widget_get_allocation(entry->widget, &allocation);
774 text_entry_set_cursor_position(entry,
775 x - allocation.x - text_offset_left,
776 y - allocation.y - text_offset_left);
782 text_entry_button_handler(struct widget *widget,
783 struct input *input, uint32_t time,
785 enum wl_pointer_button_state state, void *data)
787 struct text_entry *entry = data;
788 struct rectangle allocation;
791 widget_get_allocation(entry->widget, &allocation);
792 input_get_position(input, &x, &y);
794 if (button != BTN_LEFT) {
798 text_entry_set_cursor_position(entry,
799 x - allocation.x - text_offset_left,
800 y - allocation.y - text_offset_left);
802 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
803 struct wl_seat *seat = input_get_seat(input);
805 text_entry_activate(entry, seat);
807 text_entry_set_anchor_position(entry,
808 x - allocation.x - text_offset_left,
809 y - allocation.y - text_offset_left);
811 widget_set_motion_handler(entry->widget, text_entry_motion_handler);
813 widget_set_motion_handler(entry->widget, NULL);
818 editor_button_handler(struct widget *widget,
819 struct input *input, uint32_t time,
821 enum wl_pointer_button_state state, void *data)
823 struct editor *editor = data;
825 if (button != BTN_LEFT) {
829 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
830 struct wl_seat *seat = input_get_seat(input);
832 text_entry_deactivate(editor->entry, seat);
833 text_entry_deactivate(editor->editor, seat);
838 global_handler(struct display *display, uint32_t name,
839 const char *interface, uint32_t version, void *data)
841 struct editor *editor = data;
843 if (!strcmp(interface, "text_model_factory")) {
844 editor->text_model_factory =
845 display_bind(display, name,
846 &text_model_factory_interface, 1);
851 main(int argc, char *argv[])
853 struct editor editor;
855 editor.display = display_create(argc, argv);
856 if (editor.display == NULL) {
857 fprintf(stderr, "failed to create display: %m\n");
861 display_set_user_data(editor.display, &editor);
862 display_set_global_handler(editor.display, global_handler);
864 editor.window = window_create(editor.display);
865 editor.widget = frame_create(editor.window, &editor);
867 editor.entry = text_entry_create(&editor, "Entry");
868 editor.editor = text_entry_create(&editor, "Editor");
869 text_entry_set_preedit(editor.editor, "preedit", strlen("preedit"));
871 window_set_title(editor.window, "Text Editor");
873 widget_set_redraw_handler(editor.widget, redraw_handler);
874 widget_set_resize_handler(editor.widget, resize_handler);
875 widget_set_button_handler(editor.widget, editor_button_handler);
877 window_schedule_resize(editor.window, 500, 400);
879 display_run(editor.display);
881 text_entry_destroy(editor.entry);
882 text_entry_destroy(editor.editor);