blob: 346f363500838c98b1864641af92e0de9451585a [file] [log] [blame]
Jan Arne Petersencba9e472012-06-21 21:52:19 +02001/*
2 * Copyright © 2012 Openismus GmbH
Jan Arne Petersen4c265182012-09-09 23:08:30 +02003 * Copyright © 2012 Intel Corporation
Jan Arne Petersencba9e472012-06-21 21:52:19 +02004 *
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.
14 *
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.
22 */
23
Philipp Brüschweiler591cfca2012-07-11 22:25:29 +020024#include <assert.h>
Jan Arne Petersencba9e472012-06-21 21:52:19 +020025#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28
29#include <linux/input.h>
30#include <cairo.h>
31
32#include "window.h"
33#include "text-client-protocol.h"
34
35struct text_entry {
36 struct widget *widget;
Jan Arne Petersene829adc2012-08-10 16:47:22 +020037 struct window *window;
Jan Arne Petersencba9e472012-06-21 21:52:19 +020038 char *text;
39 int active;
Jan Arne Petersencba9e472012-06-21 21:52:19 +020040 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
Jan Arne Petersenf80bc062012-09-09 23:08:34 +020052static void text_entry_redraw_handler(struct widget *widget, void *data);
53static void text_entry_button_handler(struct widget *widget,
54 struct input *input, uint32_t time,
55 uint32_t button,
56 enum wl_pointer_button_state state, void *data);
57
Jan Arne Petersencba9e472012-06-21 21:52:19 +020058static void
59text_entry_append(struct text_entry *entry, const char *text)
60{
61 entry->text = realloc(entry->text, strlen(entry->text) + strlen(text) + 1);
62 strcat(entry->text, text);
63}
64
65
66static void
67text_model_commit_string(void *data,
68 struct text_model *text_model,
69 const char *text,
70 uint32_t index)
71{
72 struct text_entry *entry = data;
73
74 text_entry_append(entry, text);
75
76 widget_schedule_redraw(entry->widget);
77}
78
Jan Arne Petersen72f60822012-08-10 16:47:19 +020079static void
80text_model_preedit_string(void *data,
81 struct text_model *text_model,
82 const char *text,
83 uint32_t index)
84{
85}
86
87static void
88text_model_preedit_styling(void *data,
89 struct text_model *text_model)
90{
91}
92
93static void
94text_model_key(void *data,
95 struct text_model *text_model)
96{
97}
98
99static void
100text_model_selection_replacement(void *data,
101 struct text_model *text_model)
102{
103}
104
105static void
106text_model_direction(void *data,
107 struct text_model *text_model)
108{
109}
110
111static void
112text_model_locale(void *data,
113 struct text_model *text_model)
114{
115}
116
Jan Arne Petersende3b6a12012-08-10 16:47:21 +0200117static void
118text_model_activated(void *data,
119 struct text_model *text_model)
120{
121 struct text_entry *entry = data;
122
123 entry->active = 1;
124
125 widget_schedule_redraw(entry->widget);
126}
127
128static void
129text_model_deactivated(void *data,
130 struct text_model *text_model)
131{
132 struct text_entry *entry = data;
133
134 entry->active = 0;
135
136 widget_schedule_redraw(entry->widget);
137}
138
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200139static const struct text_model_listener text_model_listener = {
Jan Arne Petersen72f60822012-08-10 16:47:19 +0200140 text_model_commit_string,
141 text_model_preedit_string,
142 text_model_preedit_styling,
143 text_model_key,
144 text_model_selection_replacement,
145 text_model_direction,
Jan Arne Petersende3b6a12012-08-10 16:47:21 +0200146 text_model_locale,
147 text_model_activated,
148 text_model_deactivated
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200149};
150
151static struct text_entry*
152text_entry_create(struct editor *editor, const char *text)
153{
154 struct text_entry *entry;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200155
156 entry = malloc(sizeof *entry);
157
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200158 entry->widget = widget_add_widget(editor->widget, entry);
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200159 entry->window = editor->window;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200160 entry->text = strdup(text);
161 entry->active = 0;
Jan Arne Petersen4c265182012-09-09 23:08:30 +0200162 entry->model = text_model_factory_create_text_model(editor->text_model_factory);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200163 text_model_add_listener(entry->model, &text_model_listener, entry);
164
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200165 widget_set_redraw_handler(entry->widget, text_entry_redraw_handler);
166 widget_set_button_handler(entry->widget, text_entry_button_handler);
167
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200168 return entry;
169}
170
171static void
172text_entry_destroy(struct text_entry *entry)
173{
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200174 widget_destroy(entry->widget);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200175 text_model_destroy(entry->model);
176 free(entry->text);
177 free(entry);
178}
179
180static void
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200181redraw_handler(struct widget *widget, void *data)
182{
183 struct editor *editor = data;
184 cairo_surface_t *surface;
185 struct rectangle allocation;
186 cairo_t *cr;
187
188 surface = window_get_surface(editor->window);
189 widget_get_allocation(editor->widget, &allocation);
190
191 cr = cairo_create(surface);
192 cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
193 cairo_clip(cr);
194
195 cairo_translate(cr, allocation.x, allocation.y);
196
197 /* Draw background */
198 cairo_push_group(cr);
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200199 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200200 cairo_set_source_rgba(cr, 1, 1, 1, 1);
201 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
202 cairo_fill(cr);
203
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200204 cairo_pop_group_to_source(cr);
205 cairo_paint(cr);
206
207 cairo_destroy(cr);
208 cairo_surface_destroy(surface);
209}
210
211static void
212text_entry_allocate(struct text_entry *entry, int32_t x, int32_t y,
213 int32_t width, int32_t height)
214{
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200215 widget_set_allocation(entry->widget, x, y, width, height);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200216}
217
218static void
219resize_handler(struct widget *widget,
220 int32_t width, int32_t height, void *data)
221{
222 struct editor *editor = data;
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200223 struct rectangle allocation;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200224
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200225 widget_get_allocation(editor->widget, &allocation);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200226
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200227 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);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200233}
234
235static void
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200236text_entry_activate(struct text_entry *entry,
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200237 struct wl_seat *seat)
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200238{
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200239 struct wl_surface *surface = window_get_wl_surface(entry->window);
240
241 text_model_activate(entry->model,
242 seat,
243 surface);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200244}
245
246static void
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200247text_entry_deactivate(struct text_entry *entry,
248 struct wl_seat *seat)
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200249{
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200250 text_model_deactivate(entry->model,
251 seat);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200252}
253
254static void
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200255text_entry_redraw_handler(struct widget *widget, void *data)
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200256{
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200257 struct text_entry *entry = data;
258 cairo_surface_t *surface;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200259 struct rectangle allocation;
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200260 cairo_t *cr;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200261
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200262 surface = window_get_surface(entry->window);
263 widget_get_allocation(entry->widget, &allocation);
264
265 cr = cairo_create(surface);
266 cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
267 cairo_clip(cr);
268
269 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
270
271 cairo_push_group(cr);
272 cairo_translate(cr, allocation.x, allocation.y);
273
274 cairo_set_source_rgba(cr, 1, 1, 1, 1);
275 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
276 cairo_fill(cr);
277
278 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
279
280 if (entry->active) {
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);
284 cairo_stroke(cr);
285 }
286
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);
292
293 cairo_translate(cr, 10, allocation.height / 2);
294 cairo_show_text(cr, entry->text);
295 cairo_pop_group_to_source(cr);
296 cairo_paint(cr);
297
298 cairo_destroy(cr);
299 cairo_surface_destroy(surface);
300}
301
302static void
303text_entry_button_handler(struct widget *widget,
304 struct input *input, uint32_t time,
305 uint32_t button,
306 enum wl_pointer_button_state state, void *data)
307{
308 struct text_entry *entry = data;
309
310 if (button != BTN_LEFT) {
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200311 return;
312 }
313
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200314 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
315 struct wl_seat *seat = input_get_seat(input);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200316
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200317 text_entry_activate(entry, seat);
318 }
319}
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200320
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200321static void
322editor_button_handler(struct widget *widget,
323 struct input *input, uint32_t time,
324 uint32_t button,
325 enum wl_pointer_button_state state, void *data)
326{
327 struct editor *editor = data;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200328
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200329 if (button != BTN_LEFT) {
330 return;
331 }
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200332
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200333 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
334 struct wl_seat *seat = input_get_seat(input);
335
Jan Arne Petersene829adc2012-08-10 16:47:22 +0200336 text_entry_deactivate(editor->entry, seat);
337 text_entry_deactivate(editor->editor, seat);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200338 }
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200339}
340
341static void
342global_handler(struct wl_display *display, uint32_t id,
343 const char *interface, uint32_t version, void *data)
344{
345 struct editor *editor = data;
346
Jan Arne Petersen51963742012-08-10 16:47:20 +0200347 if (!strcmp(interface, "text_model_factory")) {
348 editor->text_model_factory = wl_display_bind(display, id,
349 &text_model_factory_interface);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200350 }
351}
352
353int
354main(int argc, char *argv[])
355{
356 struct editor editor;
357
358 editor.display = display_create(argc, argv);
359 if (editor.display == NULL) {
360 fprintf(stderr, "failed to create display: %m\n");
361 return -1;
362 }
363 wl_display_add_global_listener(display_get_display(editor.display),
364 global_handler, &editor);
365
366
367 editor.window = window_create(editor.display);
368 editor.widget = frame_create(editor.window, &editor);
369
370 editor.entry = text_entry_create(&editor, "Entry");
371 editor.editor = text_entry_create(&editor, "Editor");
372
373 window_set_title(editor.window, "Text Editor");
374
375 widget_set_redraw_handler(editor.widget, redraw_handler);
376 widget_set_resize_handler(editor.widget, resize_handler);
Jan Arne Petersenf80bc062012-09-09 23:08:34 +0200377 widget_set_button_handler(editor.widget, editor_button_handler);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200378
379 window_schedule_resize(editor.window, 500, 400);
380
381 display_run(editor.display);
382
383 text_entry_destroy(editor.entry);
384 text_entry_destroy(editor.editor);
385
386 return 0;
387}