Root/src/browsedialog.cpp

1#include <string>
2
3#include "browsedialog.h"
4
5#include "FastDelegate.h"
6#include "filelister.h"
7#include "gmenu2x.h"
8
9using namespace fastdelegate;
10
11BrowseDialog::BrowseDialog(GMenu2X *gmenu2x, const string &title,
12        const string &subtitle)
13    : Dialog(gmenu2x)
14    , title(title)
15    , subtitle(subtitle)
16    , ts_pressed(false)
17    , buttonBox(gmenu2x)
18{
19    IconButton *btn;
20
21    btn = new IconButton(gmenu2x, "skin:imgs/buttons/x.png", gmenu2x->tr["Up one folder"]);
22    btn->setAction(MakeDelegate(this, &BrowseDialog::directoryUp));
23    buttonBox.add(btn);
24
25    btn = new IconButton(gmenu2x, "skin:imgs/buttons/b.png", gmenu2x->tr["Enter folder"]);
26    btn->setAction(MakeDelegate(this, &BrowseDialog::directoryEnter));
27    buttonBox.add(btn);
28
29    btn = new IconButton(gmenu2x, "skin:imgs/buttons/start.png", gmenu2x->tr["Confirm"]);
30    btn->setAction(MakeDelegate(this, &BrowseDialog::confirm));
31    buttonBox.add(btn);
32
33    iconGoUp = gmenu2x->sc.skinRes("imgs/go-up.png");
34    iconFolder = gmenu2x->sc.skinRes("imgs/folder.png");
35    iconFile = gmenu2x->sc.skinRes("imgs/file.png");
36}
37
38BrowseDialog::~BrowseDialog()
39{
40}
41
42bool BrowseDialog::exec()
43{
44    if (!fl)
45        return false;
46
47    string path = fl->getPath();
48    if (path.empty() || !fileExists(path) || path.compare(0, CARD_ROOT_LEN, CARD_ROOT) != 0)
49        setPath(CARD_ROOT);
50
51    fl->browse();
52
53    rowHeight = gmenu2x->font->getHeight()+1; // gp2x=15+1 / pandora=19+1
54    numRows = (gmenu2x->resY-gmenu2x->skinConfInt["topBarHeight"]-20)/rowHeight;
55    clipRect = (SDL_Rect){0, gmenu2x->skinConfInt["topBarHeight"]+1, gmenu2x->resX-9, gmenu2x->resY-gmenu2x->skinConfInt["topBarHeight"]-25};
56    touchRect = (SDL_Rect){2, gmenu2x->skinConfInt["topBarHeight"]+4, gmenu2x->resX-12, clipRect.h};
57
58    selected = 0;
59    close = false;
60    while (!close) {
61        if (gmenu2x->f200) gmenu2x->ts.poll();
62
63        paint();
64
65        handleInput();
66    }
67
68    return result;
69}
70
71BrowseDialog::Action BrowseDialog::getAction(bevent_t *event)
72{
73    BrowseDialog::Action action;
74
75    switch(event->button) {
76        case MENU:
77            action = BrowseDialog::ACT_CLOSE;
78            break;
79        case UP:
80            action = BrowseDialog::ACT_UP;
81            break;
82        case DOWN:
83            action = BrowseDialog::ACT_DOWN;
84            break;
85        case ALTLEFT:
86            action = BrowseDialog::ACT_SCROLLUP;
87            break;
88        case ALTRIGHT:
89            action = BrowseDialog::ACT_SCROLLDOWN;
90            break;
91        case LEFT:
92        case CLEAR:
93            action = BrowseDialog::ACT_GOUP;
94            break;
95        case ACCEPT:
96            action = BrowseDialog::ACT_SELECT;
97            break;
98        case SETTINGS:
99            action = BrowseDialog::ACT_CONFIRM;
100            break;
101        default:
102            action = BrowseDialog::ACT_NONE;
103            break;
104    }
105
106    return action;
107}
108
109void BrowseDialog::handleInput()
110{
111    BrowseDialog::Action action;
112    bevent_t event;
113
114    do {
115        gmenu2x->input.waitForEvent(&event);
116    } while (event.state != PRESSED);
117
118    /*
119    while(!gmenu2x->input.update())
120      usleep(LOOP_DELAY);
121      */
122
123    if (ts_pressed && !gmenu2x->ts.pressed()) {
124        action = BrowseDialog::ACT_SELECT;
125        ts_pressed = false;
126    } else {
127        action = getAction(&event);
128    }
129
130    if (gmenu2x->f200 && gmenu2x->ts.pressed() && !gmenu2x->ts.inRect(touchRect)) ts_pressed = false;
131
132    if (action == BrowseDialog::ACT_SELECT && (*fl)[selected] == "..")
133        action = BrowseDialog::ACT_GOUP;
134    switch (action) {
135    case BrowseDialog::ACT_CLOSE:
136        close = true;
137        result = false;
138        break;
139    case BrowseDialog::ACT_UP:
140        if (selected == 0)
141            selected = fl->size() - 1;
142        else
143            selected -= 1;
144        break;
145    case BrowseDialog::ACT_SCROLLUP:
146        if (selected <= numRows - 2)
147            selected = 0;
148        else
149            selected -= numRows - 2;
150        break;
151    case BrowseDialog::ACT_DOWN:
152        if (fl->size() - 1 <= selected)
153            selected = 0;
154        else
155            selected += 1;
156        break;
157    case BrowseDialog::ACT_SCROLLDOWN:
158        if (selected+(numRows-2)>=fl->size())
159            selected = fl->size()-1;
160        else
161            selected += numRows-2;
162        break;
163    case BrowseDialog::ACT_GOUP:
164        directoryUp();
165        break;
166    case BrowseDialog::ACT_SELECT:
167        if (fl->isDirectory(selected)) {
168            directoryEnter();
169            break;
170        }
171        /* Falltrough */
172    case BrowseDialog::ACT_CONFIRM:
173        confirm();
174        break;
175    default:
176        break;
177    }
178
179    buttonBox.handleTS();
180}
181
182#include <iostream>
183
184void BrowseDialog::directoryUp()
185{
186    string path = fl->getPath();
187    string::size_type p = path.rfind("/");
188
189    if (p == path.size() - 1)
190        p = path.rfind("/", p - 1);
191    
192    if (p == string::npos || path.compare(0, 1, "/") != 0 || path.length() < 2) {
193        close = true;
194        result = false;
195    } else {
196        selected = 0;
197        setPath(path.substr(0, p));
198    }
199}
200
201void BrowseDialog::directoryEnter()
202{
203    string path = fl->getPath();
204    if (path[path.size()-1] != '/')
205        path += "/";
206
207    setPath(path + fl->at(selected));
208
209    selected = 0;
210}
211
212void BrowseDialog::confirm()
213{
214    result = true;
215    close = true;
216}
217
218void BrowseDialog::paint()
219{
220    unsigned int i, iY;
221    unsigned int firstElement, lastElement;
222    unsigned int offsetY;
223    Surface *icon;
224
225    gmenu2x->bg->blit(gmenu2x->s, 0, 0);
226    drawTitleIcon("icons/explorer.png", true);
227    writeTitle(title);
228    writeSubTitle(subtitle);
229
230    buttonBox.paint(5);
231
232    // TODO(MtH): I have no idea what the right value of firstElement would be,
233    // but originally it was undefined and that is never a good idea.
234    firstElement = 0;
235    if (selected>firstElement+numRows-1)
236        firstElement = selected-numRows+1;
237    else if (selected < firstElement)
238        firstElement = selected;
239
240    //Selection
241    iY = selected-firstElement;
242    iY = gmenu2x->skinConfInt["topBarHeight"]+1+(iY*rowHeight);
243    gmenu2x->s->box(2, iY, gmenu2x->resX-12, rowHeight-1, gmenu2x->skinConfColors[COLOR_SELECTION_BG]);
244
245    lastElement = firstElement + numRows;
246    if (lastElement > fl->size())
247        lastElement = fl->size();
248
249    offsetY = gmenu2x->skinConfInt["topBarHeight"]+1;
250
251    //Files & Directories
252    gmenu2x->s->setClipRect(clipRect);
253    for (i = firstElement; i < lastElement; i++) {
254        if (fl->isDirectory(i)) {
255            if ((*fl)[i]=="..")
256                icon = iconGoUp;
257            else
258                icon = iconFolder;
259        } else {
260            icon = iconFile;
261        }
262        icon->blit(gmenu2x->s, 5, offsetY);
263        gmenu2x->s->write(gmenu2x->font, (*fl)[i], 24, offsetY + 8, ASFont::HAlignLeft, ASFont::VAlignMiddle);
264
265        if (gmenu2x->f200 && gmenu2x->ts.pressed() && gmenu2x->ts.inRect(touchRect.x, offsetY + 3, touchRect.w, rowHeight)) {
266            ts_pressed = true;
267            selected = i;
268        }
269
270        offsetY += rowHeight;
271    }
272    gmenu2x->s->clearClipRect();
273
274    gmenu2x->drawScrollBar(numRows,fl->size(),firstElement,clipRect.y,clipRect.h);
275    gmenu2x->s->flip();
276}
277

Archive Download this file



interactive