blob: e037c2f9f21fa9a9b2b142be656e9231ccc1adad [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 Petersenece6b5a2013-04-18 16:47:15 +0200394static void
395text_model_language(void *data,
396 struct text_model *text_model,
397 uint32_t serial,
398 const char *language)
399{
400}
401
402static void
403text_model_text_direction(void *data,
404 struct text_model *text_model,
405 uint32_t serial,
406 uint32_t direction)
407{
408}
409
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200410static const struct text_model_listener text_model_listener = {
Jan Arne Petersen72f60822012-08-10 16:47:19 +0200411 text_model_commit_string,
412 text_model_preedit_string,
Jan Arne Petersene202bae2012-09-09 23:08:44 +0200413 text_model_delete_surrounding_text,
Jan Arne Petersen1cc9e082013-01-31 15:52:23 +0100414 text_model_cursor_position,
Jan Arne Petersen72f60822012-08-10 16:47:19 +0200415 text_model_preedit_styling,
Jan Arne Petersen46535312013-01-16 21:26:38 +0100416 text_model_preedit_cursor,
Jan Arne Petersend9be93b2012-11-18 19:06:43 +0100417 text_model_modifiers_map,
418 text_model_keysym,
Jan Arne Petersen680275f2012-09-24 14:51:14 +0200419 text_model_enter,
Jan Arne Petersen61381972013-01-31 15:52:21 +0100420 text_model_leave,
Jan Arne Petersenece6b5a2013-04-18 16:47:15 +0200421 text_model_input_panel_state,
422 text_model_language,
423 text_model_text_direction
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200424};
425
426static struct text_entry*
427text_entry_create(struct editor *editor, const char *text)
428{
429 struct text_entry *entry;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200430
Jan Arne Petersencd997062012-11-18 19:06:44 +0100431 entry = calloc(1, sizeof *entry);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200432
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200433 entry->widget = widget_add_widget(editor->widget, entry);
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200434 entry->window = editor->window;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200435 entry->text = strdup(text);
436 entry->active = 0;
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200437 entry->cursor = strlen(text);
Jan Arne Petersen0e5bd452012-09-09 23:08:38 +0200438 entry->anchor = entry->cursor;
Jan Arne Petersen4c265182012-09-09 23:08:30 +0200439 entry->model = text_model_factory_create_text_model(editor->text_model_factory);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200440 text_model_add_listener(entry->model, &text_model_listener, entry);
441
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200442 widget_set_redraw_handler(entry->widget, text_entry_redraw_handler);
443 widget_set_button_handler(entry->widget, text_entry_button_handler);
444
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200445 return entry;
446}
447
448static void
449text_entry_destroy(struct text_entry *entry)
450{
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200451 widget_destroy(entry->widget);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200452 text_model_destroy(entry->model);
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100453 g_clear_object(&entry->layout);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200454 free(entry->text);
455 free(entry);
456}
457
458static void
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200459redraw_handler(struct widget *widget, void *data)
460{
461 struct editor *editor = data;
462 cairo_surface_t *surface;
463 struct rectangle allocation;
464 cairo_t *cr;
465
466 surface = window_get_surface(editor->window);
467 widget_get_allocation(editor->widget, &allocation);
468
469 cr = cairo_create(surface);
470 cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
471 cairo_clip(cr);
472
473 cairo_translate(cr, allocation.x, allocation.y);
474
475 /* Draw background */
476 cairo_push_group(cr);
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200477 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200478 cairo_set_source_rgba(cr, 1, 1, 1, 1);
479 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
480 cairo_fill(cr);
481
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200482 cairo_pop_group_to_source(cr);
483 cairo_paint(cr);
484
485 cairo_destroy(cr);
486 cairo_surface_destroy(surface);
487}
488
489static void
490text_entry_allocate(struct text_entry *entry, int32_t x, int32_t y,
491 int32_t width, int32_t height)
492{
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200493 widget_set_allocation(entry->widget, x, y, width, height);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200494}
495
496static void
497resize_handler(struct widget *widget,
498 int32_t width, int32_t height, void *data)
499{
500 struct editor *editor = data;
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200501 struct rectangle allocation;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200502
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200503 widget_get_allocation(editor->widget, &allocation);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200504
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200505 text_entry_allocate(editor->entry,
506 allocation.x + 20, allocation.y + 20,
507 width - 40, height / 2 - 40);
508 text_entry_allocate(editor->editor,
509 allocation.x + 20, allocation.y + height / 2 + 20,
510 width - 40, height / 2 - 40);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200511}
512
513static void
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200514text_entry_activate(struct text_entry *entry,
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200515 struct wl_seat *seat)
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200516{
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200517 struct wl_surface *surface = window_get_wl_surface(entry->window);
518
Jan Arne Petersen61381972013-01-31 15:52:21 +0100519 if (entry->click_to_show && entry->active) {
520 text_model_show_input_panel(entry->model);
521
522 return;
523 }
524
525 if (!entry->click_to_show)
526 text_model_show_input_panel(entry->model);
527
Jan Arne Petersenc7d2a982013-01-16 21:26:39 +0100528 entry->serial++;
529
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200530 text_model_activate(entry->model,
Jan Arne Petersenc7d2a982013-01-16 21:26:39 +0100531 entry->serial,
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200532 seat,
533 surface);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200534}
535
536static void
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200537text_entry_deactivate(struct text_entry *entry,
538 struct wl_seat *seat)
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200539{
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200540 text_model_deactivate(entry->model,
541 seat);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200542}
543
544static void
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200545text_entry_update_layout(struct text_entry *entry)
546{
547 char *text;
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100548 PangoAttrList *attr_list;
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200549
Philipp Brüschweiler237358b2012-10-02 11:06:51 +0200550 assert(((unsigned int)entry->cursor) <= strlen(entry->text) +
Jan Arne Petersen46535312013-01-16 21:26:38 +0100551 (entry->preedit.text ? strlen(entry->preedit.text) : 0));
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200552
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100553 if (entry->preedit.text) {
554 text = malloc(strlen(entry->text) + strlen(entry->preedit.text) + 1);
555 strncpy(text, entry->text, entry->cursor);
556 strcpy(text + entry->cursor, entry->preedit.text);
557 strcpy(text + entry->cursor + strlen(entry->preedit.text),
558 entry->text + entry->cursor);
559 } else {
560 text = strdup(entry->text);
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200561 }
562
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100563 if (entry->cursor != entry->anchor) {
564 int start_index = MIN(entry->cursor, entry->anchor);
565 int end_index = MAX(entry->cursor, entry->anchor);
566 PangoAttribute *attr;
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200567
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100568 attr_list = pango_attr_list_copy(entry->preedit.attr_list);
569
570 if (!attr_list)
571 attr_list = pango_attr_list_new();
572
573 attr = pango_attr_background_new(0.3 * 65535, 0.3 * 65535, 65535);
574 attr->start_index = start_index;
575 attr->end_index = end_index;
576 pango_attr_list_insert(attr_list, attr);
577
578 attr = pango_attr_foreground_new(65535, 65535, 65535);
579 attr->start_index = start_index;
580 attr->end_index = end_index;
581 pango_attr_list_insert(attr_list, attr);
582 } else {
583 attr_list = pango_attr_list_ref(entry->preedit.attr_list);
584 }
585
586 if (entry->preedit.text && !entry->preedit.attr_list) {
587 PangoAttribute *attr;
588
589 if (!attr_list)
590 attr_list = pango_attr_list_new();
591
592 attr = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE);
593 attr->start_index = entry->cursor;
594 attr->end_index = entry->cursor + strlen(entry->preedit.text);
595 pango_attr_list_insert(attr_list, attr);
596 }
597
598 if (entry->layout) {
599 pango_layout_set_text(entry->layout, text, -1);
600 pango_layout_set_attributes(entry->layout, attr_list);
601 }
602
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200603 free(text);
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100604 pango_attr_list_unref(attr_list);
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200605}
606
607static void
Jan Arne Petersen0558a932013-01-16 21:26:45 +0100608text_entry_update(struct text_entry *entry)
609{
610 text_model_set_content_type(entry->model,
611 TEXT_MODEL_CONTENT_HINT_NONE,
612 entry->content_purpose);
613
614 text_model_set_surrounding_text(entry->model,
615 entry->text,
616 entry->cursor,
617 entry->anchor);
Jan Arne Petersen0eabcaa2013-01-31 15:52:20 +0100618
619 text_model_commit(entry->model);
Jan Arne Petersen0558a932013-01-16 21:26:45 +0100620}
621
622static void
Jan Arne Petersen1cc9e082013-01-31 15:52:23 +0100623text_entry_insert_at_cursor(struct text_entry *entry, const char *text,
624 int32_t cursor, int32_t anchor)
Jan Arne Petersen09e7c962012-09-09 23:08:37 +0200625{
626 char *new_text = malloc(strlen(entry->text) + strlen(text) + 1);
627
628 strncpy(new_text, entry->text, entry->cursor);
629 strcpy(new_text + entry->cursor, text);
630 strcpy(new_text + entry->cursor + strlen(text),
631 entry->text + entry->cursor);
632
633 free(entry->text);
634 entry->text = new_text;
Jan Arne Petersen1cc9e082013-01-31 15:52:23 +0100635 if (anchor >= 0)
636 entry->anchor = entry->cursor + strlen(text) + anchor;
637 else
638 entry->anchor = entry->cursor + 1 + anchor;
639 if (cursor >= 0)
640 entry->cursor += strlen(text) + cursor;
641 else
642 entry->cursor += 1 + cursor;
Jan Arne Petersen09e7c962012-09-09 23:08:37 +0200643
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200644 text_entry_update_layout(entry);
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100645
646 widget_schedule_redraw(entry->widget);
647
Jan Arne Petersen0558a932013-01-16 21:26:45 +0100648 text_entry_update(entry);
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200649}
650
651static void
Jan Arne Petersen4a17fae2013-01-16 21:26:40 +0100652text_entry_reset_preedit(struct text_entry *entry)
653{
654 entry->preedit.cursor = 0;
655
656 free(entry->preedit.text);
657 entry->preedit.text = NULL;
658
659 free(entry->preedit.commit);
660 entry->preedit.commit = NULL;
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100661
662 pango_attr_list_unref(entry->preedit.attr_list);
663 entry->preedit.attr_list = NULL;
Jan Arne Petersen4a17fae2013-01-16 21:26:40 +0100664}
665
666static void
667text_entry_commit_and_reset(struct text_entry *entry)
668{
669 char *commit = NULL;
670
671 if (entry->preedit.commit)
672 commit = strdup(entry->preedit.commit);
673
674 text_entry_reset_preedit(entry);
675 if (commit) {
Jan Arne Petersen1cc9e082013-01-31 15:52:23 +0100676 text_entry_insert_at_cursor(entry, commit, 0, 0);
Jan Arne Petersen4a17fae2013-01-16 21:26:40 +0100677 free(commit);
678 }
679}
680
681static void
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200682text_entry_set_preedit(struct text_entry *entry,
683 const char *preedit_text,
684 int preedit_cursor)
685{
Jan Arne Petersen4a17fae2013-01-16 21:26:40 +0100686 text_entry_reset_preedit(entry);
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200687
688 if (!preedit_text)
689 return;
690
Jan Arne Petersen46535312013-01-16 21:26:38 +0100691 entry->preedit.text = strdup(preedit_text);
692 entry->preedit.cursor = preedit_cursor;
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200693
694 text_entry_update_layout(entry);
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100695
696 widget_schedule_redraw(entry->widget);
Jan Arne Petersen09e7c962012-09-09 23:08:37 +0200697}
698
Jan Arne Petersen3489ba92013-01-16 21:26:47 +0100699static uint32_t
700text_entry_try_invoke_preedit_action(struct text_entry *entry,
701 int32_t x, int32_t y,
702 uint32_t button,
703 enum wl_pointer_button_state state)
704{
705 int index, trailing;
706 uint32_t cursor;
707
708 if (!entry->preedit.text)
709 return 0;
710
711 pango_layout_xy_to_index(entry->layout,
712 x * PANGO_SCALE, y * PANGO_SCALE,
713 &index, &trailing);
714 cursor = index + trailing;
715
716 if (cursor < entry->cursor ||
717 cursor > entry->cursor + strlen(entry->preedit.text)) {
718 return 0;
719 }
720
721 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
722 text_model_invoke_action(entry->model,
723 button,
724 cursor - entry->cursor);
725
726 return 1;
727}
728
Jan Arne Petersen09e7c962012-09-09 23:08:37 +0200729static void
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200730text_entry_set_cursor_position(struct text_entry *entry,
731 int32_t x, int32_t y)
732{
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100733 int index, trailing;
734
Jan Arne Petersen4a17fae2013-01-16 21:26:40 +0100735 text_entry_commit_and_reset(entry);
736
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100737 pango_layout_xy_to_index(entry->layout,
738 x * PANGO_SCALE, y * PANGO_SCALE,
739 &index, &trailing);
740 entry->cursor = index + trailing;
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200741
Jan Arne Petersenc7d2a982013-01-16 21:26:39 +0100742 entry->serial++;
743
744 text_model_reset(entry->model, entry->serial);
Jan Arne Petersenc1e481e2012-09-09 23:08:46 +0200745
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200746 text_entry_update_layout(entry);
747
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200748 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 Petersen7e634a02012-09-09 23:08:36 +0200751}
752
753static void
Jan Arne Petersen0e5bd452012-09-09 23:08:38 +0200754text_entry_set_anchor_position(struct text_entry *entry,
755 int32_t x, int32_t y)
756{
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100757 int index, trailing;
758
759 pango_layout_xy_to_index(entry->layout,
760 x * PANGO_SCALE, y * PANGO_SCALE,
761 &index, &trailing);
762 entry->anchor = index + trailing;
763
764 text_entry_update_layout(entry);
Jan Arne Petersen0e5bd452012-09-09 23:08:38 +0200765
766 widget_schedule_redraw(entry->widget);
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100767
Jan Arne Petersen0558a932013-01-16 21:26:45 +0100768 text_entry_update(entry);
Jan Arne Petersen0e5bd452012-09-09 23:08:38 +0200769}
770
771static void
Jan Arne Petersene202bae2012-09-09 23:08:44 +0200772text_entry_delete_text(struct text_entry *entry,
773 uint32_t index, uint32_t length)
774{
775 if (entry->cursor > index)
776 entry->cursor -= length;
777
Jan Arne Petersen80ad1a92012-09-17 15:28:10 +0200778 entry->anchor = entry->cursor;
779
Jan Arne Petersene202bae2012-09-09 23:08:44 +0200780 entry->text[index] = '\0';
781 strcat(entry->text, entry->text + index + length);
782
783 text_entry_update_layout(entry);
784
785 widget_schedule_redraw(entry->widget);
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100786
Jan Arne Petersen0558a932013-01-16 21:26:45 +0100787 text_entry_update(entry);
Jan Arne Petersene202bae2012-09-09 23:08:44 +0200788}
789
790static void
Jan Arne Petersene386dd22012-09-17 15:28:09 +0200791text_entry_delete_selected_text(struct text_entry *entry)
792{
793 uint32_t start_index = entry->anchor < entry->cursor ? entry->anchor : entry->cursor;
794 uint32_t end_index = entry->anchor < entry->cursor ? entry->cursor : entry->anchor;
795
796 if (entry->anchor == entry->cursor)
797 return;
798
799 text_entry_delete_text(entry, start_index, end_index - start_index);
800
801 entry->anchor = entry->cursor;
802}
803
804static void
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200805text_entry_draw_cursor(struct text_entry *entry, cairo_t *cr)
806{
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100807 PangoRectangle extents;
808 PangoRectangle cursor_pos;
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200809
Jan Arne Petersen46535312013-01-16 21:26:38 +0100810 if (entry->preedit.text && entry->preedit.cursor < 0)
811 return;
812
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100813 pango_layout_get_extents(entry->layout, &extents, NULL);
814 pango_layout_get_cursor_pos(entry->layout,
815 entry->cursor + entry->preedit.cursor,
816 &cursor_pos, NULL);
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200817
818 cairo_set_line_width(cr, 1.0);
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100819 cairo_move_to(cr, PANGO_PIXELS(cursor_pos.x), PANGO_PIXELS(extents.height) + 2);
820 cairo_line_to(cr, PANGO_PIXELS(cursor_pos.x), - 2);
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200821 cairo_stroke(cr);
822}
823
Philipp Brüschweiler9f897c72012-10-02 11:06:53 +0200824static const int text_offset_left = 10;
825
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200826static void
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200827text_entry_redraw_handler(struct widget *widget, void *data)
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200828{
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200829 struct text_entry *entry = data;
830 cairo_surface_t *surface;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200831 struct rectangle allocation;
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200832 cairo_t *cr;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200833
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200834 surface = window_get_surface(entry->window);
835 widget_get_allocation(entry->widget, &allocation);
836
837 cr = cairo_create(surface);
838 cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
839 cairo_clip(cr);
840
841 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
842
843 cairo_push_group(cr);
844 cairo_translate(cr, allocation.x, allocation.y);
845
846 cairo_set_source_rgba(cr, 1, 1, 1, 1);
847 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
848 cairo_fill(cr);
849
850 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
851
852 if (entry->active) {
853 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
854 cairo_set_line_width (cr, 3);
855 cairo_set_source_rgba(cr, 0, 0, 1, 1.0);
856 cairo_stroke(cr);
857 }
858
859 cairo_set_source_rgba(cr, 0, 0, 0, 1);
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200860
Philipp Brüschweiler9f897c72012-10-02 11:06:53 +0200861 cairo_translate(cr, text_offset_left, allocation.height / 2);
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200862
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100863 if (!entry->layout)
864 entry->layout = pango_cairo_create_layout(cr);
865 else
866 pango_cairo_update_layout(cr, entry->layout);
867
868 text_entry_update_layout(entry);
869
870 pango_cairo_show_layout(cr, entry->layout);
Jan Arne Petersen0e5bd452012-09-09 23:08:38 +0200871
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200872 text_entry_draw_cursor(entry, cr);
873
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200874 cairo_pop_group_to_source(cr);
875 cairo_paint(cr);
876
877 cairo_destroy(cr);
878 cairo_surface_destroy(surface);
879}
880
Jan Arne Petersen0e5bd452012-09-09 23:08:38 +0200881static int
882text_entry_motion_handler(struct widget *widget,
883 struct input *input, uint32_t time,
884 float x, float y, void *data)
885{
886 struct text_entry *entry = data;
887 struct rectangle allocation;
888
889 widget_get_allocation(entry->widget, &allocation);
890
891 text_entry_set_cursor_position(entry,
Philipp Brüschweiler9f897c72012-10-02 11:06:53 +0200892 x - allocation.x - text_offset_left,
893 y - allocation.y - text_offset_left);
Jan Arne Petersen0e5bd452012-09-09 23:08:38 +0200894
895 return CURSOR_IBEAM;
896}
897
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200898static void
899text_entry_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 text_entry *entry = data;
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200905 struct rectangle allocation;
Rob Bradford9d1d32b2012-11-18 19:06:49 +0100906 struct editor *editor;
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200907 int32_t x, y;
Jan Arne Petersen3489ba92013-01-16 21:26:47 +0100908 uint32_t result;
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200909
910 widget_get_allocation(entry->widget, &allocation);
911 input_get_position(input, &x, &y);
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200912
Jan Arne Petersen3489ba92013-01-16 21:26:47 +0100913 x -= allocation.x + text_offset_left;
914 y -= allocation.y + text_offset_left;
915
Rob Bradford9d1d32b2012-11-18 19:06:49 +0100916 editor = window_get_user_data(entry->window);
917
Jan Arne Petersen3489ba92013-01-16 21:26:47 +0100918 result = text_entry_try_invoke_preedit_action(entry, x, y, button, state);
919
920 if (result)
921 return;
922
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200923 if (button != BTN_LEFT) {
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200924 return;
925 }
926
Jan Arne Petersen3489ba92013-01-16 21:26:47 +0100927 text_entry_set_cursor_position(entry, x, y);
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200928
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200929 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
930 struct wl_seat *seat = input_get_seat(input);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200931
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200932 text_entry_activate(entry, seat);
Rob Bradford9d1d32b2012-11-18 19:06:49 +0100933 editor->active_entry = entry;
Jan Arne Petersen0e5bd452012-09-09 23:08:38 +0200934
Jan Arne Petersen3489ba92013-01-16 21:26:47 +0100935 text_entry_set_anchor_position(entry, x, y);
Jan Arne Petersen0e5bd452012-09-09 23:08:38 +0200936
937 widget_set_motion_handler(entry->widget, text_entry_motion_handler);
938 } else {
939 widget_set_motion_handler(entry->widget, NULL);
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200940 }
941}
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200942
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200943static void
944editor_button_handler(struct widget *widget,
945 struct input *input, uint32_t time,
946 uint32_t button,
947 enum wl_pointer_button_state state, void *data)
948{
949 struct editor *editor = data;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200950
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200951 if (button != BTN_LEFT) {
952 return;
953 }
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200954
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200955 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
956 struct wl_seat *seat = input_get_seat(input);
957
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200958 text_entry_deactivate(editor->entry, seat);
959 text_entry_deactivate(editor->editor, seat);
Rob Bradford9d1d32b2012-11-18 19:06:49 +0100960 editor->active_entry = NULL;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200961 }
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200962}
963
964static void
Rob Bradford9d1d32b2012-11-18 19:06:49 +0100965key_handler(struct window *window,
966 struct input *input, uint32_t time,
967 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
968 void *data)
969{
970 struct editor *editor = data;
971 struct text_entry *entry;
972 const char *start, *end, *new_char;
973 char text[16];
974
975 if (!editor->active_entry)
976 return;
977
978 entry = editor->active_entry;
979
980 if (state != WL_KEYBOARD_KEY_STATE_PRESSED)
981 return;
982
983 switch (sym) {
984 case XKB_KEY_BackSpace:
Jan Arne Petersen4a17fae2013-01-16 21:26:40 +0100985 text_entry_commit_and_reset(entry);
986
Rob Bradford9d1d32b2012-11-18 19:06:49 +0100987 start = utf8_prev_char(entry->text, entry->text + entry->cursor);
988
989 if (start == NULL)
990 break;
991
992 end = utf8_end_char(entry->text + entry->cursor);
993 text_entry_delete_text(entry,
994 start - entry->text,
995 end - start);
996 break;
997 case XKB_KEY_Delete:
Jan Arne Petersen4a17fae2013-01-16 21:26:40 +0100998 text_entry_commit_and_reset(entry);
999
Rob Bradford9d1d32b2012-11-18 19:06:49 +01001000 start = utf8_start_char(entry->text, entry->text + entry->cursor);
1001
1002 if (start == NULL)
1003 break;
1004
1005 end = utf8_next_char(start);
1006
1007 if (end == NULL)
1008 break;
1009
1010 text_entry_delete_text(entry,
1011 start - entry->text,
1012 end - start);
1013 break;
1014 case XKB_KEY_Left:
Jan Arne Petersen4a17fae2013-01-16 21:26:40 +01001015 text_entry_commit_and_reset(entry);
1016
Rob Bradford9d1d32b2012-11-18 19:06:49 +01001017 new_char = utf8_prev_char(entry->text, entry->text + entry->cursor);
1018 if (new_char != NULL) {
1019 entry->cursor = new_char - entry->text;
1020 entry->anchor = entry->cursor;
1021 widget_schedule_redraw(entry->widget);
1022 }
1023 break;
1024 case XKB_KEY_Right:
Jan Arne Petersen4a17fae2013-01-16 21:26:40 +01001025 text_entry_commit_and_reset(entry);
1026
Rob Bradford9d1d32b2012-11-18 19:06:49 +01001027 new_char = utf8_next_char(entry->text + entry->cursor);
1028 if (new_char != NULL) {
1029 entry->cursor = new_char - entry->text;
1030 entry->anchor = entry->cursor;
1031 widget_schedule_redraw(entry->widget);
1032 }
1033 break;
1034 default:
1035 if (xkb_keysym_to_utf8(sym, text, sizeof(text)) <= 0)
1036 break;
1037
Jan Arne Petersen4a17fae2013-01-16 21:26:40 +01001038 text_entry_commit_and_reset(entry);
1039
Jan Arne Petersen1cc9e082013-01-31 15:52:23 +01001040 text_entry_insert_at_cursor(entry, text, 0, 0);
Rob Bradford9d1d32b2012-11-18 19:06:49 +01001041 break;
1042 }
1043
1044 widget_schedule_redraw(entry->widget);
1045}
1046
1047static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001048global_handler(struct display *display, uint32_t name,
Jan Arne Petersencba9e472012-06-21 21:52:19 +02001049 const char *interface, uint32_t version, void *data)
1050{
1051 struct editor *editor = data;
1052
Jan Arne Petersen51963742012-08-10 16:47:20 +02001053 if (!strcmp(interface, "text_model_factory")) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001054 editor->text_model_factory =
1055 display_bind(display, name,
1056 &text_model_factory_interface, 1);
Jan Arne Petersencba9e472012-06-21 21:52:19 +02001057 }
1058}
1059
1060int
1061main(int argc, char *argv[])
1062{
1063 struct editor editor;
Jan Arne Petersen61381972013-01-31 15:52:21 +01001064 int i;
1065 uint32_t click_to_show = 0;
1066
1067 for (i = 1; i < argc; i++)
1068 if (strcmp("--click-to-show", argv[i]) == 0)
1069 click_to_show = 1;
Jan Arne Petersencba9e472012-06-21 21:52:19 +02001070
Jan Arne Petersen25f6db52012-11-05 03:26:40 +01001071 memset(&editor, 0, sizeof editor);
1072
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +01001073#ifdef HAVE_PANGO
1074 g_type_init();
1075#endif
1076
Kristian Høgsberg4172f662013-02-20 15:27:49 -05001077 editor.display = display_create(&argc, argv);
Jan Arne Petersencba9e472012-06-21 21:52:19 +02001078 if (editor.display == NULL) {
1079 fprintf(stderr, "failed to create display: %m\n");
1080 return -1;
1081 }
Jan Arne Petersencba9e472012-06-21 21:52:19 +02001082
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001083 display_set_user_data(editor.display, &editor);
1084 display_set_global_handler(editor.display, global_handler);
Jan Arne Petersencba9e472012-06-21 21:52:19 +02001085
1086 editor.window = window_create(editor.display);
1087 editor.widget = frame_create(editor.window, &editor);
1088
1089 editor.entry = text_entry_create(&editor, "Entry");
Jan Arne Petersen61381972013-01-31 15:52:21 +01001090 editor.entry->click_to_show = click_to_show;
Jan Arne Petersen0558a932013-01-16 21:26:45 +01001091 editor.editor = text_entry_create(&editor, "Numeric");
1092 editor.editor->content_purpose = TEXT_MODEL_CONTENT_PURPOSE_NUMBER;
Jan Arne Petersen61381972013-01-31 15:52:21 +01001093 editor.editor->click_to_show = click_to_show;
Jan Arne Petersencba9e472012-06-21 21:52:19 +02001094
1095 window_set_title(editor.window, "Text Editor");
Rob Bradford9d1d32b2012-11-18 19:06:49 +01001096 window_set_key_handler(editor.window, key_handler);
1097 window_set_user_data(editor.window, &editor);
Jan Arne Petersencba9e472012-06-21 21:52:19 +02001098
1099 widget_set_redraw_handler(editor.widget, redraw_handler);
1100 widget_set_resize_handler(editor.widget, resize_handler);
Jan Arne Petersenf80bc062012-09-09 23:08:34 +02001101 widget_set_button_handler(editor.widget, editor_button_handler);
Jan Arne Petersencba9e472012-06-21 21:52:19 +02001102
1103 window_schedule_resize(editor.window, 500, 400);
1104
1105 display_run(editor.display);
1106
1107 text_entry_destroy(editor.entry);
1108 text_entry_destroy(editor.editor);
1109
1110 return 0;
1111}