Files
filemanager/main.c

134 lines
3.9 KiB
C

#include <gtk/gtk.h>
#include <stdio.h>
#include <dirent.h>
typedef struct{
GtkApplication *app;
GtkWidget *window;
char dir[256];
} Appdata;
static void buttons (GtkApplication *app, Appdata *data, char *directory);
// action on clicked button
static void on_button_clicked(GtkButton *btn, gpointer user_data){
Appdata *data = (Appdata *)user_data;
const char *label = gtk_button_get_label(btn);
g_print("Button %s clicked\n",label);
// remove child(grid buttons)
GtkWidget *child = gtk_widget_get_first_child(GTK_WIDGET(data->window));
while (child != NULL) {
GtkWidget *next_child = gtk_widget_get_next_sibling(child);
gtk_window_set_child(GTK_WINDOW(data->window), NULL);
child = next_child;
}
// get into char directory label of button with previos dir name
char directory[256];
snprintf(directory, sizeof(directory), "%s/%s", data->dir,label);
// save full path
snprintf(data->dir, sizeof(directory), "%s", directory);
buttons(data->app,data,directory);
}
// gets number of items and return value in arrays
int *check_dir(int *arr, DIR *d, char *directory){
int i = 0;
int j = 0;
struct dirent *dir;
d = opendir(directory);
if (d) {
while ((dir = readdir(d)) != NULL){
char *name = dir->d_name;
// temporary make them ' ' for get into previos dir
if (*name == ' '){
printf("directory starts with . : %s\n", name);
}
else{
i += 1;
if(i - 10 == 0){
i = 0;
j += 1;
}
arr[0] = i;
arr[1] = j;
}
}
}
return 0;
}
// present buttons
static void buttons (GtkApplication *app, Appdata *data,char *directory){
int p[2];
int g;
int i = 0;
int j = 0;
DIR *d;
struct dirent *dir;
// char directory[100];
d = opendir(directory);
GtkWidget* grid = gtk_grid_new();
check_dir(p, d, directory);
printf("directory: %s\n",directory);
printf("%i %i\n",p[0],p[1]);
g = 10;
while ((dir = readdir(d)) != NULL && i <=p[1] && j < g){
if (d) {
char *name = dir->d_name;
// temporary make them ' ' for get into previos dir
if (*name == ' '){
printf("directory starts with . : %s\n", name);
}
else{
if (i == p[1]){
printf("i=10 g=p[0]\n");
g = p[0];
}
printf("set directory label: %s\n", name);
GtkWidget* btn = gtk_button_new();
gtk_grid_set_row_spacing(GTK_GRID (grid), 10);
gtk_grid_set_column_spacing(GTK_GRID (grid), 10);
gtk_grid_set_column_homogeneous(GTK_GRID (grid), TRUE);
gtk_grid_set_row_homogeneous(GTK_GRID (grid), TRUE);
gtk_button_set_label(GTK_BUTTON(btn),name);
gtk_grid_attach (GTK_GRID(grid), btn, j, i, 1,1);
gtk_widget_set_halign(btn, GTK_ALIGN_CENTER);
gtk_widget_set_valign(btn, GTK_ALIGN_CENTER);
g_signal_connect(btn, "clicked",G_CALLBACK(on_button_clicked),data);
j++;
if (j >= 10){
j = 0;
i++;
}
}
}
}
closedir(d);
gtk_window_set_child(GTK_WINDOW(data->window),grid);
gtk_window_present(GTK_WINDOW(data->window));
printf("set child and present window %d\n", data->window);
}
static void app_activate (GtkApplication *app, gpointer *user_data){
Appdata *data = (Appdata *)user_data;
scanf("%256[^\n]", &data->dir);
// create window if not exist
if (!data->window){
data->window = gtk_application_window_new (GTK_APPLICATION(app));
gtk_window_set_title (GTK_WINDOW (data->window), "File Manager");
gtk_window_set_default_size (GTK_WINDOW (data->window), 600, 400);
gtk_window_set_application(GTK_WINDOW(data->window),app);
}
buttons(app,data,data->dir);
g_print("Started\n");
}
int main (int argc, char **argv){
Appdata *data = g_new0(Appdata,1 );
data->app = gtk_application_new ("fun.kotyara.filemanager", G_APPLICATION_DEFAULT_FLAGS);
g_signal_connect (data->app, "activate", G_CALLBACK(app_activate),data);
int status = g_application_run (G_APPLICATION (data->app), argc, argv);
g_object_unref(data->app);
g_free(data);
return status;
}