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;
157 cairo_scaled_font_glyph_extents(layout->font,
158 layout->glyphs, layout->num_glyphs,
161 for (i = 1; i < layout->num_glyphs; i++) {
162 if (layout->glyphs[i].x >= x) {
167 if (x >= layout->glyphs[layout->num_glyphs - 1].x && x < extents.width)
168 return layout->num_glyphs - 1;
170 return layout->num_glyphs;
174 text_layout_index_to_pos(struct text_layout *layout, uint32_t index, cairo_rectangle_t *pos)
176 cairo_text_extents_t extents;
181 cairo_scaled_font_glyph_extents(layout->font,
182 layout->glyphs, layout->num_glyphs,
185 if ((int)index >= layout->num_glyphs) {
186 pos->x = extents.x_advance;
187 pos->y = layout->num_glyphs ? layout->glyphs[layout->num_glyphs - 1].y : 0;
189 pos->height = extents.height;
193 pos->x = layout->glyphs[index].x;
194 pos->y = layout->glyphs[index].y;
195 pos->width = (int)index < layout->num_glyphs - 1 ? layout->glyphs[index + 1].x : extents.x_advance - pos->x;
196 pos->height = extents.height;
200 text_layout_get_cursor_pos(struct text_layout *layout, int index, cairo_rectangle_t *pos)
202 text_layout_index_to_pos(layout, index, pos);
206 static void text_entry_redraw_handler(struct widget *widget, void *data);
207 static void text_entry_button_handler(struct widget *widget,
208 struct input *input, uint32_t time,
210 enum wl_pointer_button_state state, void *data);
211 static void text_entry_insert_at_cursor(struct text_entry *entry, const char *text);
212 static void text_entry_set_preedit(struct text_entry *entry,
213 const char *preedit_text,
215 static void text_entry_delete_text(struct text_entry *entry,
216 uint32_t index, uint32_t length);
217 static void text_entry_delete_selected_text(struct text_entry *entry);
220 text_model_commit_string(void *data,
221 struct text_model *text_model,
225 struct text_entry *entry = data;
227 if (index > strlen(text)) {
228 fprintf(stderr, "Invalid cursor index %d\n", index);
229 index = strlen(text);
232 text_entry_delete_selected_text(entry);
233 text_entry_insert_at_cursor(entry, text);
235 widget_schedule_redraw(entry->widget);
239 text_model_preedit_string(void *data,
240 struct text_model *text_model,
244 struct text_entry *entry = data;
246 if (index > strlen(text)) {
247 fprintf(stderr, "Invalid cursor index %d\n", index);
248 index = strlen(text);
251 text_entry_delete_selected_text(entry);
252 text_entry_set_preedit(entry, text, index);
254 widget_schedule_redraw(entry->widget);
258 text_model_delete_surrounding_text(void *data,
259 struct text_model *text_model,
263 struct text_entry *entry = data;
264 uint32_t cursor_index = index + entry->cursor;
266 if (cursor_index > strlen(entry->text)) {
267 fprintf(stderr, "Invalid cursor index %d\n", index);
271 if (cursor_index + length > strlen(entry->text)) {
272 fprintf(stderr, "Invalid length %d\n", length);
279 text_entry_delete_text(entry, cursor_index, length);
283 text_model_preedit_styling(void *data,
284 struct text_model *text_model)
289 text_model_key(void *data,
290 struct text_model *text_model,
294 struct text_entry *entry = data;
295 const char *state_label;
296 const char *key_label = "released";
298 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
299 state_label = "pressed";
306 case XKB_KEY_KP_Enter:
310 if (entry->cursor > 0) {
312 entry->anchor = entry->cursor;
313 widget_schedule_redraw(entry->widget);
317 if (entry->cursor < strlen(entry->text)) {
319 entry->anchor = entry->cursor;
320 widget_schedule_redraw(entry->widget);
324 key_label = "Unknown";
327 fprintf(stderr, "%s key was %s.\n", key_label, state_label);
331 text_model_selection_replacement(void *data,
332 struct text_model *text_model)
337 text_model_direction(void *data,
338 struct text_model *text_model)
343 text_model_locale(void *data,
344 struct text_model *text_model)
349 text_model_activated(void *data,
350 struct text_model *text_model)
352 struct text_entry *entry = data;
356 widget_schedule_redraw(entry->widget);
360 text_model_deactivated(void *data,
361 struct text_model *text_model)
363 struct text_entry *entry = data;
367 widget_schedule_redraw(entry->widget);
370 static const struct text_model_listener text_model_listener = {
371 text_model_commit_string,
372 text_model_preedit_string,
373 text_model_delete_surrounding_text,
374 text_model_preedit_styling,
376 text_model_selection_replacement,
377 text_model_direction,
379 text_model_activated,
380 text_model_deactivated
383 static struct text_entry*
384 text_entry_create(struct editor *editor, const char *text)
386 struct text_entry *entry;
388 entry = malloc(sizeof *entry);
390 entry->widget = widget_add_widget(editor->widget, entry);
391 entry->window = editor->window;
392 entry->text = strdup(text);
394 entry->cursor = strlen(text);
395 entry->anchor = entry->cursor;
396 entry->preedit_text = NULL;
397 entry->preedit_cursor = 0;
398 entry->model = text_model_factory_create_text_model(editor->text_model_factory);
399 text_model_add_listener(entry->model, &text_model_listener, entry);
401 entry->layout = text_layout_create();
402 text_layout_set_text(entry->layout, entry->text);
404 widget_set_redraw_handler(entry->widget, text_entry_redraw_handler);
405 widget_set_button_handler(entry->widget, text_entry_button_handler);
411 text_entry_destroy(struct text_entry *entry)
413 widget_destroy(entry->widget);
414 text_model_destroy(entry->model);
415 text_layout_destroy(entry->layout);
421 redraw_handler(struct widget *widget, void *data)
423 struct editor *editor = data;
424 cairo_surface_t *surface;
425 struct rectangle allocation;
428 surface = window_get_surface(editor->window);
429 widget_get_allocation(editor->widget, &allocation);
431 cr = cairo_create(surface);
432 cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
435 cairo_translate(cr, allocation.x, allocation.y);
437 /* Draw background */
438 cairo_push_group(cr);
439 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
440 cairo_set_source_rgba(cr, 1, 1, 1, 1);
441 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
444 cairo_pop_group_to_source(cr);
448 cairo_surface_destroy(surface);
452 text_entry_allocate(struct text_entry *entry, int32_t x, int32_t y,
453 int32_t width, int32_t height)
455 widget_set_allocation(entry->widget, x, y, width, height);
459 resize_handler(struct widget *widget,
460 int32_t width, int32_t height, void *data)
462 struct editor *editor = data;
463 struct rectangle allocation;
465 widget_get_allocation(editor->widget, &allocation);
467 text_entry_allocate(editor->entry,
468 allocation.x + 20, allocation.y + 20,
469 width - 40, height / 2 - 40);
470 text_entry_allocate(editor->editor,
471 allocation.x + 20, allocation.y + height / 2 + 20,
472 width - 40, height / 2 - 40);
476 text_entry_activate(struct text_entry *entry,
477 struct wl_seat *seat)
479 struct wl_surface *surface = window_get_wl_surface(entry->window);
481 text_model_activate(entry->model,
487 text_entry_deactivate(struct text_entry *entry,
488 struct wl_seat *seat)
490 text_model_deactivate(entry->model,
495 text_entry_update_layout(struct text_entry *entry)
499 assert(((unsigned int)entry->cursor) <= strlen(entry->text));
501 if (!entry->preedit_text) {
502 text_layout_set_text(entry->layout, entry->text);
506 text = malloc(strlen(entry->text) + strlen(entry->preedit_text) + 1);
507 strncpy(text, entry->text, entry->cursor);
508 strcpy(text + entry->cursor, entry->preedit_text);
509 strcpy(text + entry->cursor + strlen(entry->preedit_text),
510 entry->text + entry->cursor);
512 text_layout_set_text(entry->layout, text);
515 widget_schedule_redraw(entry->widget);
517 text_model_set_surrounding_text(entry->model,
524 text_entry_insert_at_cursor(struct text_entry *entry, const char *text)
526 char *new_text = malloc(strlen(entry->text) + strlen(text) + 1);
528 strncpy(new_text, entry->text, entry->cursor);
529 strcpy(new_text + entry->cursor, text);
530 strcpy(new_text + entry->cursor + strlen(text),
531 entry->text + entry->cursor);
534 entry->text = new_text;
535 entry->cursor += strlen(text);
536 entry->anchor += strlen(text);
538 text_entry_update_layout(entry);
542 text_entry_set_preedit(struct text_entry *entry,
543 const char *preedit_text,
546 if (entry->preedit_text) {
547 free(entry->preedit_text);
548 entry->preedit_text = NULL;
549 entry->preedit_cursor = 0;
555 entry->preedit_text = strdup(preedit_text);
556 entry->preedit_cursor = preedit_cursor;
558 text_entry_update_layout(entry);
562 text_entry_set_cursor_position(struct text_entry *entry,
563 int32_t x, int32_t y)
565 entry->cursor = text_layout_xy_to_index(entry->layout, x, y);
567 text_model_reset(entry->model);
569 if (entry->cursor >= entry->preedit_cursor) {
570 entry->cursor -= entry->preedit_cursor;
573 text_entry_update_layout(entry);
575 widget_schedule_redraw(entry->widget);
579 text_entry_set_anchor_position(struct text_entry *entry,
580 int32_t x, int32_t y)
582 entry->anchor = text_layout_xy_to_index(entry->layout, x, y);
584 widget_schedule_redraw(entry->widget);
588 text_entry_delete_text(struct text_entry *entry,
589 uint32_t index, uint32_t length)
591 if (entry->cursor > index)
592 entry->cursor -= length;
594 entry->text[index] = '\0';
595 strcat(entry->text, entry->text + index + length);
597 text_entry_update_layout(entry);
599 widget_schedule_redraw(entry->widget);
603 text_entry_delete_selected_text(struct text_entry *entry)
605 uint32_t start_index = entry->anchor < entry->cursor ? entry->anchor : entry->cursor;
606 uint32_t end_index = entry->anchor < entry->cursor ? entry->cursor : entry->anchor;
608 if (entry->anchor == entry->cursor)
611 text_entry_delete_text(entry, start_index, end_index - start_index);
613 entry->anchor = entry->cursor;
617 text_entry_draw_selection(struct text_entry *entry, cairo_t *cr)
619 cairo_text_extents_t extents;
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;
622 cairo_rectangle_t start;
623 cairo_rectangle_t end;
625 if (entry->anchor == entry->cursor)
628 text_layout_extents(entry->layout, &extents);
630 text_layout_index_to_pos(entry->layout, start_index, &start);
631 text_layout_index_to_pos(entry->layout, end_index, &end);
635 cairo_set_source_rgba(cr, 0.0, 0.0, 1.0, 1.0);
637 start.x, extents.y_bearing + extents.height + 2,
638 end.x - start.x, -extents.height - 4);
642 start.x, extents.y_bearing + extents.height,
643 end.x - start.x, -extents.height);
645 cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0);
646 text_layout_draw(entry->layout, cr);
652 text_entry_draw_cursor(struct text_entry *entry, cairo_t *cr)
654 cairo_text_extents_t extents;
655 cairo_rectangle_t cursor_pos;
657 text_layout_extents(entry->layout, &extents);
658 text_layout_get_cursor_pos(entry->layout,
659 entry->cursor + entry->preedit_cursor,
662 cairo_set_line_width(cr, 1.0);
663 cairo_move_to(cr, cursor_pos.x, extents.y_bearing + extents.height + 2);
664 cairo_line_to(cr, cursor_pos.x, extents.y_bearing - 2);
669 text_entry_draw_preedit(struct text_entry *entry, cairo_t *cr)
671 cairo_text_extents_t extents;
672 cairo_rectangle_t start;
673 cairo_rectangle_t end;
675 if (!entry->preedit_text)
678 text_layout_extents(entry->layout, &extents);
680 text_layout_index_to_pos(entry->layout, entry->cursor, &start);
681 text_layout_index_to_pos(entry->layout,
682 entry->cursor + strlen(entry->preedit_text),
687 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
697 text_entry_redraw_handler(struct widget *widget, void *data)
699 struct text_entry *entry = data;
700 cairo_surface_t *surface;
701 struct rectangle allocation;
704 surface = window_get_surface(entry->window);
705 widget_get_allocation(entry->widget, &allocation);
707 cr = cairo_create(surface);
708 cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
711 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
713 cairo_push_group(cr);
714 cairo_translate(cr, allocation.x, allocation.y);
716 cairo_set_source_rgba(cr, 1, 1, 1, 1);
717 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
720 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
723 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
724 cairo_set_line_width (cr, 3);
725 cairo_set_source_rgba(cr, 0, 0, 1, 1.0);
729 cairo_set_source_rgba(cr, 0, 0, 0, 1);
731 cairo_translate(cr, 10, allocation.height / 2);
732 text_layout_draw(entry->layout, cr);
734 text_entry_draw_selection(entry, cr);
736 text_entry_draw_cursor(entry, cr);
738 text_entry_draw_preedit(entry, cr);
740 cairo_pop_group_to_source(cr);
744 cairo_surface_destroy(surface);
748 text_entry_motion_handler(struct widget *widget,
749 struct input *input, uint32_t time,
750 float x, float y, void *data)
752 struct text_entry *entry = data;
753 struct rectangle allocation;
755 widget_get_allocation(entry->widget, &allocation);
757 text_entry_set_cursor_position(entry,
765 text_entry_button_handler(struct widget *widget,
766 struct input *input, uint32_t time,
768 enum wl_pointer_button_state state, void *data)
770 struct text_entry *entry = data;
771 struct rectangle allocation;
774 widget_get_allocation(entry->widget, &allocation);
775 input_get_position(input, &x, &y);
777 if (button != BTN_LEFT) {
781 text_entry_set_cursor_position(entry,
785 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
786 struct wl_seat *seat = input_get_seat(input);
788 text_entry_activate(entry, seat);
790 text_entry_set_anchor_position(entry,
794 widget_set_motion_handler(entry->widget, text_entry_motion_handler);
796 widget_set_motion_handler(entry->widget, NULL);
801 editor_button_handler(struct widget *widget,
802 struct input *input, uint32_t time,
804 enum wl_pointer_button_state state, void *data)
806 struct editor *editor = data;
808 if (button != BTN_LEFT) {
812 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
813 struct wl_seat *seat = input_get_seat(input);
815 text_entry_deactivate(editor->entry, seat);
816 text_entry_deactivate(editor->editor, seat);
821 global_handler(struct wl_display *display, uint32_t id,
822 const char *interface, uint32_t version, void *data)
824 struct editor *editor = data;
826 if (!strcmp(interface, "text_model_factory")) {
827 editor->text_model_factory = wl_display_bind(display, id,
828 &text_model_factory_interface);
833 main(int argc, char *argv[])
835 struct editor editor;
837 editor.display = display_create(argc, argv);
838 if (editor.display == NULL) {
839 fprintf(stderr, "failed to create display: %m\n");
842 wl_display_add_global_listener(display_get_display(editor.display),
843 global_handler, &editor);
846 editor.window = window_create(editor.display);
847 editor.widget = frame_create(editor.window, &editor);
849 editor.entry = text_entry_create(&editor, "Entry");
850 editor.editor = text_entry_create(&editor, "Editor");
851 text_entry_set_preedit(editor.editor, "preedit", strlen("preedit"));
853 window_set_title(editor.window, "Text Editor");
855 widget_set_redraw_handler(editor.widget, redraw_handler);
856 widget_set_resize_handler(editor.widget, resize_handler);
857 widget_set_button_handler(editor.widget, editor_button_handler);
859 window_schedule_resize(editor.window, 500, 400);
861 display_run(editor.display);
863 text_entry_destroy(editor.entry);
864 text_entry_destroy(editor.editor);