Add gtk123

This commit is contained in:
daleclack 2022-02-01 17:14:51 +08:00
parent f374edeb29
commit 9875c98a22
8 changed files with 524 additions and 0 deletions

View File

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

View File

@ -0,0 +1,62 @@
set(CMAKE_CXX_STANDARD 17)
cmake_minimum_required(VERSION 3.0.0)
project(gtk123 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_package (PkgConfig REQUIRED)
pkg_check_modules (GTKMM3 REQUIRED gtkmm-3.0)
include_directories (${GTKMM3_INCLUDE_DIRS})
link_directories (${GTKMM3_LIBRARY_DIRS})
#Compile Resource
set(RESOURCE_LIST
icons/16x16/actions/list-add.svg
icons/16x16/actions/list-remove.svg
folder.svg
folder-images.svg
image_file.svg)
compile_gresources(RESOURCE_FILE
XML_OUT
TYPE EMBED_C
RESOURCES ${RESOURCE_LIST}
PREFIX "/org/gtk/daleclack"
SOURCE_DIR ${PROJECT_SOURCE_DIR}/../default_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 x86_64-w64-mingw32-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} src/main.cc src/MyWin.cc src/MyPrefs.cc ${RESOURCE_FILE})
else()
add_executable(${PROJECT_NAME} src/main.cc src/MyWin.cc src/MyPrefs.cc ${RESOURCE_FILE})
endif(WIN32)
#Add command to generate .gitignore
add_custom_command(TARGET ${PROJECT_NAME}
COMMAND echo \"*\" > ${CMAKE_BINARY_DIR}/.gitignore
COMMAND echo \"**/*\" > ${CMAKE_BINARY_DIR}/.hgignore)
SET (CMAKE_EXTRA_CXX_FLAGS ${GTKMM3_CFLAGS_OTHER})
target_link_libraries (${PROJECT_NAME} ${GTKMM3_LIBRARIES} -lpthread)

View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.38.2 -->
<interface>
<requires lib="gtk+" version="3.24"/>
<object class="GtkStack">
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<placeholder/>
</child>
</object>
</interface>

View File

@ -0,0 +1,315 @@
#include "MyPrefs.hh"
#include "winpe.xpm"
#include "../Gtk4/img7.xpm"
#include "image_types.hh"
#include <iostream>
#include <string>
MyPrefs::MyPrefs()
: main_box(Gtk::ORIENTATION_VERTICAL, 10),
views_box(Gtk::ORIENTATION_HORIZONTAL, 5),
btnbox(Gtk::ORIENTATION_HORIZONTAL, 5)
{
// Initalize Window
set_title("Window Preferences");
set_default_size(800, 450);
// Initalize Stores
folders_store = Gtk::ListStore::create(n_columns);
folders_store->set_default_sort_func(sigc::mem_fun(*this, &MyPrefs::sort_func));
folders_store->set_sort_column(-1, Gtk::SORT_ASCENDING);
folders_view.set_model(folders_store);
folder_selection = folders_view.get_selection();
folder_selection->signal_changed().connect(sigc::mem_fun(*this, &MyPrefs::folders_view_changed));
images_store = Gtk::ListStore::create(n_columns);
images_store->set_default_sort_func(sigc::mem_fun(*this, &MyPrefs::sort_func));
images_store->set_sort_column(-1, Gtk::SORT_ASCENDING);
images_view.set_model(images_store);
image_selection = images_view.get_selection();
image_selection->signal_changed().connect(sigc::mem_fun(*this, &MyPrefs::images_view_changed));
// Load Pixbufs
auto pixbuf = Gdk::Pixbuf::create_from_resource("/org/gtk/daleclack/folder.svg");
folder_pixbuf = pixbuf->scale_simple(24, 24, Gdk::INTERP_BILINEAR);
pixbuf.reset();
pixbuf = Gdk::Pixbuf::create_from_resource("/org/gtk/daleclack/folder-images.svg");
image_pixbuf = pixbuf->scale_simple(24, 24, Gdk::INTERP_BILINEAR);
pixbuf.reset();
pixbuf = Gdk::Pixbuf::create_from_resource("/org/gtk/daleclack/image_file.svg");
imagefile_pixbuf = pixbuf->scale_simple(24, 24, Gdk::INTERP_BILINEAR);
pixbuf.reset();
// Add Default Value for folders view
auto row = *(folders_store->append());
row[n_columns.m_col_path] = "";
row[n_columns.m_col_name] = "Default Backgrounds";
row[n_columns.m_col_pixbuf] = folder_pixbuf;
row[n_columns.m_col_internal] = true;
row = *(folders_store->append());
row[n_columns.m_col_path] = Glib::get_home_dir();
row[n_columns.m_col_name] = "User's Home";
row[n_columns.m_col_pixbuf] = folder_pixbuf;
row[n_columns.m_col_internal] = false;
row = *(folders_store->append());
row[n_columns.m_col_path] = Glib::get_user_special_dir(Glib::USER_DIRECTORY_PICTURES);
row[n_columns.m_col_name] = "User's Pictures Directory";
row[n_columns.m_col_pixbuf] = image_pixbuf;
row[n_columns.m_col_internal] = false;
// Append Column for the folders view
folders_view.append_column(" ", n_columns.m_col_pixbuf);
folders_view.append_column("Name", n_columns.m_col_name);
// Default Value for imags view
default_folders_view();
images_view.append_column(" ", n_columns.m_col_pixbuf);
images_view.append_column("Images", n_columns.m_col_name);
// Add Views
main_box.pack_start(views_box);
sw_folders.add(folders_view);
views_box.pack_start(sw_folders);
sw_images.add(images_view);
views_box.pack_start(sw_images);
// Allow Selection
has_selection = true;
// Add Control Buttons
btnadd.set_image_from_icon_name("list-add");
btnadd.signal_clicked().connect(sigc::mem_fun(*this, &MyPrefs::btnadd_clicked));
btnremove.set_image_from_icon_name("list-remove");
btnremove.signal_clicked().connect(sigc::mem_fun(*this, &MyPrefs::btnremove_clicked));
btnbox.pack_start(btnadd, Gtk::PACK_SHRINK);
btnbox.pack_start(btnremove, Gtk::PACK_SHRINK);
main_box.pack_start(btnbox, Gtk::PACK_SHRINK);
// Add Main Box to window
const int margin_value = 15;
main_box.set_margin_top(margin_value);
main_box.set_margin_bottom(margin_value);
main_box.set_margin_start(margin_value);
main_box.set_margin_end(margin_value);
add(main_box);
show_all_children();
}
void MyPrefs::btnadd_clicked()
{
// Create a dialog
dialog = Gtk::FileChooserNative::create("Add a folder", *this,
Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER, "OK", "Cancel");
dialog->signal_response().connect(sigc::mem_fun(*this, &MyPrefs::dialog_response));
dialog->show();
}
void MyPrefs::dialog_response(int response_id)
{
if (response_id == Gtk::RESPONSE_ACCEPT)
{
// Get File Basename and Path
auto file = dialog->get_file();
auto basename = file->get_basename();
auto filename = file->get_path();
// Add to list
auto row = *(folders_store->append());
row[n_columns.m_col_path] = filename;
row[n_columns.m_col_name] = basename;
row[n_columns.m_col_pixbuf] = folder_pixbuf;
file.reset();
}
dialog.reset();
}
void MyPrefs::btnremove_clicked()
{
// Get the selection and remove the selected item
auto iter = folder_selection->get_selected();
if (iter)
{
folders_store->erase(iter);
}
}
void MyPrefs::folders_view_changed()
{
// Get Selected Folder
auto row = *(folder_selection->get_selected());
if (row[n_columns.m_col_internal])
{
// The default folder
default_folders_view();
}
else
{
// User defined folder
std::string path = row[n_columns.m_col_path];
update_images_view(path);
}
}
void MyPrefs::default_folders_view()
{
// Clear the store
images_store->clear();
// Add Default values
auto row = *(images_store->append());
row[n_columns.m_col_path] = ":1";
row[n_columns.m_col_name] = "winpe.xpm";
row[n_columns.m_col_internal] = true;
row[n_columns.m_col_pixbuf] = imagefile_pixbuf;
row = *(images_store->append());
row[n_columns.m_col_path] = ":2";
row[n_columns.m_col_name] = "img7.xpm";
row[n_columns.m_col_internal] = true;
row[n_columns.m_col_pixbuf] = imagefile_pixbuf;
}
int MyPrefs::sort_func(const Gtk::TreeModel::iterator &a, const Gtk::TreeModel::iterator &b)
{
Glib::ustring name_a, name_b;
// Get file names
auto row_a = *(a);
auto row_b = *(b);
name_a = row_a[n_columns.m_col_name];
name_b = row_b[n_columns.m_col_name];
// Proform sort process
if (name_a[0] != '.' && name_b[0] == '.')
{
return 1;
}
if (name_a[0] == '.' && name_b[0] != '.')
{
return -1;
}
else
{
return g_utf8_collate(name_a.c_str(), name_b.c_str());
}
}
void MyPrefs::update_images_view(std::string &folder_path)
{
// Unselect everything
has_selection = false;
// Clear the store
images_store->clear();
// Add Files into store
try
{
Glib::Dir dir1(folder_path);
bool is_dir, file_valid;
Glib::ustring display_name;
std::string filename, pathname;
do
{
// Get File Name
filename = dir1.read_name();
// Get Path for a file
pathname = Glib::build_filename(folder_path, filename);
is_dir = Glib::file_test(pathname, Glib::FILE_TEST_IS_DIR);
display_name = Glib::filename_to_utf8(filename);
// Filter the file
// Get Pattern of the file
file_valid = false;
size_t pos = filename.find_last_of('.');
if (pos != std::string::npos)
{
std::string pattern = "*" + filename.substr(pos);
for (int i = 0; supported_globs[i] != NULL; i++)
{
std::string glob = std::string(supported_globs[i]);
if (glob == pattern)
{
file_valid = true;
break;
}
}
}
if (!is_dir && file_valid)
{
// Add item to store
auto row = *(images_store->append());
row[n_columns.m_col_path] = pathname;
row[n_columns.m_col_name] = display_name;
row[n_columns.m_col_internal] = false;
row[n_columns.m_col_pixbuf] = imagefile_pixbuf;
}
} while (filename != "");
has_selection = true;
}
catch (Glib::Error &ex)
{
std::cout << ex.what() << std::endl;
}
}
void MyPrefs::images_view_changed()
{
// Set the background as selected
if (has_selection)
{
auto row = *(image_selection->get_selected());
if (row[n_columns.m_col_internal])
{
std::string path = row[n_columns.m_col_path];
switch (path[1])
{
case '1':
set_background_internal(winpe);
break;
case '2':
set_background_internal(img7);
break;
}
}
else
{
std::string path = row[n_columns.m_col_path];
set_background_file(path);
}
}
}
void MyPrefs::set_background_internal(const char *const *data)
{
// Set a internal background
auto pixbuf = Gdk::Pixbuf::create_from_xpm_data(data);
auto sized = pixbuf->scale_simple(1024, 576, Gdk::INTERP_BILINEAR);
gtk_image_set_from_pixbuf(background1->gobj(), sized->gobj());
pixbuf.reset();
sized.reset();
}
void MyPrefs::set_background_file(std::string &path)
{
// Set Background from a file
auto pixbuf = Gdk::Pixbuf::create_from_file(path);
auto sized = pixbuf->scale_simple(1024, 576, Gdk::INTERP_BILINEAR);
gtk_image_set_from_pixbuf(background1->gobj(), sized->gobj());
pixbuf.reset();
sized.reset();
}
void MyPrefs::set_background(Gtk::Image *back)
{
background1 = back;
}

View File

@ -0,0 +1,63 @@
#pragma once
#include <gtkmm.h>
class MyPrefs : public Gtk::Window
{
public:
MyPrefs();
void set_background(Gtk::Image *back);
protected:
class ModelColumns : public Gtk::TreeModelColumnRecord
{
public:
ModelColumns()
{
add(m_col_pixbuf);
add(m_col_path);
add(m_col_name);
add(m_col_internal);
}
Gtk::TreeModelColumn<Glib::RefPtr<Gdk::Pixbuf>> m_col_pixbuf;
Gtk::TreeModelColumn<std::string> m_col_path;
Gtk::TreeModelColumn<Glib::ustring> m_col_name;
Gtk::TreeModelColumn<bool> m_col_internal;
};
ModelColumns n_columns;
Glib::RefPtr<Gtk::ListStore> folders_store, images_store;
Glib::RefPtr<Gtk::TreeSelection> folder_selection, image_selection;
bool has_selection;
private:
// Widget for parent window
Gtk::Image *background1;
Glib::RefPtr<Gtk::Builder> stackbuilder;
// Child Widgets
Gtk::Stack *stack;
Gtk::TreeView folders_view, images_view;
Gtk::ScrolledWindow sw_folders, sw_images;
Gtk::Box main_box, views_box, btnbox;
Gtk::Button btnadd, btnremove;
// Folder Open Dialog
Glib::RefPtr<Gdk::Pixbuf> folder_pixbuf, image_pixbuf, imagefile_pixbuf;
Glib::RefPtr<Gtk::FileChooserNative> dialog;
void dialog_response(int response_id);
// Sort for ListStore
int sort_func(const Gtk::TreeModel::iterator &a, const Gtk::TreeModel::iterator &b);
// Signal Handlers
void btnadd_clicked();
void btnremove_clicked();
void folders_view_changed();
void images_view_changed();
void default_folders_view();
void update_images_view(std::string &folder_path);
void set_background_internal(const char *const *data);
void set_background_file(std::string &path);
};

View File

@ -0,0 +1,39 @@
#include "MyWin.hh"
#include "winpe.xpm"
MyWin::MyWin()
{
// Initalize Window
set_icon_name("org.gtk.daleclack");
set_default_size(1024, 576);
set_title("Background Preferences Test");
// Add Background
auto pixbuf = Gdk::Pixbuf::create_from_xpm_data(winpe);
auto sized = pixbuf->scale_simple(1024, 576, Gdk::INTERP_BILINEAR);
gtk_image_set_from_pixbuf(m_back.gobj(), sized->gobj());
pixbuf.reset();
sized.reset();
// Button for background
btnback.set_label("Window Preferences");
btnback.set_halign(Gtk::ALIGN_CENTER);
btnback.set_valign(Gtk::ALIGN_CENTER);
btnback.set_relief(Gtk::RELIEF_NONE);
m_overlay.add_overlay(btnback);
btnback.signal_clicked().connect(sigc::mem_fun(*this, &MyWin::btnback_clicked));
// Add widgets
m_overlay.add(m_back);
add(m_overlay);
// Initalize MyPrefs
prefs_win.set_background(&m_back);
show_all_children();
}
void MyWin::btnback_clicked()
{
prefs_win.show_all();
}

View File

@ -0,0 +1,22 @@
#pragma once
#include <gtkmm.h>
#include "MyPrefs.hh"
class MyWin : public Gtk::Window
{
public:
MyWin();
private:
//Child Widgets
Gtk::Overlay m_overlay;
Gtk::Image m_back;
Gtk::Button btnback;
//Background Preferences
MyPrefs prefs_win;
//Signal Handlers
void btnback_clicked();
};

View File

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