blob: b0b400eb566b378bd5d2614395a991020682169b [file] [log] [blame]
Jan Arne Petersencba9e472012-06-21 21:52:19 +02001/*
2 * Copyright © 2012 Openismus GmbH
Jan Arne Petersen4c265182012-09-09 23:08:30 +02003 * Copyright © 2012 Intel Corporation
Jan Arne Petersencba9e472012-06-21 21:52:19 +02004 *
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
Philipp Brüschweiler591cfca2012-07-11 22:25:29 +020024#include <assert.h>
Jan Arne Petersencba9e472012-06-21 21:52:19 +020025#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
Jan Arne Petersenb9eb02c2012-09-09 23:08:35 +020035static const char *font_name = "sans-serif";
36static int font_size = 14;
37
38struct 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
Jan Arne Petersencba9e472012-06-21 21:52:19 +020047struct text_entry {
48 struct widget *widget;
Jan Arne Petersene829adc2012-08-10 16:47:22 +020049 struct window *window;
Jan Arne Petersencba9e472012-06-21 21:52:19 +020050 char *text;
51 int active;
Jan Arne Petersen7e634a02012-09-09 23:08:36 +020052 uint32_t cursor;
Jan Arne Petersen0e5bd452012-09-09 23:08:38 +020053 uint32_t anchor;
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +020054 char *preedit_text;
55 uint32_t preedit_cursor;
Jan Arne Petersencba9e472012-06-21 21:52:19 +020056 struct text_model *model;
Jan Arne Petersenb9eb02c2012-09-09 23:08:35 +020057 struct text_layout *layout;
Jan Arne Petersencba9e472012-06-21 21:52:19 +020058};
59
60struct editor {
Jan Arne Petersen51963742012-08-10 16:47:20 +020061 struct text_model_factory *text_model_factory;
Jan Arne Petersencba9e472012-06-21 21:52:19 +020062 struct display *display;
63 struct window *window;
64 struct widget *widget;
65 struct text_entry *entry;
66 struct text_entry *editor;
67};
68
Jan Arne Petersenb9eb02c2012-09-09 23:08:35 +020069static struct text_layout *
70text_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
99static void
100text_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
113static void
114text_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
134static void
135text_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
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200143static void
144text_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
151static int
152text_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
173static void
174text_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
199static void
200text_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}
Jan Arne Petersenb9eb02c2012-09-09 23:08:35 +0200205
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200206static void text_entry_redraw_handler(struct widget *widget, void *data);
207static 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);
Jan Arne Petersen09e7c962012-09-09 23:08:37 +0200211static void text_entry_insert_at_cursor(struct text_entry *entry, const char *text);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200212
213static void
214text_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
Jan Arne Petersen09e7c962012-09-09 23:08:37 +0200221 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);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200227
228 widget_schedule_redraw(entry->widget);
229}
230
Jan Arne Petersen72f60822012-08-10 16:47:19 +0200231static void
232text_model_preedit_string(void *data,
233 struct text_model *text_model,
234 const char *text,
235 uint32_t index)
236{
237}
238
239static void
240text_model_preedit_styling(void *data,
241 struct text_model *text_model)
242{
243}
244
245static void
246text_model_key(void *data,
247 struct text_model *text_model)
248{
249}
250
251static void
252text_model_selection_replacement(void *data,
253 struct text_model *text_model)
254{
255}
256
257static void
258text_model_direction(void *data,
259 struct text_model *text_model)
260{
261}
262
263static void
264text_model_locale(void *data,
265 struct text_model *text_model)
266{
267}
268
Jan Arne Petersende3b6a12012-08-10 16:47:21 +0200269static void
270text_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
280static void
281text_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
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200291static const struct text_model_listener text_model_listener = {
Jan Arne Petersen72f60822012-08-10 16:47:19 +0200292 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,
Jan Arne Petersende3b6a12012-08-10 16:47:21 +0200298 text_model_locale,
299 text_model_activated,
300 text_model_deactivated
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200301};
302
303static struct text_entry*
304text_entry_create(struct editor *editor, const char *text)
305{
306 struct text_entry *entry;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200307
308 entry = malloc(sizeof *entry);
309
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200310 entry->widget = widget_add_widget(editor->widget, entry);
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200311 entry->window = editor->window;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200312 entry->text = strdup(text);
313 entry->active = 0;
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200314 entry->cursor = strlen(text);
Jan Arne Petersen0e5bd452012-09-09 23:08:38 +0200315 entry->anchor = entry->cursor;
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200316 entry->preedit_text = NULL;
317 entry->preedit_cursor = 0;
Jan Arne Petersen4c265182012-09-09 23:08:30 +0200318 entry->model = text_model_factory_create_text_model(editor->text_model_factory);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200319 text_model_add_listener(entry->model, &text_model_listener, entry);
320
Jan Arne Petersenb9eb02c2012-09-09 23:08:35 +0200321 entry->layout = text_layout_create();
322 text_layout_set_text(entry->layout, entry->text);
323
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200324 widget_set_redraw_handler(entry->widget, text_entry_redraw_handler);
325 widget_set_button_handler(entry->widget, text_entry_button_handler);
326
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200327 return entry;
328}
329
330static void
331text_entry_destroy(struct text_entry *entry)
332{
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200333 widget_destroy(entry->widget);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200334 text_model_destroy(entry->model);
Jan Arne Petersenb9eb02c2012-09-09 23:08:35 +0200335 text_layout_destroy(entry->layout);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200336 free(entry->text);
337 free(entry);
338}
339
340static void
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200341redraw_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);
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200359 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200360 cairo_set_source_rgba(cr, 1, 1, 1, 1);
361 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
362 cairo_fill(cr);
363
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200364 cairo_pop_group_to_source(cr);
365 cairo_paint(cr);
366
367 cairo_destroy(cr);
368 cairo_surface_destroy(surface);
369}
370
371static void
372text_entry_allocate(struct text_entry *entry, int32_t x, int32_t y,
373 int32_t width, int32_t height)
374{
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200375 widget_set_allocation(entry->widget, x, y, width, height);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200376}
377
378static void
379resize_handler(struct widget *widget,
380 int32_t width, int32_t height, void *data)
381{
382 struct editor *editor = data;
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200383 struct rectangle allocation;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200384
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200385 widget_get_allocation(editor->widget, &allocation);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200386
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200387 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);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200393}
394
395static void
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200396text_entry_activate(struct text_entry *entry,
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200397 struct wl_seat *seat)
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200398{
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200399 struct wl_surface *surface = window_get_wl_surface(entry->window);
400
401 text_model_activate(entry->model,
402 seat,
403 surface);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200404}
405
406static void
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200407text_entry_deactivate(struct text_entry *entry,
408 struct wl_seat *seat)
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200409{
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200410 text_model_deactivate(entry->model,
411 seat);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200412}
413
414static void
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200415text_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);
Jan Arne Petersencb08f4d2012-09-09 23:08:40 +0200436
437 text_model_set_surrounding_text(entry->model,
438 entry->text,
439 entry->cursor,
440 entry->anchor);
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200441}
442
443static void
Jan Arne Petersen09e7c962012-09-09 23:08:37 +0200444text_entry_insert_at_cursor(struct text_entry *entry, const char *text)
445{
446 char *new_text = malloc(strlen(entry->text) + strlen(text) + 1);
447
448 strncpy(new_text, entry->text, entry->cursor);
449 strcpy(new_text + entry->cursor, text);
450 strcpy(new_text + entry->cursor + strlen(text),
451 entry->text + entry->cursor);
452
453 free(entry->text);
454 entry->text = new_text;
455 entry->cursor += strlen(text);
Jan Arne Petersen0e5bd452012-09-09 23:08:38 +0200456 entry->anchor += strlen(text);
Jan Arne Petersen09e7c962012-09-09 23:08:37 +0200457
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200458 text_entry_update_layout(entry);
459}
460
461static void
462text_entry_set_preedit(struct text_entry *entry,
463 const char *preedit_text,
464 int preedit_cursor)
465{
466 if (entry->preedit_text) {
467 free(entry->preedit_text);
468 entry->preedit_text = NULL;
469 entry->preedit_cursor = 0;
470 }
471
472 if (!preedit_text)
473 return;
474
475 entry->preedit_text = strdup(preedit_text);
476 entry->preedit_cursor = preedit_cursor;
477
478 text_entry_update_layout(entry);
Jan Arne Petersen09e7c962012-09-09 23:08:37 +0200479}
480
481static void
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200482text_entry_set_cursor_position(struct text_entry *entry,
483 int32_t x, int32_t y)
484{
485 entry->cursor = text_layout_xy_to_index(entry->layout, x, y);
486
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200487 if (entry->cursor >= entry->preedit_cursor) {
488 entry->cursor -= entry->preedit_cursor;
489 }
490
491 text_entry_update_layout(entry);
492
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200493 widget_schedule_redraw(entry->widget);
494}
495
496static void
Jan Arne Petersen0e5bd452012-09-09 23:08:38 +0200497text_entry_set_anchor_position(struct text_entry *entry,
498 int32_t x, int32_t y)
499{
500 entry->anchor = text_layout_xy_to_index(entry->layout, x, y);
501
502 widget_schedule_redraw(entry->widget);
503}
504
505static void
506text_entry_draw_selection(struct text_entry *entry, cairo_t *cr)
507{
508 cairo_text_extents_t extents;
509 uint32_t start_index = entry->anchor < entry->cursor ? entry->anchor : entry->cursor;
510 uint32_t end_index = entry->anchor < entry->cursor ? entry->cursor : entry->anchor;
511 cairo_rectangle_t start;
512 cairo_rectangle_t end;
513
514 if (entry->anchor == entry->cursor)
515 return;
516
517 text_layout_extents(entry->layout, &extents);
518
519 text_layout_index_to_pos(entry->layout, start_index, &start);
520 text_layout_index_to_pos(entry->layout, end_index, &end);
521
522 cairo_save (cr);
523
524 cairo_set_source_rgba(cr, 0.0, 0.0, 1.0, 1.0);
525 cairo_rectangle(cr,
526 start.x, extents.y_bearing + extents.height + 2,
527 end.x - start.x, -extents.height - 4);
528 cairo_fill(cr);
529
530 cairo_rectangle(cr,
531 start.x, extents.y_bearing + extents.height,
532 end.x - start.x, -extents.height);
533 cairo_clip(cr);
534 cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0);
535 text_layout_draw(entry->layout, cr);
536
537 cairo_restore (cr);
538}
539
540static void
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200541text_entry_draw_cursor(struct text_entry *entry, cairo_t *cr)
542{
543 cairo_text_extents_t extents;
544 cairo_rectangle_t cursor_pos;
545
546 text_layout_extents(entry->layout, &extents);
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200547 text_layout_get_cursor_pos(entry->layout,
548 entry->cursor + entry->preedit_cursor,
549 &cursor_pos);
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200550
551 cairo_set_line_width(cr, 1.0);
552 cairo_move_to(cr, cursor_pos.x, extents.y_bearing + extents.height + 2);
553 cairo_line_to(cr, cursor_pos.x, extents.y_bearing - 2);
554 cairo_stroke(cr);
555}
556
557static void
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200558text_entry_draw_preedit(struct text_entry *entry, cairo_t *cr)
559{
560 cairo_text_extents_t extents;
561 cairo_rectangle_t start;
562 cairo_rectangle_t end;
563
564 if (!entry->preedit_text)
565 return;
566
567 text_layout_extents(entry->layout, &extents);
568
569 text_layout_index_to_pos(entry->layout, entry->cursor, &start);
570 text_layout_index_to_pos(entry->layout,
571 entry->cursor + strlen(entry->preedit_text),
572 &end);
573
574 cairo_save (cr);
575
576 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
577 cairo_rectangle(cr,
578 start.x, 0,
579 end.x - start.x, 1);
580 cairo_fill(cr);
581
582 cairo_restore (cr);
583}
584
585static void
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200586text_entry_redraw_handler(struct widget *widget, void *data)
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200587{
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200588 struct text_entry *entry = data;
589 cairo_surface_t *surface;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200590 struct rectangle allocation;
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200591 cairo_t *cr;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200592
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200593 surface = window_get_surface(entry->window);
594 widget_get_allocation(entry->widget, &allocation);
595
596 cr = cairo_create(surface);
597 cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
598 cairo_clip(cr);
599
600 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
601
602 cairo_push_group(cr);
603 cairo_translate(cr, allocation.x, allocation.y);
604
605 cairo_set_source_rgba(cr, 1, 1, 1, 1);
606 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
607 cairo_fill(cr);
608
609 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
610
611 if (entry->active) {
612 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
613 cairo_set_line_width (cr, 3);
614 cairo_set_source_rgba(cr, 0, 0, 1, 1.0);
615 cairo_stroke(cr);
616 }
617
618 cairo_set_source_rgba(cr, 0, 0, 0, 1);
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200619
620 cairo_translate(cr, 10, allocation.height / 2);
Jan Arne Petersenb9eb02c2012-09-09 23:08:35 +0200621 text_layout_draw(entry->layout, cr);
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200622
Jan Arne Petersen0e5bd452012-09-09 23:08:38 +0200623 text_entry_draw_selection(entry, cr);
624
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200625 text_entry_draw_cursor(entry, cr);
626
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200627 text_entry_draw_preedit(entry, cr);
628
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200629 cairo_pop_group_to_source(cr);
630 cairo_paint(cr);
631
632 cairo_destroy(cr);
633 cairo_surface_destroy(surface);
634}
635
Jan Arne Petersen0e5bd452012-09-09 23:08:38 +0200636static int
637text_entry_motion_handler(struct widget *widget,
638 struct input *input, uint32_t time,
639 float x, float y, void *data)
640{
641 struct text_entry *entry = data;
642 struct rectangle allocation;
643
644 widget_get_allocation(entry->widget, &allocation);
645
646 text_entry_set_cursor_position(entry,
647 x - allocation.x,
648 y - allocation.y);
649
650 return CURSOR_IBEAM;
651}
652
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200653static void
654text_entry_button_handler(struct widget *widget,
655 struct input *input, uint32_t time,
656 uint32_t button,
657 enum wl_pointer_button_state state, void *data)
658{
659 struct text_entry *entry = data;
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200660 struct rectangle allocation;
661 int32_t x, y;
662
663 widget_get_allocation(entry->widget, &allocation);
664 input_get_position(input, &x, &y);
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200665
666 if (button != BTN_LEFT) {
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200667 return;
668 }
669
Jan Arne Petersen0e5bd452012-09-09 23:08:38 +0200670 text_entry_set_cursor_position(entry,
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200671 x - allocation.x,
672 y - allocation.y);
673
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200674 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
675 struct wl_seat *seat = input_get_seat(input);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200676
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200677 text_entry_activate(entry, seat);
Jan Arne Petersen0e5bd452012-09-09 23:08:38 +0200678
679 text_entry_set_anchor_position(entry,
680 x - allocation.x,
681 y - allocation.y);
682
683 widget_set_motion_handler(entry->widget, text_entry_motion_handler);
684 } else {
685 widget_set_motion_handler(entry->widget, NULL);
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200686 }
687}
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200688
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200689static void
690editor_button_handler(struct widget *widget,
691 struct input *input, uint32_t time,
692 uint32_t button,
693 enum wl_pointer_button_state state, void *data)
694{
695 struct editor *editor = data;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200696
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200697 if (button != BTN_LEFT) {
698 return;
699 }
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200700
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200701 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
702 struct wl_seat *seat = input_get_seat(input);
703
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200704 text_entry_deactivate(editor->entry, seat);
705 text_entry_deactivate(editor->editor, seat);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200706 }
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200707}
708
709static void
710global_handler(struct wl_display *display, uint32_t id,
711 const char *interface, uint32_t version, void *data)
712{
713 struct editor *editor = data;
714
Jan Arne Petersen51963742012-08-10 16:47:20 +0200715 if (!strcmp(interface, "text_model_factory")) {
716 editor->text_model_factory = wl_display_bind(display, id,
717 &text_model_factory_interface);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200718 }
719}
720
721int
722main(int argc, char *argv[])
723{
724 struct editor editor;
725
726 editor.display = display_create(argc, argv);
727 if (editor.display == NULL) {
728 fprintf(stderr, "failed to create display: %m\n");
729 return -1;
730 }
731 wl_display_add_global_listener(display_get_display(editor.display),
732 global_handler, &editor);
733
734
735 editor.window = window_create(editor.display);
736 editor.widget = frame_create(editor.window, &editor);
737
738 editor.entry = text_entry_create(&editor, "Entry");
739 editor.editor = text_entry_create(&editor, "Editor");
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200740 text_entry_set_preedit(editor.editor, "preedit", strlen("preedit"));
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200741
742 window_set_title(editor.window, "Text Editor");
743
744 widget_set_redraw_handler(editor.widget, redraw_handler);
745 widget_set_resize_handler(editor.widget, resize_handler);
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200746 widget_set_button_handler(editor.widget, editor_button_handler);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200747
748 window_schedule_resize(editor.window, 500, 400);
749
750 display_run(editor.display);
751
752 text_entry_destroy(editor.entry);
753 text_entry_destroy(editor.editor);
754
755 return 0;
756}