blob: 392edda41c2888cedbfcd69a2f9929ccea07e24c [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 {
Philipp Brüschweilerf25602b2012-07-11 22:25:31 +020043 struct text_model_manager *text_model_manager;
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 Petersencba9e472012-06-21 21:52:19 +0200110static const struct text_model_listener text_model_listener = {
Jan Arne Petersen72f60822012-08-10 16:47:19 +0200111 text_model_commit_string,
112 text_model_preedit_string,
113 text_model_preedit_styling,
114 text_model_key,
115 text_model_selection_replacement,
116 text_model_direction,
117 text_model_locale
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200118};
119
120static struct text_entry*
121text_entry_create(struct editor *editor, const char *text)
122{
123 struct text_entry *entry;
124 struct wl_surface *surface;
125
126 entry = malloc(sizeof *entry);
127
128 surface = window_get_wl_surface(editor->window);
129
130 entry->widget = editor->widget;
131 entry->text = strdup(text);
132 entry->active = 0;
Philipp Brüschweilerf25602b2012-07-11 22:25:31 +0200133 entry->model = text_model_manager_create_text_model(editor->text_model_manager, surface);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200134 text_model_add_listener(entry->model, &text_model_listener, entry);
135
136 return entry;
137}
138
139static void
140text_entry_destroy(struct text_entry *entry)
141{
142 text_model_destroy(entry->model);
143 free(entry->text);
144 free(entry);
145}
146
147static void
148text_entry_draw(struct text_entry *entry, cairo_t *cr)
149{
150 cairo_save(cr);
151 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
152
153 cairo_rectangle(cr, entry->allocation.x, entry->allocation.y, entry->allocation.width, entry->allocation.height);
154 cairo_clip(cr);
155
156 cairo_translate(cr, entry->allocation.x, entry->allocation.y);
157 cairo_rectangle(cr, 0, 0, entry->allocation.width, entry->allocation.height);
158 cairo_set_source_rgba(cr, 1, 1, 1, 0.5);
159 cairo_fill(cr);
160 if (entry->active) {
161 cairo_rectangle(cr, 0, 0, entry->allocation.width, entry->allocation.height);
162 cairo_set_source_rgba(cr, 0, 0, 1, 0.5);
163 cairo_stroke(cr);
164 }
165
166 cairo_set_source_rgb(cr, 0, 0, 0);
167 cairo_select_font_face(cr, "sans",
168 CAIRO_FONT_SLANT_NORMAL,
169 CAIRO_FONT_WEIGHT_BOLD);
170 cairo_set_font_size(cr, 14);
171
172 cairo_translate(cr, 10, entry->allocation.height / 2);
173 cairo_show_text(cr, entry->text);
174
175 cairo_restore(cr);
176}
177
178static void
179redraw_handler(struct widget *widget, void *data)
180{
181 struct editor *editor = data;
182 cairo_surface_t *surface;
183 struct rectangle allocation;
184 cairo_t *cr;
185
186 surface = window_get_surface(editor->window);
187 widget_get_allocation(editor->widget, &allocation);
188
189 cr = cairo_create(surface);
190 cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
191 cairo_clip(cr);
192
193 cairo_translate(cr, allocation.x, allocation.y);
194
195 /* Draw background */
196 cairo_push_group(cr);
197 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
198 cairo_set_source_rgba(cr, 1, 1, 1, 1);
199 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
200 cairo_fill(cr);
201
202 /* Entry */
203 text_entry_draw(editor->entry, cr);
204
205 /* Editor */
206 text_entry_draw(editor->editor, cr);
207
208 cairo_pop_group_to_source(cr);
209 cairo_paint(cr);
210
211 cairo_destroy(cr);
212 cairo_surface_destroy(surface);
213}
214
215static void
216text_entry_allocate(struct text_entry *entry, int32_t x, int32_t y,
217 int32_t width, int32_t height)
218{
219 entry->allocation.x = x;
220 entry->allocation.y = y;
221 entry->allocation.width = width;
222 entry->allocation.height = height;
223}
224
225static void
226resize_handler(struct widget *widget,
227 int32_t width, int32_t height, void *data)
228{
229 struct editor *editor = data;
230
231 text_entry_allocate(editor->entry, 20, 20, width - 40, height / 2 - 40);
232 text_entry_allocate(editor->editor, 20, height / 2 + 20, width - 40, height / 2 - 40);
233}
234
235static int32_t
236rectangle_contains(struct rectangle *rectangle, int32_t x, int32_t y)
237{
238 if (x < rectangle->x || x > rectangle->x + rectangle->width) {
239 return 0;
240 }
241
242 if (y < rectangle->y || y > rectangle->y + rectangle->height) {
243 return 0;
244 }
245
246 return 1;
247}
248
249static void
250text_entry_activate(struct text_entry *entry)
251{
252 text_model_activate(entry->model);
253}
254
255static void
256text_entry_deactivate(struct text_entry *entry)
257{
258 text_model_deactivate(entry->model);
259}
260
261static void
262button_handler(struct widget *widget,
263 struct input *input, uint32_t time,
264 uint32_t button,
265 enum wl_pointer_button_state state, void *data)
266{
267 struct editor *editor = data;
268 struct rectangle allocation;
269 int32_t x, y;
270
271 if (state != WL_POINTER_BUTTON_STATE_PRESSED || button != BTN_LEFT) {
272 return;
273 }
274
275 input_get_position(input, &x, &y);
276
277 widget_get_allocation(editor->widget, &allocation);
278 x -= allocation.x;
279 y -= allocation.y;
280
Philipp Brüschweiler591cfca2012-07-11 22:25:29 +0200281 int32_t activate_entry = rectangle_contains(&editor->entry->allocation, x, y);
282 int32_t activate_editor = rectangle_contains(&editor->editor->allocation, x, y);
283 assert(!(activate_entry && activate_editor));
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200284
Philipp Brüschweiler591cfca2012-07-11 22:25:29 +0200285 if (activate_entry) {
286 if (editor->editor->active)
287 text_entry_deactivate(editor->editor);
288 if (!editor->entry->active)
289 text_entry_activate(editor->entry);
290 } else if (activate_editor) {
291 if (editor->entry->active)
292 text_entry_deactivate(editor->entry);
293 if (!editor->editor->active)
294 text_entry_activate(editor->editor);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200295 } else {
Philipp Brüschweiler591cfca2012-07-11 22:25:29 +0200296 if (editor->entry->active)
297 text_entry_deactivate(editor->entry);
298 if (editor->editor->active)
299 text_entry_deactivate(editor->editor);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200300 }
Philipp Brüschweiler591cfca2012-07-11 22:25:29 +0200301 editor->entry->active = activate_entry;
302 editor->editor->active = activate_editor;
303 assert(!(editor->entry->active && editor->editor->active));
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200304
305 widget_schedule_redraw(widget);
306}
307
308static void
309global_handler(struct wl_display *display, uint32_t id,
310 const char *interface, uint32_t version, void *data)
311{
312 struct editor *editor = data;
313
Philipp Brüschweilerf25602b2012-07-11 22:25:31 +0200314 if (!strcmp(interface, "text_model_manager")) {
315 editor->text_model_manager = wl_display_bind(display, id,
316 &text_model_manager_interface);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200317 }
318}
319
320int
321main(int argc, char *argv[])
322{
323 struct editor editor;
324
325 editor.display = display_create(argc, argv);
326 if (editor.display == NULL) {
327 fprintf(stderr, "failed to create display: %m\n");
328 return -1;
329 }
330 wl_display_add_global_listener(display_get_display(editor.display),
331 global_handler, &editor);
332
333
334 editor.window = window_create(editor.display);
335 editor.widget = frame_create(editor.window, &editor);
336
337 editor.entry = text_entry_create(&editor, "Entry");
338 editor.editor = text_entry_create(&editor, "Editor");
339
340 window_set_title(editor.window, "Text Editor");
341
342 widget_set_redraw_handler(editor.widget, redraw_handler);
343 widget_set_resize_handler(editor.widget, resize_handler);
344 widget_set_button_handler(editor.widget, button_handler);
345
346 window_schedule_resize(editor.window, 500, 400);
347
348 display_run(editor.display);
349
350 text_entry_destroy(editor.entry);
351 text_entry_destroy(editor.editor);
352
353 return 0;
354}