diff --git a/main.c b/main.c index 956c111..43130e3 100644 --- a/main.c +++ b/main.c @@ -2,20 +2,32 @@ #include #include -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 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); - char dir = *label; - - GtkWidget *child = gtk_widget_get_first_child(GTK_WIDGET(user_data)); + // 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_grid_remove(user_data, 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){ @@ -26,8 +38,9 @@ int *check_dir(int *arr, DIR *d, char *directory){ if (d) { while ((dir = readdir(d)) != NULL){ char *name = dir->d_name; - if (*name == '.'){ - printf("directory starts with .\n"); + // temporary make them ' ' for get into previos dir + if (*name == ' '){ + printf("directory starts with . : %s\n", name); } else{ i += 1; @@ -44,7 +57,7 @@ int *check_dir(int *arr, DIR *d, char *directory){ } // 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 g; int i = 0; @@ -53,19 +66,17 @@ static void buttons (GtkApplication *app, gpointer *user_data,char *directory){ struct dirent *dir; // char directory[100]; 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(); 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; - if (*name == '.'){ - printf("directory starts with .\n"); + // temporary make them ' ' for get into previos dir + if (*name == ' '){ + printf("directory starts with . : %s\n", name); } else{ 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_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),grid); + g_signal_connect(btn, "clicked",G_CALLBACK(on_button_clicked),data); j++; if (j >= 10){ j = 0; @@ -92,22 +103,31 @@ static void buttons (GtkApplication *app, gpointer *user_data,char *directory){ } } closedir(d); - printf("set child and present window\n"); - gtk_window_set_child(GTK_WINDOW(window),grid); - gtk_window_present(GTK_WINDOW(window)); + 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){ - char directory[100]; - scanf("%100[^\n]", &directory); - buttons(app,user_data,directory); + 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){ - GtkApplication *app = gtk_application_new ("fun.kotyara.filemanager", G_APPLICATION_DEFAULT_FLAGS); - g_signal_connect (app, "activate", G_CALLBACK(app_activate),NULL); - int status = g_application_run (G_APPLICATION (app), argc, argv); - g_object_unref(app); + 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; }