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);
219 text_model_commit_string(void *data,
220 struct text_model *text_model,
224 struct text_entry *entry = data;
226 if (index > strlen(text)) {
227 fprintf(stderr, "Invalid cursor index %d\n", index);
228 index = strlen(text);
231 text_entry_insert_at_cursor(entry, text);
233 widget_schedule_redraw(entry->widget);
237 text_model_preedit_string(void *data,
238 struct text_model *text_model,
242 struct text_entry *entry = data;
244 if (index > strlen(text)) {
245 fprintf(stderr, "Invalid cursor index %d\n", index);
246 index = strlen(text);
249 text_entry_set_preedit(entry, text, index);
251 widget_schedule_redraw(entry->widget);
255 text_model_delete_surrounding_text(void *data,
256 struct text_model *text_model,
260 struct text_entry *entry = data;
261 uint32_t cursor_index = index + entry->cursor;
263 if (cursor_index > strlen(entry->text)) {
264 fprintf(stderr, "Invalid cursor index %d\n", index);
268 if (cursor_index + length > strlen(entry->text)) {
269 fprintf(stderr, "Invalid length %d\n", length);
276 text_entry_delete_text(entry, cursor_index, length);
280 text_model_preedit_styling(void *data,
281 struct text_model *text_model)
286 text_model_key(void *data,
287 struct text_model *text_model,
291 const char *state_label;
292 const char *key_label;
294 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
295 state_label = "pressed";
297 state_label = "released";
304 case XKB_KEY_KP_Enter:
308 key_label = "Unknown";
311 fprintf(stderr, "%s key was %s.\n", key_label, state_label);
315 text_model_selection_replacement(void *data,
316 struct text_model *text_model)
321 text_model_direction(void *data,
322 struct text_model *text_model)
327 text_model_locale(void *data,
328 struct text_model *text_model)
333 text_model_activated(void *data,
334 struct text_model *text_model)
336 struct text_entry *entry = data;
340 widget_schedule_redraw(entry->widget);
344 text_model_deactivated(void *data,
345 struct text_model *text_model)
347 struct text_entry *entry = data;
351 widget_schedule_redraw(entry->widget);
354 static const struct text_model_listener text_model_listener = {
355 text_model_commit_string,
356 text_model_preedit_string,
357 text_model_delete_surrounding_text,
358 text_model_preedit_styling,
360 text_model_selection_replacement,
361 text_model_direction,
363 text_model_activated,
364 text_model_deactivated
367 static struct text_entry*
368 text_entry_create(struct editor *editor, const char *text)
370 struct text_entry *entry;
372 entry = malloc(sizeof *entry);
374 entry->widget = widget_add_widget(editor->widget, entry);
375 entry->window = editor->window;
376 entry->text = strdup(text);
378 entry->cursor = strlen(text);
379 entry->anchor = entry->cursor;
380 entry->preedit_text = NULL;
381 entry->preedit_cursor = 0;
382 entry->model = text_model_factory_create_text_model(editor->text_model_factory);
383 text_model_add_listener(entry->model, &text_model_listener, entry);
385 entry->layout = text_layout_create();
386 text_layout_set_text(entry->layout, entry->text);
388 widget_set_redraw_handler(entry->widget, text_entry_redraw_handler);
389 widget_set_button_handler(entry->widget, text_entry_button_handler);
395 text_entry_destroy(struct text_entry *entry)
397 widget_destroy(entry->widget);
398 text_model_destroy(entry->model);
399 text_layout_destroy(entry->layout);
405 redraw_handler(struct widget *widget, void *data)
407 struct editor *editor = data;
408 cairo_surface_t *surface;
409 struct rectangle allocation;
412 surface = window_get_surface(editor->window);
413 widget_get_allocation(editor->widget, &allocation);
415 cr = cairo_create(surface);
416 cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
419 cairo_translate(cr, allocation.x, allocation.y);
421 /* Draw background */
422 cairo_push_group(cr);
423 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
424 cairo_set_source_rgba(cr, 1, 1, 1, 1);
425 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
428 cairo_pop_group_to_source(cr);
432 cairo_surface_destroy(surface);
436 text_entry_allocate(struct text_entry *entry, int32_t x, int32_t y,
437 int32_t width, int32_t height)
439 widget_set_allocation(entry->widget, x, y, width, height);
443 resize_handler(struct widget *widget,
444 int32_t width, int32_t height, void *data)
446 struct editor *editor = data;
447 struct rectangle allocation;
449 widget_get_allocation(editor->widget, &allocation);
451 text_entry_allocate(editor->entry,
452 allocation.x + 20, allocation.y + 20,
453 width - 40, height / 2 - 40);
454 text_entry_allocate(editor->editor,
455 allocation.x + 20, allocation.y + height / 2 + 20,
456 width - 40, height / 2 - 40);
460 text_entry_activate(struct text_entry *entry,
461 struct wl_seat *seat)
463 struct wl_surface *surface = window_get_wl_surface(entry->window);
465 text_model_activate(entry->model,
471 text_entry_deactivate(struct text_entry *entry,
472 struct wl_seat *seat)
474 text_model_deactivate(entry->model,
479 text_entry_update_layout(struct text_entry *entry)
483 assert(((unsigned int)entry->cursor) <= strlen(entry->text));
485 if (!entry->preedit_text) {
486 text_layout_set_text(entry->layout, entry->text);
490 text = malloc(strlen(entry->text) + strlen(entry->preedit_text) + 1);
491 strncpy(text, entry->text, entry->cursor);
492 strcpy(text + entry->cursor, entry->preedit_text);
493 strcpy(text + entry->cursor + strlen(entry->preedit_text),
494 entry->text + entry->cursor);
496 text_layout_set_text(entry->layout, text);
499 widget_schedule_redraw(entry->widget);
501 text_model_set_surrounding_text(entry->model,
508 text_entry_insert_at_cursor(struct text_entry *entry, const char *text)
510 char *new_text = malloc(strlen(entry->text) + strlen(text) + 1);
512 strncpy(new_text, entry->text, entry->cursor);
513 strcpy(new_text + entry->cursor, text);
514 strcpy(new_text + entry->cursor + strlen(text),
515 entry->text + entry->cursor);
518 entry->text = new_text;
519 entry->cursor += strlen(text);
520 entry->anchor += strlen(text);
522 text_entry_update_layout(entry);
526 text_entry_set_preedit(struct text_entry *entry,
527 const char *preedit_text,
530 if (entry->preedit_text) {
531 free(entry->preedit_text);
532 entry->preedit_text = NULL;
533 entry->preedit_cursor = 0;
539 entry->preedit_text = strdup(preedit_text);
540 entry->preedit_cursor = preedit_cursor;
542 text_entry_update_layout(entry);
546 text_entry_set_cursor_position(struct text_entry *entry,
547 int32_t x, int32_t y)
549 entry->cursor = text_layout_xy_to_index(entry->layout, x, y);
551 if (entry->cursor >= entry->preedit_cursor) {
552 entry->cursor -= entry->preedit_cursor;
555 text_entry_update_layout(entry);
557 widget_schedule_redraw(entry->widget);
561 text_entry_set_anchor_position(struct text_entry *entry,
562 int32_t x, int32_t y)
564 entry->anchor = text_layout_xy_to_index(entry->layout, x, y);
566 widget_schedule_redraw(entry->widget);
570 text_entry_delete_text(struct text_entry *entry,
571 uint32_t index, uint32_t length)
573 if (entry->cursor > index)
574 entry->cursor -= length;
576 entry->text[index] = '\0';
577 strcat(entry->text, entry->text + index + length);
579 text_entry_update_layout(entry);
581 widget_schedule_redraw(entry->widget);
585 text_entry_draw_selection(struct text_entry *entry, cairo_t *cr)
587 cairo_text_extents_t extents;
588 uint32_t start_index = entry->anchor < entry->cursor ? entry->anchor : entry->cursor;
589 uint32_t end_index = entry->anchor < entry->cursor ? entry->cursor : entry->anchor;
590 cairo_rectangle_t start;
591 cairo_rectangle_t end;
593 if (entry->anchor == entry->cursor)
596 text_layout_extents(entry->layout, &extents);
598 text_layout_index_to_pos(entry->layout, start_index, &start);
599 text_layout_index_to_pos(entry->layout, end_index, &end);
603 cairo_set_source_rgba(cr, 0.0, 0.0, 1.0, 1.0);
605 start.x, extents.y_bearing + extents.height + 2,
606 end.x - start.x, -extents.height - 4);
610 start.x, extents.y_bearing + extents.height,
611 end.x - start.x, -extents.height);
613 cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0);
614 text_layout_draw(entry->layout, cr);
620 text_entry_draw_cursor(struct text_entry *entry, cairo_t *cr)
622 cairo_text_extents_t extents;
623 cairo_rectangle_t cursor_pos;
625 text_layout_extents(entry->layout, &extents);
626 text_layout_get_cursor_pos(entry->layout,
627 entry->cursor + entry->preedit_cursor,
630 cairo_set_line_width(cr, 1.0);
631 cairo_move_to(cr, cursor_pos.x, extents.y_bearing + extents.height + 2);
632 cairo_line_to(cr, cursor_pos.x, extents.y_bearing - 2);
637 text_entry_draw_preedit(struct text_entry *entry, cairo_t *cr)
639 cairo_text_extents_t extents;
640 cairo_rectangle_t start;
641 cairo_rectangle_t end;
643 if (!entry->preedit_text)
646 text_layout_extents(entry->layout, &extents);
648 text_layout_index_to_pos(entry->layout, entry->cursor, &start);
649 text_layout_index_to_pos(entry->layout,
650 entry->cursor + strlen(entry->preedit_text),
655 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
665 text_entry_redraw_handler(struct widget *widget, void *data)
667 struct text_entry *entry = data;
668 cairo_surface_t *surface;
669 struct rectangle allocation;
672 surface = window_get_surface(entry->window);
673 widget_get_allocation(entry->widget, &allocation);
675 cr = cairo_create(surface);
676 cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
679 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
681 cairo_push_group(cr);
682 cairo_translate(cr, allocation.x, allocation.y);
684 cairo_set_source_rgba(cr, 1, 1, 1, 1);
685 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
688 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
691 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
692 cairo_set_line_width (cr, 3);
693 cairo_set_source_rgba(cr, 0, 0, 1, 1.0);
697 cairo_set_source_rgba(cr, 0, 0, 0, 1);
699 cairo_translate(cr, 10, allocation.height / 2);
700 text_layout_draw(entry->layout, cr);
702 text_entry_draw_selection(entry, cr);
704 text_entry_draw_cursor(entry, cr);
706 text_entry_draw_preedit(entry, cr);
708 cairo_pop_group_to_source(cr);
712 cairo_surface_destroy(surface);
716 text_entry_motion_handler(struct widget *widget,
717 struct input *input, uint32_t time,
718 float x, float y, void *data)
720 struct text_entry *entry = data;
721 struct rectangle allocation;
723 widget_get_allocation(entry->widget, &allocation);
725 text_entry_set_cursor_position(entry,
733 text_entry_button_handler(struct widget *widget,
734 struct input *input, uint32_t time,
736 enum wl_pointer_button_state state, void *data)
738 struct text_entry *entry = data;
739 struct rectangle allocation;
742 widget_get_allocation(entry->widget, &allocation);
743 input_get_position(input, &x, &y);
745 if (button != BTN_LEFT) {
749 text_entry_set_cursor_position(entry,
753 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
754 struct wl_seat *seat = input_get_seat(input);
756 text_entry_activate(entry, seat);
758 text_entry_set_anchor_position(entry,
762 widget_set_motion_handler(entry->widget, text_entry_motion_handler);
764 widget_set_motion_handler(entry->widget, NULL);
769 editor_button_handler(struct widget *widget,
770 struct input *input, uint32_t time,
772 enum wl_pointer_button_state state, void *data)
774 struct editor *editor = data;
776 if (button != BTN_LEFT) {
780 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
781 struct wl_seat *seat = input_get_seat(input);
783 text_entry_deactivate(editor->entry, seat);
784 text_entry_deactivate(editor->editor, seat);
789 global_handler(struct wl_display *display, uint32_t id,
790 const char *interface, uint32_t version, void *data)
792 struct editor *editor = data;
794 if (!strcmp(interface, "text_model_factory")) {
795 editor->text_model_factory = wl_display_bind(display, id,
796 &text_model_factory_interface);
801 main(int argc, char *argv[])
803 struct editor editor;
805 editor.display = display_create(argc, argv);
806 if (editor.display == NULL) {
807 fprintf(stderr, "failed to create display: %m\n");
810 wl_display_add_global_listener(display_get_display(editor.display),
811 global_handler, &editor);
814 editor.window = window_create(editor.display);
815 editor.widget = frame_create(editor.window, &editor);
817 editor.entry = text_entry_create(&editor, "Entry");
818 editor.editor = text_entry_create(&editor, "Editor");
819 text_entry_set_preedit(editor.editor, "preedit", strlen("preedit"));
821 window_set_title(editor.window, "Text Editor");
823 widget_set_redraw_handler(editor.widget, redraw_handler);
824 widget_set_resize_handler(editor.widget, resize_handler);
825 widget_set_button_handler(editor.widget, editor_button_handler);
827 window_schedule_resize(editor.window, 500, 400);
829 display_run(editor.display);
831 text_entry_destroy(editor.entry);
832 text_entry_destroy(editor.editor);