blob: 9cb7326955206dd835f0a79bc35d347ec78954a5 [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
Philipp Brüschweiler591cfca2012-07-11 22:25:29 +020023#include <assert.h>
Jan Arne Petersencba9e472012-06-21 21:52:19 +020024#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27
28#include <linux/input.h>
29#include <cairo.h>
30
31#include "window.h"
32#include "text-client-protocol.h"
33
34struct text_entry {
35 struct widget *widget;
36 char *text;
37 int active;
38 struct rectangle allocation;
39 struct text_model *model;
40};
41
42struct editor {
Jan Arne Petersen51963742012-08-10 16:47:20 +020043 struct text_model_factory *text_model_factory;
Jan Arne Petersencba9e472012-06-21 21:52:19 +020044 struct display *display;
45 struct window *window;
46 struct widget *widget;
47 struct text_entry *entry;
48 struct text_entry *editor;
49};
50
51static void
52text_entry_append(struct text_entry *entry, const char *text)
53{
54 entry->text = realloc(entry->text, strlen(entry->text) + strlen(text) + 1);
55 strcat(entry->text, text);
56}
57
58
59static void
60text_model_commit_string(void *data,
61 struct text_model *text_model,
62 const char *text,
63 uint32_t index)
64{
65 struct text_entry *entry = data;
66
67 text_entry_append(entry, text);
68
69 widget_schedule_redraw(entry->widget);
70}
71
Jan Arne Petersen72f60822012-08-10 16:47:19 +020072static void
73text_model_preedit_string(void *data,
74 struct text_model *text_model,
75 const char *text,
76 uint32_t index)
77{
78}
79
80static void
81text_model_preedit_styling(void *data,
82 struct text_model *text_model)
83{
84}
85
86static void
87text_model_key(void *data,
88 struct text_model *text_model)
89{
90}
91
92static void
93text_model_selection_replacement(void *data,
94 struct text_model *text_model)
95{
96}
97
98static void
99text_model_direction(void *data,
100 struct text_model *text_model)
101{
102}
103
104static void
105text_model_locale(void *data,
106 struct text_model *text_model)
107{
108}
109
Jan Arne Petersende3b6a12012-08-10 16:47:21 +0200110static void
111text_model_activated(void *data,
112 struct text_model *text_model)
113{
114 struct text_entry *entry = data;
115
116 entry->active = 1;
117
118 widget_schedule_redraw(entry->widget);
119}
120
121static void
122text_model_deactivated(void *data,
123 struct text_model *text_model)
124{
125 struct text_entry *entry = data;
126
127 entry->active = 0;
128
129 widget_schedule_redraw(entry->widget);
130}
131
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200132static const struct text_model_listener text_model_listener = {
Jan Arne Petersen72f60822012-08-10 16:47:19 +0200133 text_model_commit_string,
134 text_model_preedit_string,
135 text_model_preedit_styling,
136 text_model_key,
137 text_model_selection_replacement,
138 text_model_direction,
Jan Arne Petersende3b6a12012-08-10 16:47:21 +0200139 text_model_locale,
140 text_model_activated,
141 text_model_deactivated
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200142};
143
144static struct text_entry*
145text_entry_create(struct editor *editor, const char *text)
146{
147 struct text_entry *entry;
148 struct wl_surface *surface;
149
150 entry = malloc(sizeof *entry);
151
152 surface = window_get_wl_surface(editor->window);
153
154 entry->widget = editor->widget;
155 entry->text = strdup(text);
156 entry->active = 0;
Jan Arne Petersen51963742012-08-10 16:47:20 +0200157 entry->model = text_model_factory_create_text_model(editor->text_model_factory, surface);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200158 text_model_add_listener(entry->model, &text_model_listener, entry);
159
160 return entry;
161}
162
163static void
164text_entry_destroy(struct text_entry *entry)
165{
166 text_model_destroy(entry->model);
167 free(entry->text);
168 free(entry);
169}
170
171static void
172text_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
202static void
203redraw_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
239static void
240text_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
249static void
250resize_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
259static int32_t
260rectangle_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
273static void
274text_entry_activate(struct text_entry *entry)
275{
276 text_model_activate(entry->model);
277}
278
279static void
280text_entry_deactivate(struct text_entry *entry)
281{
282 text_model_deactivate(entry->model);
283}
284
285static void
286button_handler(struct widget *widget,
287 struct input *input, uint32_t time,
288 uint32_t button,
289 enum wl_pointer_button_state state, void *data)
290{
291 struct editor *editor = data;
292 struct rectangle allocation;
293 int32_t x, y;
294
295 if (state != WL_POINTER_BUTTON_STATE_PRESSED || button != BTN_LEFT) {
296 return;
297 }
298
299 input_get_position(input, &x, &y);
300
301 widget_get_allocation(editor->widget, &allocation);
302 x -= allocation.x;
303 y -= allocation.y;
304
Philipp Brüschweiler591cfca2012-07-11 22:25:29 +0200305 int32_t activate_entry = rectangle_contains(&editor->entry->allocation, x, y);
306 int32_t activate_editor = rectangle_contains(&editor->editor->allocation, x, y);
307 assert(!(activate_entry && activate_editor));
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200308
Philipp Brüschweiler591cfca2012-07-11 22:25:29 +0200309 if (activate_entry) {
Jan Arne Petersende3b6a12012-08-10 16:47:21 +0200310 text_entry_activate(editor->entry);
Philipp Brüschweiler591cfca2012-07-11 22:25:29 +0200311 } else if (activate_editor) {
Jan Arne Petersende3b6a12012-08-10 16:47:21 +0200312 text_entry_activate(editor->editor);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200313 } else {
Jan Arne Petersende3b6a12012-08-10 16:47:21 +0200314 text_entry_deactivate(editor->entry);
315 text_entry_deactivate(editor->editor);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200316 }
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200317
318 widget_schedule_redraw(widget);
319}
320
321static void
322global_handler(struct wl_display *display, uint32_t id,
323 const char *interface, uint32_t version, void *data)
324{
325 struct editor *editor = data;
326
Jan Arne Petersen51963742012-08-10 16:47:20 +0200327 if (!strcmp(interface, "text_model_factory")) {
328 editor->text_model_factory = wl_display_bind(display, id,
329 &text_model_factory_interface);
330 } else if (!strcmp(interface, "wl_seat")) {
331 fprintf(stderr, "wl_seat added\n");
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200332 }
333}
334
335int
336main(int argc, char *argv[])
337{
338 struct editor editor;
339
340 editor.display = display_create(argc, argv);
341 if (editor.display == NULL) {
342 fprintf(stderr, "failed to create display: %m\n");
343 return -1;
344 }
345 wl_display_add_global_listener(display_get_display(editor.display),
346 global_handler, &editor);
347
348
349 editor.window = window_create(editor.display);
350 editor.widget = frame_create(editor.window, &editor);
351
352 editor.entry = text_entry_create(&editor, "Entry");
353 editor.editor = text_entry_create(&editor, "Editor");
354
355 window_set_title(editor.window, "Text Editor");
356
357 widget_set_redraw_handler(editor.widget, redraw_handler);
358 widget_set_resize_handler(editor.widget, resize_handler);
359 widget_set_button_handler(editor.widget, button_handler);
360
361 window_schedule_resize(editor.window, 500, 400);
362
363 display_run(editor.display);
364
365 text_entry_destroy(editor.entry);
366 text_entry_destroy(editor.editor);
367
368 return 0;
369}