Add gtk150

This commit is contained in:
daleclack 2023-06-07 12:01:36 +08:00
parent 84d29d8937
commit 473ce13537
5 changed files with 154 additions and 6 deletions

View File

@ -65,7 +65,8 @@ static void btnadd_clicked(GtkWidget *widget, MainWin *win)
g_list_store_append(G_LIST_STORE(model), my_item_new(str));
}
static void btnremove_clicked(GtkWidget *widget, MainWin *win){
static void btnremove_clicked(GtkWidget *widget, MainWin *win)
{
// Get the position of selected item
GtkSingleSelection *selection = win->selection;
guint position = gtk_single_selection_get_selected(selection);
@ -75,12 +76,21 @@ static void btnremove_clicked(GtkWidget *widget, MainWin *win){
g_list_store_remove(store, position);
}
static void btnremove_all_clicked(GtkWidget *widget, MainWin *win){
static void btnremove_all_clicked(GtkWidget *widget, MainWin *win)
{
// Remove all data from store
GListModel *model = win->model;
g_list_store_remove_all(G_LIST_STORE(model));
}
static void selection_changed(GtkSelectionModel *model,
guint position,
guint n_data,
MainWin *win)
{
}
static void main_win_dispose(GObject *object)
{
// Clear List Model

View File

@ -27,6 +27,9 @@ MainWin::MainWin(){
context_menu.set_menu_model(model);
context_menu.set_parent(m_overlay);
context_menu.set_has_arrow(false);
// Add actions
add_action("back", sigc::mem_fun(*this, &MainWin::back_activated));
// Add widgets
m_overlay.set_child(m_background);
@ -39,3 +42,7 @@ void MainWin::pressed(int n_click, double x, double y){
context_menu.popup();
}
void MainWin::back_activated(){
prefs_win.present();
}

View File

@ -1,6 +1,7 @@
#pragma once
#include <gtkmm.h>
#include "MyPrefs.hh"
class MainWin : public Gtk::ApplicationWindow
{
@ -19,6 +20,10 @@ private:
// Gesture for right click
Glib::RefPtr<Gtk::GestureClick> right_click;
// Prefs Window
MyPrefs prefs_win;
// Signal Handler
void pressed(int n_click, double x, double y);
void back_activated();
};

View File

@ -1,5 +1,86 @@
#include "MyPrefs.hh"
MyPrefs::MyPrefs(){
}
MyPrefs::MyPrefs()
: main_box(Gtk::Orientation::VERTICAL, 5),
views_box(Gtk::Orientation::HORIZONTAL, 5),
buttons_box(Gtk::Orientation::HORIZONTAL, 5)
{
// Initalize the window
set_icon_name("org.gtk.daleclack");
set_default_size(800, 450);
// Create List Store for folders_view
folder_store = Gio::ListStore<MyItem>::create();
folder_select = Gtk::SingleSelection::create(folder_store);
folders_view.set_model(folder_select);
// Fill the store
folder_store->append(MyItem::create("User's home folder", Glib::get_home_dir()));
folder_store->append(MyItem::create("test2", "test3"));
// Initalize factory to renderer the object
folder_image_factory = Gtk::SignalListItemFactory::create();
folder_image_factory->signal_setup().connect(sigc::mem_fun(*this, &MyPrefs::folder_image_setup));
folder_image_factory->signal_bind().connect(sigc::mem_fun(*this, &MyPrefs::folder_image_bind));
folder_string_factory = Gtk::SignalListItemFactory::create();
folder_string_factory->signal_setup().connect(sigc::mem_fun(*this, &MyPrefs::folder_string_setup));
folder_string_factory->signal_bind().connect(sigc::mem_fun(*this, &MyPrefs::folder_string_bind));
// Create Columns and append
folder_image_column = Gtk::ColumnViewColumn::create(" ", folder_image_factory);
folder_string_column = Gtk::ColumnViewColumn::create("Names", folder_string_factory);
folders_view.append_column(folder_image_column);
folders_view.append_column(folder_string_column);
// Add timer to scan
selection_timer = Glib::signal_timeout().connect(sigc::mem_fun(*this, &MyPrefs::timeout_func), 16);
// Pack widgets
folders_view.set_expand();
images_view.set_expand();
views_box.append(folders_view);
views_box.append(images_view);
main_box.append(views_box);
main_box.append(buttons_box);
set_child(main_box);
}
void MyPrefs::folder_image_setup(const Glib::RefPtr<Gtk::ListItem> &item){
// Add image to show
item->set_child(image_folder);
}
void MyPrefs::folder_image_bind(const Glib::RefPtr<Gtk::ListItem> &item){
// Get the image widget
auto image = dynamic_cast<Gtk::Image*>(item->get_child());
// Bind the value of item
auto value = Glib::RefPtr<MyItem>(dynamic_cast<MyItem*>(item->get_item().get()));
// Set image
image->set_from_icon_name(value->icon_name);
}
void MyPrefs::folder_string_setup(const Glib::RefPtr<Gtk::ListItem> &item)
{
// Add a label to the item
item->set_child(label_folder);
}
void MyPrefs::folder_string_bind(const Glib::RefPtr<Gtk::ListItem> &item)
{
// Get the label
auto label = dynamic_cast<Gtk::Label*>(item->get_child());
// Bind the value of item
auto value = Glib::RefPtr<MyItem>(dynamic_cast<MyItem*>(item->get_item().get()));
// Set the label
label->set_label(value->file_name);
}
bool MyPrefs::timeout_func(){
// Refresh the view for folders
// Refresh the view for images
return true;
}

View File

@ -2,10 +2,55 @@
#include <gtkmm.h>
// The Gio List Store
class MyItem : public Glib::ObjectBase
{
public:
static Glib::RefPtr<MyItem> create(Glib::ustring file_name1, std::string path1){
MyItem *item = new MyItem;
item->file_name = file_name1;
item->path = path1;
item->icon_name = "folder";
return Glib::RefPtr<MyItem>(item);
}
Glib::ustring file_name;
Glib::ustring icon_name;
std::string path;
};
// These typedefs is to simplify the complex type names...
typedef Glib::RefPtr<Gtk::SignalListItemFactory> MyFactory;
typedef Glib::RefPtr<Gtk::SingleSelection> MySelection;
typedef Glib::RefPtr<Gio::ListStore<MyItem>> MyStore;
typedef Glib::RefPtr<Gtk::ColumnViewColumn> MyColumn;
class MyPrefs : public Gtk::Window
{
public:
MyPrefs();
private:
Glib::RefPtr<Gtk::Builder> stack_builder;
// Child widgets
Gtk::ColumnView folders_view, images_view;
Gtk::Box main_box, views_box, buttons_box;
// The Selection in a store
MySelection folder_select;
// The store to save the item
MyStore folder_store;
// Item Factory to renderer the objects
MyFactory folder_image_factory, folder_string_factory;
MyColumn folder_image_column, folder_string_column;
Gtk::Label label_folder;
Gtk::Image image_folder;
// Timer to scan the selection, for the absense of "changed" signal
sigc::connection selection_timer;
bool timeout_func();
// Signal Handlers
void folder_image_setup(const Glib::RefPtr<Gtk::ListItem> &item);
void folder_image_bind(const Glib::RefPtr<Gtk::ListItem> &item);
void folder_string_setup(const Glib::RefPtr<Gtk::ListItem> &item);
void folder_string_bind(const Glib::RefPtr<Gtk::ListItem> &item);
};