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; |
Philipp Brüschweiler | 70f8367 | 2012-10-02 11:06:54 +0200 | [diff] [blame] | 156 | double d; |
| 157 | |
| 158 | if (layout->num_glyphs == 0) |
| 159 | return 0; |
Jan Arne Petersen | 7e634a0 | 2012-09-09 23:08:36 +0200 | [diff] [blame] | 160 | |
| 161 | cairo_scaled_font_glyph_extents(layout->font, |
| 162 | layout->glyphs, layout->num_glyphs, |
| 163 | &extents); |
| 164 | |
Philipp Brüschweiler | 70f8367 | 2012-10-02 11:06:54 +0200 | [diff] [blame] | 165 | if (x < 0) |
| 166 | return 0; |
| 167 | |
| 168 | for (i = 0; i < layout->num_glyphs - 1; ++i) { |
| 169 | d = layout->glyphs[i + 1].x - layout->glyphs[i].x; |
| 170 | if (x < layout->glyphs[i].x + d/2) |
| 171 | return i; |
Jan Arne Petersen | 7e634a0 | 2012-09-09 23:08:36 +0200 | [diff] [blame] | 172 | } |
| 173 | |
Philipp Brüschweiler | 70f8367 | 2012-10-02 11:06:54 +0200 | [diff] [blame] | 174 | d = extents.width - layout->glyphs[layout->num_glyphs - 1].x; |
| 175 | if (x < layout->glyphs[layout->num_glyphs - 1].x + d/2) |
Jan Arne Petersen | 7e634a0 | 2012-09-09 23:08:36 +0200 | [diff] [blame] | 176 | return layout->num_glyphs - 1; |
| 177 | |
| 178 | return layout->num_glyphs; |
| 179 | } |
| 180 | |
| 181 | static void |
| 182 | text_layout_index_to_pos(struct text_layout *layout, uint32_t index, cairo_rectangle_t *pos) |
| 183 | { |
| 184 | cairo_text_extents_t extents; |
| 185 | |
| 186 | if (!pos) |
| 187 | return; |
| 188 | |
| 189 | cairo_scaled_font_glyph_extents(layout->font, |
| 190 | layout->glyphs, layout->num_glyphs, |
| 191 | &extents); |
| 192 | |
| 193 | if ((int)index >= layout->num_glyphs) { |
| 194 | pos->x = extents.x_advance; |
| 195 | pos->y = layout->num_glyphs ? layout->glyphs[layout->num_glyphs - 1].y : 0; |
| 196 | pos->width = 1; |
| 197 | pos->height = extents.height; |
| 198 | return; |
| 199 | } |
| 200 | |
| 201 | pos->x = layout->glyphs[index].x; |
| 202 | pos->y = layout->glyphs[index].y; |
| 203 | pos->width = (int)index < layout->num_glyphs - 1 ? layout->glyphs[index + 1].x : extents.x_advance - pos->x; |
| 204 | pos->height = extents.height; |
| 205 | } |
| 206 | |
| 207 | static void |
| 208 | text_layout_get_cursor_pos(struct text_layout *layout, int index, cairo_rectangle_t *pos) |
| 209 | { |
| 210 | text_layout_index_to_pos(layout, index, pos); |
| 211 | pos->width = 1; |
| 212 | } |
Jan Arne Petersen | b9eb02c | 2012-09-09 23:08:35 +0200 | [diff] [blame] | 213 | |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 214 | static void text_entry_redraw_handler(struct widget *widget, void *data); |
| 215 | static void text_entry_button_handler(struct widget *widget, |
| 216 | struct input *input, uint32_t time, |
| 217 | uint32_t button, |
| 218 | enum wl_pointer_button_state state, void *data); |
Jan Arne Petersen | 09e7c96 | 2012-09-09 23:08:37 +0200 | [diff] [blame] | 219 | 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] | 220 | static void text_entry_set_preedit(struct text_entry *entry, |
| 221 | const char *preedit_text, |
| 222 | int preedit_cursor); |
Jan Arne Petersen | e202bae | 2012-09-09 23:08:44 +0200 | [diff] [blame] | 223 | static void text_entry_delete_text(struct text_entry *entry, |
| 224 | uint32_t index, uint32_t length); |
Jan Arne Petersen | e386dd2 | 2012-09-17 15:28:09 +0200 | [diff] [blame] | 225 | static void text_entry_delete_selected_text(struct text_entry *entry); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 226 | |
| 227 | static void |
| 228 | text_model_commit_string(void *data, |
| 229 | struct text_model *text_model, |
| 230 | const char *text, |
| 231 | uint32_t index) |
| 232 | { |
| 233 | struct text_entry *entry = data; |
| 234 | |
John Kåre Alsaker | 011a1ce | 2012-10-12 12:25:06 +0200 | [diff] [blame^] | 235 | if (index > strlen(text)) |
Jan Arne Petersen | 09e7c96 | 2012-09-09 23:08:37 +0200 | [diff] [blame] | 236 | fprintf(stderr, "Invalid cursor index %d\n", index); |
Jan Arne Petersen | 09e7c96 | 2012-09-09 23:08:37 +0200 | [diff] [blame] | 237 | |
Jan Arne Petersen | e386dd2 | 2012-09-17 15:28:09 +0200 | [diff] [blame] | 238 | text_entry_delete_selected_text(entry); |
Jan Arne Petersen | 09e7c96 | 2012-09-09 23:08:37 +0200 | [diff] [blame] | 239 | text_entry_insert_at_cursor(entry, text); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 240 | |
| 241 | widget_schedule_redraw(entry->widget); |
| 242 | } |
| 243 | |
Jan Arne Petersen | 72f6082 | 2012-08-10 16:47:19 +0200 | [diff] [blame] | 244 | static void |
| 245 | text_model_preedit_string(void *data, |
| 246 | struct text_model *text_model, |
| 247 | const char *text, |
| 248 | uint32_t index) |
| 249 | { |
Jan Arne Petersen | 43f4aa8 | 2012-09-09 23:08:43 +0200 | [diff] [blame] | 250 | struct text_entry *entry = data; |
| 251 | |
| 252 | if (index > strlen(text)) { |
| 253 | fprintf(stderr, "Invalid cursor index %d\n", index); |
| 254 | index = strlen(text); |
| 255 | } |
| 256 | |
Jan Arne Petersen | e386dd2 | 2012-09-17 15:28:09 +0200 | [diff] [blame] | 257 | text_entry_delete_selected_text(entry); |
Jan Arne Petersen | 43f4aa8 | 2012-09-09 23:08:43 +0200 | [diff] [blame] | 258 | text_entry_set_preedit(entry, text, index); |
| 259 | |
| 260 | widget_schedule_redraw(entry->widget); |
Jan Arne Petersen | 72f6082 | 2012-08-10 16:47:19 +0200 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | static void |
Jan Arne Petersen | e202bae | 2012-09-09 23:08:44 +0200 | [diff] [blame] | 264 | text_model_delete_surrounding_text(void *data, |
| 265 | struct text_model *text_model, |
| 266 | int32_t index, |
| 267 | uint32_t length) |
| 268 | { |
| 269 | struct text_entry *entry = data; |
| 270 | uint32_t cursor_index = index + entry->cursor; |
| 271 | |
| 272 | if (cursor_index > strlen(entry->text)) { |
| 273 | fprintf(stderr, "Invalid cursor index %d\n", index); |
| 274 | return; |
| 275 | } |
| 276 | |
| 277 | if (cursor_index + length > strlen(entry->text)) { |
| 278 | fprintf(stderr, "Invalid length %d\n", length); |
| 279 | return; |
| 280 | } |
| 281 | |
| 282 | if (length == 0) |
| 283 | return; |
| 284 | |
| 285 | text_entry_delete_text(entry, cursor_index, length); |
| 286 | } |
| 287 | |
| 288 | static void |
Jan Arne Petersen | 72f6082 | 2012-08-10 16:47:19 +0200 | [diff] [blame] | 289 | text_model_preedit_styling(void *data, |
| 290 | struct text_model *text_model) |
| 291 | { |
| 292 | } |
| 293 | |
| 294 | static void |
| 295 | text_model_key(void *data, |
Jan Arne Petersen | ce8a443 | 2012-09-09 23:08:45 +0200 | [diff] [blame] | 296 | struct text_model *text_model, |
| 297 | uint32_t key, |
| 298 | uint32_t state) |
Jan Arne Petersen | 72f6082 | 2012-08-10 16:47:19 +0200 | [diff] [blame] | 299 | { |
Jan Arne Petersen | 8aba11d | 2012-09-17 15:28:07 +0200 | [diff] [blame] | 300 | struct text_entry *entry = data; |
Jan Arne Petersen | ce8a443 | 2012-09-09 23:08:45 +0200 | [diff] [blame] | 301 | const char *state_label; |
Jan Arne Petersen | 8aba11d | 2012-09-17 15:28:07 +0200 | [diff] [blame] | 302 | const char *key_label = "released"; |
Jan Arne Petersen | ce8a443 | 2012-09-09 23:08:45 +0200 | [diff] [blame] | 303 | |
| 304 | if (state == WL_KEYBOARD_KEY_STATE_PRESSED) { |
| 305 | state_label = "pressed"; |
Jan Arne Petersen | ce8a443 | 2012-09-09 23:08:45 +0200 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | switch (key) { |
| 309 | case XKB_KEY_Tab: |
| 310 | key_label = "Tab"; |
| 311 | break; |
| 312 | case XKB_KEY_KP_Enter: |
| 313 | key_label = "Enter"; |
| 314 | break; |
Jan Arne Petersen | 8aba11d | 2012-09-17 15:28:07 +0200 | [diff] [blame] | 315 | case XKB_KEY_Left: |
| 316 | if (entry->cursor > 0) { |
| 317 | entry->cursor--; |
| 318 | entry->anchor = entry->cursor; |
| 319 | widget_schedule_redraw(entry->widget); |
| 320 | } |
| 321 | break; |
| 322 | case XKB_KEY_Right: |
| 323 | if (entry->cursor < strlen(entry->text)) { |
| 324 | entry->cursor++; |
| 325 | entry->anchor = entry->cursor; |
| 326 | widget_schedule_redraw(entry->widget); |
| 327 | } |
| 328 | break; |
Jan Arne Petersen | ce8a443 | 2012-09-09 23:08:45 +0200 | [diff] [blame] | 329 | default: |
| 330 | key_label = "Unknown"; |
| 331 | } |
| 332 | |
| 333 | fprintf(stderr, "%s key was %s.\n", key_label, state_label); |
Jan Arne Petersen | 72f6082 | 2012-08-10 16:47:19 +0200 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | static void |
| 337 | text_model_selection_replacement(void *data, |
| 338 | struct text_model *text_model) |
| 339 | { |
| 340 | } |
| 341 | |
| 342 | static void |
| 343 | text_model_direction(void *data, |
| 344 | struct text_model *text_model) |
| 345 | { |
| 346 | } |
| 347 | |
| 348 | static void |
| 349 | text_model_locale(void *data, |
| 350 | struct text_model *text_model) |
| 351 | { |
| 352 | } |
| 353 | |
Jan Arne Petersen | de3b6a1 | 2012-08-10 16:47:21 +0200 | [diff] [blame] | 354 | static void |
Jan Arne Petersen | 680275f | 2012-09-24 14:51:14 +0200 | [diff] [blame] | 355 | text_model_enter(void *data, |
| 356 | struct text_model *text_model, |
| 357 | struct wl_surface *surface) |
Jan Arne Petersen | de3b6a1 | 2012-08-10 16:47:21 +0200 | [diff] [blame] | 358 | { |
| 359 | struct text_entry *entry = data; |
| 360 | |
Jan Arne Petersen | 680275f | 2012-09-24 14:51:14 +0200 | [diff] [blame] | 361 | if (surface != window_get_wl_surface(entry->window)) |
| 362 | return; |
| 363 | |
Jan Arne Petersen | de3b6a1 | 2012-08-10 16:47:21 +0200 | [diff] [blame] | 364 | entry->active = 1; |
| 365 | |
| 366 | widget_schedule_redraw(entry->widget); |
| 367 | } |
| 368 | |
| 369 | static void |
Jan Arne Petersen | 680275f | 2012-09-24 14:51:14 +0200 | [diff] [blame] | 370 | text_model_leave(void *data, |
| 371 | struct text_model *text_model) |
Jan Arne Petersen | de3b6a1 | 2012-08-10 16:47:21 +0200 | [diff] [blame] | 372 | { |
| 373 | struct text_entry *entry = data; |
| 374 | |
| 375 | entry->active = 0; |
| 376 | |
| 377 | widget_schedule_redraw(entry->widget); |
| 378 | } |
| 379 | |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 380 | static const struct text_model_listener text_model_listener = { |
Jan Arne Petersen | 72f6082 | 2012-08-10 16:47:19 +0200 | [diff] [blame] | 381 | text_model_commit_string, |
| 382 | text_model_preedit_string, |
Jan Arne Petersen | e202bae | 2012-09-09 23:08:44 +0200 | [diff] [blame] | 383 | text_model_delete_surrounding_text, |
Jan Arne Petersen | 72f6082 | 2012-08-10 16:47:19 +0200 | [diff] [blame] | 384 | text_model_preedit_styling, |
| 385 | text_model_key, |
| 386 | text_model_selection_replacement, |
| 387 | text_model_direction, |
Jan Arne Petersen | de3b6a1 | 2012-08-10 16:47:21 +0200 | [diff] [blame] | 388 | text_model_locale, |
Jan Arne Petersen | 680275f | 2012-09-24 14:51:14 +0200 | [diff] [blame] | 389 | text_model_enter, |
| 390 | text_model_leave |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 391 | }; |
| 392 | |
| 393 | static struct text_entry* |
| 394 | text_entry_create(struct editor *editor, const char *text) |
| 395 | { |
| 396 | struct text_entry *entry; |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 397 | |
| 398 | entry = malloc(sizeof *entry); |
| 399 | |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 400 | entry->widget = widget_add_widget(editor->widget, entry); |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 401 | entry->window = editor->window; |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 402 | entry->text = strdup(text); |
| 403 | entry->active = 0; |
Jan Arne Petersen | 7e634a0 | 2012-09-09 23:08:36 +0200 | [diff] [blame] | 404 | entry->cursor = strlen(text); |
Jan Arne Petersen | 0e5bd45 | 2012-09-09 23:08:38 +0200 | [diff] [blame] | 405 | entry->anchor = entry->cursor; |
Jan Arne Petersen | c1fbcb7 | 2012-09-09 23:08:39 +0200 | [diff] [blame] | 406 | entry->preedit_text = NULL; |
| 407 | entry->preedit_cursor = 0; |
Jan Arne Petersen | 4c26518 | 2012-09-09 23:08:30 +0200 | [diff] [blame] | 408 | 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] | 409 | text_model_add_listener(entry->model, &text_model_listener, entry); |
| 410 | |
Jan Arne Petersen | b9eb02c | 2012-09-09 23:08:35 +0200 | [diff] [blame] | 411 | entry->layout = text_layout_create(); |
| 412 | text_layout_set_text(entry->layout, entry->text); |
| 413 | |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 414 | widget_set_redraw_handler(entry->widget, text_entry_redraw_handler); |
| 415 | widget_set_button_handler(entry->widget, text_entry_button_handler); |
| 416 | |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 417 | return entry; |
| 418 | } |
| 419 | |
| 420 | static void |
| 421 | text_entry_destroy(struct text_entry *entry) |
| 422 | { |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 423 | widget_destroy(entry->widget); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 424 | text_model_destroy(entry->model); |
Jan Arne Petersen | b9eb02c | 2012-09-09 23:08:35 +0200 | [diff] [blame] | 425 | text_layout_destroy(entry->layout); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 426 | free(entry->text); |
| 427 | free(entry); |
| 428 | } |
| 429 | |
| 430 | static void |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 431 | redraw_handler(struct widget *widget, void *data) |
| 432 | { |
| 433 | struct editor *editor = data; |
| 434 | cairo_surface_t *surface; |
| 435 | struct rectangle allocation; |
| 436 | cairo_t *cr; |
| 437 | |
| 438 | surface = window_get_surface(editor->window); |
| 439 | widget_get_allocation(editor->widget, &allocation); |
| 440 | |
| 441 | cr = cairo_create(surface); |
| 442 | cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height); |
| 443 | cairo_clip(cr); |
| 444 | |
| 445 | cairo_translate(cr, allocation.x, allocation.y); |
| 446 | |
| 447 | /* Draw background */ |
| 448 | cairo_push_group(cr); |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 449 | cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 450 | cairo_set_source_rgba(cr, 1, 1, 1, 1); |
| 451 | cairo_rectangle(cr, 0, 0, allocation.width, allocation.height); |
| 452 | cairo_fill(cr); |
| 453 | |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 454 | cairo_pop_group_to_source(cr); |
| 455 | cairo_paint(cr); |
| 456 | |
| 457 | cairo_destroy(cr); |
| 458 | cairo_surface_destroy(surface); |
| 459 | } |
| 460 | |
| 461 | static void |
| 462 | text_entry_allocate(struct text_entry *entry, int32_t x, int32_t y, |
| 463 | int32_t width, int32_t height) |
| 464 | { |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 465 | widget_set_allocation(entry->widget, x, y, width, height); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | static void |
| 469 | resize_handler(struct widget *widget, |
| 470 | int32_t width, int32_t height, void *data) |
| 471 | { |
| 472 | struct editor *editor = data; |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 473 | struct rectangle allocation; |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 474 | |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 475 | widget_get_allocation(editor->widget, &allocation); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 476 | |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 477 | text_entry_allocate(editor->entry, |
| 478 | allocation.x + 20, allocation.y + 20, |
| 479 | width - 40, height / 2 - 40); |
| 480 | text_entry_allocate(editor->editor, |
| 481 | allocation.x + 20, allocation.y + height / 2 + 20, |
| 482 | width - 40, height / 2 - 40); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | static void |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 486 | text_entry_activate(struct text_entry *entry, |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 487 | struct wl_seat *seat) |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 488 | { |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 489 | struct wl_surface *surface = window_get_wl_surface(entry->window); |
| 490 | |
| 491 | text_model_activate(entry->model, |
| 492 | seat, |
| 493 | surface); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | static void |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 497 | text_entry_deactivate(struct text_entry *entry, |
| 498 | struct wl_seat *seat) |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 499 | { |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 500 | text_model_deactivate(entry->model, |
| 501 | seat); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | static void |
Jan Arne Petersen | c1fbcb7 | 2012-09-09 23:08:39 +0200 | [diff] [blame] | 505 | text_entry_update_layout(struct text_entry *entry) |
| 506 | { |
| 507 | char *text; |
| 508 | |
Philipp Brüschweiler | 237358b | 2012-10-02 11:06:51 +0200 | [diff] [blame] | 509 | assert(((unsigned int)entry->cursor) <= strlen(entry->text) + |
| 510 | (entry->preedit_text ? strlen(entry->preedit_text) : 0)); |
Jan Arne Petersen | c1fbcb7 | 2012-09-09 23:08:39 +0200 | [diff] [blame] | 511 | |
| 512 | if (!entry->preedit_text) { |
| 513 | text_layout_set_text(entry->layout, entry->text); |
| 514 | return; |
| 515 | } |
| 516 | |
| 517 | text = malloc(strlen(entry->text) + strlen(entry->preedit_text) + 1); |
| 518 | strncpy(text, entry->text, entry->cursor); |
| 519 | strcpy(text + entry->cursor, entry->preedit_text); |
| 520 | strcpy(text + entry->cursor + strlen(entry->preedit_text), |
| 521 | entry->text + entry->cursor); |
| 522 | |
| 523 | text_layout_set_text(entry->layout, text); |
| 524 | free(text); |
| 525 | |
| 526 | widget_schedule_redraw(entry->widget); |
Jan Arne Petersen | cb08f4d | 2012-09-09 23:08:40 +0200 | [diff] [blame] | 527 | |
| 528 | text_model_set_surrounding_text(entry->model, |
| 529 | entry->text, |
| 530 | entry->cursor, |
| 531 | entry->anchor); |
Jan Arne Petersen | c1fbcb7 | 2012-09-09 23:08:39 +0200 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | static void |
Jan Arne Petersen | 09e7c96 | 2012-09-09 23:08:37 +0200 | [diff] [blame] | 535 | text_entry_insert_at_cursor(struct text_entry *entry, const char *text) |
| 536 | { |
| 537 | char *new_text = malloc(strlen(entry->text) + strlen(text) + 1); |
| 538 | |
| 539 | strncpy(new_text, entry->text, entry->cursor); |
| 540 | strcpy(new_text + entry->cursor, text); |
| 541 | strcpy(new_text + entry->cursor + strlen(text), |
| 542 | entry->text + entry->cursor); |
| 543 | |
| 544 | free(entry->text); |
| 545 | entry->text = new_text; |
| 546 | entry->cursor += strlen(text); |
Jan Arne Petersen | 0e5bd45 | 2012-09-09 23:08:38 +0200 | [diff] [blame] | 547 | entry->anchor += strlen(text); |
Jan Arne Petersen | 09e7c96 | 2012-09-09 23:08:37 +0200 | [diff] [blame] | 548 | |
Jan Arne Petersen | c1fbcb7 | 2012-09-09 23:08:39 +0200 | [diff] [blame] | 549 | text_entry_update_layout(entry); |
| 550 | } |
| 551 | |
| 552 | static void |
| 553 | text_entry_set_preedit(struct text_entry *entry, |
| 554 | const char *preedit_text, |
| 555 | int preedit_cursor) |
| 556 | { |
| 557 | if (entry->preedit_text) { |
| 558 | free(entry->preedit_text); |
| 559 | entry->preedit_text = NULL; |
| 560 | entry->preedit_cursor = 0; |
| 561 | } |
| 562 | |
| 563 | if (!preedit_text) |
| 564 | return; |
| 565 | |
| 566 | entry->preedit_text = strdup(preedit_text); |
| 567 | entry->preedit_cursor = preedit_cursor; |
| 568 | |
| 569 | text_entry_update_layout(entry); |
Jan Arne Petersen | 09e7c96 | 2012-09-09 23:08:37 +0200 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | static void |
Jan Arne Petersen | 7e634a0 | 2012-09-09 23:08:36 +0200 | [diff] [blame] | 573 | text_entry_set_cursor_position(struct text_entry *entry, |
| 574 | int32_t x, int32_t y) |
| 575 | { |
| 576 | entry->cursor = text_layout_xy_to_index(entry->layout, x, y); |
| 577 | |
Jan Arne Petersen | c1e481e | 2012-09-09 23:08:46 +0200 | [diff] [blame] | 578 | text_model_reset(entry->model); |
| 579 | |
Jan Arne Petersen | c1fbcb7 | 2012-09-09 23:08:39 +0200 | [diff] [blame] | 580 | if (entry->cursor >= entry->preedit_cursor) { |
| 581 | entry->cursor -= entry->preedit_cursor; |
| 582 | } |
| 583 | |
| 584 | text_entry_update_layout(entry); |
| 585 | |
Jan Arne Petersen | 7e634a0 | 2012-09-09 23:08:36 +0200 | [diff] [blame] | 586 | widget_schedule_redraw(entry->widget); |
| 587 | } |
| 588 | |
| 589 | static void |
Jan Arne Petersen | 0e5bd45 | 2012-09-09 23:08:38 +0200 | [diff] [blame] | 590 | text_entry_set_anchor_position(struct text_entry *entry, |
| 591 | int32_t x, int32_t y) |
| 592 | { |
| 593 | entry->anchor = text_layout_xy_to_index(entry->layout, x, y); |
| 594 | |
| 595 | widget_schedule_redraw(entry->widget); |
| 596 | } |
| 597 | |
| 598 | static void |
Jan Arne Petersen | e202bae | 2012-09-09 23:08:44 +0200 | [diff] [blame] | 599 | text_entry_delete_text(struct text_entry *entry, |
| 600 | uint32_t index, uint32_t length) |
| 601 | { |
| 602 | if (entry->cursor > index) |
| 603 | entry->cursor -= length; |
| 604 | |
Jan Arne Petersen | 80ad1a9 | 2012-09-17 15:28:10 +0200 | [diff] [blame] | 605 | entry->anchor = entry->cursor; |
| 606 | |
Jan Arne Petersen | e202bae | 2012-09-09 23:08:44 +0200 | [diff] [blame] | 607 | entry->text[index] = '\0'; |
| 608 | strcat(entry->text, entry->text + index + length); |
| 609 | |
| 610 | text_entry_update_layout(entry); |
| 611 | |
| 612 | widget_schedule_redraw(entry->widget); |
| 613 | } |
| 614 | |
| 615 | static void |
Jan Arne Petersen | e386dd2 | 2012-09-17 15:28:09 +0200 | [diff] [blame] | 616 | text_entry_delete_selected_text(struct text_entry *entry) |
| 617 | { |
| 618 | uint32_t start_index = entry->anchor < entry->cursor ? entry->anchor : entry->cursor; |
| 619 | uint32_t end_index = entry->anchor < entry->cursor ? entry->cursor : entry->anchor; |
| 620 | |
| 621 | if (entry->anchor == entry->cursor) |
| 622 | return; |
| 623 | |
| 624 | text_entry_delete_text(entry, start_index, end_index - start_index); |
| 625 | |
| 626 | entry->anchor = entry->cursor; |
| 627 | } |
| 628 | |
| 629 | static void |
Jan Arne Petersen | 0e5bd45 | 2012-09-09 23:08:38 +0200 | [diff] [blame] | 630 | text_entry_draw_selection(struct text_entry *entry, cairo_t *cr) |
| 631 | { |
| 632 | cairo_text_extents_t extents; |
| 633 | uint32_t start_index = entry->anchor < entry->cursor ? entry->anchor : entry->cursor; |
| 634 | uint32_t end_index = entry->anchor < entry->cursor ? entry->cursor : entry->anchor; |
| 635 | cairo_rectangle_t start; |
| 636 | cairo_rectangle_t end; |
| 637 | |
| 638 | if (entry->anchor == entry->cursor) |
| 639 | return; |
| 640 | |
| 641 | text_layout_extents(entry->layout, &extents); |
| 642 | |
| 643 | text_layout_index_to_pos(entry->layout, start_index, &start); |
| 644 | text_layout_index_to_pos(entry->layout, end_index, &end); |
| 645 | |
| 646 | cairo_save (cr); |
| 647 | |
Philipp Brüschweiler | b8911dc | 2012-10-02 11:06:52 +0200 | [diff] [blame] | 648 | 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] | 649 | cairo_rectangle(cr, |
| 650 | start.x, extents.y_bearing + extents.height + 2, |
| 651 | end.x - start.x, -extents.height - 4); |
| 652 | cairo_fill(cr); |
| 653 | |
| 654 | cairo_rectangle(cr, |
| 655 | start.x, extents.y_bearing + extents.height, |
| 656 | end.x - start.x, -extents.height); |
| 657 | cairo_clip(cr); |
| 658 | cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0); |
| 659 | text_layout_draw(entry->layout, cr); |
| 660 | |
| 661 | cairo_restore (cr); |
| 662 | } |
| 663 | |
| 664 | static void |
Jan Arne Petersen | 7e634a0 | 2012-09-09 23:08:36 +0200 | [diff] [blame] | 665 | text_entry_draw_cursor(struct text_entry *entry, cairo_t *cr) |
| 666 | { |
| 667 | cairo_text_extents_t extents; |
| 668 | cairo_rectangle_t cursor_pos; |
| 669 | |
| 670 | text_layout_extents(entry->layout, &extents); |
Jan Arne Petersen | c1fbcb7 | 2012-09-09 23:08:39 +0200 | [diff] [blame] | 671 | text_layout_get_cursor_pos(entry->layout, |
| 672 | entry->cursor + entry->preedit_cursor, |
| 673 | &cursor_pos); |
Jan Arne Petersen | 7e634a0 | 2012-09-09 23:08:36 +0200 | [diff] [blame] | 674 | |
| 675 | cairo_set_line_width(cr, 1.0); |
| 676 | cairo_move_to(cr, cursor_pos.x, extents.y_bearing + extents.height + 2); |
| 677 | cairo_line_to(cr, cursor_pos.x, extents.y_bearing - 2); |
| 678 | cairo_stroke(cr); |
| 679 | } |
| 680 | |
| 681 | static void |
Jan Arne Petersen | c1fbcb7 | 2012-09-09 23:08:39 +0200 | [diff] [blame] | 682 | text_entry_draw_preedit(struct text_entry *entry, cairo_t *cr) |
| 683 | { |
| 684 | cairo_text_extents_t extents; |
| 685 | cairo_rectangle_t start; |
| 686 | cairo_rectangle_t end; |
| 687 | |
| 688 | if (!entry->preedit_text) |
| 689 | return; |
| 690 | |
| 691 | text_layout_extents(entry->layout, &extents); |
| 692 | |
| 693 | text_layout_index_to_pos(entry->layout, entry->cursor, &start); |
| 694 | text_layout_index_to_pos(entry->layout, |
| 695 | entry->cursor + strlen(entry->preedit_text), |
| 696 | &end); |
| 697 | |
| 698 | cairo_save (cr); |
| 699 | |
| 700 | cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0); |
| 701 | cairo_rectangle(cr, |
| 702 | start.x, 0, |
| 703 | end.x - start.x, 1); |
| 704 | cairo_fill(cr); |
| 705 | |
| 706 | cairo_restore (cr); |
| 707 | } |
| 708 | |
Philipp Brüschweiler | 9f897c7 | 2012-10-02 11:06:53 +0200 | [diff] [blame] | 709 | static const int text_offset_left = 10; |
| 710 | |
Jan Arne Petersen | c1fbcb7 | 2012-09-09 23:08:39 +0200 | [diff] [blame] | 711 | static void |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 712 | text_entry_redraw_handler(struct widget *widget, void *data) |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 713 | { |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 714 | struct text_entry *entry = data; |
| 715 | cairo_surface_t *surface; |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 716 | struct rectangle allocation; |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 717 | cairo_t *cr; |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 718 | |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 719 | surface = window_get_surface(entry->window); |
| 720 | widget_get_allocation(entry->widget, &allocation); |
| 721 | |
| 722 | cr = cairo_create(surface); |
| 723 | cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height); |
| 724 | cairo_clip(cr); |
| 725 | |
| 726 | cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); |
| 727 | |
| 728 | cairo_push_group(cr); |
| 729 | cairo_translate(cr, allocation.x, allocation.y); |
| 730 | |
| 731 | cairo_set_source_rgba(cr, 1, 1, 1, 1); |
| 732 | cairo_rectangle(cr, 0, 0, allocation.width, allocation.height); |
| 733 | cairo_fill(cr); |
| 734 | |
| 735 | cairo_set_operator(cr, CAIRO_OPERATOR_OVER); |
| 736 | |
| 737 | if (entry->active) { |
| 738 | cairo_rectangle(cr, 0, 0, allocation.width, allocation.height); |
| 739 | cairo_set_line_width (cr, 3); |
| 740 | cairo_set_source_rgba(cr, 0, 0, 1, 1.0); |
| 741 | cairo_stroke(cr); |
| 742 | } |
| 743 | |
| 744 | cairo_set_source_rgba(cr, 0, 0, 0, 1); |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 745 | |
Philipp Brüschweiler | 9f897c7 | 2012-10-02 11:06:53 +0200 | [diff] [blame] | 746 | cairo_translate(cr, text_offset_left, allocation.height / 2); |
Jan Arne Petersen | b9eb02c | 2012-09-09 23:08:35 +0200 | [diff] [blame] | 747 | text_layout_draw(entry->layout, cr); |
Jan Arne Petersen | 7e634a0 | 2012-09-09 23:08:36 +0200 | [diff] [blame] | 748 | |
Jan Arne Petersen | 0e5bd45 | 2012-09-09 23:08:38 +0200 | [diff] [blame] | 749 | text_entry_draw_selection(entry, cr); |
| 750 | |
Jan Arne Petersen | 7e634a0 | 2012-09-09 23:08:36 +0200 | [diff] [blame] | 751 | text_entry_draw_cursor(entry, cr); |
| 752 | |
Jan Arne Petersen | c1fbcb7 | 2012-09-09 23:08:39 +0200 | [diff] [blame] | 753 | text_entry_draw_preedit(entry, cr); |
| 754 | |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 755 | cairo_pop_group_to_source(cr); |
| 756 | cairo_paint(cr); |
| 757 | |
| 758 | cairo_destroy(cr); |
| 759 | cairo_surface_destroy(surface); |
| 760 | } |
| 761 | |
Jan Arne Petersen | 0e5bd45 | 2012-09-09 23:08:38 +0200 | [diff] [blame] | 762 | static int |
| 763 | text_entry_motion_handler(struct widget *widget, |
| 764 | struct input *input, uint32_t time, |
| 765 | float x, float y, void *data) |
| 766 | { |
| 767 | struct text_entry *entry = data; |
| 768 | struct rectangle allocation; |
| 769 | |
| 770 | widget_get_allocation(entry->widget, &allocation); |
| 771 | |
| 772 | text_entry_set_cursor_position(entry, |
Philipp Brüschweiler | 9f897c7 | 2012-10-02 11:06:53 +0200 | [diff] [blame] | 773 | x - allocation.x - text_offset_left, |
| 774 | y - allocation.y - text_offset_left); |
Jan Arne Petersen | 0e5bd45 | 2012-09-09 23:08:38 +0200 | [diff] [blame] | 775 | |
| 776 | return CURSOR_IBEAM; |
| 777 | } |
| 778 | |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 779 | static void |
| 780 | text_entry_button_handler(struct widget *widget, |
| 781 | struct input *input, uint32_t time, |
| 782 | uint32_t button, |
| 783 | enum wl_pointer_button_state state, void *data) |
| 784 | { |
| 785 | struct text_entry *entry = data; |
Jan Arne Petersen | 7e634a0 | 2012-09-09 23:08:36 +0200 | [diff] [blame] | 786 | struct rectangle allocation; |
| 787 | int32_t x, y; |
| 788 | |
| 789 | widget_get_allocation(entry->widget, &allocation); |
| 790 | input_get_position(input, &x, &y); |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 791 | |
| 792 | if (button != BTN_LEFT) { |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 793 | return; |
| 794 | } |
| 795 | |
Jan Arne Petersen | 0e5bd45 | 2012-09-09 23:08:38 +0200 | [diff] [blame] | 796 | text_entry_set_cursor_position(entry, |
Philipp Brüschweiler | 9f897c7 | 2012-10-02 11:06:53 +0200 | [diff] [blame] | 797 | x - allocation.x - text_offset_left, |
| 798 | y - allocation.y - text_offset_left); |
Jan Arne Petersen | 7e634a0 | 2012-09-09 23:08:36 +0200 | [diff] [blame] | 799 | |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 800 | if (state == WL_POINTER_BUTTON_STATE_PRESSED) { |
| 801 | struct wl_seat *seat = input_get_seat(input); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 802 | |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 803 | text_entry_activate(entry, seat); |
Jan Arne Petersen | 0e5bd45 | 2012-09-09 23:08:38 +0200 | [diff] [blame] | 804 | |
| 805 | text_entry_set_anchor_position(entry, |
Philipp Brüschweiler | 9f897c7 | 2012-10-02 11:06:53 +0200 | [diff] [blame] | 806 | x - allocation.x - text_offset_left, |
| 807 | y - allocation.y - text_offset_left); |
Jan Arne Petersen | 0e5bd45 | 2012-09-09 23:08:38 +0200 | [diff] [blame] | 808 | |
| 809 | widget_set_motion_handler(entry->widget, text_entry_motion_handler); |
| 810 | } else { |
| 811 | widget_set_motion_handler(entry->widget, NULL); |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 812 | } |
| 813 | } |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 814 | |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 815 | static void |
| 816 | editor_button_handler(struct widget *widget, |
| 817 | struct input *input, uint32_t time, |
| 818 | uint32_t button, |
| 819 | enum wl_pointer_button_state state, void *data) |
| 820 | { |
| 821 | struct editor *editor = data; |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 822 | |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 823 | if (button != BTN_LEFT) { |
| 824 | return; |
| 825 | } |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 826 | |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 827 | if (state == WL_POINTER_BUTTON_STATE_PRESSED) { |
| 828 | struct wl_seat *seat = input_get_seat(input); |
| 829 | |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 830 | text_entry_deactivate(editor->entry, seat); |
| 831 | text_entry_deactivate(editor->editor, seat); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 832 | } |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | static void |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 836 | global_handler(struct display *display, uint32_t name, |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 837 | const char *interface, uint32_t version, void *data) |
| 838 | { |
| 839 | struct editor *editor = data; |
| 840 | |
Jan Arne Petersen | 5196374 | 2012-08-10 16:47:20 +0200 | [diff] [blame] | 841 | if (!strcmp(interface, "text_model_factory")) { |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 842 | editor->text_model_factory = |
| 843 | display_bind(display, name, |
| 844 | &text_model_factory_interface, 1); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 845 | } |
| 846 | } |
| 847 | |
| 848 | int |
| 849 | main(int argc, char *argv[]) |
| 850 | { |
| 851 | struct editor editor; |
| 852 | |
| 853 | editor.display = display_create(argc, argv); |
| 854 | if (editor.display == NULL) { |
| 855 | fprintf(stderr, "failed to create display: %m\n"); |
| 856 | return -1; |
| 857 | } |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 858 | |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 859 | display_set_user_data(editor.display, &editor); |
| 860 | display_set_global_handler(editor.display, global_handler); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 861 | |
| 862 | editor.window = window_create(editor.display); |
| 863 | editor.widget = frame_create(editor.window, &editor); |
| 864 | |
| 865 | editor.entry = text_entry_create(&editor, "Entry"); |
| 866 | editor.editor = text_entry_create(&editor, "Editor"); |
Jan Arne Petersen | c1fbcb7 | 2012-09-09 23:08:39 +0200 | [diff] [blame] | 867 | text_entry_set_preedit(editor.editor, "preedit", strlen("preedit")); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 868 | |
| 869 | window_set_title(editor.window, "Text Editor"); |
| 870 | |
| 871 | widget_set_redraw_handler(editor.widget, redraw_handler); |
| 872 | widget_set_resize_handler(editor.widget, resize_handler); |
Jan Arne Petersen | f80bc06 | 2012-09-09 23:08:34 +0200 | [diff] [blame] | 873 | widget_set_button_handler(editor.widget, editor_button_handler); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 874 | |
| 875 | window_schedule_resize(editor.window, 500, 400); |
| 876 | |
| 877 | display_run(editor.display); |
| 878 | |
| 879 | text_entry_destroy(editor.entry); |
| 880 | text_entry_destroy(editor.editor); |
| 881 | |
| 882 | return 0; |
| 883 | } |