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);
214 text_model_commit_string(void *data,
215 struct text_model *text_model,
219 struct text_entry *entry = data;
221 if (index > strlen(text)) {
222 fprintf(stderr, "Invalid cursor index %d\n", index);
223 index = strlen(text);
226 text_entry_insert_at_cursor(entry, text);
228 widget_schedule_redraw(entry->widget);
232 text_model_preedit_string(void *data,
233 struct text_model *text_model,
240 text_model_preedit_styling(void *data,
241 struct text_model *text_model)
246 text_model_key(void *data,
247 struct text_model *text_model)
252 text_model_selection_replacement(void *data,
253 struct text_model *text_model)
258 text_model_direction(void *data,
259 struct text_model *text_model)
264 text_model_locale(void *data,
265 struct text_model *text_model)
270 text_model_activated(void *data,
271 struct text_model *text_model)
273 struct text_entry *entry = data;
277 widget_schedule_redraw(entry->widget);
281 text_model_deactivated(void *data,
282 struct text_model *text_model)
284 struct text_entry *entry = data;
288 widget_schedule_redraw(entry->widget);
291 static const struct text_model_listener text_model_listener = {
292 text_model_commit_string,
293 text_model_preedit_string,
294 text_model_preedit_styling,
296 text_model_selection_replacement,
297 text_model_direction,
299 text_model_activated,
300 text_model_deactivated
303 static struct text_entry*
304 text_entry_create(struct editor *editor, const char *text)
306 struct text_entry *entry;
308 entry = malloc(sizeof *entry);
310 entry->widget = widget_add_widget(editor->widget, entry);
311 entry->window = editor->window;
312 entry->text = strdup(text);
314 entry->cursor = strlen(text);
315 entry->anchor = entry->cursor;
316 entry->preedit_text = NULL;
317 entry->preedit_cursor = 0;
318 entry->model = text_model_factory_create_text_model(editor->text_model_factory);
319 text_model_add_listener(entry->model, &text_model_listener, entry);
321 entry->layout = text_layout_create();
322 text_layout_set_text(entry->layout, entry->text);
324 widget_set_redraw_handler(entry->widget, text_entry_redraw_handler);
325 widget_set_button_handler(entry->widget, text_entry_button_handler);
331 text_entry_destroy(struct text_entry *entry)
333 widget_destroy(entry->widget);
334 text_model_destroy(entry->model);
335 text_layout_destroy(entry->layout);
341 redraw_handler(struct widget *widget, void *data)
343 struct editor *editor = data;
344 cairo_surface_t *surface;
345 struct rectangle allocation;
348 surface = window_get_surface(editor->window);
349 widget_get_allocation(editor->widget, &allocation);
351 cr = cairo_create(surface);
352 cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
355 cairo_translate(cr, allocation.x, allocation.y);
357 /* Draw background */
358 cairo_push_group(cr);
359 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
360 cairo_set_source_rgba(cr, 1, 1, 1, 1);
361 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
364 cairo_pop_group_to_source(cr);
368 cairo_surface_destroy(surface);
372 text_entry_allocate(struct text_entry *entry, int32_t x, int32_t y,
373 int32_t width, int32_t height)
375 widget_set_allocation(entry->widget, x, y, width, height);
379 resize_handler(struct widget *widget,
380 int32_t width, int32_t height, void *data)
382 struct editor *editor = data;
383 struct rectangle allocation;
385 widget_get_allocation(editor->widget, &allocation);
387 text_entry_allocate(editor->entry,
388 allocation.x + 20, allocation.y + 20,
389 width - 40, height / 2 - 40);
390 text_entry_allocate(editor->editor,
391 allocation.x + 20, allocation.y + height / 2 + 20,
392 width - 40, height / 2 - 40);
396 text_entry_activate(struct text_entry *entry,
397 struct wl_seat *seat)
399 struct wl_surface *surface = window_get_wl_surface(entry->window);
401 text_model_activate(entry->model,
407 text_entry_deactivate(struct text_entry *entry,
408 struct wl_seat *seat)
410 text_model_deactivate(entry->model,
415 text_entry_update_layout(struct text_entry *entry)
419 assert(((unsigned int)entry->cursor) <= strlen(entry->text));
421 if (!entry->preedit_text) {
422 text_layout_set_text(entry->layout, entry->text);
426 text = malloc(strlen(entry->text) + strlen(entry->preedit_text) + 1);
427 strncpy(text, entry->text, entry->cursor);
428 strcpy(text + entry->cursor, entry->preedit_text);
429 strcpy(text + entry->cursor + strlen(entry->preedit_text),
430 entry->text + entry->cursor);
432 text_layout_set_text(entry->layout, text);
435 widget_schedule_redraw(entry->widget);
437 text_model_set_surrounding_text(entry->model,
444 text_entry_insert_at_cursor(struct text_entry *entry, const char *text)
446 char *new_text = malloc(strlen(entry->text) + strlen(text) + 1);
448 strncpy(new_text, entry->text, entry->cursor);
449 strcpy(new_text + entry->cursor, text);
450 strcpy(new_text + entry->cursor + strlen(text),
451 entry->text + entry->cursor);
454 entry->text = new_text;
455 entry->cursor += strlen(text);
456 entry->anchor += strlen(text);
458 text_entry_update_layout(entry);
462 text_entry_set_preedit(struct text_entry *entry,
463 const char *preedit_text,
466 if (entry->preedit_text) {
467 free(entry->preedit_text);
468 entry->preedit_text = NULL;
469 entry->preedit_cursor = 0;
475 entry->preedit_text = strdup(preedit_text);
476 entry->preedit_cursor = preedit_cursor;
478 text_entry_update_layout(entry);
482 text_entry_set_cursor_position(struct text_entry *entry,
483 int32_t x, int32_t y)
485 entry->cursor = text_layout_xy_to_index(entry->layout, x, y);
487 if (entry->cursor >= entry->preedit_cursor) {
488 entry->cursor -= entry->preedit_cursor;
491 text_entry_update_layout(entry);
493 widget_schedule_redraw(entry->widget);
497 text_entry_set_anchor_position(struct text_entry *entry,
498 int32_t x, int32_t y)
500 entry->anchor = text_layout_xy_to_index(entry->layout, x, y);
502 widget_schedule_redraw(entry->widget);
506 text_entry_draw_selection(struct text_entry *entry, cairo_t *cr)
508 cairo_text_extents_t extents;
509 uint32_t start_index = entry->anchor < entry->cursor ? entry->anchor : entry->cursor;
510 uint32_t end_index = entry->anchor < entry->cursor ? entry->cursor : entry->anchor;
511 cairo_rectangle_t start;
512 cairo_rectangle_t end;
514 if (entry->anchor == entry->cursor)
517 text_layout_extents(entry->layout, &extents);
519 text_layout_index_to_pos(entry->layout, start_index, &start);
520 text_layout_index_to_pos(entry->layout, end_index, &end);
524 cairo_set_source_rgba(cr, 0.0, 0.0, 1.0, 1.0);
526 start.x, extents.y_bearing + extents.height + 2,
527 end.x - start.x, -extents.height - 4);
531 start.x, extents.y_bearing + extents.height,
532 end.x - start.x, -extents.height);
534 cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0);
535 text_layout_draw(entry->layout, cr);
541 text_entry_draw_cursor(struct text_entry *entry, cairo_t *cr)
543 cairo_text_extents_t extents;
544 cairo_rectangle_t cursor_pos;
546 text_layout_extents(entry->layout, &extents);
547 text_layout_get_cursor_pos(entry->layout,
548 entry->cursor + entry->preedit_cursor,
551 cairo_set_line_width(cr, 1.0);
552 cairo_move_to(cr, cursor_pos.x, extents.y_bearing + extents.height + 2);
553 cairo_line_to(cr, cursor_pos.x, extents.y_bearing - 2);
558 text_entry_draw_preedit(struct text_entry *entry, cairo_t *cr)
560 cairo_text_extents_t extents;
561 cairo_rectangle_t start;
562 cairo_rectangle_t end;
564 if (!entry->preedit_text)
567 text_layout_extents(entry->layout, &extents);
569 text_layout_index_to_pos(entry->layout, entry->cursor, &start);
570 text_layout_index_to_pos(entry->layout,
571 entry->cursor + strlen(entry->preedit_text),
576 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
586 text_entry_redraw_handler(struct widget *widget, void *data)
588 struct text_entry *entry = data;
589 cairo_surface_t *surface;
590 struct rectangle allocation;
593 surface = window_get_surface(entry->window);
594 widget_get_allocation(entry->widget, &allocation);
596 cr = cairo_create(surface);
597 cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
600 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
602 cairo_push_group(cr);
603 cairo_translate(cr, allocation.x, allocation.y);
605 cairo_set_source_rgba(cr, 1, 1, 1, 1);
606 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
609 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
612 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
613 cairo_set_line_width (cr, 3);
614 cairo_set_source_rgba(cr, 0, 0, 1, 1.0);
618 cairo_set_source_rgba(cr, 0, 0, 0, 1);
620 cairo_translate(cr, 10, allocation.height / 2);
621 text_layout_draw(entry->layout, cr);
623 text_entry_draw_selection(entry, cr);
625 text_entry_draw_cursor(entry, cr);
627 text_entry_draw_preedit(entry, cr);
629 cairo_pop_group_to_source(cr);
633 cairo_surface_destroy(surface);
637 text_entry_motion_handler(struct widget *widget,
638 struct input *input, uint32_t time,
639 float x, float y, void *data)
641 struct text_entry *entry = data;
642 struct rectangle allocation;
644 widget_get_allocation(entry->widget, &allocation);
646 text_entry_set_cursor_position(entry,
654 text_entry_button_handler(struct widget *widget,
655 struct input *input, uint32_t time,
657 enum wl_pointer_button_state state, void *data)
659 struct text_entry *entry = data;
660 struct rectangle allocation;
663 widget_get_allocation(entry->widget, &allocation);
664 input_get_position(input, &x, &y);
666 if (button != BTN_LEFT) {
670 text_entry_set_cursor_position(entry,
674 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
675 struct wl_seat *seat = input_get_seat(input);
677 text_entry_activate(entry, seat);
679 text_entry_set_anchor_position(entry,
683 widget_set_motion_handler(entry->widget, text_entry_motion_handler);
685 widget_set_motion_handler(entry->widget, NULL);
690 editor_button_handler(struct widget *widget,
691 struct input *input, uint32_t time,
693 enum wl_pointer_button_state state, void *data)
695 struct editor *editor = data;
697 if (button != BTN_LEFT) {
701 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
702 struct wl_seat *seat = input_get_seat(input);
704 text_entry_deactivate(editor->entry, seat);
705 text_entry_deactivate(editor->editor, seat);
710 global_handler(struct wl_display *display, uint32_t id,
711 const char *interface, uint32_t version, void *data)
713 struct editor *editor = data;
715 if (!strcmp(interface, "text_model_factory")) {
716 editor->text_model_factory = wl_display_bind(display, id,
717 &text_model_factory_interface);
722 main(int argc, char *argv[])
724 struct editor editor;
726 editor.display = display_create(argc, argv);
727 if (editor.display == NULL) {
728 fprintf(stderr, "failed to create display: %m\n");
731 wl_display_add_global_listener(display_get_display(editor.display),
732 global_handler, &editor);
735 editor.window = window_create(editor.display);
736 editor.widget = frame_create(editor.window, &editor);
738 editor.entry = text_entry_create(&editor, "Entry");
739 editor.editor = text_entry_create(&editor, "Editor");
740 text_entry_set_preedit(editor.editor, "preedit", strlen("preedit"));
742 window_set_title(editor.window, "Text Editor");
744 widget_set_redraw_handler(editor.widget, redraw_handler);
745 widget_set_resize_handler(editor.widget, resize_handler);
746 widget_set_button_handler(editor.widget, editor_button_handler);
748 window_schedule_resize(editor.window, 500, 400);
750 display_run(editor.display);
752 text_entry_destroy(editor.entry);
753 text_entry_destroy(editor.editor);