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