Root/src/wallpaperdialog.cpp

Source at commit 9394a95a6c43f853ce615706b8ff30992489d6cc created 5 years 5 months ago.
By Maarten ter Huurne, Fixed formatting string type mismatches
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 "wallpaperdialog.h"
22
23#include "debug.h"
24#include "buttonbox.h"
25#include "filelister.h"
26#include "gmenu2x.h"
27#include "iconbutton.h"
28#include "surface.h"
29#include "utilities.h"
30
31#include <iostream>
32
33using namespace std;
34
35WallpaperDialog::WallpaperDialog(GMenu2X& gmenu2x)
36    : Dialog(gmenu2x)
37{
38}
39
40bool WallpaperDialog::exec()
41{
42    bool close = false, result = true;
43
44    FileLister fl;
45    fl.setShowDirectories(false);
46    fl.setFilter("png");
47
48    fl.browse(GMenu2X::getHome() + "/skins/"
49        + gmenu2x.confStr["skin"] + "/wallpapers", true);
50    fl.browse(GMENU2X_SYSTEM_DIR "/skins/"
51        + gmenu2x.confStr["skin"] + "/wallpapers", false);
52
53    if (gmenu2x.confStr["skin"] != "Default") {
54        fl.browse(GMenu2X::getHome() + "/skins/Default/wallpapers", false);
55        fl.browse(GMENU2X_SYSTEM_DIR "/skins/Default/wallpapers", false);
56    }
57
58    vector<string> wallpapers = fl.getFiles();
59
60    DEBUG("Wallpapers: %zd\n", wallpapers.size());
61
62    uint i, selected = 0, firstElement = 0, iY;
63
64    ButtonBox buttonbox;
65    buttonbox.add(unique_ptr<IconButton>(new IconButton(gmenu2x, "skin:imgs/buttons/accept.png", gmenu2x.tr["Select"])));
66    buttonbox.add(unique_ptr<IconButton>(new IconButton(gmenu2x, "skin:imgs/buttons/cancel.png", gmenu2x.tr["Exit"])));
67
68    unsigned int top, height;
69    tie(top, height) = gmenu2x.getContentArea();
70
71    int fontheight = gmenu2x.font->getLineSpacing();
72    unsigned int nb_elements = height / fontheight;
73
74    while (!close) {
75        OutputSurface& s = *gmenu2x.s;
76
77        if (selected > firstElement + nb_elements - 1)
78            firstElement = selected - nb_elements + 1;
79        if (selected < firstElement)
80            firstElement = selected;
81
82        //Wallpaper
83        gmenu2x.sc[((string)"skin:wallpapers/" + wallpapers[selected]).c_str()]->blit(s, 0, 0);
84
85        gmenu2x.drawTopBar(s);
86        gmenu2x.drawBottomBar(s);
87
88        drawTitleIcon(s, "icons/wallpaper.png", true);
89        writeTitle(s, gmenu2x.tr["Wallpaper selection"]);
90        writeSubTitle(s, gmenu2x.tr["Select a wallpaper from the list"]);
91
92        buttonbox.paint(s, 5, gmenu2x.resY - 1);
93
94        //Selection
95        iY = selected - firstElement;
96        iY = top + (iY * fontheight);
97        s.box(2, iY, 308, fontheight, gmenu2x.skinConfColors[COLOR_SELECTION_BG]);
98
99        //Files & Directories
100        s.setClipRect(0, top, 311, height);
101        for (i = firstElement; i < wallpapers.size()
102                    && i < firstElement + nb_elements; i++) {
103            iY = i-firstElement;
104            gmenu2x.font->write(s, wallpapers[i], 5,
105                        top + (iY * fontheight),
106                        Font::HAlignLeft, Font::VAlignTop);
107        }
108        s.clearClipRect();
109
110        gmenu2x.drawScrollBar(nb_elements, wallpapers.size(), firstElement);
111        s.flip();
112
113        switch(gmenu2x.input.waitForPressedButton()) {
114            case InputManager::CANCEL:
115                close = true;
116                result = false;
117                break;
118            case InputManager::UP:
119                if (selected == 0) selected = wallpapers.size()-1;
120                else selected -= 1;
121                break;
122            case InputManager::ALTLEFT:
123                if ((int)(selected - nb_elements + 1) < 0)
124                    selected = 0;
125                else
126                    selected -= nb_elements - 1;
127                break;
128            case InputManager::DOWN:
129                if (selected+1 >= wallpapers.size()) selected = 0;
130                else selected += 1;
131                break;
132            case InputManager::ALTRIGHT:
133                if (selected + nb_elements - 1 >= wallpapers.size())
134                    selected = wallpapers.size() - 1;
135                else
136                    selected += nb_elements - 1;
137                break;
138            case InputManager::ACCEPT:
139                close = true;
140                if (wallpapers.size() > 0)
141                    wallpaper = gmenu2x.sc.getSkinFilePath("wallpapers/" + wallpapers[selected]);
142                else result = false;
143            default:
144                break;
145        }
146    }
147
148    for (uint i=0; i<wallpapers.size(); i++) {
149      gmenu2x.sc.del("skin:wallpapers/" + wallpapers[i]);
150    }
151
152    return result;
153}
154

Archive Download this file



interactive