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,
217 text_model_commit_string(void *data,
218 struct text_model *text_model,
222 struct text_entry *entry = data;
224 if (index > strlen(text)) {
225 fprintf(stderr, "Invalid cursor index %d\n", index);
226 index = strlen(text);
229 text_entry_insert_at_cursor(entry, text);
231 widget_schedule_redraw(entry->widget);
235 text_model_preedit_string(void *data,
236 struct text_model *text_model,
240 struct text_entry *entry = data;
242 if (index > strlen(text)) {
243 fprintf(stderr, "Invalid cursor index %d\n", index);
244 index = strlen(text);
247 text_entry_set_preedit(entry, text, index);
249 widget_schedule_redraw(entry->widget);
253 text_model_preedit_styling(void *data,
254 struct text_model *text_model)
259 text_model_key(void *data,
260 struct text_model *text_model)
265 text_model_selection_replacement(void *data,
266 struct text_model *text_model)
271 text_model_direction(void *data,
272 struct text_model *text_model)
277 text_model_locale(void *data,
278 struct text_model *text_model)
283 text_model_activated(void *data,
284 struct text_model *text_model)
286 struct text_entry *entry = data;
290 widget_schedule_redraw(entry->widget);
294 text_model_deactivated(void *data,
295 struct text_model *text_model)
297 struct text_entry *entry = data;
301 widget_schedule_redraw(entry->widget);
304 static const struct text_model_listener text_model_listener = {
305 text_model_commit_string,
306 text_model_preedit_string,
307 text_model_preedit_styling,
309 text_model_selection_replacement,
310 text_model_direction,
312 text_model_activated,
313 text_model_deactivated
316 static struct text_entry*
317 text_entry_create(struct editor *editor, const char *text)
319 struct text_entry *entry;
321 entry = malloc(sizeof *entry);
323 entry->widget = widget_add_widget(editor->widget, entry);
324 entry->window = editor->window;
325 entry->text = strdup(text);
327 entry->cursor = strlen(text);
328 entry->anchor = entry->cursor;
329 entry->preedit_text = NULL;
330 entry->preedit_cursor = 0;
331 entry->model = text_model_factory_create_text_model(editor->text_model_factory);
332 text_model_add_listener(entry->model, &text_model_listener, entry);
334 entry->layout = text_layout_create();
335 text_layout_set_text(entry->layout, entry->text);
337 widget_set_redraw_handler(entry->widget, text_entry_redraw_handler);
338 widget_set_button_handler(entry->widget, text_entry_button_handler);
344 text_entry_destroy(struct text_entry *entry)
346 widget_destroy(entry->widget);
347 text_model_destroy(entry->model);
348 text_layout_destroy(entry->layout);
354 redraw_handler(struct widget *widget, void *data)
356 struct editor *editor = data;
357 cairo_surface_t *surface;
358 struct rectangle allocation;
361 surface = window_get_surface(editor->window);
362 widget_get_allocation(editor->widget, &allocation);
364 cr = cairo_create(surface);
365 cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
368 cairo_translate(cr, allocation.x, allocation.y);
370 /* Draw background */
371 cairo_push_group(cr);
372 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
373 cairo_set_source_rgba(cr, 1, 1, 1, 1);
374 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
377 cairo_pop_group_to_source(cr);
381 cairo_surface_destroy(surface);
385 text_entry_allocate(struct text_entry *entry, int32_t x, int32_t y,
386 int32_t width, int32_t height)
388 widget_set_allocation(entry->widget, x, y, width, height);
392 resize_handler(struct widget *widget,
393 int32_t width, int32_t height, void *data)
395 struct editor *editor = data;
396 struct rectangle allocation;
398 widget_get_allocation(editor->widget, &allocation);
400 text_entry_allocate(editor->entry,
401 allocation.x + 20, allocation.y + 20,
402 width - 40, height / 2 - 40);
403 text_entry_allocate(editor->editor,
404 allocation.x + 20, allocation.y + height / 2 + 20,
405 width - 40, height / 2 - 40);
409 text_entry_activate(struct text_entry *entry,
410 struct wl_seat *seat)
412 struct wl_surface *surface = window_get_wl_surface(entry->window);
414 text_model_activate(entry->model,
420 text_entry_deactivate(struct text_entry *entry,
421 struct wl_seat *seat)
423 text_model_deactivate(entry->model,
428 text_entry_update_layout(struct text_entry *entry)
432 assert(((unsigned int)entry->cursor) <= strlen(entry->text));
434 if (!entry->preedit_text) {
435 text_layout_set_text(entry->layout, entry->text);
439 text = malloc(strlen(entry->text) + strlen(entry->preedit_text) + 1);
440 strncpy(text, entry->text, entry->cursor);
441 strcpy(text + entry->cursor, entry->preedit_text);
442 strcpy(text + entry->cursor + strlen(entry->preedit_text),
443 entry->text + entry->cursor);
445 text_layout_set_text(entry->layout, text);
448 widget_schedule_redraw(entry->widget);
450 text_model_set_surrounding_text(entry->model,
457 text_entry_insert_at_cursor(struct text_entry *entry, const char *text)
459 char *new_text = malloc(strlen(entry->text) + strlen(text) + 1);
461 strncpy(new_text, entry->text, entry->cursor);
462 strcpy(new_text + entry->cursor, text);
463 strcpy(new_text + entry->cursor + strlen(text),
464 entry->text + entry->cursor);
467 entry->text = new_text;
468 entry->cursor += strlen(text);
469 entry->anchor += strlen(text);
471 text_entry_update_layout(entry);
475 text_entry_set_preedit(struct text_entry *entry,
476 const char *preedit_text,
479 if (entry->preedit_text) {
480 free(entry->preedit_text);
481 entry->preedit_text = NULL;
482 entry->preedit_cursor = 0;
488 entry->preedit_text = strdup(preedit_text);
489 entry->preedit_cursor = preedit_cursor;
491 text_entry_update_layout(entry);
495 text_entry_set_cursor_position(struct text_entry *entry,
496 int32_t x, int32_t y)
498 entry->cursor = text_layout_xy_to_index(entry->layout, x, y);
500 if (entry->cursor >= entry->preedit_cursor) {
501 entry->cursor -= entry->preedit_cursor;
504 text_entry_update_layout(entry);
506 widget_schedule_redraw(entry->widget);
510 text_entry_set_anchor_position(struct text_entry *entry,
511 int32_t x, int32_t y)
513 entry->anchor = text_layout_xy_to_index(entry->layout, x, y);
515 widget_schedule_redraw(entry->widget);
519 text_entry_draw_selection(struct text_entry *entry, cairo_t *cr)
521 cairo_text_extents_t extents;
522 uint32_t start_index = entry->anchor < entry->cursor ? entry->anchor : entry->cursor;
523 uint32_t end_index = entry->anchor < entry->cursor ? entry->cursor : entry->anchor;
524 cairo_rectangle_t start;
525 cairo_rectangle_t end;
527 if (entry->anchor == entry->cursor)
530 text_layout_extents(entry->layout, &extents);
532 text_layout_index_to_pos(entry->layout, start_index, &start);
533 text_layout_index_to_pos(entry->layout, end_index, &end);
537 cairo_set_source_rgba(cr, 0.0, 0.0, 1.0, 1.0);
539 start.x, extents.y_bearing + extents.height + 2,
540 end.x - start.x, -extents.height - 4);
544 start.x, extents.y_bearing + extents.height,
545 end.x - start.x, -extents.height);
547 cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0);
548 text_layout_draw(entry->layout, cr);
554 text_entry_draw_cursor(struct text_entry *entry, cairo_t *cr)
556 cairo_text_extents_t extents;
557 cairo_rectangle_t cursor_pos;
559 text_layout_extents(entry->layout, &extents);
560 text_layout_get_cursor_pos(entry->layout,
561 entry->cursor + entry->preedit_cursor,
564 cairo_set_line_width(cr, 1.0);
565 cairo_move_to(cr, cursor_pos.x, extents.y_bearing + extents.height + 2);
566 cairo_line_to(cr, cursor_pos.x, extents.y_bearing - 2);
571 text_entry_draw_preedit(struct text_entry *entry, cairo_t *cr)
573 cairo_text_extents_t extents;
574 cairo_rectangle_t start;
575 cairo_rectangle_t end;
577 if (!entry->preedit_text)
580 text_layout_extents(entry->layout, &extents);
582 text_layout_index_to_pos(entry->layout, entry->cursor, &start);
583 text_layout_index_to_pos(entry->layout,
584 entry->cursor + strlen(entry->preedit_text),
589 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
599 text_entry_redraw_handler(struct widget *widget, void *data)
601 struct text_entry *entry = data;
602 cairo_surface_t *surface;
603 struct rectangle allocation;
606 surface = window_get_surface(entry->window);
607 widget_get_allocation(entry->widget, &allocation);
609 cr = cairo_create(surface);
610 cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
613 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
615 cairo_push_group(cr);
616 cairo_translate(cr, allocation.x, allocation.y);
618 cairo_set_source_rgba(cr, 1, 1, 1, 1);
619 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
622 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
625 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
626 cairo_set_line_width (cr, 3);
627 cairo_set_source_rgba(cr, 0, 0, 1, 1.0);
631 cairo_set_source_rgba(cr, 0, 0, 0, 1);
633 cairo_translate(cr, 10, allocation.height / 2);
634 text_layout_draw(entry->layout, cr);
636 text_entry_draw_selection(entry, cr);
638 text_entry_draw_cursor(entry, cr);
640 text_entry_draw_preedit(entry, cr);
642 cairo_pop_group_to_source(cr);
646 cairo_surface_destroy(surface);
650 text_entry_motion_handler(struct widget *widget,
651 struct input *input, uint32_t time,
652 float x, float y, void *data)
654 struct text_entry *entry = data;
655 struct rectangle allocation;
657 widget_get_allocation(entry->widget, &allocation);
659 text_entry_set_cursor_position(entry,
667 text_entry_button_handler(struct widget *widget,
668 struct input *input, uint32_t time,
670 enum wl_pointer_button_state state, void *data)
672 struct text_entry *entry = data;
673 struct rectangle allocation;
676 widget_get_allocation(entry->widget, &allocation);
677 input_get_position(input, &x, &y);
679 if (button != BTN_LEFT) {
683 text_entry_set_cursor_position(entry,
687 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
688 struct wl_seat *seat = input_get_seat(input);
690 text_entry_activate(entry, seat);
692 text_entry_set_anchor_position(entry,
696 widget_set_motion_handler(entry->widget, text_entry_motion_handler);
698 widget_set_motion_handler(entry->widget, NULL);
703 editor_button_handler(struct widget *widget,
704 struct input *input, uint32_t time,
706 enum wl_pointer_button_state state, void *data)
708 struct editor *editor = data;
710 if (button != BTN_LEFT) {
714 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
715 struct wl_seat *seat = input_get_seat(input);
717 text_entry_deactivate(editor->entry, seat);
718 text_entry_deactivate(editor->editor, seat);
723 global_handler(struct wl_display *display, uint32_t id,
724 const char *interface, uint32_t version, void *data)
726 struct editor *editor = data;
728 if (!strcmp(interface, "text_model_factory")) {
729 editor->text_model_factory = wl_display_bind(display, id,
730 &text_model_factory_interface);
735 main(int argc, char *argv[])
737 struct editor editor;
739 editor.display = display_create(argc, argv);
740 if (editor.display == NULL) {
741 fprintf(stderr, "failed to create display: %m\n");
744 wl_display_add_global_listener(display_get_display(editor.display),
745 global_handler, &editor);
748 editor.window = window_create(editor.display);
749 editor.widget = frame_create(editor.window, &editor);
751 editor.entry = text_entry_create(&editor, "Entry");
752 editor.editor = text_entry_create(&editor, "Editor");
753 text_entry_set_preedit(editor.editor, "preedit", strlen("preedit"));
755 window_set_title(editor.window, "Text Editor");
757 widget_set_redraw_handler(editor.widget, redraw_handler);
758 widget_set_resize_handler(editor.widget, resize_handler);
759 widget_set_button_handler(editor.widget, editor_button_handler);
761 window_schedule_resize(editor.window, 500, 400);
763 display_run(editor.display);
765 text_entry_destroy(editor.entry);
766 text_entry_destroy(editor.editor);