File size: 5,125 Bytes
c7436b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#include <stdio.h>
#include "task_menu.h"
#include <string.h>
#include "tree_browser_state.h"
#include "filemanager.h"
bool TaskMenu :: actionsCreated = false;

TaskMenu :: TaskMenu(UserInterface *ui, TreeBrowserState *state, Path *p) : ContextMenu(ui, state, 0, 0)
{
	path = p;
	this->state = state;
	if(state)
		browsable = state->node;
	else
		browsable = NULL;
	screen = NULL;
    context_state = e_new;
    keyb = NULL;
    window = NULL;
}

// y_offs = line above selected, relative to window
// corner = line of selected, relative to window

TaskMenu :: ~TaskMenu(void)
{
}

void TaskMenu :: init(Window *pwin, Keyboard *key)
{
    screen = pwin->getScreen();
    keyb = key;

    IndexedList<ObjectWithMenu*> *objects = ObjectWithMenu :: getObjectsWithMenu();

    // We do this only once
    if (!actionsCreated) {
        for(int i=0;i<objects->get_elements();i++) {
            (*objects)[i]->create_task_items();
        }
        TasksCollection::sort();
        actionsCreated = true;

        // Set all actions that we collect here to persistent, so that they won't get cleaned up
        IndexedList<TaskCategory *> *categories = TasksCollection::getCategories();
        for (int i=0; i < categories->get_elements(); i++) {
            TaskCategory *cat = (*categories)[i];
            IndexedList<Action *> *catActions = cat->getActions();
            for (int j=0; j < catActions->get_elements(); j++) {
                Action *a = (*catActions)[j];
                a->setPersistent();
            }
        }
    }

    int len, max_len;
    int rows, size_y;

    bool writablePath = path ? FileManager :: getFileManager() -> is_path_writable(path) : false;
    if(context_state == e_new) {

        for(int i=0;i<objects->get_elements();i++) {
        	(*objects)[i]->update_task_items(writablePath); // This should actually update them, according to the current state of each object
        }

        IndexedList<TaskCategory *> *categories = TasksCollection::getCategories();
        for (int i=0; i < categories->get_elements(); i++) {
            TaskCategory *cat = (*categories)[i];
            if (cat->countActive()) {
                Action *catAction = new Action(cat->getName(), 0, 0, 0);
                catAction->setObject(cat);
                actions.append(catAction);
            }
        }

        int items = actions.get_elements();
        if(!items) {
            printf("No items.. exit.\n");
            context_state = e_finished;
            return;
        } else {
            printf("Number of items: %d\n", items);
        }
        rows = items + 2;
        size_y = screen->get_size_y();
        if(rows > size_y-4) {
            rows = size_y-4;
        } else if (rows < 8) {
        	rows = 8;
        }
		y_offs = (size_y - rows) >> 1;
        
/*

        max_len = 0;

        for(int i=0;i<items;i++) {

        	Action *a = actions[i];

            len = strlen(a->getName());

            if(len > max_len)

                max_len = len;

        }

        if(max_len > 30)

            max_len = 30;

*/
    }
    max_len = 28;

    window = new Window(screen, (screen->get_size_x() - max_len - 2) >> 1, y_offs, max_len+2, rows);
    window->set_color(user_interface->color_fg);
    window->draw_border();
    context_state = e_active;

    draw();
}

int TaskMenu :: select_item(void)
{
    Action *a = actions[item_index];
    TaskCategory *cat = (TaskCategory *)a->getObject();
    printf("Action Category selected: %s\n", actions[item_index]->getName());

    if (!a) {
        return 1;
    }
    int first_selectable_sub_item = 0;
    IndexedList <Action *> *actionlist = cat->getActions();
    int num_el = actionlist->get_elements();

    Action *singleAction = (*actionlist)[0];
    if ((num_el == 1) && (singleAction->isEnabled())) { // Only one enabled item, so we can just execute it
        selectedAction = singleAction;
        return 1; // done
    }

    for(int i=0; i < num_el; i++) {
        Action *a = (*actionlist)[i];
        if(a->isEnabled()) {
            first_selectable_sub_item = i;
            break;
        }
    }
    subContext = new TaskSubMenu(user_interface, state, cat, first_selectable_sub_item, item_index);
    subContext->init(window, keyb);
    user_interface->activate_uiobject(subContext);
    return 0;
}

TaskSubMenu :: TaskSubMenu(UserInterface *ui, TreeBrowserState *state, TaskCategory *cat, int first, int item) : ContextMenu(ui, state, first, item), category(cat)
{

}

int TaskSubMenu :: get_items(void)
{
    int itms = 0;
    int enabled = 0;
    IndexedList<Action *> *catActions = category->getActions();
    for (int j=0; j < catActions->get_elements(); j++) {
        Action *a = (*catActions)[j];
        if (a->isShown()) {
            appendAction(a);
            itms++;
            if (a->isEnabled()) {
                enabled++;
            }
        }
    }
    if (enabled == 0) {
        return 0;
    }
    return itms;
}