aboutsummaryrefslogtreecommitdiffstats
path: root/roms/u-boot-sam460ex/board/ACube/menu/string_edit.c
blob: 64d53a01906cf2f2c4d10d32993b0cf215542615 (plain)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "menu.h"
#include "string_edit.h"

//static int num_lines = 0;
static int x_pos=0, y_pos=0;
static int width = 0, height = 0;
static int cursor_pos = 0;
static char *buffer;
static char *prompt;
static int buffer_length;
static char backup[1024];

#define STRINGBOX_WIDTH       70
#define STRINGBOX_HEIGHT      4
#define STRINGBOX_X           5
#define STRINGBOX_Y           5

static void string_edit_draw(void)
{
    int 
	i,j,
	length = buffer_length,
	attr = MENUATTR_NORMAL;

    char
	c;

    //int cursor_x = 0, cursor_y = 0;

    for (i=0; i<height; i++)
    {
	for (j=0; j<width; j++)
	{
	    int 
		offset = j+i*width;
	    
	    if (offset > length) 
		c = ' ';
	    else
		c = buffer[offset];

	    if (offset == cursor_pos)
#if 1 //def SIM
		attr = MENUATTR_HILITE;
	    else
		attr = MENUATTR_NORMAL;
#else
	    {
		cursor_x = x_pos+j;
		cursor_y = y_pos+i;
	    }
#endif
	    video_draw_text(x_pos + j, y_pos + i, attr, &c, 1);
	}
    }
#if 0 //ndef SIM
    video_set_cursor(cursor_y, cursor_y); 
#endif
}

static void string_clear_frame(void)
{
    video_clear_box(STRINGBOX_X-2, STRINGBOX_Y-2,
		    STRINGBOX_WIDTH+4, STRINGBOX_HEIGHT+4,
		    ' ', MENUATTR_NORMAL);
}

static void string_draw_frame(void)
{
    string_clear_frame();
    video_draw_box(SINGLE_BOX, MENUATTR_NORMAL, prompt, 0, 
		   STRINGBOX_X-2, STRINGBOX_Y-2,
		   STRINGBOX_WIDTH+4, STRINGBOX_HEIGHT+4);
}

static void backspace(void)
{
    int
	i;
    
    char 
	*s;

    if (cursor_pos == 0) return;
    if (cursor_pos == buffer_length)
    {
	buffer_length--;
	cursor_pos--;
	buffer[buffer_length] = 0;
    }
    else
    {
	s = buffer+cursor_pos-1;
	for (i = 0; i < buffer_length - cursor_pos + 1; i++)
	{
	    *s = *(s+1);
	    s++;
	}
	cursor_pos--;
	buffer_length--;
    }
}

static void insert(int key)
{
    int
	i;

    char
	*s;

    s = buffer + buffer_length;
    for (i = 0; i < buffer_length - cursor_pos + 1; i++)
    {
	*(s+1) = *s;
	s--;
    }
    *(buffer + cursor_pos) = (char)key;
    cursor_pos++;
    buffer_length++;
}

bool menu_string_edit(char *_prompt, char *string, int buffersize)
{
    int 
	key;

    prompt = _prompt;
    x_pos  = STRINGBOX_X;
    y_pos  = STRINGBOX_Y;
    width  = STRINGBOX_WIDTH;
    height = STRINGBOX_HEIGHT;

    buffer = string;
    strcpy(backup, string);

    buffer_length = cursor_pos = strlen(buffer);

    string_draw_frame();
    do
    {
	string_edit_draw();
	key = video_get_key();
	switch(key)
	{
	case KEY_ABORT:
	    strcpy(string, backup);
	    string_clear_frame();
	    menu_draw_current_form();
	    return false;
	case KEY_ACTIVATE:
	    string_clear_frame();
	    menu_draw_current_form();
	    return true;
	case KEY_NEXT_OPTION:
	    cursor_pos++;
	    if (cursor_pos > buffer_length) cursor_pos = buffer_length;
	    break;
	case KEY_PREV_OPTION:
	    cursor_pos--;
	    if (cursor_pos < 0) cursor_pos = 0;
	    break;
	case KEY_DELETE:
	    /* Backspace */
	    backspace();
	    break;
	default:
	    if (key >= 32 && key <= 127)
	    {
		insert(key);
	    }
	    break;
	}
    } while (1);
    
    return false;
}