File: data_entry_color.c

package info (click to toggle)
mlterm 3.1.2-1.3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 23,168 kB
  • sloc: ansic: 102,795; sh: 9,676; java: 2,018; perl: 1,601; makefile: 1,595; cpp: 771; sed: 16
file content (120 lines) | stat: -rw-r--r-- 2,793 bytes parent folder | download | duplicates (7)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <string.h> /* strncasecmp/strcasecmp/strdup/strchr/strrchr/memmove*/
#include <stdlib.h> /* free/malloc */
#include "comm.h"
#include "data.h"
#include "data_entry_color.h"
int entry_color_edit(window_t *window, entry_t *entry, int x, int y){
	entry_color_t *data;
	int result;
	window_t * edit;

	data = entry->data;
	edit = window_new(x+2, y, x +12, y +9, 1, window); /*XXX*/
	result = color_select(edit, colorid_from_name(data->current));
	if (result != colorid_from_name(data->current)){
		free(data->current);
		data->current = strdup(colorname_from_id(result));
		entry->modified = 1;
	}
	return -1; /* finished */
}

void entry_color_reset(entry_t *entry){
	entry_color_t * data = entry->data;
	mlterm_set_param(entry->key, data->initial);
}

void entry_color_apply(entry_t *entry){
	entry_color_t * data = entry->data;
	mlterm_set_param(entry->key, data->current);
	free(data->current);
	data->current = strdup(mlterm_get_param(entry->key));
}

entry_color_t *entry_color_new(const char *key){
	entry_color_t *entry;
	
	entry = malloc(sizeof(entry_color_t));
	entry->initial = mlterm_get_param(key);
	if (entry->initial)
		entry->initial = strdup(entry->initial);
	else
		entry->initial = strdup("");
	entry->current = strdup(entry->initial);
	return entry;
}

int entry_color_add(section_t *section, const char *name, const char * key){
	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_COLOR;
	section->entry[section->size].data = entry_color_new(key);
	section->size++;
	return 0;
}

void entry_color_free_data(entry_t * entry){
	entry_color_t *data;

	data = entry->data;
	free(data->initial);
	free(data->current);
	free(data);
	return;
}


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

	data = entry->data;
	display_colorcube(window, x+1, y, colorid_from_name(data->current));
  	display_str(window, x+2, y, data->current, DC_NORMAL);
}

int color_select(window_t *edit, int initial){
	int ind, i, flag = 1;
	int buffer;

	window_clear(edit);
	ind = initial;

	while(1){
		if (flag){
			window_clear(edit);
			for(i = 0; i < 8; i++){				
				if (i == ind)
					window_addstr(edit, 0, i, ">");
				set_fg_color(i);
				window_addstr(edit, 1, i, colorname_from_id(i));
				set_fg_color_default();
			}
			flush_stdout();
			flag = 0;
		}
		buffer = read_one();
		switch(buffer){
		case KEY_ESC:
			return initial;
		case KEY_UP:
		case KEY_LEFT:
			ind--;
			if (ind < 0)
				ind = 8;
			flag = 1;
			break;
		case KEY_DOWN:
		case KEY_RIGHT:
			ind++;
			if (ind > 8)
				ind = 0;
			flag = 1;			
			break;
		case 10: /* ret */
  			return ind;
		}
	}
}