replace string parameter of image's constructor with filesystem::path

This commit is contained in:
Jinhao 2015-11-30 00:58:29 +08:00
parent c86a00bea5
commit b35f293b9e
11 changed files with 103 additions and 67 deletions

View File

@ -67,7 +67,7 @@ namespace nana
}
};
typedef std::basic_string<nana::char_t, casei_char_traits<nana::char_t> > cistring;
typedef std::basic_string<wchar_t, casei_char_traits<wchar_t> > cistring;
namespace detail

View File

@ -146,12 +146,18 @@ namespace filesystem
using string_type = std::basic_string<value_type>;
path();
path(const value_type* source);
path(const string_type& source);
template<typename Source>
path(const Source& source)
{
_m_assign(source);
}
int compare(const path& other) const;
bool empty() const;
path extension() const;
path parent_path() const;
file_type what() const;
@ -160,6 +166,9 @@ namespace filesystem
const value_type*c_str() const;
const string_type& native() const;
operator string_type() const;
private:
void _m_assign(const std::string& source_utf8);
void _m_assign(const std::wstring& source);
private:
string_type pathstr_;
};

View File

@ -15,7 +15,7 @@ namespace nana{ namespace paint{
public:
typedef nana::paint::graphics& graph_reference;
virtual ~image_impl_interface() = 0; //The destructor is defined in ../image.cpp
virtual bool open(const nana::char_t* filename) = 0;
virtual bool open(const nana::experimental::filesystem::path& file) = 0;
virtual bool open(const void* data, std::size_t bytes) = 0; // reads image from memory
virtual bool alpha_channel() const = 0;
virtual bool empty() const = 0;

View File

@ -15,6 +15,7 @@
#define NANA_PAINT_IMAGE_HPP
#include "graphics.hpp"
#include "../filesystem/filesystem.hpp"
namespace nana
{
@ -31,12 +32,21 @@ namespace paint
image();
image(const image&);
image(image&&);
image(const nana::char_t* file);
image(const nana::string& filename);
//image(const nana::char_t* file);
//image(const nana::string& filename); //deprecated
image(const ::nana::experimental::filesystem::path& file);
template<typename Source>
image(const Source& source)
{
open(source);
}
~image();
image& operator=(const image& rhs);
image& operator=(image&&);
bool open(const nana::string& filename);
//bool open(const nana::string& filename); //deprecated
bool open(const ::nana::experimental::filesystem::path& file);
/// Opens an icon from a specified buffer
bool open_icon(const void* data, std::size_t bytes);

View File

@ -51,18 +51,6 @@ namespace nana {
//class path
path::path() {}
path::path(const value_type* source)
: path(string_type{ source })
{}
path::path(const string_type& source)
: pathstr_(source)
{
auto pos = pathstr_.find_last_of(splstr);
for (; (pos != string_type::npos) && (pos + 1 == pathstr_.size()); pos = pathstr_.find_last_of(splstr))
pathstr_.erase(pos);
}
int path::compare(const path& p) const
{
return pathstr_.compare(p.pathstr_);
@ -78,6 +66,23 @@ namespace nana {
#endif
}
path path::extension() const
{
#if defined(NANA_WINDOWS)
auto pos = pathstr_.find_last_of(L"\\/.");
#else
auto pos = pathstr_.find_last_of("\\/.");
#endif
if ((pos == pathstr_.npos) || (pathstr_[pos] != '.'))
return path();
if (pos + 1 == pathstr_.size())
return path();
return path(pathstr_.substr(pos));
}
path path::parent_path() const
{
return{filesystem::parent_path(pathstr_)};
@ -143,6 +148,24 @@ namespace nana {
{
return native();
}
void path::_m_assign(const std::string& source_utf8)
{
#if defined(NANA_WINDOWS)
pathstr_ = utf8_cast(source_utf8);
#else
pathstr_ = source_utf8;
#endif
}
void path::_m_assign(const std::wstring& source)
{
#if defined(NANA_WINDOWS)
pathstr_ = source;
#else
pathstr_ = utf8_cast(source);
#endif
}
//end class path
bool operator==(const path& lhs, const path& rhs)

View File

@ -463,10 +463,10 @@ namespace detail
}
}
void window_manager::default_icon(const nana::paint::image& small, const nana::paint::image& big)
void window_manager::default_icon(const nana::paint::image& small_icon, const nana::paint::image& big_icon)
{
impl_->default_icon_big = big;
impl_->default_icon_small = small;
impl_->default_icon_big = big_icon;
impl_->default_icon_small = small_icon;
}
void window_manager::icon(core_window_t* wd, const paint::image& small_icon, const paint::image& big_icon)

View File

@ -1014,7 +1014,7 @@ namespace nana
auto find_lowest = [&revises](double level_px)
{
double v = std::numeric_limits<double>::max();
double v = (std::numeric_limits<double>::max)();
for (auto i = revises.begin(); i != revises.end(); ++i)
{
if (i->min_px >= 0 && i->min_px < v && i->min_px > level_px)

View File

@ -3155,7 +3155,7 @@ namespace nana
graph->rectangle(rectangle{ item_xpos + static_cast<int>(header.pixels), y + 2,
ts.width + static_cast<unsigned>(content_pos)-header.pixels, essence_->item_size - 4 }, true);
}
extreme_text = std::max(extreme_text, item_xpos + content_pos + ts.width);
extreme_text = (std::max)(extreme_text, item_xpos + content_pos + ts.width);
}
}
}

View File

@ -82,15 +82,9 @@ namespace nana{ namespace paint
return false;
}
bool open(const nana::char_t* filename) override
bool open(const nana::experimental::filesystem::path& filename) override
{
if(nullptr == filename) return false;
std::ifstream ifs;
#if defined(NANA_UNICODE)
ifs.open(static_cast<std::string>(nana::charset(filename)).c_str(), std::ios::binary);
#else
ifs.open(filename, std::ios::binary);
#endif
std::ifstream ifs(filename.c_str(), std::ios::binary);
if(ifs)
{
ifs.seekg(0, std::ios::end);

View File

@ -23,7 +23,7 @@ namespace nana{ namespace paint
image_ico(bool is_ico);
bool open(const nana::char_t* filename) override;
bool open(const ::nana::experimental::filesystem::path& filename) override;
bool open(const void* data, std::size_t bytes) override;
bool alpha_channel() const override;
bool empty() const override;

View File

@ -40,19 +40,19 @@ namespace paint
//class image_ico
image_ico::image_ico(bool is_ico): is_ico_(is_ico){}
bool image_ico::open(const nana::char_t* filename)
bool image_ico::open(const nana::experimental::filesystem::path& file)
{
close();
#if defined(NANA_WINDOWS)
HICON handle = 0;
if(is_ico_)
{
handle = (HICON)::LoadImage(0, filename, IMAGE_ICON, 0, 0, LR_LOADFROMFILE);
handle = (HICON)::LoadImage(0, file.c_str(), IMAGE_ICON, 0, 0, LR_LOADFROMFILE);
}
else
{
SHFILEINFO sfi;
::SHGetFileInfo(filename, 0, &sfi, sizeof(sfi), SHGFI_ICON);
::SHGetFileInfo(file.c_str(), 0, &sfi, sizeof(sfi), SHGFI_ICON);
handle = sfi.hIcon;
}
@ -173,15 +173,9 @@ namespace paint
: image_ptr_(std::move(r.image_ptr_))
{}
image::image(const nana::char_t* file)
image::image(const ::nana::experimental::filesystem::path& file)
{
if(file)
open(file);
}
image::image(const nana::string& file)
{
this->open(file);
open(file);
}
image::~image()
@ -204,21 +198,33 @@ namespace paint
return *this;
}
bool image::open(const nana::string& filename)
bool image::open(const ::nana::experimental::filesystem::path& file)
{
image_ptr_.reset();
auto extension = file.extension().native();
if (extension.empty())
return false;
image::image_impl_interface * helper = nullptr;
if(filename.size())
{
auto dotpos = filename.find_last_of('.');
if (dotpos != filename.npos)
{
auto type_str = ::nana::cistring(filename.substr(dotpos + 1).data());
std::transform(extension.begin(), extension.end(), extension.begin(), std::tolower);
#if defined(NANA_WINDOWS)
const wchar_t* ext_ico = L".ico";
const wchar_t* ext_png = L".png";
const wchar_t* ext_jpg = L".jpg";
const wchar_t* ext_jpeg = L".jpeg";
#else
const char* ext_ico = ".ico";
const char* ext_png = ".png";
const char* ext_jpg = ".jpg";
const char* ext_jpeg = ".jpeg";
#endif
do
{
if (L"ICO" == type_str)
if (ext_ico == extension)
{
#if defined(NANA_WINDOWS)
helper = new detail::image_ico(true);
@ -228,7 +234,7 @@ namespace paint
break;
}
if (L"PNG" == type_str)
if (ext_png == extension)
{
#if defined(NANA_ENABLE_PNG)
helper = new detail::image_png;
@ -238,7 +244,7 @@ namespace paint
break;
}
if (L"JPG" == type_str || L"JPEG" == type_str)
if (ext_jpg == extension || ext_jpeg == extension)
{
#if defined(NANA_ENABLE_JPEG)
helper = new detail::image_jpeg;
@ -248,32 +254,26 @@ namespace paint
break;
}
} while (false);
}
//Check for BMP
if(!helper)
if (!helper)
{
#if defined(NANA_UNICODE)
std::ifstream ifs(std::string(nana::charset(filename)).c_str(), std::ios::binary);
#else
std::ifstream ifs(filename.c_str(), std::ios::binary);
#endif
if(ifs)
std::ifstream ifs(file.c_str(), std::ios::binary);
if (ifs)
{
unsigned short meta = 0;
ifs.read(reinterpret_cast<char*>(&meta), 2);
if(*reinterpret_cast<const short*>("BM") == meta)
if (*reinterpret_cast<const short*>("BM") == meta)
helper = new detail::image_bmp;
else if(*reinterpret_cast<const short*>("MZ") == meta)
else if (*reinterpret_cast<const short*>("MZ") == meta)
helper = new detail::image_ico(false);
}
}
if(helper)
if (helper)
{
image_ptr_ = std::shared_ptr<image_impl_interface>(helper);
return helper->open(filename.data());
return helper->open(file.c_str());
}
}
return false;