Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2012 Openismus GmbH |
Jan Arne Petersen | 4c26518 | 2012-09-09 23:08:30 +0200 | [diff] [blame] | 3 | * Copyright © 2012 Intel Corporation |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 4 | * |
| 5 | * Permission to use, copy, modify, distribute, and sell this software and |
| 6 | * its documentation for any purpose is hereby granted without fee, provided |
| 7 | * that the above copyright notice appear in all copies and that both that |
| 8 | * copyright notice and this permission notice appear in supporting |
| 9 | * documentation, and that the name of the copyright holders not be used in |
| 10 | * advertising or publicity pertaining to distribution of the software |
| 11 | * without specific, written prior permission. The copyright holders make |
| 12 | * no representations about the suitability of this software for any |
| 13 | * purpose. It is provided "as is" without express or implied warranty. |
| 14 | * |
| 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS |
| 16 | * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 18 | * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER |
| 19 | * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF |
| 20 | * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 21 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | */ |
| 23 | |
Philipp Brüschweiler | 591cfca | 2012-07-11 22:25:29 +0200 | [diff] [blame] | 24 | #include <assert.h> |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 28 | |
| 29 | #include <linux/input.h> |
| 30 | #include <cairo.h> |
| 31 | |
| 32 | #include "window.h" |
| 33 | #include "text-client-protocol.h" |
| 34 | |
Jan Arne Petersen | b9eb02c | 2012-09-09 23:08:35 +0200 | [diff] [blame] | 35 | static const char *font_name = "sans-serif"; |
| 36 | static int font_size = 14; |
| 37 | |
| 38 | struct text_layout { |
| 39 | cairo_glyph_t *glyphs; |
| 40 | int num_glyphs; |
| 41 | cairo_text_cluster_t *clusters; |
| 42 | int num_clusters; |
| 43 | cairo_text_cluster_flags_t cluster_flags; |
| 44 | cairo_scaled_font_t *font; |
| 45 | }; |
| 46 | |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 47 | struct text_entry { |
| 48 | struct widget *widget; |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 49 | struct window *window; |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 50 | char *text; |
| 51 | int active; |
Jan Arne Petersen | 7e634a0 | 2012-09-09 23:08:36 +0200 | [diff] [blame] | 52 | uint32_t cursor; |
Jan Arne Petersen | 0e5bd45 | 2012-09-09 23:08:38 +0200 | [diff] [blame] | 53 | uint32_t anchor; |
Jan Arne Petersen | c1fbcb7 | 2012-09-09 23:08:39 +0200 | [diff] [blame] | 54 | char *preedit_text; |
| 55 | uint32_t preedit_cursor; |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 56 | struct text_model *model; |
Jan Arne Petersen | b9eb02c | 2012-09-09 23:08:35 +0200 | [diff] [blame] | 57 | struct text_layout *layout; |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | struct editor { |
Jan Arne Petersen | 5196374 | 2012-08-10 16:47:20 +0200 | [diff] [blame] | 61 | struct text_model_factory *text_model_factory; |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 62 | struct display *display; |
| 63 | struct window *window; |
| 64 | struct widget *widget; |
| 65 | struct text_entry *entry; |
| 66 | struct text_entry *editor; |
| 67 | }; |
| 68 | |
Jan Arne Petersen | b9eb02c | 2012-09-09 23:08:35 +0200 | [diff] [blame] | 69 | static struct text_layout * |
| 70 | text_layout_create(void) |
| 71 | { |
| 72 | struct text_layout *layout; |
| 73 | cairo_surface_t *surface; |
| 74 | cairo_t *cr; |
| 75 | |
| 76 | layout = malloc(sizeof *layout); |
| 77 | if (!layout) |
| 78 | return NULL; |
| 79 | |
| 80 | layout->glyphs = NULL; |
| 81 | layout->num_glyphs = 0; |
| 82 | |
| 83 | layout->clusters = NULL; |
| 84 | layout->num_clusters = 0; |
| 85 | |
| 86 | surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0); |
| 87 | cr = cairo_create(surface); |
| 88 | cairo_set_font_size(cr, font_size); |
| 89 | cairo_select_font_face(cr, font_name, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL); |
| 90 | layout->font = cairo_get_scaled_font(cr); |
| 91 | cairo_scaled_font_reference(layout->font); |
| 92 | |
| 93 | cairo_destroy(cr); |
| 94 | cairo_surface_destroy(surface); |
| 95 | |
| 96 | return layout; |
| 97 | } |
| 98 | |
| 99 | static void |
| 100 | text_layout_destroy(struct text_layout *layout) |
| 101 | { |
| 102 | if (layout->glyphs) |
| 103 | cairo_glyph_free(layout->glyphs); |
| 104 | |
| 105 | if (layout->clusters) |
| 106 | cairo_text_cluster_free(layout->clusters); |
| 107 | |
| 108 | cairo_scaled_font_destroy(layout->font); |
| 109 | |
| 110 | free(layout); |
| 111 | } |
| 112 | |
| 113 | static void |
| 114 | text_layout_set_text(struct text_layout *layout, |
| 115 | const char *text) |
| 116 | { |
| 117 | if (layout->glyphs) |
| 118 | cairo_glyph_free(layout->glyphs); |
| 119 | |
| 120 | if (layout->clusters) |
| 121 | cairo_text_cluster_free(layout->clusters); |
| 122 | |
| 123 | layout->glyphs = NULL; |
| 124 | layout->num_glyphs = 0; |
| 125 | layout->clusters = NULL; |
| 126 | layout->num_clusters = 0; |
| 127 | |
| 128 | cairo_scaled_font_text_to_glyphs(layout->font, 0, 0, text, -1, |
| 129 | &layout->glyphs, &layout->num_glyphs, |
| 130 | &layout->clusters, &layout->num_clusters, |
| 131 | &layout->cluster_flags); |
| 132 | } |
| 133 | |
| 134 | static void |
| 135 | text_layout_draw(struct text_layout *layout, cairo_t *cr) |
| 136 | { |
| 137 | cairo_save(cr); |
| 138 | cairo_set_scaled_font(cr, layout->font); |
| 139 | cairo_show_glyphs(cr, layout->glyphs, layout->num_glyphs); |
| 140 | cairo_restore(cr); |
| 141 | } |
| 142 | |
Jan Arne Petersen | 7e634a0 | 2012-09-09 23:08:36 +0200 | [diff] [blame] | 143 | static void |
| 144 | text_layout_extents(struct text_layout *layout, cairo_text_extents_t *extents) |
| 145 | { |
| 146 | cairo_scaled_font_glyph_extents(layout->font, |
| 147 | layout->glyphs, layout->num_glyphs, |
| 148 | extents); |
| 149 | } |
| 150 | |
| 151 | static int |
| 152 | text_layout_xy_to_index(struct text_layout *layout, double x, double y) |
| 153 | { |
| 154 | cairo_text_extents_t extents; |
| 155 | int i; |
| 156 | |
| 157 | cairo_scaled_font_glyph_extents(layout->font, |
| 158 | layout->glyphs, layout->num_glyphs, |
| 159 | &extents); |
| 160 | |
| 161 | for (i = 1; i < layout->num_glyphs; i++) { |
| 162 | if (layout->glyphs[i].x >= x) { |
| 163 | return i - 1; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | if (x >= layout->glyphs[layout->num_glyphs - 1].x && x < extents.width) |
| 168 | return layout->num_glyphs - 1; |
| 169 | |
| 170 | return layout->num_glyphs; |
| 171 | } |
| 172 | |
| 173 | static void |
| 174 | text_layout_index_to_pos(struct text_layout *layout, uint32_t index, cairo_rectangle_t *pos) |
| 175 | { |
| 176 | cairo_text_extents_t extents; |
| 177 | |
| 178 | if (!pos) |
| 179 | return; |
| 180 | |
| 181 | cairo_scaled_font_glyph_extents(layout->font, |
| 182 | layout->glyphs, layout->num_glyphs, |
| 183 | &extents); |
| 184 | |
| 185 | if ((int)index >= layout->num_glyphs) { |
| 186 | pos->x = extents.x_advance; |
| 187 | pos->y = layout->num_glyphs ? layout->glyphs[layout->num_glyphs - 1].y : 0; |
| 188 | pos->width = 1; |
| 189 | pos->height = extents.height; |
| 190 | return; |
| 191 | } |
| 192 | |
| 193 | pos->x = layout->glyphs[index].x; |
| 194 | pos->y = layout->glyphs[index].y; |
| 195 | pos->width = (int)index < layout->num_glyphs - 1 ? layout->glyphs[index + 1].x : extents.x_advance - pos->x; |
| 196 | pos->height = extents.height; |
| 197 | } |
| 198 | |
| 199 | static void |
| 200 | text_layout_get_cursor_pos(struct text_layout *layout, int index, cairo_rectangle_t *pos) |
| 201 | { |
| 202 | text_layout_index_to_pos(layout, index, pos); |
| 203 | pos->width = 1; |
| 204 | } |
Jan Arne Petersen | b9eb02c | 2012-09-09 23:08:35 +0200 | [diff] [blame] | 205 | |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 206 | static void text_entry_redraw_handler(struct widget *widget, void *data); |
| 207 | static void text_entry_button_handler(struct widget *widget, |
| 208 | struct input *input, uint32_t time, |
| 209 | uint32_t button, |
| 210 | enum wl_pointer_button_state state, void *data); |
Jan Arne Petersen | 09e7c96 | 2012-09-09 23:08:37 +0200 | [diff] [blame] | 211 | static void text_entry_insert_at_cursor(struct text_entry *entry, const char *text); |
Jan Arne Petersen | 43f4aa8 | 2012-09-09 23:08:43 +0200 | [diff] [blame] | 212 | static void text_entry_set_preedit(struct text_entry *entry, |
| 213 | const char *preedit_text, |
| 214 | int preedit_cursor); |
Jan Arne Petersen | e202bae | 2012-09-09 23:08:44 +0200 | [diff] [blame] | 215 | static void text_entry_delete_text(struct text_entry *entry, |
| 216 | uint32_t index, uint32_t length); |
Jan Arne Petersen | e386dd2 | 2012-09-17 15:28:09 +0200 | [diff] [blame] | 217 | static void text_entry_delete_selected_text(struct text_entry *entry); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 218 | |
| 219 | static void |
| 220 | text_model_commit_string(void *data, |
| 221 | struct text_model *text_model, |
| 222 | const char *text, |
| 223 | uint32_t index) |
| 224 | { |
| 225 | struct text_entry *entry = data; |
| 226 | |
Jan Arne Petersen | 09e7c96 | 2012-09-09 23:08:37 +0200 | [diff] [blame] | 227 | if (index > strlen(text)) { |
| 228 | fprintf(stderr, "Invalid cursor index %d\n", index); |
| 229 | index = strlen(text); |
| 230 | } |
| 231 | |
Jan Arne Petersen | e386dd2 | 2012-09-17 15:28:09 +0200 | [diff] [blame] | 232 | text_entry_delete_selected_text(entry); |
Jan Arne Petersen | 09e7c96 | 2012-09-09 23:08:37 +0200 | [diff] [blame] | 233 | text_entry_insert_at_cursor(entry, text); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 234 | |
| 235 | widget_schedule_redraw(entry->widget); |
| 236 | } |
| 237 | |
Jan Arne Petersen | 72f6082 | 2012-08-10 16:47:19 +0200 | [diff] [blame] | 238 | static void |
| 239 | text_model_preedit_string(void *data, |
| 240 | struct text_model *text_model, |
| 241 | const char *text, |
| 242 | uint32_t index) |
| 243 | { |
Jan Arne Petersen | 43f4aa8 | 2012-09-09 23:08:43 +0200 | [diff] [blame] | 244 | struct text_entry *entry = data; |
| 245 | |
| 246 | if (index > strlen(text)) { |
| 247 | fprintf(stderr, "Invalid cursor index %d\n", index); |
| 248 | index = strlen(text); |
| 249 | } |
| 250 | |
Jan Arne Petersen | e386dd2 | 2012-09-17 15:28:09 +0200 | [diff] [blame] | 251 | text_entry_delete_selected_text(entry); |
Jan Arne Petersen | 43f4aa8 | 2012-09-09 23:08:43 +0200 | [diff] [blame] | 252 | text_entry_set_preedit(entry, text, index); |
| 253 | |
| 254 | widget_schedule_redraw(entry->widget); |
Jan Arne Petersen | 72f6082 | 2012-08-10 16:47:19 +0200 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | static void |
Jan Arne Petersen | e202bae | 2012-09-09 23:08:44 +0200 | [diff] [blame] | 258 | text_model_delete_surrounding_text(void *data, |
| 259 | struct text_model *text_model, |
| 260 | int32_t index, |
| 261 | uint32_t length) |
| 262 | { |
| 263 | struct text_entry *entry = data; |
| 264 | uint32_t cursor_index = index + entry->cursor; |
| 265 | |
| 266 | if (cursor_index > strlen(entry->text)) { |
| 267 | fprintf(stderr, "Invalid cursor index %d\n", index); |
| 268 | return; |
| 269 | } |
| 270 | |
| 271 | if (cursor_index + length > strlen(entry->text)) { |
| 272 | fprintf(stderr, "Invalid length %d\n", length); |
| 273 | return; |
| 274 | } |
| 275 | |
| 276 | if (length == 0) |
| 277 | return; |
| 278 | |
| 279 | text_entry_delete_text(entry, cursor_index, length); |
| 280 | } |
| 281 | |
| 282 | static void |
Jan Arne Petersen | 72f6082 | 2012-08-10 16:47:19 +0200 | [diff] [blame] | 283 | text_model_preedit_styling(void *data, |
| 284 | struct text_model *text_model) |
| 285 | { |
| 286 | } |
| 287 | |
| 288 | static void |
| 289 | text_model_key(void *data, |
Jan Arne Petersen | ce8a443 | 2012-09-09 23:08:45 +0200 | [diff] [blame] | 290 | struct text_model *text_model, |
| 291 | uint32_t key, |
| 292 | uint32_t state) |
Jan Arne Petersen | 72f6082 | 2012-08-10 16:47:19 +0200 | [diff] [blame] | 293 | { |
Jan Arne Petersen | 8aba11d | 2012-09-17 15:28:07 +0200 | [diff] [blame] | 294 | struct text_entry *entry = data; |
Jan Arne Petersen | ce8a443 | 2012-09-09 23:08:45 +0200 | [diff] [blame] | 295 | const char *state_label; |
Jan Arne Petersen | 8aba11d | 2012-09-17 15:28:07 +0200 | [diff] [blame] | 296 | const char *key_label = "released"; |
Jan Arne Petersen | ce8a443 | 2012-09-09 23:08:45 +0200 | [diff] [blame] | 297 | |
| 298 | if (state == WL_KEYBOARD_KEY_STATE_PRESSED) { |
| 299 | state_label = "pressed"; |
Jan Arne Petersen | ce8a443 | 2012-09-09 23:08:45 +0200 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | switch (key) { |
| 303 | case XKB_KEY_Tab: |
| 304 | key_label = "Tab"; |
| 305 | break; |
| 306 | case XKB_KEY_KP_Enter: |
| 307 | key_label = "Enter"; |
| 308 | break; |
Jan Arne Petersen | 8aba11d | 2012-09-17 15:28:07 +0200 | [diff] [blame] | 309 | case XKB_KEY_Left: |
| 310 | if (entry->cursor > 0) { |
| 311 | entry->cursor--; |
| 312 | entry->anchor = entry->cursor; |
| 313 | widget_schedule_redraw(entry->widget); |
| 314 | } |
| 315 | break; |
| 316 | case XKB_KEY_Right: |
| 317 | if (entry->cursor < strlen(entry->text)) { |
| 318 | entry->cursor++; |
| 319 | entry->anchor = entry->cursor; |
| 320 | widget_schedule_redraw(entry->widget); |
| 321 | } |
| 322 | break; |
Jan Arne Petersen | ce8a443 | 2012-09-09 23:08:45 +0200 | [diff] [blame] | 323 | default: |
| 324 | key_label = "Unknown"; |
| 325 | } |
| 326 | |
| 327 | fprintf(stderr, "%s key was %s.\n", key_label, state_label); |
Jan Arne Petersen | 72f6082 | 2012-08-10 16:47:19 +0200 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | static void |
| 331 | text_model_selection_replacement(void *data, |
| 332 | struct text_model *text_model) |
| 333 | { |
| 334 | } |
| 335 | |
| 336 | static void |
| 337 | text_model_direction(void *data, |
| 338 | struct text_model *text_model) |
| 339 | { |
| 340 | } |
| 341 | |
| 342 | static void |
| 343 | text_model_locale(void *data, |
| 344 | struct text_model *text_model) |
| 345 | { |
| 346 | } |
| 347 | |
Jan Arne Petersen | de3b6a1 | 2012-08-10 16:47:21 +0200 | [diff] [blame] | 348 | static void |
Jan Arne Petersen | 680275f | 2012-09-24 14:51:14 +0200 | [diff] [blame] | 349 | text_model_enter(void *data, |
| 350 | struct text_model *text_model, |
| 351 | struct wl_surface *surface) |
Jan Arne Petersen | de3b6a1 | 2012-08-10 16:47:21 +0200 | [diff] [blame] | 352 | { |
| 353 | struct text_entry *entry = data; |
| 354 | |
Jan Arne Petersen | 680275f | 2012-09-24 14:51:14 +0200 | [diff] [blame] | 355 | if (surface != window_get_wl_surface(entry->window)) |
| 356 | return; |
| 357 | |
Jan Arne Petersen | de3b6a1 | 2012-08-10 16:47:21 +0200 | [diff] [blame] | 358 | entry->active = 1; |
| 359 | |
| 360 | widget_schedule_redraw(entry->widget); |
| 361 | } |
| 362 | |
| 363 | static void |
Jan Arne Petersen | 680275f | 2012-09-24 14:51:14 +0200 | [diff] [blame] | 364 | text_model_leave(void *data, |
| 365 | struct text_model *text_model) |
Jan Arne Petersen | de3b6a1 | 2012-08-10 16:47:21 +0200 | [diff] [blame] | 366 | { |
| 367 | struct text_entry *entry = data; |
| 368 | |
| 369 | entry->active = 0; |
| 370 | |
| 371 | widget_schedule_redraw(entry->widget); |
| 372 | } |
| 373 | |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 374 | static const struct text_model_listener text_model_listener = { |
Jan Arne Petersen | 72f6082 | 2012-08-10 16:47:19 +0200 | [diff] [blame] | 375 | text_model_commit_string, |
| 376 | text_model_preedit_string, |
Jan Arne Petersen | e202bae | 2012-09-09 23:08:44 +0200 | [diff] [blame] | 377 | text_model_delete_surrounding_text, |
Jan Arne Petersen | 72f6082 | 2012-08-10 16:47:19 +0200 | [diff] [blame] | 378 | text_model_preedit_styling, |
| 379 | text_model_key, |
| 380 | text_model_selection_replacement, |
| 381 | text_model_direction, |
Jan Arne Petersen | de3b6a1 | 2012-08-10 16:47:21 +0200 | [diff] [blame] | 382 | text_model_locale, |
Jan Arne Petersen | 680275f | 2012-09-24 14:51:14 +0200 | [diff] [blame] | 383 | text_model_enter, |
| 384 | text_model_leave |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 385 | }; |
| 386 | |
| 387 | static struct text_entry* |
| 388 | text_entry_create(struct editor *editor, const char *text) |
| 389 | { |
| 390 | struct text_entry *entry; |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 391 | |
| 392 | entry = malloc(sizeof *entry); |
| 393 | |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 394 | entry->widget = widget_add_widget(editor->widget, entry); |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 395 | entry->window = editor->window; |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 396 | entry->text = strdup(text); |
| 397 | entry->active = 0; |
Jan Arne Petersen | 7e634a0 | 2012-09-09 23:08:36 +0200 | [diff] [blame] | 398 | entry->cursor = strlen(text); |
Jan Arne Petersen | 0e5bd45 | 2012-09-09 23:08:38 +0200 | [diff] [blame] | 399 | entry->anchor = entry->cursor; |
Jan Arne Petersen | c1fbcb7 | 2012-09-09 23:08:39 +0200 | [diff] [blame] | 400 | entry->preedit_text = NULL; |
| 401 | entry->preedit_cursor = 0; |
Jan Arne Petersen | 4c26518 | 2012-09-09 23:08:30 +0200 | [diff] [blame] | 402 | entry->model = text_model_factory_create_text_model(editor->text_model_factory); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 403 | text_model_add_listener(entry->model, &text_model_listener, entry); |
| 404 | |
Jan Arne Petersen | b9eb02c | 2012-09-09 23:08:35 +0200 | [diff] [blame] | 405 | entry->layout = text_layout_create(); |
| 406 | text_layout_set_text(entry->layout, entry->text); |
| 407 | |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 408 | widget_set_redraw_handler(entry->widget, text_entry_redraw_handler); |
| 409 | widget_set_button_handler(entry->widget, text_entry_button_handler); |
| 410 | |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 411 | return entry; |
| 412 | } |
| 413 | |
| 414 | static void |
| 415 | text_entry_destroy(struct text_entry *entry) |
| 416 | { |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 417 | widget_destroy(entry->widget); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 418 | text_model_destroy(entry->model); |
Jan Arne Petersen | b9eb02c | 2012-09-09 23:08:35 +0200 | [diff] [blame] | 419 | text_layout_destroy(entry->layout); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 420 | free(entry->text); |
| 421 | free(entry); |
| 422 | } |
| 423 | |
| 424 | static void |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 425 | redraw_handler(struct widget *widget, void *data) |
| 426 | { |
| 427 | struct editor *editor = data; |
| 428 | cairo_surface_t *surface; |
| 429 | struct rectangle allocation; |
| 430 | cairo_t *cr; |
| 431 | |
| 432 | surface = window_get_surface(editor->window); |
| 433 | widget_get_allocation(editor->widget, &allocation); |
| 434 | |
| 435 | cr = cairo_create(surface); |
| 436 | cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height); |
| 437 | cairo_clip(cr); |
| 438 | |
| 439 | cairo_translate(cr, allocation.x, allocation.y); |
| 440 | |
| 441 | /* Draw background */ |
| 442 | cairo_push_group(cr); |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 443 | cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 444 | cairo_set_source_rgba(cr, 1, 1, 1, 1); |
| 445 | cairo_rectangle(cr, 0, 0, allocation.width, allocation.height); |
| 446 | cairo_fill(cr); |
| 447 | |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 448 | cairo_pop_group_to_source(cr); |
| 449 | cairo_paint(cr); |
| 450 | |
| 451 | cairo_destroy(cr); |
| 452 | cairo_surface_destroy(surface); |
| 453 | } |
| 454 | |
| 455 | static void |
| 456 | text_entry_allocate(struct text_entry *entry, int32_t x, int32_t y, |
| 457 | int32_t width, int32_t height) |
| 458 | { |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 459 | widget_set_allocation(entry->widget, x, y, width, height); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | static void |
| 463 | resize_handler(struct widget *widget, |
| 464 | int32_t width, int32_t height, void *data) |
| 465 | { |
| 466 | struct editor *editor = data; |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 467 | struct rectangle allocation; |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 468 | |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 469 | widget_get_allocation(editor->widget, &allocation); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 470 | |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 471 | text_entry_allocate(editor->entry, |
| 472 | allocation.x + 20, allocation.y + 20, |
| 473 | width - 40, height / 2 - 40); |
| 474 | text_entry_allocate(editor->editor, |
| 475 | allocation.x + 20, allocation.y + height / 2 + 20, |
| 476 | width - 40, height / 2 - 40); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | static void |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 480 | text_entry_activate(struct text_entry *entry, |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 481 | struct wl_seat *seat) |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 482 | { |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 483 | struct wl_surface *surface = window_get_wl_surface(entry->window); |
| 484 | |
| 485 | text_model_activate(entry->model, |
| 486 | seat, |
| 487 | surface); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | static void |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 491 | text_entry_deactivate(struct text_entry *entry, |
| 492 | struct wl_seat *seat) |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 493 | { |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 494 | text_model_deactivate(entry->model, |
| 495 | seat); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | static void |
Jan Arne Petersen | c1fbcb7 | 2012-09-09 23:08:39 +0200 | [diff] [blame] | 499 | text_entry_update_layout(struct text_entry *entry) |
| 500 | { |
| 501 | char *text; |
| 502 | |
Philipp Brüschweiler | 237358b | 2012-10-02 11:06:51 +0200 | [diff] [blame] | 503 | assert(((unsigned int)entry->cursor) <= strlen(entry->text) + |
| 504 | (entry->preedit_text ? strlen(entry->preedit_text) : 0)); |
Jan Arne Petersen | c1fbcb7 | 2012-09-09 23:08:39 +0200 | [diff] [blame] | 505 | |
| 506 | if (!entry->preedit_text) { |
| 507 | text_layout_set_text(entry->layout, entry->text); |
| 508 | return; |
| 509 | } |
| 510 | |
| 511 | text = malloc(strlen(entry->text) + strlen(entry->preedit_text) + 1); |
| 512 | strncpy(text, entry->text, entry->cursor); |
| 513 | strcpy(text + entry->cursor, entry->preedit_text); |
| 514 | strcpy(text + entry->cursor + strlen(entry->preedit_text), |
| 515 | entry->text + entry->cursor); |
| 516 | |
| 517 | text_layout_set_text(entry->layout, text); |
| 518 | free(text); |
| 519 | |
| 520 | widget_schedule_redraw(entry->widget); |
Jan Arne Petersen | cb08f4d | 2012-09-09 23:08:40 +0200 | [diff] [blame] | 521 | |
| 522 | text_model_set_surrounding_text(entry->model, |
| 523 | entry->text, |
| 524 | entry->cursor, |
| 525 | entry->anchor); |
Jan Arne Petersen | c1fbcb7 | 2012-09-09 23:08:39 +0200 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | static void |
Jan Arne Petersen | 09e7c96 | 2012-09-09 23:08:37 +0200 | [diff] [blame] | 529 | text_entry_insert_at_cursor(struct text_entry *entry, const char *text) |
| 530 | { |
| 531 | char *new_text = malloc(strlen(entry->text) + strlen(text) + 1); |
| 532 | |
| 533 | strncpy(new_text, entry->text, entry->cursor); |
| 534 | strcpy(new_text + entry->cursor, text); |
| 535 | strcpy(new_text + entry->cursor + strlen(text), |
| 536 | entry->text + entry->cursor); |
| 537 | |
| 538 | free(entry->text); |
| 539 | entry->text = new_text; |
| 540 | entry->cursor += strlen(text); |
Jan Arne Petersen | 0e5bd45 | 2012-09-09 23:08:38 +0200 | [diff] [blame] | 541 | entry->anchor += strlen(text); |
Jan Arne Petersen | 09e7c96 | 2012-09-09 23:08:37 +0200 | [diff] [blame] | 542 | |
Jan Arne Petersen | c1fbcb7 | 2012-09-09 23:08:39 +0200 | [diff] [blame] | 543 | text_entry_update_layout(entry); |
| 544 | } |
| 545 | |
| 546 | static void |
| 547 | text_entry_set_preedit(struct text_entry *entry, |
| 548 | const char *preedit_text, |
| 549 | int preedit_cursor) |
| 550 | { |
| 551 | if (entry->preedit_text) { |
| 552 | free(entry->preedit_text); |
| 553 | entry->preedit_text = NULL; |
| 554 | entry->preedit_cursor = 0; |
| 555 | } |
| 556 | |
| 557 | if (!preedit_text) |
| 558 | return; |
| 559 | |
| 560 | entry->preedit_text = strdup(preedit_text); |
| 561 | entry->preedit_cursor = preedit_cursor; |
| 562 | |
| 563 | text_entry_update_layout(entry); |
Jan Arne Petersen | 09e7c96 | 2012-09-09 23:08:37 +0200 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | static void |
Jan Arne Petersen | 7e634a0 | 2012-09-09 23:08:36 +0200 | [diff] [blame] | 567 | text_entry_set_cursor_position(struct text_entry *entry, |
| 568 | int32_t x, int32_t y) |
| 569 | { |
| 570 | entry->cursor = text_layout_xy_to_index(entry->layout, x, y); |
| 571 | |
Jan Arne Petersen | c1e481e | 2012-09-09 23:08:46 +0200 | [diff] [blame] | 572 | text_model_reset(entry->model); |
| 573 | |
Jan Arne Petersen | c1fbcb7 | 2012-09-09 23:08:39 +0200 | [diff] [blame] | 574 | if (entry->cursor >= entry->preedit_cursor) { |
| 575 | entry->cursor -= entry->preedit_cursor; |
| 576 | } |
| 577 | |
| 578 | text_entry_update_layout(entry); |
| 579 | |
Jan Arne Petersen | 7e634a0 | 2012-09-09 23:08:36 +0200 | [diff] [blame] | 580 | widget_schedule_redraw(entry->widget); |
| 581 | } |
| 582 | |
| 583 | static void |
Jan Arne Petersen | 0e5bd45 | 2012-09-09 23:08:38 +0200 | [diff] [blame] | 584 | text_entry_set_anchor_position(struct text_entry *entry, |
| 585 | int32_t x, int32_t y) |
| 586 | { |
| 587 | entry->anchor = text_layout_xy_to_index(entry->layout, x, y); |
| 588 | |
| 589 | widget_schedule_redraw(entry->widget); |
| 590 | } |
| 591 | |
| 592 | static void |
Jan Arne Petersen | e202bae | 2012-09-09 23:08:44 +0200 | [diff] [blame] | 593 | text_entry_delete_text(struct text_entry *entry, |
| 594 | uint32_t index, uint32_t length) |
| 595 | { |
| 596 | if (entry->cursor > index) |
| 597 | entry->cursor -= length; |
| 598 | |
Jan Arne Petersen | 80ad1a9 | 2012-09-17 15:28:10 +0200 | [diff] [blame] | 599 | entry->anchor = entry->cursor; |
| 600 | |
Jan Arne Petersen | e202bae | 2012-09-09 23:08:44 +0200 | [diff] [blame] | 601 | entry->text[index] = '\0'; |
| 602 | strcat(entry->text, entry->text + index + length); |
| 603 | |
| 604 | text_entry_update_layout(entry); |
| 605 | |
| 606 | widget_schedule_redraw(entry->widget); |
| 607 | } |
| 608 | |
| 609 | static void |
Jan Arne Petersen | e386dd2 | 2012-09-17 15:28:09 +0200 | [diff] [blame] | 610 | text_entry_delete_selected_text(struct text_entry *entry) |
| 611 | { |
| 612 | uint32_t start_index = entry->anchor < entry->cursor ? entry->anchor : entry->cursor; |
| 613 | uint32_t end_index = entry->anchor < entry->cursor ? entry->cursor : entry->anchor; |
| 614 | |
| 615 | if (entry->anchor == entry->cursor) |
| 616 | return; |
| 617 | |
| 618 | text_entry_delete_text(entry, start_index, end_index - start_index); |
| 619 | |
| 620 | entry->anchor = entry->cursor; |
| 621 | } |
| 622 | |
| 623 | static void |
Jan Arne Petersen | 0e5bd45 | 2012-09-09 23:08:38 +0200 | [diff] [blame] | 624 | text_entry_draw_selection(struct text_entry *entry, cairo_t *cr) |
| 625 | { |
| 626 | cairo_text_extents_t extents; |
| 627 | uint32_t start_index = entry->anchor < entry->cursor ? entry->anchor : entry->cursor; |
| 628 | uint32_t end_index = entry->anchor < entry->cursor ? entry->cursor : entry->anchor; |
| 629 | cairo_rectangle_t start; |
| 630 | cairo_rectangle_t end; |
| 631 | |
| 632 | if (entry->anchor == entry->cursor) |
| 633 | return; |
| 634 | |
| 635 | text_layout_extents(entry->layout, &extents); |
| 636 | |
| 637 | text_layout_index_to_pos(entry->layout, start_index, &start); |
| 638 | text_layout_index_to_pos(entry->layout, end_index, &end); |
| 639 | |
| 640 | cairo_save (cr); |
| 641 | |
Philipp Brüschweiler | b8911dc | 2012-10-02 11:06:52 +0200 | [diff] [blame] | 642 | cairo_set_source_rgba(cr, 0.3, 0.3, 1.0, 0.5); |
Jan Arne Petersen | 0e5bd45 | 2012-09-09 23:08:38 +0200 | [diff] [blame] | 643 | cairo_rectangle(cr, |
| 644 | start.x, extents.y_bearing + extents.height + 2, |
| 645 | end.x - start.x, -extents.height - 4); |
| 646 | cairo_fill(cr); |
| 647 | |
| 648 | cairo_rectangle(cr, |
| 649 | start.x, extents.y_bearing + extents.height, |
| 650 | end.x - start.x, -extents.height); |
| 651 | cairo_clip(cr); |
| 652 | cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0); |
| 653 | text_layout_draw(entry->layout, cr); |
| 654 | |
| 655 | cairo_restore (cr); |
| 656 | } |
| 657 | |
| 658 | static void |
Jan Arne Petersen | 7e634a0 | 2012-09-09 23:08:36 +0200 | [diff] [blame] | 659 | text_entry_draw_cursor(struct text_entry *entry, cairo_t *cr) |
| 660 | { |
| 661 | cairo_text_extents_t extents; |
| 662 | cairo_rectangle_t cursor_pos; |
| 663 | |
| 664 | text_layout_extents(entry->layout, &extents); |
Jan Arne Petersen | c1fbcb7 | 2012-09-09 23:08:39 +0200 | [diff] [blame] | 665 | text_layout_get_cursor_pos(entry->layout, |
| 666 | entry->cursor + entry->preedit_cursor, |
| 667 | &cursor_pos); |
Jan Arne Petersen | 7e634a0 | 2012-09-09 23:08:36 +0200 | [diff] [blame] | 668 | |
| 669 | cairo_set_line_width(cr, 1.0); |
| 670 | cairo_move_to(cr, cursor_pos.x, extents.y_bearing + extents.height + 2); |
| 671 | cairo_line_to(cr, cursor_pos.x, extents.y_bearing - 2); |
| 672 | cairo_stroke(cr); |
| 673 | } |
| 674 | |
| 675 | static void |
Jan Arne Petersen | c1fbcb7 | 2012-09-09 23:08:39 +0200 | [diff] [blame] | 676 | text_entry_draw_preedit(struct text_entry *entry, cairo_t *cr) |
| 677 | { |
| 678 | cairo_text_extents_t extents; |
| 679 | cairo_rectangle_t start; |
| 680 | cairo_rectangle_t end; |
| 681 | |
| 682 | if (!entry->preedit_text) |
| 683 | return; |
| 684 | |
| 685 | text_layout_extents(entry->layout, &extents); |
| 686 | |
| 687 | text_layout_index_to_pos(entry->layout, entry->cursor, &start); |
| 688 | text_layout_index_to_pos(entry->layout, |
| 689 | entry->cursor + strlen(entry->preedit_text), |
| 690 | &end); |
| 691 | |
| 692 | cairo_save (cr); |
| 693 | |
| 694 | cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0); |
| 695 | cairo_rectangle(cr, |
| 696 | start.x, 0, |
| 697 | end.x - start.x, 1); |
| 698 | cairo_fill(cr); |
| 699 | |
| 700 | cairo_restore (cr); |
| 701 | } |
| 702 | |
Philipp Brüschweiler | 9f897c7 | 2012-10-02 11:06:53 +0200 | [diff] [blame^] | 703 | static const int text_offset_left = 10; |
| 704 | |
Jan Arne Petersen | c1fbcb7 | 2012-09-09 23:08:39 +0200 | [diff] [blame] | 705 | static void |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 706 | text_entry_redraw_handler(struct widget *widget, void *data) |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 707 | { |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 708 | struct text_entry *entry = data; |
| 709 | cairo_surface_t *surface; |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 710 | struct rectangle allocation; |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 711 | cairo_t *cr; |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 712 | |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 713 | surface = window_get_surface(entry->window); |
| 714 | widget_get_allocation(entry->widget, &allocation); |
| 715 | |
| 716 | cr = cairo_create(surface); |
| 717 | cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height); |
| 718 | cairo_clip(cr); |
| 719 | |
| 720 | cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); |
| 721 | |
| 722 | cairo_push_group(cr); |
| 723 | cairo_translate(cr, allocation.x, allocation.y); |
| 724 | |
| 725 | cairo_set_source_rgba(cr, 1, 1, 1, 1); |
| 726 | cairo_rectangle(cr, 0, 0, allocation.width, allocation.height); |
| 727 | cairo_fill(cr); |
| 728 | |
| 729 | cairo_set_operator(cr, CAIRO_OPERATOR_OVER); |
| 730 | |
| 731 | if (entry->active) { |
| 732 | cairo_rectangle(cr, 0, 0, allocation.width, allocation.height); |
| 733 | cairo_set_line_width (cr, 3); |
| 734 | cairo_set_source_rgba(cr, 0, 0, 1, 1.0); |
| 735 | cairo_stroke(cr); |
| 736 | } |
| 737 | |
| 738 | cairo_set_source_rgba(cr, 0, 0, 0, 1); |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 739 | |
Philipp Brüschweiler | 9f897c7 | 2012-10-02 11:06:53 +0200 | [diff] [blame^] | 740 | cairo_translate(cr, text_offset_left, allocation.height / 2); |
Jan Arne Petersen | b9eb02c | 2012-09-09 23:08:35 +0200 | [diff] [blame] | 741 | text_layout_draw(entry->layout, cr); |
Jan Arne Petersen | 7e634a0 | 2012-09-09 23:08:36 +0200 | [diff] [blame] | 742 | |
Jan Arne Petersen | 0e5bd45 | 2012-09-09 23:08:38 +0200 | [diff] [blame] | 743 | text_entry_draw_selection(entry, cr); |
| 744 | |
Jan Arne Petersen | 7e634a0 | 2012-09-09 23:08:36 +0200 | [diff] [blame] | 745 | text_entry_draw_cursor(entry, cr); |
| 746 | |
Jan Arne Petersen | c1fbcb7 | 2012-09-09 23:08:39 +0200 | [diff] [blame] | 747 | text_entry_draw_preedit(entry, cr); |
| 748 | |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 749 | cairo_pop_group_to_source(cr); |
| 750 | cairo_paint(cr); |
| 751 | |
| 752 | cairo_destroy(cr); |
| 753 | cairo_surface_destroy(surface); |
| 754 | } |
| 755 | |
Jan Arne Petersen | 0e5bd45 | 2012-09-09 23:08:38 +0200 | [diff] [blame] | 756 | static int |
| 757 | text_entry_motion_handler(struct widget *widget, |
| 758 | struct input *input, uint32_t time, |
| 759 | float x, float y, void *data) |
| 760 | { |
| 761 | struct text_entry *entry = data; |
| 762 | struct rectangle allocation; |
| 763 | |
| 764 | widget_get_allocation(entry->widget, &allocation); |
| 765 | |
| 766 | text_entry_set_cursor_position(entry, |
Philipp Brüschweiler | 9f897c7 | 2012-10-02 11:06:53 +0200 | [diff] [blame^] | 767 | x - allocation.x - text_offset_left, |
| 768 | y - allocation.y - text_offset_left); |
Jan Arne Petersen | 0e5bd45 | 2012-09-09 23:08:38 +0200 | [diff] [blame] | 769 | |
| 770 | return CURSOR_IBEAM; |
| 771 | } |
| 772 | |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 773 | static void |
| 774 | text_entry_button_handler(struct widget *widget, |
| 775 | struct input *input, uint32_t time, |
| 776 | uint32_t button, |
| 777 | enum wl_pointer_button_state state, void *data) |
| 778 | { |
| 779 | struct text_entry *entry = data; |
Jan Arne Petersen | 7e634a0 | 2012-09-09 23:08:36 +0200 | [diff] [blame] | 780 | struct rectangle allocation; |
| 781 | int32_t x, y; |
| 782 | |
| 783 | widget_get_allocation(entry->widget, &allocation); |
| 784 | input_get_position(input, &x, &y); |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 785 | |
| 786 | if (button != BTN_LEFT) { |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 787 | return; |
| 788 | } |
| 789 | |
Jan Arne Petersen | 0e5bd45 | 2012-09-09 23:08:38 +0200 | [diff] [blame] | 790 | text_entry_set_cursor_position(entry, |
Philipp Brüschweiler | 9f897c7 | 2012-10-02 11:06:53 +0200 | [diff] [blame^] | 791 | x - allocation.x - text_offset_left, |
| 792 | y - allocation.y - text_offset_left); |
Jan Arne Petersen | 7e634a0 | 2012-09-09 23:08:36 +0200 | [diff] [blame] | 793 | |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 794 | if (state == WL_POINTER_BUTTON_STATE_PRESSED) { |
| 795 | struct wl_seat *seat = input_get_seat(input); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 796 | |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 797 | text_entry_activate(entry, seat); |
Jan Arne Petersen | 0e5bd45 | 2012-09-09 23:08:38 +0200 | [diff] [blame] | 798 | |
| 799 | text_entry_set_anchor_position(entry, |
Philipp Brüschweiler | 9f897c7 | 2012-10-02 11:06:53 +0200 | [diff] [blame^] | 800 | x - allocation.x - text_offset_left, |
| 801 | y - allocation.y - text_offset_left); |
Jan Arne Petersen | 0e5bd45 | 2012-09-09 23:08:38 +0200 | [diff] [blame] | 802 | |
| 803 | widget_set_motion_handler(entry->widget, text_entry_motion_handler); |
| 804 | } else { |
| 805 | widget_set_motion_handler(entry->widget, NULL); |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 806 | } |
| 807 | } |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 808 | |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 809 | static void |
| 810 | editor_button_handler(struct widget *widget, |
| 811 | struct input *input, uint32_t time, |
| 812 | uint32_t button, |
| 813 | enum wl_pointer_button_state state, void *data) |
| 814 | { |
| 815 | struct editor *editor = data; |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 816 | |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 817 | if (button != BTN_LEFT) { |
| 818 | return; |
| 819 | } |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 820 | |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 821 | if (state == WL_POINTER_BUTTON_STATE_PRESSED) { |
| 822 | struct wl_seat *seat = input_get_seat(input); |
| 823 | |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 824 | text_entry_deactivate(editor->entry, seat); |
| 825 | text_entry_deactivate(editor->editor, seat); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 826 | } |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | static void |
| 830 | global_handler(struct wl_display *display, uint32_t id, |
| 831 | const char *interface, uint32_t version, void *data) |
| 832 | { |
| 833 | struct editor *editor = data; |
| 834 | |
Jan Arne Petersen | 5196374 | 2012-08-10 16:47:20 +0200 | [diff] [blame] | 835 | if (!strcmp(interface, "text_model_factory")) { |
| 836 | editor->text_model_factory = wl_display_bind(display, id, |
| 837 | &text_model_factory_interface); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 838 | } |
| 839 | } |
| 840 | |
| 841 | int |
| 842 | main(int argc, char *argv[]) |
| 843 | { |
| 844 | struct editor editor; |
| 845 | |
| 846 | editor.display = display_create(argc, argv); |
| 847 | if (editor.display == NULL) { |
| 848 | fprintf(stderr, "failed to create display: %m\n"); |
| 849 | return -1; |
| 850 | } |
| 851 | wl_display_add_global_listener(display_get_display(editor.display), |
| 852 | global_handler, &editor); |
| 853 | |
| 854 | |
| 855 | editor.window = window_create(editor.display); |
| 856 | editor.widget = frame_create(editor.window, &editor); |
| 857 | |
| 858 | editor.entry = text_entry_create(&editor, "Entry"); |
| 859 | editor.editor = text_entry_create(&editor, "Editor"); |
Jan Arne Petersen | c1fbcb7 | 2012-09-09 23:08:39 +0200 | [diff] [blame] | 860 | text_entry_set_preedit(editor.editor, "preedit", strlen("preedit")); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 861 | |
| 862 | window_set_title(editor.window, "Text Editor"); |
| 863 | |
| 864 | widget_set_redraw_handler(editor.widget, redraw_handler); |
| 865 | widget_set_resize_handler(editor.widget, resize_handler); |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 866 | widget_set_button_handler(editor.widget, editor_button_handler); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 867 | |
| 868 | window_schedule_resize(editor.window, 500, 400); |
| 869 | |
| 870 | display_run(editor.display); |
| 871 | |
| 872 | text_entry_destroy(editor.entry); |
| 873 | text_entry_destroy(editor.editor); |
| 874 | |
| 875 | return 0; |
| 876 | } |