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 text_model_reset(entry->model);
553 if (entry->cursor >= entry->preedit_cursor) {
554 entry->cursor -= entry->preedit_cursor;
557 text_entry_update_layout(entry);
559 widget_schedule_redraw(entry->widget);
563 text_entry_set_anchor_position(struct text_entry *entry,
564 int32_t x, int32_t y)
566 entry->anchor = text_layout_xy_to_index(entry->layout, x, y);
568 widget_schedule_redraw(entry->widget);
572 text_entry_delete_text(struct text_entry *entry,
573 uint32_t index, uint32_t length)
575 if (entry->cursor > index)
576 entry->cursor -= length;
578 entry->text[index] = '\0';
579 strcat(entry->text, entry->text + index + length);
581 text_entry_update_layout(entry);
583 widget_schedule_redraw(entry->widget);
587 text_entry_draw_selection(struct text_entry *entry, cairo_t *cr)
589 cairo_text_extents_t extents;
590 uint32_t start_index = entry->anchor < entry->cursor ? entry->anchor : entry->cursor;
591 uint32_t end_index = entry->anchor < entry->cursor ? entry->cursor : entry->anchor;
592 cairo_rectangle_t start;
593 cairo_rectangle_t end;
595 if (entry->anchor == entry->cursor)
598 text_layout_extents(entry->layout, &extents);
600 text_layout_index_to_pos(entry->layout, start_index, &start);
601 text_layout_index_to_pos(entry->layout, end_index, &end);
605 cairo_set_source_rgba(cr, 0.0, 0.0, 1.0, 1.0);
607 start.x, extents.y_bearing + extents.height + 2,
608 end.x - start.x, -extents.height - 4);
612 start.x, extents.y_bearing + extents.height,
613 end.x - start.x, -extents.height);
615 cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0);
616 text_layout_draw(entry->layout, cr);
622 text_entry_draw_cursor(struct text_entry *entry, cairo_t *cr)
624 cairo_text_extents_t extents;
625 cairo_rectangle_t cursor_pos;
627 text_layout_extents(entry->layout, &extents);
628 text_layout_get_cursor_pos(entry->layout,
629 entry->cursor + entry->preedit_cursor,
632 cairo_set_line_width(cr, 1.0);
633 cairo_move_to(cr, cursor_pos.x, extents.y_bearing + extents.height + 2);
634 cairo_line_to(cr, cursor_pos.x, extents.y_bearing - 2);
639 text_entry_draw_preedit(struct text_entry *entry, cairo_t *cr)
641 cairo_text_extents_t extents;
642 cairo_rectangle_t start;
643 cairo_rectangle_t end;
645 if (!entry->preedit_text)
648 text_layout_extents(entry->layout, &extents);
650 text_layout_index_to_pos(entry->layout, entry->cursor, &start);
651 text_layout_index_to_pos(entry->layout,
652 entry->cursor + strlen(entry->preedit_text),
657 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
667 text_entry_redraw_handler(struct widget *widget, void *data)
669 struct text_entry *entry = data;
670 cairo_surface_t *surface;
671 struct rectangle allocation;
674 surface = window_get_surface(entry->window);
675 widget_get_allocation(entry->widget, &allocation);
677 cr = cairo_create(surface);
678 cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
681 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
683 cairo_push_group(cr);
684 cairo_translate(cr, allocation.x, allocation.y);
686 cairo_set_source_rgba(cr, 1, 1, 1, 1);
687 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
690 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
693 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
694 cairo_set_line_width (cr, 3);
695 cairo_set_source_rgba(cr, 0, 0, 1, 1.0);
699 cairo_set_source_rgba(cr, 0, 0, 0, 1);
701 cairo_translate(cr, 10, allocation.height / 2);
702 text_layout_draw(entry->layout, cr);
704 text_entry_draw_selection(entry, cr);
706 text_entry_draw_cursor(entry, cr);
708 text_entry_draw_preedit(entry, cr);
710 cairo_pop_group_to_source(cr);
714 cairo_surface_destroy(surface);
718 text_entry_motion_handler(struct widget *widget,
719 struct input *input, uint32_t time,
720 float x, float y, void *data)
722 struct text_entry *entry = data;
723 struct rectangle allocation;
725 widget_get_allocation(entry->widget, &allocation);
727 text_entry_set_cursor_position(entry,
735 text_entry_button_handler(struct widget *widget,
736 struct input *input, uint32_t time,
738 enum wl_pointer_button_state state, void *data)
740 struct text_entry *entry = data;
741 struct rectangle allocation;
744 widget_get_allocation(entry->widget, &allocation);
745 input_get_position(input, &x, &y);
747 if (button != BTN_LEFT) {
751 text_entry_set_cursor_position(entry,
755 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
756 struct wl_seat *seat = input_get_seat(input);
758 text_entry_activate(entry, seat);
760 text_entry_set_anchor_position(entry,
764 widget_set_motion_handler(entry->widget, text_entry_motion_handler);
766 widget_set_motion_handler(entry->widget, NULL);
771 editor_button_handler(struct widget *widget,
772 struct input *input, uint32_t time,
774 enum wl_pointer_button_state state, void *data)
776 struct editor *editor = data;
778 if (button != BTN_LEFT) {
782 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
783 struct wl_seat *seat = input_get_seat(input);
785 text_entry_deactivate(editor->entry, seat);
786 text_entry_deactivate(editor->editor, seat);
791 global_handler(struct wl_display *display, uint32_t id,
792 const char *interface, uint32_t version, void *data)
794 struct editor *editor = data;
796 if (!strcmp(interface, "text_model_factory")) {
797 editor->text_model_factory = wl_display_bind(display, id,
798 &text_model_factory_interface);
803 main(int argc, char *argv[])
805 struct editor editor;
807 editor.display = display_create(argc, argv);
808 if (editor.display == NULL) {
809 fprintf(stderr, "failed to create display: %m\n");
812 wl_display_add_global_listener(display_get_display(editor.display),
813 global_handler, &editor);
816 editor.window = window_create(editor.display);
817 editor.widget = frame_create(editor.window, &editor);
819 editor.entry = text_entry_create(&editor, "Entry");
820 editor.editor = text_entry_create(&editor, "Editor");
821 text_entry_set_preedit(editor.editor, "preedit", strlen("preedit"));
823 window_set_title(editor.window, "Text Editor");
825 widget_set_redraw_handler(editor.widget, redraw_handler);
826 widget_set_resize_handler(editor.widget, resize_handler);
827 widget_set_button_handler(editor.widget, editor_button_handler);
829 window_schedule_resize(editor.window, 500, 400);
831 display_run(editor.display);
833 text_entry_destroy(editor.entry);
834 text_entry_destroy(editor.editor);