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