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 | cd99706 | 2012-11-18 19:06:44 +0100 | [diff] [blame] | 58 | struct { |
| 59 | xkb_mod_mask_t shift_mask; |
| 60 | } keysym; |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | struct editor { |
Jan Arne Petersen | 5196374 | 2012-08-10 16:47:20 +0200 | [diff] [blame] | 64 | struct text_model_factory *text_model_factory; |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 65 | struct display *display; |
| 66 | struct window *window; |
| 67 | struct widget *widget; |
| 68 | struct text_entry *entry; |
| 69 | struct text_entry *editor; |
Rob Bradford | 9d1d32b | 2012-11-18 19:06:49 +0100 | [diff] [blame^] | 70 | struct text_entry *active_entry; |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 71 | }; |
| 72 | |
Jan Arne Petersen | 6345faa | 2012-11-05 03:26:39 +0100 | [diff] [blame] | 73 | static const char * |
| 74 | utf8_start_char(const char *text, const char *p) |
| 75 | { |
| 76 | for (; p >= text; --p) { |
| 77 | if ((*p & 0xc0) != 0x80) |
| 78 | return p; |
| 79 | } |
| 80 | return NULL; |
| 81 | } |
| 82 | |
| 83 | static const char * |
| 84 | utf8_prev_char(const char *text, const char *p) |
| 85 | { |
| 86 | if (p > text) |
| 87 | return utf8_start_char(text, --p); |
| 88 | return NULL; |
| 89 | } |
| 90 | |
| 91 | static const char * |
| 92 | utf8_end_char(const char *p) |
| 93 | { |
| 94 | while ((*p & 0xc0) == 0x80) |
| 95 | p++; |
| 96 | return p; |
| 97 | } |
| 98 | |
| 99 | static const char * |
| 100 | utf8_next_char(const char *p) |
| 101 | { |
| 102 | if (*p != 0) |
| 103 | return utf8_end_char(++p); |
| 104 | return NULL; |
| 105 | } |
| 106 | |
Jan Arne Petersen | b9eb02c | 2012-09-09 23:08:35 +0200 | [diff] [blame] | 107 | static struct text_layout * |
| 108 | text_layout_create(void) |
| 109 | { |
| 110 | struct text_layout *layout; |
| 111 | cairo_surface_t *surface; |
| 112 | cairo_t *cr; |
| 113 | |
| 114 | layout = malloc(sizeof *layout); |
| 115 | if (!layout) |
| 116 | return NULL; |
| 117 | |
| 118 | layout->glyphs = NULL; |
| 119 | layout->num_glyphs = 0; |
| 120 | |
| 121 | layout->clusters = NULL; |
| 122 | layout->num_clusters = 0; |
| 123 | |
| 124 | surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0); |
| 125 | cr = cairo_create(surface); |
| 126 | cairo_set_font_size(cr, font_size); |
| 127 | cairo_select_font_face(cr, font_name, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL); |
| 128 | layout->font = cairo_get_scaled_font(cr); |
| 129 | cairo_scaled_font_reference(layout->font); |
| 130 | |
| 131 | cairo_destroy(cr); |
| 132 | cairo_surface_destroy(surface); |
| 133 | |
| 134 | return layout; |
| 135 | } |
| 136 | |
| 137 | static void |
| 138 | text_layout_destroy(struct text_layout *layout) |
| 139 | { |
| 140 | if (layout->glyphs) |
| 141 | cairo_glyph_free(layout->glyphs); |
| 142 | |
| 143 | if (layout->clusters) |
| 144 | cairo_text_cluster_free(layout->clusters); |
| 145 | |
| 146 | cairo_scaled_font_destroy(layout->font); |
| 147 | |
| 148 | free(layout); |
| 149 | } |
| 150 | |
| 151 | static void |
| 152 | text_layout_set_text(struct text_layout *layout, |
| 153 | const char *text) |
| 154 | { |
| 155 | if (layout->glyphs) |
| 156 | cairo_glyph_free(layout->glyphs); |
| 157 | |
| 158 | if (layout->clusters) |
| 159 | cairo_text_cluster_free(layout->clusters); |
| 160 | |
| 161 | layout->glyphs = NULL; |
| 162 | layout->num_glyphs = 0; |
| 163 | layout->clusters = NULL; |
| 164 | layout->num_clusters = 0; |
| 165 | |
| 166 | cairo_scaled_font_text_to_glyphs(layout->font, 0, 0, text, -1, |
| 167 | &layout->glyphs, &layout->num_glyphs, |
| 168 | &layout->clusters, &layout->num_clusters, |
| 169 | &layout->cluster_flags); |
| 170 | } |
| 171 | |
| 172 | static void |
| 173 | text_layout_draw(struct text_layout *layout, cairo_t *cr) |
| 174 | { |
| 175 | cairo_save(cr); |
| 176 | cairo_set_scaled_font(cr, layout->font); |
| 177 | cairo_show_glyphs(cr, layout->glyphs, layout->num_glyphs); |
| 178 | cairo_restore(cr); |
| 179 | } |
| 180 | |
Jan Arne Petersen | 7e634a0 | 2012-09-09 23:08:36 +0200 | [diff] [blame] | 181 | static void |
| 182 | text_layout_extents(struct text_layout *layout, cairo_text_extents_t *extents) |
| 183 | { |
| 184 | cairo_scaled_font_glyph_extents(layout->font, |
| 185 | layout->glyphs, layout->num_glyphs, |
| 186 | extents); |
| 187 | } |
| 188 | |
Jan Arne Petersen | 6345faa | 2012-11-05 03:26:39 +0100 | [diff] [blame] | 189 | static uint32_t |
| 190 | bytes_from_glyphs(struct text_layout *layout, uint32_t index) |
| 191 | { |
| 192 | int i; |
| 193 | uint32_t glyphs = 0, bytes = 0; |
| 194 | |
| 195 | for (i = 0; i < layout->num_clusters && glyphs < index; i++) { |
| 196 | bytes += layout->clusters[i].num_bytes; |
| 197 | glyphs += layout->clusters[i].num_glyphs; |
| 198 | } |
| 199 | |
| 200 | return bytes; |
| 201 | } |
| 202 | |
| 203 | static uint32_t |
| 204 | glyphs_from_bytes(struct text_layout *layout, uint32_t index) |
| 205 | { |
| 206 | int i; |
| 207 | uint32_t glyphs = 0, bytes = 0; |
| 208 | |
| 209 | for (i = 0; i < layout->num_clusters && bytes < index; i++) { |
| 210 | bytes += layout->clusters[i].num_bytes; |
| 211 | glyphs += layout->clusters[i].num_glyphs; |
| 212 | } |
| 213 | |
| 214 | return glyphs; |
| 215 | } |
| 216 | |
Jan Arne Petersen | 7e634a0 | 2012-09-09 23:08:36 +0200 | [diff] [blame] | 217 | static int |
| 218 | text_layout_xy_to_index(struct text_layout *layout, double x, double y) |
| 219 | { |
| 220 | cairo_text_extents_t extents; |
| 221 | int i; |
Philipp Brüschweiler | 70f8367 | 2012-10-02 11:06:54 +0200 | [diff] [blame] | 222 | double d; |
| 223 | |
| 224 | if (layout->num_glyphs == 0) |
| 225 | return 0; |
Jan Arne Petersen | 7e634a0 | 2012-09-09 23:08:36 +0200 | [diff] [blame] | 226 | |
| 227 | cairo_scaled_font_glyph_extents(layout->font, |
| 228 | layout->glyphs, layout->num_glyphs, |
| 229 | &extents); |
| 230 | |
Philipp Brüschweiler | 70f8367 | 2012-10-02 11:06:54 +0200 | [diff] [blame] | 231 | if (x < 0) |
| 232 | return 0; |
| 233 | |
| 234 | for (i = 0; i < layout->num_glyphs - 1; ++i) { |
| 235 | d = layout->glyphs[i + 1].x - layout->glyphs[i].x; |
| 236 | if (x < layout->glyphs[i].x + d/2) |
Jan Arne Petersen | 6345faa | 2012-11-05 03:26:39 +0100 | [diff] [blame] | 237 | return bytes_from_glyphs(layout, i); |
Jan Arne Petersen | 7e634a0 | 2012-09-09 23:08:36 +0200 | [diff] [blame] | 238 | } |
| 239 | |
Philipp Brüschweiler | 70f8367 | 2012-10-02 11:06:54 +0200 | [diff] [blame] | 240 | d = extents.width - layout->glyphs[layout->num_glyphs - 1].x; |
| 241 | if (x < layout->glyphs[layout->num_glyphs - 1].x + d/2) |
Jan Arne Petersen | 6345faa | 2012-11-05 03:26:39 +0100 | [diff] [blame] | 242 | return bytes_from_glyphs(layout, layout->num_glyphs - 1); |
Jan Arne Petersen | 7e634a0 | 2012-09-09 23:08:36 +0200 | [diff] [blame] | 243 | |
Jan Arne Petersen | 6345faa | 2012-11-05 03:26:39 +0100 | [diff] [blame] | 244 | return bytes_from_glyphs(layout, layout->num_glyphs); |
Jan Arne Petersen | 7e634a0 | 2012-09-09 23:08:36 +0200 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | static void |
| 248 | text_layout_index_to_pos(struct text_layout *layout, uint32_t index, cairo_rectangle_t *pos) |
| 249 | { |
| 250 | cairo_text_extents_t extents; |
Jan Arne Petersen | 6345faa | 2012-11-05 03:26:39 +0100 | [diff] [blame] | 251 | int glyph_index = glyphs_from_bytes(layout, index); |
Jan Arne Petersen | 7e634a0 | 2012-09-09 23:08:36 +0200 | [diff] [blame] | 252 | |
| 253 | if (!pos) |
| 254 | return; |
| 255 | |
| 256 | cairo_scaled_font_glyph_extents(layout->font, |
| 257 | layout->glyphs, layout->num_glyphs, |
| 258 | &extents); |
| 259 | |
Jan Arne Petersen | 6345faa | 2012-11-05 03:26:39 +0100 | [diff] [blame] | 260 | if (glyph_index >= layout->num_glyphs) { |
Jan Arne Petersen | 7e634a0 | 2012-09-09 23:08:36 +0200 | [diff] [blame] | 261 | pos->x = extents.x_advance; |
| 262 | pos->y = layout->num_glyphs ? layout->glyphs[layout->num_glyphs - 1].y : 0; |
| 263 | pos->width = 1; |
| 264 | pos->height = extents.height; |
| 265 | return; |
| 266 | } |
| 267 | |
Jan Arne Petersen | 6345faa | 2012-11-05 03:26:39 +0100 | [diff] [blame] | 268 | pos->x = layout->glyphs[glyph_index].x; |
| 269 | pos->y = layout->glyphs[glyph_index].y; |
| 270 | pos->width = glyph_index < layout->num_glyphs - 1 ? layout->glyphs[glyph_index + 1].x : extents.x_advance - pos->x; |
Jan Arne Petersen | 7e634a0 | 2012-09-09 23:08:36 +0200 | [diff] [blame] | 271 | pos->height = extents.height; |
| 272 | } |
| 273 | |
| 274 | static void |
| 275 | text_layout_get_cursor_pos(struct text_layout *layout, int index, cairo_rectangle_t *pos) |
| 276 | { |
| 277 | text_layout_index_to_pos(layout, index, pos); |
| 278 | pos->width = 1; |
| 279 | } |
Jan Arne Petersen | b9eb02c | 2012-09-09 23:08:35 +0200 | [diff] [blame] | 280 | |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 281 | static void text_entry_redraw_handler(struct widget *widget, void *data); |
| 282 | static void text_entry_button_handler(struct widget *widget, |
| 283 | struct input *input, uint32_t time, |
| 284 | uint32_t button, |
| 285 | enum wl_pointer_button_state state, void *data); |
Jan Arne Petersen | 09e7c96 | 2012-09-09 23:08:37 +0200 | [diff] [blame] | 286 | 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] | 287 | static void text_entry_set_preedit(struct text_entry *entry, |
| 288 | const char *preedit_text, |
| 289 | int preedit_cursor); |
Jan Arne Petersen | e202bae | 2012-09-09 23:08:44 +0200 | [diff] [blame] | 290 | static void text_entry_delete_text(struct text_entry *entry, |
| 291 | uint32_t index, uint32_t length); |
Jan Arne Petersen | e386dd2 | 2012-09-17 15:28:09 +0200 | [diff] [blame] | 292 | static void text_entry_delete_selected_text(struct text_entry *entry); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 293 | |
| 294 | static void |
| 295 | text_model_commit_string(void *data, |
| 296 | struct text_model *text_model, |
| 297 | const char *text, |
| 298 | uint32_t index) |
| 299 | { |
| 300 | struct text_entry *entry = data; |
| 301 | |
John Kåre Alsaker | 011a1ce | 2012-10-12 12:25:06 +0200 | [diff] [blame] | 302 | if (index > strlen(text)) |
Jan Arne Petersen | 09e7c96 | 2012-09-09 23:08:37 +0200 | [diff] [blame] | 303 | fprintf(stderr, "Invalid cursor index %d\n", index); |
Jan Arne Petersen | 09e7c96 | 2012-09-09 23:08:37 +0200 | [diff] [blame] | 304 | |
Jan Arne Petersen | e386dd2 | 2012-09-17 15:28:09 +0200 | [diff] [blame] | 305 | text_entry_delete_selected_text(entry); |
Jan Arne Petersen | 09e7c96 | 2012-09-09 23:08:37 +0200 | [diff] [blame] | 306 | text_entry_insert_at_cursor(entry, text); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 307 | |
| 308 | widget_schedule_redraw(entry->widget); |
| 309 | } |
| 310 | |
Jan Arne Petersen | 72f6082 | 2012-08-10 16:47:19 +0200 | [diff] [blame] | 311 | static void |
| 312 | text_model_preedit_string(void *data, |
| 313 | struct text_model *text_model, |
| 314 | const char *text, |
| 315 | uint32_t index) |
| 316 | { |
Jan Arne Petersen | 43f4aa8 | 2012-09-09 23:08:43 +0200 | [diff] [blame] | 317 | struct text_entry *entry = data; |
| 318 | |
| 319 | if (index > strlen(text)) { |
| 320 | fprintf(stderr, "Invalid cursor index %d\n", index); |
| 321 | index = strlen(text); |
| 322 | } |
| 323 | |
Jan Arne Petersen | e386dd2 | 2012-09-17 15:28:09 +0200 | [diff] [blame] | 324 | text_entry_delete_selected_text(entry); |
Jan Arne Petersen | 43f4aa8 | 2012-09-09 23:08:43 +0200 | [diff] [blame] | 325 | text_entry_set_preedit(entry, text, index); |
| 326 | |
| 327 | widget_schedule_redraw(entry->widget); |
Jan Arne Petersen | 72f6082 | 2012-08-10 16:47:19 +0200 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | static void |
Jan Arne Petersen | e202bae | 2012-09-09 23:08:44 +0200 | [diff] [blame] | 331 | text_model_delete_surrounding_text(void *data, |
| 332 | struct text_model *text_model, |
| 333 | int32_t index, |
| 334 | uint32_t length) |
| 335 | { |
| 336 | struct text_entry *entry = data; |
| 337 | uint32_t cursor_index = index + entry->cursor; |
Jan Arne Petersen | 6345faa | 2012-11-05 03:26:39 +0100 | [diff] [blame] | 338 | const char *start, *end; |
Jan Arne Petersen | e202bae | 2012-09-09 23:08:44 +0200 | [diff] [blame] | 339 | |
| 340 | if (cursor_index > strlen(entry->text)) { |
| 341 | fprintf(stderr, "Invalid cursor index %d\n", index); |
| 342 | return; |
| 343 | } |
| 344 | |
| 345 | if (cursor_index + length > strlen(entry->text)) { |
| 346 | fprintf(stderr, "Invalid length %d\n", length); |
| 347 | return; |
| 348 | } |
| 349 | |
| 350 | if (length == 0) |
| 351 | return; |
| 352 | |
Jan Arne Petersen | 6345faa | 2012-11-05 03:26:39 +0100 | [diff] [blame] | 353 | start = utf8_start_char(entry->text, entry->text + cursor_index); |
| 354 | end = utf8_end_char(entry->text + cursor_index + length); |
| 355 | |
| 356 | text_entry_delete_text(entry, |
| 357 | start - entry->text, |
| 358 | end - start); |
Jan Arne Petersen | e202bae | 2012-09-09 23:08:44 +0200 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | static void |
Jan Arne Petersen | 72f6082 | 2012-08-10 16:47:19 +0200 | [diff] [blame] | 362 | text_model_preedit_styling(void *data, |
| 363 | struct text_model *text_model) |
| 364 | { |
| 365 | } |
| 366 | |
| 367 | static void |
Jan Arne Petersen | d9be93b | 2012-11-18 19:06:43 +0100 | [diff] [blame] | 368 | text_model_modifiers_map(void *data, |
| 369 | struct text_model *text_model, |
| 370 | struct wl_array *map) |
| 371 | { |
Jan Arne Petersen | cd99706 | 2012-11-18 19:06:44 +0100 | [diff] [blame] | 372 | struct text_entry *entry = data; |
| 373 | |
| 374 | entry->keysym.shift_mask = keysym_modifiers_get_mask(map, "Shift"); |
Jan Arne Petersen | d9be93b | 2012-11-18 19:06:43 +0100 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | static void |
| 378 | text_model_keysym(void *data, |
| 379 | struct text_model *text_model, |
| 380 | uint32_t serial, |
| 381 | uint32_t time, |
| 382 | uint32_t key, |
| 383 | uint32_t state, |
| 384 | uint32_t modifiers) |
Jan Arne Petersen | 72f6082 | 2012-08-10 16:47:19 +0200 | [diff] [blame] | 385 | { |
Jan Arne Petersen | 8aba11d | 2012-09-17 15:28:07 +0200 | [diff] [blame] | 386 | struct text_entry *entry = data; |
Jan Arne Petersen | cd99706 | 2012-11-18 19:06:44 +0100 | [diff] [blame] | 387 | const char *state_label = "release"; |
| 388 | const char *key_label = "Unknown"; |
Jan Arne Petersen | 6345faa | 2012-11-05 03:26:39 +0100 | [diff] [blame] | 389 | const char *new_char; |
Jan Arne Petersen | ce8a443 | 2012-09-09 23:08:45 +0200 | [diff] [blame] | 390 | |
| 391 | if (state == WL_KEYBOARD_KEY_STATE_PRESSED) { |
| 392 | state_label = "pressed"; |
Jan Arne Petersen | ce8a443 | 2012-09-09 23:08:45 +0200 | [diff] [blame] | 393 | } |
| 394 | |
Jan Arne Petersen | cd99706 | 2012-11-18 19:06:44 +0100 | [diff] [blame] | 395 | if (key == XKB_KEY_Left || |
| 396 | key == XKB_KEY_Right) { |
| 397 | if (state != WL_KEYBOARD_KEY_STATE_RELEASED) |
| 398 | return; |
| 399 | |
| 400 | if (key == XKB_KEY_Left) |
| 401 | new_char = utf8_prev_char(entry->text, entry->text + entry->cursor); |
| 402 | else |
| 403 | new_char = utf8_next_char(entry->text + entry->cursor); |
| 404 | |
| 405 | if (new_char != NULL) { |
| 406 | entry->cursor = new_char - entry->text; |
| 407 | if (!(modifiers & entry->keysym.shift_mask)) |
| 408 | entry->anchor = entry->cursor; |
| 409 | widget_schedule_redraw(entry->widget); |
| 410 | } |
| 411 | |
| 412 | return; |
| 413 | } |
| 414 | |
Jan Arne Petersen | ce8a443 | 2012-09-09 23:08:45 +0200 | [diff] [blame] | 415 | switch (key) { |
| 416 | case XKB_KEY_Tab: |
| 417 | key_label = "Tab"; |
| 418 | break; |
| 419 | case XKB_KEY_KP_Enter: |
Jan Arne Petersen | cd99706 | 2012-11-18 19:06:44 +0100 | [diff] [blame] | 420 | case XKB_KEY_Return: |
Jan Arne Petersen | ce8a443 | 2012-09-09 23:08:45 +0200 | [diff] [blame] | 421 | key_label = "Enter"; |
| 422 | break; |
Jan Arne Petersen | ce8a443 | 2012-09-09 23:08:45 +0200 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | fprintf(stderr, "%s key was %s.\n", key_label, state_label); |
Jan Arne Petersen | 72f6082 | 2012-08-10 16:47:19 +0200 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | static void |
| 429 | text_model_selection_replacement(void *data, |
| 430 | struct text_model *text_model) |
| 431 | { |
| 432 | } |
| 433 | |
| 434 | static void |
| 435 | text_model_direction(void *data, |
| 436 | struct text_model *text_model) |
| 437 | { |
| 438 | } |
| 439 | |
| 440 | static void |
| 441 | text_model_locale(void *data, |
| 442 | struct text_model *text_model) |
| 443 | { |
| 444 | } |
| 445 | |
Jan Arne Petersen | de3b6a1 | 2012-08-10 16:47:21 +0200 | [diff] [blame] | 446 | static void |
Jan Arne Petersen | 680275f | 2012-09-24 14:51:14 +0200 | [diff] [blame] | 447 | text_model_enter(void *data, |
| 448 | struct text_model *text_model, |
| 449 | struct wl_surface *surface) |
Jan Arne Petersen | de3b6a1 | 2012-08-10 16:47:21 +0200 | [diff] [blame] | 450 | { |
| 451 | struct text_entry *entry = data; |
| 452 | |
Jan Arne Petersen | 680275f | 2012-09-24 14:51:14 +0200 | [diff] [blame] | 453 | if (surface != window_get_wl_surface(entry->window)) |
| 454 | return; |
| 455 | |
Jan Arne Petersen | de3b6a1 | 2012-08-10 16:47:21 +0200 | [diff] [blame] | 456 | entry->active = 1; |
| 457 | |
| 458 | widget_schedule_redraw(entry->widget); |
| 459 | } |
| 460 | |
| 461 | static void |
Jan Arne Petersen | 680275f | 2012-09-24 14:51:14 +0200 | [diff] [blame] | 462 | text_model_leave(void *data, |
| 463 | struct text_model *text_model) |
Jan Arne Petersen | de3b6a1 | 2012-08-10 16:47:21 +0200 | [diff] [blame] | 464 | { |
| 465 | struct text_entry *entry = data; |
| 466 | |
| 467 | entry->active = 0; |
| 468 | |
| 469 | widget_schedule_redraw(entry->widget); |
| 470 | } |
| 471 | |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 472 | static const struct text_model_listener text_model_listener = { |
Jan Arne Petersen | 72f6082 | 2012-08-10 16:47:19 +0200 | [diff] [blame] | 473 | text_model_commit_string, |
| 474 | text_model_preedit_string, |
Jan Arne Petersen | e202bae | 2012-09-09 23:08:44 +0200 | [diff] [blame] | 475 | text_model_delete_surrounding_text, |
Jan Arne Petersen | 72f6082 | 2012-08-10 16:47:19 +0200 | [diff] [blame] | 476 | text_model_preedit_styling, |
Jan Arne Petersen | d9be93b | 2012-11-18 19:06:43 +0100 | [diff] [blame] | 477 | text_model_modifiers_map, |
| 478 | text_model_keysym, |
Jan Arne Petersen | 72f6082 | 2012-08-10 16:47:19 +0200 | [diff] [blame] | 479 | text_model_selection_replacement, |
| 480 | text_model_direction, |
Jan Arne Petersen | de3b6a1 | 2012-08-10 16:47:21 +0200 | [diff] [blame] | 481 | text_model_locale, |
Jan Arne Petersen | 680275f | 2012-09-24 14:51:14 +0200 | [diff] [blame] | 482 | text_model_enter, |
| 483 | text_model_leave |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 484 | }; |
| 485 | |
| 486 | static struct text_entry* |
| 487 | text_entry_create(struct editor *editor, const char *text) |
| 488 | { |
| 489 | struct text_entry *entry; |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 490 | |
Jan Arne Petersen | cd99706 | 2012-11-18 19:06:44 +0100 | [diff] [blame] | 491 | entry = calloc(1, sizeof *entry); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 492 | |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 493 | entry->widget = widget_add_widget(editor->widget, entry); |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 494 | entry->window = editor->window; |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 495 | entry->text = strdup(text); |
| 496 | entry->active = 0; |
Jan Arne Petersen | 7e634a0 | 2012-09-09 23:08:36 +0200 | [diff] [blame] | 497 | entry->cursor = strlen(text); |
Jan Arne Petersen | 0e5bd45 | 2012-09-09 23:08:38 +0200 | [diff] [blame] | 498 | entry->anchor = entry->cursor; |
Jan Arne Petersen | c1fbcb7 | 2012-09-09 23:08:39 +0200 | [diff] [blame] | 499 | entry->preedit_text = NULL; |
| 500 | entry->preedit_cursor = 0; |
Jan Arne Petersen | 4c26518 | 2012-09-09 23:08:30 +0200 | [diff] [blame] | 501 | 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] | 502 | text_model_add_listener(entry->model, &text_model_listener, entry); |
| 503 | |
Jan Arne Petersen | b9eb02c | 2012-09-09 23:08:35 +0200 | [diff] [blame] | 504 | entry->layout = text_layout_create(); |
| 505 | text_layout_set_text(entry->layout, entry->text); |
| 506 | |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 507 | widget_set_redraw_handler(entry->widget, text_entry_redraw_handler); |
| 508 | widget_set_button_handler(entry->widget, text_entry_button_handler); |
| 509 | |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 510 | return entry; |
| 511 | } |
| 512 | |
| 513 | static void |
| 514 | text_entry_destroy(struct text_entry *entry) |
| 515 | { |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 516 | widget_destroy(entry->widget); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 517 | text_model_destroy(entry->model); |
Jan Arne Petersen | b9eb02c | 2012-09-09 23:08:35 +0200 | [diff] [blame] | 518 | text_layout_destroy(entry->layout); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 519 | free(entry->text); |
| 520 | free(entry); |
| 521 | } |
| 522 | |
| 523 | static void |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 524 | redraw_handler(struct widget *widget, void *data) |
| 525 | { |
| 526 | struct editor *editor = data; |
| 527 | cairo_surface_t *surface; |
| 528 | struct rectangle allocation; |
| 529 | cairo_t *cr; |
| 530 | |
| 531 | surface = window_get_surface(editor->window); |
| 532 | widget_get_allocation(editor->widget, &allocation); |
| 533 | |
| 534 | cr = cairo_create(surface); |
| 535 | cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height); |
| 536 | cairo_clip(cr); |
| 537 | |
| 538 | cairo_translate(cr, allocation.x, allocation.y); |
| 539 | |
| 540 | /* Draw background */ |
| 541 | cairo_push_group(cr); |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 542 | cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 543 | cairo_set_source_rgba(cr, 1, 1, 1, 1); |
| 544 | cairo_rectangle(cr, 0, 0, allocation.width, allocation.height); |
| 545 | cairo_fill(cr); |
| 546 | |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 547 | cairo_pop_group_to_source(cr); |
| 548 | cairo_paint(cr); |
| 549 | |
| 550 | cairo_destroy(cr); |
| 551 | cairo_surface_destroy(surface); |
| 552 | } |
| 553 | |
| 554 | static void |
| 555 | text_entry_allocate(struct text_entry *entry, int32_t x, int32_t y, |
| 556 | int32_t width, int32_t height) |
| 557 | { |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 558 | widget_set_allocation(entry->widget, x, y, width, height); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | static void |
| 562 | resize_handler(struct widget *widget, |
| 563 | int32_t width, int32_t height, void *data) |
| 564 | { |
| 565 | struct editor *editor = data; |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 566 | struct rectangle allocation; |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 567 | |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 568 | widget_get_allocation(editor->widget, &allocation); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 569 | |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 570 | text_entry_allocate(editor->entry, |
| 571 | allocation.x + 20, allocation.y + 20, |
| 572 | width - 40, height / 2 - 40); |
| 573 | text_entry_allocate(editor->editor, |
| 574 | allocation.x + 20, allocation.y + height / 2 + 20, |
| 575 | width - 40, height / 2 - 40); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | static void |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 579 | text_entry_activate(struct text_entry *entry, |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 580 | struct wl_seat *seat) |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 581 | { |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 582 | struct wl_surface *surface = window_get_wl_surface(entry->window); |
| 583 | |
| 584 | text_model_activate(entry->model, |
| 585 | seat, |
| 586 | surface); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | static void |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 590 | text_entry_deactivate(struct text_entry *entry, |
| 591 | struct wl_seat *seat) |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 592 | { |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 593 | text_model_deactivate(entry->model, |
| 594 | seat); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | static void |
Jan Arne Petersen | c1fbcb7 | 2012-09-09 23:08:39 +0200 | [diff] [blame] | 598 | text_entry_update_layout(struct text_entry *entry) |
| 599 | { |
| 600 | char *text; |
| 601 | |
Philipp Brüschweiler | 237358b | 2012-10-02 11:06:51 +0200 | [diff] [blame] | 602 | assert(((unsigned int)entry->cursor) <= strlen(entry->text) + |
| 603 | (entry->preedit_text ? strlen(entry->preedit_text) : 0)); |
Jan Arne Petersen | c1fbcb7 | 2012-09-09 23:08:39 +0200 | [diff] [blame] | 604 | |
| 605 | if (!entry->preedit_text) { |
| 606 | text_layout_set_text(entry->layout, entry->text); |
| 607 | return; |
| 608 | } |
| 609 | |
| 610 | text = malloc(strlen(entry->text) + strlen(entry->preedit_text) + 1); |
| 611 | strncpy(text, entry->text, entry->cursor); |
| 612 | strcpy(text + entry->cursor, entry->preedit_text); |
| 613 | strcpy(text + entry->cursor + strlen(entry->preedit_text), |
| 614 | entry->text + entry->cursor); |
| 615 | |
| 616 | text_layout_set_text(entry->layout, text); |
| 617 | free(text); |
| 618 | |
| 619 | widget_schedule_redraw(entry->widget); |
Jan Arne Petersen | cb08f4d | 2012-09-09 23:08:40 +0200 | [diff] [blame] | 620 | |
| 621 | text_model_set_surrounding_text(entry->model, |
| 622 | entry->text, |
| 623 | entry->cursor, |
| 624 | entry->anchor); |
Jan Arne Petersen | c1fbcb7 | 2012-09-09 23:08:39 +0200 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | static void |
Jan Arne Petersen | 09e7c96 | 2012-09-09 23:08:37 +0200 | [diff] [blame] | 628 | text_entry_insert_at_cursor(struct text_entry *entry, const char *text) |
| 629 | { |
| 630 | char *new_text = malloc(strlen(entry->text) + strlen(text) + 1); |
| 631 | |
| 632 | strncpy(new_text, entry->text, entry->cursor); |
| 633 | strcpy(new_text + entry->cursor, text); |
| 634 | strcpy(new_text + entry->cursor + strlen(text), |
| 635 | entry->text + entry->cursor); |
| 636 | |
| 637 | free(entry->text); |
| 638 | entry->text = new_text; |
| 639 | entry->cursor += strlen(text); |
Jan Arne Petersen | 0e5bd45 | 2012-09-09 23:08:38 +0200 | [diff] [blame] | 640 | entry->anchor += strlen(text); |
Jan Arne Petersen | 09e7c96 | 2012-09-09 23:08:37 +0200 | [diff] [blame] | 641 | |
Jan Arne Petersen | c1fbcb7 | 2012-09-09 23:08:39 +0200 | [diff] [blame] | 642 | text_entry_update_layout(entry); |
| 643 | } |
| 644 | |
| 645 | static void |
| 646 | text_entry_set_preedit(struct text_entry *entry, |
| 647 | const char *preedit_text, |
| 648 | int preedit_cursor) |
| 649 | { |
| 650 | if (entry->preedit_text) { |
| 651 | free(entry->preedit_text); |
| 652 | entry->preedit_text = NULL; |
| 653 | entry->preedit_cursor = 0; |
| 654 | } |
| 655 | |
| 656 | if (!preedit_text) |
| 657 | return; |
| 658 | |
| 659 | entry->preedit_text = strdup(preedit_text); |
| 660 | entry->preedit_cursor = preedit_cursor; |
| 661 | |
| 662 | text_entry_update_layout(entry); |
Jan Arne Petersen | 09e7c96 | 2012-09-09 23:08:37 +0200 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | static void |
Jan Arne Petersen | 7e634a0 | 2012-09-09 23:08:36 +0200 | [diff] [blame] | 666 | text_entry_set_cursor_position(struct text_entry *entry, |
| 667 | int32_t x, int32_t y) |
| 668 | { |
| 669 | entry->cursor = text_layout_xy_to_index(entry->layout, x, y); |
| 670 | |
Jan Arne Petersen | c1e481e | 2012-09-09 23:08:46 +0200 | [diff] [blame] | 671 | text_model_reset(entry->model); |
| 672 | |
Jan Arne Petersen | c1fbcb7 | 2012-09-09 23:08:39 +0200 | [diff] [blame] | 673 | if (entry->cursor >= entry->preedit_cursor) { |
| 674 | entry->cursor -= entry->preedit_cursor; |
| 675 | } |
| 676 | |
| 677 | text_entry_update_layout(entry); |
| 678 | |
Jan Arne Petersen | 7e634a0 | 2012-09-09 23:08:36 +0200 | [diff] [blame] | 679 | widget_schedule_redraw(entry->widget); |
| 680 | } |
| 681 | |
| 682 | static void |
Jan Arne Petersen | 0e5bd45 | 2012-09-09 23:08:38 +0200 | [diff] [blame] | 683 | text_entry_set_anchor_position(struct text_entry *entry, |
| 684 | int32_t x, int32_t y) |
| 685 | { |
| 686 | entry->anchor = text_layout_xy_to_index(entry->layout, x, y); |
| 687 | |
| 688 | widget_schedule_redraw(entry->widget); |
| 689 | } |
| 690 | |
| 691 | static void |
Jan Arne Petersen | e202bae | 2012-09-09 23:08:44 +0200 | [diff] [blame] | 692 | text_entry_delete_text(struct text_entry *entry, |
| 693 | uint32_t index, uint32_t length) |
| 694 | { |
| 695 | if (entry->cursor > index) |
| 696 | entry->cursor -= length; |
| 697 | |
Jan Arne Petersen | 80ad1a9 | 2012-09-17 15:28:10 +0200 | [diff] [blame] | 698 | entry->anchor = entry->cursor; |
| 699 | |
Jan Arne Petersen | e202bae | 2012-09-09 23:08:44 +0200 | [diff] [blame] | 700 | entry->text[index] = '\0'; |
| 701 | strcat(entry->text, entry->text + index + length); |
| 702 | |
| 703 | text_entry_update_layout(entry); |
| 704 | |
| 705 | widget_schedule_redraw(entry->widget); |
| 706 | } |
| 707 | |
| 708 | static void |
Jan Arne Petersen | e386dd2 | 2012-09-17 15:28:09 +0200 | [diff] [blame] | 709 | text_entry_delete_selected_text(struct text_entry *entry) |
| 710 | { |
| 711 | uint32_t start_index = entry->anchor < entry->cursor ? entry->anchor : entry->cursor; |
| 712 | uint32_t end_index = entry->anchor < entry->cursor ? entry->cursor : entry->anchor; |
| 713 | |
| 714 | if (entry->anchor == entry->cursor) |
| 715 | return; |
| 716 | |
| 717 | text_entry_delete_text(entry, start_index, end_index - start_index); |
| 718 | |
| 719 | entry->anchor = entry->cursor; |
| 720 | } |
| 721 | |
| 722 | static void |
Jan Arne Petersen | 0e5bd45 | 2012-09-09 23:08:38 +0200 | [diff] [blame] | 723 | text_entry_draw_selection(struct text_entry *entry, cairo_t *cr) |
| 724 | { |
| 725 | cairo_text_extents_t extents; |
| 726 | uint32_t start_index = entry->anchor < entry->cursor ? entry->anchor : entry->cursor; |
| 727 | uint32_t end_index = entry->anchor < entry->cursor ? entry->cursor : entry->anchor; |
| 728 | cairo_rectangle_t start; |
| 729 | cairo_rectangle_t end; |
| 730 | |
| 731 | if (entry->anchor == entry->cursor) |
| 732 | return; |
| 733 | |
| 734 | text_layout_extents(entry->layout, &extents); |
| 735 | |
| 736 | text_layout_index_to_pos(entry->layout, start_index, &start); |
| 737 | text_layout_index_to_pos(entry->layout, end_index, &end); |
| 738 | |
| 739 | cairo_save (cr); |
| 740 | |
Philipp Brüschweiler | b8911dc | 2012-10-02 11:06:52 +0200 | [diff] [blame] | 741 | 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] | 742 | cairo_rectangle(cr, |
| 743 | start.x, extents.y_bearing + extents.height + 2, |
| 744 | end.x - start.x, -extents.height - 4); |
| 745 | cairo_fill(cr); |
| 746 | |
| 747 | cairo_rectangle(cr, |
| 748 | start.x, extents.y_bearing + extents.height, |
| 749 | end.x - start.x, -extents.height); |
| 750 | cairo_clip(cr); |
| 751 | cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0); |
| 752 | text_layout_draw(entry->layout, cr); |
| 753 | |
| 754 | cairo_restore (cr); |
| 755 | } |
| 756 | |
| 757 | static void |
Jan Arne Petersen | 7e634a0 | 2012-09-09 23:08:36 +0200 | [diff] [blame] | 758 | text_entry_draw_cursor(struct text_entry *entry, cairo_t *cr) |
| 759 | { |
| 760 | cairo_text_extents_t extents; |
| 761 | cairo_rectangle_t cursor_pos; |
| 762 | |
| 763 | text_layout_extents(entry->layout, &extents); |
Jan Arne Petersen | c1fbcb7 | 2012-09-09 23:08:39 +0200 | [diff] [blame] | 764 | text_layout_get_cursor_pos(entry->layout, |
| 765 | entry->cursor + entry->preedit_cursor, |
| 766 | &cursor_pos); |
Jan Arne Petersen | 7e634a0 | 2012-09-09 23:08:36 +0200 | [diff] [blame] | 767 | |
| 768 | cairo_set_line_width(cr, 1.0); |
| 769 | cairo_move_to(cr, cursor_pos.x, extents.y_bearing + extents.height + 2); |
| 770 | cairo_line_to(cr, cursor_pos.x, extents.y_bearing - 2); |
| 771 | cairo_stroke(cr); |
| 772 | } |
| 773 | |
| 774 | static void |
Jan Arne Petersen | c1fbcb7 | 2012-09-09 23:08:39 +0200 | [diff] [blame] | 775 | text_entry_draw_preedit(struct text_entry *entry, cairo_t *cr) |
| 776 | { |
| 777 | cairo_text_extents_t extents; |
| 778 | cairo_rectangle_t start; |
| 779 | cairo_rectangle_t end; |
| 780 | |
| 781 | if (!entry->preedit_text) |
| 782 | return; |
| 783 | |
| 784 | text_layout_extents(entry->layout, &extents); |
| 785 | |
| 786 | text_layout_index_to_pos(entry->layout, entry->cursor, &start); |
| 787 | text_layout_index_to_pos(entry->layout, |
| 788 | entry->cursor + strlen(entry->preedit_text), |
| 789 | &end); |
| 790 | |
| 791 | cairo_save (cr); |
| 792 | |
| 793 | cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0); |
| 794 | cairo_rectangle(cr, |
| 795 | start.x, 0, |
| 796 | end.x - start.x, 1); |
| 797 | cairo_fill(cr); |
| 798 | |
| 799 | cairo_restore (cr); |
| 800 | } |
| 801 | |
Philipp Brüschweiler | 9f897c7 | 2012-10-02 11:06:53 +0200 | [diff] [blame] | 802 | static const int text_offset_left = 10; |
| 803 | |
Jan Arne Petersen | c1fbcb7 | 2012-09-09 23:08:39 +0200 | [diff] [blame] | 804 | static void |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 805 | text_entry_redraw_handler(struct widget *widget, void *data) |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 806 | { |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 807 | struct text_entry *entry = data; |
| 808 | cairo_surface_t *surface; |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 809 | struct rectangle allocation; |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 810 | cairo_t *cr; |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 811 | |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 812 | surface = window_get_surface(entry->window); |
| 813 | widget_get_allocation(entry->widget, &allocation); |
| 814 | |
| 815 | cr = cairo_create(surface); |
| 816 | cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height); |
| 817 | cairo_clip(cr); |
| 818 | |
| 819 | cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); |
| 820 | |
| 821 | cairo_push_group(cr); |
| 822 | cairo_translate(cr, allocation.x, allocation.y); |
| 823 | |
| 824 | cairo_set_source_rgba(cr, 1, 1, 1, 1); |
| 825 | cairo_rectangle(cr, 0, 0, allocation.width, allocation.height); |
| 826 | cairo_fill(cr); |
| 827 | |
| 828 | cairo_set_operator(cr, CAIRO_OPERATOR_OVER); |
| 829 | |
| 830 | if (entry->active) { |
| 831 | cairo_rectangle(cr, 0, 0, allocation.width, allocation.height); |
| 832 | cairo_set_line_width (cr, 3); |
| 833 | cairo_set_source_rgba(cr, 0, 0, 1, 1.0); |
| 834 | cairo_stroke(cr); |
| 835 | } |
| 836 | |
| 837 | cairo_set_source_rgba(cr, 0, 0, 0, 1); |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 838 | |
Philipp Brüschweiler | 9f897c7 | 2012-10-02 11:06:53 +0200 | [diff] [blame] | 839 | cairo_translate(cr, text_offset_left, allocation.height / 2); |
Jan Arne Petersen | b9eb02c | 2012-09-09 23:08:35 +0200 | [diff] [blame] | 840 | text_layout_draw(entry->layout, cr); |
Jan Arne Petersen | 7e634a0 | 2012-09-09 23:08:36 +0200 | [diff] [blame] | 841 | |
Jan Arne Petersen | 0e5bd45 | 2012-09-09 23:08:38 +0200 | [diff] [blame] | 842 | text_entry_draw_selection(entry, cr); |
| 843 | |
Jan Arne Petersen | 7e634a0 | 2012-09-09 23:08:36 +0200 | [diff] [blame] | 844 | text_entry_draw_cursor(entry, cr); |
| 845 | |
Jan Arne Petersen | c1fbcb7 | 2012-09-09 23:08:39 +0200 | [diff] [blame] | 846 | text_entry_draw_preedit(entry, cr); |
| 847 | |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 848 | cairo_pop_group_to_source(cr); |
| 849 | cairo_paint(cr); |
| 850 | |
| 851 | cairo_destroy(cr); |
| 852 | cairo_surface_destroy(surface); |
| 853 | } |
| 854 | |
Jan Arne Petersen | 0e5bd45 | 2012-09-09 23:08:38 +0200 | [diff] [blame] | 855 | static int |
| 856 | text_entry_motion_handler(struct widget *widget, |
| 857 | struct input *input, uint32_t time, |
| 858 | float x, float y, void *data) |
| 859 | { |
| 860 | struct text_entry *entry = data; |
| 861 | struct rectangle allocation; |
| 862 | |
| 863 | widget_get_allocation(entry->widget, &allocation); |
| 864 | |
| 865 | text_entry_set_cursor_position(entry, |
Philipp Brüschweiler | 9f897c7 | 2012-10-02 11:06:53 +0200 | [diff] [blame] | 866 | x - allocation.x - text_offset_left, |
| 867 | y - allocation.y - text_offset_left); |
Jan Arne Petersen | 0e5bd45 | 2012-09-09 23:08:38 +0200 | [diff] [blame] | 868 | |
| 869 | return CURSOR_IBEAM; |
| 870 | } |
| 871 | |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 872 | static void |
| 873 | text_entry_button_handler(struct widget *widget, |
| 874 | struct input *input, uint32_t time, |
| 875 | uint32_t button, |
| 876 | enum wl_pointer_button_state state, void *data) |
| 877 | { |
| 878 | struct text_entry *entry = data; |
Jan Arne Petersen | 7e634a0 | 2012-09-09 23:08:36 +0200 | [diff] [blame] | 879 | struct rectangle allocation; |
Rob Bradford | 9d1d32b | 2012-11-18 19:06:49 +0100 | [diff] [blame^] | 880 | struct editor *editor; |
Jan Arne Petersen | 7e634a0 | 2012-09-09 23:08:36 +0200 | [diff] [blame] | 881 | int32_t x, y; |
| 882 | |
| 883 | widget_get_allocation(entry->widget, &allocation); |
| 884 | input_get_position(input, &x, &y); |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 885 | |
Rob Bradford | 9d1d32b | 2012-11-18 19:06:49 +0100 | [diff] [blame^] | 886 | editor = window_get_user_data(entry->window); |
| 887 | |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 888 | if (button != BTN_LEFT) { |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 889 | return; |
| 890 | } |
| 891 | |
Jan Arne Petersen | 0e5bd45 | 2012-09-09 23:08:38 +0200 | [diff] [blame] | 892 | text_entry_set_cursor_position(entry, |
Philipp Brüschweiler | 9f897c7 | 2012-10-02 11:06:53 +0200 | [diff] [blame] | 893 | x - allocation.x - text_offset_left, |
| 894 | y - allocation.y - text_offset_left); |
Jan Arne Petersen | 7e634a0 | 2012-09-09 23:08:36 +0200 | [diff] [blame] | 895 | |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 896 | if (state == WL_POINTER_BUTTON_STATE_PRESSED) { |
| 897 | struct wl_seat *seat = input_get_seat(input); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 898 | |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 899 | text_entry_activate(entry, seat); |
Rob Bradford | 9d1d32b | 2012-11-18 19:06:49 +0100 | [diff] [blame^] | 900 | editor->active_entry = entry; |
Jan Arne Petersen | 0e5bd45 | 2012-09-09 23:08:38 +0200 | [diff] [blame] | 901 | |
| 902 | text_entry_set_anchor_position(entry, |
Philipp Brüschweiler | 9f897c7 | 2012-10-02 11:06:53 +0200 | [diff] [blame] | 903 | x - allocation.x - text_offset_left, |
| 904 | y - allocation.y - text_offset_left); |
Jan Arne Petersen | 0e5bd45 | 2012-09-09 23:08:38 +0200 | [diff] [blame] | 905 | |
| 906 | widget_set_motion_handler(entry->widget, text_entry_motion_handler); |
| 907 | } else { |
| 908 | widget_set_motion_handler(entry->widget, NULL); |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 909 | } |
| 910 | } |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 911 | |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 912 | static void |
| 913 | editor_button_handler(struct widget *widget, |
| 914 | struct input *input, uint32_t time, |
| 915 | uint32_t button, |
| 916 | enum wl_pointer_button_state state, void *data) |
| 917 | { |
| 918 | struct editor *editor = data; |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 919 | |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 920 | if (button != BTN_LEFT) { |
| 921 | return; |
| 922 | } |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 923 | |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 924 | if (state == WL_POINTER_BUTTON_STATE_PRESSED) { |
| 925 | struct wl_seat *seat = input_get_seat(input); |
| 926 | |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 927 | text_entry_deactivate(editor->entry, seat); |
| 928 | text_entry_deactivate(editor->editor, seat); |
Rob Bradford | 9d1d32b | 2012-11-18 19:06:49 +0100 | [diff] [blame^] | 929 | editor->active_entry = NULL; |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 930 | } |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 931 | } |
| 932 | |
| 933 | static void |
Rob Bradford | 9d1d32b | 2012-11-18 19:06:49 +0100 | [diff] [blame^] | 934 | key_handler(struct window *window, |
| 935 | struct input *input, uint32_t time, |
| 936 | uint32_t key, uint32_t sym, enum wl_keyboard_key_state state, |
| 937 | void *data) |
| 938 | { |
| 939 | struct editor *editor = data; |
| 940 | struct text_entry *entry; |
| 941 | const char *start, *end, *new_char; |
| 942 | char text[16]; |
| 943 | |
| 944 | if (!editor->active_entry) |
| 945 | return; |
| 946 | |
| 947 | entry = editor->active_entry; |
| 948 | |
| 949 | if (state != WL_KEYBOARD_KEY_STATE_PRESSED) |
| 950 | return; |
| 951 | |
| 952 | switch (sym) { |
| 953 | case XKB_KEY_BackSpace: |
| 954 | start = utf8_prev_char(entry->text, entry->text + entry->cursor); |
| 955 | |
| 956 | if (start == NULL) |
| 957 | break; |
| 958 | |
| 959 | end = utf8_end_char(entry->text + entry->cursor); |
| 960 | text_entry_delete_text(entry, |
| 961 | start - entry->text, |
| 962 | end - start); |
| 963 | break; |
| 964 | case XKB_KEY_Delete: |
| 965 | start = utf8_start_char(entry->text, entry->text + entry->cursor); |
| 966 | |
| 967 | if (start == NULL) |
| 968 | break; |
| 969 | |
| 970 | end = utf8_next_char(start); |
| 971 | |
| 972 | if (end == NULL) |
| 973 | break; |
| 974 | |
| 975 | text_entry_delete_text(entry, |
| 976 | start - entry->text, |
| 977 | end - start); |
| 978 | break; |
| 979 | case XKB_KEY_Left: |
| 980 | new_char = utf8_prev_char(entry->text, entry->text + entry->cursor); |
| 981 | if (new_char != NULL) { |
| 982 | entry->cursor = new_char - entry->text; |
| 983 | entry->anchor = entry->cursor; |
| 984 | widget_schedule_redraw(entry->widget); |
| 985 | } |
| 986 | break; |
| 987 | case XKB_KEY_Right: |
| 988 | new_char = utf8_next_char(entry->text + entry->cursor); |
| 989 | if (new_char != NULL) { |
| 990 | entry->cursor = new_char - entry->text; |
| 991 | entry->anchor = entry->cursor; |
| 992 | widget_schedule_redraw(entry->widget); |
| 993 | } |
| 994 | break; |
| 995 | default: |
| 996 | if (xkb_keysym_to_utf8(sym, text, sizeof(text)) <= 0) |
| 997 | break; |
| 998 | |
| 999 | text_entry_insert_at_cursor(entry, text); |
| 1000 | break; |
| 1001 | } |
| 1002 | |
| 1003 | widget_schedule_redraw(entry->widget); |
| 1004 | } |
| 1005 | |
| 1006 | static void |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 1007 | global_handler(struct display *display, uint32_t name, |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 1008 | const char *interface, uint32_t version, void *data) |
| 1009 | { |
| 1010 | struct editor *editor = data; |
| 1011 | |
Jan Arne Petersen | 5196374 | 2012-08-10 16:47:20 +0200 | [diff] [blame] | 1012 | if (!strcmp(interface, "text_model_factory")) { |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 1013 | editor->text_model_factory = |
| 1014 | display_bind(display, name, |
| 1015 | &text_model_factory_interface, 1); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 1016 | } |
| 1017 | } |
| 1018 | |
| 1019 | int |
| 1020 | main(int argc, char *argv[]) |
| 1021 | { |
| 1022 | struct editor editor; |
| 1023 | |
Jan Arne Petersen | 25f6db5 | 2012-11-05 03:26:40 +0100 | [diff] [blame] | 1024 | memset(&editor, 0, sizeof editor); |
| 1025 | |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 1026 | editor.display = display_create(argc, argv); |
| 1027 | if (editor.display == NULL) { |
| 1028 | fprintf(stderr, "failed to create display: %m\n"); |
| 1029 | return -1; |
| 1030 | } |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 1031 | |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 1032 | display_set_user_data(editor.display, &editor); |
| 1033 | display_set_global_handler(editor.display, global_handler); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 1034 | |
| 1035 | editor.window = window_create(editor.display); |
| 1036 | editor.widget = frame_create(editor.window, &editor); |
| 1037 | |
| 1038 | editor.entry = text_entry_create(&editor, "Entry"); |
| 1039 | editor.editor = text_entry_create(&editor, "Editor"); |
| 1040 | |
| 1041 | window_set_title(editor.window, "Text Editor"); |
Rob Bradford | 9d1d32b | 2012-11-18 19:06:49 +0100 | [diff] [blame^] | 1042 | window_set_key_handler(editor.window, key_handler); |
| 1043 | window_set_user_data(editor.window, &editor); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 1044 | |
| 1045 | widget_set_redraw_handler(editor.widget, redraw_handler); |
| 1046 | widget_set_resize_handler(editor.widget, resize_handler); |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 1047 | widget_set_button_handler(editor.widget, editor_button_handler); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 1048 | |
| 1049 | window_schedule_resize(editor.window, 500, 400); |
| 1050 | |
| 1051 | display_run(editor.display); |
| 1052 | |
| 1053 | text_entry_destroy(editor.entry); |
| 1054 | text_entry_destroy(editor.editor); |
| 1055 | |
| 1056 | return 0; |
| 1057 | } |