editor: Add support for setting a preedit
[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
213 static void
214 text_model_commit_string(void *data,
215                          struct text_model *text_model,
216                          const char *text,
217                          uint32_t index)
218 {
219         struct text_entry *entry = data;
220
221         if (index > strlen(text)) {
222                 fprintf(stderr, "Invalid cursor index %d\n", index);
223                 index = strlen(text);
224         }
225
226         text_entry_insert_at_cursor(entry, text);
227
228         widget_schedule_redraw(entry->widget);
229 }
230
231 static void
232 text_model_preedit_string(void *data,
233                           struct text_model *text_model,
234                           const char *text,
235                           uint32_t index)
236 {
237 }
238
239 static void
240 text_model_preedit_styling(void *data,
241                            struct text_model *text_model)
242 {
243 }
244
245 static void
246 text_model_key(void *data,
247                struct text_model *text_model)
248 {
249 }
250
251 static void
252 text_model_selection_replacement(void *data,
253                                  struct text_model *text_model)
254 {
255 }
256
257 static void
258 text_model_direction(void *data,
259                      struct text_model *text_model)
260 {
261 }
262
263 static void
264 text_model_locale(void *data,
265                   struct text_model *text_model)
266 {
267 }
268
269 static void
270 text_model_activated(void *data,
271                      struct text_model *text_model)
272 {
273         struct text_entry *entry = data;
274
275         entry->active = 1;
276
277         widget_schedule_redraw(entry->widget);
278 }
279
280 static void
281 text_model_deactivated(void *data,
282                        struct text_model *text_model)
283 {
284         struct text_entry *entry = data;
285
286         entry->active = 0;
287
288         widget_schedule_redraw(entry->widget);
289 }
290
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,
295         text_model_key,
296         text_model_selection_replacement,
297         text_model_direction,
298         text_model_locale,
299         text_model_activated,
300         text_model_deactivated
301 };
302
303 static struct text_entry*
304 text_entry_create(struct editor *editor, const char *text)
305 {
306         struct text_entry *entry;
307
308         entry = malloc(sizeof *entry);
309
310         entry->widget = widget_add_widget(editor->widget, entry);
311         entry->window = editor->window;
312         entry->text = strdup(text);
313         entry->active = 0;
314         entry->cursor = strlen(text);
315         entry->anchor = entry->cursor;
316         entry->preedit_text = NULL;
317         entry->preedit_cursor = 0;
318         entry->model = text_model_factory_create_text_model(editor->text_model_factory);
319         text_model_add_listener(entry->model, &text_model_listener, entry);
320
321         entry->layout = text_layout_create();
322         text_layout_set_text(entry->layout, entry->text);
323
324         widget_set_redraw_handler(entry->widget, text_entry_redraw_handler);
325         widget_set_button_handler(entry->widget, text_entry_button_handler);
326
327         return entry;
328 }
329
330 static void
331 text_entry_destroy(struct text_entry *entry)
332 {
333         widget_destroy(entry->widget);
334         text_model_destroy(entry->model);
335         text_layout_destroy(entry->layout);
336         free(entry->text);
337         free(entry);
338 }
339
340 static void
341 redraw_handler(struct widget *widget, void *data)
342 {
343         struct editor *editor = data;
344         cairo_surface_t *surface;
345         struct rectangle allocation;
346         cairo_t *cr;
347
348         surface = window_get_surface(editor->window);
349         widget_get_allocation(editor->widget, &allocation);
350
351         cr = cairo_create(surface);
352         cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
353         cairo_clip(cr);
354
355         cairo_translate(cr, allocation.x, allocation.y);
356
357         /* Draw background */
358         cairo_push_group(cr);
359         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
360         cairo_set_source_rgba(cr, 1, 1, 1, 1);
361         cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
362         cairo_fill(cr);
363
364         cairo_pop_group_to_source(cr);
365         cairo_paint(cr);
366
367         cairo_destroy(cr);
368         cairo_surface_destroy(surface);
369 }
370
371 static void
372 text_entry_allocate(struct text_entry *entry, int32_t x, int32_t y,
373                     int32_t width, int32_t height)
374 {
375         widget_set_allocation(entry->widget, x, y, width, height);
376 }
377
378 static void
379 resize_handler(struct widget *widget,
380                int32_t width, int32_t height, void *data)
381 {
382         struct editor *editor = data;
383         struct rectangle allocation;
384
385         widget_get_allocation(editor->widget, &allocation);
386
387         text_entry_allocate(editor->entry,
388                             allocation.x + 20, allocation.y + 20,
389                             width - 40, height / 2 - 40);
390         text_entry_allocate(editor->editor,
391                             allocation.x + 20, allocation.y + height / 2 + 20,
392                             width - 40, height / 2 - 40);
393 }
394
395 static void
396 text_entry_activate(struct text_entry *entry,
397                     struct wl_seat *seat)
398 {
399         struct wl_surface *surface = window_get_wl_surface(entry->window);
400
401         text_model_activate(entry->model,
402                             seat,
403                             surface);
404 }
405
406 static void
407 text_entry_deactivate(struct text_entry *entry,
408                       struct wl_seat *seat)
409 {
410         text_model_deactivate(entry->model,
411                               seat);
412 }
413
414 static void
415 text_entry_update_layout(struct text_entry *entry)
416 {
417         char *text;
418
419         assert(((unsigned int)entry->cursor) <= strlen(entry->text));
420
421         if (!entry->preedit_text) {
422                 text_layout_set_text(entry->layout, entry->text);
423                 return;
424         }
425
426         text = malloc(strlen(entry->text) + strlen(entry->preedit_text) + 1);
427         strncpy(text, entry->text, entry->cursor);
428         strcpy(text + entry->cursor, entry->preedit_text);
429         strcpy(text + entry->cursor + strlen(entry->preedit_text),
430                entry->text + entry->cursor);
431
432         text_layout_set_text(entry->layout, text);
433         free(text);
434
435         widget_schedule_redraw(entry->widget);
436 }
437
438 static void
439 text_entry_insert_at_cursor(struct text_entry *entry, const char *text)
440 {
441         char *new_text = malloc(strlen(entry->text) + strlen(text) + 1);
442
443         strncpy(new_text, entry->text, entry->cursor);
444         strcpy(new_text + entry->cursor, text);
445         strcpy(new_text + entry->cursor + strlen(text),
446                entry->text + entry->cursor);
447
448         free(entry->text);
449         entry->text = new_text;
450         entry->cursor += strlen(text);
451         entry->anchor += strlen(text);
452
453         text_entry_update_layout(entry);
454 }
455
456 static void
457 text_entry_set_preedit(struct text_entry *entry,
458                        const char *preedit_text,
459                        int preedit_cursor)
460 {
461         if (entry->preedit_text) {
462                 free(entry->preedit_text);
463                 entry->preedit_text = NULL;
464                 entry->preedit_cursor = 0;
465         }
466
467         if (!preedit_text)
468                 return;
469
470         entry->preedit_text = strdup(preedit_text);
471         entry->preedit_cursor = preedit_cursor;
472
473         text_entry_update_layout(entry);
474 }
475
476 static void
477 text_entry_set_cursor_position(struct text_entry *entry,
478                                int32_t x, int32_t y)
479 {
480         entry->cursor = text_layout_xy_to_index(entry->layout, x, y);
481
482         if (entry->cursor >= entry->preedit_cursor) {
483                 entry->cursor -= entry->preedit_cursor;
484         }
485
486         text_entry_update_layout(entry);
487
488         widget_schedule_redraw(entry->widget);
489 }
490
491 static void
492 text_entry_set_anchor_position(struct text_entry *entry,
493                                int32_t x, int32_t y)
494 {
495         entry->anchor = text_layout_xy_to_index(entry->layout, x, y);
496
497         widget_schedule_redraw(entry->widget);
498 }
499
500 static void
501 text_entry_draw_selection(struct text_entry *entry, cairo_t *cr)
502 {
503         cairo_text_extents_t extents;
504         uint32_t start_index = entry->anchor < entry->cursor ? entry->anchor : entry->cursor;
505         uint32_t end_index = entry->anchor < entry->cursor ? entry->cursor : entry->anchor;
506         cairo_rectangle_t start;
507         cairo_rectangle_t end;
508
509         if (entry->anchor == entry->cursor)
510                 return;
511
512         text_layout_extents(entry->layout, &extents);
513
514         text_layout_index_to_pos(entry->layout, start_index, &start);
515         text_layout_index_to_pos(entry->layout, end_index, &end);
516
517         cairo_save (cr);
518
519         cairo_set_source_rgba(cr, 0.0, 0.0, 1.0, 1.0);
520         cairo_rectangle(cr,
521                         start.x, extents.y_bearing + extents.height + 2,
522                         end.x - start.x, -extents.height - 4);
523         cairo_fill(cr);
524
525         cairo_rectangle(cr,
526                         start.x, extents.y_bearing + extents.height,
527                         end.x - start.x, -extents.height);
528         cairo_clip(cr);
529         cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0);
530         text_layout_draw(entry->layout, cr);
531
532         cairo_restore (cr);
533 }
534
535 static void
536 text_entry_draw_cursor(struct text_entry *entry, cairo_t *cr)
537 {
538         cairo_text_extents_t extents;
539         cairo_rectangle_t cursor_pos;
540
541         text_layout_extents(entry->layout, &extents);
542         text_layout_get_cursor_pos(entry->layout,
543                                    entry->cursor + entry->preedit_cursor,
544                                    &cursor_pos);
545
546         cairo_set_line_width(cr, 1.0);
547         cairo_move_to(cr, cursor_pos.x, extents.y_bearing + extents.height + 2);
548         cairo_line_to(cr, cursor_pos.x, extents.y_bearing - 2);
549         cairo_stroke(cr);
550 }
551
552 static void
553 text_entry_draw_preedit(struct text_entry *entry, cairo_t *cr)
554 {
555         cairo_text_extents_t extents;
556         cairo_rectangle_t start;
557         cairo_rectangle_t end;
558
559         if (!entry->preedit_text)
560                 return;
561
562         text_layout_extents(entry->layout, &extents);
563
564         text_layout_index_to_pos(entry->layout, entry->cursor, &start);
565         text_layout_index_to_pos(entry->layout,
566                                  entry->cursor + strlen(entry->preedit_text),
567                                  &end);
568
569         cairo_save (cr);
570
571         cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
572         cairo_rectangle(cr,
573                         start.x, 0,
574                         end.x - start.x, 1);
575         cairo_fill(cr);
576
577         cairo_restore (cr);
578 }
579
580 static void
581 text_entry_redraw_handler(struct widget *widget, void *data)
582 {
583         struct text_entry *entry = data;
584         cairo_surface_t *surface;
585         struct rectangle allocation;
586         cairo_t *cr;
587
588         surface = window_get_surface(entry->window);
589         widget_get_allocation(entry->widget, &allocation);
590
591         cr = cairo_create(surface);
592         cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
593         cairo_clip(cr);
594
595         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
596
597         cairo_push_group(cr);
598         cairo_translate(cr, allocation.x, allocation.y);
599
600         cairo_set_source_rgba(cr, 1, 1, 1, 1);
601         cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
602         cairo_fill(cr);
603
604         cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
605
606         if (entry->active) {
607                 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
608                 cairo_set_line_width (cr, 3);
609                 cairo_set_source_rgba(cr, 0, 0, 1, 1.0);
610                 cairo_stroke(cr);
611         }
612
613         cairo_set_source_rgba(cr, 0, 0, 0, 1);
614
615         cairo_translate(cr, 10, allocation.height / 2);
616         text_layout_draw(entry->layout, cr);
617
618         text_entry_draw_selection(entry, cr);
619
620         text_entry_draw_cursor(entry, cr);
621
622         text_entry_draw_preedit(entry, cr);
623
624         cairo_pop_group_to_source(cr);
625         cairo_paint(cr);
626
627         cairo_destroy(cr);
628         cairo_surface_destroy(surface);
629 }
630
631 static int
632 text_entry_motion_handler(struct widget *widget,
633                           struct input *input, uint32_t time,
634                           float x, float y, void *data)
635 {
636         struct text_entry *entry = data;
637         struct rectangle allocation;
638
639         widget_get_allocation(entry->widget, &allocation);
640
641         text_entry_set_cursor_position(entry,
642                                        x - allocation.x,
643                                        y - allocation.y);
644
645         return CURSOR_IBEAM;
646 }
647
648 static void
649 text_entry_button_handler(struct widget *widget,
650                           struct input *input, uint32_t time,
651                           uint32_t button,
652                           enum wl_pointer_button_state state, void *data)
653 {
654         struct text_entry *entry = data;
655         struct rectangle allocation;
656         int32_t x, y;
657
658         widget_get_allocation(entry->widget, &allocation);
659         input_get_position(input, &x, &y);
660
661         if (button != BTN_LEFT) {
662                 return;
663         }
664
665         text_entry_set_cursor_position(entry,
666                                        x - allocation.x,
667                                        y - allocation.y);
668
669         if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
670                 struct wl_seat *seat = input_get_seat(input);
671
672                 text_entry_activate(entry, seat);
673
674                 text_entry_set_anchor_position(entry,
675                                                x - allocation.x,
676                                                y - allocation.y);
677
678                 widget_set_motion_handler(entry->widget, text_entry_motion_handler);
679         } else {
680                 widget_set_motion_handler(entry->widget, NULL);
681         }
682 }
683
684 static void
685 editor_button_handler(struct widget *widget,
686                       struct input *input, uint32_t time,
687                       uint32_t button,
688                       enum wl_pointer_button_state state, void *data)
689 {
690         struct editor *editor = data;
691
692         if (button != BTN_LEFT) {
693                 return;
694         }
695
696         if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
697                 struct wl_seat *seat = input_get_seat(input);
698
699                 text_entry_deactivate(editor->entry, seat);
700                 text_entry_deactivate(editor->editor, seat);
701         }
702 }
703
704 static void
705 global_handler(struct wl_display *display, uint32_t id,
706                const char *interface, uint32_t version, void *data)
707 {
708         struct editor *editor = data;
709
710         if (!strcmp(interface, "text_model_factory")) {
711                 editor->text_model_factory = wl_display_bind(display, id,
712                                                              &text_model_factory_interface);
713         }
714 }
715
716 int
717 main(int argc, char *argv[])
718 {
719         struct editor editor;
720
721         editor.display = display_create(argc, argv);
722         if (editor.display == NULL) {
723                 fprintf(stderr, "failed to create display: %m\n");
724                 return -1;
725         }
726         wl_display_add_global_listener(display_get_display(editor.display),
727                                        global_handler, &editor);
728
729
730         editor.window = window_create(editor.display);
731         editor.widget = frame_create(editor.window, &editor);
732
733         editor.entry = text_entry_create(&editor, "Entry");
734         editor.editor = text_entry_create(&editor, "Editor");
735         text_entry_set_preedit(editor.editor, "preedit", strlen("preedit"));
736
737         window_set_title(editor.window, "Text Editor");
738
739         widget_set_redraw_handler(editor.widget, redraw_handler);
740         widget_set_resize_handler(editor.widget, resize_handler);
741         widget_set_button_handler(editor.widget, editor_button_handler);
742
743         window_schedule_resize(editor.window, 500, 400);
744
745         display_run(editor.display);
746
747         text_entry_destroy(editor.entry);
748         text_entry_destroy(editor.editor);
749
750         return 0;
751 }