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_enter(void *data,
350 struct text_model *text_model,
351 struct wl_surface *surface)
353 struct text_entry *entry = data;
355 if (surface != window_get_wl_surface(entry->window))
360 widget_schedule_redraw(entry->widget);
364 text_model_leave(void *data,
365 struct text_model *text_model)
367 struct text_entry *entry = data;
371 widget_schedule_redraw(entry->widget);
374 static const struct text_model_listener text_model_listener = {
375 text_model_commit_string,
376 text_model_preedit_string,
377 text_model_delete_surrounding_text,
378 text_model_preedit_styling,
380 text_model_selection_replacement,
381 text_model_direction,
387 static struct text_entry*
388 text_entry_create(struct editor *editor, const char *text)
390 struct text_entry *entry;
392 entry = malloc(sizeof *entry);
394 entry->widget = widget_add_widget(editor->widget, entry);
395 entry->window = editor->window;
396 entry->text = strdup(text);
398 entry->cursor = strlen(text);
399 entry->anchor = entry->cursor;
400 entry->preedit_text = NULL;
401 entry->preedit_cursor = 0;
402 entry->model = text_model_factory_create_text_model(editor->text_model_factory);
403 text_model_add_listener(entry->model, &text_model_listener, entry);
405 entry->layout = text_layout_create();
406 text_layout_set_text(entry->layout, entry->text);
408 widget_set_redraw_handler(entry->widget, text_entry_redraw_handler);
409 widget_set_button_handler(entry->widget, text_entry_button_handler);
415 text_entry_destroy(struct text_entry *entry)
417 widget_destroy(entry->widget);
418 text_model_destroy(entry->model);
419 text_layout_destroy(entry->layout);
425 redraw_handler(struct widget *widget, void *data)
427 struct editor *editor = data;
428 cairo_surface_t *surface;
429 struct rectangle allocation;
432 surface = window_get_surface(editor->window);
433 widget_get_allocation(editor->widget, &allocation);
435 cr = cairo_create(surface);
436 cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
439 cairo_translate(cr, allocation.x, allocation.y);
441 /* Draw background */
442 cairo_push_group(cr);
443 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
444 cairo_set_source_rgba(cr, 1, 1, 1, 1);
445 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
448 cairo_pop_group_to_source(cr);
452 cairo_surface_destroy(surface);
456 text_entry_allocate(struct text_entry *entry, int32_t x, int32_t y,
457 int32_t width, int32_t height)
459 widget_set_allocation(entry->widget, x, y, width, height);
463 resize_handler(struct widget *widget,
464 int32_t width, int32_t height, void *data)
466 struct editor *editor = data;
467 struct rectangle allocation;
469 widget_get_allocation(editor->widget, &allocation);
471 text_entry_allocate(editor->entry,
472 allocation.x + 20, allocation.y + 20,
473 width - 40, height / 2 - 40);
474 text_entry_allocate(editor->editor,
475 allocation.x + 20, allocation.y + height / 2 + 20,
476 width - 40, height / 2 - 40);
480 text_entry_activate(struct text_entry *entry,
481 struct wl_seat *seat)
483 struct wl_surface *surface = window_get_wl_surface(entry->window);
485 text_model_activate(entry->model,
491 text_entry_deactivate(struct text_entry *entry,
492 struct wl_seat *seat)
494 text_model_deactivate(entry->model,
499 text_entry_update_layout(struct text_entry *entry)
503 assert(((unsigned int)entry->cursor) <= strlen(entry->text) +
504 (entry->preedit_text ? strlen(entry->preedit_text) : 0));
506 if (!entry->preedit_text) {
507 text_layout_set_text(entry->layout, entry->text);
511 text = malloc(strlen(entry->text) + strlen(entry->preedit_text) + 1);
512 strncpy(text, entry->text, entry->cursor);
513 strcpy(text + entry->cursor, entry->preedit_text);
514 strcpy(text + entry->cursor + strlen(entry->preedit_text),
515 entry->text + entry->cursor);
517 text_layout_set_text(entry->layout, text);
520 widget_schedule_redraw(entry->widget);
522 text_model_set_surrounding_text(entry->model,
529 text_entry_insert_at_cursor(struct text_entry *entry, const char *text)
531 char *new_text = malloc(strlen(entry->text) + strlen(text) + 1);
533 strncpy(new_text, entry->text, entry->cursor);
534 strcpy(new_text + entry->cursor, text);
535 strcpy(new_text + entry->cursor + strlen(text),
536 entry->text + entry->cursor);
539 entry->text = new_text;
540 entry->cursor += strlen(text);
541 entry->anchor += strlen(text);
543 text_entry_update_layout(entry);
547 text_entry_set_preedit(struct text_entry *entry,
548 const char *preedit_text,
551 if (entry->preedit_text) {
552 free(entry->preedit_text);
553 entry->preedit_text = NULL;
554 entry->preedit_cursor = 0;
560 entry->preedit_text = strdup(preedit_text);
561 entry->preedit_cursor = preedit_cursor;
563 text_entry_update_layout(entry);
567 text_entry_set_cursor_position(struct text_entry *entry,
568 int32_t x, int32_t y)
570 entry->cursor = text_layout_xy_to_index(entry->layout, x, y);
572 text_model_reset(entry->model);
574 if (entry->cursor >= entry->preedit_cursor) {
575 entry->cursor -= entry->preedit_cursor;
578 text_entry_update_layout(entry);
580 widget_schedule_redraw(entry->widget);
584 text_entry_set_anchor_position(struct text_entry *entry,
585 int32_t x, int32_t y)
587 entry->anchor = text_layout_xy_to_index(entry->layout, x, y);
589 widget_schedule_redraw(entry->widget);
593 text_entry_delete_text(struct text_entry *entry,
594 uint32_t index, uint32_t length)
596 if (entry->cursor > index)
597 entry->cursor -= length;
599 entry->anchor = entry->cursor;
601 entry->text[index] = '\0';
602 strcat(entry->text, entry->text + index + length);
604 text_entry_update_layout(entry);
606 widget_schedule_redraw(entry->widget);
610 text_entry_delete_selected_text(struct text_entry *entry)
612 uint32_t start_index = entry->anchor < entry->cursor ? entry->anchor : entry->cursor;
613 uint32_t end_index = entry->anchor < entry->cursor ? entry->cursor : entry->anchor;
615 if (entry->anchor == entry->cursor)
618 text_entry_delete_text(entry, start_index, end_index - start_index);
620 entry->anchor = entry->cursor;
624 text_entry_draw_selection(struct text_entry *entry, cairo_t *cr)
626 cairo_text_extents_t extents;
627 uint32_t start_index = entry->anchor < entry->cursor ? entry->anchor : entry->cursor;
628 uint32_t end_index = entry->anchor < entry->cursor ? entry->cursor : entry->anchor;
629 cairo_rectangle_t start;
630 cairo_rectangle_t end;
632 if (entry->anchor == entry->cursor)
635 text_layout_extents(entry->layout, &extents);
637 text_layout_index_to_pos(entry->layout, start_index, &start);
638 text_layout_index_to_pos(entry->layout, end_index, &end);
642 cairo_set_source_rgba(cr, 0.0, 0.0, 1.0, 1.0);
644 start.x, extents.y_bearing + extents.height + 2,
645 end.x - start.x, -extents.height - 4);
649 start.x, extents.y_bearing + extents.height,
650 end.x - start.x, -extents.height);
652 cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0);
653 text_layout_draw(entry->layout, cr);
659 text_entry_draw_cursor(struct text_entry *entry, cairo_t *cr)
661 cairo_text_extents_t extents;
662 cairo_rectangle_t cursor_pos;
664 text_layout_extents(entry->layout, &extents);
665 text_layout_get_cursor_pos(entry->layout,
666 entry->cursor + entry->preedit_cursor,
669 cairo_set_line_width(cr, 1.0);
670 cairo_move_to(cr, cursor_pos.x, extents.y_bearing + extents.height + 2);
671 cairo_line_to(cr, cursor_pos.x, extents.y_bearing - 2);
676 text_entry_draw_preedit(struct text_entry *entry, cairo_t *cr)
678 cairo_text_extents_t extents;
679 cairo_rectangle_t start;
680 cairo_rectangle_t end;
682 if (!entry->preedit_text)
685 text_layout_extents(entry->layout, &extents);
687 text_layout_index_to_pos(entry->layout, entry->cursor, &start);
688 text_layout_index_to_pos(entry->layout,
689 entry->cursor + strlen(entry->preedit_text),
694 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
704 text_entry_redraw_handler(struct widget *widget, void *data)
706 struct text_entry *entry = data;
707 cairo_surface_t *surface;
708 struct rectangle allocation;
711 surface = window_get_surface(entry->window);
712 widget_get_allocation(entry->widget, &allocation);
714 cr = cairo_create(surface);
715 cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
718 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
720 cairo_push_group(cr);
721 cairo_translate(cr, allocation.x, allocation.y);
723 cairo_set_source_rgba(cr, 1, 1, 1, 1);
724 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
727 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
730 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
731 cairo_set_line_width (cr, 3);
732 cairo_set_source_rgba(cr, 0, 0, 1, 1.0);
736 cairo_set_source_rgba(cr, 0, 0, 0, 1);
738 cairo_translate(cr, 10, allocation.height / 2);
739 text_layout_draw(entry->layout, cr);
741 text_entry_draw_selection(entry, cr);
743 text_entry_draw_cursor(entry, cr);
745 text_entry_draw_preedit(entry, cr);
747 cairo_pop_group_to_source(cr);
751 cairo_surface_destroy(surface);
755 text_entry_motion_handler(struct widget *widget,
756 struct input *input, uint32_t time,
757 float x, float y, void *data)
759 struct text_entry *entry = data;
760 struct rectangle allocation;
762 widget_get_allocation(entry->widget, &allocation);
764 text_entry_set_cursor_position(entry,
772 text_entry_button_handler(struct widget *widget,
773 struct input *input, uint32_t time,
775 enum wl_pointer_button_state state, void *data)
777 struct text_entry *entry = data;
778 struct rectangle allocation;
781 widget_get_allocation(entry->widget, &allocation);
782 input_get_position(input, &x, &y);
784 if (button != BTN_LEFT) {
788 text_entry_set_cursor_position(entry,
792 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
793 struct wl_seat *seat = input_get_seat(input);
795 text_entry_activate(entry, seat);
797 text_entry_set_anchor_position(entry,
801 widget_set_motion_handler(entry->widget, text_entry_motion_handler);
803 widget_set_motion_handler(entry->widget, NULL);
808 editor_button_handler(struct widget *widget,
809 struct input *input, uint32_t time,
811 enum wl_pointer_button_state state, void *data)
813 struct editor *editor = data;
815 if (button != BTN_LEFT) {
819 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
820 struct wl_seat *seat = input_get_seat(input);
822 text_entry_deactivate(editor->entry, seat);
823 text_entry_deactivate(editor->editor, seat);
828 global_handler(struct wl_display *display, uint32_t id,
829 const char *interface, uint32_t version, void *data)
831 struct editor *editor = data;
833 if (!strcmp(interface, "text_model_factory")) {
834 editor->text_model_factory = wl_display_bind(display, id,
835 &text_model_factory_interface);
840 main(int argc, char *argv[])
842 struct editor editor;
844 editor.display = display_create(argc, argv);
845 if (editor.display == NULL) {
846 fprintf(stderr, "failed to create display: %m\n");
849 wl_display_add_global_listener(display_get_display(editor.display),
850 global_handler, &editor);
853 editor.window = window_create(editor.display);
854 editor.widget = frame_create(editor.window, &editor);
856 editor.entry = text_entry_create(&editor, "Entry");
857 editor.editor = text_entry_create(&editor, "Editor");
858 text_entry_set_preedit(editor.editor, "preedit", strlen("preedit"));
860 window_set_title(editor.window, "Text Editor");
862 widget_set_redraw_handler(editor.widget, redraw_handler);
863 widget_set_resize_handler(editor.widget, resize_handler);
864 widget_set_button_handler(editor.widget, editor_button_handler);
866 window_schedule_resize(editor.window, 500, 400);
868 display_run(editor.display);
870 text_entry_destroy(editor.entry);
871 text_entry_destroy(editor.editor);