Update gtk149

This commit is contained in:
daleclack 2023-04-02 22:05:21 +08:00
parent 6fb706ca1c
commit d4d9e644a9
3 changed files with 93 additions and 16 deletions

View File

@ -1 +1,64 @@
#include "FileGridView.h"
#include "FileGridView.h"
static void setup_grid_item(GtkListItemFactory *factory, GtkListItem *item)
{
GtkWidget *under_box, *info_box;
GtkWidget *image;
GtkWidget *label_filename, *label_filetype, *label_filesize;
// Create box for file icon and file info
under_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5);
info_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
image = gtk_image_new();
label_filename = gtk_label_new(" ");
label_filetype = gtk_label_new(" ");
label_filesize = gtk_label_new(" ");
// Pack widgets
gtk_image_set_pixel_size(GTK_IMAGE(image), 48);
gtk_box_append(GTK_BOX(under_box), image);
gtk_box_append(GTK_BOX(info_box), label_filename);
gtk_box_append(GTK_BOX(info_box), label_filetype);
gtk_box_append(GTK_BOX(info_box), label_filesize);
gtk_box_append(GTK_BOX(under_box), info_box);
gtk_list_item_set_child(item, under_box);
}
static void bind_grid_item(GtkListItemFactory *factory, GtkListItem *item)
{
GtkWidget *under_box, *info_box;
GtkWidget *image;
GtkWidget *label_filename, *label_filetype, *label_filesize;
// Get Widgets
under_box = gtk_list_item_get_child(item);
image = gtk_widget_get_first_child(under_box);
info_box = gtk_widget_get_next_sibling(image);
label_filename = gtk_widget_get_first_child(info_box);
label_filetype = gtk_widget_get_next_sibling(label_filename);
label_filesize = gtk_widget_get_next_sibling(label_filetype);
// Get GFileInfo
GFileInfo *info = G_FILE_INFO(gtk_list_item_get_item(item));
// The icon for file or folders
gtk_image_set_from_gicon(GTK_IMAGE(image), g_file_info_get_icon(info));
// File name, content type and size
gtk_label_set_label(GTK_LABEL(label_filename), g_file_info_get_display_name(info));
gtk_label_set_label(GTK_LABEL(label_filetype), g_file_info_get_content_type(info));
gtk_label_set_label(GTK_LABEL(label_filesize), g_format_size(g_file_info_get_size(info)));
}
GtkWidget *create_grid_view(GListModel *model)
{
// Create factory
GtkListItemFactory *factory = gtk_signal_list_item_factory_new();
g_signal_connect(factory, "setup", G_CALLBACK(setup_grid_item), NULL);
g_signal_connect(factory, "bind", G_CALLBACK(bind_grid_item), NULL);
// Create view
GtkSingleSelection *selection = gtk_single_selection_new(model);
GtkWidget *grid_view = gtk_grid_view_new(GTK_SELECTION_MODEL(selection), factory);
return grid_view;
}

View File

@ -2,3 +2,4 @@
#include <gtk/gtk.h>
GtkWidget *create_grid_view(GListModel *model);

View File

@ -6,20 +6,17 @@ struct _FileWindow
{
GtkApplicationWindow parent_instance;
GListModel *model;
GtkWidget *column_view;
GtkWidget *column_view, *grid_view;
GtkWidget *main_box;
GtkWidget *scrolled_window;
GtkWidget *scrolled_window_column, *scrolled_window_grid;
GtkWidget *stack;
};
G_DEFINE_TYPE(FileWindow, file_window, GTK_TYPE_APPLICATION_WINDOW)
static void file_window_dispose(GObject *object)
{
// Clear List Model
// g_list_store_remove_all(G_LIST_STORE(file_window(object)->model));
G_OBJECT_CLASS(file_window_parent_class)->dispose(object);
}
@ -38,17 +35,33 @@ static void file_window_init(FileWindow *self)
// Create column view widget
self->column_view = create_column_view(self->model);
// Create widgets for layout
self->scrolled_window = gtk_scrolled_window_new();
self->main_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
// Create grid view widget
self->grid_view = create_grid_view(self->model);
// Add widgets to the layout
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(self->scrolled_window),
// Create widgets for layout
self->scrolled_window_column = gtk_scrolled_window_new();
self->scrolled_window_grid = gtk_scrolled_window_new();
self->main_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
self->stack = gtk_stack_new();
// Add scrolled window for columns view
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(self->scrolled_window_column),
GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
gtk_widget_set_hexpand(self->scrolled_window, TRUE);
gtk_widget_set_vexpand(self->scrolled_window, TRUE);
gtk_scrolled_window_set_child(GTK_SCROLLED_WINDOW(self->scrolled_window), self->column_view);
gtk_box_append(GTK_BOX(self->main_box), self->scrolled_window);
gtk_widget_set_hexpand(self->scrolled_window_column, TRUE);
gtk_widget_set_vexpand(self->scrolled_window_column, TRUE);
gtk_scrolled_window_set_child(GTK_SCROLLED_WINDOW(self->scrolled_window_column), self->column_view);
gtk_stack_add_named(GTK_STACK(self->stack), self->scrolled_window_column, "Column View");
// Add scrolled window for grid view
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(self->scrolled_window_grid),
GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
gtk_widget_set_hexpand(self->scrolled_window_grid, TRUE);
gtk_widget_set_vexpand(self->scrolled_window_grid, TRUE);
gtk_scrolled_window_set_child(GTK_SCROLLED_WINDOW(self->scrolled_window_grid), self->grid_view);
gtk_stack_add_named(GTK_STACK(self->stack), self->scrolled_window_grid, "Grid View");
gtk_box_append(GTK_BOX(self->main_box), self->stack);
gtk_stack_set_visible_child(GTK_STACK(self->stack), self->scrolled_window_grid);
gtk_window_set_child(GTK_WINDOW(self), self->main_box);
}