blob: 5c072f21ca6930147ca02c1d0e314e347f96568e [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
Jan Arne Petersen680275f2012-09-24 14:51:14 +0200339text_model_enter(void *data,
340 struct text_model *text_model,
341 struct wl_surface *surface)
Jan Arne Petersende3b6a12012-08-10 16:47:21 +0200342{
343 struct text_entry *entry = data;
344
Jan Arne Petersen680275f2012-09-24 14:51:14 +0200345 if (surface != window_get_wl_surface(entry->window))
346 return;
347
Jan Arne Petersende3b6a12012-08-10 16:47:21 +0200348 entry->active = 1;
349
350 widget_schedule_redraw(entry->widget);
351}
352
353static void
Jan Arne Petersen680275f2012-09-24 14:51:14 +0200354text_model_leave(void *data,
355 struct text_model *text_model)
Jan Arne Petersende3b6a12012-08-10 16:47:21 +0200356{
357 struct text_entry *entry = data;
358
Jan Arne Petersen4a17fae2013-01-16 21:26:40 +0100359 text_entry_commit_and_reset(entry);
360
Jan Arne Petersende3b6a12012-08-10 16:47:21 +0200361 entry->active = 0;
362
363 widget_schedule_redraw(entry->widget);
364}
365
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200366static const struct text_model_listener text_model_listener = {
Jan Arne Petersen72f60822012-08-10 16:47:19 +0200367 text_model_commit_string,
368 text_model_preedit_string,
Jan Arne Petersene202bae2012-09-09 23:08:44 +0200369 text_model_delete_surrounding_text,
Jan Arne Petersen72f60822012-08-10 16:47:19 +0200370 text_model_preedit_styling,
Jan Arne Petersen46535312013-01-16 21:26:38 +0100371 text_model_preedit_cursor,
Jan Arne Petersend9be93b2012-11-18 19:06:43 +0100372 text_model_modifiers_map,
373 text_model_keysym,
Jan Arne Petersen680275f2012-09-24 14:51:14 +0200374 text_model_enter,
375 text_model_leave
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200376};
377
378static struct text_entry*
379text_entry_create(struct editor *editor, const char *text)
380{
381 struct text_entry *entry;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200382
Jan Arne Petersencd997062012-11-18 19:06:44 +0100383 entry = calloc(1, sizeof *entry);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200384
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200385 entry->widget = widget_add_widget(editor->widget, entry);
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200386 entry->window = editor->window;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200387 entry->text = strdup(text);
388 entry->active = 0;
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200389 entry->cursor = strlen(text);
Jan Arne Petersen0e5bd452012-09-09 23:08:38 +0200390 entry->anchor = entry->cursor;
Jan Arne Petersen4c265182012-09-09 23:08:30 +0200391 entry->model = text_model_factory_create_text_model(editor->text_model_factory);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200392 text_model_add_listener(entry->model, &text_model_listener, entry);
393
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200394 widget_set_redraw_handler(entry->widget, text_entry_redraw_handler);
395 widget_set_button_handler(entry->widget, text_entry_button_handler);
396
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200397 return entry;
398}
399
400static void
401text_entry_destroy(struct text_entry *entry)
402{
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200403 widget_destroy(entry->widget);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200404 text_model_destroy(entry->model);
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100405 g_clear_object(&entry->layout);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200406 free(entry->text);
407 free(entry);
408}
409
410static void
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200411redraw_handler(struct widget *widget, void *data)
412{
413 struct editor *editor = data;
414 cairo_surface_t *surface;
415 struct rectangle allocation;
416 cairo_t *cr;
417
418 surface = window_get_surface(editor->window);
419 widget_get_allocation(editor->widget, &allocation);
420
421 cr = cairo_create(surface);
422 cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
423 cairo_clip(cr);
424
425 cairo_translate(cr, allocation.x, allocation.y);
426
427 /* Draw background */
428 cairo_push_group(cr);
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200429 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200430 cairo_set_source_rgba(cr, 1, 1, 1, 1);
431 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
432 cairo_fill(cr);
433
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200434 cairo_pop_group_to_source(cr);
435 cairo_paint(cr);
436
437 cairo_destroy(cr);
438 cairo_surface_destroy(surface);
439}
440
441static void
442text_entry_allocate(struct text_entry *entry, int32_t x, int32_t y,
443 int32_t width, int32_t height)
444{
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200445 widget_set_allocation(entry->widget, x, y, width, height);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200446}
447
448static void
449resize_handler(struct widget *widget,
450 int32_t width, int32_t height, void *data)
451{
452 struct editor *editor = data;
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200453 struct rectangle allocation;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200454
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200455 widget_get_allocation(editor->widget, &allocation);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200456
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200457 text_entry_allocate(editor->entry,
458 allocation.x + 20, allocation.y + 20,
459 width - 40, height / 2 - 40);
460 text_entry_allocate(editor->editor,
461 allocation.x + 20, allocation.y + height / 2 + 20,
462 width - 40, height / 2 - 40);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200463}
464
465static void
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200466text_entry_activate(struct text_entry *entry,
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200467 struct wl_seat *seat)
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200468{
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200469 struct wl_surface *surface = window_get_wl_surface(entry->window);
470
Jan Arne Petersenc7d2a982013-01-16 21:26:39 +0100471 entry->serial++;
472
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200473 text_model_activate(entry->model,
Jan Arne Petersenc7d2a982013-01-16 21:26:39 +0100474 entry->serial,
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200475 seat,
476 surface);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200477}
478
479static void
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200480text_entry_deactivate(struct text_entry *entry,
481 struct wl_seat *seat)
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200482{
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200483 text_model_deactivate(entry->model,
484 seat);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200485}
486
487static void
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200488text_entry_update_layout(struct text_entry *entry)
489{
490 char *text;
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100491 PangoAttrList *attr_list;
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200492
Philipp Brüschweiler237358b2012-10-02 11:06:51 +0200493 assert(((unsigned int)entry->cursor) <= strlen(entry->text) +
Jan Arne Petersen46535312013-01-16 21:26:38 +0100494 (entry->preedit.text ? strlen(entry->preedit.text) : 0));
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200495
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100496 if (entry->preedit.text) {
497 text = malloc(strlen(entry->text) + strlen(entry->preedit.text) + 1);
498 strncpy(text, entry->text, entry->cursor);
499 strcpy(text + entry->cursor, entry->preedit.text);
500 strcpy(text + entry->cursor + strlen(entry->preedit.text),
501 entry->text + entry->cursor);
502 } else {
503 text = strdup(entry->text);
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200504 }
505
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100506 if (entry->cursor != entry->anchor) {
507 int start_index = MIN(entry->cursor, entry->anchor);
508 int end_index = MAX(entry->cursor, entry->anchor);
509 PangoAttribute *attr;
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200510
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100511 attr_list = pango_attr_list_copy(entry->preedit.attr_list);
512
513 if (!attr_list)
514 attr_list = pango_attr_list_new();
515
516 attr = pango_attr_background_new(0.3 * 65535, 0.3 * 65535, 65535);
517 attr->start_index = start_index;
518 attr->end_index = end_index;
519 pango_attr_list_insert(attr_list, attr);
520
521 attr = pango_attr_foreground_new(65535, 65535, 65535);
522 attr->start_index = start_index;
523 attr->end_index = end_index;
524 pango_attr_list_insert(attr_list, attr);
525 } else {
526 attr_list = pango_attr_list_ref(entry->preedit.attr_list);
527 }
528
529 if (entry->preedit.text && !entry->preedit.attr_list) {
530 PangoAttribute *attr;
531
532 if (!attr_list)
533 attr_list = pango_attr_list_new();
534
535 attr = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE);
536 attr->start_index = entry->cursor;
537 attr->end_index = entry->cursor + strlen(entry->preedit.text);
538 pango_attr_list_insert(attr_list, attr);
539 }
540
541 if (entry->layout) {
542 pango_layout_set_text(entry->layout, text, -1);
543 pango_layout_set_attributes(entry->layout, attr_list);
544 }
545
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200546 free(text);
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100547 pango_attr_list_unref(attr_list);
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200548}
549
550static void
Jan Arne Petersen0558a932013-01-16 21:26:45 +0100551text_entry_update(struct text_entry *entry)
552{
553 text_model_set_content_type(entry->model,
554 TEXT_MODEL_CONTENT_HINT_NONE,
555 entry->content_purpose);
556
557 text_model_set_surrounding_text(entry->model,
558 entry->text,
559 entry->cursor,
560 entry->anchor);
Jan Arne Petersen0eabcaa2013-01-31 15:52:20 +0100561
562 text_model_commit(entry->model);
Jan Arne Petersen0558a932013-01-16 21:26:45 +0100563}
564
565static void
Jan Arne Petersen09e7c962012-09-09 23:08:37 +0200566text_entry_insert_at_cursor(struct text_entry *entry, const char *text)
567{
568 char *new_text = malloc(strlen(entry->text) + strlen(text) + 1);
569
570 strncpy(new_text, entry->text, entry->cursor);
571 strcpy(new_text + entry->cursor, text);
572 strcpy(new_text + entry->cursor + strlen(text),
573 entry->text + entry->cursor);
574
575 free(entry->text);
576 entry->text = new_text;
577 entry->cursor += strlen(text);
Jan Arne Petersen0e5bd452012-09-09 23:08:38 +0200578 entry->anchor += strlen(text);
Jan Arne Petersen09e7c962012-09-09 23:08:37 +0200579
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200580 text_entry_update_layout(entry);
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100581
582 widget_schedule_redraw(entry->widget);
583
Jan Arne Petersen0558a932013-01-16 21:26:45 +0100584 text_entry_update(entry);
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200585}
586
587static void
Jan Arne Petersen4a17fae2013-01-16 21:26:40 +0100588text_entry_reset_preedit(struct text_entry *entry)
589{
590 entry->preedit.cursor = 0;
591
592 free(entry->preedit.text);
593 entry->preedit.text = NULL;
594
595 free(entry->preedit.commit);
596 entry->preedit.commit = NULL;
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100597
598 pango_attr_list_unref(entry->preedit.attr_list);
599 entry->preedit.attr_list = NULL;
Jan Arne Petersen4a17fae2013-01-16 21:26:40 +0100600}
601
602static void
603text_entry_commit_and_reset(struct text_entry *entry)
604{
605 char *commit = NULL;
606
607 if (entry->preedit.commit)
608 commit = strdup(entry->preedit.commit);
609
610 text_entry_reset_preedit(entry);
611 if (commit) {
612 text_entry_insert_at_cursor(entry, commit);
613 free(commit);
614 }
615}
616
617static void
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200618text_entry_set_preedit(struct text_entry *entry,
619 const char *preedit_text,
620 int preedit_cursor)
621{
Jan Arne Petersen4a17fae2013-01-16 21:26:40 +0100622 text_entry_reset_preedit(entry);
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200623
624 if (!preedit_text)
625 return;
626
Jan Arne Petersen46535312013-01-16 21:26:38 +0100627 entry->preedit.text = strdup(preedit_text);
628 entry->preedit.cursor = preedit_cursor;
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200629
630 text_entry_update_layout(entry);
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100631
632 widget_schedule_redraw(entry->widget);
Jan Arne Petersen09e7c962012-09-09 23:08:37 +0200633}
634
Jan Arne Petersen3489ba92013-01-16 21:26:47 +0100635static uint32_t
636text_entry_try_invoke_preedit_action(struct text_entry *entry,
637 int32_t x, int32_t y,
638 uint32_t button,
639 enum wl_pointer_button_state state)
640{
641 int index, trailing;
642 uint32_t cursor;
643
644 if (!entry->preedit.text)
645 return 0;
646
647 pango_layout_xy_to_index(entry->layout,
648 x * PANGO_SCALE, y * PANGO_SCALE,
649 &index, &trailing);
650 cursor = index + trailing;
651
652 if (cursor < entry->cursor ||
653 cursor > entry->cursor + strlen(entry->preedit.text)) {
654 return 0;
655 }
656
657 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
658 text_model_invoke_action(entry->model,
659 button,
660 cursor - entry->cursor);
661
662 return 1;
663}
664
Jan Arne Petersen09e7c962012-09-09 23:08:37 +0200665static void
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200666text_entry_set_cursor_position(struct text_entry *entry,
667 int32_t x, int32_t y)
668{
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100669 int index, trailing;
670
Jan Arne Petersen4a17fae2013-01-16 21:26:40 +0100671 text_entry_commit_and_reset(entry);
672
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100673 pango_layout_xy_to_index(entry->layout,
674 x * PANGO_SCALE, y * PANGO_SCALE,
675 &index, &trailing);
676 entry->cursor = index + trailing;
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200677
Jan Arne Petersenc7d2a982013-01-16 21:26:39 +0100678 entry->serial++;
679
680 text_model_reset(entry->model, entry->serial);
Jan Arne Petersenc1e481e2012-09-09 23:08:46 +0200681
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200682 text_entry_update_layout(entry);
683
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200684 widget_schedule_redraw(entry->widget);
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100685
Jan Arne Petersen0558a932013-01-16 21:26:45 +0100686 text_entry_update(entry);
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200687}
688
689static void
Jan Arne Petersen0e5bd452012-09-09 23:08:38 +0200690text_entry_set_anchor_position(struct text_entry *entry,
691 int32_t x, int32_t y)
692{
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100693 int index, trailing;
694
695 pango_layout_xy_to_index(entry->layout,
696 x * PANGO_SCALE, y * PANGO_SCALE,
697 &index, &trailing);
698 entry->anchor = index + trailing;
699
700 text_entry_update_layout(entry);
Jan Arne Petersen0e5bd452012-09-09 23:08:38 +0200701
702 widget_schedule_redraw(entry->widget);
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100703
Jan Arne Petersen0558a932013-01-16 21:26:45 +0100704 text_entry_update(entry);
Jan Arne Petersen0e5bd452012-09-09 23:08:38 +0200705}
706
707static void
Jan Arne Petersene202bae2012-09-09 23:08:44 +0200708text_entry_delete_text(struct text_entry *entry,
709 uint32_t index, uint32_t length)
710{
711 if (entry->cursor > index)
712 entry->cursor -= length;
713
Jan Arne Petersen80ad1a92012-09-17 15:28:10 +0200714 entry->anchor = entry->cursor;
715
Jan Arne Petersene202bae2012-09-09 23:08:44 +0200716 entry->text[index] = '\0';
717 strcat(entry->text, entry->text + index + length);
718
719 text_entry_update_layout(entry);
720
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 Petersene202bae2012-09-09 23:08:44 +0200724}
725
726static void
Jan Arne Petersene386dd22012-09-17 15:28:09 +0200727text_entry_delete_selected_text(struct text_entry *entry)
728{
729 uint32_t start_index = entry->anchor < entry->cursor ? entry->anchor : entry->cursor;
730 uint32_t end_index = entry->anchor < entry->cursor ? entry->cursor : entry->anchor;
731
732 if (entry->anchor == entry->cursor)
733 return;
734
735 text_entry_delete_text(entry, start_index, end_index - start_index);
736
737 entry->anchor = entry->cursor;
738}
739
740static void
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200741text_entry_draw_cursor(struct text_entry *entry, cairo_t *cr)
742{
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100743 PangoRectangle extents;
744 PangoRectangle cursor_pos;
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200745
Jan Arne Petersen46535312013-01-16 21:26:38 +0100746 if (entry->preedit.text && entry->preedit.cursor < 0)
747 return;
748
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100749 pango_layout_get_extents(entry->layout, &extents, NULL);
750 pango_layout_get_cursor_pos(entry->layout,
751 entry->cursor + entry->preedit.cursor,
752 &cursor_pos, NULL);
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200753
754 cairo_set_line_width(cr, 1.0);
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100755 cairo_move_to(cr, PANGO_PIXELS(cursor_pos.x), PANGO_PIXELS(extents.height) + 2);
756 cairo_line_to(cr, PANGO_PIXELS(cursor_pos.x), - 2);
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200757 cairo_stroke(cr);
758}
759
Philipp Brüschweiler9f897c72012-10-02 11:06:53 +0200760static const int text_offset_left = 10;
761
Jan Arne Petersenc1fbcb72012-09-09 23:08:39 +0200762static void
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200763text_entry_redraw_handler(struct widget *widget, void *data)
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200764{
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200765 struct text_entry *entry = data;
766 cairo_surface_t *surface;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200767 struct rectangle allocation;
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200768 cairo_t *cr;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200769
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200770 surface = window_get_surface(entry->window);
771 widget_get_allocation(entry->widget, &allocation);
772
773 cr = cairo_create(surface);
774 cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
775 cairo_clip(cr);
776
777 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
778
779 cairo_push_group(cr);
780 cairo_translate(cr, allocation.x, allocation.y);
781
782 cairo_set_source_rgba(cr, 1, 1, 1, 1);
783 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
784 cairo_fill(cr);
785
786 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
787
788 if (entry->active) {
789 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
790 cairo_set_line_width (cr, 3);
791 cairo_set_source_rgba(cr, 0, 0, 1, 1.0);
792 cairo_stroke(cr);
793 }
794
795 cairo_set_source_rgba(cr, 0, 0, 0, 1);
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200796
Philipp Brüschweiler9f897c72012-10-02 11:06:53 +0200797 cairo_translate(cr, text_offset_left, allocation.height / 2);
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200798
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +0100799 if (!entry->layout)
800 entry->layout = pango_cairo_create_layout(cr);
801 else
802 pango_cairo_update_layout(cr, entry->layout);
803
804 text_entry_update_layout(entry);
805
806 pango_cairo_show_layout(cr, entry->layout);
Jan Arne Petersen0e5bd452012-09-09 23:08:38 +0200807
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200808 text_entry_draw_cursor(entry, cr);
809
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200810 cairo_pop_group_to_source(cr);
811 cairo_paint(cr);
812
813 cairo_destroy(cr);
814 cairo_surface_destroy(surface);
815}
816
Jan Arne Petersen0e5bd452012-09-09 23:08:38 +0200817static int
818text_entry_motion_handler(struct widget *widget,
819 struct input *input, uint32_t time,
820 float x, float y, void *data)
821{
822 struct text_entry *entry = data;
823 struct rectangle allocation;
824
825 widget_get_allocation(entry->widget, &allocation);
826
827 text_entry_set_cursor_position(entry,
Philipp Brüschweiler9f897c72012-10-02 11:06:53 +0200828 x - allocation.x - text_offset_left,
829 y - allocation.y - text_offset_left);
Jan Arne Petersen0e5bd452012-09-09 23:08:38 +0200830
831 return CURSOR_IBEAM;
832}
833
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200834static void
835text_entry_button_handler(struct widget *widget,
836 struct input *input, uint32_t time,
837 uint32_t button,
838 enum wl_pointer_button_state state, void *data)
839{
840 struct text_entry *entry = data;
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200841 struct rectangle allocation;
Rob Bradford9d1d32b2012-11-18 19:06:49 +0100842 struct editor *editor;
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200843 int32_t x, y;
Jan Arne Petersen3489ba92013-01-16 21:26:47 +0100844 uint32_t result;
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200845
846 widget_get_allocation(entry->widget, &allocation);
847 input_get_position(input, &x, &y);
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200848
Jan Arne Petersen3489ba92013-01-16 21:26:47 +0100849 x -= allocation.x + text_offset_left;
850 y -= allocation.y + text_offset_left;
851
Rob Bradford9d1d32b2012-11-18 19:06:49 +0100852 editor = window_get_user_data(entry->window);
853
Jan Arne Petersen3489ba92013-01-16 21:26:47 +0100854 result = text_entry_try_invoke_preedit_action(entry, x, y, button, state);
855
856 if (result)
857 return;
858
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200859 if (button != BTN_LEFT) {
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200860 return;
861 }
862
Jan Arne Petersen3489ba92013-01-16 21:26:47 +0100863 text_entry_set_cursor_position(entry, x, y);
Jan Arne Petersen7e634a02012-09-09 23:08:36 +0200864
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200865 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
866 struct wl_seat *seat = input_get_seat(input);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200867
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200868 text_entry_activate(entry, seat);
Rob Bradford9d1d32b2012-11-18 19:06:49 +0100869 editor->active_entry = entry;
Jan Arne Petersen0e5bd452012-09-09 23:08:38 +0200870
Jan Arne Petersen3489ba92013-01-16 21:26:47 +0100871 text_entry_set_anchor_position(entry, x, y);
Jan Arne Petersen0e5bd452012-09-09 23:08:38 +0200872
873 widget_set_motion_handler(entry->widget, text_entry_motion_handler);
874 } else {
875 widget_set_motion_handler(entry->widget, NULL);
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200876 }
877}
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200878
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200879static void
880editor_button_handler(struct widget *widget,
881 struct input *input, uint32_t time,
882 uint32_t button,
883 enum wl_pointer_button_state state, void *data)
884{
885 struct editor *editor = data;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200886
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200887 if (button != BTN_LEFT) {
888 return;
889 }
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200890
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200891 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
892 struct wl_seat *seat = input_get_seat(input);
893
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200894 text_entry_deactivate(editor->entry, seat);
895 text_entry_deactivate(editor->editor, seat);
Rob Bradford9d1d32b2012-11-18 19:06:49 +0100896 editor->active_entry = NULL;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200897 }
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200898}
899
900static void
Rob Bradford9d1d32b2012-11-18 19:06:49 +0100901key_handler(struct window *window,
902 struct input *input, uint32_t time,
903 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
904 void *data)
905{
906 struct editor *editor = data;
907 struct text_entry *entry;
908 const char *start, *end, *new_char;
909 char text[16];
910
911 if (!editor->active_entry)
912 return;
913
914 entry = editor->active_entry;
915
916 if (state != WL_KEYBOARD_KEY_STATE_PRESSED)
917 return;
918
919 switch (sym) {
920 case XKB_KEY_BackSpace:
Jan Arne Petersen4a17fae2013-01-16 21:26:40 +0100921 text_entry_commit_and_reset(entry);
922
Rob Bradford9d1d32b2012-11-18 19:06:49 +0100923 start = utf8_prev_char(entry->text, entry->text + entry->cursor);
924
925 if (start == NULL)
926 break;
927
928 end = utf8_end_char(entry->text + entry->cursor);
929 text_entry_delete_text(entry,
930 start - entry->text,
931 end - start);
932 break;
933 case XKB_KEY_Delete:
Jan Arne Petersen4a17fae2013-01-16 21:26:40 +0100934 text_entry_commit_and_reset(entry);
935
Rob Bradford9d1d32b2012-11-18 19:06:49 +0100936 start = utf8_start_char(entry->text, entry->text + entry->cursor);
937
938 if (start == NULL)
939 break;
940
941 end = utf8_next_char(start);
942
943 if (end == NULL)
944 break;
945
946 text_entry_delete_text(entry,
947 start - entry->text,
948 end - start);
949 break;
950 case XKB_KEY_Left:
Jan Arne Petersen4a17fae2013-01-16 21:26:40 +0100951 text_entry_commit_and_reset(entry);
952
Rob Bradford9d1d32b2012-11-18 19:06:49 +0100953 new_char = utf8_prev_char(entry->text, entry->text + entry->cursor);
954 if (new_char != NULL) {
955 entry->cursor = new_char - entry->text;
956 entry->anchor = entry->cursor;
957 widget_schedule_redraw(entry->widget);
958 }
959 break;
960 case XKB_KEY_Right:
Jan Arne Petersen4a17fae2013-01-16 21:26:40 +0100961 text_entry_commit_and_reset(entry);
962
Rob Bradford9d1d32b2012-11-18 19:06:49 +0100963 new_char = utf8_next_char(entry->text + entry->cursor);
964 if (new_char != NULL) {
965 entry->cursor = new_char - entry->text;
966 entry->anchor = entry->cursor;
967 widget_schedule_redraw(entry->widget);
968 }
969 break;
970 default:
971 if (xkb_keysym_to_utf8(sym, text, sizeof(text)) <= 0)
972 break;
973
Jan Arne Petersen4a17fae2013-01-16 21:26:40 +0100974 text_entry_commit_and_reset(entry);
975
Rob Bradford9d1d32b2012-11-18 19:06:49 +0100976 text_entry_insert_at_cursor(entry, text);
977 break;
978 }
979
980 widget_schedule_redraw(entry->widget);
981}
982
983static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400984global_handler(struct display *display, uint32_t name,
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200985 const char *interface, uint32_t version, void *data)
986{
987 struct editor *editor = data;
988
Jan Arne Petersen51963742012-08-10 16:47:20 +0200989 if (!strcmp(interface, "text_model_factory")) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400990 editor->text_model_factory =
991 display_bind(display, name,
992 &text_model_factory_interface, 1);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200993 }
994}
995
996int
997main(int argc, char *argv[])
998{
999 struct editor editor;
1000
Jan Arne Petersen25f6db52012-11-05 03:26:40 +01001001 memset(&editor, 0, sizeof editor);
1002
Jan Arne Petersen0a1cf392013-01-16 21:26:42 +01001003#ifdef HAVE_PANGO
1004 g_type_init();
1005#endif
1006
Jan Arne Petersencba9e472012-06-21 21:52:19 +02001007 editor.display = display_create(argc, argv);
1008 if (editor.display == NULL) {
1009 fprintf(stderr, "failed to create display: %m\n");
1010 return -1;
1011 }
Jan Arne Petersencba9e472012-06-21 21:52:19 +02001012
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001013 display_set_user_data(editor.display, &editor);
1014 display_set_global_handler(editor.display, global_handler);
Jan Arne Petersencba9e472012-06-21 21:52:19 +02001015
1016 editor.window = window_create(editor.display);
1017 editor.widget = frame_create(editor.window, &editor);
1018
1019 editor.entry = text_entry_create(&editor, "Entry");
Jan Arne Petersen0558a932013-01-16 21:26:45 +01001020 editor.editor = text_entry_create(&editor, "Numeric");
1021 editor.editor->content_purpose = TEXT_MODEL_CONTENT_PURPOSE_NUMBER;
Jan Arne Petersencba9e472012-06-21 21:52:19 +02001022
1023 window_set_title(editor.window, "Text Editor");
Rob Bradford9d1d32b2012-11-18 19:06:49 +01001024 window_set_key_handler(editor.window, key_handler);
1025 window_set_user_data(editor.window, &editor);
Jan Arne Petersencba9e472012-06-21 21:52:19 +02001026
1027 widget_set_redraw_handler(editor.widget, redraw_handler);
1028 widget_set_resize_handler(editor.widget, resize_handler);
Jan Arne Petersenf80bc062012-09-09 23:08:34 +02001029 widget_set_button_handler(editor.widget, editor_button_handler);
Jan Arne Petersencba9e472012-06-21 21:52:19 +02001030
1031 window_schedule_resize(editor.window, 500, 400);
1032
1033 display_run(editor.display);
1034
1035 text_entry_destroy(editor.entry);
1036 text_entry_destroy(editor.editor);
1037
1038 return 0;
1039}