Update gtk149

This commit is contained in:
daleclack 2023-04-02 21:09:20 +08:00
parent 07dc0e8b81
commit 6fb706ca1c
11 changed files with 230 additions and 158 deletions

View File

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

View File

@ -26,7 +26,7 @@ link_directories (${GTK4_LIBRARY_DIRS})
# set(PO_DIR ${CMAKE_BINARY_DIR}/po/zh_CN/LC_MESSAGES)
#Source files
set(SOURCE_FILE src/main.cpp src/MainWin.cpp)
set(SOURCE_FILE src/main.cpp src/FileWindow.cpp src/FileColumnView.cpp src/FileGridView.cpp)
#Compile Resource

View File

@ -0,0 +1,143 @@
#include "FileColumnView.h"
static void setup_fileicon_item(GtkListItemFactory *factory, GtkListItem *item)
{
// Create Image for icon
GtkWidget *image;
image = gtk_image_new();
gtk_image_set_pixel_size(GTK_IMAGE(image), 24);
gtk_list_item_set_child(item, image);
}
static void bind_fileicon_item(GtkListItemFactory *factory, GtkListItem *item)
{
// Get the image widget
GtkWidget *image;
image = gtk_list_item_get_child(item);
// Set image for column
GFileInfo *info = G_FILE_INFO(gtk_list_item_get_item(item));
gtk_image_set_from_gicon(GTK_IMAGE(image), g_file_info_get_icon(info));
}
static void setup_filename_item(GtkListItemFactory *factory, GtkListItem *item)
{
// Create label to show list item
GtkWidget *label;
label = gtk_label_new(" ");
gtk_widget_set_halign(label, GTK_ALIGN_START);
gtk_list_item_set_child(item, label);
}
static void bind_filename_item(GtkListItemFactory *factory, GtkListItem *item)
{
// Get child for list item
GtkWidget *label;
label = gtk_list_item_get_child(item);
// Get string and add the string to the label
GFileInfo *info = G_FILE_INFO(gtk_list_item_get_item(item));
const char *display_name = g_file_info_get_display_name(info);
gtk_label_set_label(GTK_LABEL(label), display_name);
}
static void setup_filetype_item(GtkListItemFactory *factory, GtkListItem *item)
{
// Create label to show list item
GtkWidget *label;
label = gtk_label_new(" ");
gtk_widget_set_halign(label, GTK_ALIGN_START);
gtk_list_item_set_child(item, label);
}
static void bind_filetype_item(GtkListItemFactory *factory, GtkListItem *item)
{
// Get child for list item
GtkWidget *label;
label = gtk_list_item_get_child(item);
// Get string and set it to the label
GFileInfo *info = G_FILE_INFO(gtk_list_item_get_item(item));
const char *type_str = g_file_info_get_content_type(info);
gtk_label_set_label(GTK_LABEL(label), type_str);
}
static void setup_filesize_item(GtkListItemFactory *factory, GtkListItem *item)
{
// Create label to show list item
GtkWidget *label;
label = gtk_label_new(" ");
gtk_widget_set_halign(label, GTK_ALIGN_START);
gtk_list_item_set_child(item, label);
}
static void bind_filesize_item(GtkListItemFactory *factory, GtkListItem *item)
{
// Get child for list item
GtkWidget *label;
label = gtk_list_item_get_child(item);
// Get string and set it to the label
GFileInfo *info = G_FILE_INFO(gtk_list_item_get_item(item));
const char *size_str = g_format_size(g_file_info_get_size(info));
gtk_label_set_label(GTK_LABEL(label), size_str);
}
static void listview_activated(GtkColumnView *view, guint position, GtkDirectoryList *list)
{
// Get the model
GtkSelectionModel *model = gtk_column_view_get_model(view);
GFileInfo *info = G_FILE_INFO(g_list_model_get_item(G_LIST_MODEL(model), position));
// if the file type is directory, open the directory
if (g_file_info_get_file_type(info) == G_FILE_TYPE_DIRECTORY)
{
gtk_directory_list_set_file(list,
G_FILE(g_file_info_get_attribute_object(info, "standard::file")));
}
g_object_unref(info);
}
GtkWidget *create_column_view(GListModel *model)
{
// Create view
GtkSingleSelection *selection = gtk_single_selection_new(model);
GtkWidget *view = gtk_column_view_new(GTK_SELECTION_MODEL(selection));
// Create Factory for file icon
GtkListItemFactory *factory = gtk_signal_list_item_factory_new();
g_signal_connect(factory, "setup", G_CALLBACK(setup_fileicon_item), NULL);
g_signal_connect(factory, "bind", G_CALLBACK(bind_fileicon_item), NULL);
GtkColumnViewColumn *column = gtk_column_view_column_new("Name", factory);
gtk_column_view_append_column(GTK_COLUMN_VIEW(view), column);
g_object_unref(column);
// Create Factory for file name
factory = gtk_signal_list_item_factory_new();
g_signal_connect(factory, "setup", G_CALLBACK(setup_filename_item), NULL);
g_signal_connect(factory, "bind", G_CALLBACK(bind_filename_item), NULL);
column = gtk_column_view_column_new("Name", factory);
gtk_column_view_append_column(GTK_COLUMN_VIEW(view), column);
g_object_unref(column);
// Create Factory for file type
factory = gtk_signal_list_item_factory_new();
g_signal_connect(factory, "setup", G_CALLBACK(setup_filetype_item), NULL);
g_signal_connect(factory, "bind", G_CALLBACK(bind_filetype_item), NULL);
column = gtk_column_view_column_new("Type", factory);
gtk_column_view_append_column(GTK_COLUMN_VIEW(view), column);
g_object_unref(column);
// Create Factory for file size
factory = gtk_signal_list_item_factory_new();
g_signal_connect(factory, "setup", G_CALLBACK(setup_filesize_item), NULL);
g_signal_connect(factory, "bind", G_CALLBACK(bind_filesize_item), NULL);
column = gtk_column_view_column_new("Size", factory);
gtk_column_view_append_column(GTK_COLUMN_VIEW(view), column);
g_object_unref(column);
// Link signals
g_signal_connect(view, "activate", G_CALLBACK(listview_activated), model);
return view;
}

View File

@ -0,0 +1,6 @@
#pragma once
#include <gtk/gtk.h>
// Create column view widget
GtkWidget *create_column_view(GListModel *model);

View File

@ -0,0 +1 @@
#include "FileGridView.h"

View File

@ -0,0 +1,4 @@
#pragma once
#include <gtk/gtk.h>

View File

@ -0,0 +1,63 @@
#include "FileWindow.h"
#include "FileColumnView.h"
#include "FileGridView.h"
struct _FileWindow
{
GtkApplicationWindow parent_instance;
GListModel *model;
GtkWidget *column_view;
GtkWidget *main_box;
GtkWidget *scrolled_window;
};
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);
}
static void file_window_init(FileWindow *self)
{
GtkColumnViewColumn *column;
// Initalize window
gtk_window_set_icon_name(GTK_WINDOW(self), "org.gtk.daleclack");
gtk_window_set_default_size(GTK_WINDOW(self), 640, 400);
// Create list model
GFile *file = g_file_new_for_path(g_get_home_dir());
self->model = G_LIST_MODEL(gtk_directory_list_new(
"standard::name,standard::display-name,standard::icon,standard::size,standard::content-type", file));
// 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);
// Add widgets to the layout
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(self->scrolled_window),
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_window_set_child(GTK_WINDOW(self), self->main_box);
}
static void file_window_class_init(FileWindowClass *klass)
{
G_OBJECT_CLASS(klass)->dispose = file_window_dispose;
}
FileWindow *file_window_new(GtkApplication *app)
{
return FILE_WINDOW(g_object_new(file_window_get_type(), "application", app, NULL));
}

View File

@ -0,0 +1,7 @@
#pragma once
#include <gtk/gtk.h>
G_DECLARE_FINAL_TYPE(FileWindow, file_window, FILE, WINDOW, GtkApplicationWindow)
FileWindow *file_window_new(GtkApplication *app);

View File

@ -1,148 +0,0 @@
#include "MainWin.h"
struct _MainWin
{
GtkApplicationWindow parent_instance;
GListModel *model;
GtkWidget *view;
GtkListItemFactory *factory;
GtkSingleSelection *selection;
GtkWidget *main_box;
GtkWidget *scrolled_window;
GtkWidget *btn_add, *btn_remove, *btn_remove_all;
GtkWidget *entry_add;
};
G_DEFINE_TYPE(MainWin, main_win, GTK_TYPE_APPLICATION_WINDOW)
static void setup_filename_item(GtkListItemFactory *factory, GtkListItem *item)
{
// Create label to show list item
GtkWidget *label;
label = gtk_label_new(" ");
gtk_widget_set_halign(label, GTK_ALIGN_START);
gtk_list_item_set_child(item, label);
}
static void bind_filename_item(GtkListItemFactory *factory, GtkListItem *item)
{
// Get child for list item
GtkWidget *label;
label = gtk_list_item_get_child(item);
// Get string and add the string to the label
GFileInfo *info = G_FILE_INFO(gtk_list_item_get_item(item));
const char *display_name = g_file_info_get_display_name(info);
gtk_label_set_label(GTK_LABEL(label), display_name);
}
static void setup_filetype_item(GtkListItemFactory *factory, GtkListItem *item){
// Create label to show list item
GtkWidget *label;
label = gtk_label_new(" ");
gtk_widget_set_halign(label, GTK_ALIGN_START);
gtk_list_item_set_child(item, label);
}
static void bind_filetype_item(GtkListItemFactory *factory, GtkListItem *item){
// Get child for list item
GtkWidget *label;
label = gtk_list_item_get_child(item);
// Get string and set it to the label
GFileInfo *info = G_FILE_INFO(gtk_list_item_get_item(item));
const char *type_str = g_file_info_get_content_type(info);
gtk_label_set_label(GTK_LABEL(label), type_str);
}
static void setup_filesize_item(GtkListItemFactory *factory, GtkListItem *item){
// Create label to show list item
GtkWidget *label;
label = gtk_label_new(" ");
gtk_widget_set_halign(label, GTK_ALIGN_START);
gtk_list_item_set_child(item, label);
}
static void bind_filesize_item(GtkListItemFactory *factory, GtkListItem *item){
// Get child for list item
GtkWidget *label;
label = gtk_list_item_get_child(item);
// Get string and set it to the label
GFileInfo *info = G_FILE_INFO(gtk_list_item_get_item(item));
const char *size_str = g_format_size(g_file_info_get_size(info));
gtk_label_set_label(GTK_LABEL(label), size_str);
}
static void main_win_dispose(GObject *object)
{
// Clear List Model
// g_list_store_remove_all(G_LIST_STORE(MAIN_WIN(object)->model));
G_OBJECT_CLASS(main_win_parent_class)->dispose(object);
}
static void main_win_init(MainWin *self)
{
GtkColumnViewColumn *column;
// Initalize window
gtk_window_set_icon_name(GTK_WINDOW(self), "org.gtk.daleclack");
gtk_window_set_default_size(GTK_WINDOW(self), 640, 360);
// Create list model
GFile *file = g_file_new_for_path(g_get_home_dir());
self->model = G_LIST_MODEL(gtk_directory_list_new(
"standard::name,standard::display-name,standard::icon,standard::size,standard::content-type", file));
// Create view
self->selection = gtk_single_selection_new(self->model);
self->view = gtk_column_view_new(GTK_SELECTION_MODEL(self->selection));
// Create Factory for file name
self->factory = gtk_signal_list_item_factory_new();
g_signal_connect(self->factory, "setup", G_CALLBACK(setup_filename_item), NULL);
g_signal_connect(self->factory, "bind", G_CALLBACK(bind_filename_item), NULL);
column = gtk_column_view_column_new("Name", self->factory);
gtk_column_view_append_column(GTK_COLUMN_VIEW(self->view), column);
g_object_unref(column);
// Create Factory for file type
self->factory = gtk_signal_list_item_factory_new();
g_signal_connect(self->factory, "setup", G_CALLBACK(setup_filetype_item), NULL);
g_signal_connect(self->factory, "bind", G_CALLBACK(bind_filetype_item), NULL);
column = gtk_column_view_column_new("Type", self->factory);
gtk_column_view_append_column(GTK_COLUMN_VIEW(self->view), column);
g_object_unref(column);
// Create Factory for file size
self->factory = gtk_signal_list_item_factory_new();
g_signal_connect(self->factory, "setup", G_CALLBACK(setup_filesize_item), NULL);
g_signal_connect(self->factory, "bind", G_CALLBACK(bind_filesize_item), NULL);
column = gtk_column_view_column_new("Size", self->factory);
gtk_column_view_append_column(GTK_COLUMN_VIEW(self->view), column);
g_object_unref(column);
// Create widgets for layout
self->scrolled_window = gtk_scrolled_window_new();
self->main_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
// Link signals
// Add widgets to the layout
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(self->scrolled_window),
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->view);
gtk_box_append(GTK_BOX(self->main_box), self->scrolled_window);
gtk_window_set_child(GTK_WINDOW(self), self->main_box);
}
static void main_win_class_init(MainWinClass *klass)
{
G_OBJECT_CLASS(klass)->dispose = main_win_dispose;
}
MainWin *main_win_new(GtkApplication *app)
{
return MAIN_WIN(g_object_new(main_win_get_type(), "application", app, NULL));
}

View File

@ -1,7 +0,0 @@
#pragma once
#include <gtk/gtk.h>
G_DECLARE_FINAL_TYPE(MainWin, main_win, MAIN, WIN, GtkApplicationWindow)
MainWin *main_win_new(GtkApplication *app);

View File

@ -1,7 +1,7 @@
#include "MainWin.h"
#include "FileWindow.h"
static void gtkmain(GtkApplication *app, gpointer data){
MainWin *window = main_win_new(app);
FileWindow *window = file_window_new(app);
gtk_window_present(GTK_WINDOW(window));
}