summaryrefslogtreecommitdiffstats
path: root/apps/gui/select.c
blob: 5cde812a2f2ef334fe57574b93e50bcdffa316e8 (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2005 by Kevin Ferrare
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include "select.h"

#include "lang.h"
#include "textarea.h"
#include "sprintf.h"
#include "kernel.h"
#include "screen_access.h"

void gui_select_init_numeric(struct gui_select * select,
        const char * title,
        int init_value,
        int min_value,
        int max_value,
        int step,
        const char * unit,
        void (*formatter)(char* dest,
                          int dest_length,
                          int variable,
                          const char* unit)
                          )
{
    select->canceled=false;
    select->validated=false;
    select->title=title;
    select->min_value=min_value;
    select->max_value=max_value+1;
    select->option=init_value;
    select->step=step;
    select->extra_string=unit;
    select->formatter=formatter;
    select->items=NULL;
    select->limit_loop=false;
}

void gui_select_init_items(struct gui_select * select,
        const char * title,
        int selected,
        const struct opt_items * items,
        int nb_items)
{
    select->canceled=false;
    select->validated=false;
    select->title=title;
    select->min_value=0;
    select->max_value=nb_items;
    select->option=selected;
    select->step=1;
    select->formatter=NULL;
    select->items=items;
    select->limit_loop=false;
}

void gui_select_next(struct gui_select * select)
{
    if(select->option + select->step >= select->max_value)
    {
        if(!select->limit_loop)
        {
            if(select->option==select->max_value-1)
                select->option=select->min_value;
            else
                select->option=select->max_value-1;
        }
    }
    else
        select->option+=select->step;
}

void gui_select_prev(struct gui_select * select)
{
    if(select->option - select->step < select->min_value)
    {
        if(!select->limit_loop)
        {
            if(select->option==select->min_value)
                select->option=select->max_value-1;
            else
                select->option=select->min_value;
        }
    }
    else
        select->option-=select->step;
}

void gui_select_draw(struct gui_select * select, struct screen * display)
{
#ifdef HAVE_LCD_BITMAP
    screen_set_xmargin(display, 0);
#endif
    gui_textarea_clear(display);
    display->puts_scroll(0, 0, select->title);

    if(gui_select_is_canceled(select))
        display->puts_scroll(0, 0, str(LANG_MENU_SETTING_CANCEL));
    if(select->items)
        display->puts_scroll(0, 1, P2STR(select->items[select->option].string));
    else
    {
        char buffer[30];
        if(!select->formatter)
            snprintf(buffer, sizeof buffer,"%d %s", select->option, select->extra_string);
        else
            select->formatter(buffer, sizeof buffer, select->option, select->extra_string);
        display->puts_scroll(0, 1, buffer);
    }
    gui_textarea_update(display);
}

void gui_syncselect_draw(struct gui_select * select)
{
    int i;
    FOR_NB_SCREENS(i)
        gui_select_draw(select, &(screens[i]));
}

bool gui_syncselect_do_button(struct gui_select * select, int button)
{
    gui_select_limit_loop(select, false);
    switch(button)
    {
        case SELECT_INC | BUTTON_REPEAT :
#ifdef SELECT_RC_INC
        case SELECT_RC_INC | BUTTON_REPEAT :
#endif
            gui_select_limit_loop(select, true);
        case SELECT_INC :
#ifdef SELECT_RC_INC
        case SELECT_RC_INC :
#endif
            gui_select_next(select);
            return(true);

        case SELECT_DEC | BUTTON_REPEAT :
#ifdef SELECT_RC_DEC
        case SELECT_RC_DEC | BUTTON_REPEAT :
#endif
            gui_select_limit_loop(select, true);
        case SELECT_DEC :
#ifdef SELECT_RC_DEC
        case SELECT_RC_DEC :
#endif
            gui_select_prev(select);
            return(true);

        case SELECT_OK :
#ifdef SELECT_RC_OK
        case SELECT_RC_OK :
#endif
#ifdef SELECT_RC_OK2
        case SELECT_RC_OK2 :
#endif
#ifdef SELECT_OK2
        case SELECT_OK2 :
#endif
            gui_select_validate(select);
            return(false);

        case SELECT_CANCEL :
#ifdef SELECT_CANCEL2
        case SELECT_CANCEL2 :
#endif 
#ifdef SELECT_RC_CANCEL
        case SELECT_RC_CANCEL :
#endif
#ifdef SELECT_RC_CANCEL2
        case SELECT_RC_CANCEL2 :
#endif
            gui_select_cancel(select);
            gui_syncselect_draw(select);
            sleep(HZ/2);
            return(false);
    }
    return(false);
}