blob: 145ba20c0598deee699d89e14d44e3548250da24 [file] [log] [blame]
Jan Arne Petersencba9e472012-06-21 21:52:19 +02001/*
2 * Copyright © 2012 Openismus GmbH
3 *
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.
13 *
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.
21 */
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26
27#include <linux/input.h>
28#include <cairo.h>
29
30#include "window.h"
31#include "text-client-protocol.h"
32
33struct text_entry {
34 struct widget *widget;
35 char *text;
36 int active;
37 struct rectangle allocation;
38 struct text_model *model;
39};
40
41struct editor {
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;
48};
49
50static void
51text_entry_append(struct text_entry *entry, const char *text)
52{
53 entry->text = realloc(entry->text, strlen(entry->text) + strlen(text) + 1);
54 strcat(entry->text, text);
55}
56
57
58static void
59text_model_commit_string(void *data,
60 struct text_model *text_model,
61 const char *text,
62 uint32_t index)
63{
64 struct text_entry *entry = data;
65
66 text_entry_append(entry, text);
67
68 widget_schedule_redraw(entry->widget);
69}
70
71static const struct text_model_listener text_model_listener = {
72 text_model_commit_string
73};
74
75static struct text_entry*
76text_entry_create(struct editor *editor, const char *text)
77{
78 struct text_entry *entry;
79 struct wl_surface *surface;
80
81 entry = malloc(sizeof *entry);
82
83 surface = window_get_wl_surface(editor->window);
84
85 entry->widget = editor->widget;
86 entry->text = strdup(text);
87 entry->active = 0;
88 entry->model = input_method_create_text_model(editor->input_method, surface);
89 text_model_add_listener(entry->model, &text_model_listener, entry);
90
91 return entry;
92}
93
94static void
95text_entry_destroy(struct text_entry *entry)
96{
97 text_model_destroy(entry->model);
98 free(entry->text);
99 free(entry);
100}
101
102static void
103text_entry_draw(struct text_entry *entry, cairo_t *cr)
104{
105 cairo_save(cr);
106 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
107
108 cairo_rectangle(cr, entry->allocation.x, entry->allocation.y, entry->allocation.width, entry->allocation.height);
109 cairo_clip(cr);
110
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);
114 cairo_fill(cr);
115 if (entry->active) {
116 cairo_rectangle(cr, 0, 0, entry->allocation.width, entry->allocation.height);
117 cairo_set_source_rgba(cr, 0, 0, 1, 0.5);
118 cairo_stroke(cr);
119 }
120
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);
126
127 cairo_translate(cr, 10, entry->allocation.height / 2);
128 cairo_show_text(cr, entry->text);
129
130 cairo_restore(cr);
131}
132
133static void
134redraw_handler(struct widget *widget, void *data)
135{
136 struct editor *editor = data;
137 cairo_surface_t *surface;
138 struct rectangle allocation;
139 cairo_t *cr;
140
141 surface = window_get_surface(editor->window);
142 widget_get_allocation(editor->widget, &allocation);
143
144 cr = cairo_create(surface);
145 cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
146 cairo_clip(cr);
147
148 cairo_translate(cr, allocation.x, allocation.y);
149
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);
155 cairo_fill(cr);
156
157 /* Entry */
158 text_entry_draw(editor->entry, cr);
159
160 /* Editor */
161 text_entry_draw(editor->editor, cr);
162
163 cairo_pop_group_to_source(cr);
164 cairo_paint(cr);
165
166 cairo_destroy(cr);
167 cairo_surface_destroy(surface);
168}
169
170static void
171text_entry_allocate(struct text_entry *entry, int32_t x, int32_t y,
172 int32_t width, int32_t height)
173{
174 entry->allocation.x = x;
175 entry->allocation.y = y;
176 entry->allocation.width = width;
177 entry->allocation.height = height;
178}
179
180static void
181resize_handler(struct widget *widget,
182 int32_t width, int32_t height, void *data)
183{
184 struct editor *editor = data;
185
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);
188}
189
190static int32_t
191rectangle_contains(struct rectangle *rectangle, int32_t x, int32_t y)
192{
193 if (x < rectangle->x || x > rectangle->x + rectangle->width) {
194 return 0;
195 }
196
197 if (y < rectangle->y || y > rectangle->y + rectangle->height) {
198 return 0;
199 }
200
201 return 1;
202}
203
204static void
205text_entry_activate(struct text_entry *entry)
206{
207 text_model_activate(entry->model);
208}
209
210static void
211text_entry_deactivate(struct text_entry *entry)
212{
213 text_model_deactivate(entry->model);
214}
215
216static void
217button_handler(struct widget *widget,
218 struct input *input, uint32_t time,
219 uint32_t button,
220 enum wl_pointer_button_state state, void *data)
221{
222 struct editor *editor = data;
223 struct rectangle allocation;
224 int32_t x, y;
225
226 if (state != WL_POINTER_BUTTON_STATE_PRESSED || button != BTN_LEFT) {
227 return;
228 }
229
230 input_get_position(input, &x, &y);
231
232 widget_get_allocation(editor->widget, &allocation);
233 x -= allocation.x;
234 y -= allocation.y;
235
236 editor->entry->active = 0;
237 editor->editor->active = 0;
238
239 editor->entry->active = rectangle_contains(&editor->entry->allocation, x, y);
240 editor->editor->active = rectangle_contains(&editor->editor->allocation, x, y);
241
242 if (editor->entry->active) {
243 text_entry_activate(editor->entry);
244 } else {
245 text_entry_deactivate(editor->entry);
246 }
247 if (editor->editor->active) {
248 text_entry_activate(editor->editor);
249 } else {
250 text_entry_deactivate(editor->editor);
251 }
252
253 widget_schedule_redraw(widget);
254}
255
256static void
257global_handler(struct wl_display *display, uint32_t id,
258 const char *interface, uint32_t version, void *data)
259{
260 struct editor *editor = data;
261
262 if (!strcmp(interface, "input_method")) {
263 editor->input_method = wl_display_bind(display, id, &input_method_interface);
264 }
265}
266
267int
268main(int argc, char *argv[])
269{
270 struct editor editor;
271
272 editor.display = display_create(argc, argv);
273 if (editor.display == NULL) {
274 fprintf(stderr, "failed to create display: %m\n");
275 return -1;
276 }
277 wl_display_add_global_listener(display_get_display(editor.display),
278 global_handler, &editor);
279
280
281 editor.window = window_create(editor.display);
282 editor.widget = frame_create(editor.window, &editor);
283
284 editor.entry = text_entry_create(&editor, "Entry");
285 editor.editor = text_entry_create(&editor, "Editor");
286
287 window_set_title(editor.window, "Text Editor");
288
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);
292
293 window_schedule_resize(editor.window, 500, 400);
294
295 display_run(editor.display);
296
297 text_entry_destroy(editor.entry);
298 text_entry_destroy(editor.editor);
299
300 return 0;
301}