2 * Copyright © 2012 Openismus GmbH
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
28 #include <linux/input.h>
32 #include "text-client-protocol.h"
35 struct widget *widget;
36 struct window *window;
39 struct rectangle allocation;
40 struct text_model *model;
44 struct text_model_factory *text_model_factory;
45 struct display *display;
46 struct window *window;
47 struct widget *widget;
48 struct text_entry *entry;
49 struct text_entry *editor;
53 text_entry_append(struct text_entry *entry, const char *text)
55 entry->text = realloc(entry->text, strlen(entry->text) + strlen(text) + 1);
56 strcat(entry->text, text);
61 text_model_commit_string(void *data,
62 struct text_model *text_model,
66 struct text_entry *entry = data;
68 text_entry_append(entry, text);
70 widget_schedule_redraw(entry->widget);
74 text_model_preedit_string(void *data,
75 struct text_model *text_model,
82 text_model_preedit_styling(void *data,
83 struct text_model *text_model)
88 text_model_key(void *data,
89 struct text_model *text_model)
94 text_model_selection_replacement(void *data,
95 struct text_model *text_model)
100 text_model_direction(void *data,
101 struct text_model *text_model)
106 text_model_locale(void *data,
107 struct text_model *text_model)
112 text_model_activated(void *data,
113 struct text_model *text_model)
115 struct text_entry *entry = data;
119 widget_schedule_redraw(entry->widget);
123 text_model_deactivated(void *data,
124 struct text_model *text_model)
126 struct text_entry *entry = data;
130 widget_schedule_redraw(entry->widget);
133 static const struct text_model_listener text_model_listener = {
134 text_model_commit_string,
135 text_model_preedit_string,
136 text_model_preedit_styling,
138 text_model_selection_replacement,
139 text_model_direction,
141 text_model_activated,
142 text_model_deactivated
145 static struct text_entry*
146 text_entry_create(struct editor *editor, const char *text)
148 struct text_entry *entry;
149 struct wl_surface *surface;
151 entry = malloc(sizeof *entry);
153 surface = window_get_wl_surface(editor->window);
155 entry->widget = editor->widget;
156 entry->window = editor->window;
157 entry->text = strdup(text);
159 entry->model = text_model_factory_create_text_model(editor->text_model_factory, surface);
160 text_model_add_listener(entry->model, &text_model_listener, entry);
166 text_entry_destroy(struct text_entry *entry)
168 text_model_destroy(entry->model);
174 text_entry_draw(struct text_entry *entry, cairo_t *cr)
177 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
179 cairo_rectangle(cr, entry->allocation.x, entry->allocation.y, entry->allocation.width, entry->allocation.height);
182 cairo_translate(cr, entry->allocation.x, entry->allocation.y);
183 cairo_rectangle(cr, 0, 0, entry->allocation.width, entry->allocation.height);
184 cairo_set_source_rgba(cr, 1, 1, 1, 0.5);
187 cairo_rectangle(cr, 0, 0, entry->allocation.width, entry->allocation.height);
188 cairo_set_source_rgba(cr, 0, 0, 1, 0.5);
192 cairo_set_source_rgb(cr, 0, 0, 0);
193 cairo_select_font_face(cr, "sans",
194 CAIRO_FONT_SLANT_NORMAL,
195 CAIRO_FONT_WEIGHT_BOLD);
196 cairo_set_font_size(cr, 14);
198 cairo_translate(cr, 10, entry->allocation.height / 2);
199 cairo_show_text(cr, entry->text);
205 redraw_handler(struct widget *widget, void *data)
207 struct editor *editor = data;
208 cairo_surface_t *surface;
209 struct rectangle allocation;
212 surface = window_get_surface(editor->window);
213 widget_get_allocation(editor->widget, &allocation);
215 cr = cairo_create(surface);
216 cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
219 cairo_translate(cr, allocation.x, allocation.y);
221 /* Draw background */
222 cairo_push_group(cr);
223 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
224 cairo_set_source_rgba(cr, 1, 1, 1, 1);
225 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
229 text_entry_draw(editor->entry, cr);
232 text_entry_draw(editor->editor, cr);
234 cairo_pop_group_to_source(cr);
238 cairo_surface_destroy(surface);
242 text_entry_allocate(struct text_entry *entry, int32_t x, int32_t y,
243 int32_t width, int32_t height)
245 entry->allocation.x = x;
246 entry->allocation.y = y;
247 entry->allocation.width = width;
248 entry->allocation.height = height;
252 resize_handler(struct widget *widget,
253 int32_t width, int32_t height, void *data)
255 struct editor *editor = data;
257 text_entry_allocate(editor->entry, 20, 20, width - 40, height / 2 - 40);
258 text_entry_allocate(editor->editor, 20, height / 2 + 20, width - 40, height / 2 - 40);
262 rectangle_contains(struct rectangle *rectangle, int32_t x, int32_t y)
264 if (x < rectangle->x || x > rectangle->x + rectangle->width) {
268 if (y < rectangle->y || y > rectangle->y + rectangle->height) {
276 text_entry_activate(struct text_entry *entry,
277 struct wl_seat *seat)
279 struct wl_surface *surface = window_get_wl_surface(entry->window);
281 text_model_activate(entry->model,
287 text_entry_deactivate(struct text_entry *entry,
288 struct wl_seat *seat)
290 text_model_deactivate(entry->model,
295 button_handler(struct widget *widget,
296 struct input *input, uint32_t time,
298 enum wl_pointer_button_state state, void *data)
300 struct editor *editor = data;
301 struct rectangle allocation;
303 struct wl_seat *seat;
305 if (state != WL_POINTER_BUTTON_STATE_PRESSED || button != BTN_LEFT) {
309 input_get_position(input, &x, &y);
311 widget_get_allocation(editor->widget, &allocation);
315 int32_t activate_entry = rectangle_contains(&editor->entry->allocation, x, y);
316 int32_t activate_editor = rectangle_contains(&editor->editor->allocation, x, y);
317 assert(!(activate_entry && activate_editor));
319 seat = input_get_seat(input);
321 if (activate_entry) {
322 text_entry_activate(editor->entry, seat);
323 } else if (activate_editor) {
324 text_entry_activate(editor->editor, seat);
326 text_entry_deactivate(editor->entry, seat);
327 text_entry_deactivate(editor->editor, seat);
330 widget_schedule_redraw(widget);
334 global_handler(struct wl_display *display, uint32_t id,
335 const char *interface, uint32_t version, void *data)
337 struct editor *editor = data;
339 if (!strcmp(interface, "text_model_factory")) {
340 editor->text_model_factory = wl_display_bind(display, id,
341 &text_model_factory_interface);
342 } else if (!strcmp(interface, "wl_seat")) {
343 fprintf(stderr, "wl_seat added\n");
348 main(int argc, char *argv[])
350 struct editor editor;
352 editor.display = display_create(argc, argv);
353 if (editor.display == NULL) {
354 fprintf(stderr, "failed to create display: %m\n");
357 wl_display_add_global_listener(display_get_display(editor.display),
358 global_handler, &editor);
361 editor.window = window_create(editor.display);
362 editor.widget = frame_create(editor.window, &editor);
364 editor.entry = text_entry_create(&editor, "Entry");
365 editor.editor = text_entry_create(&editor, "Editor");
367 window_set_title(editor.window, "Text Editor");
369 widget_set_redraw_handler(editor.widget, redraw_handler);
370 widget_set_resize_handler(editor.widget, resize_handler);
371 widget_set_button_handler(editor.widget, button_handler);
373 window_schedule_resize(editor.window, 500, 400);
375 display_run(editor.display);
377 text_entry_destroy(editor.entry);
378 text_entry_destroy(editor.editor);