Skip to content
This repository has been archived by the owner on Jan 13, 2020. It is now read-only.

Commit

Permalink
Make WallpaperDialog a subclass of BrowseDialog
Browse files Browse the repository at this point in the history
The implementation of BrowseDialog is reused in WallpaperDialog,
shortening the code in WallpaperDialog and unifying button actions.
  • Loading branch information
Nebuleon committed Oct 7, 2014
1 parent c649e24 commit 7862677
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 108 deletions.
11 changes: 7 additions & 4 deletions src/gmenu2x.cpp
Expand Up @@ -882,10 +882,13 @@ void GMenu2X::showContextMenu() {

void GMenu2X::changeWallpaper() {
WallpaperDialog wp(this);
if (wp.exec() && confStr["wallpaper"] != wp.wallpaper) {
confStr["wallpaper"] = wp.wallpaper;
initBG();
writeConfig();
if (wp.exec()) {
string newWallpaper = wp.getFullPath();
if (confStr["wallpaper"] != newWallpaper) {
confStr["wallpaper"] = newWallpaper;
initBG();
writeConfig();
}
}
}

Expand Down
126 changes: 25 additions & 101 deletions src/wallpaperdialog.cpp
Expand Up @@ -21,30 +21,22 @@
#include "wallpaperdialog.h"

#include "debug.h"
#include "buttonbox.h"
#include "filelister.h"
#include "gmenu2x.h"
#include "iconbutton.h"
#include "surface.h"
#include "utilities.h"

#include <iostream>

using namespace std;

WallpaperDialog::WallpaperDialog(GMenu2X *gmenu2x)
: Dialog(gmenu2x)
: BrowseDialog(gmenu2x,
gmenu2x->tr["Wallpaper selection"],
gmenu2x->tr["Select a wallpaper from the list"])
{
}

bool WallpaperDialog::exec()
{
bool close = false, result = true;

FileLister fl;
fl.setShowDirectories(false);
fl.setFilter("png");
}

void WallpaperDialog::initPath()
{
fl.browse(GMenu2X::getHome() + "/skins/"
+ gmenu2x->confStr["skin"] + "/wallpapers", true);
fl.browse(GMENU2X_SYSTEM_DIR "/skins/"
Expand All @@ -55,98 +47,30 @@ bool WallpaperDialog::exec()
fl.browse(GMENU2X_SYSTEM_DIR "/skins/Default/wallpapers", false);
}

vector<string> wallpapers = fl.getFiles();

DEBUG("Wallpapers: %i\n", wallpapers.size());

uint i, selected = 0, firstElement = 0, iY;

ButtonBox buttonbox;
buttonbox.add(unique_ptr<IconButton>(new IconButton(gmenu2x, "skin:imgs/buttons/accept.png", gmenu2x->tr["Select"])));
buttonbox.add(unique_ptr<IconButton>(new IconButton(gmenu2x, "skin:imgs/buttons/cancel.png", gmenu2x->tr["Exit"])));

unsigned int top, height;
tie(top, height) = gmenu2x->getContentArea();

int fontheight = gmenu2x->font->getLineSpacing();
unsigned int nb_elements = height / fontheight;

while (!close) {
OutputSurface& s = *gmenu2x->s;

if (selected > firstElement + nb_elements - 1)
firstElement = selected - nb_elements + 1;
if (selected < firstElement)
firstElement = selected;

//Wallpaper
gmenu2x->sc[((string)"skin:wallpapers/" + wallpapers[selected]).c_str()]->blit(s, 0, 0);

gmenu2x->drawTopBar(s);
gmenu2x->drawBottomBar(s);

drawTitleIcon(s, "icons/wallpaper.png", true);
writeTitle(s, gmenu2x->tr["Wallpaper selection"]);
writeSubTitle(s, gmenu2x->tr["Select a wallpaper from the list"]);

buttonbox.paint(s, 5, gmenu2x->resY - 1);
DEBUG("Wallpapers: %i\n", fl.size());
}

//Selection
iY = selected - firstElement;
iY = top + (iY * fontheight);
s.box(2, iY, 308, fontheight, gmenu2x->skinConfColors[COLOR_SELECTION_BG]);
void WallpaperDialog::paintBackground()
{
// Preview the new wallpaper.
gmenu2x->sc["skin:wallpapers/" + fl[selected]]->blit(*gmenu2x->s, 0, 0);

//Files & Directories
s.setClipRect(0, top, 311, height);
for (i = firstElement; i < wallpapers.size()
&& i < firstElement + nb_elements; i++) {
iY = i-firstElement;
gmenu2x->font->write(s, wallpapers[i], 5,
top + (iY * fontheight),
Font::HAlignLeft, Font::VAlignTop);
}
s.clearClipRect();
gmenu2x->drawTopBar(*gmenu2x->s);
gmenu2x->drawBottomBar(*gmenu2x->s);
}

gmenu2x->drawScrollBar(nb_elements, wallpapers.size(), firstElement);
s.flip();
bool WallpaperDialog::exec()
{
bool result = BrowseDialog::exec();

switch(gmenu2x->input.waitForPressedButton()) {
case InputManager::CANCEL:
close = true;
result = false;
break;
case InputManager::UP:
if (selected == 0) selected = wallpapers.size()-1;
else selected -= 1;
break;
case InputManager::ALTLEFT:
if ((int)(selected - nb_elements + 1) < 0)
selected = 0;
else
selected -= nb_elements - 1;
break;
case InputManager::DOWN:
if (selected+1 >= wallpapers.size()) selected = 0;
else selected += 1;
break;
case InputManager::ALTRIGHT:
if (selected + nb_elements - 1 >= wallpapers.size())
selected = wallpapers.size() - 1;
else
selected += nb_elements - 1;
break;
case InputManager::ACCEPT:
close = true;
if (wallpapers.size() > 0)
wallpaper = gmenu2x->sc.getSkinFilePath("wallpapers/" + wallpapers[selected]);
else result = false;
default:
break;
}
for (size_t i = 0; i < fl.size(); i++) {
gmenu2x->sc.del("skin:wallpapers/" + fl[i]);
}

for (uint i=0; i<wallpapers.size(); i++)
gmenu2x->sc.del("skin:wallpapers/" + wallpapers[i]);

return result;
}

string WallpaperDialog::getFullPath()
{
return gmenu2x->sc.getSkinFilePath("wallpapers/" + fl[selected]);
}
11 changes: 8 additions & 3 deletions src/wallpaperdialog.h
Expand Up @@ -21,16 +21,21 @@
#ifndef WALLPAPERDIALOG_H
#define WALLPAPERDIALOG_H

#include "dialog.h"
#include "browsedialog.h"

#include <string>

class WallpaperDialog : protected Dialog {
class WallpaperDialog : public BrowseDialog {
public:
WallpaperDialog(GMenu2X *gmenu2x);
std::string wallpaper;

bool exec();

std::string getFullPath();

protected:
virtual void initPath() override;
virtual void paintBackground() override;
};

#endif // WALLPAPERDIALOG_H

0 comments on commit 7862677

Please sign in to comment.