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;
52 struct text_model *model;
53 struct text_layout *layout;
57 struct text_model_factory *text_model_factory;
58 struct display *display;
59 struct window *window;
60 struct widget *widget;
61 struct text_entry *entry;
62 struct text_entry *editor;
65 static struct text_layout *
66 text_layout_create(void)
68 struct text_layout *layout;
69 cairo_surface_t *surface;
72 layout = malloc(sizeof *layout);
76 layout->glyphs = NULL;
77 layout->num_glyphs = 0;
79 layout->clusters = NULL;
80 layout->num_clusters = 0;
82 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
83 cr = cairo_create(surface);
84 cairo_set_font_size(cr, font_size);
85 cairo_select_font_face(cr, font_name, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
86 layout->font = cairo_get_scaled_font(cr);
87 cairo_scaled_font_reference(layout->font);
90 cairo_surface_destroy(surface);
96 text_layout_destroy(struct text_layout *layout)
99 cairo_glyph_free(layout->glyphs);
101 if (layout->clusters)
102 cairo_text_cluster_free(layout->clusters);
104 cairo_scaled_font_destroy(layout->font);
110 text_layout_set_text(struct text_layout *layout,
114 cairo_glyph_free(layout->glyphs);
116 if (layout->clusters)
117 cairo_text_cluster_free(layout->clusters);
119 layout->glyphs = NULL;
120 layout->num_glyphs = 0;
121 layout->clusters = NULL;
122 layout->num_clusters = 0;
124 cairo_scaled_font_text_to_glyphs(layout->font, 0, 0, text, -1,
125 &layout->glyphs, &layout->num_glyphs,
126 &layout->clusters, &layout->num_clusters,
127 &layout->cluster_flags);
131 text_layout_draw(struct text_layout *layout, cairo_t *cr)
134 cairo_set_scaled_font(cr, layout->font);
135 cairo_show_glyphs(cr, layout->glyphs, layout->num_glyphs);
140 static void text_entry_redraw_handler(struct widget *widget, void *data);
141 static void text_entry_button_handler(struct widget *widget,
142 struct input *input, uint32_t time,
144 enum wl_pointer_button_state state, void *data);
147 text_entry_append(struct text_entry *entry, const char *text)
149 entry->text = realloc(entry->text, strlen(entry->text) + strlen(text) + 1);
150 strcat(entry->text, text);
151 text_layout_set_text(entry->layout, entry->text);
156 text_model_commit_string(void *data,
157 struct text_model *text_model,
161 struct text_entry *entry = data;
163 text_entry_append(entry, text);
165 widget_schedule_redraw(entry->widget);
169 text_model_preedit_string(void *data,
170 struct text_model *text_model,
177 text_model_preedit_styling(void *data,
178 struct text_model *text_model)
183 text_model_key(void *data,
184 struct text_model *text_model)
189 text_model_selection_replacement(void *data,
190 struct text_model *text_model)
195 text_model_direction(void *data,
196 struct text_model *text_model)
201 text_model_locale(void *data,
202 struct text_model *text_model)
207 text_model_activated(void *data,
208 struct text_model *text_model)
210 struct text_entry *entry = data;
214 widget_schedule_redraw(entry->widget);
218 text_model_deactivated(void *data,
219 struct text_model *text_model)
221 struct text_entry *entry = data;
225 widget_schedule_redraw(entry->widget);
228 static const struct text_model_listener text_model_listener = {
229 text_model_commit_string,
230 text_model_preedit_string,
231 text_model_preedit_styling,
233 text_model_selection_replacement,
234 text_model_direction,
236 text_model_activated,
237 text_model_deactivated
240 static struct text_entry*
241 text_entry_create(struct editor *editor, const char *text)
243 struct text_entry *entry;
245 entry = malloc(sizeof *entry);
247 entry->widget = widget_add_widget(editor->widget, entry);
248 entry->window = editor->window;
249 entry->text = strdup(text);
251 entry->model = text_model_factory_create_text_model(editor->text_model_factory);
252 text_model_add_listener(entry->model, &text_model_listener, entry);
254 entry->layout = text_layout_create();
255 text_layout_set_text(entry->layout, entry->text);
257 widget_set_redraw_handler(entry->widget, text_entry_redraw_handler);
258 widget_set_button_handler(entry->widget, text_entry_button_handler);
264 text_entry_destroy(struct text_entry *entry)
266 widget_destroy(entry->widget);
267 text_model_destroy(entry->model);
268 text_layout_destroy(entry->layout);
274 redraw_handler(struct widget *widget, void *data)
276 struct editor *editor = data;
277 cairo_surface_t *surface;
278 struct rectangle allocation;
281 surface = window_get_surface(editor->window);
282 widget_get_allocation(editor->widget, &allocation);
284 cr = cairo_create(surface);
285 cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
288 cairo_translate(cr, allocation.x, allocation.y);
290 /* Draw background */
291 cairo_push_group(cr);
292 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
293 cairo_set_source_rgba(cr, 1, 1, 1, 1);
294 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
297 cairo_pop_group_to_source(cr);
301 cairo_surface_destroy(surface);
305 text_entry_allocate(struct text_entry *entry, int32_t x, int32_t y,
306 int32_t width, int32_t height)
308 widget_set_allocation(entry->widget, x, y, width, height);
312 resize_handler(struct widget *widget,
313 int32_t width, int32_t height, void *data)
315 struct editor *editor = data;
316 struct rectangle allocation;
318 widget_get_allocation(editor->widget, &allocation);
320 text_entry_allocate(editor->entry,
321 allocation.x + 20, allocation.y + 20,
322 width - 40, height / 2 - 40);
323 text_entry_allocate(editor->editor,
324 allocation.x + 20, allocation.y + height / 2 + 20,
325 width - 40, height / 2 - 40);
329 text_entry_activate(struct text_entry *entry,
330 struct wl_seat *seat)
332 struct wl_surface *surface = window_get_wl_surface(entry->window);
334 text_model_activate(entry->model,
340 text_entry_deactivate(struct text_entry *entry,
341 struct wl_seat *seat)
343 text_model_deactivate(entry->model,
348 text_entry_redraw_handler(struct widget *widget, void *data)
350 struct text_entry *entry = data;
351 cairo_surface_t *surface;
352 struct rectangle allocation;
355 surface = window_get_surface(entry->window);
356 widget_get_allocation(entry->widget, &allocation);
358 cr = cairo_create(surface);
359 cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
362 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
364 cairo_push_group(cr);
365 cairo_translate(cr, allocation.x, allocation.y);
367 cairo_set_source_rgba(cr, 1, 1, 1, 1);
368 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
371 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
374 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
375 cairo_set_line_width (cr, 3);
376 cairo_set_source_rgba(cr, 0, 0, 1, 1.0);
380 cairo_set_source_rgba(cr, 0, 0, 0, 1);
382 cairo_translate(cr, 10, allocation.height / 2);
383 text_layout_draw(entry->layout, cr);
384 cairo_pop_group_to_source(cr);
388 cairo_surface_destroy(surface);
392 text_entry_button_handler(struct widget *widget,
393 struct input *input, uint32_t time,
395 enum wl_pointer_button_state state, void *data)
397 struct text_entry *entry = data;
399 if (button != BTN_LEFT) {
403 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
404 struct wl_seat *seat = input_get_seat(input);
406 text_entry_activate(entry, seat);
411 editor_button_handler(struct widget *widget,
412 struct input *input, uint32_t time,
414 enum wl_pointer_button_state state, void *data)
416 struct editor *editor = data;
418 if (button != BTN_LEFT) {
422 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
423 struct wl_seat *seat = input_get_seat(input);
425 text_entry_deactivate(editor->entry, seat);
426 text_entry_deactivate(editor->editor, seat);
431 global_handler(struct wl_display *display, uint32_t id,
432 const char *interface, uint32_t version, void *data)
434 struct editor *editor = data;
436 if (!strcmp(interface, "text_model_factory")) {
437 editor->text_model_factory = wl_display_bind(display, id,
438 &text_model_factory_interface);
443 main(int argc, char *argv[])
445 struct editor editor;
447 editor.display = display_create(argc, argv);
448 if (editor.display == NULL) {
449 fprintf(stderr, "failed to create display: %m\n");
452 wl_display_add_global_listener(display_get_display(editor.display),
453 global_handler, &editor);
456 editor.window = window_create(editor.display);
457 editor.widget = frame_create(editor.window, &editor);
459 editor.entry = text_entry_create(&editor, "Entry");
460 editor.editor = text_entry_create(&editor, "Editor");
462 window_set_title(editor.window, "Text Editor");
464 widget_set_redraw_handler(editor.widget, redraw_handler);
465 widget_set_resize_handler(editor.widget, resize_handler);
466 widget_set_button_handler(editor.widget, editor_button_handler);
468 window_schedule_resize(editor.window, 500, 400);
470 display_run(editor.display);
472 text_entry_destroy(editor.entry);
473 text_entry_destroy(editor.editor);