blob: 9498d53bc3866cf30f65740e0be23e2d5609afcd [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 {
43 struct input_method *input_method;
44 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
72static const struct text_model_listener text_model_listener = {
73 text_model_commit_string
74};
75
76static struct text_entry*
77text_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 = input_method_create_text_model(editor->input_method, surface);
90 text_model_add_listener(entry->model, &text_model_listener, entry);
91
92 return entry;
93}
94
95static void
96text_entry_destroy(struct text_entry *entry)
97{
98 text_model_destroy(entry->model);
99 free(entry->text);
100 free(entry);
101}
102
103static void
104text_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
134static void
135redraw_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
171static void
172text_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
181static void
182resize_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
191static int32_t
192rectangle_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
205static void
206text_entry_activate(struct text_entry *entry)
207{
208 text_model_activate(entry->model);
209}
210
211static void
212text_entry_deactivate(struct text_entry *entry)
213{
214 text_model_deactivate(entry->model);
215}
216
217static void
218button_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
Philipp Brüschweiler591cfca2012-07-11 22:25:29 +0200237 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));
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200240
Philipp Brüschweiler591cfca2012-07-11 22:25:29 +0200241 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);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200251 } else {
Philipp Brüschweiler591cfca2012-07-11 22:25:29 +0200252 if (editor->entry->active)
253 text_entry_deactivate(editor->entry);
254 if (editor->editor->active)
255 text_entry_deactivate(editor->editor);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200256 }
Philipp Brüschweiler591cfca2012-07-11 22:25:29 +0200257 editor->entry->active = activate_entry;
258 editor->editor->active = activate_editor;
259 assert(!(editor->entry->active && editor->editor->active));
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200260
261 widget_schedule_redraw(widget);
262}
263
264static void
265global_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, "input_method")) {
271 editor->input_method = wl_display_bind(display, id, &input_method_interface);
272 }
273}
274
275int
276main(int argc, char *argv[])
277{
278 struct editor editor;
279
280 editor.display = display_create(argc, argv);
281 if (editor.display == NULL) {
282 fprintf(stderr, "failed to create display: %m\n");
283 return -1;
284 }
285 wl_display_add_global_listener(display_get_display(editor.display),
286 global_handler, &editor);
287
288
289 editor.window = window_create(editor.display);
290 editor.widget = frame_create(editor.window, &editor);
291
292 editor.entry = text_entry_create(&editor, "Entry");
293 editor.editor = text_entry_create(&editor, "Editor");
294
295 window_set_title(editor.window, "Text Editor");
296
297 widget_set_redraw_handler(editor.widget, redraw_handler);
298 widget_set_resize_handler(editor.widget, resize_handler);
299 widget_set_button_handler(editor.widget, button_handler);
300
301 window_schedule_resize(editor.window, 500, 400);
302
303 display_run(editor.display);
304
305 text_entry_destroy(editor.entry);
306 text_entry_destroy(editor.editor);
307
308 return 0;
309}