mirror of
https://github.com/aseprite/aseprite.git
synced 2025-03-18 16:21:07 +00:00
Now the recent list of paths can contain 16 paths different from the recent list of files.
- Added RecentItems class in base library.
This commit is contained in:
parent
9db66ce6b2
commit
6bcf559b35
@ -439,8 +439,8 @@ bool app_rebuild_recent_list()
|
||||
submenu = new Menu();
|
||||
list_menuitem->setSubmenu(submenu);
|
||||
|
||||
RecentFiles::const_iterator it = App::instance()->getRecentFiles()->begin();
|
||||
RecentFiles::const_iterator end = App::instance()->getRecentFiles()->end();
|
||||
RecentFiles::const_iterator it = App::instance()->getRecentFiles()->files_begin();
|
||||
RecentFiles::const_iterator end = App::instance()->getRecentFiles()->files_end();
|
||||
|
||||
if (it != end) {
|
||||
Params params;
|
||||
|
63
src/base/recent_items.h
Normal file
63
src/base/recent_items.h
Normal file
@ -0,0 +1,63 @@
|
||||
// ASEPRITE base library
|
||||
// Copyright (C) 2001-2012 David Capello
|
||||
//
|
||||
// This source file is ditributed under a BSD-like license, please
|
||||
// read LICENSE.txt for more information.
|
||||
|
||||
#ifndef BASE_RECENT_ITEMS_H_INCLUDED
|
||||
#define BASE_RECENT_ITEMS_H_INCLUDED
|
||||
|
||||
#include <list>
|
||||
#include <algorithm>
|
||||
|
||||
namespace base {
|
||||
|
||||
template<typename T>
|
||||
class RecentItems {
|
||||
public:
|
||||
typedef std::list<T> Items;
|
||||
typedef typename Items::iterator iterator;
|
||||
typedef typename Items::const_iterator const_iterator;
|
||||
|
||||
RecentItems(size_t limit) : m_limit(limit) { }
|
||||
|
||||
const_iterator begin() { return m_items.begin(); }
|
||||
const_iterator end() { return m_items.end(); }
|
||||
|
||||
size_t size() const { return m_items.size(); }
|
||||
size_t limit() const { return m_limit; }
|
||||
|
||||
void addItem(const T& item) {
|
||||
iterator it = std::find(m_items.begin(), m_items.end(), item);
|
||||
|
||||
// If the item already exist in the list...
|
||||
if (it != m_items.end()) {
|
||||
// Move it to the first position
|
||||
m_items.erase(it);
|
||||
m_items.insert(m_items.begin(), item);
|
||||
return;
|
||||
}
|
||||
|
||||
// Does the list is full?
|
||||
if (m_items.size() == m_limit) {
|
||||
// Remove the last entry
|
||||
m_items.erase(--m_items.end());
|
||||
}
|
||||
|
||||
m_items.insert(m_items.begin(), item);
|
||||
}
|
||||
|
||||
void removeItem(const T& item) {
|
||||
iterator it = std::find(m_items.begin(), m_items.end(), item);
|
||||
if (it != m_items.end())
|
||||
m_items.erase(it);
|
||||
}
|
||||
|
||||
private:
|
||||
Items m_items;
|
||||
size_t m_limit;
|
||||
};
|
||||
|
||||
} // namespace base
|
||||
|
||||
#endif
|
@ -450,22 +450,10 @@ static void update_location(Widget* window)
|
||||
newItem = location->addItem("");
|
||||
newItem = location->addItem("-------- Recent Paths --------");
|
||||
|
||||
std::set<std::string> included;
|
||||
|
||||
// For each recent file...
|
||||
RecentFiles::const_iterator it = App::instance()->getRecentFiles()->begin();
|
||||
RecentFiles::const_iterator end = App::instance()->getRecentFiles()->end();
|
||||
for (; it != end; ++it) {
|
||||
// Get the path of the recent file
|
||||
base::string path = base::get_file_path(*it);
|
||||
|
||||
// Check if the path was not already included in the list
|
||||
if (included.find(path) == included.end()) {
|
||||
included.insert(path);
|
||||
|
||||
location->addItem(path);
|
||||
}
|
||||
}
|
||||
RecentFiles::const_iterator it = App::instance()->getRecentFiles()->paths_begin();
|
||||
RecentFiles::const_iterator end = App::instance()->getRecentFiles()->paths_end();
|
||||
for (; it != end; ++it)
|
||||
location->addItem(*it);
|
||||
}
|
||||
|
||||
// Select the location
|
||||
|
@ -21,85 +21,66 @@
|
||||
#include "recent_files.h"
|
||||
|
||||
#include "app.h"
|
||||
#include "base/path.h"
|
||||
#include "ini_file.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
|
||||
RecentFiles::RecentFiles()
|
||||
: m_limit(16)
|
||||
: m_files(16)
|
||||
, m_paths(16)
|
||||
{
|
||||
const char* filename;
|
||||
char buf[512];
|
||||
int c;
|
||||
|
||||
for (c=m_limit-1; c>=0; c--) {
|
||||
for (int c=m_files.limit()-1; c>=0; c--) {
|
||||
sprintf(buf, "Filename%02d", c);
|
||||
|
||||
filename = get_config_string("RecentFiles", buf, NULL);
|
||||
if ((filename) && (*filename))
|
||||
addRecentFile(filename);
|
||||
const char* filename = get_config_string("RecentFiles", buf, NULL);
|
||||
if (filename && *filename)
|
||||
m_files.addItem(filename);
|
||||
}
|
||||
|
||||
for (int c=m_paths.limit()-1; c>=0; c--) {
|
||||
sprintf(buf, "Path%02d", c);
|
||||
|
||||
const char* path = get_config_string("RecentPaths", buf, NULL);
|
||||
if (path && *path)
|
||||
m_paths.addItem(path);
|
||||
}
|
||||
}
|
||||
|
||||
RecentFiles::~RecentFiles()
|
||||
{
|
||||
char buf[512];
|
||||
int c = 0;
|
||||
|
||||
for (const_iterator it = begin(); it != end(); ++it) {
|
||||
int c = 0;
|
||||
for (const_iterator it = files_begin(); it != files_end(); ++it) {
|
||||
const char* filename = it->c_str();
|
||||
sprintf(buf, "Filename%02d", c);
|
||||
set_config_string("RecentFiles", buf, filename);
|
||||
c++;
|
||||
}
|
||||
|
||||
m_files.clear();
|
||||
}
|
||||
|
||||
RecentFiles::const_iterator RecentFiles::begin()
|
||||
{
|
||||
return m_files.begin();
|
||||
}
|
||||
|
||||
RecentFiles::const_iterator RecentFiles::end()
|
||||
{
|
||||
return m_files.end();
|
||||
c = 0;
|
||||
for (const_iterator it = paths_begin(); it != paths_end(); ++it) {
|
||||
const char* path = it->c_str();
|
||||
sprintf(buf, "Path%02d", c);
|
||||
set_config_string("RecentPaths", buf, path);
|
||||
c++;
|
||||
}
|
||||
}
|
||||
|
||||
void RecentFiles::addRecentFile(const char* filename)
|
||||
{
|
||||
iterator it = std::find(m_files.begin(), m_files.end(), filename);
|
||||
|
||||
// If the filename already exist in the list...
|
||||
if (it != m_files.end()) {
|
||||
// Move it to the first position
|
||||
m_files.erase(it);
|
||||
m_files.insert(m_files.begin(), filename);
|
||||
|
||||
app_rebuild_recent_list();
|
||||
return;
|
||||
}
|
||||
|
||||
// If the filename does not exist...
|
||||
|
||||
// Does the list is full?
|
||||
if ((int)m_files.size() == m_limit) {
|
||||
// Remove the last entry
|
||||
m_files.erase(--m_files.end());
|
||||
}
|
||||
|
||||
m_files.insert(m_files.begin(), filename);
|
||||
m_files.addItem(filename);
|
||||
m_paths.addItem(base::get_file_path(filename));
|
||||
app_rebuild_recent_list();
|
||||
}
|
||||
|
||||
void RecentFiles::removeRecentFile(const char* filename)
|
||||
{
|
||||
iterator it = std::find(m_files.begin(), m_files.end(), filename);
|
||||
|
||||
if (it != m_files.end()) {
|
||||
m_files.erase(it);
|
||||
app_rebuild_recent_list();
|
||||
}
|
||||
m_files.removeItem(filename);
|
||||
m_paths.removeItem(base::get_file_path(filename));
|
||||
app_rebuild_recent_list();
|
||||
}
|
||||
|
@ -19,18 +19,24 @@
|
||||
#ifndef RECENT_FILES_H_INCLUDED
|
||||
#define RECENT_FILES_H_INCLUDED
|
||||
|
||||
#include <list>
|
||||
#include "base/recent_items.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
class RecentFiles
|
||||
{
|
||||
public:
|
||||
typedef std::list<std::string> FilesList;
|
||||
typedef FilesList::iterator iterator;
|
||||
typedef FilesList::const_iterator const_iterator;
|
||||
typedef base::RecentItems<std::string> List;
|
||||
typedef List::iterator iterator;
|
||||
typedef List::const_iterator const_iterator;
|
||||
|
||||
const_iterator begin();
|
||||
const_iterator end();
|
||||
// Iterate through recent files.
|
||||
const_iterator files_begin() { return m_files.begin(); }
|
||||
const_iterator files_end() { return m_files.end(); }
|
||||
|
||||
// Iterate through recent paths.
|
||||
const_iterator paths_begin() { return m_paths.begin(); }
|
||||
const_iterator paths_end() { return m_paths.end(); }
|
||||
|
||||
RecentFiles();
|
||||
~RecentFiles();
|
||||
@ -39,8 +45,8 @@ public:
|
||||
void removeRecentFile(const char* filename);
|
||||
|
||||
private:
|
||||
RecentFiles::FilesList m_files;
|
||||
int m_limit;
|
||||
List m_files;
|
||||
List m_paths;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
Loading…
x
Reference in New Issue
Block a user