text: Assign text_model to a wl_seat
[profile/ivi/weston.git] / clients / editor.c
1 /*
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 <assert.h>
24 #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
34 struct text_entry {
35         struct widget *widget;
36         struct window *window;
37         char *text;
38         int active;
39         struct rectangle allocation;
40         struct text_model *model;
41 };
42
43 struct editor {
44         struct text_model_factory *text_model_factory;
45         struct display *display;
46         struct window *window;
47         struct widget *widget;
48         struct text_entry *entry;
49         struct text_entry *editor;
50 };
51
52 static void
53 text_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
60 static void
61 text_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
73 static void
74 text_model_preedit_string(void *data,
75                           struct text_model *text_model,
76                           const char *text,
77                           uint32_t index)
78 {
79 }
80
81 static void
82 text_model_preedit_styling(void *data,
83                            struct text_model *text_model)
84 {
85 }
86
87 static void
88 text_model_key(void *data,
89                struct text_model *text_model)
90 {
91 }
92
93 static void
94 text_model_selection_replacement(void *data,
95                                  struct text_model *text_model)
96 {
97 }
98
99 static void
100 text_model_direction(void *data,
101                      struct text_model *text_model)
102 {
103 }
104
105 static void
106 text_model_locale(void *data,
107                   struct text_model *text_model)
108 {
109 }
110
111 static void
112 text_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
122 static void
123 text_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
133 static const struct text_model_listener text_model_listener = {
134         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,
140         text_model_locale,
141         text_model_activated,
142         text_model_deactivated
143 };
144
145 static struct text_entry*
146 text_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;
156         entry->window = editor->window;
157         entry->text = strdup(text);
158         entry->active = 0;
159         entry->model = text_model_factory_create_text_model(editor->text_model_factory, surface);
160         text_model_add_listener(entry->model, &text_model_listener, entry);
161
162         return entry;
163 }
164
165 static void
166 text_entry_destroy(struct text_entry *entry)
167 {
168         text_model_destroy(entry->model);
169         free(entry->text);
170         free(entry);
171 }
172
173 static void
174 text_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
204 static void
205 redraw_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
241 static void
242 text_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
251 static void
252 resize_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
261 static int32_t
262 rectangle_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
275 static void
276 text_entry_activate(struct text_entry *entry,
277                     struct wl_seat *seat)
278 {
279         struct wl_surface *surface = window_get_wl_surface(entry->window);
280
281         text_model_activate(entry->model,
282                             seat,
283                             surface);
284 }
285
286 static void
287 text_entry_deactivate(struct text_entry *entry,
288                       struct wl_seat *seat)
289 {
290         text_model_deactivate(entry->model,
291                               seat);
292 }
293
294 static void
295 button_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;
303         struct wl_seat *seat;
304
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
315         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));
318
319         seat = input_get_seat(input);
320
321         if (activate_entry) {
322                 text_entry_activate(editor->entry, seat);
323         } else if (activate_editor) {
324                 text_entry_activate(editor->editor, seat);
325         } else {
326                 text_entry_deactivate(editor->entry, seat);
327                 text_entry_deactivate(editor->editor, seat);
328         }
329
330         widget_schedule_redraw(widget);
331 }
332
333 static void
334 global_handler(struct wl_display *display, uint32_t id,
335                const char *interface, uint32_t version, void *data)
336 {
337         struct editor *editor = data;
338
339         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");
344         }
345 }
346
347 int
348 main(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 }