blob: 0de25000d7a47c8f28284773e39718850272b668 [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 Petersen1cc9e082013-01-31 15:52:23 +010056 struct {
57 int32_t cursor;
58 int32_t anchor;
59 } pending_commit;
Jan Arne Petersencba9e472012-06-21 21:52:19 +020060 struct text_model *model;
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +010061 PangoLayout *layout;
Jan Arne Petersencd997062012-11-18 19:06:44 +010062 struct {
63 xkb_mod_mask_t shift_mask;
64 } keysym;
Jan Arne Petersenc7d2a982013-01-16 21:26:39 +010065 uint32_t serial;
Jan Arne Petersen0558a932013-01-16 21:26:45 +010066 uint32_t content_purpose;
Jan Arne Petersen61381972013-01-31 15:52:21 +010067 uint32_t click_to_show;
Jan Arne Petersencba9e472012-06-21 21:52:19 +020068};
69
70struct editor {
Jan Arne Petersen51963742012-08-10 16:47:20 +020071 struct text_model_factory *text_model_factory;
Jan Arne Petersencba9e472012-06-21 21:52:19 +020072 struct display *display;
73 struct window *window;
74 struct widget *widget;
75 struct text_entry *entry;
76 struct text_entry *editor;
Rob Bradford9d1d32b2012-11-18 19:06:49 +010077 struct text_entry *active_entry;
Jan Arne Petersencba9e472012-06-21 21:52:19 +020078};
79
Jan Arne Petersen6345faa2012-11-05 03:26:39 +010080static const char *
81utf8_start_char(const char *text, const char *p)
82{
83 for (; p >= text; --p) {
84 if ((*p & 0xc0) != 0x80)
85 return p;
86 }
87 return NULL;
88}
89
90static const char *
91utf8_prev_char(const char *text, const char *p)
92{
93 if (p > text)
94 return utf8_start_char(text, --p);
95 return NULL;
96}
97
98static const char *
99utf8_end_char(const char *p)
100{
101 while ((*p & 0xc0) == 0x80)
102 p++;
103 return p;
104}
105
106static const char *
107utf8_next_char(const char *p)
108{
109 if (*p != 0)
110 return utf8_end_char(++p);
111 return NULL;
112}
113
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200114static void text_entry_redraw_handler(struct widget *widget, void *data);
115static void text_entry_button_handler(struct widget *widget,
116 struct input *input, uint32_t time,
117 uint32_t button,
118 enum wl_pointer_button_state state, void *data);
Jan Arne Petersen1cc9e082013-01-31 15:52:23 +0100119static void text_entry_insert_at_cursor(struct text_entry *entry, const char *text,
120 int32_t cursor, int32_t anchor);
Jan Arne Petersen43f4aa82012-09-09 23:08:43 +0200121static void text_entry_set_preedit(struct text_entry *entry,
122 const char *preedit_text,
123 int preedit_cursor);
Jan Arne Petersene202bae2012-09-09 23:08:44 +0200124static void text_entry_delete_text(struct text_entry *entry,
125 uint32_t index, uint32_t length);
Jan Arne Petersene386dd22012-09-17 15:28:09 +0200126static void text_entry_delete_selected_text(struct text_entry *entry);
Jan Arne Petersen4a17fae2013-01-16 21:26:40 +0100127static void text_entry_reset_preedit(struct text_entry *entry);
128static void text_entry_commit_and_reset(struct text_entry *entry);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200129
130static void
131text_model_commit_string(void *data,
132 struct text_model *text_model,
Jan Arne Petersenc7d2a982013-01-16 21:26:39 +0100133 uint32_t serial,
Jan Arne Petersen1cc9e082013-01-31 15:52:23 +0100134 const char *text)
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200135{
136 struct text_entry *entry = data;
137
Jan Arne Petersen4a17fae2013-01-16 21:26:40 +0100138 text_entry_reset_preedit(entry);
139
Jan Arne Petersene386dd22012-09-17 15:28:09 +0200140 text_entry_delete_selected_text(entry);
Jan Arne Petersen1cc9e082013-01-31 15:52:23 +0100141 text_entry_insert_at_cursor(entry, text,
142 entry->pending_commit.cursor,
143 entry->pending_commit.anchor);
144
145 memset(&entry->pending_commit, 0, sizeof entry->pending_commit);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200146
147 widget_schedule_redraw(entry->widget);
148}
149
Jan Arne Petersen72f60822012-08-10 16:47:19 +0200150static void
151text_model_preedit_string(void *data,
152 struct text_model *text_model,
Jan Arne Petersenc7d2a982013-01-16 21:26:39 +0100153 uint32_t serial,
Jan Arne Petersen72f60822012-08-10 16:47:19 +0200154 const char *text,
Jan Arne Petersen46535312013-01-16 21:26:38 +0100155 const char *commit)
Jan Arne Petersen72f60822012-08-10 16:47:19 +0200156{
Jan Arne Petersen43f4aa82012-09-09 23:08:43 +0200157 struct text_entry *entry = data;
158
Jan Arne Petersene386dd22012-09-17 15:28:09 +0200159 text_entry_delete_selected_text(entry);
Jan Arne Petersen46535312013-01-16 21:26:38 +0100160 text_entry_set_preedit(entry, text, entry->preedit_info.cursor);
Jan Arne Petersen4a17fae2013-01-16 21:26:40 +0100161 entry->preedit.commit = strdup(commit);
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100162 entry->preedit.attr_list = entry->preedit_info.attr_list;
Jan Arne Petersen4a17fae2013-01-16 21:26:40 +0100163
164 entry->preedit_info.cursor = 0;
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100165 entry->preedit_info.attr_list = NULL;
Jan Arne Petersen43f4aa82012-09-09 23:08:43 +0200166
167 widget_schedule_redraw(entry->widget);
Jan Arne Petersen72f60822012-08-10 16:47:19 +0200168}
169
170static void
Jan Arne Petersene202bae2012-09-09 23:08:44 +0200171text_model_delete_surrounding_text(void *data,
172 struct text_model *text_model,
Jan Arne Petersenc7d2a982013-01-16 21:26:39 +0100173 uint32_t serial,
Jan Arne Petersene202bae2012-09-09 23:08:44 +0200174 int32_t index,
175 uint32_t length)
176{
177 struct text_entry *entry = data;
178 uint32_t cursor_index = index + entry->cursor;
Jan Arne Petersen6345faa2012-11-05 03:26:39 +0100179 const char *start, *end;
Jan Arne Petersene202bae2012-09-09 23:08:44 +0200180
181 if (cursor_index > strlen(entry->text)) {
182 fprintf(stderr, "Invalid cursor index %d\n", index);
183 return;
184 }
185
186 if (cursor_index + length > strlen(entry->text)) {
187 fprintf(stderr, "Invalid length %d\n", length);
188 return;
189 }
190
191 if (length == 0)
192 return;
193
Jan Arne Petersen6345faa2012-11-05 03:26:39 +0100194 start = utf8_start_char(entry->text, entry->text + cursor_index);
195 end = utf8_end_char(entry->text + cursor_index + length);
196
197 text_entry_delete_text(entry,
198 start - entry->text,
199 end - start);
Jan Arne Petersene202bae2012-09-09 23:08:44 +0200200}
201
202static void
Jan Arne Petersen1cc9e082013-01-31 15:52:23 +0100203text_model_cursor_position(void *data,
204 struct text_model *text_model,
205 uint32_t serial,
206 int32_t index,
207 int32_t anchor)
208{
209 struct text_entry *entry = data;
210
211 entry->pending_commit.cursor = index;
212 entry->pending_commit.anchor = anchor;
213}
214
215static void
Jan Arne Petersen72f60822012-08-10 16:47:19 +0200216text_model_preedit_styling(void *data,
Jan Arne Petersen46535312013-01-16 21:26:38 +0100217 struct text_model *text_model,
Jan Arne Petersenc7d2a982013-01-16 21:26:39 +0100218 uint32_t serial,
Jan Arne Petersen46535312013-01-16 21:26:38 +0100219 uint32_t index,
220 uint32_t length,
221 uint32_t style)
Jan Arne Petersen72f60822012-08-10 16:47:19 +0200222{
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100223 struct text_entry *entry = data;
224 PangoAttribute *attr1 = NULL;
225 PangoAttribute *attr2 = NULL;
226
227 if (!entry->preedit_info.attr_list)
228 entry->preedit_info.attr_list = pango_attr_list_new();
229
230 switch (style) {
231 case TEXT_MODEL_PREEDIT_STYLE_DEFAULT:
232 case TEXT_MODEL_PREEDIT_STYLE_UNDERLINE:
233 attr1 = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE);
234 break;
235 case TEXT_MODEL_PREEDIT_STYLE_INCORRECT:
236 attr1 = pango_attr_underline_new(PANGO_UNDERLINE_ERROR);
237 attr2 = pango_attr_underline_color_new(65535, 0, 0);
238 break;
239 case TEXT_MODEL_PREEDIT_STYLE_SELECTION:
240 attr1 = pango_attr_background_new(0.3 * 65535, 0.3 * 65535, 65535);
241 attr2 = pango_attr_foreground_new(65535, 65535, 65535);
242 break;
243 case TEXT_MODEL_PREEDIT_STYLE_HIGHLIGHT:
244 case TEXT_MODEL_PREEDIT_STYLE_ACTIVE:
245 attr1 = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE);
246 attr2 = pango_attr_weight_new(PANGO_WEIGHT_BOLD);
247 break;
248 case TEXT_MODEL_PREEDIT_STYLE_INACTIVE:
249 attr1 = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE);
250 attr2 = pango_attr_foreground_new(0.3 * 65535, 0.3 * 65535, 0.3 * 65535);
251 break;
252 }
253
254 if (attr1) {
255 attr1->start_index = entry->cursor + index;
256 attr1->end_index = entry->cursor + index + length;
257 pango_attr_list_insert(entry->preedit_info.attr_list, attr1);
258 }
259
260 if (attr2) {
261 attr2->start_index = entry->cursor + index;
262 attr2->end_index = entry->cursor + index + length;
263 pango_attr_list_insert(entry->preedit_info.attr_list, attr2);
264 }
Jan Arne Petersen72f60822012-08-10 16:47:19 +0200265}
266
267static void
Jan Arne Petersen46535312013-01-16 21:26:38 +0100268text_model_preedit_cursor(void *data,
269 struct text_model *text_model,
Jan Arne Petersenc7d2a982013-01-16 21:26:39 +0100270 uint32_t serial,
Jan Arne Petersen46535312013-01-16 21:26:38 +0100271 int32_t index)
272{
273 struct text_entry *entry = data;
274
275 entry->preedit_info.cursor = index;
276}
277
278static void
Jan Arne Petersend9be93b2012-11-18 19:06:43 +0100279text_model_modifiers_map(void *data,
280 struct text_model *text_model,
281 struct wl_array *map)
282{
Jan Arne Petersencd997062012-11-18 19:06:44 +0100283 struct text_entry *entry = data;
284
285 entry->keysym.shift_mask = keysym_modifiers_get_mask(map, "Shift");
Jan Arne Petersend9be93b2012-11-18 19:06:43 +0100286}
287
288static void
289text_model_keysym(void *data,
290 struct text_model *text_model,
291 uint32_t serial,
292 uint32_t time,
293 uint32_t key,
294 uint32_t state,
295 uint32_t modifiers)
Jan Arne Petersen72f60822012-08-10 16:47:19 +0200296{
Jan Arne Petersen8aba11d2012-09-17 15:28:07 +0200297 struct text_entry *entry = data;
Jan Arne Petersencd997062012-11-18 19:06:44 +0100298 const char *state_label = "release";
299 const char *key_label = "Unknown";
Jan Arne Petersen6345faa2012-11-05 03:26:39 +0100300 const char *new_char;
Jan Arne Petersence8a4432012-09-09 23:08:45 +0200301
302 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
303 state_label = "pressed";
Jan Arne Petersence8a4432012-09-09 23:08:45 +0200304 }
305
Jan Arne Petersencd997062012-11-18 19:06:44 +0100306 if (key == XKB_KEY_Left ||
307 key == XKB_KEY_Right) {
308 if (state != WL_KEYBOARD_KEY_STATE_RELEASED)
309 return;
310
311 if (key == XKB_KEY_Left)
312 new_char = utf8_prev_char(entry->text, entry->text + entry->cursor);
313 else
314 new_char = utf8_next_char(entry->text + entry->cursor);
315
316 if (new_char != NULL) {
317 entry->cursor = new_char - entry->text;
318 if (!(modifiers & entry->keysym.shift_mask))
319 entry->anchor = entry->cursor;
320 widget_schedule_redraw(entry->widget);
321 }
322
323 return;
324 }
325
Jan Arne Petersen3fb6e712013-01-16 21:26:52 +0100326 if (key == XKB_KEY_BackSpace) {
327 const char *start, *end;
328
329 text_entry_commit_and_reset(entry);
330
331 start = utf8_prev_char(entry->text, entry->text + entry->cursor);
332
333 if (start == NULL)
334 return;
335
336 end = utf8_end_char(entry->text + entry->cursor);
337 text_entry_delete_text(entry,
338 start - entry->text,
339 end - start);
340
341 return;
342 }
343
Jan Arne Petersence8a4432012-09-09 23:08:45 +0200344 switch (key) {
345 case XKB_KEY_Tab:
346 key_label = "Tab";
347 break;
348 case XKB_KEY_KP_Enter:
Jan Arne Petersencd997062012-11-18 19:06:44 +0100349 case XKB_KEY_Return:
Jan Arne Petersence8a4432012-09-09 23:08:45 +0200350 key_label = "Enter";
351 break;
Jan Arne Petersence8a4432012-09-09 23:08:45 +0200352 }
353
354 fprintf(stderr, "%s key was %s.\n", key_label, state_label);
Jan Arne Petersen72f60822012-08-10 16:47:19 +0200355}
356
357static void
Jan Arne Petersen680275f2012-09-24 14:51:14 +0200358text_model_enter(void *data,
359 struct text_model *text_model,
360 struct wl_surface *surface)
Jan Arne Petersende3b6a12012-08-10 16:47:21 +0200361{
362 struct text_entry *entry = data;
363
Jan Arne Petersen680275f2012-09-24 14:51:14 +0200364 if (surface != window_get_wl_surface(entry->window))
365 return;
366
Jan Arne Petersende3b6a12012-08-10 16:47:21 +0200367 entry->active = 1;
368
369 widget_schedule_redraw(entry->widget);
370}
371
372static void
Jan Arne Petersen680275f2012-09-24 14:51:14 +0200373text_model_leave(void *data,
374 struct text_model *text_model)
Jan Arne Petersende3b6a12012-08-10 16:47:21 +0200375{
376 struct text_entry *entry = data;
377
Jan Arne Petersen4a17fae2013-01-16 21:26:40 +0100378 text_entry_commit_and_reset(entry);
379
Jan Arne Petersende3b6a12012-08-10 16:47:21 +0200380 entry->active = 0;
381
Jan Arne Petersen61381972013-01-31 15:52:21 +0100382 text_model_hide_input_panel(text_model);
383
Jan Arne Petersende3b6a12012-08-10 16:47:21 +0200384 widget_schedule_redraw(entry->widget);
385}
386
Jan Arne Petersen61381972013-01-31 15:52:21 +0100387static void
388text_model_input_panel_state(void *data,
389 struct text_model *text_model,
390 uint32_t state)
391{
392}
393
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200394static const struct text_model_listener text_model_listener = {
Jan Arne Petersen72f60822012-08-10 16:47:19 +0200395 text_model_commit_string,
396 text_model_preedit_string,
Jan Arne Petersene202bae2012-09-09 23:08:44 +0200397 text_model_delete_surrounding_text,
Jan Arne Petersen1cc9e082013-01-31 15:52:23 +0100398 text_model_cursor_position,
Jan Arne Petersen72f60822012-08-10 16:47:19 +0200399 text_model_preedit_styling,
Jan Arne Petersen46535312013-01-16 21:26:38 +0100400 text_model_preedit_cursor,
Jan Arne Petersend9be93b2012-11-18 19:06:43 +0100401 text_model_modifiers_map,
402 text_model_keysym,
Jan Arne Petersen680275f2012-09-24 14:51:14 +0200403 text_model_enter,
Jan Arne Petersen61381972013-01-31 15:52:21 +0100404 text_model_leave,
405 text_model_input_panel_state
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200406};
407
408static struct text_entry*
409text_entry_create(struct editor *editor, const char *text)
410{
411 struct text_entry *entry;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200412
Jan Arne Petersencd997062012-11-18 19:06:44 +0100413 entry = calloc(1, sizeof *entry);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200414
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200415 entry->widget = widget_add_widget(editor->widget, entry);
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200416 entry->window = editor->window;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200417 entry->text = strdup(text);
418 entry->active = 0;
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200419 entry->cursor = strlen(text);
Jan Arne Petersen0e5bd452012-09-09 23:08:38 +0200420 entry->anchor = entry->cursor;
Jan Arne Petersen4c265182012-09-09 23:08:30 +0200421 entry->model = text_model_factory_create_text_model(editor->text_model_factory);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200422 text_model_add_listener(entry->model, &text_model_listener, entry);
423
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200424 widget_set_redraw_handler(entry->widget, text_entry_redraw_handler);
425 widget_set_button_handler(entry->widget, text_entry_button_handler);
426
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200427 return entry;
428}
429
430static void
431text_entry_destroy(struct text_entry *entry)
432{
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200433 widget_destroy(entry->widget);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200434 text_model_destroy(entry->model);
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100435 g_clear_object(&entry->layout);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200436 free(entry->text);
437 free(entry);
438}
439
440static void
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200441redraw_handler(struct widget *widget, void *data)
442{
443 struct editor *editor = data;
444 cairo_surface_t *surface;
445 struct rectangle allocation;
446 cairo_t *cr;
447
448 surface = window_get_surface(editor->window);
449 widget_get_allocation(editor->widget, &allocation);
450
451 cr = cairo_create(surface);
452 cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
453 cairo_clip(cr);
454
455 cairo_translate(cr, allocation.x, allocation.y);
456
457 /* Draw background */
458 cairo_push_group(cr);
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200459 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200460 cairo_set_source_rgba(cr, 1, 1, 1, 1);
461 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
462 cairo_fill(cr);
463
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200464 cairo_pop_group_to_source(cr);
465 cairo_paint(cr);
466
467 cairo_destroy(cr);
468 cairo_surface_destroy(surface);
469}
470
471static void
472text_entry_allocate(struct text_entry *entry, int32_t x, int32_t y,
473 int32_t width, int32_t height)
474{
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200475 widget_set_allocation(entry->widget, x, y, width, height);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200476}
477
478static void
479resize_handler(struct widget *widget,
480 int32_t width, int32_t height, void *data)
481{
482 struct editor *editor = data;
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200483 struct rectangle allocation;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200484
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200485 widget_get_allocation(editor->widget, &allocation);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200486
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200487 text_entry_allocate(editor->entry,
488 allocation.x + 20, allocation.y + 20,
489 width - 40, height / 2 - 40);
490 text_entry_allocate(editor->editor,
491 allocation.x + 20, allocation.y + height / 2 + 20,
492 width - 40, height / 2 - 40);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200493}
494
495static void
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200496text_entry_activate(struct text_entry *entry,
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200497 struct wl_seat *seat)
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200498{
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200499 struct wl_surface *surface = window_get_wl_surface(entry->window);
500
Jan Arne Petersen61381972013-01-31 15:52:21 +0100501 if (entry->click_to_show && entry->active) {
502 text_model_show_input_panel(entry->model);
503
504 return;
505 }
506
507 if (!entry->click_to_show)
508 text_model_show_input_panel(entry->model);
509
Jan Arne Petersenc7d2a982013-01-16 21:26:39 +0100510 entry->serial++;
511
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200512 text_model_activate(entry->model,
Jan Arne Petersenc7d2a982013-01-16 21:26:39 +0100513 entry->serial,
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200514 seat,
515 surface);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200516}
517
518static void
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200519text_entry_deactivate(struct text_entry *entry,
520 struct wl_seat *seat)
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200521{
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200522 text_model_deactivate(entry->model,
523 seat);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200524}
525
526static void
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200527text_entry_update_layout(struct text_entry *entry)
528{
529 char *text;
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100530 PangoAttrList *attr_list;
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200531
Philipp Brüschweiler237358b2012-10-02 11:06:51 +0200532 assert(((unsigned int)entry->cursor) <= strlen(entry->text) +
Jan Arne Petersen46535312013-01-16 21:26:38 +0100533 (entry->preedit.text ? strlen(entry->preedit.text) : 0));
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200534
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100535 if (entry->preedit.text) {
536 text = malloc(strlen(entry->text) + strlen(entry->preedit.text) + 1);
537 strncpy(text, entry->text, entry->cursor);
538 strcpy(text + entry->cursor, entry->preedit.text);
539 strcpy(text + entry->cursor + strlen(entry->preedit.text),
540 entry->text + entry->cursor);
541 } else {
542 text = strdup(entry->text);
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200543 }
544
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100545 if (entry->cursor != entry->anchor) {
546 int start_index = MIN(entry->cursor, entry->anchor);
547 int end_index = MAX(entry->cursor, entry->anchor);
548 PangoAttribute *attr;
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200549
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100550 attr_list = pango_attr_list_copy(entry->preedit.attr_list);
551
552 if (!attr_list)
553 attr_list = pango_attr_list_new();
554
555 attr = pango_attr_background_new(0.3 * 65535, 0.3 * 65535, 65535);
556 attr->start_index = start_index;
557 attr->end_index = end_index;
558 pango_attr_list_insert(attr_list, attr);
559
560 attr = pango_attr_foreground_new(65535, 65535, 65535);
561 attr->start_index = start_index;
562 attr->end_index = end_index;
563 pango_attr_list_insert(attr_list, attr);
564 } else {
565 attr_list = pango_attr_list_ref(entry->preedit.attr_list);
566 }
567
568 if (entry->preedit.text && !entry->preedit.attr_list) {
569 PangoAttribute *attr;
570
571 if (!attr_list)
572 attr_list = pango_attr_list_new();
573
574 attr = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE);
575 attr->start_index = entry->cursor;
576 attr->end_index = entry->cursor + strlen(entry->preedit.text);
577 pango_attr_list_insert(attr_list, attr);
578 }
579
580 if (entry->layout) {
581 pango_layout_set_text(entry->layout, text, -1);
582 pango_layout_set_attributes(entry->layout, attr_list);
583 }
584
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200585 free(text);
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100586 pango_attr_list_unref(attr_list);
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200587}
588
589static void
Jan Arne Petersen0558a932013-01-16 21:26:45 +0100590text_entry_update(struct text_entry *entry)
591{
592 text_model_set_content_type(entry->model,
593 TEXT_MODEL_CONTENT_HINT_NONE,
594 entry->content_purpose);
595
596 text_model_set_surrounding_text(entry->model,
597 entry->text,
598 entry->cursor,
599 entry->anchor);
Jan Arne Petersen0eabcaa2013-01-31 15:52:20 +0100600
601 text_model_commit(entry->model);
Jan Arne Petersen0558a932013-01-16 21:26:45 +0100602}
603
604static void
Jan Arne Petersen1cc9e082013-01-31 15:52:23 +0100605text_entry_insert_at_cursor(struct text_entry *entry, const char *text,
606 int32_t cursor, int32_t anchor)
Jan Arne Petersen09e7c962012-09-09 23:08:37 +0200607{
608 char *new_text = malloc(strlen(entry->text) + strlen(text) + 1);
609
610 strncpy(new_text, entry->text, entry->cursor);
611 strcpy(new_text + entry->cursor, text);
612 strcpy(new_text + entry->cursor + strlen(text),
613 entry->text + entry->cursor);
614
615 free(entry->text);
616 entry->text = new_text;
Jan Arne Petersen1cc9e082013-01-31 15:52:23 +0100617 if (anchor >= 0)
618 entry->anchor = entry->cursor + strlen(text) + anchor;
619 else
620 entry->anchor = entry->cursor + 1 + anchor;
621 if (cursor >= 0)
622 entry->cursor += strlen(text) + cursor;
623 else
624 entry->cursor += 1 + cursor;
Jan Arne Petersen09e7c962012-09-09 23:08:37 +0200625
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200626 text_entry_update_layout(entry);
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100627
628 widget_schedule_redraw(entry->widget);
629
Jan Arne Petersen0558a932013-01-16 21:26:45 +0100630 text_entry_update(entry);
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200631}
632
633static void
Jan Arne Petersen4a17fae2013-01-16 21:26:40 +0100634text_entry_reset_preedit(struct text_entry *entry)
635{
636 entry->preedit.cursor = 0;
637
638 free(entry->preedit.text);
639 entry->preedit.text = NULL;
640
641 free(entry->preedit.commit);
642 entry->preedit.commit = NULL;
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100643
644 pango_attr_list_unref(entry->preedit.attr_list);
645 entry->preedit.attr_list = NULL;
Jan Arne Petersen4a17fae2013-01-16 21:26:40 +0100646}
647
648static void
649text_entry_commit_and_reset(struct text_entry *entry)
650{
651 char *commit = NULL;
652
653 if (entry->preedit.commit)
654 commit = strdup(entry->preedit.commit);
655
656 text_entry_reset_preedit(entry);
657 if (commit) {
Jan Arne Petersen1cc9e082013-01-31 15:52:23 +0100658 text_entry_insert_at_cursor(entry, commit, 0, 0);
Jan Arne Petersen4a17fae2013-01-16 21:26:40 +0100659 free(commit);
660 }
661}
662
663static void
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200664text_entry_set_preedit(struct text_entry *entry,
665 const char *preedit_text,
666 int preedit_cursor)
667{
Jan Arne Petersen4a17fae2013-01-16 21:26:40 +0100668 text_entry_reset_preedit(entry);
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200669
670 if (!preedit_text)
671 return;
672
Jan Arne Petersen46535312013-01-16 21:26:38 +0100673 entry->preedit.text = strdup(preedit_text);
674 entry->preedit.cursor = preedit_cursor;
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200675
676 text_entry_update_layout(entry);
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100677
678 widget_schedule_redraw(entry->widget);
Jan Arne Petersen09e7c962012-09-09 23:08:37 +0200679}
680
Jan Arne Petersen3489ba92013-01-16 21:26:47 +0100681static uint32_t
682text_entry_try_invoke_preedit_action(struct text_entry *entry,
683 int32_t x, int32_t y,
684 uint32_t button,
685 enum wl_pointer_button_state state)
686{
687 int index, trailing;
688 uint32_t cursor;
689
690 if (!entry->preedit.text)
691 return 0;
692
693 pango_layout_xy_to_index(entry->layout,
694 x * PANGO_SCALE, y * PANGO_SCALE,
695 &index, &trailing);
696 cursor = index + trailing;
697
698 if (cursor < entry->cursor ||
699 cursor > entry->cursor + strlen(entry->preedit.text)) {
700 return 0;
701 }
702
703 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
704 text_model_invoke_action(entry->model,
705 button,
706 cursor - entry->cursor);
707
708 return 1;
709}
710
Jan Arne Petersen09e7c962012-09-09 23:08:37 +0200711static void
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200712text_entry_set_cursor_position(struct text_entry *entry,
713 int32_t x, int32_t y)
714{
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100715 int index, trailing;
716
Jan Arne Petersen4a17fae2013-01-16 21:26:40 +0100717 text_entry_commit_and_reset(entry);
718
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100719 pango_layout_xy_to_index(entry->layout,
720 x * PANGO_SCALE, y * PANGO_SCALE,
721 &index, &trailing);
722 entry->cursor = index + trailing;
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200723
Jan Arne Petersenc7d2a982013-01-16 21:26:39 +0100724 entry->serial++;
725
726 text_model_reset(entry->model, entry->serial);
Jan Arne Petersenc1e481e2012-09-09 23:08:46 +0200727
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200728 text_entry_update_layout(entry);
729
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200730 widget_schedule_redraw(entry->widget);
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100731
Jan Arne Petersen0558a932013-01-16 21:26:45 +0100732 text_entry_update(entry);
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200733}
734
735static void
Jan Arne Petersen0e5bd452012-09-09 23:08:38 +0200736text_entry_set_anchor_position(struct text_entry *entry,
737 int32_t x, int32_t y)
738{
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100739 int index, trailing;
740
741 pango_layout_xy_to_index(entry->layout,
742 x * PANGO_SCALE, y * PANGO_SCALE,
743 &index, &trailing);
744 entry->anchor = index + trailing;
745
746 text_entry_update_layout(entry);
Jan Arne Petersen0e5bd452012-09-09 23:08:38 +0200747
748 widget_schedule_redraw(entry->widget);
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100749
Jan Arne Petersen0558a932013-01-16 21:26:45 +0100750 text_entry_update(entry);
Jan Arne Petersen0e5bd452012-09-09 23:08:38 +0200751}
752
753static void
Jan Arne Petersene202bae2012-09-09 23:08:44 +0200754text_entry_delete_text(struct text_entry *entry,
755 uint32_t index, uint32_t length)
756{
757 if (entry->cursor > index)
758 entry->cursor -= length;
759
Jan Arne Petersen80ad1a92012-09-17 15:28:10 +0200760 entry->anchor = entry->cursor;
761
Jan Arne Petersene202bae2012-09-09 23:08:44 +0200762 entry->text[index] = '\0';
763 strcat(entry->text, entry->text + index + length);
764
765 text_entry_update_layout(entry);
766
767 widget_schedule_redraw(entry->widget);
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100768
Jan Arne Petersen0558a932013-01-16 21:26:45 +0100769 text_entry_update(entry);
Jan Arne Petersene202bae2012-09-09 23:08:44 +0200770}
771
772static void
Jan Arne Petersene386dd22012-09-17 15:28:09 +0200773text_entry_delete_selected_text(struct text_entry *entry)
774{
775 uint32_t start_index = entry->anchor < entry->cursor ? entry->anchor : entry->cursor;
776 uint32_t end_index = entry->anchor < entry->cursor ? entry->cursor : entry->anchor;
777
778 if (entry->anchor == entry->cursor)
779 return;
780
781 text_entry_delete_text(entry, start_index, end_index - start_index);
782
783 entry->anchor = entry->cursor;
784}
785
786static void
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200787text_entry_draw_cursor(struct text_entry *entry, cairo_t *cr)
788{
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100789 PangoRectangle extents;
790 PangoRectangle cursor_pos;
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200791
Jan Arne Petersen46535312013-01-16 21:26:38 +0100792 if (entry->preedit.text && entry->preedit.cursor < 0)
793 return;
794
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100795 pango_layout_get_extents(entry->layout, &extents, NULL);
796 pango_layout_get_cursor_pos(entry->layout,
797 entry->cursor + entry->preedit.cursor,
798 &cursor_pos, NULL);
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200799
800 cairo_set_line_width(cr, 1.0);
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100801 cairo_move_to(cr, PANGO_PIXELS(cursor_pos.x), PANGO_PIXELS(extents.height) + 2);
802 cairo_line_to(cr, PANGO_PIXELS(cursor_pos.x), - 2);
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200803 cairo_stroke(cr);
804}
805
Philipp Brüschweiler9f897c72012-10-02 11:06:53 +0200806static const int text_offset_left = 10;
807
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200808static void
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200809text_entry_redraw_handler(struct widget *widget, void *data)
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200810{
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200811 struct text_entry *entry = data;
812 cairo_surface_t *surface;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200813 struct rectangle allocation;
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200814 cairo_t *cr;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200815
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200816 surface = window_get_surface(entry->window);
817 widget_get_allocation(entry->widget, &allocation);
818
819 cr = cairo_create(surface);
820 cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
821 cairo_clip(cr);
822
823 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
824
825 cairo_push_group(cr);
826 cairo_translate(cr, allocation.x, allocation.y);
827
828 cairo_set_source_rgba(cr, 1, 1, 1, 1);
829 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
830 cairo_fill(cr);
831
832 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
833
834 if (entry->active) {
835 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
836 cairo_set_line_width (cr, 3);
837 cairo_set_source_rgba(cr, 0, 0, 1, 1.0);
838 cairo_stroke(cr);
839 }
840
841 cairo_set_source_rgba(cr, 0, 0, 0, 1);
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200842
Philipp Brüschweiler9f897c72012-10-02 11:06:53 +0200843 cairo_translate(cr, text_offset_left, allocation.height / 2);
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200844
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100845 if (!entry->layout)
846 entry->layout = pango_cairo_create_layout(cr);
847 else
848 pango_cairo_update_layout(cr, entry->layout);
849
850 text_entry_update_layout(entry);
851
852 pango_cairo_show_layout(cr, entry->layout);
Jan Arne Petersen0e5bd452012-09-09 23:08:38 +0200853
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200854 text_entry_draw_cursor(entry, cr);
855
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200856 cairo_pop_group_to_source(cr);
857 cairo_paint(cr);
858
859 cairo_destroy(cr);
860 cairo_surface_destroy(surface);
861}
862
Jan Arne Petersen0e5bd452012-09-09 23:08:38 +0200863static int
864text_entry_motion_handler(struct widget *widget,
865 struct input *input, uint32_t time,
866 float x, float y, void *data)
867{
868 struct text_entry *entry = data;
869 struct rectangle allocation;
870
871 widget_get_allocation(entry->widget, &allocation);
872
873 text_entry_set_cursor_position(entry,
Philipp Brüschweiler9f897c72012-10-02 11:06:53 +0200874 x - allocation.x - text_offset_left,
875 y - allocation.y - text_offset_left);
Jan Arne Petersen0e5bd452012-09-09 23:08:38 +0200876
877 return CURSOR_IBEAM;
878}
879
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200880static void
881text_entry_button_handler(struct widget *widget,
882 struct input *input, uint32_t time,
883 uint32_t button,
884 enum wl_pointer_button_state state, void *data)
885{
886 struct text_entry *entry = data;
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200887 struct rectangle allocation;
Rob Bradford9d1d32b2012-11-18 19:06:49 +0100888 struct editor *editor;
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200889 int32_t x, y;
Jan Arne Petersen3489ba92013-01-16 21:26:47 +0100890 uint32_t result;
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200891
892 widget_get_allocation(entry->widget, &allocation);
893 input_get_position(input, &x, &y);
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200894
Jan Arne Petersen3489ba92013-01-16 21:26:47 +0100895 x -= allocation.x + text_offset_left;
896 y -= allocation.y + text_offset_left;
897
Rob Bradford9d1d32b2012-11-18 19:06:49 +0100898 editor = window_get_user_data(entry->window);
899
Jan Arne Petersen3489ba92013-01-16 21:26:47 +0100900 result = text_entry_try_invoke_preedit_action(entry, x, y, button, state);
901
902 if (result)
903 return;
904
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200905 if (button != BTN_LEFT) {
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200906 return;
907 }
908
Jan Arne Petersen3489ba92013-01-16 21:26:47 +0100909 text_entry_set_cursor_position(entry, x, y);
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200910
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200911 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
912 struct wl_seat *seat = input_get_seat(input);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200913
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200914 text_entry_activate(entry, seat);
Rob Bradford9d1d32b2012-11-18 19:06:49 +0100915 editor->active_entry = entry;
Jan Arne Petersen0e5bd452012-09-09 23:08:38 +0200916
Jan Arne Petersen3489ba92013-01-16 21:26:47 +0100917 text_entry_set_anchor_position(entry, x, y);
Jan Arne Petersen0e5bd452012-09-09 23:08:38 +0200918
919 widget_set_motion_handler(entry->widget, text_entry_motion_handler);
920 } else {
921 widget_set_motion_handler(entry->widget, NULL);
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200922 }
923}
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200924
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200925static void
926editor_button_handler(struct widget *widget,
927 struct input *input, uint32_t time,
928 uint32_t button,
929 enum wl_pointer_button_state state, void *data)
930{
931 struct editor *editor = data;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200932
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200933 if (button != BTN_LEFT) {
934 return;
935 }
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200936
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200937 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
938 struct wl_seat *seat = input_get_seat(input);
939
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200940 text_entry_deactivate(editor->entry, seat);
941 text_entry_deactivate(editor->editor, seat);
Rob Bradford9d1d32b2012-11-18 19:06:49 +0100942 editor->active_entry = NULL;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200943 }
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200944}
945
946static void
Rob Bradford9d1d32b2012-11-18 19:06:49 +0100947key_handler(struct window *window,
948 struct input *input, uint32_t time,
949 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
950 void *data)
951{
952 struct editor *editor = data;
953 struct text_entry *entry;
954 const char *start, *end, *new_char;
955 char text[16];
956
957 if (!editor->active_entry)
958 return;
959
960 entry = editor->active_entry;
961
962 if (state != WL_KEYBOARD_KEY_STATE_PRESSED)
963 return;
964
965 switch (sym) {
966 case XKB_KEY_BackSpace:
Jan Arne Petersen4a17fae2013-01-16 21:26:40 +0100967 text_entry_commit_and_reset(entry);
968
Rob Bradford9d1d32b2012-11-18 19:06:49 +0100969 start = utf8_prev_char(entry->text, entry->text + entry->cursor);
970
971 if (start == NULL)
972 break;
973
974 end = utf8_end_char(entry->text + entry->cursor);
975 text_entry_delete_text(entry,
976 start - entry->text,
977 end - start);
978 break;
979 case XKB_KEY_Delete:
Jan Arne Petersen4a17fae2013-01-16 21:26:40 +0100980 text_entry_commit_and_reset(entry);
981
Rob Bradford9d1d32b2012-11-18 19:06:49 +0100982 start = utf8_start_char(entry->text, entry->text + entry->cursor);
983
984 if (start == NULL)
985 break;
986
987 end = utf8_next_char(start);
988
989 if (end == NULL)
990 break;
991
992 text_entry_delete_text(entry,
993 start - entry->text,
994 end - start);
995 break;
996 case XKB_KEY_Left:
Jan Arne Petersen4a17fae2013-01-16 21:26:40 +0100997 text_entry_commit_and_reset(entry);
998
Rob Bradford9d1d32b2012-11-18 19:06:49 +0100999 new_char = utf8_prev_char(entry->text, entry->text + entry->cursor);
1000 if (new_char != NULL) {
1001 entry->cursor = new_char - entry->text;
1002 entry->anchor = entry->cursor;
1003 widget_schedule_redraw(entry->widget);
1004 }
1005 break;
1006 case XKB_KEY_Right:
Jan Arne Petersen4a17fae2013-01-16 21:26:40 +01001007 text_entry_commit_and_reset(entry);
1008
Rob Bradford9d1d32b2012-11-18 19:06:49 +01001009 new_char = utf8_next_char(entry->text + entry->cursor);
1010 if (new_char != NULL) {
1011 entry->cursor = new_char - entry->text;
1012 entry->anchor = entry->cursor;
1013 widget_schedule_redraw(entry->widget);
1014 }
1015 break;
1016 default:
1017 if (xkb_keysym_to_utf8(sym, text, sizeof(text)) <= 0)
1018 break;
1019
Jan Arne Petersen4a17fae2013-01-16 21:26:40 +01001020 text_entry_commit_and_reset(entry);
1021
Jan Arne Petersen1cc9e082013-01-31 15:52:23 +01001022 text_entry_insert_at_cursor(entry, text, 0, 0);
Rob Bradford9d1d32b2012-11-18 19:06:49 +01001023 break;
1024 }
1025
1026 widget_schedule_redraw(entry->widget);
1027}
1028
1029static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001030global_handler(struct display *display, uint32_t name,
Jan Arne Petersencba9e472012-06-21 21:52:19 +02001031 const char *interface, uint32_t version, void *data)
1032{
1033 struct editor *editor = data;
1034
Jan Arne Petersen51963742012-08-10 16:47:20 +02001035 if (!strcmp(interface, "text_model_factory")) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001036 editor->text_model_factory =
1037 display_bind(display, name,
1038 &text_model_factory_interface, 1);
Jan Arne Petersencba9e472012-06-21 21:52:19 +02001039 }
1040}
1041
1042int
1043main(int argc, char *argv[])
1044{
1045 struct editor editor;
Jan Arne Petersen61381972013-01-31 15:52:21 +01001046 int i;
1047 uint32_t click_to_show = 0;
1048
1049 for (i = 1; i < argc; i++)
1050 if (strcmp("--click-to-show", argv[i]) == 0)
1051 click_to_show = 1;
Jan Arne Petersencba9e472012-06-21 21:52:19 +02001052
Jan Arne Petersen25f6db52012-11-05 03:26:40 +01001053 memset(&editor, 0, sizeof editor);
1054
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +01001055#ifdef HAVE_PANGO
1056 g_type_init();
1057#endif
1058
Kristian Høgsberg4172f662013-02-20 15:27:49 -05001059 editor.display = display_create(&argc, argv);
Jan Arne Petersencba9e472012-06-21 21:52:19 +02001060 if (editor.display == NULL) {
1061 fprintf(stderr, "failed to create display: %m\n");
1062 return -1;
1063 }
Jan Arne Petersencba9e472012-06-21 21:52:19 +02001064
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001065 display_set_user_data(editor.display, &editor);
1066 display_set_global_handler(editor.display, global_handler);
Jan Arne Petersencba9e472012-06-21 21:52:19 +02001067
1068 editor.window = window_create(editor.display);
1069 editor.widget = frame_create(editor.window, &editor);
1070
1071 editor.entry = text_entry_create(&editor, "Entry");
Jan Arne Petersen61381972013-01-31 15:52:21 +01001072 editor.entry->click_to_show = click_to_show;
Jan Arne Petersen0558a932013-01-16 21:26:45 +01001073 editor.editor = text_entry_create(&editor, "Numeric");
1074 editor.editor->content_purpose = TEXT_MODEL_CONTENT_PURPOSE_NUMBER;
Jan Arne Petersen61381972013-01-31 15:52:21 +01001075 editor.editor->click_to_show = click_to_show;
Jan Arne Petersencba9e472012-06-21 21:52:19 +02001076
1077 window_set_title(editor.window, "Text Editor");
Rob Bradford9d1d32b2012-11-18 19:06:49 +01001078 window_set_key_handler(editor.window, key_handler);
1079 window_set_user_data(editor.window, &editor);
Jan Arne Petersencba9e472012-06-21 21:52:19 +02001080
1081 widget_set_redraw_handler(editor.widget, redraw_handler);
1082 widget_set_resize_handler(editor.widget, resize_handler);
Jan Arne Petersenf80bc062012-09-09 23:08:34 +02001083 widget_set_button_handler(editor.widget, editor_button_handler);
Jan Arne Petersencba9e472012-06-21 21:52:19 +02001084
1085 window_schedule_resize(editor.window, 500, 400);
1086
1087 display_run(editor.display);
1088
1089 text_entry_destroy(editor.entry);
1090 text_entry_destroy(editor.editor);
1091
1092 return 0;
1093}