add typedef struct and finally maked moving through directories
This commit is contained in:
70
main.c
70
main.c
@@ -2,20 +2,32 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
|
|
||||||
static void buttons (GtkApplication *app, gpointer *user_data, char *directory);
|
typedef struct{
|
||||||
|
GtkApplication *app;
|
||||||
|
GtkWidget *window;
|
||||||
|
char dir[256];
|
||||||
|
} Appdata;
|
||||||
|
|
||||||
|
static void buttons (GtkApplication *app, Appdata *data, char *directory);
|
||||||
|
|
||||||
// action on clicked button
|
// action on clicked button
|
||||||
static void on_button_clicked(GtkButton *btn, gpointer user_data){
|
static void on_button_clicked(GtkButton *btn, gpointer user_data){
|
||||||
|
Appdata *data = (Appdata *)user_data;
|
||||||
const char *label = gtk_button_get_label(btn);
|
const char *label = gtk_button_get_label(btn);
|
||||||
g_print("Button %s clicked\n",label);
|
g_print("Button %s clicked\n",label);
|
||||||
char dir = *label;
|
// remove child(grid buttons)
|
||||||
|
GtkWidget *child = gtk_widget_get_first_child(GTK_WIDGET(data->window));
|
||||||
GtkWidget *child = gtk_widget_get_first_child(GTK_WIDGET(user_data));
|
|
||||||
while (child != NULL) {
|
while (child != NULL) {
|
||||||
GtkWidget *next_child = gtk_widget_get_next_sibling(child);
|
GtkWidget *next_child = gtk_widget_get_next_sibling(child);
|
||||||
gtk_grid_remove(user_data, child);
|
gtk_window_set_child(GTK_WINDOW(data->window), NULL);
|
||||||
child = next_child;
|
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
|
// gets number of items and return value in arrays
|
||||||
int *check_dir(int *arr, DIR *d, char *directory){
|
int *check_dir(int *arr, DIR *d, char *directory){
|
||||||
@@ -26,8 +38,9 @@ int *check_dir(int *arr, DIR *d, char *directory){
|
|||||||
if (d) {
|
if (d) {
|
||||||
while ((dir = readdir(d)) != NULL){
|
while ((dir = readdir(d)) != NULL){
|
||||||
char *name = dir->d_name;
|
char *name = dir->d_name;
|
||||||
if (*name == '.'){
|
// temporary make them ' ' for get into previos dir
|
||||||
printf("directory starts with .\n");
|
if (*name == ' '){
|
||||||
|
printf("directory starts with . : %s\n", name);
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
i += 1;
|
i += 1;
|
||||||
@@ -44,7 +57,7 @@ int *check_dir(int *arr, DIR *d, char *directory){
|
|||||||
}
|
}
|
||||||
|
|
||||||
// present buttons
|
// present buttons
|
||||||
static void buttons (GtkApplication *app, gpointer *user_data,char *directory){
|
static void buttons (GtkApplication *app, Appdata *data,char *directory){
|
||||||
int p[2];
|
int p[2];
|
||||||
int g;
|
int g;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
@@ -53,19 +66,17 @@ static void buttons (GtkApplication *app, gpointer *user_data,char *directory){
|
|||||||
struct dirent *dir;
|
struct dirent *dir;
|
||||||
// char directory[100];
|
// char directory[100];
|
||||||
d = opendir(directory);
|
d = opendir(directory);
|
||||||
GtkWidget *window = gtk_application_window_new (GTK_APPLICATION(app));
|
|
||||||
gtk_window_set_title (GTK_WINDOW (window), "File Manager");
|
|
||||||
gtk_window_set_default_size (GTK_WINDOW (window), 600, 400);
|
|
||||||
gtk_window_set_application(GTK_WINDOW(window),app);
|
|
||||||
GtkWidget* grid = gtk_grid_new();
|
GtkWidget* grid = gtk_grid_new();
|
||||||
check_dir(p, d, directory);
|
check_dir(p, d, directory);
|
||||||
|
printf("directory: %s\n",directory);
|
||||||
printf("%i %i\n",p[0],p[1]);
|
printf("%i %i\n",p[0],p[1]);
|
||||||
g = 10;
|
g = 10;
|
||||||
while ((dir = readdir(d)) != NULL && i <=p[1] && j < g){
|
while ((dir = readdir(d)) != NULL && i <=p[1] && j < g){
|
||||||
if (d) {
|
if (d) {
|
||||||
char *name = dir->d_name;
|
char *name = dir->d_name;
|
||||||
if (*name == '.'){
|
// temporary make them ' ' for get into previos dir
|
||||||
printf("directory starts with .\n");
|
if (*name == ' '){
|
||||||
|
printf("directory starts with . : %s\n", name);
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
if (i == p[1]){
|
if (i == p[1]){
|
||||||
@@ -82,7 +93,7 @@ static void buttons (GtkApplication *app, gpointer *user_data,char *directory){
|
|||||||
gtk_grid_attach (GTK_GRID(grid), btn, j, i, 1,1);
|
gtk_grid_attach (GTK_GRID(grid), btn, j, i, 1,1);
|
||||||
gtk_widget_set_halign(btn, GTK_ALIGN_CENTER);
|
gtk_widget_set_halign(btn, GTK_ALIGN_CENTER);
|
||||||
gtk_widget_set_valign(btn, GTK_ALIGN_CENTER);
|
gtk_widget_set_valign(btn, GTK_ALIGN_CENTER);
|
||||||
g_signal_connect(btn, "clicked",G_CALLBACK(on_button_clicked),grid);
|
g_signal_connect(btn, "clicked",G_CALLBACK(on_button_clicked),data);
|
||||||
j++;
|
j++;
|
||||||
if (j >= 10){
|
if (j >= 10){
|
||||||
j = 0;
|
j = 0;
|
||||||
@@ -92,22 +103,31 @@ static void buttons (GtkApplication *app, gpointer *user_data,char *directory){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
closedir(d);
|
closedir(d);
|
||||||
printf("set child and present window\n");
|
gtk_window_set_child(GTK_WINDOW(data->window),grid);
|
||||||
gtk_window_set_child(GTK_WINDOW(window),grid);
|
gtk_window_present(GTK_WINDOW(data->window));
|
||||||
gtk_window_present(GTK_WINDOW(window));
|
printf("set child and present window %d\n", data->window);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void app_activate (GtkApplication *app, gpointer *user_data){
|
static void app_activate (GtkApplication *app, gpointer *user_data){
|
||||||
char directory[100];
|
Appdata *data = (Appdata *)user_data;
|
||||||
scanf("%100[^\n]", &directory);
|
scanf("%256[^\n]", &data->dir);
|
||||||
buttons(app,user_data,directory);
|
// 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");
|
g_print("Started\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
int main (int argc, char **argv){
|
int main (int argc, char **argv){
|
||||||
GtkApplication *app = gtk_application_new ("fun.kotyara.filemanager", G_APPLICATION_DEFAULT_FLAGS);
|
Appdata *data = g_new0(Appdata,1 );
|
||||||
g_signal_connect (app, "activate", G_CALLBACK(app_activate),NULL);
|
data->app = gtk_application_new ("fun.kotyara.filemanager", G_APPLICATION_DEFAULT_FLAGS);
|
||||||
int status = g_application_run (G_APPLICATION (app), argc, argv);
|
g_signal_connect (data->app, "activate", G_CALLBACK(app_activate),data);
|
||||||
g_object_unref(app);
|
int status = g_application_run (G_APPLICATION (data->app), argc, argv);
|
||||||
|
g_object_unref(data->app);
|
||||||
|
g_free(data);
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user