Skip to content

Commit

Permalink
deprecate drawing
Browse files Browse the repository at this point in the history
  • Loading branch information
cnjinhao committed Jun 19, 2024
1 parent b98d54f commit 8d79df0
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 86 deletions.
2 changes: 0 additions & 2 deletions include/nana/gui.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
#include "gui/compact.hpp"
#include "gui/screen.hpp"
#include "gui/widgets/form.hpp"
#ifndef NANA_DRAWING_REMOVED
#include "gui/drawing.hpp"
#endif
#include "gui/msgbox.hpp"
#include "gui/place.hpp"

Expand Down
3 changes: 3 additions & 0 deletions include/nana/gui/basis.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ namespace nana
struct native_window_handle_impl;
struct native_drawable_impl;
struct event_handle_impl;
struct drawing_handle_impl;
}

using drawing_handle = detail::drawing_handle_impl*;

#ifdef NANA_X11
namespace x11
{
Expand Down
11 changes: 3 additions & 8 deletions include/nana/gui/detail/drawer.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* A Drawer Implementation
* Nana C++ Library(https://nana.acemind.cn)
* Copyright(C) 2003-2017 Jinhao([email protected])
* Copyright(C) 2003-2024 Jinhao([email protected])
*
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
Expand Down Expand Up @@ -152,14 +152,9 @@ namespace nana
void attached(widget&, drawer_trigger&);
drawer_trigger* detached();
public:
std::function<void(paint::graphics&)> drawing() const;
void drawing(std::function<void(paint::graphics&)>&&);

#ifndef NANA_DRAWING_REMOVED
void clear();
void* draw(std::function<void(paint::graphics&)> &&, bool diehard);
void erase(void* diehard);
#endif
drawing_handle drawing(std::function<void(paint::graphics&)>&&, bool diehard) noexcept;
void erase(drawing_handle) noexcept;
private:
void _m_effect_bground_subsequent();
method_state& _m_mth_state(int pos);
Expand Down
4 changes: 0 additions & 4 deletions include/nana/gui/drawing.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
* @file: nana/gui/drawing.hpp
*/

#ifndef NANA_DRAWING_REMOVED

#ifndef NANA_GUI_DRAWING_HPP
#define NANA_GUI_DRAWING_HPP

Expand Down Expand Up @@ -55,5 +53,3 @@ namespace nana

#include <nana/pop_ignore_diagnostic>
#endif

#endif //NANA_DRAWING_REMOVED
4 changes: 2 additions & 2 deletions include/nana/gui/programming_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -534,8 +534,8 @@ namespace api
/// the specified window is a text editor window, false otherwise.
bool keyboard_numeric(window, bool padding);

std::function<void(paint::graphics&)> drawing(window);
void drawing(window, std::function<void(paint::graphics&)>);
drawing_handle drawing(window, std::function<void(paint::graphics&)>) noexcept;
void remove_drawing(window, drawing_handle) noexcept;
}//end namespace api

namespace API = api;
Expand Down
5 changes: 2 additions & 3 deletions include/nana/gui/widgets/widget.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* The fundamental widget class implementation
* Nana C++ Library(https://nana.acemind.cn)
* Copyright(C) 2003-2020 Jinhao([email protected])
* Copyright(C) 2003-2024 Jinhao([email protected])
*
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
Expand Down Expand Up @@ -128,8 +128,7 @@ namespace nana
operator dummy_bool_type() const;
operator window() const;

std::function<void(paint::graphics&)> drawing() const;
void drawing(std::function<void(paint::graphics&)>);
drawing_handle drawing(std::function<void(paint::graphics&)>);

protected:
std::unique_ptr<::nana::detail::widget_notifier_interface> _m_wdg_notifier();
Expand Down
69 changes: 26 additions & 43 deletions source/gui/detail/drawer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,7 @@ namespace nana
drawer_trigger* realizer{ nullptr };
method_state mth_state[event_size];

std::function<void(paint::graphics&)> draw;

#ifndef NANA_DRAWING_REMOVED
std::vector<std::pair<std::function<void(paint::graphics&)>, bool>*> draws; //Drawing function and flag for clearable
#endif
};

drawer::drawer()
Expand All @@ -241,10 +237,8 @@ namespace nana

drawer::~drawer()
{
#ifndef NANA_DRAWING_REMOVED
for(auto p : data_impl_->draws)
delete p;
#endif

delete data_impl_;
}
Expand Down Expand Up @@ -422,67 +416,56 @@ namespace nana
return nullptr;
}

std::function<void(paint::graphics&)> drawer::drawing() const
{
return data_impl_->draw;
}

void drawer::drawing(std::function<void(paint::graphics&)>&& fn)
drawing_handle drawer::drawing(std::function<void(paint::graphics&)>&& fn, bool diehard) noexcept
{
data_impl_->draw = std::move(fn);
}

#ifndef NANA_DRAWING_REMOVED
void drawer::clear()
{
for (auto i = data_impl_->draws.cbegin(); i != data_impl_->draws.cend();)
std::pair<std::function<void(paint::graphics&)>, bool>* pdw = nullptr;
try {
pdw = new std::pair<std::function<void(paint::graphics&)>, bool>(std::move(fn), diehard);
data_impl_->draws.emplace_back(pdw);
return reinterpret_cast<drawing_handle>(pdw);
}
catch (...)
{
if ((*i)->second)
{
delete (*i);
i = data_impl_->draws.erase(i);
}
else
++i;
delete pdw;
return nullptr;
}
}

void* drawer::draw(std::function<void(paint::graphics&)> && f, bool diehard)
void drawer::erase(drawing_handle dw) noexcept
{
if(f)
for (auto i = data_impl_->draws.cbegin(); i != data_impl_->draws.cend(); ++i)
{
auto p = new std::pair<std::function<void(paint::graphics&)>, bool>(std::move(f), !diehard);
data_impl_->draws.emplace_back(p);
return p;
if (reinterpret_cast<drawing_handle>(*i) == dw)
{
delete (*i);
data_impl_->draws.erase(i);
return;
}
}
return nullptr;
}

void drawer::erase(void * p)
void drawer::clear()
{
for (auto i = data_impl_->draws.begin(); i != data_impl_->draws.end(); ++i)
for (auto i = data_impl_->draws.cbegin(); i != data_impl_->draws.cend();)
{
if (*i == p)
if (!(*i)->second)
{
delete (*i);
data_impl_->draws.erase(i);
return;
i = data_impl_->draws.erase(i);
}
else
++i;
}
}
#endif

void drawer::_m_effect_bground_subsequent()
{
if (data_impl_->draw)
data_impl_->draw(graphics);

auto & effect = data_impl_->window_handle->effect;

#ifndef NANA_DRAWING_REMOVED
for (auto * dw : data_impl_->draws)
dw->first(graphics);
#endif

auto& effect = data_impl_->window_handle->effect;
if (effect.bground)
{
if (effect.bground_fade_rate >= 0.01)
Expand Down
21 changes: 12 additions & 9 deletions source/gui/drawing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
* @file: nana/gui/drawing.cpp
*/

#ifndef NANA_DRAWING_REMOVED
#include "detail/basic_window.hpp"
#include <nana/gui/drawing.hpp>
#include <nana/gui/programming_interface.hpp>
Expand Down Expand Up @@ -55,42 +54,46 @@ namespace nana

void drawing::draw(const draw_fn_t& f)
{
internal_scope_guard lock;
if(api::empty_window(handle_)) return;
restrict::get_drawer(handle_).draw(draw_fn_t(f), false);
restrict::get_drawer(handle_).drawing(draw_fn_t(f), false);
}

void drawing::draw(draw_fn_t&& f)
{
internal_scope_guard lock;
if(api::empty_window(handle_)) return;
restrict::get_drawer(handle_).draw(std::move(f), false);
restrict::get_drawer(handle_).drawing(std::move(f), false);
}

drawing::diehard_t drawing::draw_diehard(const draw_fn_t& f)
{
internal_scope_guard lock;
if(api::empty_window(handle_)) return nullptr;
return reinterpret_cast<diehard_t>(restrict::get_drawer(handle_).draw(draw_fn_t(f), true));
return reinterpret_cast<diehard_t>(restrict::get_drawer(handle_).drawing(draw_fn_t(f), true));
}

drawing::diehard_t drawing::draw_diehard(draw_fn_t&& f)
{
internal_scope_guard lock;
if(api::empty_window(handle_)) return nullptr;
return reinterpret_cast<diehard_t>(restrict::get_drawer(handle_).draw(std::move(f), true));
return reinterpret_cast<diehard_t>(restrict::get_drawer(handle_).drawing(std::move(f), true));
}

void drawing::erase(diehard_t d)
{
internal_scope_guard lock;
//Fixed by Tumiz
//https://github.com/cnjinhao/nana/issues/153
if(!api::empty_window(handle_))
restrict::get_drawer(handle_).erase(d);
restrict::get_drawer(handle_).erase(reinterpret_cast<drawing_handle>(d));
}

void drawing::clear()
{
internal_scope_guard lock;
if(api::empty_window(handle_)) return;
restrict::get_drawer(handle_).clear();
}
//end class drawing
}//end namespace nana

#endif //NANA_DRAWING_REMOVED
}//end namespace nana
14 changes: 6 additions & 8 deletions source/gui/programming_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1755,22 +1755,20 @@ namespace api
#endif
}

std::function<void(paint::graphics&)> drawing(window wd)
drawing_handle drawing(window wd, std::function<void(paint::graphics&)> fn) noexcept
{
internal_scope_guard lock;
if (!is_window(wd))
return {};
return nullptr;

return wd->drawer.drawing();
return wd->drawer.drawing(std::move(fn), false);
}

void drawing(window wd, std::function<void(paint::graphics&)> fn)
void remove_drawing(window wd, drawing_handle dw) noexcept
{
internal_scope_guard lock;
if (!is_window(wd))
return;

wd->drawer.drawing(std::move(fn));
if (is_window(wd))
wd->drawer.erase(dw);
}
}//end namespace api
}//end namespace nana
9 changes: 2 additions & 7 deletions source/gui/widgets/widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,14 +293,9 @@ namespace nana
return handle();
}

std::function<void(paint::graphics&)> widget::drawing() const
drawing_handle widget::drawing(std::function<void(paint::graphics&)> draw_fn)
{
return api::drawing(handle());
}

void widget::drawing(std::function<void(paint::graphics&)> draw_fn)
{
api::drawing(handle(), std::move(draw_fn));
return api::drawing(handle(), std::move(draw_fn));
}

std::unique_ptr<::nana::detail::widget_notifier_interface> widget::_m_wdg_notifier()
Expand Down

0 comments on commit 8d79df0

Please sign in to comment.