Extract the text_model_manager interface from input_method
[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         char *text;
37         int active;
38         struct rectangle allocation;
39         struct text_model *model;
40 };
41
42 struct editor {
43         struct text_model_manager *text_model_manager;
44         struct display *display;
45         struct window *window;
46         struct widget *widget;
47         struct text_entry *entry;
48         struct text_entry *editor;
49 };
50
51 static void
52 text_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
59 static void
60 text_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
72 static const struct text_model_listener text_model_listener = {
73         text_model_commit_string
74 };
75
76 static struct text_entry*
77 text_entry_create(struct editor *editor, const char *text)
78 {
79         struct text_entry *entry;
80         struct wl_surface *surface;
81
82         entry = malloc(sizeof *entry);
83
84         surface = window_get_wl_surface(editor->window);
85
86         entry->widget = editor->widget;
87         entry->text = strdup(text);
88         entry->active = 0;
89         entry->model = text_model_manager_create_text_model(editor->text_model_manager, surface);
90         text_model_add_listener(entry->model, &text_model_listener, entry);
91
92         return entry;
93 }
94
95 static void
96 text_entry_destroy(struct text_entry *entry)
97 {
98         text_model_destroy(entry->model);
99         free(entry->text);
100         free(entry);
101 }
102
103 static void
104 text_entry_draw(struct text_entry *entry, cairo_t *cr)
105 {
106         cairo_save(cr);
107         cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
108
109         cairo_rectangle(cr, entry->allocation.x, entry->allocation.y, entry->allocation.width, entry->allocation.height);
110         cairo_clip(cr);
111
112         cairo_translate(cr, entry->allocation.x, entry->allocation.y);
113         cairo_rectangle(cr, 0, 0, entry->allocation.width, entry->allocation.height);
114         cairo_set_source_rgba(cr, 1, 1, 1, 0.5);
115         cairo_fill(cr);
116         if (entry->active) {
117                 cairo_rectangle(cr, 0, 0, entry->allocation.width, entry->allocation.height);
118                 cairo_set_source_rgba(cr, 0, 0, 1, 0.5);
119                 cairo_stroke(cr);
120         }
121
122         cairo_set_source_rgb(cr, 0, 0, 0);
123         cairo_select_font_face(cr, "sans",
124                                CAIRO_FONT_SLANT_NORMAL,
125                                CAIRO_FONT_WEIGHT_BOLD);
126         cairo_set_font_size(cr, 14);
127
128         cairo_translate(cr, 10, entry->allocation.height / 2);
129         cairo_show_text(cr, entry->text);
130
131         cairo_restore(cr);
132 }
133
134 static void
135 redraw_handler(struct widget *widget, void *data)
136 {
137         struct editor *editor = data;
138         cairo_surface_t *surface;
139         struct rectangle allocation;
140         cairo_t *cr;
141
142         surface = window_get_surface(editor->window);
143         widget_get_allocation(editor->widget, &allocation);
144
145         cr = cairo_create(surface);
146         cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
147         cairo_clip(cr);
148
149         cairo_translate(cr, allocation.x, allocation.y);
150
151         /* Draw background */
152         cairo_push_group(cr);
153         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);  
154         cairo_set_source_rgba(cr, 1, 1, 1, 1);
155         cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
156         cairo_fill(cr);
157
158         /* Entry */
159         text_entry_draw(editor->entry, cr);
160
161         /* Editor */
162         text_entry_draw(editor->editor, cr);
163
164         cairo_pop_group_to_source(cr);
165         cairo_paint(cr);
166
167         cairo_destroy(cr);
168         cairo_surface_destroy(surface);
169 }
170
171 static void
172 text_entry_allocate(struct text_entry *entry, int32_t x, int32_t y,
173                     int32_t width, int32_t height)
174 {
175         entry->allocation.x = x;
176         entry->allocation.y = y;
177         entry->allocation.width = width;
178         entry->allocation.height = height;
179 }
180
181 static void
182 resize_handler(struct widget *widget,
183                int32_t width, int32_t height, void *data)
184 {
185         struct editor *editor = data;
186
187         text_entry_allocate(editor->entry, 20, 20, width - 40, height / 2 - 40);
188         text_entry_allocate(editor->editor, 20, height / 2 + 20, width - 40, height / 2 - 40);
189 }
190
191 static int32_t
192 rectangle_contains(struct rectangle *rectangle, int32_t x, int32_t y)
193 {
194         if (x < rectangle->x || x > rectangle->x + rectangle->width) {
195                 return 0;
196         }
197
198         if (y < rectangle->y || y > rectangle->y + rectangle->height) {
199                 return 0;
200         }
201
202         return 1;
203 }
204
205 static void
206 text_entry_activate(struct text_entry *entry)
207 {
208         text_model_activate(entry->model);
209 }
210
211 static void
212 text_entry_deactivate(struct text_entry *entry)
213 {
214         text_model_deactivate(entry->model);
215 }
216
217 static void
218 button_handler(struct widget *widget,
219                struct input *input, uint32_t time,
220                uint32_t button,
221                enum wl_pointer_button_state state, void *data)
222 {
223         struct editor *editor = data;
224         struct rectangle allocation;
225         int32_t x, y;
226
227         if (state != WL_POINTER_BUTTON_STATE_PRESSED || button != BTN_LEFT) {
228                 return;
229         }
230
231         input_get_position(input, &x, &y);
232
233         widget_get_allocation(editor->widget, &allocation);
234         x -= allocation.x;
235         y -= allocation.y;
236
237         int32_t activate_entry = rectangle_contains(&editor->entry->allocation, x, y);
238         int32_t activate_editor = rectangle_contains(&editor->editor->allocation, x, y);
239         assert(!(activate_entry && activate_editor));
240
241         if (activate_entry) {
242                 if (editor->editor->active)
243                         text_entry_deactivate(editor->editor);
244                 if (!editor->entry->active)
245                         text_entry_activate(editor->entry);
246         } else if (activate_editor) {
247                 if (editor->entry->active)
248                         text_entry_deactivate(editor->entry);
249                 if (!editor->editor->active)
250                         text_entry_activate(editor->editor);
251         } else {
252                 if (editor->entry->active)
253                         text_entry_deactivate(editor->entry);
254                 if (editor->editor->active)
255                         text_entry_deactivate(editor->editor);
256         }
257         editor->entry->active = activate_entry;
258         editor->editor->active = activate_editor;
259         assert(!(editor->entry->active && editor->editor->active));
260
261         widget_schedule_redraw(widget);
262 }
263
264 static void
265 global_handler(struct wl_display *display, uint32_t id,
266                const char *interface, uint32_t version, void *data)
267 {
268         struct editor *editor = data;
269
270         if (!strcmp(interface, "text_model_manager")) {
271                 editor->text_model_manager = wl_display_bind(display, id,
272                                                              &text_model_manager_interface);
273         }
274 }
275
276 int
277 main(int argc, char *argv[])
278 {
279         struct editor editor;
280
281         editor.display = display_create(argc, argv);
282         if (editor.display == NULL) {
283                 fprintf(stderr, "failed to create display: %m\n");
284                 return -1;
285         }
286         wl_display_add_global_listener(display_get_display(editor.display),
287                                        global_handler, &editor);
288
289
290         editor.window = window_create(editor.display);
291         editor.widget = frame_create(editor.window, &editor);
292
293         editor.entry = text_entry_create(&editor, "Entry");
294         editor.editor = text_entry_create(&editor, "Editor");
295
296         window_set_title(editor.window, "Text Editor");
297
298         widget_set_redraw_handler(editor.widget, redraw_handler);
299         widget_set_resize_handler(editor.widget, resize_handler);
300         widget_set_button_handler(editor.widget, button_handler);
301
302         window_schedule_resize(editor.window, 500, 400);
303
304         display_run(editor.display);
305
306         text_entry_destroy(editor.entry);
307         text_entry_destroy(editor.editor);
308
309         return 0;
310 }