blob: 2909109ddd18f8aaf81d742bb55ba85447331749 [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
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +010024#include "config.h"
25
Philipp Brüschweiler591cfca2012-07-11 22:25:29 +020026#include <assert.h>
Jan Arne Petersencba9e472012-06-21 21:52:19 +020027#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30
31#include <linux/input.h>
32#include <cairo.h>
33
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +010034#include <pango/pangocairo.h>
35
Jan Arne Petersencba9e472012-06-21 21:52:19 +020036#include "window.h"
37#include "text-client-protocol.h"
38
39struct text_entry {
40 struct widget *widget;
Jan Arne Petersene829adc2012-08-10 16:47:22 +020041 struct window *window;
Jan Arne Petersencba9e472012-06-21 21:52:19 +020042 char *text;
43 int active;
Jan Arne Petersen7e634a02012-09-09 23:08:36 +020044 uint32_t cursor;
Jan Arne Petersen0e5bd452012-09-09 23:08:38 +020045 uint32_t anchor;
Jan Arne Petersen46535312013-01-16 21:26:38 +010046 struct {
47 char *text;
48 int32_t cursor;
49 char *commit;
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +010050 PangoAttrList *attr_list;
Jan Arne Petersen46535312013-01-16 21:26:38 +010051 } preedit;
52 struct {
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +010053 PangoAttrList *attr_list;
Jan Arne Petersen46535312013-01-16 21:26:38 +010054 int32_t cursor;
55 } preedit_info;
Jan Arne Petersencba9e472012-06-21 21:52:19 +020056 struct text_model *model;
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +010057 PangoLayout *layout;
Jan Arne Petersencd997062012-11-18 19:06:44 +010058 struct {
59 xkb_mod_mask_t shift_mask;
60 } keysym;
Jan Arne Petersenc7d2a982013-01-16 21:26:39 +010061 uint32_t serial;
Jan Arne Petersen0558a932013-01-16 21:26:45 +010062 uint32_t content_purpose;
Jan Arne Petersencba9e472012-06-21 21:52:19 +020063};
64
65struct editor {
Jan Arne Petersen51963742012-08-10 16:47:20 +020066 struct text_model_factory *text_model_factory;
Jan Arne Petersencba9e472012-06-21 21:52:19 +020067 struct display *display;
68 struct window *window;
69 struct widget *widget;
70 struct text_entry *entry;
71 struct text_entry *editor;
Rob Bradford9d1d32b2012-11-18 19:06:49 +010072 struct text_entry *active_entry;
Jan Arne Petersencba9e472012-06-21 21:52:19 +020073};
74
Jan Arne Petersen6345faa2012-11-05 03:26:39 +010075static const char *
76utf8_start_char(const char *text, const char *p)
77{
78 for (; p >= text; --p) {
79 if ((*p & 0xc0) != 0x80)
80 return p;
81 }
82 return NULL;
83}
84
85static const char *
86utf8_prev_char(const char *text, const char *p)
87{
88 if (p > text)
89 return utf8_start_char(text, --p);
90 return NULL;
91}
92
93static const char *
94utf8_end_char(const char *p)
95{
96 while ((*p & 0xc0) == 0x80)
97 p++;
98 return p;
99}
100
101static const char *
102utf8_next_char(const char *p)
103{
104 if (*p != 0)
105 return utf8_end_char(++p);
106 return NULL;
107}
108
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200109static void text_entry_redraw_handler(struct widget *widget, void *data);
110static void text_entry_button_handler(struct widget *widget,
111 struct input *input, uint32_t time,
112 uint32_t button,
113 enum wl_pointer_button_state state, void *data);
Jan Arne Petersen09e7c962012-09-09 23:08:37 +0200114static void text_entry_insert_at_cursor(struct text_entry *entry, const char *text);
Jan Arne Petersen43f4aa82012-09-09 23:08:43 +0200115static void text_entry_set_preedit(struct text_entry *entry,
116 const char *preedit_text,
117 int preedit_cursor);
Jan Arne Petersene202bae2012-09-09 23:08:44 +0200118static void text_entry_delete_text(struct text_entry *entry,
119 uint32_t index, uint32_t length);
Jan Arne Petersene386dd22012-09-17 15:28:09 +0200120static void text_entry_delete_selected_text(struct text_entry *entry);
Jan Arne Petersen4a17fae2013-01-16 21:26:40 +0100121static void text_entry_reset_preedit(struct text_entry *entry);
122static void text_entry_commit_and_reset(struct text_entry *entry);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200123
124static void
125text_model_commit_string(void *data,
126 struct text_model *text_model,
Jan Arne Petersenc7d2a982013-01-16 21:26:39 +0100127 uint32_t serial,
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200128 const char *text,
129 uint32_t index)
130{
131 struct text_entry *entry = data;
132
John Kåre Alsaker011a1ce2012-10-12 12:25:06 +0200133 if (index > strlen(text))
Jan Arne Petersen09e7c962012-09-09 23:08:37 +0200134 fprintf(stderr, "Invalid cursor index %d\n", index);
Jan Arne Petersen09e7c962012-09-09 23:08:37 +0200135
Jan Arne Petersen4a17fae2013-01-16 21:26:40 +0100136 text_entry_reset_preedit(entry);
137
Jan Arne Petersene386dd22012-09-17 15:28:09 +0200138 text_entry_delete_selected_text(entry);
Jan Arne Petersen09e7c962012-09-09 23:08:37 +0200139 text_entry_insert_at_cursor(entry, text);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200140
141 widget_schedule_redraw(entry->widget);
142}
143
Jan Arne Petersen72f60822012-08-10 16:47:19 +0200144static void
145text_model_preedit_string(void *data,
146 struct text_model *text_model,
Jan Arne Petersenc7d2a982013-01-16 21:26:39 +0100147 uint32_t serial,
Jan Arne Petersen72f60822012-08-10 16:47:19 +0200148 const char *text,
Jan Arne Petersen46535312013-01-16 21:26:38 +0100149 const char *commit)
Jan Arne Petersen72f60822012-08-10 16:47:19 +0200150{
Jan Arne Petersen43f4aa82012-09-09 23:08:43 +0200151 struct text_entry *entry = data;
152
Jan Arne Petersene386dd22012-09-17 15:28:09 +0200153 text_entry_delete_selected_text(entry);
Jan Arne Petersen46535312013-01-16 21:26:38 +0100154 text_entry_set_preedit(entry, text, entry->preedit_info.cursor);
Jan Arne Petersen4a17fae2013-01-16 21:26:40 +0100155 entry->preedit.commit = strdup(commit);
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100156 entry->preedit.attr_list = entry->preedit_info.attr_list;
Jan Arne Petersen4a17fae2013-01-16 21:26:40 +0100157
158 entry->preedit_info.cursor = 0;
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100159 entry->preedit_info.attr_list = NULL;
Jan Arne Petersen43f4aa82012-09-09 23:08:43 +0200160
161 widget_schedule_redraw(entry->widget);
Jan Arne Petersen72f60822012-08-10 16:47:19 +0200162}
163
164static void
Jan Arne Petersene202bae2012-09-09 23:08:44 +0200165text_model_delete_surrounding_text(void *data,
166 struct text_model *text_model,
Jan Arne Petersenc7d2a982013-01-16 21:26:39 +0100167 uint32_t serial,
Jan Arne Petersene202bae2012-09-09 23:08:44 +0200168 int32_t index,
169 uint32_t length)
170{
171 struct text_entry *entry = data;
172 uint32_t cursor_index = index + entry->cursor;
Jan Arne Petersen6345faa2012-11-05 03:26:39 +0100173 const char *start, *end;
Jan Arne Petersene202bae2012-09-09 23:08:44 +0200174
175 if (cursor_index > strlen(entry->text)) {
176 fprintf(stderr, "Invalid cursor index %d\n", index);
177 return;
178 }
179
180 if (cursor_index + length > strlen(entry->text)) {
181 fprintf(stderr, "Invalid length %d\n", length);
182 return;
183 }
184
185 if (length == 0)
186 return;
187
Jan Arne Petersen6345faa2012-11-05 03:26:39 +0100188 start = utf8_start_char(entry->text, entry->text + cursor_index);
189 end = utf8_end_char(entry->text + cursor_index + length);
190
191 text_entry_delete_text(entry,
192 start - entry->text,
193 end - start);
Jan Arne Petersene202bae2012-09-09 23:08:44 +0200194}
195
196static void
Jan Arne Petersen72f60822012-08-10 16:47:19 +0200197text_model_preedit_styling(void *data,
Jan Arne Petersen46535312013-01-16 21:26:38 +0100198 struct text_model *text_model,
Jan Arne Petersenc7d2a982013-01-16 21:26:39 +0100199 uint32_t serial,
Jan Arne Petersen46535312013-01-16 21:26:38 +0100200 uint32_t index,
201 uint32_t length,
202 uint32_t style)
Jan Arne Petersen72f60822012-08-10 16:47:19 +0200203{
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100204 struct text_entry *entry = data;
205 PangoAttribute *attr1 = NULL;
206 PangoAttribute *attr2 = NULL;
207
208 if (!entry->preedit_info.attr_list)
209 entry->preedit_info.attr_list = pango_attr_list_new();
210
211 switch (style) {
212 case TEXT_MODEL_PREEDIT_STYLE_DEFAULT:
213 case TEXT_MODEL_PREEDIT_STYLE_UNDERLINE:
214 attr1 = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE);
215 break;
216 case TEXT_MODEL_PREEDIT_STYLE_INCORRECT:
217 attr1 = pango_attr_underline_new(PANGO_UNDERLINE_ERROR);
218 attr2 = pango_attr_underline_color_new(65535, 0, 0);
219 break;
220 case TEXT_MODEL_PREEDIT_STYLE_SELECTION:
221 attr1 = pango_attr_background_new(0.3 * 65535, 0.3 * 65535, 65535);
222 attr2 = pango_attr_foreground_new(65535, 65535, 65535);
223 break;
224 case TEXT_MODEL_PREEDIT_STYLE_HIGHLIGHT:
225 case TEXT_MODEL_PREEDIT_STYLE_ACTIVE:
226 attr1 = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE);
227 attr2 = pango_attr_weight_new(PANGO_WEIGHT_BOLD);
228 break;
229 case TEXT_MODEL_PREEDIT_STYLE_INACTIVE:
230 attr1 = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE);
231 attr2 = pango_attr_foreground_new(0.3 * 65535, 0.3 * 65535, 0.3 * 65535);
232 break;
233 }
234
235 if (attr1) {
236 attr1->start_index = entry->cursor + index;
237 attr1->end_index = entry->cursor + index + length;
238 pango_attr_list_insert(entry->preedit_info.attr_list, attr1);
239 }
240
241 if (attr2) {
242 attr2->start_index = entry->cursor + index;
243 attr2->end_index = entry->cursor + index + length;
244 pango_attr_list_insert(entry->preedit_info.attr_list, attr2);
245 }
Jan Arne Petersen72f60822012-08-10 16:47:19 +0200246}
247
248static void
Jan Arne Petersen46535312013-01-16 21:26:38 +0100249text_model_preedit_cursor(void *data,
250 struct text_model *text_model,
Jan Arne Petersenc7d2a982013-01-16 21:26:39 +0100251 uint32_t serial,
Jan Arne Petersen46535312013-01-16 21:26:38 +0100252 int32_t index)
253{
254 struct text_entry *entry = data;
255
256 entry->preedit_info.cursor = index;
257}
258
259static void
Jan Arne Petersend9be93b2012-11-18 19:06:43 +0100260text_model_modifiers_map(void *data,
261 struct text_model *text_model,
262 struct wl_array *map)
263{
Jan Arne Petersencd997062012-11-18 19:06:44 +0100264 struct text_entry *entry = data;
265
266 entry->keysym.shift_mask = keysym_modifiers_get_mask(map, "Shift");
Jan Arne Petersend9be93b2012-11-18 19:06:43 +0100267}
268
269static void
270text_model_keysym(void *data,
271 struct text_model *text_model,
272 uint32_t serial,
273 uint32_t time,
274 uint32_t key,
275 uint32_t state,
276 uint32_t modifiers)
Jan Arne Petersen72f60822012-08-10 16:47:19 +0200277{
Jan Arne Petersen8aba11d2012-09-17 15:28:07 +0200278 struct text_entry *entry = data;
Jan Arne Petersencd997062012-11-18 19:06:44 +0100279 const char *state_label = "release";
280 const char *key_label = "Unknown";
Jan Arne Petersen6345faa2012-11-05 03:26:39 +0100281 const char *new_char;
Jan Arne Petersence8a4432012-09-09 23:08:45 +0200282
283 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
284 state_label = "pressed";
Jan Arne Petersence8a4432012-09-09 23:08:45 +0200285 }
286
Jan Arne Petersencd997062012-11-18 19:06:44 +0100287 if (key == XKB_KEY_Left ||
288 key == XKB_KEY_Right) {
289 if (state != WL_KEYBOARD_KEY_STATE_RELEASED)
290 return;
291
292 if (key == XKB_KEY_Left)
293 new_char = utf8_prev_char(entry->text, entry->text + entry->cursor);
294 else
295 new_char = utf8_next_char(entry->text + entry->cursor);
296
297 if (new_char != NULL) {
298 entry->cursor = new_char - entry->text;
299 if (!(modifiers & entry->keysym.shift_mask))
300 entry->anchor = entry->cursor;
301 widget_schedule_redraw(entry->widget);
302 }
303
304 return;
305 }
306
Jan Arne Petersen3fb6e712013-01-16 21:26:52 +0100307 if (key == XKB_KEY_BackSpace) {
308 const char *start, *end;
309
310 text_entry_commit_and_reset(entry);
311
312 start = utf8_prev_char(entry->text, entry->text + entry->cursor);
313
314 if (start == NULL)
315 return;
316
317 end = utf8_end_char(entry->text + entry->cursor);
318 text_entry_delete_text(entry,
319 start - entry->text,
320 end - start);
321
322 return;
323 }
324
Jan Arne Petersence8a4432012-09-09 23:08:45 +0200325 switch (key) {
326 case XKB_KEY_Tab:
327 key_label = "Tab";
328 break;
329 case XKB_KEY_KP_Enter:
Jan Arne Petersencd997062012-11-18 19:06:44 +0100330 case XKB_KEY_Return:
Jan Arne Petersence8a4432012-09-09 23:08:45 +0200331 key_label = "Enter";
332 break;
Jan Arne Petersence8a4432012-09-09 23:08:45 +0200333 }
334
335 fprintf(stderr, "%s key was %s.\n", key_label, state_label);
Jan Arne Petersen72f60822012-08-10 16:47:19 +0200336}
337
338static void
339text_model_selection_replacement(void *data,
340 struct text_model *text_model)
341{
342}
343
344static void
345text_model_direction(void *data,
346 struct text_model *text_model)
347{
348}
349
350static void
351text_model_locale(void *data,
352 struct text_model *text_model)
353{
354}
355
Jan Arne Petersende3b6a12012-08-10 16:47:21 +0200356static void
Jan Arne Petersen680275f2012-09-24 14:51:14 +0200357text_model_enter(void *data,
358 struct text_model *text_model,
359 struct wl_surface *surface)
Jan Arne Petersende3b6a12012-08-10 16:47:21 +0200360{
361 struct text_entry *entry = data;
362
Jan Arne Petersen680275f2012-09-24 14:51:14 +0200363 if (surface != window_get_wl_surface(entry->window))
364 return;
365
Jan Arne Petersende3b6a12012-08-10 16:47:21 +0200366 entry->active = 1;
367
368 widget_schedule_redraw(entry->widget);
369}
370
371static void
Jan Arne Petersen680275f2012-09-24 14:51:14 +0200372text_model_leave(void *data,
373 struct text_model *text_model)
Jan Arne Petersende3b6a12012-08-10 16:47:21 +0200374{
375 struct text_entry *entry = data;
376
Jan Arne Petersen4a17fae2013-01-16 21:26:40 +0100377 text_entry_commit_and_reset(entry);
378
Jan Arne Petersende3b6a12012-08-10 16:47:21 +0200379 entry->active = 0;
380
381 widget_schedule_redraw(entry->widget);
382}
383
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200384static const struct text_model_listener text_model_listener = {
Jan Arne Petersen72f60822012-08-10 16:47:19 +0200385 text_model_commit_string,
386 text_model_preedit_string,
Jan Arne Petersene202bae2012-09-09 23:08:44 +0200387 text_model_delete_surrounding_text,
Jan Arne Petersen72f60822012-08-10 16:47:19 +0200388 text_model_preedit_styling,
Jan Arne Petersen46535312013-01-16 21:26:38 +0100389 text_model_preedit_cursor,
Jan Arne Petersend9be93b2012-11-18 19:06:43 +0100390 text_model_modifiers_map,
391 text_model_keysym,
Jan Arne Petersen72f60822012-08-10 16:47:19 +0200392 text_model_selection_replacement,
393 text_model_direction,
Jan Arne Petersende3b6a12012-08-10 16:47:21 +0200394 text_model_locale,
Jan Arne Petersen680275f2012-09-24 14:51:14 +0200395 text_model_enter,
396 text_model_leave
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200397};
398
399static struct text_entry*
400text_entry_create(struct editor *editor, const char *text)
401{
402 struct text_entry *entry;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200403
Jan Arne Petersencd997062012-11-18 19:06:44 +0100404 entry = calloc(1, sizeof *entry);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200405
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200406 entry->widget = widget_add_widget(editor->widget, entry);
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200407 entry->window = editor->window;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200408 entry->text = strdup(text);
409 entry->active = 0;
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200410 entry->cursor = strlen(text);
Jan Arne Petersen0e5bd452012-09-09 23:08:38 +0200411 entry->anchor = entry->cursor;
Jan Arne Petersen4c265182012-09-09 23:08:30 +0200412 entry->model = text_model_factory_create_text_model(editor->text_model_factory);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200413 text_model_add_listener(entry->model, &text_model_listener, entry);
414
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200415 widget_set_redraw_handler(entry->widget, text_entry_redraw_handler);
416 widget_set_button_handler(entry->widget, text_entry_button_handler);
417
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200418 return entry;
419}
420
421static void
422text_entry_destroy(struct text_entry *entry)
423{
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200424 widget_destroy(entry->widget);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200425 text_model_destroy(entry->model);
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100426 g_clear_object(&entry->layout);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200427 free(entry->text);
428 free(entry);
429}
430
431static void
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200432redraw_handler(struct widget *widget, void *data)
433{
434 struct editor *editor = data;
435 cairo_surface_t *surface;
436 struct rectangle allocation;
437 cairo_t *cr;
438
439 surface = window_get_surface(editor->window);
440 widget_get_allocation(editor->widget, &allocation);
441
442 cr = cairo_create(surface);
443 cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
444 cairo_clip(cr);
445
446 cairo_translate(cr, allocation.x, allocation.y);
447
448 /* Draw background */
449 cairo_push_group(cr);
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200450 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200451 cairo_set_source_rgba(cr, 1, 1, 1, 1);
452 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
453 cairo_fill(cr);
454
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200455 cairo_pop_group_to_source(cr);
456 cairo_paint(cr);
457
458 cairo_destroy(cr);
459 cairo_surface_destroy(surface);
460}
461
462static void
463text_entry_allocate(struct text_entry *entry, int32_t x, int32_t y,
464 int32_t width, int32_t height)
465{
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200466 widget_set_allocation(entry->widget, x, y, width, height);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200467}
468
469static void
470resize_handler(struct widget *widget,
471 int32_t width, int32_t height, void *data)
472{
473 struct editor *editor = data;
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200474 struct rectangle allocation;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200475
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200476 widget_get_allocation(editor->widget, &allocation);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200477
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200478 text_entry_allocate(editor->entry,
479 allocation.x + 20, allocation.y + 20,
480 width - 40, height / 2 - 40);
481 text_entry_allocate(editor->editor,
482 allocation.x + 20, allocation.y + height / 2 + 20,
483 width - 40, height / 2 - 40);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200484}
485
486static void
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200487text_entry_activate(struct text_entry *entry,
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200488 struct wl_seat *seat)
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200489{
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200490 struct wl_surface *surface = window_get_wl_surface(entry->window);
491
Jan Arne Petersenc7d2a982013-01-16 21:26:39 +0100492 entry->serial++;
493
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200494 text_model_activate(entry->model,
Jan Arne Petersenc7d2a982013-01-16 21:26:39 +0100495 entry->serial,
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200496 seat,
497 surface);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200498}
499
500static void
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200501text_entry_deactivate(struct text_entry *entry,
502 struct wl_seat *seat)
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200503{
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200504 text_model_deactivate(entry->model,
505 seat);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200506}
507
508static void
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200509text_entry_update_layout(struct text_entry *entry)
510{
511 char *text;
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100512 PangoAttrList *attr_list;
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200513
Philipp Brüschweiler237358b2012-10-02 11:06:51 +0200514 assert(((unsigned int)entry->cursor) <= strlen(entry->text) +
Jan Arne Petersen46535312013-01-16 21:26:38 +0100515 (entry->preedit.text ? strlen(entry->preedit.text) : 0));
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200516
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100517 if (entry->preedit.text) {
518 text = malloc(strlen(entry->text) + strlen(entry->preedit.text) + 1);
519 strncpy(text, entry->text, entry->cursor);
520 strcpy(text + entry->cursor, entry->preedit.text);
521 strcpy(text + entry->cursor + strlen(entry->preedit.text),
522 entry->text + entry->cursor);
523 } else {
524 text = strdup(entry->text);
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200525 }
526
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100527 if (entry->cursor != entry->anchor) {
528 int start_index = MIN(entry->cursor, entry->anchor);
529 int end_index = MAX(entry->cursor, entry->anchor);
530 PangoAttribute *attr;
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200531
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100532 attr_list = pango_attr_list_copy(entry->preedit.attr_list);
533
534 if (!attr_list)
535 attr_list = pango_attr_list_new();
536
537 attr = pango_attr_background_new(0.3 * 65535, 0.3 * 65535, 65535);
538 attr->start_index = start_index;
539 attr->end_index = end_index;
540 pango_attr_list_insert(attr_list, attr);
541
542 attr = pango_attr_foreground_new(65535, 65535, 65535);
543 attr->start_index = start_index;
544 attr->end_index = end_index;
545 pango_attr_list_insert(attr_list, attr);
546 } else {
547 attr_list = pango_attr_list_ref(entry->preedit.attr_list);
548 }
549
550 if (entry->preedit.text && !entry->preedit.attr_list) {
551 PangoAttribute *attr;
552
553 if (!attr_list)
554 attr_list = pango_attr_list_new();
555
556 attr = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE);
557 attr->start_index = entry->cursor;
558 attr->end_index = entry->cursor + strlen(entry->preedit.text);
559 pango_attr_list_insert(attr_list, attr);
560 }
561
562 if (entry->layout) {
563 pango_layout_set_text(entry->layout, text, -1);
564 pango_layout_set_attributes(entry->layout, attr_list);
565 }
566
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200567 free(text);
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100568 pango_attr_list_unref(attr_list);
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200569}
570
571static void
Jan Arne Petersen0558a932013-01-16 21:26:45 +0100572text_entry_update(struct text_entry *entry)
573{
574 text_model_set_content_type(entry->model,
575 TEXT_MODEL_CONTENT_HINT_NONE,
576 entry->content_purpose);
577
578 text_model_set_surrounding_text(entry->model,
579 entry->text,
580 entry->cursor,
581 entry->anchor);
582}
583
584static void
Jan Arne Petersen09e7c962012-09-09 23:08:37 +0200585text_entry_insert_at_cursor(struct text_entry *entry, const char *text)
586{
587 char *new_text = malloc(strlen(entry->text) + strlen(text) + 1);
588
589 strncpy(new_text, entry->text, entry->cursor);
590 strcpy(new_text + entry->cursor, text);
591 strcpy(new_text + entry->cursor + strlen(text),
592 entry->text + entry->cursor);
593
594 free(entry->text);
595 entry->text = new_text;
596 entry->cursor += strlen(text);
Jan Arne Petersen0e5bd452012-09-09 23:08:38 +0200597 entry->anchor += strlen(text);
Jan Arne Petersen09e7c962012-09-09 23:08:37 +0200598
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200599 text_entry_update_layout(entry);
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100600
601 widget_schedule_redraw(entry->widget);
602
Jan Arne Petersen0558a932013-01-16 21:26:45 +0100603 text_entry_update(entry);
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200604}
605
606static void
Jan Arne Petersen4a17fae2013-01-16 21:26:40 +0100607text_entry_reset_preedit(struct text_entry *entry)
608{
609 entry->preedit.cursor = 0;
610
611 free(entry->preedit.text);
612 entry->preedit.text = NULL;
613
614 free(entry->preedit.commit);
615 entry->preedit.commit = NULL;
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100616
617 pango_attr_list_unref(entry->preedit.attr_list);
618 entry->preedit.attr_list = NULL;
Jan Arne Petersen4a17fae2013-01-16 21:26:40 +0100619}
620
621static void
622text_entry_commit_and_reset(struct text_entry *entry)
623{
624 char *commit = NULL;
625
626 if (entry->preedit.commit)
627 commit = strdup(entry->preedit.commit);
628
629 text_entry_reset_preedit(entry);
630 if (commit) {
631 text_entry_insert_at_cursor(entry, commit);
632 free(commit);
633 }
634}
635
636static void
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200637text_entry_set_preedit(struct text_entry *entry,
638 const char *preedit_text,
639 int preedit_cursor)
640{
Jan Arne Petersen4a17fae2013-01-16 21:26:40 +0100641 text_entry_reset_preedit(entry);
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200642
643 if (!preedit_text)
644 return;
645
Jan Arne Petersen46535312013-01-16 21:26:38 +0100646 entry->preedit.text = strdup(preedit_text);
647 entry->preedit.cursor = preedit_cursor;
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200648
649 text_entry_update_layout(entry);
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100650
651 widget_schedule_redraw(entry->widget);
Jan Arne Petersen09e7c962012-09-09 23:08:37 +0200652}
653
Jan Arne Petersen3489ba92013-01-16 21:26:47 +0100654static uint32_t
655text_entry_try_invoke_preedit_action(struct text_entry *entry,
656 int32_t x, int32_t y,
657 uint32_t button,
658 enum wl_pointer_button_state state)
659{
660 int index, trailing;
661 uint32_t cursor;
662
663 if (!entry->preedit.text)
664 return 0;
665
666 pango_layout_xy_to_index(entry->layout,
667 x * PANGO_SCALE, y * PANGO_SCALE,
668 &index, &trailing);
669 cursor = index + trailing;
670
671 if (cursor < entry->cursor ||
672 cursor > entry->cursor + strlen(entry->preedit.text)) {
673 return 0;
674 }
675
676 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
677 text_model_invoke_action(entry->model,
678 button,
679 cursor - entry->cursor);
680
681 return 1;
682}
683
Jan Arne Petersen09e7c962012-09-09 23:08:37 +0200684static void
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200685text_entry_set_cursor_position(struct text_entry *entry,
686 int32_t x, int32_t y)
687{
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100688 int index, trailing;
689
Jan Arne Petersen4a17fae2013-01-16 21:26:40 +0100690 text_entry_commit_and_reset(entry);
691
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100692 pango_layout_xy_to_index(entry->layout,
693 x * PANGO_SCALE, y * PANGO_SCALE,
694 &index, &trailing);
695 entry->cursor = index + trailing;
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200696
Jan Arne Petersenc7d2a982013-01-16 21:26:39 +0100697 entry->serial++;
698
699 text_model_reset(entry->model, entry->serial);
Jan Arne Petersenc1e481e2012-09-09 23:08:46 +0200700
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200701 text_entry_update_layout(entry);
702
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200703 widget_schedule_redraw(entry->widget);
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100704
Jan Arne Petersen0558a932013-01-16 21:26:45 +0100705 text_entry_update(entry);
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200706}
707
708static void
Jan Arne Petersen0e5bd452012-09-09 23:08:38 +0200709text_entry_set_anchor_position(struct text_entry *entry,
710 int32_t x, int32_t y)
711{
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100712 int index, trailing;
713
714 pango_layout_xy_to_index(entry->layout,
715 x * PANGO_SCALE, y * PANGO_SCALE,
716 &index, &trailing);
717 entry->anchor = index + trailing;
718
719 text_entry_update_layout(entry);
Jan Arne Petersen0e5bd452012-09-09 23:08:38 +0200720
721 widget_schedule_redraw(entry->widget);
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100722
Jan Arne Petersen0558a932013-01-16 21:26:45 +0100723 text_entry_update(entry);
Jan Arne Petersen0e5bd452012-09-09 23:08:38 +0200724}
725
726static void
Jan Arne Petersene202bae2012-09-09 23:08:44 +0200727text_entry_delete_text(struct text_entry *entry,
728 uint32_t index, uint32_t length)
729{
730 if (entry->cursor > index)
731 entry->cursor -= length;
732
Jan Arne Petersen80ad1a92012-09-17 15:28:10 +0200733 entry->anchor = entry->cursor;
734
Jan Arne Petersene202bae2012-09-09 23:08:44 +0200735 entry->text[index] = '\0';
736 strcat(entry->text, entry->text + index + length);
737
738 text_entry_update_layout(entry);
739
740 widget_schedule_redraw(entry->widget);
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100741
Jan Arne Petersen0558a932013-01-16 21:26:45 +0100742 text_entry_update(entry);
Jan Arne Petersene202bae2012-09-09 23:08:44 +0200743}
744
745static void
Jan Arne Petersene386dd22012-09-17 15:28:09 +0200746text_entry_delete_selected_text(struct text_entry *entry)
747{
748 uint32_t start_index = entry->anchor < entry->cursor ? entry->anchor : entry->cursor;
749 uint32_t end_index = entry->anchor < entry->cursor ? entry->cursor : entry->anchor;
750
751 if (entry->anchor == entry->cursor)
752 return;
753
754 text_entry_delete_text(entry, start_index, end_index - start_index);
755
756 entry->anchor = entry->cursor;
757}
758
759static void
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200760text_entry_draw_cursor(struct text_entry *entry, cairo_t *cr)
761{
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100762 PangoRectangle extents;
763 PangoRectangle cursor_pos;
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200764
Jan Arne Petersen46535312013-01-16 21:26:38 +0100765 if (entry->preedit.text && entry->preedit.cursor < 0)
766 return;
767
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100768 pango_layout_get_extents(entry->layout, &extents, NULL);
769 pango_layout_get_cursor_pos(entry->layout,
770 entry->cursor + entry->preedit.cursor,
771 &cursor_pos, NULL);
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200772
773 cairo_set_line_width(cr, 1.0);
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100774 cairo_move_to(cr, PANGO_PIXELS(cursor_pos.x), PANGO_PIXELS(extents.height) + 2);
775 cairo_line_to(cr, PANGO_PIXELS(cursor_pos.x), - 2);
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200776 cairo_stroke(cr);
777}
778
Philipp Brüschweiler9f897c72012-10-02 11:06:53 +0200779static const int text_offset_left = 10;
780
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200781static void
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200782text_entry_redraw_handler(struct widget *widget, void *data)
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200783{
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200784 struct text_entry *entry = data;
785 cairo_surface_t *surface;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200786 struct rectangle allocation;
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200787 cairo_t *cr;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200788
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200789 surface = window_get_surface(entry->window);
790 widget_get_allocation(entry->widget, &allocation);
791
792 cr = cairo_create(surface);
793 cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
794 cairo_clip(cr);
795
796 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
797
798 cairo_push_group(cr);
799 cairo_translate(cr, allocation.x, allocation.y);
800
801 cairo_set_source_rgba(cr, 1, 1, 1, 1);
802 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
803 cairo_fill(cr);
804
805 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
806
807 if (entry->active) {
808 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
809 cairo_set_line_width (cr, 3);
810 cairo_set_source_rgba(cr, 0, 0, 1, 1.0);
811 cairo_stroke(cr);
812 }
813
814 cairo_set_source_rgba(cr, 0, 0, 0, 1);
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200815
Philipp Brüschweiler9f897c72012-10-02 11:06:53 +0200816 cairo_translate(cr, text_offset_left, allocation.height / 2);
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200817
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100818 if (!entry->layout)
819 entry->layout = pango_cairo_create_layout(cr);
820 else
821 pango_cairo_update_layout(cr, entry->layout);
822
823 text_entry_update_layout(entry);
824
825 pango_cairo_show_layout(cr, entry->layout);
Jan Arne Petersen0e5bd452012-09-09 23:08:38 +0200826
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200827 text_entry_draw_cursor(entry, cr);
828
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200829 cairo_pop_group_to_source(cr);
830 cairo_paint(cr);
831
832 cairo_destroy(cr);
833 cairo_surface_destroy(surface);
834}
835
Jan Arne Petersen0e5bd452012-09-09 23:08:38 +0200836static int
837text_entry_motion_handler(struct widget *widget,
838 struct input *input, uint32_t time,
839 float x, float y, void *data)
840{
841 struct text_entry *entry = data;
842 struct rectangle allocation;
843
844 widget_get_allocation(entry->widget, &allocation);
845
846 text_entry_set_cursor_position(entry,
Philipp Brüschweiler9f897c72012-10-02 11:06:53 +0200847 x - allocation.x - text_offset_left,
848 y - allocation.y - text_offset_left);
Jan Arne Petersen0e5bd452012-09-09 23:08:38 +0200849
850 return CURSOR_IBEAM;
851}
852
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200853static void
854text_entry_button_handler(struct widget *widget,
855 struct input *input, uint32_t time,
856 uint32_t button,
857 enum wl_pointer_button_state state, void *data)
858{
859 struct text_entry *entry = data;
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200860 struct rectangle allocation;
Rob Bradford9d1d32b2012-11-18 19:06:49 +0100861 struct editor *editor;
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200862 int32_t x, y;
Jan Arne Petersen3489ba92013-01-16 21:26:47 +0100863 uint32_t result;
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200864
865 widget_get_allocation(entry->widget, &allocation);
866 input_get_position(input, &x, &y);
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200867
Jan Arne Petersen3489ba92013-01-16 21:26:47 +0100868 x -= allocation.x + text_offset_left;
869 y -= allocation.y + text_offset_left;
870
Rob Bradford9d1d32b2012-11-18 19:06:49 +0100871 editor = window_get_user_data(entry->window);
872
Jan Arne Petersen3489ba92013-01-16 21:26:47 +0100873 result = text_entry_try_invoke_preedit_action(entry, x, y, button, state);
874
875 if (result)
876 return;
877
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200878 if (button != BTN_LEFT) {
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200879 return;
880 }
881
Jan Arne Petersen3489ba92013-01-16 21:26:47 +0100882 text_entry_set_cursor_position(entry, x, y);
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200883
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200884 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
885 struct wl_seat *seat = input_get_seat(input);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200886
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200887 text_entry_activate(entry, seat);
Rob Bradford9d1d32b2012-11-18 19:06:49 +0100888 editor->active_entry = entry;
Jan Arne Petersen0e5bd452012-09-09 23:08:38 +0200889
Jan Arne Petersen3489ba92013-01-16 21:26:47 +0100890 text_entry_set_anchor_position(entry, x, y);
Jan Arne Petersen0e5bd452012-09-09 23:08:38 +0200891
892 widget_set_motion_handler(entry->widget, text_entry_motion_handler);
893 } else {
894 widget_set_motion_handler(entry->widget, NULL);
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200895 }
896}
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200897
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200898static void
899editor_button_handler(struct widget *widget,
900 struct input *input, uint32_t time,
901 uint32_t button,
902 enum wl_pointer_button_state state, void *data)
903{
904 struct editor *editor = data;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200905
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200906 if (button != BTN_LEFT) {
907 return;
908 }
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200909
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200910 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
911 struct wl_seat *seat = input_get_seat(input);
912
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200913 text_entry_deactivate(editor->entry, seat);
914 text_entry_deactivate(editor->editor, seat);
Rob Bradford9d1d32b2012-11-18 19:06:49 +0100915 editor->active_entry = NULL;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200916 }
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200917}
918
919static void
Rob Bradford9d1d32b2012-11-18 19:06:49 +0100920key_handler(struct window *window,
921 struct input *input, uint32_t time,
922 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
923 void *data)
924{
925 struct editor *editor = data;
926 struct text_entry *entry;
927 const char *start, *end, *new_char;
928 char text[16];
929
930 if (!editor->active_entry)
931 return;
932
933 entry = editor->active_entry;
934
935 if (state != WL_KEYBOARD_KEY_STATE_PRESSED)
936 return;
937
938 switch (sym) {
939 case XKB_KEY_BackSpace:
Jan Arne Petersen4a17fae2013-01-16 21:26:40 +0100940 text_entry_commit_and_reset(entry);
941
Rob Bradford9d1d32b2012-11-18 19:06:49 +0100942 start = utf8_prev_char(entry->text, entry->text + entry->cursor);
943
944 if (start == NULL)
945 break;
946
947 end = utf8_end_char(entry->text + entry->cursor);
948 text_entry_delete_text(entry,
949 start - entry->text,
950 end - start);
951 break;
952 case XKB_KEY_Delete:
Jan Arne Petersen4a17fae2013-01-16 21:26:40 +0100953 text_entry_commit_and_reset(entry);
954
Rob Bradford9d1d32b2012-11-18 19:06:49 +0100955 start = utf8_start_char(entry->text, entry->text + entry->cursor);
956
957 if (start == NULL)
958 break;
959
960 end = utf8_next_char(start);
961
962 if (end == NULL)
963 break;
964
965 text_entry_delete_text(entry,
966 start - entry->text,
967 end - start);
968 break;
969 case XKB_KEY_Left:
Jan Arne Petersen4a17fae2013-01-16 21:26:40 +0100970 text_entry_commit_and_reset(entry);
971
Rob Bradford9d1d32b2012-11-18 19:06:49 +0100972 new_char = utf8_prev_char(entry->text, entry->text + entry->cursor);
973 if (new_char != NULL) {
974 entry->cursor = new_char - entry->text;
975 entry->anchor = entry->cursor;
976 widget_schedule_redraw(entry->widget);
977 }
978 break;
979 case XKB_KEY_Right:
Jan Arne Petersen4a17fae2013-01-16 21:26:40 +0100980 text_entry_commit_and_reset(entry);
981
Rob Bradford9d1d32b2012-11-18 19:06:49 +0100982 new_char = utf8_next_char(entry->text + entry->cursor);
983 if (new_char != NULL) {
984 entry->cursor = new_char - entry->text;
985 entry->anchor = entry->cursor;
986 widget_schedule_redraw(entry->widget);
987 }
988 break;
989 default:
990 if (xkb_keysym_to_utf8(sym, text, sizeof(text)) <= 0)
991 break;
992
Jan Arne Petersen4a17fae2013-01-16 21:26:40 +0100993 text_entry_commit_and_reset(entry);
994
Rob Bradford9d1d32b2012-11-18 19:06:49 +0100995 text_entry_insert_at_cursor(entry, text);
996 break;
997 }
998
999 widget_schedule_redraw(entry->widget);
1000}
1001
1002static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001003global_handler(struct display *display, uint32_t name,
Jan Arne Petersencba9e472012-06-21 21:52:19 +02001004 const char *interface, uint32_t version, void *data)
1005{
1006 struct editor *editor = data;
1007
Jan Arne Petersen51963742012-08-10 16:47:20 +02001008 if (!strcmp(interface, "text_model_factory")) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001009 editor->text_model_factory =
1010 display_bind(display, name,
1011 &text_model_factory_interface, 1);
Jan Arne Petersencba9e472012-06-21 21:52:19 +02001012 }
1013}
1014
1015int
1016main(int argc, char *argv[])
1017{
1018 struct editor editor;
1019
Jan Arne Petersen25f6db52012-11-05 03:26:40 +01001020 memset(&editor, 0, sizeof editor);
1021
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +01001022#ifdef HAVE_PANGO
1023 g_type_init();
1024#endif
1025
Jan Arne Petersencba9e472012-06-21 21:52:19 +02001026 editor.display = display_create(argc, argv);
1027 if (editor.display == NULL) {
1028 fprintf(stderr, "failed to create display: %m\n");
1029 return -1;
1030 }
Jan Arne Petersencba9e472012-06-21 21:52:19 +02001031
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001032 display_set_user_data(editor.display, &editor);
1033 display_set_global_handler(editor.display, global_handler);
Jan Arne Petersencba9e472012-06-21 21:52:19 +02001034
1035 editor.window = window_create(editor.display);
1036 editor.widget = frame_create(editor.window, &editor);
1037
1038 editor.entry = text_entry_create(&editor, "Entry");
Jan Arne Petersen0558a932013-01-16 21:26:45 +01001039 editor.editor = text_entry_create(&editor, "Numeric");
1040 editor.editor->content_purpose = TEXT_MODEL_CONTENT_PURPOSE_NUMBER;
Jan Arne Petersencba9e472012-06-21 21:52:19 +02001041
1042 window_set_title(editor.window, "Text Editor");
Rob Bradford9d1d32b2012-11-18 19:06:49 +01001043 window_set_key_handler(editor.window, key_handler);
1044 window_set_user_data(editor.window, &editor);
Jan Arne Petersencba9e472012-06-21 21:52:19 +02001045
1046 widget_set_redraw_handler(editor.widget, redraw_handler);
1047 widget_set_resize_handler(editor.widget, resize_handler);
Jan Arne Petersenf80bc062012-09-09 23:08:34 +02001048 widget_set_button_handler(editor.widget, editor_button_handler);
Jan Arne Petersencba9e472012-06-21 21:52:19 +02001049
1050 window_schedule_resize(editor.window, 500, 400);
1051
1052 display_run(editor.display);
1053
1054 text_entry_destroy(editor.entry);
1055 text_entry_destroy(editor.editor);
1056
1057 return 0;
1058}