Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce command to share with menu and toolbar #609

Open
wants to merge 19 commits into
base: develop-1.8
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ before_install:
# donwload nana-demo first
# we are in: 'user'/nana/
- cd ..
# we are in: 'user'/
- git clone --depth=1 --branch=master https://github.com/qPCR4vir/nana-demo.git nana-demo
# we are in: 'user'/ ------->> todo: set branch back to develop or master
- git clone --depth=1 --branch=command https://github.com/qPCR4vir/nana-demo.git nana-demo
# now we have 'user'/nana-demo, 'user'/nana/ and we are in: 'user'/
- export PATH="$HOME/bin:$PATH"
- wget --no-check-certificate --no-clobber -O /tmp/tools/cmake https://cmake.org/files/v3.12/cmake-3.12.0-rc3-Linux-x86_64.sh || true
Expand Down
6 changes: 5 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ project(nana VERSION 1.8

add_library(nana)
add_library(nana::nana ALIAS nana)
target_compile_features(nana PUBLIC cxx_std_17)
target_compile_features(nana PUBLIC cxx_std_20)
# set(CMAKE_CXX_STANDARD 17)

# need after cxx_std_14 or cxx_std_17 ??
Expand Down Expand Up @@ -110,6 +110,10 @@ endif()
foreach(subdir ${NANA_INCLUDE_SUBDIRS})
aux_source_directory(${NANA_INCLUDE_DIR}/nana${subdir} HEADERS) # todo: use GLOB to add headers too !!!!!!!
endforeach()
# While this seems to work, there is no way for CMake to generate a build system that knows when a new source file has been added.
# Normally the generated build system knows when it needs to rerun CMake because the CMakeLists.txt file is modified to add a new source.
# When the source is just added to the directory without modifying this file, one would have to manually rerun CMake to generate a build system incorporating the new file.
# https://cmake.org/cmake/help/latest/command/aux_source_directory.html

### Some nana compilation options ###
option(NANA_CMAKE_AUTOMATIC_GUI_TESTING "Activate automatic GUI testing?" OFF)
Expand Down
1 change: 1 addition & 0 deletions build/vc2019/nana.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@
<ClInclude Include="..\..\include\nana\gui.hpp" />
<ClInclude Include="..\..\include\nana\gui\animation.hpp" />
<ClInclude Include="..\..\include\nana\gui\basis.hpp" />
<ClInclude Include="..\..\include\nana\gui\command.hpp" />
<ClInclude Include="..\..\include\nana\gui\dragdrop.hpp" />
<ClInclude Include="..\..\include\nana\gui\dragger.hpp" />
<ClInclude Include="..\..\include\nana\gui\drawing.hpp" />
Expand Down
1 change: 1 addition & 0 deletions build/vc2019/nana.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,7 @@
<ClInclude Include="..\..\include\nana\paint\font_info.hpp" />
<ClInclude Include="..\..\source\paint\detail\image_format_defs.hpp" />
<ClInclude Include="..\..\source\paint\detail\image_gif.hpp" />
<ClInclude Include="..\..\include\nana\gui\command.hpp" />
</ItemGroup>
<ItemGroup>
<None Include="..\..\include\nana\pop_ignore_diagnostic">
Expand Down
1 change: 1 addition & 0 deletions build/vc2022/nana.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@
<ClInclude Include="..\..\include\nana\gui.hpp" />
<ClInclude Include="..\..\include\nana\gui\animation.hpp" />
<ClInclude Include="..\..\include\nana\gui\basis.hpp" />
<ClInclude Include="..\..\include\nana\gui\command.hpp" />
<ClInclude Include="..\..\include\nana\gui\dragdrop.hpp" />
<ClInclude Include="..\..\include\nana\gui\dragger.hpp" />
<ClInclude Include="..\..\include\nana\gui\drawing.hpp" />
Expand Down
1 change: 1 addition & 0 deletions build/vc2022/nana.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,7 @@
<ClInclude Include="..\..\include\nana\paint\font_info.hpp" />
<ClInclude Include="..\..\source\paint\detail\image_format_defs.hpp" />
<ClInclude Include="..\..\source\paint\detail\image_gif.hpp" />
<ClInclude Include="..\..\include\nana\gui\command.hpp" />
</ItemGroup>
<ItemGroup>
<None Include="..\..\include\nana\pop_ignore_diagnostic">
Expand Down
76 changes: 76 additions & 0 deletions include/nana/gui/command.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* A command Implementation
* Nana C++ Library(http://www.nanapro.org)
* Copyright(C) 2021-2021 Jinhao([email protected])
*
* 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/command.hpp
* @contributor
* qPCR4vir
*/

#pragma once
#ifndef NANA_COMMAND_HPP
#define NANA_COMMAND_HPP

#include <string>
#include <functional>

#include <nana/paint/image.hpp>

namespace nana
{
namespace detail
{

}

/// A command to be shared between menus, toolbars or button widgets.
///
/// Mostly state only, with actions and drawings controlled by the widgets.
/// Some of the state-data are automatically used only at the momment of setting
/// the command at the widget.
class command
{
public:
using event_fn_t = std::function<void(command& me)>; // &me convenient, but risky
//using event_void = std::function<void()>; // simple and universal

command(std::string_view title, ///<
event_fn_t event_handler, ///<
paint::image image = {} ///<
)
: title(title), short_title(title), event_handler(event_handler), image(image)
{}

//~command();

enum class checks // todo: see toolbar enum class tools
{
none, // = toolbar button?
option, // = toolbar toggle?
highlight
};

/// A callback functor type.
//private:
std::string title; ///< always shared
std::string short_title; ///< always shared
std::string description; ///< shared only at the moment of setting at the widget
event_fn_t event_handler; ///< always shared
checks style{ checks::none }; ///< always shared
paint::image image; ///< always shared
bool enabled{ true }; ///< always shared
bool checked{ false }; ///< always shared
//mutable wchar_t hotkey{ 0 };

};

using shared_command = std::shared_ptr<command>;

}

#endif
81 changes: 47 additions & 34 deletions include/nana/gui/widgets/button.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* (See accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*
* @file: nana/gui/widgets/button.hpp
* @file nana/gui/widgets/button.hpp
* @contributor
* besh81(pr#361)
*/
Expand All @@ -16,43 +16,45 @@
#define NANA_GUI_WIDGET_BUTTON_HPP
#include "widget.hpp"
#include <nana/gui/element.hpp>
#include <nana/gui/command.hpp>
#include <nana/push_ignore_diagnostic>


namespace nana::drawerbase::button{
namespace nana::drawerbase::button
{

/// Draw the button
class trigger: public drawer_trigger
{
class content_measurer;
struct attributes;
struct impl;
public:
trigger();
~trigger();
/// Draw the button
class trigger: public drawer_trigger
{
class content_measurer;
struct attributes;
struct impl;
public:
trigger();
~trigger();

void emit_click();
bool enable_pushed(bool);
bool pushed(bool);
const impl* get_impl() const;
impl* get_impl();
private:
void attached(widget_reference, graph_reference) override;
void refresh(graph_reference) override;
void mouse_enter(graph_reference, const arg_mouse&) override;
void mouse_leave(graph_reference, const arg_mouse&) override;
void mouse_down(graph_reference, const arg_mouse&) override;
void mouse_up(graph_reference, const arg_mouse&) override;
void key_press(graph_reference, const arg_keyboard&) override;
void focus(graph_reference, const arg_focus&) override;
private:
void _m_draw_title(graph_reference, bool enabled);
void _m_draw_background(graph_reference);
void _m_draw_border(graph_reference);
void _m_press(graph_reference, bool);
private:
std::unique_ptr<impl> impl_;
};
void emit_click();
bool enable_pushed(bool);
bool pushed(bool);
const impl* get_impl() const;
impl* get_impl();
private:
void attached(widget_reference, graph_reference) override;
void refresh(graph_reference) override;
void mouse_enter(graph_reference, const arg_mouse&) override;
void mouse_leave(graph_reference, const arg_mouse&) override;
void mouse_down(graph_reference, const arg_mouse&) override;
void mouse_up(graph_reference, const arg_mouse&) override;
void key_press(graph_reference, const arg_keyboard&) override;
void focus(graph_reference, const arg_focus&) override;
private:
void _m_draw_title(graph_reference, bool enabled);
void _m_draw_background(graph_reference);
void _m_draw_border(graph_reference);
void _m_press(graph_reference, bool);
private:
std::unique_ptr<impl> impl_;
};
}//end namespace nana::drawerbase::button

namespace nana
Expand All @@ -64,14 +66,24 @@ namespace nana
{
typedef widget_object<category::widget_tag, drawerbase::button::trigger> base_type;
public:

button();

button(window parent, std::string_view title, bool visible = true);
button(window parent, std::wstring_view title, bool visible = true);
#ifdef __cpp_char8_t
button(window parent, std::u8string_view title, bool visible = false);
#endif

button(window parent,
std::string_view title, ///<
command::event_fn_t click_handler, ///<
paint::image image = {}, ///<
bool visible = true
);
button(window parent,
shared_command scommand, ///<
bool visible = true
);
button(window parent, const nana::rectangle& = rectangle(), bool visible = true);

/// Shows an icon in front of caption
Expand All @@ -95,6 +107,7 @@ namespace nana

button& edge_effects(bool enable);
private:
shared_command scommand_;
//Overrides widget virtual functions
void _m_complete_creation() override;
void _m_caption(native_string_type&&) override;
Expand Down
26 changes: 16 additions & 10 deletions include/nana/gui/widgets/menu.hpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
/**
* A Menu implementation
* Nana C++ Library(http://www.nanapro.org)
* Copyright(C) 2009-2017 Jinhao([email protected])
* Copyright(C) 2009-2021 Jinhao([email protected])
*
* 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/widgets/menu.hpp
* @file nana/gui/widgets/menu.hpp
*
*/

#ifndef NANA_GUI_WIDGETS_MENU_HPP
#define NANA_GUI_WIDGETS_MENU_HPP
#include "widget.hpp"
#include <nana/gui/command.hpp>
#include <nana/pat/cloneable.hpp>
#include <nana/push_ignore_diagnostic>

Expand Down Expand Up @@ -59,12 +60,14 @@ namespace nana
std::size_t const pos_;
::nana::menu* const menu_;
};
/// A callback functor type.

/// A callback functor type.
typedef std::function<void(item_proxy&)> event_fn_t;

//Default constructor initializes the item as a splitter
menu_item_type();
menu_item_type(std::string, const event_fn_t&);
menu_item_type(shared_command command);

struct
{
Expand All @@ -79,11 +82,12 @@ namespace nana
menu_type* menu_ptr;
}linked;

std::string text;
event_fn_t event_handler;
std::string text;
event_fn_t event_handler;
checks style{checks::none};
paint::image image;
mutable wchar_t hotkey{0};
shared_command command;
};

class renderer_interface
Expand Down Expand Up @@ -136,17 +140,19 @@ namespace nana

/// Appends an item to the menu.
item_proxy append(std::string text_utf8, const event_fn_t& handler = {});
item_proxy append(shared_command command);
void append_splitter();

/// Inserts new item at specified position
/// Inserts a new menu item at the specified position
/**
* It will invalidate the existing item proxies from the specified position.
* @param pos The position where new item to be inserted
* @param text_utf8 The title of item
* @param handler The event handler for the item.
* @return the item proxy to the new inserted item.
* @param pos The position where the new item will be inserted
* @param text_utf8 The title (text) of item
* @param handler The event handler for the item
* @return the item proxy to the new inserted item
*/
item_proxy insert(std::size_t pos, std::string text_utf8, const event_fn_t& handler = {});
item_proxy insert(std::size_t pos, shared_command command);

void clear(); ///< Erases all of the items.
/// Closes the menu. It does not destroy the menu; just close the window for the menu.
Expand Down
6 changes: 3 additions & 3 deletions include/nana/gui/widgets/menubar.hpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/*
/**
* A Menubar implementation
* Nana C++ Library(http://www.nanapro.org)
* Copyright(C) 2009-2020 Jinhao([email protected])
* Copyright(C) 2009-2021 Jinhao([email protected])
*
* 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/widgets/menubar.hpp
* @file nana/gui/widgets/menubar.hpp
*/

#ifndef NANA_GUI_WIDGETS_MENUBAR_HPP
Expand Down
7 changes: 5 additions & 2 deletions include/nana/gui/widgets/toolbar.hpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
/**
* A Toolbar Implementation
* Nana C++ Library(http://www.nanapro.org)
* Copyright(C) 2003-2020 Jinhao([email protected])
* Copyright(C) 2003-2021 Jinhao([email protected])
*
* 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/widgets/toolbar.hpp
* @file nana/gui/widgets/toolbar.hpp
*/

#ifndef NANA_GUI_WIDGET_TOOLBAR_HPP
#define NANA_GUI_WIDGET_TOOLBAR_HPP
#include <nana/gui/command.hpp>
#include <nana/push_ignore_diagnostic>

#include "widget.hpp"
Expand Down Expand Up @@ -83,6 +84,7 @@ namespace nana
// tools::dropdown
item_proxy& dropdown_append(const std::string& text, const nana::paint::image& img, const event_fn_t& handler = {}); ///< Adds an item to the dropdown menu.
item_proxy& dropdown_append(const std::string& text, const event_fn_t& handler = {}); ///< Adds an item to the dropdown menu.
item_proxy& dropdown_append(shared_command command); ///< Adds an item to the dropdown menu.

bool dropdown_enable(std::size_t index) const; ///< Gets the dropdown menu item enable state.
item_proxy& dropdown_enable(std::size_t index, bool enable_state); ///< Sets the dropdown menu item enable state.
Expand Down Expand Up @@ -166,6 +168,7 @@ namespace nana

item_proxy append(tools t, const std::string& text, const nana::paint::image& img, const event_fn_t& handler = {}); ///< Adds a tool.
item_proxy append(tools t, const std::string& text, const event_fn_t& handler = {}); ///< Adds a tool.
item_proxy append(tools t, shared_command command); ///< Adds a tool.
void append_separator(); ///< Adds a separator.

size_type count() const noexcept; ///< Returns tools and separators count.
Expand Down
Loading