pane

Termbox2 based terminal UI library
git clone git://git.dimitrijedobrota.com/pane.git
Log | Files | Refs

widgets.c (1219B)


      1 #include <math.h>
      2 
      3 #include "pane.h"
      4 #include "widgets.h"
      5 #include "utils.h"
      6 
      7 widgetList_T widgetList_new(Pane_T pane, int size)
      8 {
      9 	widgetList_T list = calloc(1, sizeof(*list));
     10 	list->items_num = size;
     11 	list->pane = pane;
     12 
     13 	return list;
     14 }
     15 
     16 void widgetList_cacl(widgetList_T self, int index_current)
     17 {
     18 	int max_items = pane_height(self->pane);
     19 	self->display_num = ACLAMP(max_items, 0, self->items_num);
     20 
     21 	int relative = index_current - self->index_start;
     22 	if (relative < 0 && self->index_start > 0) {
     23 		self->index_start += relative;
     24 	} else if (relative > 0 && relative > self->display_num - 1) {
     25 		self->index_start += relative - (self->display_num - 1);
     26 	}
     27 	CLAMP(self->index_start, 0, self->items_num - self->display_num);
     28 }
     29 
     30 void widgetCenter_print(widget_T widget)
     31 {
     32 	size_t i;
     33 	char *message = (char *)widget->data->payload;
     34 	Pane_T pane = widget->pane;
     35 
     36 	pane_clear(pane, 0);
     37 
     38 	int len = strlen(message);
     39 	int width = pane_width(pane);
     40 	int lines = ceil((double)len / width);
     41 	int start_x = centerHorisontal(pane, MIN(len, width));
     42 	int y = centerVertical(pane, lines);
     43 
     44 	for (i = 0; i < lines; i++, y++)
     45 		tb_printf(start_x, y, TB_RED, 0, "%.*s", pane_width(pane),
     46 			  message + i * width);
     47 	tb_present();
     48 }