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.
27 #include <linux/input.h>
31 #include "text-client-protocol.h"
34 struct widget *widget;
37 struct rectangle allocation;
38 struct text_model *model;
42 struct input_method *input_method;
43 struct display *display;
44 struct window *window;
45 struct widget *widget;
46 struct text_entry *entry;
47 struct text_entry *editor;
51 text_entry_append(struct text_entry *entry, const char *text)
53 entry->text = realloc(entry->text, strlen(entry->text) + strlen(text) + 1);
54 strcat(entry->text, text);
59 text_model_commit_string(void *data,
60 struct text_model *text_model,
64 struct text_entry *entry = data;
66 text_entry_append(entry, text);
68 widget_schedule_redraw(entry->widget);
71 static const struct text_model_listener text_model_listener = {
72 text_model_commit_string
75 static struct text_entry*
76 text_entry_create(struct editor *editor, const char *text)
78 struct text_entry *entry;
79 struct wl_surface *surface;
81 entry = malloc(sizeof *entry);
83 surface = window_get_wl_surface(editor->window);
85 entry->widget = editor->widget;
86 entry->text = strdup(text);
88 entry->model = input_method_create_text_model(editor->input_method, surface);
89 text_model_add_listener(entry->model, &text_model_listener, entry);
95 text_entry_destroy(struct text_entry *entry)
97 text_model_destroy(entry->model);
103 text_entry_draw(struct text_entry *entry, cairo_t *cr)
106 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
108 cairo_rectangle(cr, entry->allocation.x, entry->allocation.y, entry->allocation.width, entry->allocation.height);
111 cairo_translate(cr, entry->allocation.x, entry->allocation.y);
112 cairo_rectangle(cr, 0, 0, entry->allocation.width, entry->allocation.height);
113 cairo_set_source_rgba(cr, 1, 1, 1, 0.5);
116 cairo_rectangle(cr, 0, 0, entry->allocation.width, entry->allocation.height);
117 cairo_set_source_rgba(cr, 0, 0, 1, 0.5);
121 cairo_set_source_rgb(cr, 0, 0, 0);
122 cairo_select_font_face(cr, "sans",
123 CAIRO_FONT_SLANT_NORMAL,
124 CAIRO_FONT_WEIGHT_BOLD);
125 cairo_set_font_size(cr, 14);
127 cairo_translate(cr, 10, entry->allocation.height / 2);
128 cairo_show_text(cr, entry->text);
134 redraw_handler(struct widget *widget, void *data)
136 struct editor *editor = data;
137 cairo_surface_t *surface;
138 struct rectangle allocation;
141 surface = window_get_surface(editor->window);
142 widget_get_allocation(editor->widget, &allocation);
144 cr = cairo_create(surface);
145 cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
148 cairo_translate(cr, allocation.x, allocation.y);
150 /* Draw background */
151 cairo_push_group(cr);
152 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
153 cairo_set_source_rgba(cr, 1, 1, 1, 1);
154 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
158 text_entry_draw(editor->entry, cr);
161 text_entry_draw(editor->editor, cr);
163 cairo_pop_group_to_source(cr);
167 cairo_surface_destroy(surface);
171 text_entry_allocate(struct text_entry *entry, int32_t x, int32_t y,
172 int32_t width, int32_t height)
174 entry->allocation.x = x;
175 entry->allocation.y = y;
176 entry->allocation.width = width;
177 entry->allocation.height = height;
181 resize_handler(struct widget *widget,
182 int32_t width, int32_t height, void *data)
184 struct editor *editor = data;
186 text_entry_allocate(editor->entry, 20, 20, width - 40, height / 2 - 40);
187 text_entry_allocate(editor->editor, 20, height / 2 + 20, width - 40, height / 2 - 40);
191 rectangle_contains(struct rectangle *rectangle, int32_t x, int32_t y)
193 if (x < rectangle->x || x > rectangle->x + rectangle->width) {
197 if (y < rectangle->y || y > rectangle->y + rectangle->height) {
205 text_entry_activate(struct text_entry *entry)
207 text_model_activate(entry->model);
211 text_entry_deactivate(struct text_entry *entry)
213 text_model_deactivate(entry->model);
217 button_handler(struct widget *widget,
218 struct input *input, uint32_t time,
220 enum wl_pointer_button_state state, void *data)
222 struct editor *editor = data;
223 struct rectangle allocation;
226 if (state != WL_POINTER_BUTTON_STATE_PRESSED || button != BTN_LEFT) {
230 input_get_position(input, &x, &y);
232 widget_get_allocation(editor->widget, &allocation);
236 editor->entry->active = 0;
237 editor->editor->active = 0;
239 editor->entry->active = rectangle_contains(&editor->entry->allocation, x, y);
240 editor->editor->active = rectangle_contains(&editor->editor->allocation, x, y);
242 if (editor->entry->active) {
243 text_entry_activate(editor->entry);
245 text_entry_deactivate(editor->entry);
247 if (editor->editor->active) {
248 text_entry_activate(editor->editor);
250 text_entry_deactivate(editor->editor);
253 widget_schedule_redraw(widget);
257 global_handler(struct wl_display *display, uint32_t id,
258 const char *interface, uint32_t version, void *data)
260 struct editor *editor = data;
262 if (!strcmp(interface, "input_method")) {
263 editor->input_method = wl_display_bind(display, id, &input_method_interface);
268 main(int argc, char *argv[])
270 struct editor editor;
272 editor.display = display_create(argc, argv);
273 if (editor.display == NULL) {
274 fprintf(stderr, "failed to create display: %m\n");
277 wl_display_add_global_listener(display_get_display(editor.display),
278 global_handler, &editor);
281 editor.window = window_create(editor.display);
282 editor.widget = frame_create(editor.window, &editor);
284 editor.entry = text_entry_create(&editor, "Entry");
285 editor.editor = text_entry_create(&editor, "Editor");
287 window_set_title(editor.window, "Text Editor");
289 widget_set_redraw_handler(editor.widget, redraw_handler);
290 widget_set_resize_handler(editor.widget, resize_handler);
291 widget_set_button_handler(editor.widget, button_handler);
293 window_schedule_resize(editor.window, 500, 400);
295 display_run(editor.display);
297 text_entry_destroy(editor.entry);
298 text_entry_destroy(editor.editor);