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;
53 struct text_model *model;
54 struct text_layout *layout;
58 struct text_model_factory *text_model_factory;
59 struct display *display;
60 struct window *window;
61 struct widget *widget;
62 struct text_entry *entry;
63 struct text_entry *editor;
66 static struct text_layout *
67 text_layout_create(void)
69 struct text_layout *layout;
70 cairo_surface_t *surface;
73 layout = malloc(sizeof *layout);
77 layout->glyphs = NULL;
78 layout->num_glyphs = 0;
80 layout->clusters = NULL;
81 layout->num_clusters = 0;
83 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
84 cr = cairo_create(surface);
85 cairo_set_font_size(cr, font_size);
86 cairo_select_font_face(cr, font_name, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
87 layout->font = cairo_get_scaled_font(cr);
88 cairo_scaled_font_reference(layout->font);
91 cairo_surface_destroy(surface);
97 text_layout_destroy(struct text_layout *layout)
100 cairo_glyph_free(layout->glyphs);
102 if (layout->clusters)
103 cairo_text_cluster_free(layout->clusters);
105 cairo_scaled_font_destroy(layout->font);
111 text_layout_set_text(struct text_layout *layout,
115 cairo_glyph_free(layout->glyphs);
117 if (layout->clusters)
118 cairo_text_cluster_free(layout->clusters);
120 layout->glyphs = NULL;
121 layout->num_glyphs = 0;
122 layout->clusters = NULL;
123 layout->num_clusters = 0;
125 cairo_scaled_font_text_to_glyphs(layout->font, 0, 0, text, -1,
126 &layout->glyphs, &layout->num_glyphs,
127 &layout->clusters, &layout->num_clusters,
128 &layout->cluster_flags);
132 text_layout_draw(struct text_layout *layout, cairo_t *cr)
135 cairo_set_scaled_font(cr, layout->font);
136 cairo_show_glyphs(cr, layout->glyphs, layout->num_glyphs);
141 text_layout_extents(struct text_layout *layout, cairo_text_extents_t *extents)
143 cairo_scaled_font_glyph_extents(layout->font,
144 layout->glyphs, layout->num_glyphs,
149 text_layout_xy_to_index(struct text_layout *layout, double x, double y)
151 cairo_text_extents_t extents;
154 cairo_scaled_font_glyph_extents(layout->font,
155 layout->glyphs, layout->num_glyphs,
158 for (i = 1; i < layout->num_glyphs; i++) {
159 if (layout->glyphs[i].x >= x) {
164 if (x >= layout->glyphs[layout->num_glyphs - 1].x && x < extents.width)
165 return layout->num_glyphs - 1;
167 return layout->num_glyphs;
171 text_layout_index_to_pos(struct text_layout *layout, uint32_t index, cairo_rectangle_t *pos)
173 cairo_text_extents_t extents;
178 cairo_scaled_font_glyph_extents(layout->font,
179 layout->glyphs, layout->num_glyphs,
182 if ((int)index >= layout->num_glyphs) {
183 pos->x = extents.x_advance;
184 pos->y = layout->num_glyphs ? layout->glyphs[layout->num_glyphs - 1].y : 0;
186 pos->height = extents.height;
190 pos->x = layout->glyphs[index].x;
191 pos->y = layout->glyphs[index].y;
192 pos->width = (int)index < layout->num_glyphs - 1 ? layout->glyphs[index + 1].x : extents.x_advance - pos->x;
193 pos->height = extents.height;
197 text_layout_get_cursor_pos(struct text_layout *layout, int index, cairo_rectangle_t *pos)
199 text_layout_index_to_pos(layout, index, pos);
203 static void text_entry_redraw_handler(struct widget *widget, void *data);
204 static void text_entry_button_handler(struct widget *widget,
205 struct input *input, uint32_t time,
207 enum wl_pointer_button_state state, void *data);
208 static void text_entry_insert_at_cursor(struct text_entry *entry, const char *text);
211 text_model_commit_string(void *data,
212 struct text_model *text_model,
216 struct text_entry *entry = data;
218 if (index > strlen(text)) {
219 fprintf(stderr, "Invalid cursor index %d\n", index);
220 index = strlen(text);
223 text_entry_insert_at_cursor(entry, text);
225 widget_schedule_redraw(entry->widget);
229 text_model_preedit_string(void *data,
230 struct text_model *text_model,
237 text_model_preedit_styling(void *data,
238 struct text_model *text_model)
243 text_model_key(void *data,
244 struct text_model *text_model)
249 text_model_selection_replacement(void *data,
250 struct text_model *text_model)
255 text_model_direction(void *data,
256 struct text_model *text_model)
261 text_model_locale(void *data,
262 struct text_model *text_model)
267 text_model_activated(void *data,
268 struct text_model *text_model)
270 struct text_entry *entry = data;
274 widget_schedule_redraw(entry->widget);
278 text_model_deactivated(void *data,
279 struct text_model *text_model)
281 struct text_entry *entry = data;
285 widget_schedule_redraw(entry->widget);
288 static const struct text_model_listener text_model_listener = {
289 text_model_commit_string,
290 text_model_preedit_string,
291 text_model_preedit_styling,
293 text_model_selection_replacement,
294 text_model_direction,
296 text_model_activated,
297 text_model_deactivated
300 static struct text_entry*
301 text_entry_create(struct editor *editor, const char *text)
303 struct text_entry *entry;
305 entry = malloc(sizeof *entry);
307 entry->widget = widget_add_widget(editor->widget, entry);
308 entry->window = editor->window;
309 entry->text = strdup(text);
311 entry->cursor = strlen(text);
312 entry->model = text_model_factory_create_text_model(editor->text_model_factory);
313 text_model_add_listener(entry->model, &text_model_listener, entry);
315 entry->layout = text_layout_create();
316 text_layout_set_text(entry->layout, entry->text);
318 widget_set_redraw_handler(entry->widget, text_entry_redraw_handler);
319 widget_set_button_handler(entry->widget, text_entry_button_handler);
325 text_entry_destroy(struct text_entry *entry)
327 widget_destroy(entry->widget);
328 text_model_destroy(entry->model);
329 text_layout_destroy(entry->layout);
335 redraw_handler(struct widget *widget, void *data)
337 struct editor *editor = data;
338 cairo_surface_t *surface;
339 struct rectangle allocation;
342 surface = window_get_surface(editor->window);
343 widget_get_allocation(editor->widget, &allocation);
345 cr = cairo_create(surface);
346 cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
349 cairo_translate(cr, allocation.x, allocation.y);
351 /* Draw background */
352 cairo_push_group(cr);
353 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
354 cairo_set_source_rgba(cr, 1, 1, 1, 1);
355 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
358 cairo_pop_group_to_source(cr);
362 cairo_surface_destroy(surface);
366 text_entry_allocate(struct text_entry *entry, int32_t x, int32_t y,
367 int32_t width, int32_t height)
369 widget_set_allocation(entry->widget, x, y, width, height);
373 resize_handler(struct widget *widget,
374 int32_t width, int32_t height, void *data)
376 struct editor *editor = data;
377 struct rectangle allocation;
379 widget_get_allocation(editor->widget, &allocation);
381 text_entry_allocate(editor->entry,
382 allocation.x + 20, allocation.y + 20,
383 width - 40, height / 2 - 40);
384 text_entry_allocate(editor->editor,
385 allocation.x + 20, allocation.y + height / 2 + 20,
386 width - 40, height / 2 - 40);
390 text_entry_activate(struct text_entry *entry,
391 struct wl_seat *seat)
393 struct wl_surface *surface = window_get_wl_surface(entry->window);
395 text_model_activate(entry->model,
401 text_entry_deactivate(struct text_entry *entry,
402 struct wl_seat *seat)
404 text_model_deactivate(entry->model,
409 text_entry_insert_at_cursor(struct text_entry *entry, const char *text)
411 char *new_text = malloc(strlen(entry->text) + strlen(text) + 1);
413 strncpy(new_text, entry->text, entry->cursor);
414 strcpy(new_text + entry->cursor, text);
415 strcpy(new_text + entry->cursor + strlen(text),
416 entry->text + entry->cursor);
419 entry->text = new_text;
420 entry->cursor += strlen(text);
422 text_layout_set_text(entry->layout, entry->text);
426 text_entry_set_cursor_position(struct text_entry *entry,
427 int32_t x, int32_t y)
429 entry->cursor = text_layout_xy_to_index(entry->layout, x, y);
431 widget_schedule_redraw(entry->widget);
435 text_entry_draw_cursor(struct text_entry *entry, cairo_t *cr)
437 cairo_text_extents_t extents;
438 cairo_rectangle_t cursor_pos;
440 text_layout_extents(entry->layout, &extents);
441 text_layout_get_cursor_pos(entry->layout, entry->cursor, &cursor_pos);
443 cairo_set_line_width(cr, 1.0);
444 cairo_move_to(cr, cursor_pos.x, extents.y_bearing + extents.height + 2);
445 cairo_line_to(cr, cursor_pos.x, extents.y_bearing - 2);
450 text_entry_redraw_handler(struct widget *widget, void *data)
452 struct text_entry *entry = data;
453 cairo_surface_t *surface;
454 struct rectangle allocation;
457 surface = window_get_surface(entry->window);
458 widget_get_allocation(entry->widget, &allocation);
460 cr = cairo_create(surface);
461 cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
464 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
466 cairo_push_group(cr);
467 cairo_translate(cr, allocation.x, allocation.y);
469 cairo_set_source_rgba(cr, 1, 1, 1, 1);
470 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
473 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
476 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
477 cairo_set_line_width (cr, 3);
478 cairo_set_source_rgba(cr, 0, 0, 1, 1.0);
482 cairo_set_source_rgba(cr, 0, 0, 0, 1);
484 cairo_translate(cr, 10, allocation.height / 2);
485 text_layout_draw(entry->layout, cr);
487 text_entry_draw_cursor(entry, cr);
489 cairo_pop_group_to_source(cr);
493 cairo_surface_destroy(surface);
497 text_entry_button_handler(struct widget *widget,
498 struct input *input, uint32_t time,
500 enum wl_pointer_button_state state, void *data)
502 struct text_entry *entry = data;
503 struct rectangle allocation;
506 widget_get_allocation(entry->widget, &allocation);
507 input_get_position(input, &x, &y);
509 if (button != BTN_LEFT) {
513 text_entry_set_cursor_position(entry,
517 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
518 struct wl_seat *seat = input_get_seat(input);
520 text_entry_activate(entry, seat);
525 editor_button_handler(struct widget *widget,
526 struct input *input, uint32_t time,
528 enum wl_pointer_button_state state, void *data)
530 struct editor *editor = data;
532 if (button != BTN_LEFT) {
536 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
537 struct wl_seat *seat = input_get_seat(input);
539 text_entry_deactivate(editor->entry, seat);
540 text_entry_deactivate(editor->editor, seat);
545 global_handler(struct wl_display *display, uint32_t id,
546 const char *interface, uint32_t version, void *data)
548 struct editor *editor = data;
550 if (!strcmp(interface, "text_model_factory")) {
551 editor->text_model_factory = wl_display_bind(display, id,
552 &text_model_factory_interface);
557 main(int argc, char *argv[])
559 struct editor editor;
561 editor.display = display_create(argc, argv);
562 if (editor.display == NULL) {
563 fprintf(stderr, "failed to create display: %m\n");
566 wl_display_add_global_listener(display_get_display(editor.display),
567 global_handler, &editor);
570 editor.window = window_create(editor.display);
571 editor.widget = frame_create(editor.window, &editor);
573 editor.entry = text_entry_create(&editor, "Entry");
574 editor.editor = text_entry_create(&editor, "Editor");
576 window_set_title(editor.window, "Text Editor");
578 widget_set_redraw_handler(editor.widget, redraw_handler);
579 widget_set_resize_handler(editor.widget, resize_handler);
580 widget_set_button_handler(editor.widget, editor_button_handler);
582 window_schedule_resize(editor.window, 500, 400);
584 display_run(editor.display);
586 text_entry_destroy(editor.entry);
587 text_entry_destroy(editor.editor);