editor: Delete selected text before adding new
[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->text[index] = '\0';
595         strcat(entry->text, entry->text + index + length);
596
597         text_entry_update_layout(entry);
598
599         widget_schedule_redraw(entry->widget);
600 }
601
602 static void
603 text_entry_delete_selected_text(struct text_entry *entry)
604 {
605         uint32_t start_index = entry->anchor < entry->cursor ? entry->anchor : entry->cursor;
606         uint32_t end_index = entry->anchor < entry->cursor ? entry->cursor : entry->anchor;
607
608         if (entry->anchor == entry->cursor)
609                 return;
610
611         text_entry_delete_text(entry, start_index, end_index - start_index);
612
613         entry->anchor = entry->cursor;
614 }
615
616 static void
617 text_entry_draw_selection(struct text_entry *entry, cairo_t *cr)
618 {
619         cairo_text_extents_t extents;
620         uint32_t start_index = entry->anchor < entry->cursor ? entry->anchor : entry->cursor;
621         uint32_t end_index = entry->anchor < entry->cursor ? entry->cursor : entry->anchor;
622         cairo_rectangle_t start;
623         cairo_rectangle_t end;
624
625         if (entry->anchor == entry->cursor)
626                 return;
627
628         text_layout_extents(entry->layout, &extents);
629
630         text_layout_index_to_pos(entry->layout, start_index, &start);
631         text_layout_index_to_pos(entry->layout, end_index, &end);
632
633         cairo_save (cr);
634
635         cairo_set_source_rgba(cr, 0.0, 0.0, 1.0, 1.0);
636         cairo_rectangle(cr,
637                         start.x, extents.y_bearing + extents.height + 2,
638                         end.x - start.x, -extents.height - 4);
639         cairo_fill(cr);
640
641         cairo_rectangle(cr,
642                         start.x, extents.y_bearing + extents.height,
643                         end.x - start.x, -extents.height);
644         cairo_clip(cr);
645         cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0);
646         text_layout_draw(entry->layout, cr);
647
648         cairo_restore (cr);
649 }
650
651 static void
652 text_entry_draw_cursor(struct text_entry *entry, cairo_t *cr)
653 {
654         cairo_text_extents_t extents;
655         cairo_rectangle_t cursor_pos;
656
657         text_layout_extents(entry->layout, &extents);
658         text_layout_get_cursor_pos(entry->layout,
659                                    entry->cursor + entry->preedit_cursor,
660                                    &cursor_pos);
661
662         cairo_set_line_width(cr, 1.0);
663         cairo_move_to(cr, cursor_pos.x, extents.y_bearing + extents.height + 2);
664         cairo_line_to(cr, cursor_pos.x, extents.y_bearing - 2);
665         cairo_stroke(cr);
666 }
667
668 static void
669 text_entry_draw_preedit(struct text_entry *entry, cairo_t *cr)
670 {
671         cairo_text_extents_t extents;
672         cairo_rectangle_t start;
673         cairo_rectangle_t end;
674
675         if (!entry->preedit_text)
676                 return;
677
678         text_layout_extents(entry->layout, &extents);
679
680         text_layout_index_to_pos(entry->layout, entry->cursor, &start);
681         text_layout_index_to_pos(entry->layout,
682                                  entry->cursor + strlen(entry->preedit_text),
683                                  &end);
684
685         cairo_save (cr);
686
687         cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
688         cairo_rectangle(cr,
689                         start.x, 0,
690                         end.x - start.x, 1);
691         cairo_fill(cr);
692
693         cairo_restore (cr);
694 }
695
696 static void
697 text_entry_redraw_handler(struct widget *widget, void *data)
698 {
699         struct text_entry *entry = data;
700         cairo_surface_t *surface;
701         struct rectangle allocation;
702         cairo_t *cr;
703
704         surface = window_get_surface(entry->window);
705         widget_get_allocation(entry->widget, &allocation);
706
707         cr = cairo_create(surface);
708         cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
709         cairo_clip(cr);
710
711         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
712
713         cairo_push_group(cr);
714         cairo_translate(cr, allocation.x, allocation.y);
715
716         cairo_set_source_rgba(cr, 1, 1, 1, 1);
717         cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
718         cairo_fill(cr);
719
720         cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
721
722         if (entry->active) {
723                 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
724                 cairo_set_line_width (cr, 3);
725                 cairo_set_source_rgba(cr, 0, 0, 1, 1.0);
726                 cairo_stroke(cr);
727         }
728
729         cairo_set_source_rgba(cr, 0, 0, 0, 1);
730
731         cairo_translate(cr, 10, allocation.height / 2);
732         text_layout_draw(entry->layout, cr);
733
734         text_entry_draw_selection(entry, cr);
735
736         text_entry_draw_cursor(entry, cr);
737
738         text_entry_draw_preedit(entry, cr);
739
740         cairo_pop_group_to_source(cr);
741         cairo_paint(cr);
742
743         cairo_destroy(cr);
744         cairo_surface_destroy(surface);
745 }
746
747 static int
748 text_entry_motion_handler(struct widget *widget,
749                           struct input *input, uint32_t time,
750                           float x, float y, void *data)
751 {
752         struct text_entry *entry = data;
753         struct rectangle allocation;
754
755         widget_get_allocation(entry->widget, &allocation);
756
757         text_entry_set_cursor_position(entry,
758                                        x - allocation.x,
759                                        y - allocation.y);
760
761         return CURSOR_IBEAM;
762 }
763
764 static void
765 text_entry_button_handler(struct widget *widget,
766                           struct input *input, uint32_t time,
767                           uint32_t button,
768                           enum wl_pointer_button_state state, void *data)
769 {
770         struct text_entry *entry = data;
771         struct rectangle allocation;
772         int32_t x, y;
773
774         widget_get_allocation(entry->widget, &allocation);
775         input_get_position(input, &x, &y);
776
777         if (button != BTN_LEFT) {
778                 return;
779         }
780
781         text_entry_set_cursor_position(entry,
782                                        x - allocation.x,
783                                        y - allocation.y);
784
785         if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
786                 struct wl_seat *seat = input_get_seat(input);
787
788                 text_entry_activate(entry, seat);
789
790                 text_entry_set_anchor_position(entry,
791                                                x - allocation.x,
792                                                y - allocation.y);
793
794                 widget_set_motion_handler(entry->widget, text_entry_motion_handler);
795         } else {
796                 widget_set_motion_handler(entry->widget, NULL);
797         }
798 }
799
800 static void
801 editor_button_handler(struct widget *widget,
802                       struct input *input, uint32_t time,
803                       uint32_t button,
804                       enum wl_pointer_button_state state, void *data)
805 {
806         struct editor *editor = data;
807
808         if (button != BTN_LEFT) {
809                 return;
810         }
811
812         if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
813                 struct wl_seat *seat = input_get_seat(input);
814
815                 text_entry_deactivate(editor->entry, seat);
816                 text_entry_deactivate(editor->editor, seat);
817         }
818 }
819
820 static void
821 global_handler(struct wl_display *display, uint32_t id,
822                const char *interface, uint32_t version, void *data)
823 {
824         struct editor *editor = data;
825
826         if (!strcmp(interface, "text_model_factory")) {
827                 editor->text_model_factory = wl_display_bind(display, id,
828                                                              &text_model_factory_interface);
829         }
830 }
831
832 int
833 main(int argc, char *argv[])
834 {
835         struct editor editor;
836
837         editor.display = display_create(argc, argv);
838         if (editor.display == NULL) {
839                 fprintf(stderr, "failed to create display: %m\n");
840                 return -1;
841         }
842         wl_display_add_global_listener(display_get_display(editor.display),
843                                        global_handler, &editor);
844
845
846         editor.window = window_create(editor.display);
847         editor.widget = frame_create(editor.window, &editor);
848
849         editor.entry = text_entry_create(&editor, "Entry");
850         editor.editor = text_entry_create(&editor, "Editor");
851         text_entry_set_preedit(editor.editor, "preedit", strlen("preedit"));
852
853         window_set_title(editor.window, "Text Editor");
854
855         widget_set_redraw_handler(editor.widget, redraw_handler);
856         widget_set_resize_handler(editor.widget, resize_handler);
857         widget_set_button_handler(editor.widget, editor_button_handler);
858
859         window_schedule_resize(editor.window, 500, 400);
860
861         display_run(editor.display);
862
863         text_entry_destroy(editor.entry);
864         text_entry_destroy(editor.editor);
865
866         return 0;
867 }