blob: a5347fef8383b9e8c278f20f8a9515e239327d46 [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;
Jan Arne Petersene829adc2012-08-10 16:47:22 +020036 struct window *window;
Jan Arne Petersencba9e472012-06-21 21:52:19 +020037 char *text;
38 int active;
39 struct rectangle allocation;
40 struct text_model *model;
41};
42
43struct editor {
Jan Arne Petersen51963742012-08-10 16:47:20 +020044 struct text_model_factory *text_model_factory;
Jan Arne Petersencba9e472012-06-21 21:52:19 +020045 struct display *display;
46 struct window *window;
47 struct widget *widget;
48 struct text_entry *entry;
49 struct text_entry *editor;
50};
51
52static void
53text_entry_append(struct text_entry *entry, const char *text)
54{
55 entry->text = realloc(entry->text, strlen(entry->text) + strlen(text) + 1);
56 strcat(entry->text, text);
57}
58
59
60static void
61text_model_commit_string(void *data,
62 struct text_model *text_model,
63 const char *text,
64 uint32_t index)
65{
66 struct text_entry *entry = data;
67
68 text_entry_append(entry, text);
69
70 widget_schedule_redraw(entry->widget);
71}
72
Jan Arne Petersen72f60822012-08-10 16:47:19 +020073static void
74text_model_preedit_string(void *data,
75 struct text_model *text_model,
76 const char *text,
77 uint32_t index)
78{
79}
80
81static void
82text_model_preedit_styling(void *data,
83 struct text_model *text_model)
84{
85}
86
87static void
88text_model_key(void *data,
89 struct text_model *text_model)
90{
91}
92
93static void
94text_model_selection_replacement(void *data,
95 struct text_model *text_model)
96{
97}
98
99static void
100text_model_direction(void *data,
101 struct text_model *text_model)
102{
103}
104
105static void
106text_model_locale(void *data,
107 struct text_model *text_model)
108{
109}
110
Jan Arne Petersende3b6a12012-08-10 16:47:21 +0200111static void
112text_model_activated(void *data,
113 struct text_model *text_model)
114{
115 struct text_entry *entry = data;
116
117 entry->active = 1;
118
119 widget_schedule_redraw(entry->widget);
120}
121
122static void
123text_model_deactivated(void *data,
124 struct text_model *text_model)
125{
126 struct text_entry *entry = data;
127
128 entry->active = 0;
129
130 widget_schedule_redraw(entry->widget);
131}
132
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200133static const struct text_model_listener text_model_listener = {
Jan Arne Petersen72f60822012-08-10 16:47:19 +0200134 text_model_commit_string,
135 text_model_preedit_string,
136 text_model_preedit_styling,
137 text_model_key,
138 text_model_selection_replacement,
139 text_model_direction,
Jan Arne Petersende3b6a12012-08-10 16:47:21 +0200140 text_model_locale,
141 text_model_activated,
142 text_model_deactivated
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200143};
144
145static struct text_entry*
146text_entry_create(struct editor *editor, const char *text)
147{
148 struct text_entry *entry;
149 struct wl_surface *surface;
150
151 entry = malloc(sizeof *entry);
152
153 surface = window_get_wl_surface(editor->window);
154
155 entry->widget = editor->widget;
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200156 entry->window = editor->window;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200157 entry->text = strdup(text);
158 entry->active = 0;
Jan Arne Petersen51963742012-08-10 16:47:20 +0200159 entry->model = text_model_factory_create_text_model(editor->text_model_factory, surface);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200160 text_model_add_listener(entry->model, &text_model_listener, entry);
161
162 return entry;
163}
164
165static void
166text_entry_destroy(struct text_entry *entry)
167{
168 text_model_destroy(entry->model);
169 free(entry->text);
170 free(entry);
171}
172
173static void
174text_entry_draw(struct text_entry *entry, cairo_t *cr)
175{
176 cairo_save(cr);
177 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
178
179 cairo_rectangle(cr, entry->allocation.x, entry->allocation.y, entry->allocation.width, entry->allocation.height);
180 cairo_clip(cr);
181
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);
185 cairo_fill(cr);
186 if (entry->active) {
187 cairo_rectangle(cr, 0, 0, entry->allocation.width, entry->allocation.height);
188 cairo_set_source_rgba(cr, 0, 0, 1, 0.5);
189 cairo_stroke(cr);
190 }
191
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);
197
198 cairo_translate(cr, 10, entry->allocation.height / 2);
199 cairo_show_text(cr, entry->text);
200
201 cairo_restore(cr);
202}
203
204static void
205redraw_handler(struct widget *widget, void *data)
206{
207 struct editor *editor = data;
208 cairo_surface_t *surface;
209 struct rectangle allocation;
210 cairo_t *cr;
211
212 surface = window_get_surface(editor->window);
213 widget_get_allocation(editor->widget, &allocation);
214
215 cr = cairo_create(surface);
216 cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
217 cairo_clip(cr);
218
219 cairo_translate(cr, allocation.x, allocation.y);
220
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);
226 cairo_fill(cr);
227
228 /* Entry */
229 text_entry_draw(editor->entry, cr);
230
231 /* Editor */
232 text_entry_draw(editor->editor, cr);
233
234 cairo_pop_group_to_source(cr);
235 cairo_paint(cr);
236
237 cairo_destroy(cr);
238 cairo_surface_destroy(surface);
239}
240
241static void
242text_entry_allocate(struct text_entry *entry, int32_t x, int32_t y,
243 int32_t width, int32_t height)
244{
245 entry->allocation.x = x;
246 entry->allocation.y = y;
247 entry->allocation.width = width;
248 entry->allocation.height = height;
249}
250
251static void
252resize_handler(struct widget *widget,
253 int32_t width, int32_t height, void *data)
254{
255 struct editor *editor = data;
256
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);
259}
260
261static int32_t
262rectangle_contains(struct rectangle *rectangle, int32_t x, int32_t y)
263{
264 if (x < rectangle->x || x > rectangle->x + rectangle->width) {
265 return 0;
266 }
267
268 if (y < rectangle->y || y > rectangle->y + rectangle->height) {
269 return 0;
270 }
271
272 return 1;
273}
274
275static void
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200276text_entry_activate(struct text_entry *entry,
277 struct wl_seat *seat)
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200278{
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200279 struct wl_surface *surface = window_get_wl_surface(entry->window);
280
281 text_model_activate(entry->model,
282 seat,
283 surface);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200284}
285
286static void
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200287text_entry_deactivate(struct text_entry *entry,
288 struct wl_seat *seat)
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200289{
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200290 text_model_deactivate(entry->model,
291 seat);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200292}
293
294static void
295button_handler(struct widget *widget,
296 struct input *input, uint32_t time,
297 uint32_t button,
298 enum wl_pointer_button_state state, void *data)
299{
300 struct editor *editor = data;
301 struct rectangle allocation;
302 int32_t x, y;
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200303 struct wl_seat *seat;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200304
305 if (state != WL_POINTER_BUTTON_STATE_PRESSED || button != BTN_LEFT) {
306 return;
307 }
308
309 input_get_position(input, &x, &y);
310
311 widget_get_allocation(editor->widget, &allocation);
312 x -= allocation.x;
313 y -= allocation.y;
314
Philipp Brüschweiler591cfca2012-07-11 22:25:29 +0200315 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));
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200318
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200319 seat = input_get_seat(input);
320
Philipp Brüschweiler591cfca2012-07-11 22:25:29 +0200321 if (activate_entry) {
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200322 text_entry_activate(editor->entry, seat);
Philipp Brüschweiler591cfca2012-07-11 22:25:29 +0200323 } else if (activate_editor) {
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200324 text_entry_activate(editor->editor, seat);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200325 } else {
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200326 text_entry_deactivate(editor->entry, seat);
327 text_entry_deactivate(editor->editor, seat);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200328 }
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200329
330 widget_schedule_redraw(widget);
331}
332
333static void
334global_handler(struct wl_display *display, uint32_t id,
335 const char *interface, uint32_t version, void *data)
336{
337 struct editor *editor = data;
338
Jan Arne Petersen51963742012-08-10 16:47:20 +0200339 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");
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200344 }
345}
346
347int
348main(int argc, char *argv[])
349{
350 struct editor editor;
351
352 editor.display = display_create(argc, argv);
353 if (editor.display == NULL) {
354 fprintf(stderr, "failed to create display: %m\n");
355 return -1;
356 }
357 wl_display_add_global_listener(display_get_display(editor.display),
358 global_handler, &editor);
359
360
361 editor.window = window_create(editor.display);
362 editor.widget = frame_create(editor.window, &editor);
363
364 editor.entry = text_entry_create(&editor, "Entry");
365 editor.editor = text_entry_create(&editor, "Editor");
366
367 window_set_title(editor.window, "Text Editor");
368
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);
372
373 window_schedule_resize(editor.window, 500, 400);
374
375 display_run(editor.display);
376
377 text_entry_destroy(editor.entry);
378 text_entry_destroy(editor.editor);
379
380 return 0;
381}