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