2 * Copyright © 2012 Openismus GmbH
3 * Copyright © 2012 Intel Corporation
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.
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.
29 #include <linux/input.h>
33 #include "text-client-protocol.h"
36 struct widget *widget;
37 struct window *window;
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;
52 static void text_entry_redraw_handler(struct widget *widget, void *data);
53 static void text_entry_button_handler(struct widget *widget,
54 struct input *input, uint32_t time,
56 enum wl_pointer_button_state state, void *data);
59 text_entry_append(struct text_entry *entry, const char *text)
61 entry->text = realloc(entry->text, strlen(entry->text) + strlen(text) + 1);
62 strcat(entry->text, text);
67 text_model_commit_string(void *data,
68 struct text_model *text_model,
72 struct text_entry *entry = data;
74 text_entry_append(entry, text);
76 widget_schedule_redraw(entry->widget);
80 text_model_preedit_string(void *data,
81 struct text_model *text_model,
88 text_model_preedit_styling(void *data,
89 struct text_model *text_model)
94 text_model_key(void *data,
95 struct text_model *text_model)
100 text_model_selection_replacement(void *data,
101 struct text_model *text_model)
106 text_model_direction(void *data,
107 struct text_model *text_model)
112 text_model_locale(void *data,
113 struct text_model *text_model)
118 text_model_activated(void *data,
119 struct text_model *text_model)
121 struct text_entry *entry = data;
125 widget_schedule_redraw(entry->widget);
129 text_model_deactivated(void *data,
130 struct text_model *text_model)
132 struct text_entry *entry = data;
136 widget_schedule_redraw(entry->widget);
139 static const struct text_model_listener text_model_listener = {
140 text_model_commit_string,
141 text_model_preedit_string,
142 text_model_preedit_styling,
144 text_model_selection_replacement,
145 text_model_direction,
147 text_model_activated,
148 text_model_deactivated
151 static struct text_entry*
152 text_entry_create(struct editor *editor, const char *text)
154 struct text_entry *entry;
156 entry = malloc(sizeof *entry);
158 entry->widget = widget_add_widget(editor->widget, entry);
159 entry->window = editor->window;
160 entry->text = strdup(text);
162 entry->model = text_model_factory_create_text_model(editor->text_model_factory);
163 text_model_add_listener(entry->model, &text_model_listener, entry);
165 widget_set_redraw_handler(entry->widget, text_entry_redraw_handler);
166 widget_set_button_handler(entry->widget, text_entry_button_handler);
172 text_entry_destroy(struct text_entry *entry)
174 widget_destroy(entry->widget);
175 text_model_destroy(entry->model);
181 redraw_handler(struct widget *widget, void *data)
183 struct editor *editor = data;
184 cairo_surface_t *surface;
185 struct rectangle allocation;
188 surface = window_get_surface(editor->window);
189 widget_get_allocation(editor->widget, &allocation);
191 cr = cairo_create(surface);
192 cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
195 cairo_translate(cr, allocation.x, allocation.y);
197 /* Draw background */
198 cairo_push_group(cr);
199 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
200 cairo_set_source_rgba(cr, 1, 1, 1, 1);
201 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
204 cairo_pop_group_to_source(cr);
208 cairo_surface_destroy(surface);
212 text_entry_allocate(struct text_entry *entry, int32_t x, int32_t y,
213 int32_t width, int32_t height)
215 widget_set_allocation(entry->widget, x, y, width, height);
219 resize_handler(struct widget *widget,
220 int32_t width, int32_t height, void *data)
222 struct editor *editor = data;
223 struct rectangle allocation;
225 widget_get_allocation(editor->widget, &allocation);
227 text_entry_allocate(editor->entry,
228 allocation.x + 20, allocation.y + 20,
229 width - 40, height / 2 - 40);
230 text_entry_allocate(editor->editor,
231 allocation.x + 20, allocation.y + height / 2 + 20,
232 width - 40, height / 2 - 40);
236 text_entry_activate(struct text_entry *entry,
237 struct wl_seat *seat)
239 struct wl_surface *surface = window_get_wl_surface(entry->window);
241 text_model_activate(entry->model,
247 text_entry_deactivate(struct text_entry *entry,
248 struct wl_seat *seat)
250 text_model_deactivate(entry->model,
255 text_entry_redraw_handler(struct widget *widget, void *data)
257 struct text_entry *entry = data;
258 cairo_surface_t *surface;
259 struct rectangle allocation;
262 surface = window_get_surface(entry->window);
263 widget_get_allocation(entry->widget, &allocation);
265 cr = cairo_create(surface);
266 cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
269 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
271 cairo_push_group(cr);
272 cairo_translate(cr, allocation.x, allocation.y);
274 cairo_set_source_rgba(cr, 1, 1, 1, 1);
275 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
278 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
281 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
282 cairo_set_line_width (cr, 3);
283 cairo_set_source_rgba(cr, 0, 0, 1, 1.0);
287 cairo_set_source_rgba(cr, 0, 0, 0, 1);
288 cairo_select_font_face(cr, "sans",
289 CAIRO_FONT_SLANT_NORMAL,
290 CAIRO_FONT_WEIGHT_BOLD);
291 cairo_set_font_size(cr, 14);
293 cairo_translate(cr, 10, allocation.height / 2);
294 cairo_show_text(cr, entry->text);
295 cairo_pop_group_to_source(cr);
299 cairo_surface_destroy(surface);
303 text_entry_button_handler(struct widget *widget,
304 struct input *input, uint32_t time,
306 enum wl_pointer_button_state state, void *data)
308 struct text_entry *entry = data;
310 if (button != BTN_LEFT) {
314 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
315 struct wl_seat *seat = input_get_seat(input);
317 text_entry_activate(entry, seat);
322 editor_button_handler(struct widget *widget,
323 struct input *input, uint32_t time,
325 enum wl_pointer_button_state state, void *data)
327 struct editor *editor = data;
329 if (button != BTN_LEFT) {
333 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
334 struct wl_seat *seat = input_get_seat(input);
336 text_entry_deactivate(editor->entry, seat);
337 text_entry_deactivate(editor->editor, seat);
342 global_handler(struct wl_display *display, uint32_t id,
343 const char *interface, uint32_t version, void *data)
345 struct editor *editor = data;
347 if (!strcmp(interface, "text_model_factory")) {
348 editor->text_model_factory = wl_display_bind(display, id,
349 &text_model_factory_interface);
354 main(int argc, char *argv[])
356 struct editor editor;
358 editor.display = display_create(argc, argv);
359 if (editor.display == NULL) {
360 fprintf(stderr, "failed to create display: %m\n");
363 wl_display_add_global_listener(display_get_display(editor.display),
364 global_handler, &editor);
367 editor.window = window_create(editor.display);
368 editor.widget = frame_create(editor.window, &editor);
370 editor.entry = text_entry_create(&editor, "Entry");
371 editor.editor = text_entry_create(&editor, "Editor");
373 window_set_title(editor.window, "Text Editor");
375 widget_set_redraw_handler(editor.widget, redraw_handler);
376 widget_set_resize_handler(editor.widget, resize_handler);
377 widget_set_button_handler(editor.widget, editor_button_handler);
379 window_schedule_resize(editor.window, 500, 400);
381 display_run(editor.display);
383 text_entry_destroy(editor.entry);
384 text_entry_destroy(editor.editor);