File: data_entry_numeric.c

package info (click to toggle)
mlterm 2.9.4-5
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 16,728 kB
  • ctags: 5,338
  • sloc: ansic: 74,649; sh: 8,532; cpp: 1,451; makefile: 1,126; perl: 496; sed: 16
file content (95 lines) | stat: -rw-r--r-- 2,440 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <string.h> /* strncasecmp/strcasecmp/strdup/strchr/strrchr/memmove*/
#include <stdlib.h> /* free/malloc */
#include "comm.h"
#include "data.h"
#include "data_entry_numeric.h"

void entry_numeric_reset(entry_t *entry){
	entry_numeric_t * data = entry->data;
	mlterm_set_value(entry->key, data->initial);
}

void entry_numeric_apply(entry_t *entry){
	entry_numeric_t * data = entry->data;
	mlterm_set_value(entry->key, data->current);
}

entry_numeric_t *entry_numeric_new(const char *key, int max, int min, const char *unit){
	entry_numeric_t *entry;
	char * tmp;

	tmp = mlterm_get_param(key);
	entry = malloc(sizeof(entry_numeric_t));
	if(tmp)
		entry->initial = atoi(strdup(tmp));
	else
		entry->initial = 0;
	entry->current = entry->initial;
	entry->unit = unit;
	entry->max = max;
	entry->min = min;
	return entry;
}

int entry_numeric_add(section_t *section, const char *name, const char * key, int min, int max, const char *unit){
	if (section->maxwidth < strlen(name))
		section->maxwidth = strlen(name);
	section->entry[section->size].name = name;
	section->entry[section->size].key = key;
	section->entry[section->size].type = ENT_NUMERIC;
	section->entry[section->size].data = entry_numeric_new(key, max, min, unit);
	section->size++;
	return 0;
}

void entry_numeric_free_data(entry_t * entry){
	free(entry->data);
	return;
}

void entry_numeric_display(window_t *window, entry_t *entry, int x, int y, int state){
	entry_numeric_t * data;

	data = entry->data;
	display_numeric(window, x, y, data->current, data->unit, DC_NORMAL);
}

int entry_numeric_edit(window_t *window, entry_t *entry, int x, int y){
	entry_numeric_t *data;
	int buffer;
	
	data = entry->data;
	buffer = read_one();
	switch(buffer){
	case KEY_ESC:
	case 10:
		return -1;
	case KEY_UP:
		data->current *= 2;
		if ((data->max != -1) && (data->current > data->max))
			data->current = data->max;
		entry->modified = 1;
		return 1; /* redraw */
	case KEY_DOWN:
		data->current /= 2;
		if ((data->min != -1) && (data->current < data->min))
			data->current = data->min;
		entry->modified = 1;
		return 1; /* redraw */
	case KEY_RIGHT:
		if ((data->max == -1) || (data->current < data->max)){
			data->current++;
			entry->modified = 1;
		}
		return 1; /* redraw */
	case KEY_LEFT:
		if ((data->min == -1) || (data->current > data->min)){
			entry->modified = 1;
			data->current--;
		}
		return 1; /* redraw */
	default:
		/* ignore */
		return 0;
	}
}