diff --git a/include/nana/basic_types.hpp b/include/nana/basic_types.hpp index da37272d..7eaa4a6b 100644 --- a/include/nana/basic_types.hpp +++ b/include/nana/basic_types.hpp @@ -67,7 +67,7 @@ namespace nana } }; - typedef std::basic_string > cistring; + typedef std::basic_string > cistring; namespace detail diff --git a/include/nana/filesystem/filesystem.hpp b/include/nana/filesystem/filesystem.hpp index 785b3c5c..f25d3b06 100644 --- a/include/nana/filesystem/filesystem.hpp +++ b/include/nana/filesystem/filesystem.hpp @@ -146,12 +146,18 @@ namespace filesystem using string_type = std::basic_string; path(); - path(const value_type* source); - path(const string_type& source); + + template + 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_; }; diff --git a/include/nana/paint/detail/image_impl_interface.hpp b/include/nana/paint/detail/image_impl_interface.hpp index 6cd604fb..379e8027 100644 --- a/include/nana/paint/detail/image_impl_interface.hpp +++ b/include/nana/paint/detail/image_impl_interface.hpp @@ -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; diff --git a/include/nana/paint/image.hpp b/include/nana/paint/image.hpp index b1c237bc..db70ce0a 100644 --- a/include/nana/paint/image.hpp +++ b/include/nana/paint/image.hpp @@ -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 + 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); diff --git a/source/filesystem/filesystem.cpp b/source/filesystem/filesystem.cpp index 723e365b..08c46e2b 100644 --- a/source/filesystem/filesystem.cpp +++ b/source/filesystem/filesystem.cpp @@ -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) diff --git a/source/gui/detail/window_manager.cpp b/source/gui/detail/window_manager.cpp index eb56ff89..93b3bf2d 100644 --- a/source/gui/detail/window_manager.cpp +++ b/source/gui/detail/window_manager.cpp @@ -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) diff --git a/source/gui/place.cpp b/source/gui/place.cpp index 6b59649c..9eff87ea 100644 --- a/source/gui/place.cpp +++ b/source/gui/place.cpp @@ -1014,7 +1014,7 @@ namespace nana auto find_lowest = [&revises](double level_px) { - double v = std::numeric_limits::max(); + double v = (std::numeric_limits::max)(); for (auto i = revises.begin(); i != revises.end(); ++i) { if (i->min_px >= 0 && i->min_px < v && i->min_px > level_px) diff --git a/source/gui/widgets/listbox.cpp b/source/gui/widgets/listbox.cpp index d6b56c73..fca1ef68 100644 --- a/source/gui/widgets/listbox.cpp +++ b/source/gui/widgets/listbox.cpp @@ -3155,7 +3155,7 @@ namespace nana graph->rectangle(rectangle{ item_xpos + static_cast(header.pixels), y + 2, ts.width + static_cast(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); } } } diff --git a/source/paint/detail/image_bmp.hpp b/source/paint/detail/image_bmp.hpp index 72d7f851..6ff94496 100644 --- a/source/paint/detail/image_bmp.hpp +++ b/source/paint/detail/image_bmp.hpp @@ -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(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); diff --git a/source/paint/detail/image_ico.hpp b/source/paint/detail/image_ico.hpp index 5be39454..a00c2820 100644 --- a/source/paint/detail/image_ico.hpp +++ b/source/paint/detail/image_ico.hpp @@ -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; diff --git a/source/paint/image.cpp b/source/paint/image.cpp index 6dbf465b..9b10e696 100644 --- a/source/paint/image.cpp +++ b/source/paint/image.cpp @@ -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(&meta), 2); - if(*reinterpret_cast("BM") == meta) + if (*reinterpret_cast("BM") == meta) helper = new detail::image_bmp; - else if(*reinterpret_cast("MZ") == meta) + else if (*reinterpret_cast("MZ") == meta) helper = new detail::image_ico(false); } } - if(helper) + if (helper) { image_ptr_ = std::shared_ptr(helper); - return helper->open(filename.data()); + return helper->open(file.c_str()); } } return false;