Add gtk157

This commit is contained in:
daleclack 2024-01-11 15:12:47 +08:00
parent bf75d5f884
commit ead36a5b9d
5 changed files with 324 additions and 0 deletions

View File

@ -0,0 +1,3 @@
{
"C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools"
}

View File

@ -0,0 +1,74 @@
set(CMAKE_CXX_STANDARD 17)
cmake_minimum_required(VERSION 3.5.0)
project(gtk157_editlist VERSION 1.0.0)
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/../../GCR_CMake/macros)
include(GlibCompileResourcesSupport)
include(CTest)
enable_testing()
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
include_directories(.)
include_directories(..)
#Find PkgConfig to use gtkmm3
find_package (PkgConfig REQUIRED)
pkg_check_modules (GTKMM4 REQUIRED gtkmm-4.0)
include_directories (${GTKMM4_INCLUDE_DIRS})
link_directories (${GTKMM4_LIBRARY_DIRS})
# Find Gettext
# find_package (Gettext REQUIRED)
# set(PO_DIR ${CMAKE_BINARY_DIR}/po/zh_CN/LC_MESSAGES)
# Source filesa
set(SOURCE_FILE src/main.cc src/MainWin.cc)
# Compile Resource
# set(RESOURCE_LIST
# STRIPBLANKS prefs_stack.ui
# STRIPBLANKS mainmenu.xml
# )
# compile_gresources(RESOURCE_FILE
# XML_OUT
# TYPE EMBED_C
# RESOURCES ${RESOURCE_LIST}
# PREFIX "/org/gtk/daleclack"
# SOURCE_DIR ${PROJECT_SOURCE_DIR}/res)
# Add a custom target to the makefile. Now make builds our resource file.
# It depends on the output RESOURCE_FILE.
# add_custom_target(resource ALL DEPENDS ${RESOURCE_FILE})
#For win32 platform,use rc resource and .ico icon
if(WIN32)
SET(CMAKE_RC_COMPILER windres)
set(app_WINRC ../icon.rc)
set_property(SOURCE ../icon.rc APPEND PROPERTY
OBJECT_DEPENDS ${PROJECT_SOURCE_DIR}/../icon.ico
)
add_executable(${PROJECT_NAME} WIN32 ${app_WINRC} ${SOURCE_FILE})
add_custom_command( TARGET ${PROJECT_NAME}
COMMAND echo * > ${CMAKE_BINARY_DIR}/.gitignore
COMMAND echo **/* > ${CMAKE_BINARY_DIR}/.hgignore)
else()
add_executable(${PROJECT_NAME} ${SOURCE_FILE})
add_custom_command( TARGET ${PROJECT_NAME}
COMMAND echo \"*\" > ${CMAKE_BINARY_DIR}/.gitignore
COMMAND echo \"**/*\" > ${CMAKE_BINARY_DIR}/.hgignore)
endif(WIN32)
#Add command to generate .gitignore and .mo files
# add_custom_command( TARGET ${PROJECT_NAME}
# COMMAND mkdir -p ${PO_DIR}
# COMMAND ${GETTEXT_MSGFMT_EXECUTABLE} ${CMAKE_SOURCE_DIR}/po/zh_CN.po -o ${PO_DIR}/${PROJECT_NAME}.mo)
SET (CMAKE_EXTRA_CXX_FLAGS ${GTKMM4_CFLAGS_OTHER})
target_link_libraries (${PROJECT_NAME} ${GTKMM4_LIBRARIES} -lpthread)

View File

@ -0,0 +1,138 @@
#include "MainWin.hh"
MainWin::MainWin()
: main_box(Gtk::Orientation::HORIZONTAL, 5),
lists_box(Gtk::Orientation::VERTICAL, 5),
drop_frame("Dropdown widget"),
list_frame("Items List"),
btn_add("Add Item"),
btn_remove("Remove Item")
{
// Initalize window
set_icon_name("org.gtk.daleclack");
set_default_size(600, 400);
set_title("Dynamic Dropdown test");
// Add overlay widget
dropdown.set_halign(Gtk::Align::CENTER);
dropdown.set_valign(Gtk::Align::CENTER);
drop_overlay.add_overlay(dropdown);
drop_frame.set_child(drop_overlay);
// Create string list
dropdown_list = Gtk::StringList::create({"Longterm", "Stable", "Develop"});
dropdown.set_model(dropdown_list);
// Create List for column View
main_list = Gio::ListStore<ModelColumns>::create();
selection = Gtk::SingleSelection::create(main_list);
main_column_view.set_model(selection);
// Add items
main_list->append(ModelColumns::create("Longterm", "5.14"));
main_list->append(ModelColumns::create("Stable", "9.1"));
main_list->append(ModelColumns::create("Develop", "10.0"));
// Add a factory to the branch column
branch_factory = Gtk::SignalListItemFactory::create();
branch_factory->signal_bind().connect(sigc::mem_fun(*this, &MainWin::bind_branch));
branch_factory->signal_setup().connect(sigc::mem_fun(*this, &MainWin::setup_branch));
branch_column = Gtk::ColumnViewColumn::create("Branch", branch_factory);
main_column_view.append_column(branch_column);
// Add a factory to the version column
version_factory = Gtk::SignalListItemFactory::create();
version_factory->signal_bind().connect(sigc::mem_fun(*this, &MainWin::bind_version));
version_factory->signal_setup().connect(sigc::mem_fun(*this, &MainWin::setup_version));
version_column = Gtk::ColumnViewColumn::create("Version", version_factory);
main_column_view.append_column(version_column);
// Link Signal for buttons
btn_add.signal_clicked().connect(sigc::mem_fun(*this, &MainWin::btnadd_clicked));
btn_remove.signal_clicked().connect(sigc::mem_fun(*this, &MainWin::btnremove_clicked));
// Add List widget
// Scrolled window for listview widget
m_sw.set_policy(Gtk::PolicyType::AUTOMATIC, Gtk::PolicyType::AUTOMATIC);
m_sw.set_expand();
m_sw.set_halign(Gtk::Align::FILL);
m_sw.set_valign(Gtk::Align::FILL);
m_sw.set_child(main_column_view);
lists_box.append(m_sw);
// Entry and buttons
btn_add.set_halign(Gtk::Align::CENTER);
btn_remove.set_halign(Gtk::Align::CENTER);
lists_box.append(item_entry);
lists_box.append(btn_add);
lists_box.append(btn_remove);
list_frame.set_child(lists_box);
// Append the frames to the main box
main_box.append(drop_frame);
main_box.append(list_frame);
set_child(main_box);
}
void MainWin::setup_branch(const Glib::RefPtr<Gtk::ListItem> &item)
{
// Set label for item
item->set_child(*Gtk::make_managed<Gtk::Entry>());
}
void MainWin::bind_branch(const Glib::RefPtr<Gtk::ListItem> &item)
{
// Get Entry
auto entry = dynamic_cast<Gtk::Entry *>(item->get_child());
if (!entry)
{
return;
}
// Bind text
auto item1 = dynamic_cast<ModelColumns *>(item.get());
Glib::Binding::bind_property(item1->property_version(), entry->property_text());
}
void MainWin::setup_version(const Glib::RefPtr<Gtk::ListItem> &item)
{
// Set label for item
item->set_child(*Gtk::make_managed<Gtk::Entry>());
}
void MainWin::bind_version(const Glib::RefPtr<Gtk::ListItem> &item)
{
// Get Entry
auto entry = dynamic_cast<Gtk::Entry *>(item->get_child());
if (!entry)
{
return;
}
// Bind text
auto item1 = dynamic_cast<ModelColumns *>(item.get());
Glib::Binding::bind_property(item1->property_version(), entry->property_text());
}
void MainWin::btnadd_clicked()
{
Glib::ustring entry_str;
// Get String from entry
entry_str = item_entry.get_text();
// Append text to the list
if (!entry_str.empty())
{
dropdown_list->append(entry_str);
}
}
void MainWin::btnremove_clicked()
{
// Get Position of item
auto pos = selection->get_selected();
// Remove item
dropdown_list->remove(pos);
}

View File

@ -0,0 +1,101 @@
#pragma once
#include "../json_nlohmann/json.hpp"
#include <vector>
#include <gtkmm.h>
using json = nlohmann::json;
typedef std::vector<std::string> str_vec;
// ModelColumns class
class ModelColumns : public Glib::Object
{
public:
// Create a new object
static Glib::RefPtr<ModelColumns> create(Glib::ustring &branch1, Glib::ustring &version1)
{
return Glib::make_refptr_for_instance<ModelColumns>(new ModelColumns(branch1, version1));
}
static Glib::RefPtr<ModelColumns> create(const char *branch1, const char *version1)
{
Glib::ustring temp_branch(branch1), temp_version(version1);
return Glib::make_refptr_for_instance<ModelColumns>(new ModelColumns(temp_branch, temp_version));
}
// Get Value of branch and version
Glib::ustring get_branch_str()
{
return branch_prep.get_value();
}
Glib::ustring get_version_str()
{
return version_prep.get_value();
}
// Get PropertyProxy
Glib::PropertyProxy<Glib::ustring> property_branch()
{
return branch_prep.get_proxy();
}
Glib::PropertyProxy<Glib::ustring> property_version()
{
return version_prep.get_proxy();
}
private:
// Use Glib::Property to bind
Glib::Property<Glib::ustring> branch_prep, version_prep;
protected:
ModelColumns(Glib::ustring branch, Glib::ustring version)
: Glib::ObjectBase(typeid(ModelColumns)),
Glib::Object(),
branch_prep(*this, "branch", branch),
version_prep(*this, "version", version)
{
}
};
// Main Window Class
class MainWin : public Gtk::ApplicationWindow
{
public:
MainWin();
private:
// Use StringList for dropdown and json file
Glib::RefPtr<Gtk::StringList> dropdown_list;
// Main list object for branchs and versions
Glib::RefPtr<Gio::ListStore<ModelColumns>> main_list;
Glib::RefPtr<Gtk::SingleSelection> selection;
// Factory to renderer branch string
Glib::RefPtr<Gtk::ColumnViewColumn> branch_column;
Glib::RefPtr<Gtk::SignalListItemFactory> branch_factory;
void setup_branch(const Glib::RefPtr<Gtk::ListItem> &item);
void bind_branch(const Glib::RefPtr<Gtk::ListItem> &item);
// Factory to renderer version string
Glib::RefPtr<Gtk::ColumnViewColumn> version_column;
Glib::RefPtr<Gtk::SignalListItemFactory> version_factory;
void setup_version(const Glib::RefPtr<Gtk::ListItem> &item);
void bind_version(const Glib::RefPtr<Gtk::ListItem> &item);
// Child widgets
Gtk::DropDown dropdown;
Gtk::Overlay drop_overlay;
Gtk::Box main_box, lists_box;
Gtk::Frame drop_frame, list_frame;
Gtk::ScrolledWindow m_sw;
Gtk::ListView main_list_view;
Gtk::ColumnView main_column_view;
Gtk::Entry item_entry;
Gtk::Button btn_add, btn_remove;
// Signal Handlers
void btnadd_clicked();
void btnremove_clicked();
};

View File

@ -0,0 +1,8 @@
#include "MainWin.hh"
int main(int argc, char **argv)
{
// Create a window and run
auto app = Gtk::Application::create("org.gtk.daleclack");
return app->make_window_and_run<MainWin>(argc, argv);
}