summaryrefslogtreecommitdiffstats
path: root/apps/gui/mask_select.c
blob: e22ba7dd03256eccbd8035cf822d28fb4d13ecbe (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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * Copyright (C) 2016 William Wilgus
 * Derivative of folder_select.c by:
 * Copyright (C) 2012 Jonathan Gordon
 * Copyright (C) 2012 Thomas Martitz
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
/* TODO add language defines */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lang.h"
#include "language.h"
#include "list.h"
#include "plugin.h"
#include "mask_select.h"
#include "talk.h"

/*
 * Order for changing child states:
 * 1) expand folder
 * 2) collapse and select
 * 3) deselect (skip to 1)
 */

enum child_state {
    EXPANDED,
    SELECTED,
    COLLAPSED,
    DESELECTED,
};

/* Children of main categories */
struct child {
    const char* name;
    struct category *category;
    enum child_state state;
    int key_value;
};

/* Main Categories in root */
struct category {
    const char *name;
    struct child *children;
    int children_count;
    int depth;
    struct category* previous;
    int key_value; /*values of all children OR|D*/
};

/* empty category for children of root only one level needed */
static struct category empty = {
     .name           = "",
     .children       = NULL,
     .children_count = 0,
     .depth          = 1,
     .previous       = NULL,
};

/* Or | all keyvalues that user selected */
static int calculate_mask_r(struct category *root, int mask)
{
    int i = 0;
    while (i < root->children_count)
    {
        struct child *this = &root->children[i];
        if (this->state == SELECTED)
            mask |= this->key_value;

        else if (this->state == EXPANDED)
            mask = calculate_mask_r(this->category, mask);
        i++;
    }
return mask;
}

static int count_items(struct category *start)
{
    int count = 0;
    int i;

    for (i=0; i<start->children_count; i++)
    {
        struct child *foo = &start->children[i];
        if (foo->state == EXPANDED)
            count += count_items(foo->category);
        count++;
    }
    return count;
}

static struct child* find_index(struct category *start,
                                int index,struct category **parent)
{
    int i = 0;

    *parent = NULL;

    while (i < start->children_count)
    {
        struct child *foo = &start->children[i];
        if (i == index)
        {
            *parent = start;
            return foo;
        }
        i++;
        if (foo->state == EXPANDED)
        {
            struct child *bar = find_index(foo->category, index - i, parent);
            if (bar)
            {
                return bar;
            }
            index -= count_items(foo->category);
        }
    }
    return NULL;
}

/* simplelist uses this callback to change
    the states of the categories/children */
static int item_action_callback(int action, struct gui_synclist *list)
{
    struct category *root = (struct category*)list->data;
    struct category *parent;
    struct child *this = find_index(root, list->selected_item, &parent);

    if (action == ACTION_STD_OK)
    {
        switch (this->state)
        {
            case EXPANDED:
                this->state = SELECTED;
                if (global_settings.talk_menu)
                    talk_id(LANG_ON, false);
                break;
            case SELECTED:
                this->state = this->category->children_count == 0 ?
                        DESELECTED : COLLAPSED;
                if (global_settings.talk_menu && this->category->children_count == 0)
                    talk_id(LANG_OFF, false);
                break;
            case COLLAPSED:
                if (this->category == NULL)
                    this->category = root;
                this->state = this->category->children_count == 0 ?
                        SELECTED : EXPANDED;
                if (global_settings.talk_menu && this->category->children_count == 0)
                    talk_id(LANG_ON, false);
                break;
            case DESELECTED:
                this->state = SELECTED;
                if (global_settings.talk_menu)
                    talk_id(LANG_ON, false);
                break;

            default:
                /* do nothing */
                return action;
        }
        list->nb_items = count_items(root);
        return ACTION_REDRAW;
    }

    return action;
}

static const char * item_get_name(int selected_item, void * data,
                                   char * buffer, size_t buffer_len)
{
    struct category *root = (struct category*)data;
    struct category *parent;
    struct child *this = find_index(root, selected_item , &parent);

    buffer[0] = '\0';

    if (parent->depth >= 0)
        for(int i = 0; i <= parent->depth; i++)
            strcat(buffer, "\t\0");

    /* state of selection needs icons so if icons are disabled use text*/
    if (!global_settings.show_icons)
        {
            if (this->state == SELECTED)
                strcat(buffer, "+\0");
            else
                strcat(buffer,"  \0");
        }
    strlcat(buffer, P2STR((const unsigned char *)this->name), buffer_len);

    return buffer;
}

static int item_get_talk(int selected_item, void *data)
{
    struct category *root = (struct category*)data;
    struct category *parent;
    struct child *this = find_index(root, selected_item , &parent);
    if (global_settings.talk_menu)
        {
            long id = P2ID((const unsigned char *)(this->name));
            if(id>=0)
                talk_id(id, true);
            else
                talk_spell(this->name, true);
            talk_id(VOICE_PAUSE,true);
            if (this->state == SELECTED)
                talk_id(LANG_ON, true);
            else if (this->state == DESELECTED)
                talk_id(LANG_OFF, true);
        }
    return 0;
}

static enum themable_icons item_get_icon(int selected_item, void * data)
{
    struct category *root = (struct category*)data;
    struct category *parent;
    struct child *this = find_index(root, selected_item, &parent);

    switch (this->state)
    {
        case SELECTED:
            return Icon_Submenu;
        case COLLAPSED:
            return Icon_NOICON;
        case EXPANDED:
            return Icon_Submenu_Entered;
        default:
            return Icon_NOICON;
    }
    return Icon_NOICON;
}

/* supply your original mask,the page header (ie. User do this..), mask items
    and count, they will be selected automatically if the mask includes
    them. If user selects new items and chooses to save settings
    new mask returned otherwise, original is returned
*/
int mask_select(int mask, const unsigned char* headermsg,
                             struct s_mask_items *mask_items, size_t items)
{
    struct simplelist_info info;

    struct child action_child[items];
            for (unsigned i = 0; i < items; i++)
            {
                action_child[i].name      = mask_items[i].name;
                action_child[i].category  = &empty;
                action_child[i].key_value = mask_items[i].bit_value;
                action_child[i].state     = mask_items[i].bit_value & mask ?
                                                SELECTED : DESELECTED;
            }

   struct category root = {
        .name           = "",
        .children       = (struct child*) &action_child,
        .children_count = items,
        .depth          = -1,
        .previous       = NULL,
    };

    simplelist_info_init(&info, P2STR(headermsg), count_items(&root), &root);
    info.get_name        = item_get_name;
    info.action_callback = item_action_callback;
    info.get_icon        = item_get_icon;
    info.get_talk        = item_get_talk;
    simplelist_show_list(&info);

    return calculate_mask_r(&root,0);
}