Root/src/wallpaperdialog.cpp

1/***************************************************************************
2 * Copyright (C) 2006 by Massimiliano Torromeo *
3 * massimiliano.torromeo@gmail.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20
21#include <iostream>
22
23#include "wallpaperdialog.h"
24#include "filelister.h"
25#include "debug.h"
26
27using namespace std;
28
29WallpaperDialog::WallpaperDialog(GMenu2X *gmenu2x)
30    : Dialog(gmenu2x)
31{
32}
33
34bool WallpaperDialog::exec()
35{
36    bool close = false, result = true;
37
38    FileLister fl;
39    fl.setFilter(".png,.jpg,.jpeg,.bmp");
40
41    string filepath = GMenu2X::getHome() + "/skins/"
42          + gmenu2x->confStr["skin"] + "/wallpapers";
43    if (fileExists(filepath))
44      fl.setPath(filepath, true);
45
46    filepath = GMENU2X_SYSTEM_DIR "/skins/"
47          + gmenu2x->confStr["skin"] + "/wallpapers";
48    if (fileExists(filepath)) {
49        fl.setPath(filepath, false);
50        fl.browse(false);
51    }
52
53    if (gmenu2x->confStr["skin"] != "Default") {
54        filepath = GMenu2X::getHome() + "/skins/Default/wallpapers";
55        if (fileExists(filepath)) {
56            fl.setPath(filepath, false);
57            fl.browse(false);
58        }
59
60        filepath = GMENU2X_SYSTEM_DIR "/skins/Default/wallpapers";
61        if (fileExists(filepath)) {
62            fl.setPath(filepath, false);
63            fl.browse(false);
64        }
65    }
66
67    vector<string> wallpapers = fl.getFiles();
68
69    DEBUG("Wallpapers: %i\n", wallpapers.size());
70
71    uint i, selected = 0, firstElement = 0, iY;
72
73    while (!close) {
74        if (selected>firstElement+9) firstElement=selected-9;
75        if (selected<firstElement) firstElement=selected;
76
77        //Wallpaper
78        gmenu2x->sc[((string)"skin:wallpapers/" + wallpapers[selected]).c_str()]->blit(gmenu2x->s, 0, 0);
79
80        gmenu2x->drawTopBar(gmenu2x->s);
81        gmenu2x->drawBottomBar(gmenu2x->s);
82
83        drawTitleIcon("icons/wallpaper.png",true);
84        writeTitle("Wallpaper selection");
85        writeSubTitle("Select an image from the list, to use as a wallpaper");
86
87        gmenu2x->drawButton(gmenu2x->s, "b", gmenu2x->tr["Select wallpaper"],5);
88
89        //Selection
90        iY = selected-firstElement;
91        iY = 44+(iY*17);
92        gmenu2x->s->box(2, iY, 308, 16, gmenu2x->skinConfColors[COLOR_SELECTION_BG]);
93
94        //Files & Directories
95        gmenu2x->s->setClipRect(0,41,311,179);
96        for (i=firstElement; i<wallpapers.size() && i<firstElement+10; i++) {
97            iY = i-firstElement;
98            gmenu2x->s->write(gmenu2x->font, wallpapers[i], 5, 52+(iY*17), ASFont::HAlignLeft, ASFont::VAlignMiddle);
99        }
100        gmenu2x->s->clearClipRect();
101
102        gmenu2x->drawScrollBar(10,wallpapers.size(),firstElement,44,170);
103        gmenu2x->s->flip();
104
105        switch(gmenu2x->input.waitForPressedButton()) {
106            case MENU:
107            case CLEAR:
108                close = true;
109                result = false;
110                break;
111            case UP:
112                if (selected == 0) selected = wallpapers.size()-1;
113                else selected -= 1;
114                break;
115            case ALTLEFT:
116                if ((int)(selected-9) < 0) selected = 0;
117                else selected -= 9;
118                break;
119            case DOWN:
120                if (selected+1 >= wallpapers.size()) selected = 0;
121                else selected += 1;
122                break;
123            case ALTRIGHT:
124                if (selected+9 >= wallpapers.size()) selected = wallpapers.size()-1;
125                else selected += 9;
126                break;
127            case ACCEPT:
128                close = true;
129                if (wallpapers.size() > 0)
130                    wallpaper = gmenu2x->sc.getSkinFilePath("wallpapers/" + wallpapers[selected]);
131                else result = false;
132            default:
133                break;
134        }
135    }
136
137    for (uint i=0; i<wallpapers.size(); i++)
138      gmenu2x->sc.del("skin:wallpapers/" + wallpapers[i]);
139
140    return result;
141}
142

Archive Download this file



interactive