From 5ab0cfdd17ae2ca1b9063abb6428197c70cba4c9 Mon Sep 17 00:00:00 2001 From: Jinhao Date: Sun, 5 Jun 2016 02:36:38 +0800 Subject: [PATCH] fix a notifier error that can't set icon from its exe file --- include/nana/gui/notifier.hpp | 2 +- source/gui/detail/native_window_interface.cpp | 19 +---- source/gui/notifier.cpp | 77 ++++++++----------- source/paint/image.cpp | 17 ++++ source/paint/image_accessor.hpp | 30 ++++++++ 5 files changed, 84 insertions(+), 61 deletions(-) create mode 100644 source/paint/image_accessor.hpp diff --git a/include/nana/gui/notifier.hpp b/include/nana/gui/notifier.hpp index 48442a96..3e136cdb 100644 --- a/include/nana/gui/notifier.hpp +++ b/include/nana/gui/notifier.hpp @@ -1,7 +1,7 @@ /* * Definition of Notifier * Nana C++ Library(http://www.nanapro.org) - * Copyright(C) 2003-2015 Jinhao(cnjinhao@hotmail.com) + * Copyright(C) 2003-2016 Jinhao(cnjinhao@hotmail.com) * * Distributed under the Boost Software License, Version 1.0. * (See accompanying file LICENSE_1_0.txt or copy at diff --git a/source/gui/detail/native_window_interface.cpp b/source/gui/detail/native_window_interface.cpp index b21eb270..4f356b83 100644 --- a/source/gui/detail/native_window_interface.cpp +++ b/source/gui/detail/native_window_interface.cpp @@ -28,24 +28,9 @@ #include #endif -namespace nana{ - namespace paint - { - class image_accessor - { - public: -#if defined(NANA_WINDOWS) - static HICON icon(const nana::paint::image& img) - { - auto ico = dynamic_cast(img.image_ptr_.get()); - if(ico && ico->ptr()) - return *(ico->ptr()); - return nullptr; - } -#endif - }; - } +#include "../../paint/image_accessor.hpp" +namespace nana{ namespace detail{ #if defined(NANA_WINDOWS) diff --git a/source/gui/notifier.cpp b/source/gui/notifier.cpp index 4a9f6ac3..d541788b 100644 --- a/source/gui/notifier.cpp +++ b/source/gui/notifier.cpp @@ -1,17 +1,17 @@ /* - * Implementation of Notifier - * Nana C++ Library(http://www.nanapro.org) - * Copyright(C) 2003-2015 Jinhao(cnjinhao@hotmail.com) - * - * Distributed under the Boost Software License, Version 1.0. - * (See accompanying file LICENSE_1_0.txt or copy at - * http://www.boost.org/LICENSE_1_0.txt) - * - * @file: nana/gui/notifier.cpp - * @contributors: - * Jan - * Benjamin Navarro(pr#81) - */ +* Implementation of Notifier +* Nana C++ Library(http://www.nanapro.org) +* Copyright(C) 2003-2016 Jinhao(cnjinhao@hotmail.com) +* +* Distributed under the Boost Software License, Version 1.0. +* (See accompanying file LICENSE_1_0.txt or copy at +* http://www.boost.org/LICENSE_1_0.txt) +* +* @file: nana/gui/notifier.cpp +* @contributors: +* Jan +* Benjamin Navarro(pr#81) +*/ #include #include #include @@ -33,9 +33,11 @@ #include #endif +#include "../paint/image_accessor.hpp" + namespace nana { - typedef std::lock_guard lock_guard; + using lock_guard = std::lock_guard; struct notifier::implement { @@ -47,13 +49,15 @@ namespace nana detail::notifier_events events; bool icon_added = false; std::size_t play_index; -#if defined(NANA_WINDOWS) - HICON icon_handle = nullptr; - std::vector icons; - void set_icon(HICON icon) + paint::image icon; + ::std::vector icons; + + void set_icon(const paint::image& ico) { - if (icon_handle) +#if defined(NANA_WINDOWS) + auto ico_handle = paint::image_accessor::icon(ico); + if (ico_handle) { NOTIFYICONDATA icon_data; memset(&icon_data, 0, sizeof icon_data); @@ -62,13 +66,13 @@ namespace nana icon_data.uID = id; icon_data.uFlags = NIF_MESSAGE | NIF_ICON; icon_data.uCallbackMessage = nana::detail::messages::tray; - icon_data.hIcon = icon; + icon_data.hIcon = ico_handle; ::Shell_NotifyIcon(icon_added ? NIM_MODIFY : NIM_ADD, &icon_data); icon_added = true; } - } #endif + } }; arg_notifier::operator nana::arg_mouse() const @@ -287,12 +291,6 @@ namespace nana icon_data.hWnd = reinterpret_cast(impl_->native_handle); icon_data.uID = impl_->id; ::Shell_NotifyIcon(NIM_DELETE, &icon_data); - - if (impl_->icon_handle) - ::DestroyIcon(impl_->icon_handle); - - for (auto handle : impl_->icons) - ::DestroyIcon(handle); #endif API::umake_event(impl_->evt_destroy); notifications::instance().cancel(impl_->native_handle, impl_->id); @@ -321,30 +319,23 @@ namespace nana void notifier::icon(const std::string& icon_file) { -#if defined(NANA_WINDOWS) - auto pre_icon = impl_->icon_handle; - auto ico = (HICON)::LoadImageW(0, to_wstring(icon_file).data(), IMAGE_ICON, 0, 0, LR_LOADFROMFILE); - if (ico) + paint::image image_ico{ icon_file }; + auto icon_handle = paint::image_accessor::icon(image_ico); + if (icon_handle) { - impl_->icon_handle = ico; impl_->ani_timer.stop(); impl_->play_index = 0; - impl_->set_icon(impl_->icon_handle); - ::DestroyIcon(pre_icon); + impl_->set_icon(image_ico); + impl_->icon = image_ico; } -#else - static_cast(icon_file); //to eliminate unused parameter compiler warning -#endif } void notifier::insert_icon(const std::string& icon_file) { -#if defined(NANA_WINDOWS) - auto icon = (HICON)::LoadImage(0, to_wstring(icon_file).data(), IMAGE_ICON, 0, 0, LR_LOADFROMFILE); - impl_->icons.push_back(icon); -#else - static_cast(icon_file); //to eliminate unused parameter compiler warning. -#endif + paint::image image_ico{ icon_file }; + auto icon_handle = paint::image_accessor::icon(image_ico); + if (icon_handle) + impl_->icons.emplace_back(static_cast(image_ico)); } void notifier::period(unsigned ms) diff --git a/source/paint/image.cpp b/source/paint/image.cpp index 529c5c44..a1e2c889 100644 --- a/source/paint/image.cpp +++ b/source/paint/image.cpp @@ -34,10 +34,27 @@ #include "detail/image_bmp.hpp" #include "detail/image_ico.hpp" +#include "image_accessor.hpp" + namespace nana { namespace paint { +#if defined(NANA_WINDOWS) + HICON image_accessor::icon(const nana::paint::image& img) + { + auto ico = dynamic_cast(img.image_ptr_.get()); + if (ico && ico->ptr()) + return *(ico->ptr()); + return nullptr; + } +#else + int image_accessor::icon(const image&) + { + return 0; + } +#endif + namespace detail { //class image_ico diff --git a/source/paint/image_accessor.hpp b/source/paint/image_accessor.hpp new file mode 100644 index 00000000..73060e7e --- /dev/null +++ b/source/paint/image_accessor.hpp @@ -0,0 +1,30 @@ +/* +* Paint Image Accessor +* Nana C++ Library(http://www.nanapro.org) +* Copyright(C) 2003-2016 Jinhao(cnjinhao@hotmail.com) +* +* Distributed under the Boost Software License, Version 1.0. +* (See accompanying file LICENSE_1_0.txt or copy at +* http://www.boost.org/LICENSE_1_0.txt) +* +* @file nana/paint/image_accessor.hpp +* @brief A declaration of class image_accessor. It is used to access image private data, internal use. +*/ +#ifndef NANA_PAINT_IMAGE_ACCESS_HEADER_INCLUDED +#define NANA_PAINT_IMAGE_ACCESS_HEADER_INCLUDED +namespace nana +{ + namespace paint + { + class image_accessor + { + public: +#if defined(NANA_WINDOWS) + static HICON icon(const image&); +#else + static int icon(const image&); +#endif + }; + } +} +#endif \ No newline at end of file