text: Add support for pre-edit string
[profile/ivi/weston.git] / clients / editor.c
1 /*
2  * Copyright © 2012 Openismus GmbH
3  * Copyright © 2012 Intel Corporation
4  *
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.
14  *
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.
22  */
23
24 #include <assert.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include <linux/input.h>
30 #include <cairo.h>
31
32 #include "window.h"
33 #include "text-client-protocol.h"
34
35 static const char *font_name = "sans-serif";
36 static int font_size = 14;
37
38 struct text_layout {
39         cairo_glyph_t *glyphs;
40         int num_glyphs;
41         cairo_text_cluster_t *clusters;
42         int num_clusters;
43         cairo_text_cluster_flags_t cluster_flags;
44         cairo_scaled_font_t *font;
45 };
46
47 struct text_entry {
48         struct widget *widget;
49         struct window *window;
50         char *text;
51         int active;
52         uint32_t cursor;
53         uint32_t anchor;
54         char *preedit_text;
55         uint32_t preedit_cursor;
56         struct text_model *model;
57         struct text_layout *layout;
58 };
59
60 struct editor {
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;
67 };
68
69 static struct text_layout *
70 text_layout_create(void)
71 {
72         struct text_layout *layout;
73         cairo_surface_t *surface;
74         cairo_t *cr;
75
76         layout = malloc(sizeof *layout);
77         if (!layout)
78                 return NULL;
79
80         layout->glyphs = NULL;
81         layout->num_glyphs = 0;
82
83         layout->clusters = NULL;
84         layout->num_clusters = 0;
85
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);
92
93         cairo_destroy(cr);
94         cairo_surface_destroy(surface);
95
96         return layout;
97 }
98
99 static void
100 text_layout_destroy(struct text_layout *layout)
101 {
102         if (layout->glyphs)
103                 cairo_glyph_free(layout->glyphs);
104
105         if (layout->clusters)
106                 cairo_text_cluster_free(layout->clusters);
107
108         cairo_scaled_font_destroy(layout->font);
109
110         free(layout);
111 }
112
113 static void
114 text_layout_set_text(struct text_layout *layout,
115                      const char *text)
116 {
117         if (layout->glyphs)
118                 cairo_glyph_free(layout->glyphs);
119
120         if (layout->clusters)
121                 cairo_text_cluster_free(layout->clusters);
122
123         layout->glyphs = NULL;
124         layout->num_glyphs = 0;
125         layout->clusters = NULL;
126         layout->num_clusters = 0;
127
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);
132 }
133
134 static void
135 text_layout_draw(struct text_layout *layout, cairo_t *cr)
136 {
137         cairo_save(cr);
138         cairo_set_scaled_font(cr, layout->font);
139         cairo_show_glyphs(cr, layout->glyphs, layout->num_glyphs);
140         cairo_restore(cr);
141 }
142
143 static void
144 text_layout_extents(struct text_layout *layout, cairo_text_extents_t *extents)
145 {
146         cairo_scaled_font_glyph_extents(layout->font,
147                                         layout->glyphs, layout->num_glyphs,
148                                         extents);
149 }
150
151 static int
152 text_layout_xy_to_index(struct text_layout *layout, double x, double y)
153 {
154         cairo_text_extents_t extents;
155         int i;
156
157         cairo_scaled_font_glyph_extents(layout->font,
158                                         layout->glyphs, layout->num_glyphs,
159                                         &extents);
160
161         for (i = 1; i < layout->num_glyphs; i++) {
162                 if (layout->glyphs[i].x >= x) {
163                         return i - 1;
164                 }
165         }
166
167         if (x >= layout->glyphs[layout->num_glyphs - 1].x && x < extents.width)
168                 return layout->num_glyphs - 1;
169
170         return layout->num_glyphs;
171 }
172
173 static void
174 text_layout_index_to_pos(struct text_layout *layout, uint32_t index, cairo_rectangle_t *pos)
175 {
176         cairo_text_extents_t extents;
177
178         if (!pos)
179                 return;
180
181         cairo_scaled_font_glyph_extents(layout->font,
182                                         layout->glyphs, layout->num_glyphs,
183                                         &extents);
184
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;
188                 pos->width = 1;
189                 pos->height = extents.height;
190                 return;
191         }
192
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;
197 }
198
199 static void
200 text_layout_get_cursor_pos(struct text_layout *layout, int index, cairo_rectangle_t *pos)
201 {
202         text_layout_index_to_pos(layout, index, pos);
203         pos->width = 1;
204 }
205
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,
209                                       uint32_t button,
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,
214                                    int preedit_cursor);
215
216 static void
217 text_model_commit_string(void *data,
218                          struct text_model *text_model,
219                          const char *text,
220                          uint32_t index)
221 {
222         struct text_entry *entry = data;
223
224         if (index > strlen(text)) {
225                 fprintf(stderr, "Invalid cursor index %d\n", index);
226                 index = strlen(text);
227         }
228
229         text_entry_insert_at_cursor(entry, text);
230
231         widget_schedule_redraw(entry->widget);
232 }
233
234 static void
235 text_model_preedit_string(void *data,
236                           struct text_model *text_model,
237                           const char *text,
238                           uint32_t index)
239 {
240         struct text_entry *entry = data;
241
242         if (index > strlen(text)) {
243                 fprintf(stderr, "Invalid cursor index %d\n", index);
244                 index = strlen(text);
245         }
246
247         text_entry_set_preedit(entry, text, index);
248
249         widget_schedule_redraw(entry->widget);
250 }
251
252 static void
253 text_model_preedit_styling(void *data,
254                            struct text_model *text_model)
255 {
256 }
257
258 static void
259 text_model_key(void *data,
260                struct text_model *text_model)
261 {
262 }
263
264 static void
265 text_model_selection_replacement(void *data,
266                                  struct text_model *text_model)
267 {
268 }
269
270 static void
271 text_model_direction(void *data,
272                      struct text_model *text_model)
273 {
274 }
275
276 static void
277 text_model_locale(void *data,
278                   struct text_model *text_model)
279 {
280 }
281
282 static void
283 text_model_activated(void *data,
284                      struct text_model *text_model)
285 {
286         struct text_entry *entry = data;
287
288         entry->active = 1;
289
290         widget_schedule_redraw(entry->widget);
291 }
292
293 static void
294 text_model_deactivated(void *data,
295                        struct text_model *text_model)
296 {
297         struct text_entry *entry = data;
298
299         entry->active = 0;
300
301         widget_schedule_redraw(entry->widget);
302 }
303
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,
308         text_model_key,
309         text_model_selection_replacement,
310         text_model_direction,
311         text_model_locale,
312         text_model_activated,
313         text_model_deactivated
314 };
315
316 static struct text_entry*
317 text_entry_create(struct editor *editor, const char *text)
318 {
319         struct text_entry *entry;
320
321         entry = malloc(sizeof *entry);
322
323         entry->widget = widget_add_widget(editor->widget, entry);
324         entry->window = editor->window;
325         entry->text = strdup(text);
326         entry->active = 0;
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);
333
334         entry->layout = text_layout_create();
335         text_layout_set_text(entry->layout, entry->text);
336
337         widget_set_redraw_handler(entry->widget, text_entry_redraw_handler);
338         widget_set_button_handler(entry->widget, text_entry_button_handler);
339
340         return entry;
341 }
342
343 static void
344 text_entry_destroy(struct text_entry *entry)
345 {
346         widget_destroy(entry->widget);
347         text_model_destroy(entry->model);
348         text_layout_destroy(entry->layout);
349         free(entry->text);
350         free(entry);
351 }
352
353 static void
354 redraw_handler(struct widget *widget, void *data)
355 {
356         struct editor *editor = data;
357         cairo_surface_t *surface;
358         struct rectangle allocation;
359         cairo_t *cr;
360
361         surface = window_get_surface(editor->window);
362         widget_get_allocation(editor->widget, &allocation);
363
364         cr = cairo_create(surface);
365         cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
366         cairo_clip(cr);
367
368         cairo_translate(cr, allocation.x, allocation.y);
369
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);
375         cairo_fill(cr);
376
377         cairo_pop_group_to_source(cr);
378         cairo_paint(cr);
379
380         cairo_destroy(cr);
381         cairo_surface_destroy(surface);
382 }
383
384 static void
385 text_entry_allocate(struct text_entry *entry, int32_t x, int32_t y,
386                     int32_t width, int32_t height)
387 {
388         widget_set_allocation(entry->widget, x, y, width, height);
389 }
390
391 static void
392 resize_handler(struct widget *widget,
393                int32_t width, int32_t height, void *data)
394 {
395         struct editor *editor = data;
396         struct rectangle allocation;
397
398         widget_get_allocation(editor->widget, &allocation);
399
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);
406 }
407
408 static void
409 text_entry_activate(struct text_entry *entry,
410                     struct wl_seat *seat)
411 {
412         struct wl_surface *surface = window_get_wl_surface(entry->window);
413
414         text_model_activate(entry->model,
415                             seat,
416                             surface);
417 }
418
419 static void
420 text_entry_deactivate(struct text_entry *entry,
421                       struct wl_seat *seat)
422 {
423         text_model_deactivate(entry->model,
424                               seat);
425 }
426
427 static void
428 text_entry_update_layout(struct text_entry *entry)
429 {
430         char *text;
431
432         assert(((unsigned int)entry->cursor) <= strlen(entry->text));
433
434         if (!entry->preedit_text) {
435                 text_layout_set_text(entry->layout, entry->text);
436                 return;
437         }
438
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);
444
445         text_layout_set_text(entry->layout, text);
446         free(text);
447
448         widget_schedule_redraw(entry->widget);
449
450         text_model_set_surrounding_text(entry->model,
451                                         entry->text,
452                                         entry->cursor,
453                                         entry->anchor);
454 }
455
456 static void
457 text_entry_insert_at_cursor(struct text_entry *entry, const char *text)
458 {
459         char *new_text = malloc(strlen(entry->text) + strlen(text) + 1);
460
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);
465
466         free(entry->text);
467         entry->text = new_text;
468         entry->cursor += strlen(text);
469         entry->anchor += strlen(text);
470
471         text_entry_update_layout(entry);
472 }
473
474 static void
475 text_entry_set_preedit(struct text_entry *entry,
476                        const char *preedit_text,
477                        int preedit_cursor)
478 {
479         if (entry->preedit_text) {
480                 free(entry->preedit_text);
481                 entry->preedit_text = NULL;
482                 entry->preedit_cursor = 0;
483         }
484
485         if (!preedit_text)
486                 return;
487
488         entry->preedit_text = strdup(preedit_text);
489         entry->preedit_cursor = preedit_cursor;
490
491         text_entry_update_layout(entry);
492 }
493
494 static void
495 text_entry_set_cursor_position(struct text_entry *entry,
496                                int32_t x, int32_t y)
497 {
498         entry->cursor = text_layout_xy_to_index(entry->layout, x, y);
499
500         if (entry->cursor >= entry->preedit_cursor) {
501                 entry->cursor -= entry->preedit_cursor;
502         }
503
504         text_entry_update_layout(entry);
505
506         widget_schedule_redraw(entry->widget);
507 }
508
509 static void
510 text_entry_set_anchor_position(struct text_entry *entry,
511                                int32_t x, int32_t y)
512 {
513         entry->anchor = text_layout_xy_to_index(entry->layout, x, y);
514
515         widget_schedule_redraw(entry->widget);
516 }
517
518 static void
519 text_entry_draw_selection(struct text_entry *entry, cairo_t *cr)
520 {
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;
526
527         if (entry->anchor == entry->cursor)
528                 return;
529
530         text_layout_extents(entry->layout, &extents);
531
532         text_layout_index_to_pos(entry->layout, start_index, &start);
533         text_layout_index_to_pos(entry->layout, end_index, &end);
534
535         cairo_save (cr);
536
537         cairo_set_source_rgba(cr, 0.0, 0.0, 1.0, 1.0);
538         cairo_rectangle(cr,
539                         start.x, extents.y_bearing + extents.height + 2,
540                         end.x - start.x, -extents.height - 4);
541         cairo_fill(cr);
542
543         cairo_rectangle(cr,
544                         start.x, extents.y_bearing + extents.height,
545                         end.x - start.x, -extents.height);
546         cairo_clip(cr);
547         cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0);
548         text_layout_draw(entry->layout, cr);
549
550         cairo_restore (cr);
551 }
552
553 static void
554 text_entry_draw_cursor(struct text_entry *entry, cairo_t *cr)
555 {
556         cairo_text_extents_t extents;
557         cairo_rectangle_t cursor_pos;
558
559         text_layout_extents(entry->layout, &extents);
560         text_layout_get_cursor_pos(entry->layout,
561                                    entry->cursor + entry->preedit_cursor,
562                                    &cursor_pos);
563
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);
567         cairo_stroke(cr);
568 }
569
570 static void
571 text_entry_draw_preedit(struct text_entry *entry, cairo_t *cr)
572 {
573         cairo_text_extents_t extents;
574         cairo_rectangle_t start;
575         cairo_rectangle_t end;
576
577         if (!entry->preedit_text)
578                 return;
579
580         text_layout_extents(entry->layout, &extents);
581
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),
585                                  &end);
586
587         cairo_save (cr);
588
589         cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
590         cairo_rectangle(cr,
591                         start.x, 0,
592                         end.x - start.x, 1);
593         cairo_fill(cr);
594
595         cairo_restore (cr);
596 }
597
598 static void
599 text_entry_redraw_handler(struct widget *widget, void *data)
600 {
601         struct text_entry *entry = data;
602         cairo_surface_t *surface;
603         struct rectangle allocation;
604         cairo_t *cr;
605
606         surface = window_get_surface(entry->window);
607         widget_get_allocation(entry->widget, &allocation);
608
609         cr = cairo_create(surface);
610         cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
611         cairo_clip(cr);
612
613         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
614
615         cairo_push_group(cr);
616         cairo_translate(cr, allocation.x, allocation.y);
617
618         cairo_set_source_rgba(cr, 1, 1, 1, 1);
619         cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
620         cairo_fill(cr);
621
622         cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
623
624         if (entry->active) {
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);
628                 cairo_stroke(cr);
629         }
630
631         cairo_set_source_rgba(cr, 0, 0, 0, 1);
632
633         cairo_translate(cr, 10, allocation.height / 2);
634         text_layout_draw(entry->layout, cr);
635
636         text_entry_draw_selection(entry, cr);
637
638         text_entry_draw_cursor(entry, cr);
639
640         text_entry_draw_preedit(entry, cr);
641
642         cairo_pop_group_to_source(cr);
643         cairo_paint(cr);
644
645         cairo_destroy(cr);
646         cairo_surface_destroy(surface);
647 }
648
649 static int
650 text_entry_motion_handler(struct widget *widget,
651                           struct input *input, uint32_t time,
652                           float x, float y, void *data)
653 {
654         struct text_entry *entry = data;
655         struct rectangle allocation;
656
657         widget_get_allocation(entry->widget, &allocation);
658
659         text_entry_set_cursor_position(entry,
660                                        x - allocation.x,
661                                        y - allocation.y);
662
663         return CURSOR_IBEAM;
664 }
665
666 static void
667 text_entry_button_handler(struct widget *widget,
668                           struct input *input, uint32_t time,
669                           uint32_t button,
670                           enum wl_pointer_button_state state, void *data)
671 {
672         struct text_entry *entry = data;
673         struct rectangle allocation;
674         int32_t x, y;
675
676         widget_get_allocation(entry->widget, &allocation);
677         input_get_position(input, &x, &y);
678
679         if (button != BTN_LEFT) {
680                 return;
681         }
682
683         text_entry_set_cursor_position(entry,
684                                        x - allocation.x,
685                                        y - allocation.y);
686
687         if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
688                 struct wl_seat *seat = input_get_seat(input);
689
690                 text_entry_activate(entry, seat);
691
692                 text_entry_set_anchor_position(entry,
693                                                x - allocation.x,
694                                                y - allocation.y);
695
696                 widget_set_motion_handler(entry->widget, text_entry_motion_handler);
697         } else {
698                 widget_set_motion_handler(entry->widget, NULL);
699         }
700 }
701
702 static void
703 editor_button_handler(struct widget *widget,
704                       struct input *input, uint32_t time,
705                       uint32_t button,
706                       enum wl_pointer_button_state state, void *data)
707 {
708         struct editor *editor = data;
709
710         if (button != BTN_LEFT) {
711                 return;
712         }
713
714         if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
715                 struct wl_seat *seat = input_get_seat(input);
716
717                 text_entry_deactivate(editor->entry, seat);
718                 text_entry_deactivate(editor->editor, seat);
719         }
720 }
721
722 static void
723 global_handler(struct wl_display *display, uint32_t id,
724                const char *interface, uint32_t version, void *data)
725 {
726         struct editor *editor = data;
727
728         if (!strcmp(interface, "text_model_factory")) {
729                 editor->text_model_factory = wl_display_bind(display, id,
730                                                              &text_model_factory_interface);
731         }
732 }
733
734 int
735 main(int argc, char *argv[])
736 {
737         struct editor editor;
738
739         editor.display = display_create(argc, argv);
740         if (editor.display == NULL) {
741                 fprintf(stderr, "failed to create display: %m\n");
742                 return -1;
743         }
744         wl_display_add_global_listener(display_get_display(editor.display),
745                                        global_handler, &editor);
746
747
748         editor.window = window_create(editor.display);
749         editor.widget = frame_create(editor.window, &editor);
750
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"));
754
755         window_set_title(editor.window, "Text Editor");
756
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);
760
761         window_schedule_resize(editor.window, 500, 400);
762
763         display_run(editor.display);
764
765         text_entry_destroy(editor.entry);
766         text_entry_destroy(editor.editor);
767
768         return 0;
769 }