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;
54 struct text_model *model;
55 struct text_layout *layout;
59 struct text_model_factory *text_model_factory;
60 struct display *display;
61 struct window *window;
62 struct widget *widget;
63 struct text_entry *entry;
64 struct text_entry *editor;
67 static struct text_layout *
68 text_layout_create(void)
70 struct text_layout *layout;
71 cairo_surface_t *surface;
74 layout = malloc(sizeof *layout);
78 layout->glyphs = NULL;
79 layout->num_glyphs = 0;
81 layout->clusters = NULL;
82 layout->num_clusters = 0;
84 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
85 cr = cairo_create(surface);
86 cairo_set_font_size(cr, font_size);
87 cairo_select_font_face(cr, font_name, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
88 layout->font = cairo_get_scaled_font(cr);
89 cairo_scaled_font_reference(layout->font);
92 cairo_surface_destroy(surface);
98 text_layout_destroy(struct text_layout *layout)
101 cairo_glyph_free(layout->glyphs);
103 if (layout->clusters)
104 cairo_text_cluster_free(layout->clusters);
106 cairo_scaled_font_destroy(layout->font);
112 text_layout_set_text(struct text_layout *layout,
116 cairo_glyph_free(layout->glyphs);
118 if (layout->clusters)
119 cairo_text_cluster_free(layout->clusters);
121 layout->glyphs = NULL;
122 layout->num_glyphs = 0;
123 layout->clusters = NULL;
124 layout->num_clusters = 0;
126 cairo_scaled_font_text_to_glyphs(layout->font, 0, 0, text, -1,
127 &layout->glyphs, &layout->num_glyphs,
128 &layout->clusters, &layout->num_clusters,
129 &layout->cluster_flags);
133 text_layout_draw(struct text_layout *layout, cairo_t *cr)
136 cairo_set_scaled_font(cr, layout->font);
137 cairo_show_glyphs(cr, layout->glyphs, layout->num_glyphs);
142 text_layout_extents(struct text_layout *layout, cairo_text_extents_t *extents)
144 cairo_scaled_font_glyph_extents(layout->font,
145 layout->glyphs, layout->num_glyphs,
150 text_layout_xy_to_index(struct text_layout *layout, double x, double y)
152 cairo_text_extents_t extents;
155 cairo_scaled_font_glyph_extents(layout->font,
156 layout->glyphs, layout->num_glyphs,
159 for (i = 1; i < layout->num_glyphs; i++) {
160 if (layout->glyphs[i].x >= x) {
165 if (x >= layout->glyphs[layout->num_glyphs - 1].x && x < extents.width)
166 return layout->num_glyphs - 1;
168 return layout->num_glyphs;
172 text_layout_index_to_pos(struct text_layout *layout, uint32_t index, cairo_rectangle_t *pos)
174 cairo_text_extents_t extents;
179 cairo_scaled_font_glyph_extents(layout->font,
180 layout->glyphs, layout->num_glyphs,
183 if ((int)index >= layout->num_glyphs) {
184 pos->x = extents.x_advance;
185 pos->y = layout->num_glyphs ? layout->glyphs[layout->num_glyphs - 1].y : 0;
187 pos->height = extents.height;
191 pos->x = layout->glyphs[index].x;
192 pos->y = layout->glyphs[index].y;
193 pos->width = (int)index < layout->num_glyphs - 1 ? layout->glyphs[index + 1].x : extents.x_advance - pos->x;
194 pos->height = extents.height;
198 text_layout_get_cursor_pos(struct text_layout *layout, int index, cairo_rectangle_t *pos)
200 text_layout_index_to_pos(layout, index, pos);
204 static void text_entry_redraw_handler(struct widget *widget, void *data);
205 static void text_entry_button_handler(struct widget *widget,
206 struct input *input, uint32_t time,
208 enum wl_pointer_button_state state, void *data);
209 static void text_entry_insert_at_cursor(struct text_entry *entry, const char *text);
212 text_model_commit_string(void *data,
213 struct text_model *text_model,
217 struct text_entry *entry = data;
219 if (index > strlen(text)) {
220 fprintf(stderr, "Invalid cursor index %d\n", index);
221 index = strlen(text);
224 text_entry_insert_at_cursor(entry, text);
226 widget_schedule_redraw(entry->widget);
230 text_model_preedit_string(void *data,
231 struct text_model *text_model,
238 text_model_preedit_styling(void *data,
239 struct text_model *text_model)
244 text_model_key(void *data,
245 struct text_model *text_model)
250 text_model_selection_replacement(void *data,
251 struct text_model *text_model)
256 text_model_direction(void *data,
257 struct text_model *text_model)
262 text_model_locale(void *data,
263 struct text_model *text_model)
268 text_model_activated(void *data,
269 struct text_model *text_model)
271 struct text_entry *entry = data;
275 widget_schedule_redraw(entry->widget);
279 text_model_deactivated(void *data,
280 struct text_model *text_model)
282 struct text_entry *entry = data;
286 widget_schedule_redraw(entry->widget);
289 static const struct text_model_listener text_model_listener = {
290 text_model_commit_string,
291 text_model_preedit_string,
292 text_model_preedit_styling,
294 text_model_selection_replacement,
295 text_model_direction,
297 text_model_activated,
298 text_model_deactivated
301 static struct text_entry*
302 text_entry_create(struct editor *editor, const char *text)
304 struct text_entry *entry;
306 entry = malloc(sizeof *entry);
308 entry->widget = widget_add_widget(editor->widget, entry);
309 entry->window = editor->window;
310 entry->text = strdup(text);
312 entry->cursor = strlen(text);
313 entry->anchor = entry->cursor;
314 entry->model = text_model_factory_create_text_model(editor->text_model_factory);
315 text_model_add_listener(entry->model, &text_model_listener, entry);
317 entry->layout = text_layout_create();
318 text_layout_set_text(entry->layout, entry->text);
320 widget_set_redraw_handler(entry->widget, text_entry_redraw_handler);
321 widget_set_button_handler(entry->widget, text_entry_button_handler);
327 text_entry_destroy(struct text_entry *entry)
329 widget_destroy(entry->widget);
330 text_model_destroy(entry->model);
331 text_layout_destroy(entry->layout);
337 redraw_handler(struct widget *widget, void *data)
339 struct editor *editor = data;
340 cairo_surface_t *surface;
341 struct rectangle allocation;
344 surface = window_get_surface(editor->window);
345 widget_get_allocation(editor->widget, &allocation);
347 cr = cairo_create(surface);
348 cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
351 cairo_translate(cr, allocation.x, allocation.y);
353 /* Draw background */
354 cairo_push_group(cr);
355 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
356 cairo_set_source_rgba(cr, 1, 1, 1, 1);
357 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
360 cairo_pop_group_to_source(cr);
364 cairo_surface_destroy(surface);
368 text_entry_allocate(struct text_entry *entry, int32_t x, int32_t y,
369 int32_t width, int32_t height)
371 widget_set_allocation(entry->widget, x, y, width, height);
375 resize_handler(struct widget *widget,
376 int32_t width, int32_t height, void *data)
378 struct editor *editor = data;
379 struct rectangle allocation;
381 widget_get_allocation(editor->widget, &allocation);
383 text_entry_allocate(editor->entry,
384 allocation.x + 20, allocation.y + 20,
385 width - 40, height / 2 - 40);
386 text_entry_allocate(editor->editor,
387 allocation.x + 20, allocation.y + height / 2 + 20,
388 width - 40, height / 2 - 40);
392 text_entry_activate(struct text_entry *entry,
393 struct wl_seat *seat)
395 struct wl_surface *surface = window_get_wl_surface(entry->window);
397 text_model_activate(entry->model,
403 text_entry_deactivate(struct text_entry *entry,
404 struct wl_seat *seat)
406 text_model_deactivate(entry->model,
411 text_entry_insert_at_cursor(struct text_entry *entry, const char *text)
413 char *new_text = malloc(strlen(entry->text) + strlen(text) + 1);
415 strncpy(new_text, entry->text, entry->cursor);
416 strcpy(new_text + entry->cursor, text);
417 strcpy(new_text + entry->cursor + strlen(text),
418 entry->text + entry->cursor);
421 entry->text = new_text;
422 entry->cursor += strlen(text);
423 entry->anchor += strlen(text);
425 text_layout_set_text(entry->layout, entry->text);
429 text_entry_set_cursor_position(struct text_entry *entry,
430 int32_t x, int32_t y)
432 entry->cursor = text_layout_xy_to_index(entry->layout, x, y);
434 widget_schedule_redraw(entry->widget);
438 text_entry_set_anchor_position(struct text_entry *entry,
439 int32_t x, int32_t y)
441 entry->anchor = text_layout_xy_to_index(entry->layout, x, y);
443 widget_schedule_redraw(entry->widget);
447 text_entry_draw_selection(struct text_entry *entry, cairo_t *cr)
449 cairo_text_extents_t extents;
450 uint32_t start_index = entry->anchor < entry->cursor ? entry->anchor : entry->cursor;
451 uint32_t end_index = entry->anchor < entry->cursor ? entry->cursor : entry->anchor;
452 cairo_rectangle_t start;
453 cairo_rectangle_t end;
455 if (entry->anchor == entry->cursor)
458 text_layout_extents(entry->layout, &extents);
460 text_layout_index_to_pos(entry->layout, start_index, &start);
461 text_layout_index_to_pos(entry->layout, end_index, &end);
465 cairo_set_source_rgba(cr, 0.0, 0.0, 1.0, 1.0);
467 start.x, extents.y_bearing + extents.height + 2,
468 end.x - start.x, -extents.height - 4);
472 start.x, extents.y_bearing + extents.height,
473 end.x - start.x, -extents.height);
475 cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0);
476 text_layout_draw(entry->layout, cr);
482 text_entry_draw_cursor(struct text_entry *entry, cairo_t *cr)
484 cairo_text_extents_t extents;
485 cairo_rectangle_t cursor_pos;
487 text_layout_extents(entry->layout, &extents);
488 text_layout_get_cursor_pos(entry->layout, entry->cursor, &cursor_pos);
490 cairo_set_line_width(cr, 1.0);
491 cairo_move_to(cr, cursor_pos.x, extents.y_bearing + extents.height + 2);
492 cairo_line_to(cr, cursor_pos.x, extents.y_bearing - 2);
497 text_entry_redraw_handler(struct widget *widget, void *data)
499 struct text_entry *entry = data;
500 cairo_surface_t *surface;
501 struct rectangle allocation;
504 surface = window_get_surface(entry->window);
505 widget_get_allocation(entry->widget, &allocation);
507 cr = cairo_create(surface);
508 cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
511 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
513 cairo_push_group(cr);
514 cairo_translate(cr, allocation.x, allocation.y);
516 cairo_set_source_rgba(cr, 1, 1, 1, 1);
517 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
520 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
523 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
524 cairo_set_line_width (cr, 3);
525 cairo_set_source_rgba(cr, 0, 0, 1, 1.0);
529 cairo_set_source_rgba(cr, 0, 0, 0, 1);
531 cairo_translate(cr, 10, allocation.height / 2);
532 text_layout_draw(entry->layout, cr);
534 text_entry_draw_selection(entry, cr);
536 text_entry_draw_cursor(entry, cr);
538 cairo_pop_group_to_source(cr);
542 cairo_surface_destroy(surface);
546 text_entry_motion_handler(struct widget *widget,
547 struct input *input, uint32_t time,
548 float x, float y, void *data)
550 struct text_entry *entry = data;
551 struct rectangle allocation;
553 widget_get_allocation(entry->widget, &allocation);
555 text_entry_set_cursor_position(entry,
563 text_entry_button_handler(struct widget *widget,
564 struct input *input, uint32_t time,
566 enum wl_pointer_button_state state, void *data)
568 struct text_entry *entry = data;
569 struct rectangle allocation;
572 widget_get_allocation(entry->widget, &allocation);
573 input_get_position(input, &x, &y);
575 if (button != BTN_LEFT) {
579 text_entry_set_cursor_position(entry,
583 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
584 struct wl_seat *seat = input_get_seat(input);
586 text_entry_activate(entry, seat);
588 text_entry_set_anchor_position(entry,
592 widget_set_motion_handler(entry->widget, text_entry_motion_handler);
594 widget_set_motion_handler(entry->widget, NULL);
599 editor_button_handler(struct widget *widget,
600 struct input *input, uint32_t time,
602 enum wl_pointer_button_state state, void *data)
604 struct editor *editor = data;
606 if (button != BTN_LEFT) {
610 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
611 struct wl_seat *seat = input_get_seat(input);
613 text_entry_deactivate(editor->entry, seat);
614 text_entry_deactivate(editor->editor, seat);
619 global_handler(struct wl_display *display, uint32_t id,
620 const char *interface, uint32_t version, void *data)
622 struct editor *editor = data;
624 if (!strcmp(interface, "text_model_factory")) {
625 editor->text_model_factory = wl_display_bind(display, id,
626 &text_model_factory_interface);
631 main(int argc, char *argv[])
633 struct editor editor;
635 editor.display = display_create(argc, argv);
636 if (editor.display == NULL) {
637 fprintf(stderr, "failed to create display: %m\n");
640 wl_display_add_global_listener(display_get_display(editor.display),
641 global_handler, &editor);
644 editor.window = window_create(editor.display);
645 editor.widget = frame_create(editor.window, &editor);
647 editor.entry = text_entry_create(&editor, "Entry");
648 editor.editor = text_entry_create(&editor, "Editor");
650 window_set_title(editor.window, "Text Editor");
652 widget_set_redraw_handler(editor.widget, redraw_handler);
653 widget_set_resize_handler(editor.widget, resize_handler);
654 widget_set_button_handler(editor.widget, editor_button_handler);
656 window_schedule_resize(editor.window, 500, 400);
658 display_run(editor.display);
660 text_entry_destroy(editor.entry);
661 text_entry_destroy(editor.editor);