1 /*
2  * Copyright 2010 Vincent Sanders <vince@simtec.co.uk>
3  *
4  * Framebuffer windowing toolkit scrollbar widgets.
5  *
6  * This file is part of NetSurf, http://www.netsurf-browser.org/
7  *
8  * NetSurf is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * NetSurf is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include <stdbool.h>
22 #include <stdlib.h>
23 #include <string.h>
24 
25 #include <libnsfb.h>
26 #include <libnsfb_plot.h>
27 #include <libnsfb_plot_util.h>
28 #include <libnsfb_event.h>
29 
30 #include "utils/log.h"
31 #include "netsurf/browser_window.h"
32 #include "netsurf/plotters.h"
33 
34 #include "framebuffer/gui.h"
35 #include "framebuffer/fbtk.h"
36 #include "framebuffer/font.h"
37 #include "framebuffer/framebuffer.h"
38 #include "framebuffer/image_data.h"
39 
40 #include "widget.h"
41 
42 //#define TEXT_WIDGET_BORDER 3 /**< The pixel border round a text widget. */
43 
44 /* Lighten a colour by taking seven eights of each channel's intensity
45  * and adding a full eighth
46  */
47 #define brighten_colour(c1)					\
48 	(((((7 * ((c1 >> 16) & 0xff)) >> 3) + 32) << 16) |	\
49 	 ((((7 * ((c1 >> 8) & 0xff)) >> 3) + 32) << 8) |	\
50 	 ((((7 * (c1 & 0xff)) >> 3) + 32) << 0))
51 
52 /* Convert pixels to points, assuming a DPI of 90 */
53 #define px_to_pt(x) (((x) * 72) / FBTK_DPI)
54 
55 /* Get a font style for a text input */
56 static inline void
fb_text_font_style(fbtk_widget_t * widget,int * font_height,int * padding,plot_font_style_t * font_style)57 fb_text_font_style(fbtk_widget_t *widget, int *font_height, int *padding,
58 		plot_font_style_t *font_style)
59 {
60 	if (widget->u.text.outline)
61 		*padding = 1;
62 	else
63 		*padding = 0;
64 
65 #ifdef FB_USE_FREETYPE
66 	*padding += widget->height / 6;
67 	*font_height = widget->height - *padding - *padding;
68 #else
69 	*font_height = FB_FONT_HEIGHT;
70 	*padding = (widget->height - *padding - *font_height) / 2;
71 #endif
72 
73 	font_style->family = PLOT_FONT_FAMILY_SANS_SERIF;
74 	font_style->size = px_to_pt(*font_height * PLOT_STYLE_SCALE);
75 	font_style->weight = 400;
76 	font_style->flags = FONTF_NONE;
77 	font_style->background = widget->bg;
78 	font_style->foreground = widget->fg;
79 }
80 
81 /** Text redraw callback.
82  *
83  * Called when a text widget requires redrawing.
84  *
85  * @param widget The widget to be redrawn.
86  * @param cbi The callback parameters.
87  * @return The callback result.
88  */
89 static int
fb_redraw_text(fbtk_widget_t * widget,fbtk_callback_info * cbi)90 fb_redraw_text(fbtk_widget_t *widget, fbtk_callback_info *cbi )
91 {
92 	nsfb_bbox_t bbox;
93 	nsfb_bbox_t rect;
94 	fbtk_widget_t *root;
95 	plot_font_style_t font_style;
96 	int caret_x, caret_y, caret_h;
97 	int fh;
98 	int padding;
99 	int scroll = 0;
100 	bool caret = false;
101 	struct redraw_context ctx = {
102 		.interactive = true,
103 		.background_images = true,
104 		.plot = &fb_plotters
105 	};
106 
107 	fb_text_font_style(widget, &fh, &padding, &font_style);
108 
109 	if (fbtk_get_caret(widget, &caret_x, &caret_y, &caret_h)) {
110 		caret = true;
111 	}
112 
113 	root = fbtk_get_root_widget(widget);
114 
115 	fbtk_get_bbox(widget, &bbox);
116 
117 	rect = bbox;
118 
119 	nsfb_claim(root->u.root.fb, &bbox);
120 
121 	/* clear background */
122 	if ((widget->bg & 0xFF000000) != 0) {
123 		/* transparent polygon filling isnt working so fake it */
124 		nsfb_plot_rectangle_fill(root->u.root.fb, &bbox, widget->bg);
125 	}
126 
127 	/* widget can have a single pixel outline border */
128 	if (widget->u.text.outline) {
129 		rect.x1--;
130 		rect.y1--;
131 		nsfb_plot_rectangle(root->u.root.fb, &rect, 1,
132 				0x00000000, false, false);
133 	}
134 
135 	if (widget->u.text.text != NULL) {
136 		int x = bbox.x0 + padding;
137 		int y = bbox.y0 + ((fh * 3 + 2) / 4) + padding;
138 
139 #ifdef FB_USE_FREETYPE
140 		/* Freetype renders text higher */
141 		y += 1;
142 #endif
143 
144 		if (caret && widget->width - padding - padding < caret_x) {
145 			scroll = (widget->width - padding - padding) - caret_x;
146 			x +=  scroll;
147 		}
148 
149 		/* Call the fb text plotting, baseline is 3/4 down the font */
150 		ctx.plot->text(&ctx,
151 			       &font_style,
152 			       x, y,
153 			       widget->u.text.text,
154 			       widget->u.text.len);
155 	}
156 
157 	if (caret) {
158 		/* This widget has caret, so render it */
159 		nsfb_t *nsfb = fbtk_get_nsfb(widget);
160 		nsfb_bbox_t line;
161 		nsfb_plot_pen_t pen;
162 
163 		line.x0 = bbox.x0 + caret_x + scroll;
164 		line.y0 = bbox.y0 + caret_y;
165 		line.x1 = bbox.x0 + caret_x + scroll;
166 		line.y1 = bbox.y0 + caret_y + caret_h;
167 
168 		pen.stroke_type = NFSB_PLOT_OPTYPE_SOLID;
169 		pen.stroke_width = 1;
170 		pen.stroke_colour = 0xFF0000FF;
171 
172 		nsfb_plot_line(nsfb, &line, &pen);
173 	}
174 
175 	nsfb_update(root->u.root.fb, &bbox);
176 
177 	return 0;
178 }
179 
180 /** Text destroy callback.
181  *
182  * Called when a text widget is destroyed.
183  *
184  * @param widget The widget being destroyed.
185  * @param cbi The callback parameters.
186  * @return The callback result.
187  */
fb_destroy_text(fbtk_widget_t * widget,fbtk_callback_info * cbi)188 static int fb_destroy_text(fbtk_widget_t *widget, fbtk_callback_info *cbi)
189 {
190 	if ((widget == NULL) || (widget->type != FB_WIDGET_TYPE_TEXT)) {
191 		return 0;
192 	}
193 
194 	if (widget->u.text.text != NULL) {
195 		free(widget->u.text.text);
196 	}
197 
198 	return 0;
199 }
200 
201 /** Text button redraw callback.
202  *
203  * Called when a text widget requires redrawing.
204  *
205  * @param widget The widget to be redrawn.
206  * @param cbi The callback parameters.
207  * @return The callback result.
208  */
209 static int
fb_redraw_text_button(fbtk_widget_t * widget,fbtk_callback_info * cbi)210 fb_redraw_text_button(fbtk_widget_t *widget, fbtk_callback_info *cbi )
211 {
212 	nsfb_bbox_t bbox;
213 	nsfb_bbox_t rect;
214 	nsfb_bbox_t line;
215 	nsfb_plot_pen_t pen;
216 	plot_font_style_t font_style;
217 	int fh;
218 	int border;
219 	fbtk_widget_t *root = fbtk_get_root_widget(widget);
220 	struct redraw_context ctx = {
221 		.interactive = true,
222 		.background_images = true,
223 		.plot = &fb_plotters
224 	};
225 
226 	fb_text_font_style(widget, &fh, &border, &font_style);
227 
228 	pen.stroke_type = NFSB_PLOT_OPTYPE_SOLID;
229 	pen.stroke_width = 1;
230 	pen.stroke_colour = brighten_colour(widget->bg);
231 
232 	fbtk_get_bbox(widget, &bbox);
233 
234 	rect = bbox;
235 	rect.x1--;
236 	rect.y1--;
237 
238 	nsfb_claim(root->u.root.fb, &bbox);
239 
240 	/* clear background */
241 	if ((widget->bg & 0xFF000000) != 0) {
242 		/* transparent polygon filling isnt working so fake it */
243 		nsfb_plot_rectangle_fill(root->u.root.fb, &rect, widget->bg);
244 	}
245 
246 	if (widget->u.text.outline) {
247 		line.x0 = rect.x0;
248 		line.y0 = rect.y0;
249 		line.x1 = rect.x0;
250 		line.y1 = rect.y1;
251 		nsfb_plot_line(root->u.root.fb, &line, &pen);
252 		line.x0 = rect.x0;
253 		line.y0 = rect.y0;
254 		line.x1 = rect.x1;
255 		line.y1 = rect.y0;
256 		nsfb_plot_line(root->u.root.fb, &line, &pen);
257 		pen.stroke_colour = darken_colour(widget->bg);
258 		line.x0 = rect.x0;
259 		line.y0 = rect.y1;
260 		line.x1 = rect.x1;
261 		line.y1 = rect.y1;
262 		nsfb_plot_line(root->u.root.fb, &line, &pen);
263 		line.x0 = rect.x1;
264 		line.y0 = rect.y0;
265 		line.x1 = rect.x1;
266 		line.y1 = rect.y1;
267 		nsfb_plot_line(root->u.root.fb, &line, &pen);
268 	}
269 
270 	if (widget->u.text.text != NULL) {
271 		/* Call the fb text plotting, baseline is 3/4 down the font */
272 		ctx.plot->text(&ctx,
273 			       &font_style,
274 			       bbox.x0 + border,
275 			       bbox.y0 + ((fh * 3) / 4) + border,
276 			       widget->u.text.text,
277 			       widget->u.text.len);
278 	}
279 
280 	nsfb_update(root->u.root.fb, &bbox);
281 
282 	return 0;
283 }
284 
285 static void
fb_text_input_remove_caret_cb(fbtk_widget_t * widget)286 fb_text_input_remove_caret_cb(fbtk_widget_t *widget)
287 {
288 	int c_x, c_y, c_h;
289 
290 	if (fbtk_get_caret(widget, &c_x, &c_y, &c_h)) {
291 		fbtk_request_redraw(widget);
292 	}
293 }
294 
295 /** Routine called when text events occour in writeable widget.
296  *
297  * @param widget The widget reciving input events.
298  * @param cbi The callback parameters.
299  * @return The callback result.
300  */
301 static int
text_input(fbtk_widget_t * widget,fbtk_callback_info * cbi)302 text_input(fbtk_widget_t *widget, fbtk_callback_info *cbi)
303 {
304 	int value;
305 	static fbtk_modifier_type modifier = FBTK_MOD_CLEAR;
306 	char *temp;
307 	plot_font_style_t font_style;
308 	int fh;
309 	int border;
310 	bool caret_moved = false;
311 
312 	fb_text_font_style(widget, &fh, &border, &font_style);
313 
314 	if (cbi->event == NULL) {
315 		/* gain focus */
316 		if (widget->u.text.text == NULL)
317 			widget->u.text.text = calloc(1,1);
318 
319 		return 0;
320 	}
321 
322 	value = cbi->event->value.keycode;
323 
324 	if (cbi->event->type != NSFB_EVENT_KEY_DOWN) {
325 		switch (value) {
326 		case NSFB_KEY_RSHIFT:
327 			modifier &= ~FBTK_MOD_RSHIFT;
328 			break;
329 
330 		case NSFB_KEY_LSHIFT:
331 			modifier &= ~FBTK_MOD_LSHIFT;
332 			break;
333 
334 		case NSFB_KEY_RCTRL:
335 			modifier &= ~FBTK_MOD_RCTRL;
336 			break;
337 
338 		case NSFB_KEY_LCTRL:
339 			modifier &= ~FBTK_MOD_LCTRL;
340 			break;
341 
342 		default:
343 			break;
344 		}
345 		return 0;
346 	}
347 
348 	switch (value) {
349 	case NSFB_KEY_BACKSPACE:
350 		if (widget->u.text.idx <= 0)
351 			break;
352 		memmove(widget->u.text.text + widget->u.text.idx - 1,
353 				widget->u.text.text + widget->u.text.idx,
354 				widget->u.text.len - widget->u.text.idx);
355 		widget->u.text.idx--;
356 		widget->u.text.len--;
357 		widget->u.text.text[widget->u.text.len] = 0;
358 
359 		fb_font_width(&font_style, widget->u.text.text,
360 				widget->u.text.len, &widget->u.text.width);
361 
362 		caret_moved = true;
363 		break;
364 
365 	case NSFB_KEY_RETURN:
366 		widget->u.text.enter(widget->u.text.pw, widget->u.text.text);
367 		break;
368 
369 	case NSFB_KEY_RIGHT:
370 		if (widget->u.text.idx < widget->u.text.len) {
371 			if (modifier == FBTK_MOD_CLEAR)
372 				widget->u.text.idx++;
373 			else
374 				widget->u.text.idx = widget->u.text.len;
375 
376 			caret_moved = true;
377 		}
378 		break;
379 
380 	case NSFB_KEY_LEFT:
381 		if (widget->u.text.idx > 0) {
382 			if (modifier == FBTK_MOD_CLEAR)
383 				widget->u.text.idx--;
384 			else
385 				widget->u.text.idx = 0;
386 
387 			caret_moved = true;
388 		}
389 		break;
390 
391 	case NSFB_KEY_HOME:
392 		if (widget->u.text.idx > 0) {
393 			widget->u.text.idx = 0;
394 
395 			caret_moved = true;
396 		}
397 		break;
398 
399 	case NSFB_KEY_END:
400 		if (widget->u.text.idx < widget->u.text.len) {
401 			widget->u.text.idx = widget->u.text.len;
402 
403 			caret_moved = true;
404 		}
405 		break;
406 
407 	case NSFB_KEY_PAGEUP:
408 	case NSFB_KEY_PAGEDOWN:
409 	case NSFB_KEY_UP:
410 	case NSFB_KEY_DOWN:
411 		/* Not handling any of these correctly yet, but avoid putting
412 		 * charcters in the text widget when they're pressed. */
413 		break;
414 
415 	case NSFB_KEY_RSHIFT:
416 		modifier |= FBTK_MOD_RSHIFT;
417 		break;
418 
419 	case NSFB_KEY_LSHIFT:
420 		modifier |= FBTK_MOD_LSHIFT;
421 		break;
422 
423 	case NSFB_KEY_RCTRL:
424 		modifier |= FBTK_MOD_RCTRL;
425 		break;
426 
427 	case NSFB_KEY_LCTRL:
428 		modifier |= FBTK_MOD_LCTRL;
429 		break;
430 
431 	default:
432 		if (modifier & FBTK_MOD_LCTRL || modifier & FBTK_MOD_RCTRL) {
433 			/* CTRL pressed, don't enter any text */
434 			if (value == NSFB_KEY_u) {
435 				/* CTRL+U: clear writable */
436 				widget->u.text.idx = 0;
437 				widget->u.text.len = 0;
438 				widget->u.text.text[widget->u.text.len] = '\0';
439 				widget->u.text.width = 0;
440 				caret_moved = true;
441 			}
442 			break;
443 		}
444 
445 		/* allow for new character and null */
446 		temp = realloc(widget->u.text.text, widget->u.text.len + 2);
447 		if (temp == NULL) {
448 			break;
449 		}
450 
451 		widget->u.text.text = temp;
452 		memmove(widget->u.text.text + widget->u.text.idx + 1,
453 				widget->u.text.text + widget->u.text.idx,
454 				widget->u.text.len - widget->u.text.idx);
455 		widget->u.text.text[widget->u.text.idx] =
456 				fbtk_keycode_to_ucs4(value, modifier);
457 		widget->u.text.idx++;
458 		widget->u.text.len++;
459 		widget->u.text.text[widget->u.text.len] = '\0';
460 
461 		fb_font_width(&font_style, widget->u.text.text,
462 				widget->u.text.len, &widget->u.text.width);
463 		caret_moved = true;
464 		break;
465 	}
466 
467 	if (caret_moved) {
468 		fb_font_width(&font_style, widget->u.text.text,
469 				widget->u.text.idx, &widget->u.text.idx_offset);
470 		fbtk_set_caret(widget, true,
471 				widget->u.text.idx_offset + border,
472 				border,
473 				widget->height - border - border,
474 				fb_text_input_remove_caret_cb);
475 	}
476 
477 	fbtk_request_redraw(widget);
478 
479 	return 0;
480 }
481 
482 /** Routine called when click events occour in writeable widget.
483  *
484  * @param widget The widget reciving click events.
485  * @param cbi The callback parameters.
486  * @return The callback result.
487  */
488 static int
text_input_click(fbtk_widget_t * widget,fbtk_callback_info * cbi)489 text_input_click(fbtk_widget_t *widget, fbtk_callback_info *cbi)
490 {
491 	plot_font_style_t font_style;
492 	int fh;
493 	int border;
494 	size_t idx;
495 
496 	fb_text_font_style(widget, &fh, &border, &font_style);
497 
498 	widget->u.text.idx = widget->u.text.len;
499 
500 	fb_font_position(&font_style, widget->u.text.text,
501 			widget->u.text.len, cbi->x - border,
502 			&idx,
503 			&widget->u.text.idx_offset);
504 	widget->u.text.idx = idx;
505 	fbtk_set_caret(widget, true,
506 			widget->u.text.idx_offset + border,
507 			border,
508 			widget->height - border - border,
509 			fb_text_input_remove_caret_cb);
510 
511 	fbtk_request_redraw(widget);
512 
513 	return 0;
514 }
515 
516 /** Routine called when "stripped of focus" event occours for writeable widget.
517  *
518  * @param widget The widget reciving "stripped of focus" event.
519  * @param cbi The callback parameters.
520  * @return The callback result.
521  */
522 static int
text_input_strip_focus(fbtk_widget_t * widget,fbtk_callback_info * cbi)523 text_input_strip_focus(fbtk_widget_t *widget, fbtk_callback_info *cbi)
524 {
525 	fbtk_set_caret(widget, false, 0, 0, 0, NULL);
526 
527 	return 0;
528 }
529 
530 /* exported function documented in fbtk.h */
531 void
fbtk_writable_text(fbtk_widget_t * widget,fbtk_enter_t enter,void * pw)532 fbtk_writable_text(fbtk_widget_t *widget, fbtk_enter_t enter, void *pw)
533 {
534 	widget->u.text.enter = enter;
535 	widget->u.text.pw = pw;
536 
537 	fbtk_set_handler(widget, FBTK_CBT_INPUT, text_input, widget);
538 }
539 
540 /* exported function documented in fbtk.h */
541 void
fbtk_set_text(fbtk_widget_t * widget,const char * text)542 fbtk_set_text(fbtk_widget_t *widget, const char *text)
543 {
544 	plot_font_style_t font_style;
545 	int c_x, c_y, c_h;
546 	int fh;
547 	int border;
548 
549 	if ((widget == NULL) || (widget->type != FB_WIDGET_TYPE_TEXT))
550 		return;
551 	if (widget->u.text.text != NULL) {
552 		if (strcmp(widget->u.text.text, text) == 0)
553 			return; /* text is being set to the same thing */
554 		free(widget->u.text.text);
555 	}
556 	widget->u.text.text = strdup(text);
557 	widget->u.text.len = strlen(text);
558 	widget->u.text.idx = widget->u.text.len;
559 
560 
561 	fb_text_font_style(widget, &fh, &border, &font_style);
562 	fb_font_width(&font_style, widget->u.text.text,
563 			widget->u.text.len, &widget->u.text.width);
564 	fb_font_width(&font_style, widget->u.text.text,
565 			widget->u.text.idx, &widget->u.text.idx_offset);
566 
567 	if (fbtk_get_caret(widget, &c_x, &c_y, &c_h)) {
568 		/* Widget has caret; move it to end of new string */
569 		fbtk_set_caret(widget, true,
570 				widget->u.text.idx_offset + border,
571 				border,
572 				widget->height - border - border,
573 				fb_text_input_remove_caret_cb);
574 	}
575 
576 	fbtk_request_redraw(widget);
577 }
578 
579 /* exported function documented in fbtk.h */
580 fbtk_widget_t *
fbtk_create_text(fbtk_widget_t * parent,int x,int y,int width,int height,colour bg,colour fg,bool outline)581 fbtk_create_text(fbtk_widget_t *parent,
582 		 int x,
583 		 int y,
584 		 int width,
585 		 int height,
586 		 colour bg,
587 		 colour fg,
588 		 bool outline)
589 {
590 	fbtk_widget_t *neww;
591 
592 	neww = fbtk_widget_new(parent, FB_WIDGET_TYPE_TEXT, x, y, width, height);
593 	neww->fg = fg;
594 	neww->bg = bg;
595 	neww->mapped = true;
596 	neww->u.text.outline = outline;
597 
598 	fbtk_set_handler(neww, FBTK_CBT_REDRAW, fb_redraw_text, NULL);
599 	fbtk_set_handler(neww, FBTK_CBT_DESTROY, fb_destroy_text, NULL);
600 
601 	return neww;
602 }
603 
604 /* exported function documented in fbtk.h */
605 fbtk_widget_t *
fbtk_create_writable_text(fbtk_widget_t * parent,int x,int y,int width,int height,colour bg,colour fg,bool outline,fbtk_enter_t enter,void * pw)606 fbtk_create_writable_text(fbtk_widget_t *parent,
607 			  int x,
608 			  int y,
609 			  int width,
610 			  int height,
611 			  colour bg,
612 			  colour fg,
613 			  bool outline,
614 			  fbtk_enter_t enter,
615 			  void *pw)
616 {
617 	fbtk_widget_t *neww;
618 
619 	neww = fbtk_widget_new(parent, FB_WIDGET_TYPE_TEXT, x, y, width, height);
620 	neww->fg = fg;
621 	neww->bg = bg;
622 	neww->mapped = true;
623 
624 	neww->u.text.outline = outline;
625 	neww->u.text.enter = enter;
626 	neww->u.text.pw = pw;
627 
628 	fbtk_set_handler(neww, FBTK_CBT_REDRAW, fb_redraw_text, NULL);
629 	fbtk_set_handler(neww, FBTK_CBT_DESTROY, fb_destroy_text, NULL);
630 	fbtk_set_handler(neww, FBTK_CBT_CLICK, text_input_click, pw);
631 	fbtk_set_handler(neww, FBTK_CBT_STRIP_FOCUS, text_input_strip_focus, NULL);
632 	fbtk_set_handler(neww, FBTK_CBT_INPUT, text_input, neww);
633 
634 	return neww;
635 }
636 
637 /* exported function documented in fbtk.h */
638 fbtk_widget_t *
fbtk_create_text_button(fbtk_widget_t * parent,int x,int y,int width,int height,colour bg,colour fg,fbtk_callback click,void * pw)639 fbtk_create_text_button(fbtk_widget_t *parent,
640 			int x,
641 			int y,
642 			int width,
643 			int height,
644 			colour bg,
645 			colour fg,
646 			fbtk_callback click,
647 			void *pw)
648 {
649 	fbtk_widget_t *neww;
650 
651 	neww = fbtk_widget_new(parent, FB_WIDGET_TYPE_TEXT, x, y, width, height);
652 	neww->fg = fg;
653 	neww->bg = bg;
654 	neww->mapped = true;
655 
656 	neww->u.text.outline = true;
657 
658 	fbtk_set_handler(neww, FBTK_CBT_REDRAW, fb_redraw_text_button, NULL);
659 	fbtk_set_handler(neww, FBTK_CBT_DESTROY, fb_destroy_text, NULL);
660 	fbtk_set_handler(neww, FBTK_CBT_CLICK, click, pw);
661 	fbtk_set_handler(neww, FBTK_CBT_POINTERENTER, fbtk_set_ptr, &hand_image);
662 
663 	return neww;
664 }
665 
666 /*
667  * Local Variables:
668  * c-basic-offset:8
669  * End:
670  */
671