Add feature for gtk122

This commit is contained in:
daleclack 2022-02-01 13:48:35 +08:00
parent 2db3b44c2b
commit 74931d4639
2 changed files with 127 additions and 18 deletions

View File

@ -1,6 +1,9 @@
#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),
@ -13,11 +16,15 @@ MyPrefs::MyPrefs()
// 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));
@ -65,6 +72,9 @@ MyPrefs::MyPrefs()
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));
@ -137,7 +147,8 @@ void MyPrefs::folders_view_changed()
else
{
// User defined folder
images_store->clear();
std::string path = row[n_columns.m_col_path];
update_images_view(path);
}
}
@ -160,23 +171,118 @@ void MyPrefs::default_folders_view()
row[n_columns.m_col_pixbuf] = image_pixbuf;
}
void MyPrefs::update_images_view() {}
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] = image_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
auto row = *(image_selection->get_selected());
if (row[n_columns.m_col_internal])
if (has_selection)
{
std::string path = row[n_columns.m_col_path];
switch (path[1])
auto row = *(image_selection->get_selected());
if (row[n_columns.m_col_internal])
{
case '1':
set_background_internal(winpe);
break;
case '2':
set_background_internal(img7);
break;
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);
}
}
}

View File

@ -27,6 +27,8 @@ protected:
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
@ -38,20 +40,21 @@ private:
Gtk::Box main_box, views_box, btnbox;
Gtk::Button btnadd, btnremove;
//Folder Open Dialog
// Folder Open Dialog
Glib::RefPtr<Gdk::Pixbuf> folder_pixbuf, image_pixbuf;
Glib::RefPtr<Gtk::FileChooserNative> dialog;
void dialog_response(int response_id);
Glib::RefPtr<Gtk::TreeSelection> folder_selection, image_selection;
//Signal Handlers
// 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();
void set_background_internal(const char * const * data);
void update_images_view(std::string &folder_path);
void set_background_internal(const char *const *data);
void set_background_file(std::string &path);
};