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);
210 text_entry_append(struct text_entry *entry, const char *text)
212 entry->text = realloc(entry->text, strlen(entry->text) + strlen(text) + 1);
213 strcat(entry->text, text);
214 text_layout_set_text(entry->layout, entry->text);
219 text_model_commit_string(void *data,
220 struct text_model *text_model,
224 struct text_entry *entry = data;
226 text_entry_append(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->model = text_model_factory_create_text_model(editor->text_model_factory);
316 text_model_add_listener(entry->model, &text_model_listener, entry);
318 entry->layout = text_layout_create();
319 text_layout_set_text(entry->layout, entry->text);
321 widget_set_redraw_handler(entry->widget, text_entry_redraw_handler);
322 widget_set_button_handler(entry->widget, text_entry_button_handler);
328 text_entry_destroy(struct text_entry *entry)
330 widget_destroy(entry->widget);
331 text_model_destroy(entry->model);
332 text_layout_destroy(entry->layout);
338 redraw_handler(struct widget *widget, void *data)
340 struct editor *editor = data;
341 cairo_surface_t *surface;
342 struct rectangle allocation;
345 surface = window_get_surface(editor->window);
346 widget_get_allocation(editor->widget, &allocation);
348 cr = cairo_create(surface);
349 cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
352 cairo_translate(cr, allocation.x, allocation.y);
354 /* Draw background */
355 cairo_push_group(cr);
356 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
357 cairo_set_source_rgba(cr, 1, 1, 1, 1);
358 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
361 cairo_pop_group_to_source(cr);
365 cairo_surface_destroy(surface);
369 text_entry_allocate(struct text_entry *entry, int32_t x, int32_t y,
370 int32_t width, int32_t height)
372 widget_set_allocation(entry->widget, x, y, width, height);
376 resize_handler(struct widget *widget,
377 int32_t width, int32_t height, void *data)
379 struct editor *editor = data;
380 struct rectangle allocation;
382 widget_get_allocation(editor->widget, &allocation);
384 text_entry_allocate(editor->entry,
385 allocation.x + 20, allocation.y + 20,
386 width - 40, height / 2 - 40);
387 text_entry_allocate(editor->editor,
388 allocation.x + 20, allocation.y + height / 2 + 20,
389 width - 40, height / 2 - 40);
393 text_entry_activate(struct text_entry *entry,
394 struct wl_seat *seat)
396 struct wl_surface *surface = window_get_wl_surface(entry->window);
398 text_model_activate(entry->model,
404 text_entry_deactivate(struct text_entry *entry,
405 struct wl_seat *seat)
407 text_model_deactivate(entry->model,
412 text_entry_set_cursor_position(struct text_entry *entry,
413 int32_t x, int32_t y)
415 entry->cursor = text_layout_xy_to_index(entry->layout, x, y);
417 widget_schedule_redraw(entry->widget);
421 text_entry_draw_cursor(struct text_entry *entry, cairo_t *cr)
423 cairo_text_extents_t extents;
424 cairo_rectangle_t cursor_pos;
426 text_layout_extents(entry->layout, &extents);
427 text_layout_get_cursor_pos(entry->layout, entry->cursor, &cursor_pos);
429 cairo_set_line_width(cr, 1.0);
430 cairo_move_to(cr, cursor_pos.x, extents.y_bearing + extents.height + 2);
431 cairo_line_to(cr, cursor_pos.x, extents.y_bearing - 2);
436 text_entry_redraw_handler(struct widget *widget, void *data)
438 struct text_entry *entry = data;
439 cairo_surface_t *surface;
440 struct rectangle allocation;
443 surface = window_get_surface(entry->window);
444 widget_get_allocation(entry->widget, &allocation);
446 cr = cairo_create(surface);
447 cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
450 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
452 cairo_push_group(cr);
453 cairo_translate(cr, allocation.x, allocation.y);
455 cairo_set_source_rgba(cr, 1, 1, 1, 1);
456 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
459 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
462 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
463 cairo_set_line_width (cr, 3);
464 cairo_set_source_rgba(cr, 0, 0, 1, 1.0);
468 cairo_set_source_rgba(cr, 0, 0, 0, 1);
470 cairo_translate(cr, 10, allocation.height / 2);
471 text_layout_draw(entry->layout, cr);
473 text_entry_draw_cursor(entry, cr);
475 cairo_pop_group_to_source(cr);
479 cairo_surface_destroy(surface);
483 text_entry_button_handler(struct widget *widget,
484 struct input *input, uint32_t time,
486 enum wl_pointer_button_state state, void *data)
488 struct text_entry *entry = data;
489 struct rectangle allocation;
492 widget_get_allocation(entry->widget, &allocation);
493 input_get_position(input, &x, &y);
495 if (button != BTN_LEFT) {
499 text_entry_set_cursor_position(entry,
503 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
504 struct wl_seat *seat = input_get_seat(input);
506 text_entry_activate(entry, seat);
511 editor_button_handler(struct widget *widget,
512 struct input *input, uint32_t time,
514 enum wl_pointer_button_state state, void *data)
516 struct editor *editor = data;
518 if (button != BTN_LEFT) {
522 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
523 struct wl_seat *seat = input_get_seat(input);
525 text_entry_deactivate(editor->entry, seat);
526 text_entry_deactivate(editor->editor, seat);
531 global_handler(struct wl_display *display, uint32_t id,
532 const char *interface, uint32_t version, void *data)
534 struct editor *editor = data;
536 if (!strcmp(interface, "text_model_factory")) {
537 editor->text_model_factory = wl_display_bind(display, id,
538 &text_model_factory_interface);
543 main(int argc, char *argv[])
545 struct editor editor;
547 editor.display = display_create(argc, argv);
548 if (editor.display == NULL) {
549 fprintf(stderr, "failed to create display: %m\n");
552 wl_display_add_global_listener(display_get_display(editor.display),
553 global_handler, &editor);
556 editor.window = window_create(editor.display);
557 editor.widget = frame_create(editor.window, &editor);
559 editor.entry = text_entry_create(&editor, "Entry");
560 editor.editor = text_entry_create(&editor, "Editor");
562 window_set_title(editor.window, "Text Editor");
564 widget_set_redraw_handler(editor.widget, redraw_handler);
565 widget_set_resize_handler(editor.widget, resize_handler);
566 widget_set_button_handler(editor.widget, editor_button_handler);
568 window_schedule_resize(editor.window, 500, 400);
570 display_run(editor.display);
572 text_entry_destroy(editor.entry);
573 text_entry_destroy(editor.editor);