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 | |
| 35 | struct text_entry { |
| 36 | struct widget *widget; |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 37 | struct window *window; |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 38 | char *text; |
| 39 | int active; |
| 40 | struct rectangle allocation; |
| 41 | struct text_model *model; |
| 42 | }; |
| 43 | |
| 44 | struct editor { |
Jan Arne Petersen | 5196374 | 2012-08-10 16:47:20 +0200 | [diff] [blame] | 45 | struct text_model_factory *text_model_factory; |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 46 | struct display *display; |
| 47 | struct window *window; |
| 48 | struct widget *widget; |
| 49 | struct text_entry *entry; |
| 50 | struct text_entry *editor; |
| 51 | }; |
| 52 | |
| 53 | static void |
| 54 | text_entry_append(struct text_entry *entry, const char *text) |
| 55 | { |
| 56 | entry->text = realloc(entry->text, strlen(entry->text) + strlen(text) + 1); |
| 57 | strcat(entry->text, text); |
| 58 | } |
| 59 | |
| 60 | |
| 61 | static void |
| 62 | text_model_commit_string(void *data, |
| 63 | struct text_model *text_model, |
| 64 | const char *text, |
| 65 | uint32_t index) |
| 66 | { |
| 67 | struct text_entry *entry = data; |
| 68 | |
| 69 | text_entry_append(entry, text); |
| 70 | |
| 71 | widget_schedule_redraw(entry->widget); |
| 72 | } |
| 73 | |
Jan Arne Petersen | 72f6082 | 2012-08-10 16:47:19 +0200 | [diff] [blame] | 74 | static void |
| 75 | text_model_preedit_string(void *data, |
| 76 | struct text_model *text_model, |
| 77 | const char *text, |
| 78 | uint32_t index) |
| 79 | { |
| 80 | } |
| 81 | |
| 82 | static void |
| 83 | text_model_preedit_styling(void *data, |
| 84 | struct text_model *text_model) |
| 85 | { |
| 86 | } |
| 87 | |
| 88 | static void |
| 89 | text_model_key(void *data, |
| 90 | struct text_model *text_model) |
| 91 | { |
| 92 | } |
| 93 | |
| 94 | static void |
| 95 | text_model_selection_replacement(void *data, |
| 96 | struct text_model *text_model) |
| 97 | { |
| 98 | } |
| 99 | |
| 100 | static void |
| 101 | text_model_direction(void *data, |
| 102 | struct text_model *text_model) |
| 103 | { |
| 104 | } |
| 105 | |
| 106 | static void |
| 107 | text_model_locale(void *data, |
| 108 | struct text_model *text_model) |
| 109 | { |
| 110 | } |
| 111 | |
Jan Arne Petersen | de3b6a1 | 2012-08-10 16:47:21 +0200 | [diff] [blame] | 112 | static void |
| 113 | text_model_activated(void *data, |
| 114 | struct text_model *text_model) |
| 115 | { |
| 116 | struct text_entry *entry = data; |
| 117 | |
| 118 | entry->active = 1; |
| 119 | |
| 120 | widget_schedule_redraw(entry->widget); |
| 121 | } |
| 122 | |
| 123 | static void |
| 124 | text_model_deactivated(void *data, |
| 125 | struct text_model *text_model) |
| 126 | { |
| 127 | struct text_entry *entry = data; |
| 128 | |
| 129 | entry->active = 0; |
| 130 | |
| 131 | widget_schedule_redraw(entry->widget); |
| 132 | } |
| 133 | |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 134 | static const struct text_model_listener text_model_listener = { |
Jan Arne Petersen | 72f6082 | 2012-08-10 16:47:19 +0200 | [diff] [blame] | 135 | text_model_commit_string, |
| 136 | text_model_preedit_string, |
| 137 | text_model_preedit_styling, |
| 138 | text_model_key, |
| 139 | text_model_selection_replacement, |
| 140 | text_model_direction, |
Jan Arne Petersen | de3b6a1 | 2012-08-10 16:47:21 +0200 | [diff] [blame] | 141 | text_model_locale, |
| 142 | text_model_activated, |
| 143 | text_model_deactivated |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | static struct text_entry* |
| 147 | text_entry_create(struct editor *editor, const char *text) |
| 148 | { |
| 149 | struct text_entry *entry; |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 150 | |
| 151 | entry = malloc(sizeof *entry); |
| 152 | |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 153 | entry->widget = editor->widget; |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 154 | entry->window = editor->window; |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 155 | entry->text = strdup(text); |
| 156 | entry->active = 0; |
Jan Arne Petersen | 4c26518 | 2012-09-09 23:08:30 +0200 | [diff] [blame^] | 157 | 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] | 158 | text_model_add_listener(entry->model, &text_model_listener, entry); |
| 159 | |
| 160 | return entry; |
| 161 | } |
| 162 | |
| 163 | static void |
| 164 | text_entry_destroy(struct text_entry *entry) |
| 165 | { |
| 166 | text_model_destroy(entry->model); |
| 167 | free(entry->text); |
| 168 | free(entry); |
| 169 | } |
| 170 | |
| 171 | static void |
| 172 | text_entry_draw(struct text_entry *entry, cairo_t *cr) |
| 173 | { |
| 174 | cairo_save(cr); |
| 175 | cairo_set_operator(cr, CAIRO_OPERATOR_OVER); |
| 176 | |
| 177 | cairo_rectangle(cr, entry->allocation.x, entry->allocation.y, entry->allocation.width, entry->allocation.height); |
| 178 | cairo_clip(cr); |
| 179 | |
| 180 | cairo_translate(cr, entry->allocation.x, entry->allocation.y); |
| 181 | cairo_rectangle(cr, 0, 0, entry->allocation.width, entry->allocation.height); |
| 182 | cairo_set_source_rgba(cr, 1, 1, 1, 0.5); |
| 183 | cairo_fill(cr); |
| 184 | if (entry->active) { |
| 185 | cairo_rectangle(cr, 0, 0, entry->allocation.width, entry->allocation.height); |
| 186 | cairo_set_source_rgba(cr, 0, 0, 1, 0.5); |
| 187 | cairo_stroke(cr); |
| 188 | } |
| 189 | |
| 190 | cairo_set_source_rgb(cr, 0, 0, 0); |
| 191 | cairo_select_font_face(cr, "sans", |
| 192 | CAIRO_FONT_SLANT_NORMAL, |
| 193 | CAIRO_FONT_WEIGHT_BOLD); |
| 194 | cairo_set_font_size(cr, 14); |
| 195 | |
| 196 | cairo_translate(cr, 10, entry->allocation.height / 2); |
| 197 | cairo_show_text(cr, entry->text); |
| 198 | |
| 199 | cairo_restore(cr); |
| 200 | } |
| 201 | |
| 202 | static void |
| 203 | redraw_handler(struct widget *widget, void *data) |
| 204 | { |
| 205 | struct editor *editor = data; |
| 206 | cairo_surface_t *surface; |
| 207 | struct rectangle allocation; |
| 208 | cairo_t *cr; |
| 209 | |
| 210 | surface = window_get_surface(editor->window); |
| 211 | widget_get_allocation(editor->widget, &allocation); |
| 212 | |
| 213 | cr = cairo_create(surface); |
| 214 | cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height); |
| 215 | cairo_clip(cr); |
| 216 | |
| 217 | cairo_translate(cr, allocation.x, allocation.y); |
| 218 | |
| 219 | /* Draw background */ |
| 220 | cairo_push_group(cr); |
| 221 | cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); |
| 222 | cairo_set_source_rgba(cr, 1, 1, 1, 1); |
| 223 | cairo_rectangle(cr, 0, 0, allocation.width, allocation.height); |
| 224 | cairo_fill(cr); |
| 225 | |
| 226 | /* Entry */ |
| 227 | text_entry_draw(editor->entry, cr); |
| 228 | |
| 229 | /* Editor */ |
| 230 | text_entry_draw(editor->editor, cr); |
| 231 | |
| 232 | cairo_pop_group_to_source(cr); |
| 233 | cairo_paint(cr); |
| 234 | |
| 235 | cairo_destroy(cr); |
| 236 | cairo_surface_destroy(surface); |
| 237 | } |
| 238 | |
| 239 | static void |
| 240 | text_entry_allocate(struct text_entry *entry, int32_t x, int32_t y, |
| 241 | int32_t width, int32_t height) |
| 242 | { |
| 243 | entry->allocation.x = x; |
| 244 | entry->allocation.y = y; |
| 245 | entry->allocation.width = width; |
| 246 | entry->allocation.height = height; |
| 247 | } |
| 248 | |
| 249 | static void |
| 250 | resize_handler(struct widget *widget, |
| 251 | int32_t width, int32_t height, void *data) |
| 252 | { |
| 253 | struct editor *editor = data; |
| 254 | |
| 255 | text_entry_allocate(editor->entry, 20, 20, width - 40, height / 2 - 40); |
| 256 | text_entry_allocate(editor->editor, 20, height / 2 + 20, width - 40, height / 2 - 40); |
| 257 | } |
| 258 | |
| 259 | static int32_t |
| 260 | rectangle_contains(struct rectangle *rectangle, int32_t x, int32_t y) |
| 261 | { |
| 262 | if (x < rectangle->x || x > rectangle->x + rectangle->width) { |
| 263 | return 0; |
| 264 | } |
| 265 | |
| 266 | if (y < rectangle->y || y > rectangle->y + rectangle->height) { |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | return 1; |
| 271 | } |
| 272 | |
| 273 | static void |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 274 | text_entry_activate(struct text_entry *entry, |
| 275 | struct wl_seat *seat) |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 276 | { |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 277 | struct wl_surface *surface = window_get_wl_surface(entry->window); |
| 278 | |
| 279 | text_model_activate(entry->model, |
| 280 | seat, |
| 281 | surface); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | static void |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 285 | text_entry_deactivate(struct text_entry *entry, |
| 286 | struct wl_seat *seat) |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 287 | { |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 288 | text_model_deactivate(entry->model, |
| 289 | seat); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | static void |
| 293 | button_handler(struct widget *widget, |
| 294 | struct input *input, uint32_t time, |
| 295 | uint32_t button, |
| 296 | enum wl_pointer_button_state state, void *data) |
| 297 | { |
| 298 | struct editor *editor = data; |
| 299 | struct rectangle allocation; |
| 300 | int32_t x, y; |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 301 | struct wl_seat *seat; |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 302 | |
| 303 | if (state != WL_POINTER_BUTTON_STATE_PRESSED || button != BTN_LEFT) { |
| 304 | return; |
| 305 | } |
| 306 | |
| 307 | input_get_position(input, &x, &y); |
| 308 | |
| 309 | widget_get_allocation(editor->widget, &allocation); |
| 310 | x -= allocation.x; |
| 311 | y -= allocation.y; |
| 312 | |
Philipp Brüschweiler | 591cfca | 2012-07-11 22:25:29 +0200 | [diff] [blame] | 313 | int32_t activate_entry = rectangle_contains(&editor->entry->allocation, x, y); |
| 314 | int32_t activate_editor = rectangle_contains(&editor->editor->allocation, x, y); |
| 315 | assert(!(activate_entry && activate_editor)); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 316 | |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 317 | seat = input_get_seat(input); |
| 318 | |
Philipp Brüschweiler | 591cfca | 2012-07-11 22:25:29 +0200 | [diff] [blame] | 319 | if (activate_entry) { |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 320 | text_entry_activate(editor->entry, seat); |
Philipp Brüschweiler | 591cfca | 2012-07-11 22:25:29 +0200 | [diff] [blame] | 321 | } else if (activate_editor) { |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 322 | text_entry_activate(editor->editor, seat); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 323 | } else { |
Jan Arne Petersen | e829adc | 2012-08-10 16:47:22 +0200 | [diff] [blame] | 324 | text_entry_deactivate(editor->entry, seat); |
| 325 | text_entry_deactivate(editor->editor, seat); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 326 | } |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 327 | |
| 328 | widget_schedule_redraw(widget); |
| 329 | } |
| 330 | |
| 331 | static void |
| 332 | global_handler(struct wl_display *display, uint32_t id, |
| 333 | const char *interface, uint32_t version, void *data) |
| 334 | { |
| 335 | struct editor *editor = data; |
| 336 | |
Jan Arne Petersen | 5196374 | 2012-08-10 16:47:20 +0200 | [diff] [blame] | 337 | if (!strcmp(interface, "text_model_factory")) { |
| 338 | editor->text_model_factory = wl_display_bind(display, id, |
| 339 | &text_model_factory_interface); |
| 340 | } else if (!strcmp(interface, "wl_seat")) { |
| 341 | fprintf(stderr, "wl_seat added\n"); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 342 | } |
| 343 | } |
| 344 | |
| 345 | int |
| 346 | main(int argc, char *argv[]) |
| 347 | { |
| 348 | struct editor editor; |
| 349 | |
| 350 | editor.display = display_create(argc, argv); |
| 351 | if (editor.display == NULL) { |
| 352 | fprintf(stderr, "failed to create display: %m\n"); |
| 353 | return -1; |
| 354 | } |
| 355 | wl_display_add_global_listener(display_get_display(editor.display), |
| 356 | global_handler, &editor); |
| 357 | |
| 358 | |
| 359 | editor.window = window_create(editor.display); |
| 360 | editor.widget = frame_create(editor.window, &editor); |
| 361 | |
| 362 | editor.entry = text_entry_create(&editor, "Entry"); |
| 363 | editor.editor = text_entry_create(&editor, "Editor"); |
| 364 | |
| 365 | window_set_title(editor.window, "Text Editor"); |
| 366 | |
| 367 | widget_set_redraw_handler(editor.widget, redraw_handler); |
| 368 | widget_set_resize_handler(editor.widget, resize_handler); |
| 369 | widget_set_button_handler(editor.widget, button_handler); |
| 370 | |
| 371 | window_schedule_resize(editor.window, 500, 400); |
| 372 | |
| 373 | display_run(editor.display); |
| 374 | |
| 375 | text_entry_destroy(editor.entry); |
| 376 | text_entry_destroy(editor.editor); |
| 377 | |
| 378 | return 0; |
| 379 | } |